Simplify JS interop for Blazor WebAssembly (#40467)
authorSteve Sanderson <SteveSandersonMS@users.noreply.github.com>
Thu, 6 Aug 2020 18:25:59 +0000 (19:25 +0100)
committerGitHub <noreply@github.com>
Thu, 6 Aug 2020 18:25:59 +0000 (19:25 +0100)
src/mono/wasm/runtime/dotnet_support.js
src/mono/wasm/runtime/driver.c

index c1ddc08..2b4a16e 100644 (file)
@@ -29,6 +29,24 @@ var DotNetSupportLib = {
                        return MONO.string_decoder.copy (mono_obj);
                }
        },
+
+       mono_wasm_invoke_js_blazor: function(exceptionMessage, callInfo, arg0, arg1, arg2)      {
+               try {
+                       var blazorExports = DOTNET._dotnet_get_global().Blazor;
+                       if (!blazorExports) {
+                               throw new Error('The blazor.webassembly.js library is not loaded.');
+                       }
+
+                       return blazorExports._internal.invokeJSFromDotNet(callInfo, arg0, arg1, arg2);
+               } catch (ex) {
+                       var exceptionJsString = ex.message + '\n' + ex.stack;
+                       var exceptionSystemString = mono_string(exceptionJsString);
+                       setValue (exceptionMessage, exceptionSystemString, 'i32'); // *exceptionMessage = exceptionSystemString;
+                       return 0;
+               }
+       },
+
+       // This is for back-compat only and will eventually be removed
        mono_wasm_invoke_js_marshalled: function(exceptionMessage, asyncHandleLongPtr, functionName, argsJson, treatResultAsVoid) {
 
                var mono_string = DOTNET._dotnet_get_global()._mono_string_cached
@@ -67,6 +85,8 @@ var DotNetSupportLib = {
                        return 0;
                }
        },
+
+       // This is for back-compat only and will eventually be removed
        mono_wasm_invoke_js_unmarshalled: function(exceptionMessage, funcName, arg0, arg1, arg2)        {
                try {
                        // Get the function you're trying to invoke
index 3fc1a7e..8e68329 100644 (file)
@@ -24,6 +24,8 @@ void core_initialize_internals ();
 #endif
 
 // Blazor specific custom routines - see dotnet_support.js for backing code
+extern void* mono_wasm_invoke_js_blazor (MonoString **exceptionMessage, void *callInfo, void* arg0, void* arg1, void* arg2);
+// The following two are for back-compat and will eventually be removed
 extern void* mono_wasm_invoke_js_marshalled (MonoString **exceptionMessage, void *asyncHandleLongPtr, MonoString *funcName, MonoString *argsJson);
 extern void* mono_wasm_invoke_js_unmarshalled (MonoString **exceptionMessage, MonoString *funcName, void* arg0, void* arg1, void* arg2);
 
@@ -348,6 +350,8 @@ void mono_initialize_internals ()
        // TODO: what happens when two types in different assemblies have the same FQN?
 
        // Blazor specific custom routines - see dotnet_support.js for backing code
+       mono_add_internal_call ("WebAssembly.JSInterop.InternalCalls::InvokeJS", mono_wasm_invoke_js_blazor);
+       // The following two are for back-compat and will eventually be removed
        mono_add_internal_call ("WebAssembly.JSInterop.InternalCalls::InvokeJSMarshalled", mono_wasm_invoke_js_marshalled);
        mono_add_internal_call ("WebAssembly.JSInterop.InternalCalls::InvokeJSUnmarshalled", mono_wasm_invoke_js_unmarshalled);