|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | import type { LoadBootResourceCallback, JsModuleExports, JsAsset, AssemblyAsset, PdbAsset, WasmAsset, IcuAsset, EmscriptenModuleInternal, InstantiateWasmSuccessCallback } from "./types"; |
| 5 | + |
5 | 6 | import { dotnetAssert, dotnetGetInternals, dotnetBrowserHostExports, dotnetUpdateInternals } from "./cross-module"; |
6 | | -import { ENVIRONMENT_IS_NODE } from "./per-module"; |
7 | 7 | import { getIcuResourceName } from "./icu"; |
8 | 8 | import { getLoaderConfig } from "./config"; |
9 | 9 | import { BrowserHost_InitializeCoreCLR } from "./run"; |
10 | 10 | import { createPromiseCompletionSource } from "./promise-completion-source"; |
11 | 11 | import { locateFile } from "./bootstrap"; |
| 12 | +import { fetchLike } from "./polyfills"; |
12 | 13 |
|
13 | 14 | const nativeModulePromiseController = createPromiseCompletionSource<EmscriptenModuleInternal>(() => { |
14 | 15 | dotnetUpdateInternals(dotnetGetInternals()); |
15 | 16 | }); |
16 | | -let wasmBinaryPromise: Promise<Response> = undefined as any; |
| 17 | +let wasmBinaryPromise: any = undefined; |
17 | 18 |
|
18 | 19 | // WASM-TODO: retry logic |
19 | 20 | // WASM-TODO: throttling logic |
20 | 21 | // WASM-TODO: invokeLibraryInitializers |
21 | 22 | // WASM-TODO: webCIL |
22 | 23 | // WASM-TODO: downloadOnly - blazor render mode auto pre-download. Really no start. |
| 24 | +// WASM-TODO: no-cache, force-cache, integrity |
| 25 | +// WASM-TODO: LoadBootResourceCallback |
| 26 | +// WASM-TODO: fail fast for missing WASM features - SIMD, EH, BigInt detection |
23 | 27 |
|
24 | 28 | export async function createRuntime(downloadOnly: boolean, loadBootResource?: LoadBootResourceCallback): Promise<any> { |
25 | 29 | if (loadBootResource) throw new Error("TODO: loadBootResource is not implemented yet"); |
@@ -76,11 +80,18 @@ function fetchWasm(asset: WasmAsset): Promise<Response> { |
76 | 80 | } |
77 | 81 |
|
78 | 82 | export async function instantiateWasm(imports: WebAssembly.Imports, successCallback: InstantiateWasmSuccessCallback): Promise<void> { |
79 | | - const res = await WebAssembly.instantiateStreaming(wasmBinaryPromise, imports); |
80 | | - successCallback(res.instance, res.module); |
| 83 | + if (wasmBinaryPromise instanceof globalThis.Response === false || !WebAssembly.instantiateStreaming) { |
| 84 | + const res = await wasmBinaryPromise; |
| 85 | + const data = await res.arrayBuffer(); |
| 86 | + const module = await WebAssembly.compile(data); |
| 87 | + const instance = await WebAssembly.instantiate(module, imports); |
| 88 | + successCallback(instance, module); |
| 89 | + } else { |
| 90 | + const res = await WebAssembly.instantiateStreaming(wasmBinaryPromise, imports); |
| 91 | + successCallback(res.instance, res.module); |
| 92 | + } |
81 | 93 | } |
82 | 94 |
|
83 | | - |
84 | 95 | async function fetchIcu(asset: IcuAsset): Promise<void> { |
85 | 96 | if (asset.name && !asset.resolvedUrl) { |
86 | 97 | asset.resolvedUrl = locateFile(asset.name); |
@@ -116,36 +127,3 @@ async function fetchBytes(asset: WasmAsset | AssemblyAsset | PdbAsset | IcuAsset |
116 | 127 | const buffer = await response.arrayBuffer(); |
117 | 128 | return new Uint8Array(buffer); |
118 | 129 | } |
119 | | - |
120 | | -async function fetchLike(resolvedUrl: string): Promise<Response> { |
121 | | - dotnetAssert.check(resolvedUrl, "Bad resolvedUrl"); |
122 | | - if (ENVIRONMENT_IS_NODE) { |
123 | | - const { promises: fs } = await import(/*! webpackIgnore: true */"fs"); |
124 | | - const { fileURLToPath } = await import(/*! webpackIgnore: true */"url"); |
125 | | - const isFileUrl = resolvedUrl.startsWith("file://"); |
126 | | - if (isFileUrl) { |
127 | | - resolvedUrl = fileURLToPath(resolvedUrl); |
128 | | - } |
129 | | - const arrayBuffer = await fs.readFile(resolvedUrl) as any; |
130 | | - return <Response><any>{ |
131 | | - ok: true, |
132 | | - headers: { |
133 | | - length: 0, |
134 | | - get: () => null |
135 | | - }, |
136 | | - url: resolvedUrl, |
137 | | - arrayBuffer: () => arrayBuffer, |
138 | | - json: () => JSON.parse(arrayBuffer), |
139 | | - text: () => { |
140 | | - throw new Error("NotImplementedException"); |
141 | | - } |
142 | | - }; |
143 | | - } else { |
144 | | - const response = await fetch(resolvedUrl); |
145 | | - if (!response.ok) { |
146 | | - throw new Error(`Failed to load ${resolvedUrl} with ${response.status} ${response.statusText}`); |
147 | | - } |
148 | | - return response; |
149 | | - } |
150 | | - |
151 | | -} |
0 commit comments