2 var DotNetSupportLib = {
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;
9 delete obj['___dotnet_global___'];
13 if (typeof ___dotnet_global___ === 'object') {
14 return ___dotnet_global___;
16 if (typeof global === 'object' && testGlobal(global)) {
17 ___dotnet_global___ = global;
18 } else if (typeof window === 'object' && testGlobal(window)) {
19 ___dotnet_global___ = window;
21 if (typeof ___dotnet_global___ === 'object') {
22 return ___dotnet_global___;
24 throw Error('unable to get DotNet global object.');
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) {
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);
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']));
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;
63 throw new Error('The Microsoft.JSInterop.js library is not loaded.');
66 if (asyncHandleJsNumber) {
67 dotNetExports.jsCallDispatcher.beginInvokeJSFromDotNet(asyncHandleJsNumber, funcNameJsString, argsJsonJsString);
70 var resultJson = dotNetExports.jsCallDispatcher.invokeJSFromDotNet(funcNameJsString, argsJsonJsString);
71 return resultJson === null ? 0 : mono_string(resultJson);
74 var exceptionJsString = ex.message + '\n' + ex.stack;
75 var exceptionSystemString = mono_string(exceptionJsString);
76 setValue (exceptionMessage, exceptionSystemString, 'i32'); // *exceptionMessage = exceptionSystemString;
80 mono_wasm_invoke_js_unmarshalled: function(exceptionMessage, funcName, arg0, arg1, arg2) {
82 // Get the function you're trying to invoke
83 var funcNameJsString = DOTNET.conv_string(funcName);
84 var dotNetExports = DOTNET._dotnet_get_global().DotNet;
86 throw new Error('The Microsoft.JSInterop.js library is not loaded.');
88 var funcInstance = dotNetExports.jsCallDispatcher.findJSFunction(funcNameJsString);
90 return funcInstance.call(null, arg0, arg1, arg2);
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;
103 autoAddDeps(DotNetSupportLib, '$DOTNET')
104 mergeInto(LibraryManager.library, DotNetSupportLib)