Skip to content
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

[mono][wasm] MacOSX Catalina Beta wasm workaround for booting mono wasm from Blazor. #13945

Closed
wants to merge 4 commits into from
Closed
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
28 changes: 25 additions & 3 deletions src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ export const monoPlatform: Platform = {
init: () => { },
};
// Emscripten works by expecting the module config to be a global
window['Module'] = createEmscriptenModuleInstance(loadAssemblyUrls, resolve, reject);

addScriptTagsToDocument();
// MacOSX Catalina initializes Module differently so needs a workaround for now.
addGlobalModuleScriptTagsToDocument(() => {
window['Module'] = createEmscriptenModuleInstance(loadAssemblyUrls, resolve, reject);
addScriptTagsToDocument();
});
});
},

Expand Down Expand Up @@ -205,6 +207,26 @@ function addScriptTagsToDocument() {
document.body.appendChild(scriptElem);
}

// Make sure we have the Module setup from the page due to a difference in MacOSX Catalina Beta.
// This may not be necassary in the Catalina release.
function addGlobalModuleScriptTagsToDocument(callback: () => void) {
const browserSupportsNativeWebAssembly = typeof WebAssembly !== 'undefined' && WebAssembly.validate;
if (!browserSupportsNativeWebAssembly) {
throw new Error('This browser does not support WebAssembly.');
}

const scriptElem = document.createElement('script');

// This pollutes global but is needed so it can be called from the script.
// The callback is put in the global scope so that it can be run after the script is loaded. onload cannot be used in this case
// for non-file scripts
window["__wasmmodulecallback__"] = callback;
scriptElem.type="text/javascript"
scriptElem.text = 'var Module = { onRuntimeInitialized: function () { console.log("Initialized"); }}; window["__wasmmodulecallback__"](); window["__wasmmodulecallback__"] = null;';

document.body.appendChild(scriptElem);
}

function createEmscriptenModuleInstance(loadAssemblyUrls: string[], onReady: () => void, onError: (reason?: any) => void) {
const module = {} as typeof Module;
const wasmBinaryFile = '_framework/wasm/mono.wasm';
Expand Down