Skip to content

[blazor] remove custom loader for .wasm file #45611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Components/Web.JS/@types/dotnet/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ declare type MonoConfigError = {
message: string;
error: any;
};
interface LoadingResource {
name: string;
url: string;
response: Promise<Response>;
}
declare type AllAssetEntryTypes = AssetEntry | AssemblyEntry | SatelliteAssemblyEntry | VfsEntry | IcuData;
declare type AssetEntry = {
name: string;
Expand All @@ -172,6 +177,7 @@ declare type AssetEntry = {
load_remote?: boolean;
is_optional?: boolean;
buffer?: ArrayBuffer;
pendingDownload?: LoadingResource;
};
interface AssemblyEntry extends AssetEntry {
name: "assembly";
Expand All @@ -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",
Expand Down
51 changes: 9 additions & 42 deletions src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
};

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

async function compileWasmModule(wasmResource: LoadingResource, imports: any): Promise<WebAssembly.Instance> {
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) {
Expand Down