[corlib] CoreRT System.Threading.Tasks (#6672)
[mono-project.git] / sdks / wasm / test.js
blob0b535c4db71a9bd7cd88f7607b6ddb00e0cec4c5
1 function inspect_object (o){
2     var r="";
3     for(var p in o) {
4         var t = typeof o[p];
5         r += "'" + p + "' => '" + t + "', ";
6     }
7     return r;
11 var Module = { 
12         print: function(x) { print ("WASM: " + x) },
13         printErr: function(x) { print ("WASM-ERR: " + x) },
15     totalDependencies: 0,
16     monitorRunDependencies: function(left) {
17       this.totalDependencies = Math.max(this.totalDependencies, left);
18       print("STATUS: "+ (left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.'));
19     },
21         instantiateWasm: function (env, receiveInstance) {
22                 //merge Module's env with emcc's env
23                 env.env = Object.assign({}, env.env, this.env);
24                 var module = new WebAssembly.Module (read ('mono.wasm', 'binary'))
25                 this.wasm_instance = new WebAssembly.Instance (module, env);
26                 this.em_cb = receiveInstance;
27                 return this
28         },
30         finish_loading: function () {
31                 this.em_cb (this.wasm_instance);
32         },
34         env: {
35         },
38 var assemblies = [ "mscorlib.dll", "System.dll", "System.Core.dll", "main.exe", "nunitlite.dll", "mini_tests.dll", "wasm_corlib_test.dll", "wasm_System_test.dll", "wasm_System.Core_test.dll" ];
40 load ("mono.js");
41 Module.finish_loading ();
44 Module.FS_createPath ("/", "managed", true, true);
46 //Load all assembly in @assemblies into the FS at /mananaged
47 assemblies.forEach (function(asm_name) {
48         print ("LOADING " + asm_name)
49         var asm = new Uint8Array (read ("managed/" + asm_name, 'binary'));
50         Module.FS_createDataFile ("managed/" + asm_name, null, asm, true, true, true);  
51 });
54 var load_runtime = Module.cwrap ('mono_wasm_load_runtime', null, ['string'])
55 var assembly_load = Module.cwrap ('mono_wasm_assembly_load', 'number', ['string'])
56 var find_class = Module.cwrap ('mono_wasm_assembly_find_class', 'number', ['number', 'string', 'string'])
57 var find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number'])
58 var invoke_method = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number'])
59 var mono_string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'number', ['number'])
60 var mono_string = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string'])
62 function call_method (method, this_arg, args) {
63         var stack = Module.Runtime.stackSave ();
64         var args_mem = Runtime.stackAlloc (args.length);
65         var eh_throw = Runtime.stackAlloc (4);
66         for (var i = 0; i < args.length; ++i)
67                 Module.setValue (args_mem + i * 4, args [i], "i32");
68         Module.setValue (eh_throw, 0, "i32");
70         var res = invoke_method (method, this_arg, args_mem, eh_throw);
72         if (Module.getValue (eh_throw, "i32") != 0) {
73                 Module.Runtime.stackRestore(stack);
74                 var msg = conv_string (res);
75                 throw new Error (msg); //the convention is that invoke_method ToString () any outgoing exception
76         }
78         Module.Runtime.stackRestore(stack);
79         return res;
82 //FIXME this is wastefull, we could remove the temp malloc by going the UTF16 route
83 //FIXME this is unsafe, cuz raw objects could be GC'd.
84 function conv_string (mono_obj) {
85         if (mono_obj == 0)
86                 return null;
87         var raw = mono_string_get_utf8 (mono_obj);
88         var res = Module.UTF8ToString (raw);
89         Module._free (raw);
91         return res;
94 function mono_send_msg (key, val) {
95         try {
96                 return conv_string (call_method (send_message, null, [mono_string (key), mono_string (val)]));
97         } catch (e) {
98                 print ("BAD SEND MSG: " + e);
99                 return null;
100         }
103 load_runtime ("managed");
104 var main_module = assembly_load ("main")
105 if (!main_module)
106         throw 1;
109 var driver_class = find_class (main_module, "", "Driver")
110 if (!driver_class)
111         throw 2;
113 var send_message = find_method (driver_class, "Send", -1)
114 if (!send_message)
115         throw 3;
117 print ("-----LOADED ----");
119 for (var i = 0; i < arguments.length; ++i) {
120         var res = mono_send_msg ("start-test", arguments [i])
121         print ("-----STARTED " + arguments [i] + "---- " + res);
123         if (res == "SUCCESS") {
124                 while (mono_send_msg ("pump-test", arguments [i]) != "DONE") {
125                         Module.pump_message ();
126                         print ("|");
127                 }
128                 print ("\nDONE")
129         }
132 var status = mono_send_msg ("test-result", "");
133 print ("Test status " + status)
134 if (status != "PASS")
135         exit (1)