2 //glue code to deal with the differences between ch, d8, jsc and sm.
3 if (typeof print === "undefined")
6 // JavaScript core does not have a console defined
7 if (typeof console === "undefined") {
8 var Console = function () {
9 this.log = function(msg){ print(msg) };
11 console = new Console();
14 if (typeof console !== "undefined") {
15 var has_console_warn = false;
17 if (typeof console.warn !== "undefined")
18 has_console_warn = true;
21 if (!has_console_warn)
22 console.warn = console.log;
25 if (typeof crypto == 'undefined') {
26 // /dev/random doesn't work on js shells, so define our own
27 // See library_fs.js:createDefaultDevices ()
29 getRandomValues: function (buffer) {
30 buffer[0] = (Math.random()*256)|0;
35 fail_exec = function(reason) {
41 arguments = WScript.Arguments;
42 load = WScript.LoadScriptFile;
43 read = WScript.LoadBinaryFile;
44 fail_exec = function(reason) {
51 if (typeof scriptArgs !== "undefined")
52 arguments = scriptArgs;
54 //end of all the nice shell glue code.
56 // set up a global variable to be accessed in the App.init
57 var testArguments = arguments;
59 function inspect_object (o){
63 r += "'" + p + "' => '" + t + "', ";
68 // Preprocess arguments
69 var args = testArguments;
70 print("Arguments: " + testArguments);
75 if (args [0].startsWith ("--profile=")) {
76 var arg = args [0].substring ("--profile=".length);
80 args = args.slice (1);
81 } else if (args [0].startsWith ("--setenv=")) {
82 var arg = args [0].substring ("--setenv=".length);
83 var parts = arg.split ('=');
84 if (parts.length != 2)
85 fail_exec ("Error: malformed argument: '" + args [0]);
86 setenv [parts [0]] = parts [1];
87 args = args.slice (1);
88 } else if (args [0].startsWith ("--runtime-arg=")) {
89 var arg = args [0].substring ("--runtime-arg=".length);
90 runtime_args.push (arg);
91 args = args.slice (1);
97 load ("mono-config.js");
100 print: function(x) { print ("WASM: " + x) },
101 printErr: function(x) { print ("WASM-ERR: " + x) },
103 onAbort: function(x) {
104 print ("ABORT: " + x);
105 var err = new Error();
106 print ("Stacktrace: \n");
111 onRuntimeInitialized: function () {
112 // Have to set env vars here to enable setting MONO_LOG_LEVEL etc.
113 var wasm_setenv = Module.cwrap ('mono_wasm_setenv', 'void', ['string', 'string']);
114 for (var variable in setenv) {
115 MONO.mono_wasm_setenv (variable, setenv [variable]);
118 MONO.mono_load_runtime_and_bcl (
120 config.deploy_prefix,
121 config.enable_debugging,
128 // The default mono_load_runtime_and_bcl defaults to using
129 // fetch to load the assets. It also provides a way to set a
130 // fetch promise callback.
131 // Here we wrap the file read in a promise and fake a fetch response
133 return new Promise((resolve, reject) => {
134 var response = { ok: true, url: asset,
135 arrayBuffer: function() {
136 return new Promise((resolve2, reject2) => {
137 resolve2(new Uint8Array (read (asset, 'binary')));
150 var assembly_load = Module.cwrap ('mono_wasm_assembly_load', 'number', ['string'])
151 var find_class = Module.cwrap ('mono_wasm_assembly_find_class', 'number', ['number', 'string', 'string'])
152 var find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number'])
153 var runtime_invoke = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number', 'number']);
154 var string_from_js = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string']);
155 var assembly_get_entry_point = Module.cwrap ('mono_wasm_assembly_get_entry_point', 'number', ['number']);
156 var string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'string', ['number']);
157 var string_array_new = Module.cwrap ('mono_wasm_string_array_new', 'number', ['number']);
158 var obj_array_set = Module.cwrap ('mono_wasm_obj_array_set', 'void', ['number', 'number', 'number']);
159 var wasm_exit = Module.cwrap ('mono_wasm_exit', 'void', ['number']);
160 var wasm_setenv = Module.cwrap ('mono_wasm_setenv', 'void', ['string', 'string']);
161 var wasm_set_main_args = Module.cwrap ('mono_wasm_set_main_args', 'void', ['number', 'number']);
162 var wasm_strdup = Module.cwrap ('mono_wasm_strdup', 'number', ['string'])
164 const IGNORE_PARAM_COUNT = -1;
169 Module.print("Initializing.....");
171 for (var i = 0; i < profilers.length; ++i) {
172 var init = Module.cwrap ('mono_wasm_load_profiler_' + profilers [i], 'void', ['string'])
177 if (args[0] == "--regression") {
178 var exec_regresion = Module.cwrap ('mono_wasm_exec_regression', 'number', ['number', 'string'])
182 res = exec_regresion (10, args[1]);
183 Module.print ("REGRESSION RESULT: " + res);
185 Module.print ("ABORT: " + e);
191 fail_exec ("REGRESSION TEST FAILED");
196 if (runtime_args.length > 0)
197 MONO.mono_wasm_set_runtime_options (runtime_args);
199 if (args[0] == "--run") {
201 if (args.length == 1)
202 fail_exec ("Error: Missing main executable argument.");
203 main_assembly = assembly_load (args[1]);
204 if (main_assembly == 0)
205 fail_exec ("Error: Unable to load main executable '" + args[1] + "'");
206 main_method = assembly_get_entry_point (main_assembly);
207 if (main_method == 0)
208 fail_exec ("Error: Main (string[]) method not found.");
210 var app_args = string_array_new (args.length - 2);
211 for (var i = 2; i < args.length; ++i) {
212 obj_array_set (app_args, i - 2, string_from_js (args [i]));
215 var main_argc = args.length - 2 + 1;
216 var main_argv = Module._malloc (main_argc * 4);
218 Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [1]), "i32")
220 for (var i = 2; i < args.length; ++i) {
221 Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [i]), "i32");
224 wasm_set_main_args (main_argc, main_argv);
227 var invoke_args = Module._malloc (4);
228 Module.setValue (invoke_args, app_args, "i32");
229 var eh_throw = Module._malloc (4);
230 Module.setValue (eh_throw, 0, "i32");
231 var res = runtime_invoke (main_method, 0, invoke_args, eh_throw);
232 var eh_res = Module.getValue (eh_throw, "i32");
234 print ("Exception:" + string_get_utf8 (res));
238 print ("JS exception: " + ex);
245 Module.print("Initializing Binding Test Suite support.....");
247 //binding test suite support code
248 binding_test_module = assembly_load ("binding_tests");
249 if (!binding_test_module)
251 Module.printErr("Binding tests module 'binding_tests' not found. Exiting Tests.")
252 throw new Error("Binding tests module 'binding_tests' not found. Exiting Tests.");
255 binding_test_class = find_class (binding_test_module, "", "TestClass");
256 if (!binding_test_class)
258 Module.printErr("Binding tests class 'TestClass' not found. Exiting Tests.")
259 throw new Error("Binding tests class 'TestClass' not found. Exiting Tests.");
262 Module.print("Binding support complete.");
265 Module.print("Checking for [main]Driver:Send ....");
267 var send_message = undefined;
271 send_message = BINDING.bind_static_method("[main]Driver:Send");
275 Module.printErr("[main]Driver:Send not found: " + e);
280 Module.print("Driver binding complete.");
283 var main_argv = Module._malloc (main_argc * 4);
284 Module.setValue (main_argv, wasm_strdup ("mono-wasm"), "i32")
285 wasm_set_main_args (main_argc, main_argv);
287 var bad_send_msg_detected = false;
288 for (var i = 0; i < testArguments.length; ++i) {
289 if (testArguments [i] == "--exclude") {
290 send_message ("--exclude", testArguments [i + 1]);
297 res = send_message("start-test", testArguments [i])
299 printErr ("BAD SEND MSG: " + e);
300 bad_send_msg_detected = true;
302 print ("-----STARTED " + testArguments [i] + "---- " + res);
304 if (res == "SUCCESS") {
305 while (send_message ("pump-test", testArguments [i]) != "DONE")
307 Module.pump_message ();
314 var status = send_message ("test-result", "");
315 print ("Test status " + status)
316 if (status != "PASS")
317 fail_exec ("BAD TEST STATUS");
319 if (bad_send_msg_detected)
320 fail_exec ("BAD MSG SEND DETECTED");
324 //binding test suite support code
325 var binding_test_module = undefined;
326 var binding_test_class = undefined;
328 // This function is called from the binding test suite
329 function call_test_method(method_name, signature, args)
331 var target_method = find_method (binding_test_class, method_name, IGNORE_PARAM_COUNT)
333 throw "Could not find " + method_name;
335 return Module.mono_method_invoke (target_method, null, signature, args);