forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruntime-test.js
More file actions
397 lines (345 loc) · 11.1 KB
/
runtime-test.js
File metadata and controls
397 lines (345 loc) · 11.1 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
// 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
//
"use strict";
//glue code to deal with the differences between chrome, ch, d8, jsc and sm.
var is_browser = typeof window != "undefined";
// if the engine doesn't provide a console
if (typeof (console) === "undefined") {
var console = {
log: globalThis.print,
clear: function () { }
};
}
globalThis.testConsole = console;
//define arguments for later
var allRuntimeArguments = null;
try {
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));
allRuntimeArguments = [];
for (let param of url.searchParams) {
if (param[0] == "arg") {
allRuntimeArguments.push(param[1]);
}
}
} else if (typeof arguments !== "undefined" && typeof arguments !== "null") {
allRuntimeArguments = arguments;
} else if (typeof process !== 'undefined' && typeof process.argv !== "undefined") {
allRuntimeArguments = process.argv.slice(2);
} else if (typeof scriptArgs !== "undefined") {
allRuntimeArguments = scriptArgs;
} else if (typeof WScript !== "undefined" && WScript.Arguments) {
allRuntimeArguments = WScript.Arguments;
} else {
allRuntimeArguments = [];
}
} catch (e) {
console.log(e);
}
function proxyMethod (prefix, func, asJson) {
return function() {
const args = [...arguments];
var 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 (asJson) {
func (JSON.stringify({
method: prefix,
payload: payload,
arguments: args
}));
} else {
func([prefix + payload, ...args.slice(1)]);
}
};
};
var methods = ["debug", "trace", "warn", "info", "error"];
for (var m of methods) {
if (typeof(console[m]) != "function") {
console[m] = proxyMethod(`console.${m}: `, console.log, false);
}
}
function proxyJson (func) {
for (var m of ["log", ...methods])
console[m] = proxyMethod(`console.${m}`,func, true);
}
if (is_browser) {
const consoleUrl = `${window.location.origin}/console`.replace('http://', 'ws://');
let consoleWebSocket = new WebSocket(consoleUrl);
consoleWebSocket.onopen = function(event) {
proxyJson(function (msg) { consoleWebSocket.send (msg); });
globalThis.testConsole.log("browser: Console websocket connected.");
};
consoleWebSocket.onerror = function(event) {
console.log(`websocket error: ${event}`);
};
}
//proxyJson(console.log);
let print = globalThis.testConsole.log;
let printErr = globalThis.testConsole.error;
if (typeof 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 ()
var crypto = {
getRandomValues: function (buffer) {
for (var i = 0; i < buffer.length; i++)
buffer [i] = (Math.random () * 256) | 0;
}
}
}
if (typeof performance == 'undefined') {
// performance.now() is used by emscripten and doesn't work in JSC
var performance = {
now: function () {
return Date.now ();
}
}
}
//end of all the nice shell glue code.
function test_exit (exit_code) {
if (is_browser) {
// Notify the selenium script
Module.exit_code = exit_code;
Module.print ("WASM EXIT " + exit_code);
var tests_done_elem = document.createElement ("label");
tests_done_elem.id = "tests_done";
tests_done_elem.innerHTML = exit_code.toString ();
document.body.appendChild (tests_done_elem);
} else {
Module.wasm_exit (exit_code);
}
}
function fail_exec (reason) {
Module.print (reason);
test_exit (1);
}
function inspect_object (o) {
var r = "";
for(var p in o) {
var t = typeof o[p];
r += "'" + p + "' => '" + t + "', ";
}
return r;
}
// Preprocess arguments
console.info("Arguments: " + allRuntimeArguments);
var profilers = [];
var setenv = {};
var runtime_args = [];
var enable_gc = true;
var enable_zoneinfo = false;
var working_dir='/';
while (allRuntimeArguments !== undefined && allRuntimeArguments.length > 0) {
if (allRuntimeArguments [0].startsWith ("--profile=")) {
var arg = allRuntimeArguments [0].substring ("--profile=".length);
profilers.push (arg);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--setenv=")) {
var arg = allRuntimeArguments [0].substring ("--setenv=".length);
var parts = arg.split ('=');
if (parts.length != 2)
fail_exec ("Error: malformed argument: '" + allRuntimeArguments [0]);
setenv [parts [0]] = parts [1];
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--runtime-arg=")) {
var arg = allRuntimeArguments [0].substring ("--runtime-arg=".length);
runtime_args = allRuntimeArguments.push (arg);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0] == "--disable-on-demand-gc") {
enable_gc = false;
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--working-dir=")) {
var arg = allRuntimeArguments [0].substring ("--working-dir=".length);
working_dir = arg;
allRuntimeArguments = allRuntimeArguments.slice (1);
} else {
break;
}
}
// cheap way to let the testing infrastructure know we're running in a browser context (or not)
setenv["IsBrowserDomSupported"] = is_browser.toString().toLowerCase();
function writeContentToFile(content, path)
{
var stream = FS.open(path, 'w+');
FS.write(stream, content, 0, content.length, 0);
FS.close(stream);
}
function loadScript (url)
{
if (is_browser) {
var script = document.createElement ("script");
script.src = url;
document.head.appendChild (script);
} else {
load (url);
}
}
var Module = {
mainScriptUrlOrBlob: "dotnet.js",
config: null,
print,
printErr,
preInit: async function() {
await MONO.mono_wasm_load_config("./mono-config.json"); // sets Module.config implicitly
},
onAbort: function(x) {
print ("ABORT: " + x);
var err = new Error();
print ("Stacktrace: \n");
print (err.stack);
test_exit (1);
},
onRuntimeInitialized: function () {
// Have to set env vars here to enable setting MONO_LOG_LEVEL etc.
for (var variable in setenv) {
MONO.mono_wasm_setenv (variable, setenv [variable]);
}
if (!enable_gc) {
Module.ccall ('mono_wasm_enable_on_demand_gc', 'void', ['number'], [0]);
}
Module.config.loaded_cb = function () {
let wds = FS.stat (working_dir);
if (wds === undefined || !FS.isDir (wds.mode)) {
fail_exec (`Could not find working directory ${working_dir}`);
return;
}
FS.chdir (working_dir);
App.init ();
};
Module.config.fetch_file_cb = function (asset) {
// console.log("fetch_file_cb('" + asset + "')");
// for testing purposes add BCL assets to VFS until we special case File.Open
// to identify when an assembly from the BCL is being open and resolve it correctly.
/*
var content = new Uint8Array (read (asset, 'binary'));
var path = asset.substr(Module.config.deploy_prefix.length);
writeContentToFile(content, path);
*/
if (typeof window != 'undefined') {
return fetch (asset, { credentials: 'same-origin' });
} else {
// The default mono_load_runtime_and_bcl defaults to using
// fetch to load the assets. It also provides a way to set a
// fetch promise callback.
// Here we wrap the file read in a promise and fake a fetch response
// structure.
return new Promise ((resolve, reject) => {
var bytes = null, error = null;
try {
bytes = read (asset, 'binary');
} catch (exc) {
console.log('v8 file read failed ' + asset + ' ' + exc)
error = exc;
}
var response = { ok: (bytes && !error), url: asset,
arrayBuffer: function () {
return new Promise ((resolve2, reject2) => {
if (error)
reject2 (error);
else
resolve2 (new Uint8Array (bytes));
}
)}
}
resolve (response);
})
}
};
MONO.mono_load_runtime_and_bcl_args (Module.config);
},
};
loadScript ("dotnet.js");
const IGNORE_PARAM_COUNT = -1;
var App = {
init: function () {
var wasm_set_main_args = Module.cwrap ('mono_wasm_set_main_args', 'void', ['number', 'number']);
var wasm_strdup = Module.cwrap ('mono_wasm_strdup', 'number', ['string']);
Module.wasm_exit = Module.cwrap ('mono_wasm_exit', 'void', ['number']);
console.info("Initializing.....");
for (var i = 0; i < profilers.length; ++i) {
var init = Module.cwrap ('mono_wasm_load_profiler_' + profilers [i], 'void', ['string'])
init ("");
}
if (allRuntimeArguments.length == 0) {
fail_exec ("Missing required --run argument");
return;
}
if (allRuntimeArguments[0] == "--regression") {
var exec_regression = Module.cwrap ('mono_wasm_exec_regression', 'number', ['number', 'string'])
var res = 0;
try {
res = exec_regression (10, allRuntimeArguments[1]);
Module.print ("REGRESSION RESULT: " + res);
} catch (e) {
Module.print ("ABORT: " + e);
print (e.stack);
res = 1;
}
if (res)
fail_exec ("REGRESSION TEST FAILED");
return;
}
if (runtime_args.length > 0)
MONO.mono_wasm_set_runtime_options (runtime_args);
if (allRuntimeArguments[0] == "--run") {
// Run an exe
if (allRuntimeArguments.length == 1) {
fail_exec ("Error: Missing main executable argument.");
return;
}
var main_assembly_name = allRuntimeArguments[1];
var app_args = allRuntimeArguments.slice (2);
var main_argc = allRuntimeArguments.length - 2 + 1;
var main_argv = Module._malloc (main_argc * 4);
var aindex = 0;
Module.setValue (main_argv + (aindex * 4), wasm_strdup (allRuntimeArguments [1]), "i32")
aindex += 1;
for (var i = 2; i < allRuntimeArguments.length; ++i) {
Module.setValue (main_argv + (aindex * 4), wasm_strdup (allRuntimeArguments [i]), "i32");
aindex += 1;
}
wasm_set_main_args (main_argc, main_argv);
// Automatic signature isn't working correctly
let result = Module.mono_call_assembly_entry_point (main_assembly_name, [app_args], "m");
let onError = function (error)
{
console.error (error);
if (error.stack)
console.error (error.stack);
test_exit (1);
}
try {
result.then (test_exit).catch (onError);
} catch (error) {
onError(error);
}
} else {
fail_exec ("Unhandled argument: " + allRuntimeArguments [0]);
}
},
call_test_method: function (method_name, args, signature) {
if ((arguments.length > 2) && (typeof (signature) !== "string"))
throw new Error("Invalid number of arguments for call_test_method");
var fqn = "[System.Private.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:" + method_name;
try {
return BINDING.call_static_method(fqn, args || [], signature);
} catch (exc) {
console.error("exception thrown in", fqn);
throw exc;
}
}
};