Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runtime-tests.js « wasm « sdks - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f91f3424bed6a4d109793890db6f2e2c0a34c2ec (plain)
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
// -*- mode: js; js-indent-level: 4; -*-
//
// Run runtime tests under a JS shell or a browser
//

//glue code to deal with the differences between chrome, ch, d8, jsc and sm.
var is_browser = typeof window != "undefined";

if (is_browser) {
	// We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters
	var url = new URL (decodeURI (window.location));
	arguments = [];
	for (var v of url.searchParams) {
		if (v [0] == "arg") {
			console.log ("URL ARG: " + v [0] + "=" + v [1]);
			arguments.push (v [1]);
		}
	}
}

if (is_browser || typeof print === "undefined")
	print = console.log;

// JavaScript core does not have a console defined
if (typeof console === "undefined") {
	var Console = function () {
		this.log = function(msg){ print(msg) };
	};
	console = new Console();
}

if (typeof console !== "undefined") {
	var has_console_warn = false;
	try {
		if (typeof console.warn !== "undefined")
			has_console_warn = true;
	} catch(e) {}

	if (!has_console_warn)
		console.warn = console.log;
}

if (typeof crypto == 'undefined') {
	// /dev/random doesn't work on js shells, so define our own
	// See library_fs.js:createDefaultDevices ()
	var crypto = {
		getRandomValues: function (buffer) {
			buffer[0] = (Math.random()*256)|0;
		}
	}
}

try {
	if (typeof arguments == "undefined")
		arguments = WScript.Arguments;
	load = WScript.LoadScriptFile;
	read = WScript.LoadBinaryFile;
} catch (e) {
}

try {
	if (typeof arguments == "undefined") {
		if (typeof scriptArgs !== "undefined")
			arguments = scriptArgs;
	}
} catch (e) {
}
//end of all the nice shell glue code.

// set up a global variable to be accessed in App.init
var testArguments = arguments;

function test_exit (exit_code) {
	if (is_browser) {
		// Notify the puppeteer script
		Module.exit_code = exit_code;
		print ("WASM EXIT " + exit_code);
	} else {
		Module.wasm_exit (exit_code);
	}
}

function fail_exec (reason) {
	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
var args = testArguments;
print("Arguments: " + testArguments);
profilers = [];
setenv = {};
runtime_args = [];
enable_gc = false;
while (true) {
	if (args [0].startsWith ("--profile=")) {
		var arg = args [0].substring ("--profile=".length);

		profilers.push (arg);

		args = args.slice (1);
	} else if (args [0].startsWith ("--setenv=")) {
		var arg = args [0].substring ("--setenv=".length);
		var parts = arg.split ('=');
		if (parts.length != 2)
			fail_exec ("Error: malformed argument: '" + args [0]);
		setenv [parts [0]] = parts [1];
		args = args.slice (1);
	} else if (args [0].startsWith ("--runtime-arg=")) {
		var arg = args [0].substring ("--runtime-arg=".length);
		runtime_args.push (arg);
		args = args.slice (1);
	} else if (args [0] == "--enable-gc") {
		enable_gc = true;
		args = args.slice (1);
	} else {
		break;
	}
}
testArguments = args;

if (typeof window == "undefined")
  load ("mono-config.js");

var Module = { 
	mainScriptUrlOrBlob: "mono.js",

	print: function(x) { print ("WASM: " + x) },
	printErr: function(x) { print ("WASM-ERR: " + x) },

	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.
		var wasm_setenv = Module.cwrap ('mono_wasm_setenv', 'void', ['string', 'string']);
		for (var variable in setenv) {
			MONO.mono_wasm_setenv (variable, setenv [variable]);
		}

		if (enable_gc) {
			var f = Module.cwrap ('mono_wasm_enable_on_demand_gc', 'void', []);
			f ();
		}

		MONO.mono_load_runtime_and_bcl (
			config.vfs_prefix,
			config.deploy_prefix,
			config.enable_debugging,
			config.file_list,
			function () {
				App.init ();
			},
			function (asset)
			{
			  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 response = { ok: true, url: asset, 
							arrayBuffer: function() {
								return new Promise((resolve2, reject2) => {
									resolve2(new Uint8Array (read (asset, 'binary')));
							}
						)}
					}
				   resolve(response)
				 })
			  }
			}
		);
	},
};

if (typeof window == "undefined")
  load ("mono.js");

const IGNORE_PARAM_COUNT = -1;

var App = {
    init: function () {

		var assembly_load = Module.cwrap ('mono_wasm_assembly_load', 'number', ['string'])
		var find_class = Module.cwrap ('mono_wasm_assembly_find_class', 'number', ['number', 'string', 'string'])
		var find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number'])
		var runtime_invoke = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number', 'number']);
		var string_from_js = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string']);
		var assembly_get_entry_point = Module.cwrap ('mono_wasm_assembly_get_entry_point', 'number', ['number']);
		var string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'string', ['number']);
		var string_array_new = Module.cwrap ('mono_wasm_string_array_new', 'number', ['number']);
		var obj_array_set = Module.cwrap ('mono_wasm_obj_array_set', 'void', ['number', 'number', 'number']);
		var exit = Module.cwrap ('mono_wasm_exit', 'void', ['number']);
		var wasm_setenv = Module.cwrap ('mono_wasm_setenv', 'void', ['string', 'string']);
		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']);
		var unbox_int = Module.cwrap ('mono_unbox_int', 'number', ['number']);

		Module.wasm_exit = Module.cwrap ('mono_wasm_exit', 'void', ['number']);

		Module.print("Initializing.....");

		for (var i = 0; i < profilers.length; ++i) {
			var init = Module.cwrap ('mono_wasm_load_profiler_' + profilers [i], 'void', ['string'])

			init ("");
		}

		if (args[0] == "--regression") {
			var exec_regression = Module.cwrap ('mono_wasm_exec_regression', 'number', ['number', 'string'])

			var res = 0;
				try {
					res = exec_regression (10, args[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 (args[0] == "--run") {
			// Run an exe
			if (args.length == 1)
				fail_exec ("Error: Missing main executable argument.");
			main_assembly = assembly_load (args[1]);
			if (main_assembly == 0)
				fail_exec ("Error: Unable to load main executable '" + args[1] + "'");
			main_method = assembly_get_entry_point (main_assembly);
			if (main_method == 0)
				fail_exec ("Error: Main (string[]) method not found.");

			var app_args = string_array_new (args.length - 2);
			for (var i = 2; i < args.length; ++i) {
				obj_array_set (app_args, i - 2, string_from_js (args [i]));
			}

			var main_argc = args.length - 2 + 1;
			var main_argv = Module._malloc (main_argc * 4);
			aindex = 0;
			Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [1]), "i32")
			aindex += 1;
			for (var i = 2; i < args.length; ++i) {
				Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [i]), "i32");
				aindex += 1;
			}
			wasm_set_main_args (main_argc, main_argv);

			try {
				var invoke_args = Module._malloc (4);
				Module.setValue (invoke_args, app_args, "i32");
				var eh_throw = Module._malloc (4);
				Module.setValue (eh_throw, 0, "i32");
				var res = runtime_invoke (main_method, 0, invoke_args, eh_throw);
				var eh_res = Module.getValue (eh_throw, "i32");
				if (eh_res == 1) {
					print ("Exception:" + string_get_utf8 (res));
					test_exit (1);
				}
				var exit_code = unbox_int (res);
				if (exit_code != 0)
					test_exit (exit_code);
			} catch (ex) {
				print ("JS exception: " + ex);
				print (ex.stack);
				test_exit (1);
			}

			if (is_browser)
				test_exit (0);

			return;
		}

		Module.print("Initializing Binding Test Suite support.....");

		//binding test suite support code
		binding_test_module = assembly_load ("binding_tests");
		if (!binding_test_module)
		{
			Module.printErr("Binding tests module 'binding_tests' not found.  Exiting Tests.")
			throw new Error("Binding tests module 'binding_tests' not found.  Exiting Tests.");
		}
		
		binding_test_class = find_class (binding_test_module, "", "TestClass");
		if (!binding_test_class)
		{
			Module.printErr("Binding tests class 'TestClass' not found.  Exiting Tests.")
			throw new Error("Binding tests class 'TestClass' not found.  Exiting Tests.");
		}		

		Module.print("Binding support complete.");

		
		Module.print("Checking for [main]Driver:Send ....");
		
		var send_message = undefined;
		
		try
		{
			send_message = BINDING.bind_static_method("[main]Driver:Send");
		}
		catch (e)
		{
			Module.printErr("[main]Driver:Send not found: " + e);
			throw e;
		
		}

		Module.print("Driver binding complete.");

		var main_argc = 1
		var main_argv = Module._malloc (main_argc * 4);
		Module.setValue (main_argv, wasm_strdup ("mono-wasm"), "i32")
		wasm_set_main_args (main_argc, main_argv);

		var bad_send_msg_detected = false;
		for (var i = 0; i < testArguments.length; ++i) {
			if (testArguments [i] == "--exclude") {
				send_message ("--exclude", testArguments [i + 1]);
				i ++;
				continue;
			}
			var res = "";
			try
			{
				res = send_message("start-test", testArguments [i])
			} catch (e) {
				Module.printErr ("BAD SEND MSG: " + e);
				bad_send_msg_detected = true;
			}
			print ("-----STARTED " + testArguments [i] + "---- " + res);

			if (res == "SUCCESS") {
				while (send_message ("pump-test", testArguments [i]) != "DONE") 
				{
					Module.pump_message ();
					print ("|");
				}
				print ("\nDONE")
			}
		}

		var status = send_message ("test-result", "");
		print ("Test status " + status)
		if (status != "PASS")
			fail_exec ("BAD TEST STATUS");

		if (bad_send_msg_detected)
			fail_exec ("BAD MSG SEND DETECTED");
		test_exit (0);
    },
};

//binding test suite support code
var binding_test_module = undefined;
var binding_test_class = undefined;

// This function is called from the binding test suite
function call_test_method(method_name, signature, args)
{
	var target_method = find_method (binding_test_class, method_name, IGNORE_PARAM_COUNT)
	if (!target_method)
		throw "Could not find " + method_name;

	return Module.mono_method_invoke (target_method, null, signature, args);
}