diff --git a/src/Components/Web.JS/@types/dotnet/dotnet.d.ts b/src/Components/Web.JS/@types/dotnet/dotnet.d.ts index 0e7590eadfdd..71be4d4b3e5b 100644 --- a/src/Components/Web.JS/@types/dotnet/dotnet.d.ts +++ b/src/Components/Web.JS/@types/dotnet/dotnet.d.ts @@ -163,6 +163,11 @@ declare type MonoConfigError = { message: string; error: any; }; +interface LoadingResource { + name: string; + url: string; + response: Promise; +} declare type AllAssetEntryTypes = AssetEntry | AssemblyEntry | SatelliteAssemblyEntry | VfsEntry | IcuData; declare type AssetEntry = { name: string; @@ -172,6 +177,7 @@ declare type AssetEntry = { load_remote?: boolean; is_optional?: boolean; buffer?: ArrayBuffer; + pendingDownload?: LoadingResource; }; interface AssemblyEntry extends AssetEntry { name: "assembly"; @@ -188,13 +194,7 @@ interface IcuData extends AssetEntry { name: "icu"; load_remote: boolean; } -declare const enum AssetBehaviours { - Resource = "resource", - Assembly = "assembly", - Heap = "heap", - ICU = "icu", - VFS = "vfs" -} +type AssetBehaviours = "resource" | "assembly" | "pdb" | "heap" | "icu" | "vfs" | "dotnetwasm" | "js-module-threads"; declare const enum GlobalizationMode { ICU = "icu", INVARIANT = "invariant", diff --git a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts index 03c2a5d90f75..b25e7336de22 100644 --- a/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts +++ b/src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts @@ -307,22 +307,6 @@ async function createEmscriptenModuleInstance(resourceLoader: WebAssemblyResourc MONO = mono; IMPORTS = imports; - // Override the mechanism for fetching the main wasm file so we can connect it to our cache - const instantiateWasm = (wasmImports, successCallback) => { - (async () => { - let compiledInstance: WebAssembly.Instance; - try { - const dotnetWasmResource = await wasmBeingLoaded; - compiledInstance = await compileWasmModule(dotnetWasmResource, wasmImports); - } catch (ex) { - printErr((ex as Error).toString()); - throw ex; - } - successCallback(compiledInstance); - })(); - return []; // No exports - }; - const onRuntimeInitialized = () => { if (!icuDataResource) { // Use invariant culture if the app does not carry icu data. @@ -520,12 +504,20 @@ async function createEmscriptenModuleInstance(resourceLoader: WebAssemblyResourc const dotnetModuleConfig: DotnetModuleConfig = { ...moduleConfig, + config: { + assets: [ + { + behavior: 'dotnetwasm', + name: 'dotnet.wasm', + pendingDownload: wasmBeingLoaded, + }, + ], + } as any, disableDotnet6Compatibility: false, preRun: [preRun, ...existingPreRun], postRun: [postRun, ...existingPostRun], print, printErr, - instantiateWasm, onRuntimeInitialized, }; @@ -651,31 +643,6 @@ async function loadICUData(icuDataResource: LoadingResource): Promise { Module.removeRunDependency(runDependencyId); } -async function compileWasmModule(wasmResource: LoadingResource, imports: any): Promise { - const wasmResourceResponse = await wasmResource.response; - - // The instantiateStreaming spec explicitly requires the following exact MIME type (with no trailing parameters, etc.) - // https://webassembly.github.io/spec/web-api/#dom-webassembly-instantiatestreaming - const hasWasmContentType = wasmResourceResponse.headers?.get('content-type') === 'application/wasm'; - - if (hasWasmContentType && typeof WebAssembly.instantiateStreaming === 'function') { - // We can use streaming compilation. We know this shouldn't fail due to the content-type header being wrong, - // as we already just checked that. So if this fails for some other reason we'll treat it as fatal. - const streamingResult = await WebAssembly.instantiateStreaming(wasmResourceResponse, imports); - return streamingResult.instance; - } else { - if (!hasWasmContentType) { - // In most cases the developer should fix this. It's unusual enough that we don't mind logging a warning each time. - console.warn('WebAssembly resource does not have the expected content type "application/wasm", so falling back to slower ArrayBuffer instantiation.'); - } - - // Fall back on ArrayBuffer instantiation. - const arrayBuffer = await wasmResourceResponse.arrayBuffer(); - const arrayBufferResult = await WebAssembly.instantiate(arrayBuffer, imports); - return arrayBufferResult.instance; - } -} - function changeExtension(filename: string, newExtensionWithLeadingDot: string) { const lastDotIndex = filename.lastIndexOf('.'); if (lastDotIndex < 0) {