Skip to content

Commit

Permalink
[wasm] load dotnet.js with import in tests (dotnet#62415)
Browse files Browse the repository at this point in the history
* load dotnet.js with import in tests
* fix proxy fail and handle errors of this type
* feedback
  • Loading branch information
pavelsavara committed Dec 6, 2021
1 parent 9e8e4fc commit 0338b25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
8 changes: 6 additions & 2 deletions src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ const DotnetSupportLib = {
__dotnet_replacements);
// here we replace things which are not exposed in another way
__dirname = scriptDirectory = __dotnet_replacements.scriptDirectory;
scriptDirectory = __dotnet_replacements.scriptDirectory;
readAsync = __dotnet_replacements.readAsync;
var fetch = __dotnet_replacements.fetch;`,
var fetch = __dotnet_replacements.fetch;
if (ENVIRONMENT_IS_NODE) {
__dirname = __dotnet_replacements.scriptDirectory;
}
`,
};

// the methods would be visible to EMCC linker
Expand Down
3 changes: 2 additions & 1 deletion src/mono/wasm/runtime/es6/dotnet.es6.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ const DotnetSupportLib = {
__dotnet_replacements);
// here we replace things which are not exposed in another way
__dirname = scriptDirectory = __dotnet_replacements.scriptDirectory;
scriptDirectory = __dotnet_replacements.scriptDirectory;
readAsync = __dotnet_replacements.readAsync;
var fetch = __dotnet_replacements.fetch;
// here we replace things which are broken on NodeJS for ES6
if (ENVIRONMENT_IS_NODE) {
__dirname = __dotnet_replacements.scriptDirectory;
getBinaryPromise = async () => {
if (!wasmBinary) {
try {
Expand Down
80 changes: 33 additions & 47 deletions src/mono/wasm/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,45 @@ const originalConsole = {
error: console.error
};

let isXUnitDoneCheck = false;
let isXmlDoneCheck = false;

function proxyMethod(prefix, func, asJson) {
function proxyConsoleMethod(prefix, func, asJson) {
return function () {
const args = [...arguments];
let payload = args[0];
if (payload === undefined) payload = 'undefined';
else if (payload === null) payload = 'null';
else if (typeof payload === 'function') payload = payload.toString();
else if (typeof payload !== 'string') {
try {
payload = JSON.stringify(payload);
} catch (e) {
payload = payload.toString();
try {
const args = [...arguments];
let payload = args[0];
if (payload === undefined) payload = 'undefined';
else if (payload === null) payload = 'null';
else if (typeof payload === 'function') payload = payload.toString();
else if (typeof payload !== 'string') {
try {
payload = JSON.stringify(payload);
} catch (e) {
payload = payload.toString();
}
}
}
if (payload.indexOf("=== TEST EXECUTION SUMMARY ===") != -1) {
isXUnitDoneCheck = true;
}

if (payload.startsWith("STARTRESULTXML")) {
originalConsole.log('Sending RESULTXML')
isXmlDoneCheck = true;
func(payload);
}
else if (asJson) {
func(JSON.stringify({
method: prefix,
payload: payload,
arguments: args
}));
} else {
func([prefix + payload, ...args.slice(1)]);
if (payload.startsWith("STARTRESULTXML")) {
originalConsole.log('Sending RESULTXML')
func(payload);
}
else if (asJson) {
func(JSON.stringify({
method: prefix,
payload: payload,
arguments: args
}));
} else {
func([prefix + payload, ...args.slice(1)]);
}
} catch (err) {
originalConsole.error(`proxyConsole failed: ${err}`)
}
};
};

const methods = ["debug", "trace", "warn", "info", "error"];
for (let m of methods) {
if (typeof (console[m]) !== "function") {
console[m] = proxyMethod(`console.${m}: `, console.log, false);
console[m] = proxyConsoleMethod(`console.${m}: `, console.log, false);
}
}

Expand Down Expand Up @@ -94,7 +91,7 @@ if (is_browser) {

// redirect output early, so that when emscripten starts it's already redirected
for (let m of ["log", ...methods])
console[m] = proxyMethod(`console.${m}`, send, true);
console[m] = proxyConsoleMethod(`console.${m}`, send, true);
}

if (typeof globalThis.crypto === 'undefined') {
Expand Down Expand Up @@ -369,20 +366,9 @@ async function loadDotnet(file) {
};
} else if (is_browser) { // vanila JS in browser
loadScript = async function (file) {
const script = document.createElement("script");
script.src = file;
document.head.appendChild(script);
let timeout = 100;
// bysy spin waiting for script to load into global namespace
while (timeout > 0) {
if (globalThis.createDotnetRuntime) {
return globalThis.createDotnetRuntime;
}
// delay 10ms
await new Promise(resolve => setTimeout(resolve, 10));
timeout--;
}
throw new Error("Can't load " + file);
globalThis.exports = {}; // if we are loading cjs file
const createDotnetRuntime = await import(file);
return typeof createDotnetRuntime === "function" ? createDotnetRuntime : globalThis.exports.createDotnetRuntime;
}
}
else if (typeof globalThis.load !== 'undefined') {
Expand Down

0 comments on commit 0338b25

Please sign in to comment.