Skip to content

Commit 0338b25

Browse files
authored
[wasm] load dotnet.js with import in tests (#62415)
* load dotnet.js with import in tests * fix proxy fail and handle errors of this type * feedback
1 parent 9e8e4fc commit 0338b25

File tree

3 files changed

+41
-50
lines changed

3 files changed

+41
-50
lines changed

src/mono/wasm/runtime/cjs/dotnet.cjs.lib.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ const DotnetSupportLib = {
1515
__dotnet_replacements);
1616
1717
// here we replace things which are not exposed in another way
18-
__dirname = scriptDirectory = __dotnet_replacements.scriptDirectory;
18+
scriptDirectory = __dotnet_replacements.scriptDirectory;
1919
readAsync = __dotnet_replacements.readAsync;
20-
var fetch = __dotnet_replacements.fetch;`,
20+
var fetch = __dotnet_replacements.fetch;
21+
if (ENVIRONMENT_IS_NODE) {
22+
__dirname = __dotnet_replacements.scriptDirectory;
23+
}
24+
`,
2125
};
2226

2327
// the methods would be visible to EMCC linker

src/mono/wasm/runtime/es6/dotnet.es6.lib.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ const DotnetSupportLib = {
1515
__dotnet_replacements);
1616
1717
// here we replace things which are not exposed in another way
18-
__dirname = scriptDirectory = __dotnet_replacements.scriptDirectory;
18+
scriptDirectory = __dotnet_replacements.scriptDirectory;
1919
readAsync = __dotnet_replacements.readAsync;
2020
var fetch = __dotnet_replacements.fetch;
2121
2222
// here we replace things which are broken on NodeJS for ES6
2323
if (ENVIRONMENT_IS_NODE) {
24+
__dirname = __dotnet_replacements.scriptDirectory;
2425
getBinaryPromise = async () => {
2526
if (!wasmBinary) {
2627
try {

src/mono/wasm/test-main.js

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,45 @@ const originalConsole = {
2222
error: console.error
2323
};
2424

25-
let isXUnitDoneCheck = false;
26-
let isXmlDoneCheck = false;
27-
28-
function proxyMethod(prefix, func, asJson) {
25+
function proxyConsoleMethod(prefix, func, asJson) {
2926
return function () {
30-
const args = [...arguments];
31-
let payload = args[0];
32-
if (payload === undefined) payload = 'undefined';
33-
else if (payload === null) payload = 'null';
34-
else if (typeof payload === 'function') payload = payload.toString();
35-
else if (typeof payload !== 'string') {
36-
try {
37-
payload = JSON.stringify(payload);
38-
} catch (e) {
39-
payload = payload.toString();
27+
try {
28+
const args = [...arguments];
29+
let payload = args[0];
30+
if (payload === undefined) payload = 'undefined';
31+
else if (payload === null) payload = 'null';
32+
else if (typeof payload === 'function') payload = payload.toString();
33+
else if (typeof payload !== 'string') {
34+
try {
35+
payload = JSON.stringify(payload);
36+
} catch (e) {
37+
payload = payload.toString();
38+
}
4039
}
41-
}
42-
if (payload.indexOf("=== TEST EXECUTION SUMMARY ===") != -1) {
43-
isXUnitDoneCheck = true;
44-
}
4540

46-
if (payload.startsWith("STARTRESULTXML")) {
47-
originalConsole.log('Sending RESULTXML')
48-
isXmlDoneCheck = true;
49-
func(payload);
50-
}
51-
else if (asJson) {
52-
func(JSON.stringify({
53-
method: prefix,
54-
payload: payload,
55-
arguments: args
56-
}));
57-
} else {
58-
func([prefix + payload, ...args.slice(1)]);
41+
if (payload.startsWith("STARTRESULTXML")) {
42+
originalConsole.log('Sending RESULTXML')
43+
func(payload);
44+
}
45+
else if (asJson) {
46+
func(JSON.stringify({
47+
method: prefix,
48+
payload: payload,
49+
arguments: args
50+
}));
51+
} else {
52+
func([prefix + payload, ...args.slice(1)]);
53+
}
54+
} catch (err) {
55+
originalConsole.error(`proxyConsole failed: ${err}`)
5956
}
6057
};
6158
};
6259

6360
const methods = ["debug", "trace", "warn", "info", "error"];
6461
for (let m of methods) {
6562
if (typeof (console[m]) !== "function") {
66-
console[m] = proxyMethod(`console.${m}: `, console.log, false);
63+
console[m] = proxyConsoleMethod(`console.${m}: `, console.log, false);
6764
}
6865
}
6966

@@ -94,7 +91,7 @@ if (is_browser) {
9491

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

10097
if (typeof globalThis.crypto === 'undefined') {
@@ -369,20 +366,9 @@ async function loadDotnet(file) {
369366
};
370367
} else if (is_browser) { // vanila JS in browser
371368
loadScript = async function (file) {
372-
const script = document.createElement("script");
373-
script.src = file;
374-
document.head.appendChild(script);
375-
let timeout = 100;
376-
// bysy spin waiting for script to load into global namespace
377-
while (timeout > 0) {
378-
if (globalThis.createDotnetRuntime) {
379-
return globalThis.createDotnetRuntime;
380-
}
381-
// delay 10ms
382-
await new Promise(resolve => setTimeout(resolve, 10));
383-
timeout--;
384-
}
385-
throw new Error("Can't load " + file);
369+
globalThis.exports = {}; // if we are loading cjs file
370+
const createDotnetRuntime = await import(file);
371+
return typeof createDotnetRuntime === "function" ? createDotnetRuntime : globalThis.exports.createDotnetRuntime;
386372
}
387373
}
388374
else if (typeof globalThis.load !== 'undefined') {

0 commit comments

Comments
 (0)