From f4179b944eef55ec8f8de40f7de2609df6f6b272 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 6 Aug 2020 19:25:59 +0100 Subject: [PATCH] Simplify JS interop for Blazor WebAssembly (#40467) --- src/mono/wasm/runtime/dotnet_support.js | 20 ++++++++++++++++++++ src/mono/wasm/runtime/driver.c | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/mono/wasm/runtime/dotnet_support.js b/src/mono/wasm/runtime/dotnet_support.js index c1ddc08..2b4a16e 100644 --- a/src/mono/wasm/runtime/dotnet_support.js +++ b/src/mono/wasm/runtime/dotnet_support.js @@ -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 diff --git a/src/mono/wasm/runtime/driver.c b/src/mono/wasm/runtime/driver.c index 3fc1a7e..8e68329 100644 --- a/src/mono/wasm/runtime/driver.c +++ b/src/mono/wasm/runtime/driver.c @@ -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); -- 2.7.4