forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-main.js
More file actions
406 lines (362 loc) · 16.4 KB
/
test-main.js
File metadata and controls
406 lines (362 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// -*- mode: js; js-indent-level: 4; -*-
//
// Run runtime tests under a JS shell or a browser
//
import { dotnet, exit } from './_framework/dotnet.js';
/*****************************************************************************
* Please don't use this as template for startup code.
* There are simpler and better samples like src\mono\sample\wasm\browser\main.js
* It has edge case polyfills.
* It handles strange things which happen with XHarness.
****************************************************************************/
//glue code to deal with the differences between chrome, ch, d8, jsc and sm.
const is_browser = typeof window != "undefined";
const is_node = !is_browser && typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';
if (is_node && process.versions.node.split(".")[0] < 14) {
throw new Error(`NodeJS at '${process.execPath}' has too low version '${process.versions.node}'`);
}
if (is_node) {
// the emscripten 3.1.34 stopped handling these when MODULARIZE is enabled
process.on('uncaughtException', function (ex) {
// ignore UnhandledPromiseRejection exceptions with exit status
if (ex !== 'unwind' && (ex.name !== "UnhandledPromiseRejection" || !ex.message.includes('"#<ExitStatus>"'))) {
throw ex;
}
});
}
if (!is_node && !is_browser && typeof globalThis.crypto === 'undefined') {
// **NOTE** this is a simple insecure polyfill for testing purposes only
// /dev/random doesn't work on js shells, so define our own
// See library_fs.js:createDefaultDevices ()
globalThis.crypto = {
getRandomValues: function (buffer) {
for (let i = 0; i < buffer.length; i++)
buffer[i] = (Math.random() * 256) | 0;
}
}
}
let v8args;
if (typeof arguments !== "undefined") {
// this must be captured in top level scope in V8
v8args = arguments;
}
async function getArgs() {
let queryArguments = [];
if (is_node) {
queryArguments = process.argv.slice(2);
} else if (is_browser) {
// We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters
const url = new URL(decodeURI(window.location));
let urlArguments = []
for (let param of url.searchParams) {
if (param[0] == "arg") {
urlArguments.push(param[1]);
}
}
queryArguments = urlArguments;
} else if (v8args !== undefined) {
queryArguments = Array.from(v8args);
} else if (typeof scriptArgs !== "undefined") {
queryArguments = Array.from(scriptArgs);
} else if (typeof WScript !== "undefined" && WScript.Arguments) {
queryArguments = Array.from(WScript.Arguments);
}
let runArgsJson;
// ToDo: runArgs should be read for all kinds of hosts, but
// fetch is added to node>=18 and current Windows's emcc node<18
if (is_browser) {
const response = await globalThis.fetch('./runArgs.json');
if (response.ok) {
runArgsJson = initRunArgs(await response.json());
} else {
console.debug(`could not load /runArgs.json: ${response.status}. Ignoring`);
}
}
if (!runArgsJson)
runArgsJson = initRunArgs({});
return processArguments(queryArguments, runArgsJson);
}
function initRunArgs(runArgs) {
// set defaults
runArgs.applicationArguments = runArgs.applicationArguments === undefined ? [] : runArgs.applicationArguments;
runArgs.profilers = runArgs.profilers === undefined ? [] : runArgs.profilers;
runArgs.workingDirectory = runArgs.workingDirectory === undefined ? '/' : runArgs.workingDirectory;
runArgs.environmentVariables = runArgs.environmentVariables === undefined ? {} : runArgs.environmentVariables;
runArgs.runtimeArgs = runArgs.runtimeArgs === undefined ? [] : runArgs.runtimeArgs;
runArgs.enableGC = runArgs.enableGC === undefined ? true : runArgs.enableGC;
runArgs.diagnosticTracing = runArgs.diagnosticTracing === undefined ? false : runArgs.diagnosticTracing;
runArgs.debugging = runArgs.debugging === undefined ? false : runArgs.debugging;
runArgs.configSrc = runArgs.configSrc === undefined ? './_framework/blazor.boot.json' : runArgs.configSrc;
// default'ing to true for tests, unless debugging
runArgs.forwardConsole = runArgs.forwardConsole === undefined ? !runArgs.debugging : runArgs.forwardConsole;
runArgs.memorySnapshot = runArgs.memorySnapshot === undefined ? true : runArgs.memorySnapshot;
return runArgs;
}
function processArguments(incomingArguments, runArgs) {
console.log("Incoming arguments: " + incomingArguments.join(' '));
while (incomingArguments && incomingArguments.length > 0) {
const currentArg = incomingArguments[0];
if (currentArg.startsWith("--profile=")) {
const arg = currentArg.substring("--profile=".length);
runArgs.profilers.push(arg);
} else if (currentArg.startsWith("--setenv=")) {
const arg = currentArg.substring("--setenv=".length);
const parts = arg.split('=');
if (parts.length != 2)
set_exit_code(1, "Error: malformed argument: '" + currentArg);
runArgs.environmentVariables[parts[0]] = parts[1];
} else if (currentArg.startsWith("--runtime-arg=")) {
const arg = currentArg.substring("--runtime-arg=".length);
runArgs.runtimeArgs.push(arg);
} else if (currentArg == "--disable-on-demand-gc") {
runArgs.enableGC = false;
} else if (currentArg == "--diagnostic-tracing") {
runArgs.diagnosticTracing = true;
} else if (currentArg.startsWith("--working-dir=")) {
const arg = currentArg.substring("--working-dir=".length);
runArgs.workingDirectory = arg;
} else if (currentArg == "--debug") {
runArgs.debugging = true;
} else if (currentArg == "--no-forward-console") {
runArgs.forwardConsole = false;
} else if (currentArg == "--no-memory-snapshot") {
runArgs.memorySnapshot = false;
} else if (currentArg.startsWith("--fetch-random-delay=")) {
const arg = currentArg.substring("--fetch-random-delay=".length);
if (is_browser) {
const delayms = Number.parseInt(arg) || 100;
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
// random sleep
const ms = delayms + (Math.random() * delayms);
console.log(`fetch ${url} started ${ms}`)
await new Promise(resolve => setTimeout(resolve, ms));
console.log(`fetch ${url} delayed ${ms}`)
const res = await originalFetch(url, options);
console.log(`fetch ${url} done ${ms}`)
return res;
}
} else {
console.warn("--fetch-random-delay only works on browser")
}
} else if (currentArg.startsWith("--config-src=")) {
const arg = currentArg.substring("--config-src=".length);
runArgs.configSrc = arg;
} else {
break;
}
incomingArguments = incomingArguments.slice(1);
}
runArgs.applicationArguments = incomingArguments;
// cheap way to let the testing infrastructure know we're running in a browser context (or not)
runArgs.environmentVariables["IsBrowserDomSupported"] = is_browser.toString().toLowerCase();
runArgs.environmentVariables["IsNodeJS"] = is_node.toString().toLowerCase();
return runArgs;
}
// we may have dependencies on NPM packages, depending on the test case
// some of them polyfill for browser built-in stuff
function loadNodeModules(config, require, modulesToLoad) {
modulesToLoad.split(',').forEach(module => {
const { 0: moduleName, 1: globalAlias } = module.split(':');
let message = `Loading npm '${moduleName}'`;
let moduleExport = require(moduleName);
if (globalAlias) {
message += ` and attaching to global as '${globalAlias}'`;
globalThis[globalAlias] = moduleExport;
} else if (moduleName == "node-fetch") {
message += ' and attaching to global';
globalThis.fetch = moduleExport.default;
globalThis.Headers = moduleExport.Headers;
globalThis.Request = moduleExport.Request;
globalThis.Response = moduleExport.Response;
} else if (moduleName == "node-abort-controller") {
message += ' and attaching to global';
globalThis.AbortController = moduleExport.AbortController;
}
console.log(message);
});
// Must be after loading npm modules.
config.environmentVariables["IsWebSocketSupported"] = ("WebSocket" in globalThis).toString().toLowerCase();
}
let mono_exit = (code, reason) => {
console.log(`test-main failed early ${code} ${reason}`);
};
const App = {
/** Runs a particular test in legacy interop tests
* @type {(method_name: string, args: any[]=, signature: any=) => return number}
*/
call_test_method: function (method_name, args, signature) {
// note: arguments here is the array of arguments passsed to this function
if ((arguments.length > 2) && (typeof (signature) !== "string"))
throw new Error("Invalid number of arguments for call_test_method");
const fqn = "[System.Runtime.InteropServices.JavaScript.Legacy.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:" + method_name;
try {
const method = App.runtime.BINDING.bind_static_method(fqn, signature);
return method.apply(null, args || []);
} catch (exc) {
console.error("exception thrown in", fqn);
throw exc;
}
},
create_function(...args) {
const code = args.pop();
const arg_count = args.length;
args.push("MONO");
args.push("BINDING");
args.push("INTERNAL");
const userFunction = new Function(...args, code);
return function (...args) {
args[arg_count + 0] = globalThis.App.runtime.MONO;
args[arg_count + 1] = globalThis.App.runtime.BINDING;
args[arg_count + 2] = globalThis.App.runtime.INTERNAL;
return userFunction(...args);
};
},
invoke_js(js_code) {
const closedEval = function (Module, MONO, BINDING, INTERNAL, code) {
return eval(code);
};
const res = closedEval(globalThis.App.runtime.Module, globalThis.App.runtime.MONO, globalThis.App.runtime.BINDING, globalThis.App.runtime.INTERNAL, js_code);
return (res === undefined || res === null || typeof res === "string")
? null
: res.toString();
}
};
globalThis.App = App; // Necessary as System.Runtime.InteropServices.JavaScript.Tests.MarshalTests (among others) call the App.call_test_method directly
function configureRuntime(dotnet, runArgs) {
dotnet
.withVirtualWorkingDirectory(runArgs.workingDirectory)
.withEnvironmentVariables(runArgs.environmentVariables)
.withDiagnosticTracing(runArgs.diagnosticTracing)
.withExitOnUnhandledError()
.withExitCodeLogging()
.withElementOnExit()
.withInteropCleanupOnExit()
.withAssertAfterExit()
.withConfig({
loadAllSatelliteResources: true
});
if (is_node) {
dotnet
.withEnvironmentVariable("NodeJSPlatform", process.platform)
.withAsyncFlushOnExit();
const modulesToLoad = runArgs.environmentVariables["NPM_MODULES"];
if (modulesToLoad) {
dotnet.withModuleConfig({
onConfigLoaded: (config, { INTERNAL }) => {
loadNodeModules(config, INTERNAL.require, modulesToLoad)
}
})
}
}
if (is_browser) {
if (runArgs.memorySnapshot) {
dotnet.withStartupMemoryCache(true);
}
dotnet.withEnvironmentVariable("IsWebSocketSupported", "true");
}
if (runArgs.runtimeArgs.length > 0) {
dotnet.withRuntimeOptions(runArgs.runtimeArgs);
}
if (runArgs.debugging) {
dotnet.withDebugging(-1);
dotnet.withWaitingForDebugger(-1);
}
if (runArgs.forwardConsole) {
dotnet.withConsoleForwarding();
}
}
async function dry_run(runArgs) {
try {
console.log("Silently starting separate runtime instance as another ES6 module to populate caches...");
// this separate instance of the ES6 module, in which we just populate the caches
const { dotnet } = await import('./_framework/dotnet.js?dry_run=true');
configureRuntime(dotnet, runArgs);
// silent minimal startup
await dotnet.withConfig({
forwardConsoleLogsToWS: false,
diagnosticTracing: false,
appendElementOnExit: false,
logExitCode: false,
pthreadPoolSize: 0,
// this just means to not continue startup after the snapshot is taken.
// If there was previously a matching snapshot, it will be used.
exitAfterSnapshot: true
}).create();
console.log("Separate runtime instance finished loading.");
} catch (err) {
if (err && err.status === 0) {
return true;
}
console.log("Separate runtime instance failed loading.", err);
return false;
}
return true;
}
async function run() {
try {
const runArgs = await getArgs();
console.log("Application arguments: " + runArgs.applicationArguments.join(' '));
if (is_browser && runArgs.memorySnapshot) {
const dryOk = await dry_run(runArgs);
if (!dryOk) {
mono_exit(1, "Failed during dry run");
return;
}
}
// this is subsequent run with the actual tests. It will use whatever was cached in the previous run.
// This way, we are testing that the cached version works.
mono_exit = exit;
if (runArgs.applicationArguments.length == 0) {
mono_exit(1, "Missing required --run argument");
return;
}
configureRuntime(dotnet, runArgs);
App.runtime = await dotnet.create();
App.runArgs = runArgs
console.info("Initializing dotnet version " + App.runtime.runtimeBuildInfo.productVersion + " commit hash " + App.runtime.runtimeBuildInfo.gitHash);
for (let i = 0; i < runArgs.profilers.length; ++i) {
const init = App.runtime.Module.cwrap('mono_wasm_load_profiler_' + runArgs.profilers[i], 'void', ['string']);
init("");
}
if (runArgs.applicationArguments[0] == "--regression") {
const exec_regression = App.runtime.Module.cwrap('mono_wasm_exec_regression', 'number', ['number', 'string']);
let res = 0;
try {
res = exec_regression(10, runArgs.applicationArguments[1]);
console.log("REGRESSION RESULT: " + res);
} catch (e) {
console.error("ABORT: " + e);
console.error(e.stack);
res = 1;
}
if (res) mono_exit(1, "REGRESSION TEST FAILED");
return;
}
if (runArgs.applicationArguments[0] == "--run") {
// Run an exe
if (runArgs.applicationArguments.length == 1) {
mono_exit(1, "Error: Missing main executable argument.");
return;
}
try {
const main_assembly_name = runArgs.applicationArguments[1];
const app_args = runArgs.applicationArguments.slice(2);
const result = await App.runtime.runMain(main_assembly_name, app_args);
console.log(`test-main.js exiting ${app_args.length > 1 ? main_assembly_name + " " + app_args[0] : main_assembly_name} with result ${result}`);
mono_exit(result);
} catch (error) {
if (error.name != "ExitStatus") {
mono_exit(1, error);
}
}
} else {
mono_exit(1, "Unhandled argument: " + runArgs.applicationArguments[0]);
}
} catch (err) {
mono_exit(1, err)
}
}
await run();