Fix ActivityTracker ES_BUILD_STANDALONE build
[mono-project.git] / sdks / wasm / dotnet_support.js
blobbe94f8766ae51594d3dcac46ef58ff3493cbfbea
2 var DotNetSupportLib = {
3         $DOTNET: {
4                 _dotnet_get_global: function() {
5                         function testGlobal(obj) {
6                                 obj['___dotnet_global___'] = obj;
7                                 var success = typeof ___dotnet_global___ === 'object' && obj['___dotnet_global___'] === obj;
8                                 if (!success) {
9                                         delete obj['___dotnet_global___'];
10                                 }
11                                 return success;
12                         }
13                         if (typeof ___dotnet_global___ === 'object') {
14                                 return ___dotnet_global___;
15                         }
16                         if (typeof global === 'object' && testGlobal(global)) {
17                                 ___dotnet_global___ = global;
18                         } else if (typeof window === 'object' && testGlobal(window)) {
19                                 ___dotnet_global___ = window;
20                         }
21                         if (typeof ___dotnet_global___ === 'object') {
22                                 return ___dotnet_global___;
23                         }
24                         throw Error('unable to get DotNet global object.');
25                 },
26                 //FIXME this is wastefull, we could remove the temp malloc by going the UTF16 route
27                 //FIXME this is unsafe, cuz raw objects could be GC'd.
28                 conv_string: function (mono_obj) {
29                         if (mono_obj == 0)
30                                 return null;
32                         if (!this.mono_string_get_utf8)
33                                 this.mono_string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'number', ['number']);
35                         var raw = this.mono_string_get_utf8 (mono_obj);
36                         var res = Module.UTF8ToString (raw);
37                         Module._free (raw);
39                         return res;
40                 },              
41         },
42         mono_wasm_invoke_js_marshalled: function(exceptionMessage, asyncHandleLongPtr, functionName, argsJson) {
44                 var mono_string = DOTNET._dotnet_get_global()._mono_string_cached
45                         || (DOTNET._dotnet_get_global()._mono_string_cached = Module.cwrap('mono_wasm_string_from_js', 'number', ['string']));
46         
47                 try {
48                         // Passing a .NET long into JS via Emscripten is tricky. The method here is to pass
49                         // as pointer to the long, then combine two reads from the HEAPU32 array.
50                         // Even though JS numbers can't represent the full range of a .NET long, it's OK
51                         // because we'll never exceed Number.MAX_SAFE_INTEGER (2^53 - 1) in this case.
52                         //var u32Index = $1 >> 2;
53                         var u32Index = asyncHandleLongPtr >> 2;
54                         var asyncHandleJsNumber = Module.HEAPU32[u32Index + 1]*4294967296 + Module.HEAPU32[u32Index];
56                         // var funcNameJsString = UTF8ToString (functionName);
57                         // var argsJsonJsString = argsJson && UTF8ToString (argsJson);
58                         var funcNameJsString = DOTNET.conv_string(functionName);
59                         var argsJsonJsString = argsJson && DOTNET.conv_string (argsJson);
61                         var dotNetExports = DOTNET._dotnet_get_global().DotNet;
62                         if (!dotNetExports) {
63                                 throw new Error('The Microsoft.JSInterop.js library is not loaded.');
64                         }
66                         if (asyncHandleJsNumber) {
67                                 dotNetExports.jsCallDispatcher.beginInvokeJSFromDotNet(asyncHandleJsNumber, funcNameJsString, argsJsonJsString);
68                                 return 0;
69                         } else {
70                                 var resultJson = dotNetExports.jsCallDispatcher.invokeJSFromDotNet(funcNameJsString, argsJsonJsString);
71                                 return resultJson === null ? 0 : mono_string(resultJson);
72                         }
73                 } catch (ex) {
74                         var exceptionJsString = ex.message + '\n' + ex.stack;
75                         var exceptionSystemString = mono_string(exceptionJsString);
76                         setValue (exceptionMessage, exceptionSystemString, 'i32'); // *exceptionMessage = exceptionSystemString;
77                         return 0;
78                 }
79         },
80         mono_wasm_invoke_js_unmarshalled: function(exceptionMessage, funcName, arg0, arg1, arg2)        {
81                 try {
82                         // Get the function you're trying to invoke
83                         var funcNameJsString = DOTNET.conv_string(funcName);
84                         var dotNetExports = DOTNET._dotnet_get_global().DotNet;
85                         if (!dotNetExports) {
86                                 throw new Error('The Microsoft.JSInterop.js library is not loaded.');
87                         }
88                         var funcInstance = dotNetExports.jsCallDispatcher.findJSFunction(funcNameJsString);
90                         return funcInstance.call(null, arg0, arg1, arg2);
91                 } catch (ex) {
92                         var exceptionJsString = ex.message + '\n' + ex.stack;
93                         var mono_string = Module.cwrap('mono_wasm_string_from_js', 'number', ['string']); // TODO: Cache
94                         var exceptionSystemString = mono_string(exceptionJsString);
95                         setValue (exceptionMessage, exceptionSystemString, 'i32'); // *exceptionMessage = exceptionSystemString;
96                         return 0;
97                 }
98         }
99         
103 autoAddDeps(DotNetSupportLib, '$DOTNET')
104 mergeInto(LibraryManager.library, DotNetSupportLib)