|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Reflection; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace System.Runtime.InteropServices.JavaScript |
| 13 | +{ |
| 14 | + // this maps to src\native\libs\System.Runtime.InteropServices.JavaScript.Native\interop\managed-exports.ts |
| 15 | + // the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction |
| 16 | + internal static unsafe partial class JavaScriptExports |
| 17 | + { |
| 18 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_ReleaseJSOwnedObjectByGCHandle")] |
| 19 | + // The JS layer invokes this method when the JS wrapper for a JS owned object has been collected by the JS garbage collector |
| 20 | + // the marshaled signature is: void ReleaseJSOwnedObjectByGCHandle(GCHandle gcHandle) |
| 21 | + public static void ReleaseJSOwnedObjectByGCHandle(JSMarshalerArgument* arguments_buffer) |
| 22 | + { |
| 23 | + ref JSMarshalerArgument arg_exc = ref arguments_buffer[0]; // initialized by caller in alloc_stack_frame() |
| 24 | + ref JSMarshalerArgument arg_1 = ref arguments_buffer[2]; // initialized and set by caller |
| 25 | + |
| 26 | + try |
| 27 | + { |
| 28 | + // when we arrive here, we are on the thread which owns the proxies or on IO thread |
| 29 | + var ctx = arg_exc.ToManagedContext; |
| 30 | + ctx.ReleaseJSOwnedObjectByGCHandle(arg_1.slot.GCHandle); |
| 31 | + } |
| 32 | + catch (Exception ex) |
| 33 | + { |
| 34 | + Environment.FailFast($"ReleaseJSOwnedObjectByGCHandle: Unexpected synchronous failure (ManagedThreadId {Environment.CurrentManagedThreadId}): " + ex); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_CallDelegate")] |
| 39 | + // the marshaled signature is: TRes? CallDelegate<T1,T2,T3TRes>(GCHandle callback, T1? arg1, T2? arg2, T3? arg3) |
| 40 | + public static void CallDelegate(JSMarshalerArgument* arguments_buffer) |
| 41 | + { |
| 42 | + ref JSMarshalerArgument arg_exc = ref arguments_buffer[0]; // initialized by JS caller in alloc_stack_frame() |
| 43 | + // arg_res is initialized by JS caller |
| 44 | + ref JSMarshalerArgument arg_1 = ref arguments_buffer[2];// initialized and set by JS caller |
| 45 | + // arg_2 set by JS caller when there are arguments |
| 46 | + // arg_3 set by JS caller when there are arguments |
| 47 | + // arg_4 set by JS caller when there are arguments |
| 48 | + try |
| 49 | + { |
| 50 | + GCHandle callback_gc_handle = (GCHandle)arg_1.slot.GCHandle; |
| 51 | + if (callback_gc_handle.Target is JSHostImplementation.ToManagedCallback callback) |
| 52 | + { |
| 53 | + // arg_2, arg_3, arg_4, arg_res are processed by the callback |
| 54 | + callback(arguments_buffer); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + throw new InvalidOperationException(SR.NullToManagedCallback); |
| 59 | + } |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + arg_exc.ToJS(ex); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_CompleteTask")] |
| 68 | + // the marshaled signature is: void CompleteTask<T>(GCHandle holder, Exception? exceptionResult, T? result) |
| 69 | + public static void CompleteTask(JSMarshalerArgument* arguments_buffer) |
| 70 | + { |
| 71 | + ref JSMarshalerArgument arg_exc = ref arguments_buffer[0]; // initialized by caller in alloc_stack_frame() |
| 72 | + ref JSMarshalerArgument arg_res = ref arguments_buffer[1]; // initialized by caller in alloc_stack_frame() |
| 73 | + ref JSMarshalerArgument arg_1 = ref arguments_buffer[2];// initialized and set by caller |
| 74 | + // arg_2 set by caller when this is SetException call |
| 75 | + // arg_3 set by caller when this is SetResult call |
| 76 | + |
| 77 | + try |
| 78 | + { |
| 79 | + // when we arrive here, we are on the thread which owns the proxies or on IO thread |
| 80 | + var ctx = arg_exc.ToManagedContext; |
| 81 | + var holder = ctx.GetPromiseHolder(arg_1.slot.GCHandle); |
| 82 | + JSHostImplementation.ToManagedCallback callback; |
| 83 | + |
| 84 | + callback = holder.Callback!; |
| 85 | + ctx.ReleasePromiseHolder(arg_1.slot.GCHandle); |
| 86 | + |
| 87 | + // arg_2, arg_3 are processed by the callback |
| 88 | + // JSProxyContext.PopOperation() is called by the callback |
| 89 | + callback!(arguments_buffer); |
| 90 | + } |
| 91 | + catch (Exception ex) |
| 92 | + { |
| 93 | + Environment.FailFast($"CompleteTask: Unexpected synchronous failure (ManagedThreadId {Environment.CurrentManagedThreadId}): " + ex); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_GetManagedStackTrace")] |
| 98 | + // the marshaled signature is: string GetManagedStackTrace(GCHandle exception) |
| 99 | + public static void GetManagedStackTrace(JSMarshalerArgument* arguments_buffer) |
| 100 | + { |
| 101 | + ref JSMarshalerArgument arg_exc = ref arguments_buffer[0]; // initialized by caller in alloc_stack_frame() |
| 102 | + ref JSMarshalerArgument arg_res = ref arguments_buffer[1]; // used as return value |
| 103 | + ref JSMarshalerArgument arg_1 = ref arguments_buffer[2];// initialized and set by caller |
| 104 | + try |
| 105 | + { |
| 106 | + // when we arrive here, we are on the thread which owns the proxies |
| 107 | + arg_exc.AssertCurrentThreadContext(); |
| 108 | + |
| 109 | + GCHandle exception_gc_handle = (GCHandle)arg_1.slot.GCHandle; |
| 110 | + if (exception_gc_handle.Target is Exception exception) |
| 111 | + { |
| 112 | + arg_res.ToJS(exception.StackTrace); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + throw new InvalidOperationException(SR.UnableToResolveHandleAsException); |
| 117 | + } |
| 118 | + } |
| 119 | + catch (Exception ex) |
| 120 | + { |
| 121 | + arg_exc.ToJS(ex); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_BindAssemblyExports")] |
| 126 | + // the marshaled signature is: Task BindAssemblyExports(string assemblyName) |
| 127 | + public static void BindAssemblyExports(JSMarshalerArgument* arguments_buffer) |
| 128 | + { |
| 129 | + ref JSMarshalerArgument arg_exc = ref arguments_buffer[0]; // initialized by caller in alloc_stack_frame() |
| 130 | + ref JSMarshalerArgument arg_res = ref arguments_buffer[1]; // used as return value |
| 131 | + ref JSMarshalerArgument arg_1 = ref arguments_buffer[2];// initialized and set by caller |
| 132 | + try |
| 133 | + { |
| 134 | + string? assemblyName; |
| 135 | + // when we arrive here, we are on the thread which owns the proxies |
| 136 | + arg_exc.AssertCurrentThreadContext(); |
| 137 | + arg_1.ToManaged(out assemblyName); |
| 138 | + |
| 139 | + var result = JSHostImplementation.BindAssemblyExports(assemblyName); |
| 140 | + |
| 141 | + arg_res.ToJS(result); |
| 142 | + } |
| 143 | + catch (Exception ex) |
| 144 | + { |
| 145 | + Environment.FailFast($"BindAssemblyExports: Unexpected synchronous failure (ManagedThreadId {Environment.CurrentManagedThreadId}): " + ex); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + [UnmanagedCallersOnly(EntryPoint = "SystemInteropJS_InvokeJSExport")] |
| 150 | + // the marshaled signature is: Task BindAssemblyExports(string assemblyName) |
| 151 | + public static void InvokeJSExport(JSMarshalerArgument* arguments_buffer) |
| 152 | + { |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments