|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Text.Json; |
| 6 | +using Microsoft.JSInterop; |
| 7 | +using Microsoft.JSInterop.Infrastructure; |
| 8 | +using WebAssembly.JSInterop; |
| 9 | + |
| 10 | +namespace Mono.WebAssembly.Interop |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Provides methods for invoking JavaScript functions for applications running |
| 14 | + /// on the Mono WebAssembly runtime. |
| 15 | + /// </summary> |
| 16 | + public class MonoWebAssemblyJSRuntime : JSInProcessRuntime |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Gets the <see cref="MonoWebAssemblyJSRuntime"/> used to perform operations using <see cref="DotNetDispatcher"/>. |
| 20 | + /// </summary> |
| 21 | + private static MonoWebAssemblyJSRuntime Instance { get; set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes the <see cref="MonoWebAssemblyJSRuntime"/> to be used to perform operations using <see cref="DotNetDispatcher"/>. |
| 25 | + /// </summary> |
| 26 | + /// <param name="jsRuntime">The <see cref="MonoWebAssemblyJSRuntime"/> instance.</param> |
| 27 | + protected static void Initialize(MonoWebAssemblyJSRuntime jsRuntime) |
| 28 | + { |
| 29 | + if (Instance != null) |
| 30 | + { |
| 31 | + throw new InvalidOperationException("MonoWebAssemblyJSRuntime has already been initialized."); |
| 32 | + } |
| 33 | + |
| 34 | + Instance = jsRuntime ?? throw new ArgumentNullException(nameof(jsRuntime)); |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + protected override string InvokeJS(string identifier, string argsJson) |
| 39 | + { |
| 40 | + var noAsyncHandle = default(long); |
| 41 | + var result = InternalCalls.InvokeJSMarshalled(out var exception, ref noAsyncHandle, identifier, argsJson); |
| 42 | + return exception != null |
| 43 | + ? throw new JSException(exception) |
| 44 | + : result; |
| 45 | + } |
| 46 | + |
| 47 | + /// <inheritdoc /> |
| 48 | + protected override void BeginInvokeJS(long asyncHandle, string identifier, string argsJson) |
| 49 | + { |
| 50 | + InternalCalls.InvokeJSMarshalled(out _, ref asyncHandle, identifier, argsJson); |
| 51 | + } |
| 52 | + |
| 53 | + // Invoked via Mono's JS interop mechanism (invoke_method) |
| 54 | + private static string InvokeDotNet(string assemblyName, string methodIdentifier, string dotNetObjectId, string argsJson) |
| 55 | + { |
| 56 | + var callInfo = new DotNetInvocationInfo(assemblyName, methodIdentifier, dotNetObjectId == null ? default : long.Parse(dotNetObjectId), callId: null); |
| 57 | + return DotNetDispatcher.Invoke(Instance, callInfo, argsJson); |
| 58 | + } |
| 59 | + |
| 60 | + // Invoked via Mono's JS interop mechanism (invoke_method) |
| 61 | + private static void EndInvokeJS(string argsJson) |
| 62 | + => DotNetDispatcher.EndInvokeJS(Instance, argsJson); |
| 63 | + |
| 64 | + // Invoked via Mono's JS interop mechanism (invoke_method) |
| 65 | + private static void BeginInvokeDotNet(string callId, string assemblyNameOrDotNetObjectId, string methodIdentifier, string argsJson) |
| 66 | + { |
| 67 | + // Figure out whether 'assemblyNameOrDotNetObjectId' is the assembly name or the instance ID |
| 68 | + // We only need one for any given call. This helps to work around the limitation that we can |
| 69 | + // only pass a maximum of 4 args in a call from JS to Mono WebAssembly. |
| 70 | + string assemblyName; |
| 71 | + long dotNetObjectId; |
| 72 | + if (char.IsDigit(assemblyNameOrDotNetObjectId[0])) |
| 73 | + { |
| 74 | + dotNetObjectId = long.Parse(assemblyNameOrDotNetObjectId); |
| 75 | + assemblyName = null; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + dotNetObjectId = default; |
| 80 | + assemblyName = assemblyNameOrDotNetObjectId; |
| 81 | + } |
| 82 | + |
| 83 | + var callInfo = new DotNetInvocationInfo(assemblyName, methodIdentifier, dotNetObjectId, callId); |
| 84 | + DotNetDispatcher.BeginInvokeDotNet(Instance, callInfo, argsJson); |
| 85 | + } |
| 86 | + |
| 87 | + protected override void EndInvokeDotNet(DotNetInvocationInfo callInfo, in DotNetInvocationResult dispatchResult) |
| 88 | + { |
| 89 | + // For failures, the common case is to call EndInvokeDotNet with the Exception object. |
| 90 | + // For these we'll serialize as something that's useful to receive on the JS side. |
| 91 | + // If the value is not an Exception, we'll just rely on it being directly JSON-serializable. |
| 92 | + var resultOrError = dispatchResult.Success ? dispatchResult.Result : dispatchResult.Exception.ToString(); |
| 93 | + |
| 94 | + // We pass 0 as the async handle because we don't want the JS-side code to |
| 95 | + // send back any notification (we're just providing a result for an existing async call) |
| 96 | + var args = JsonSerializer.Serialize(new[] { callInfo.CallId, dispatchResult.Success, resultOrError }, JsonSerializerOptions); |
| 97 | + BeginInvokeJS(0, "DotNet.jsCallDispatcher.endInvokeDotNetFromJS", args); |
| 98 | + } |
| 99 | + |
| 100 | + #region Custom MonoWebAssemblyJSRuntime methods |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Invokes the JavaScript function registered with the specified identifier. |
| 104 | + /// </summary> |
| 105 | + /// <typeparam name="TRes">The .NET type corresponding to the function's return value type.</typeparam> |
| 106 | + /// <param name="identifier">The identifier used when registering the target function.</param> |
| 107 | + /// <returns>The result of the function invocation.</returns> |
| 108 | + public TRes InvokeUnmarshalled<TRes>(string identifier) |
| 109 | + => InvokeUnmarshalled<object, object, object, TRes>(identifier, null, null, null); |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Invokes the JavaScript function registered with the specified identifier. |
| 113 | + /// </summary> |
| 114 | + /// <typeparam name="T0">The type of the first argument.</typeparam> |
| 115 | + /// <typeparam name="TRes">The .NET type corresponding to the function's return value type.</typeparam> |
| 116 | + /// <param name="identifier">The identifier used when registering the target function.</param> |
| 117 | + /// <param name="arg0">The first argument.</param> |
| 118 | + /// <returns>The result of the function invocation.</returns> |
| 119 | + public TRes InvokeUnmarshalled<T0, TRes>(string identifier, T0 arg0) |
| 120 | + => InvokeUnmarshalled<T0, object, object, TRes>(identifier, arg0, null, null); |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Invokes the JavaScript function registered with the specified identifier. |
| 124 | + /// </summary> |
| 125 | + /// <typeparam name="T0">The type of the first argument.</typeparam> |
| 126 | + /// <typeparam name="T1">The type of the second argument.</typeparam> |
| 127 | + /// <typeparam name="TRes">The .NET type corresponding to the function's return value type.</typeparam> |
| 128 | + /// <param name="identifier">The identifier used when registering the target function.</param> |
| 129 | + /// <param name="arg0">The first argument.</param> |
| 130 | + /// <param name="arg1">The second argument.</param> |
| 131 | + /// <returns>The result of the function invocation.</returns> |
| 132 | + public TRes InvokeUnmarshalled<T0, T1, TRes>(string identifier, T0 arg0, T1 arg1) |
| 133 | + => InvokeUnmarshalled<T0, T1, object, TRes>(identifier, arg0, arg1, null); |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Invokes the JavaScript function registered with the specified identifier. |
| 137 | + /// </summary> |
| 138 | + /// <typeparam name="T0">The type of the first argument.</typeparam> |
| 139 | + /// <typeparam name="T1">The type of the second argument.</typeparam> |
| 140 | + /// <typeparam name="T2">The type of the third argument.</typeparam> |
| 141 | + /// <typeparam name="TRes">The .NET type corresponding to the function's return value type.</typeparam> |
| 142 | + /// <param name="identifier">The identifier used when registering the target function.</param> |
| 143 | + /// <param name="arg0">The first argument.</param> |
| 144 | + /// <param name="arg1">The second argument.</param> |
| 145 | + /// <param name="arg2">The third argument.</param> |
| 146 | + /// <returns>The result of the function invocation.</returns> |
| 147 | + public TRes InvokeUnmarshalled<T0, T1, T2, TRes>(string identifier, T0 arg0, T1 arg1, T2 arg2) |
| 148 | + { |
| 149 | + var result = InternalCalls.InvokeJSUnmarshalled<T0, T1, T2, TRes>(out var exception, identifier, arg0, arg1, arg2); |
| 150 | + return exception != null |
| 151 | + ? throw new JSException(exception) |
| 152 | + : result; |
| 153 | + } |
| 154 | + |
| 155 | + #endregion |
| 156 | + } |
| 157 | +} |
0 commit comments