Skip to content

Commit

Permalink
Assign a locateFile to the Module that corrects the prefix
Browse files Browse the repository at this point in the history
Fix that prefix argument does not indicate the location where the script exists when dotnet.wasm is loaded
  • Loading branch information
yamachu committed Jun 28, 2022
1 parent d54b143 commit e7a473a
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/mono/wasm/runtime/es6/dotnet.es6.pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,31 @@ else {
throw new Error("MONO_WASM: Can't use moduleFactory callback of createDotnetRuntime function.")
}
var require = require || undefined;
var __dirname = __dirname || '';
var __dirname = __dirname || '';
if (Module["locateFile"] === undefined) {
// URL class is undefined on V8(ENVIRONMENT_IS_SHELL)
// To bypass assigning wasmBinaryFile variable in https://github.com/emscripten-core/emscripten/blob/4b5c4f0694174e081661944ab3ffdb43dd1949b8/src/preamble.js#L792-L793 ,
// pass implementation of locateFile to Module
Module["locateFile"] = function(path, prefix) {
// Constants for environment are defined later.
// https://github.com/emscripten-core/emscripten/blob/ae675c6abdd7e4a73dfc100b7cd258ef0cec01a2/src/shell.js#L93-L111
// emcc emits _scriptDir variable top-level, it's initialized by import.meta.url
if (ENVIRONMENT_IS_SHELL) {
// it's only used when load dotnet.wasm
if (prefix === "") {
return _scriptDir.slice(0, _scriptDir.lastIndexOf("/")) + "/" + path;
}
return prefix + path;
}
if (ENVIRONMENT_IS_NODE) {
// it's only used when load dotnet.wasm
if (prefix === "/") {
const fileExtensionTrimmedScriptDir = _scriptDir.replace(/^file:\/\//, "");
return fileExtensionTrimmedScriptDir.slice(0, fileExtensionTrimmedScriptDir.lastIndexOf("/")) + "/" + path;
}
return prefix + path;
}

return prefix + path;
}
}

0 comments on commit e7a473a

Please sign in to comment.