Skip to content

Commit 29db4a4

Browse files
authored
remove custom loader for .wasm file (#45611)
1 parent 42609e2 commit 29db4a4

File tree

2 files changed

+16
-49
lines changed

2 files changed

+16
-49
lines changed

src/Components/Web.JS/@types/dotnet/dotnet.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ declare type MonoConfigError = {
163163
message: string;
164164
error: any;
165165
};
166+
interface LoadingResource {
167+
name: string;
168+
url: string;
169+
response: Promise<Response>;
170+
}
166171
declare type AllAssetEntryTypes = AssetEntry | AssemblyEntry | SatelliteAssemblyEntry | VfsEntry | IcuData;
167172
declare type AssetEntry = {
168173
name: string;
@@ -172,6 +177,7 @@ declare type AssetEntry = {
172177
load_remote?: boolean;
173178
is_optional?: boolean;
174179
buffer?: ArrayBuffer;
180+
pendingDownload?: LoadingResource;
175181
};
176182
interface AssemblyEntry extends AssetEntry {
177183
name: "assembly";
@@ -188,13 +194,7 @@ interface IcuData extends AssetEntry {
188194
name: "icu";
189195
load_remote: boolean;
190196
}
191-
declare const enum AssetBehaviours {
192-
Resource = "resource",
193-
Assembly = "assembly",
194-
Heap = "heap",
195-
ICU = "icu",
196-
VFS = "vfs"
197-
}
197+
type AssetBehaviours = "resource" | "assembly" | "pdb" | "heap" | "icu" | "vfs" | "dotnetwasm" | "js-module-threads";
198198
declare const enum GlobalizationMode {
199199
ICU = "icu",
200200
INVARIANT = "invariant",

src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -307,22 +307,6 @@ async function createEmscriptenModuleInstance(resourceLoader: WebAssemblyResourc
307307
MONO = mono;
308308
IMPORTS = imports;
309309

310-
// Override the mechanism for fetching the main wasm file so we can connect it to our cache
311-
const instantiateWasm = (wasmImports, successCallback) => {
312-
(async () => {
313-
let compiledInstance: WebAssembly.Instance;
314-
try {
315-
const dotnetWasmResource = await wasmBeingLoaded;
316-
compiledInstance = await compileWasmModule(dotnetWasmResource, wasmImports);
317-
} catch (ex) {
318-
printErr((ex as Error).toString());
319-
throw ex;
320-
}
321-
successCallback(compiledInstance);
322-
})();
323-
return []; // No exports
324-
};
325-
326310
const onRuntimeInitialized = () => {
327311
if (!icuDataResource) {
328312
// Use invariant culture if the app does not carry icu data.
@@ -520,12 +504,20 @@ async function createEmscriptenModuleInstance(resourceLoader: WebAssemblyResourc
520504

521505
const dotnetModuleConfig: DotnetModuleConfig = {
522506
...moduleConfig,
507+
config: {
508+
assets: [
509+
{
510+
behavior: 'dotnetwasm',
511+
name: 'dotnet.wasm',
512+
pendingDownload: wasmBeingLoaded,
513+
},
514+
],
515+
} as any,
523516
disableDotnet6Compatibility: false,
524517
preRun: [preRun, ...existingPreRun],
525518
postRun: [postRun, ...existingPostRun],
526519
print,
527520
printErr,
528-
instantiateWasm,
529521
onRuntimeInitialized,
530522
};
531523

@@ -651,31 +643,6 @@ async function loadICUData(icuDataResource: LoadingResource): Promise<void> {
651643
Module.removeRunDependency(runDependencyId);
652644
}
653645

654-
async function compileWasmModule(wasmResource: LoadingResource, imports: any): Promise<WebAssembly.Instance> {
655-
const wasmResourceResponse = await wasmResource.response;
656-
657-
// The instantiateStreaming spec explicitly requires the following exact MIME type (with no trailing parameters, etc.)
658-
// https://webassembly.github.io/spec/web-api/#dom-webassembly-instantiatestreaming
659-
const hasWasmContentType = wasmResourceResponse.headers?.get('content-type') === 'application/wasm';
660-
661-
if (hasWasmContentType && typeof WebAssembly.instantiateStreaming === 'function') {
662-
// We can use streaming compilation. We know this shouldn't fail due to the content-type header being wrong,
663-
// as we already just checked that. So if this fails for some other reason we'll treat it as fatal.
664-
const streamingResult = await WebAssembly.instantiateStreaming(wasmResourceResponse, imports);
665-
return streamingResult.instance;
666-
} else {
667-
if (!hasWasmContentType) {
668-
// In most cases the developer should fix this. It's unusual enough that we don't mind logging a warning each time.
669-
console.warn('WebAssembly resource does not have the expected content type "application/wasm", so falling back to slower ArrayBuffer instantiation.');
670-
}
671-
672-
// Fall back on ArrayBuffer instantiation.
673-
const arrayBuffer = await wasmResourceResponse.arrayBuffer();
674-
const arrayBufferResult = await WebAssembly.instantiate(arrayBuffer, imports);
675-
return arrayBufferResult.instance;
676-
}
677-
}
678-
679646
function changeExtension(filename: string, newExtensionWithLeadingDot: string) {
680647
const lastDotIndex = filename.lastIndexOf('.');
681648
if (lastDotIndex < 0) {

0 commit comments

Comments
 (0)