Bump mono/corefx to fix https://github.com/mono/mono/issues/14864
[mono-project.git] / sdks / wasm / runtime-tests.js
blobd25a6ce699b8ca402f740a333c9b9009cc5ace6b
2 //glue code to deal with the differences between ch, d8, jsc and sm.
3 if (typeof print === "undefined")
4         print = console.log;
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) };
10         };
11         console = new Console();
14 if (typeof console !== "undefined") {
15         var has_console_warn = false;
16         try {
17                 if (typeof console.warn !== "undefined")
18                         has_console_warn = true;
19         } catch(e) {}
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 ()
28         var crypto = {
29                 getRandomValues: function (buffer) {
30                         buffer[0] = (Math.random()*256)|0;
31                 }
32         }
35 fail_exec = function(reason) {
36         print (reason);
37         wasm_exit (1);
40 try {
41         arguments = WScript.Arguments;
42         load = WScript.LoadScriptFile;
43         read = WScript.LoadBinaryFile;
44         fail_exec = function(reason) {
45                 print (reason);
46                 wasm_exit (1);
47         }
48 } catch(e) {}
50 try {
51         if (typeof scriptArgs !== "undefined")
52                 arguments = scriptArgs;
53 } catch(e) {}
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){
60     var r="";
61     for(var p in o) {
62         var t = typeof o[p];
63         r += "'" + p + "' => '" + t + "', ";
64     }
65     return r;
68 // Preprocess arguments
69 var args = testArguments;
70 print("Arguments: " + testArguments);
71 profilers = [];
72 setenv = {};
73 runtime_args = [];
74 while (true) {
75         if (args [0].startsWith ("--profile=")) {
76                 var arg = args [0].substring ("--profile=".length);
78                 profilers.push (arg);
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);
92         } else {
93                 break;
94         }
97 load ("mono-config.js");
99 var Module = { 
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");
107                 print (err.stack);
108                 wasm_exit (1);
109         },
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]);
116                 }
118                 MONO.mono_load_runtime_and_bcl (
119                         config.vfs_prefix,
120                         config.deploy_prefix,
121                         config.enable_debugging,
122                         config.file_list,
123                         function () {
124                                 App.init ();
125                         },
126                         function (asset ) 
127                         {
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
132                                 // structure.
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')));
138                                                         }
139                                                 )}
140                                         }
141                                    resolve(response)
142                                  })
143                         }
144                 );
145         },
148 load ("mono.js");
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;
166 var App = {
167     init: function () {
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'])
174                         init ("");
175                 }
177                 if (args[0] == "--regression") {
178                         var exec_regresion = Module.cwrap ('mono_wasm_exec_regression', 'number', ['number', 'string'])
180                         var res = 0;
181                                 try {
182                                         res = exec_regresion (10, args[1]);
183                                         Module.print ("REGRESSION RESULT: " + res);
184                                 } catch (e) {
185                                         Module.print ("ABORT: " + e);
186                                         print (e.stack);
187                                         res = 1;
188                                 }
190                         if (res)
191                                 fail_exec ("REGRESSION TEST FAILED");
193                         return;
194                 }
196                 if (runtime_args.length > 0)
197                         MONO.mono_wasm_set_runtime_options (runtime_args);
199                 if (args[0] == "--run") {
200                         // Run an exe
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]));
213                         }
215                         var main_argc = args.length - 2 + 1;
216                         var main_argv = Module._malloc (main_argc * 4);
217                         aindex = 0;
218                         Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [1]), "i32")
219                         aindex += 1;
220                         for (var i = 2; i < args.length; ++i) {
221                                 Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [i]), "i32");
222                                 aindex += 1;
223                         }
224                         wasm_set_main_args (main_argc, main_argv);
226                         try {
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");
233                                 if (eh_res == 1) {
234                                         print ("Exception:" + string_get_utf8 (res));
235                                         wasm_exit (1);
236                                 }
237                         } catch (ex) {
238                                 print ("JS exception: " + ex);
239                                 print (ex.stack);
240                                 wasm_exit (1);
241                         }
242                         return;
243                 }
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)
250                 {
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.");
253                 }
254                 
255                 binding_test_class = find_class (binding_test_module, "", "TestClass");
256                 if (!binding_test_class)
257                 {
258                         Module.printErr("Binding tests class 'TestClass' not found.  Exiting Tests.")
259                         throw new Error("Binding tests class 'TestClass' not found.  Exiting Tests.");
260                 }               
262                 Module.print("Binding support complete.");
264                 
265                 Module.print("Checking for [main]Driver:Send ....");
266                 
267                 var send_message = undefined;
268                 
269                 try
270                 {
271                         send_message = BINDING.bind_static_method("[main]Driver:Send");
272                 }
273                 catch (e)
274                 {
275                         Module.printErr("[main]Driver:Send not found: " + e);
276                         throw e;
277                 
278                 }
280                 Module.print("Driver binding complete.");
282                 var main_argc = 1
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]);
291                                 i ++;
292                                 continue;
293                         }
294                         var res = "";
295                         try
296                         {
297                                 res = send_message("start-test", testArguments [i])
298                         } catch (e) {
299                                 printErr ("BAD SEND MSG: " + e);
300                                 bad_send_msg_detected = true;
301                         }
302                         print ("-----STARTED " + testArguments [i] + "---- " + res);
304                         if (res == "SUCCESS") {
305                                 while (send_message ("pump-test", testArguments [i]) != "DONE") 
306                                 {
307                                         Module.pump_message ();
308                                         print ("|");
309                                 }
310                                 print ("\nDONE")
311                         }
312                 }
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");
321     },
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)
332         if (!target_method)
333                 throw "Could not find " + method_name;
335         return Module.mono_method_invoke (target_method, null, signature, args);