/// <summary> /// Implementation of the argument marshaling. /// It's used by JSImport code generator and should not be used by developers in source code. /// </summary> public unsafe void ToManaged(out Task?value) { if (slot.Type == MarshalerType.None) { value = null; return; } GCHandle gcHandle = (GCHandle)slot.GCHandle; JSHostImplementation.TaskCallback?holder = (JSHostImplementation.TaskCallback?)gcHandle.Target; if (holder == null) { throw new NullReferenceException("JSHostImplementation.TaskCallback"); } TaskCompletionSource tcs = new TaskCompletionSource(gcHandle); JSHostImplementation.ToManagedCallback callback = (JSMarshalerArgument * arguments_buffer) => { ref JSMarshalerArgument arg_2 = ref arguments_buffer[3]; // set by caller when this is SetException call // arg_3 set by caller when this is SetResult call, un-used here if (arg_2.slot.Type != MarshalerType.None) { arg_2.ToManaged(out Exception? fail); tcs.SetException(fail !); } else { tcs.SetResult(); } // eventual exception is handled by caller };
public static void ThrowException(ref JSMarshalerArgument arg) { arg.ToManaged(out Exception? ex); if (ex != null) { throw ex; } throw new InvalidProgramException(); }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 public static void MarshalPromise(Span <JSMarshalerArgument> arguments) { fixed(JSMarshalerArgument *ptr = arguments) { Interop.Runtime.MarshalPromise(ptr); ref JSMarshalerArgument exceptionArg = ref arguments[0]; if (exceptionArg.slot.Type != MarshalerType.None) { JSHostImplementation.ThrowException(ref exceptionArg); } }
internal static unsafe void InvokeJSImpl(JSObject jsFunction, Span <JSMarshalerArgument> arguments) { IntPtr functionJSHandle = jsFunction.JSHandle; fixed(JSMarshalerArgument *ptr = arguments) { Interop.Runtime.InvokeJSFunction(functionJSHandle, ptr); ref JSMarshalerArgument exceptionArg = ref arguments[0]; if (exceptionArg.slot.Type != MarshalerType.None) { JSHostImplementation.ThrowException(ref exceptionArg); } }
public unsafe void ToManaged(out JSObject?[]?value) { if (slot.Type == MarshalerType.None) { value = null; return; } value = new JSObject?[slot.Length]; JSMarshalerArgument *payload = (JSMarshalerArgument *)slot.IntPtrValue; for (int i = 0; i < slot.Length; i++) { ref JSMarshalerArgument arg = ref payload[i]; JSObject?val; arg.ToManaged(out val); value[i] = val; }
/// <summary> /// Implementation of the argument marshaling. /// It's used by JSImport code generator and should not be used by developers in source code. /// </summary> public unsafe void ToManaged(out Task?value) { if (slot.Type == MarshalerType.None) { value = null; return; } GCHandle gcHandle = (GCHandle)slot.GCHandle; JSHostImplementation.TaskCallback?holder = (JSHostImplementation.TaskCallback?)gcHandle.Target; if (holder == null) { throw new NullReferenceException("JSHostImplementation.TaskCallback"); } TaskCompletionSource tcs = new TaskCompletionSource(gcHandle); JSHostImplementation.ToManagedCallback callback = (JSMarshalerArgument * arguments_buffer) => { ref JSMarshalerArgument arg_exception = ref arguments_buffer[0]; try { if (arg_exception.slot.Type != MarshalerType.None) { arg_exception.ToManaged(out Exception? fail); tcs.SetException(fail !); } else { tcs.SetResult(); } arg_exception.slot.Type = MarshalerType.None; } catch (Exception ex) { arg_exception.ToJS(ex); } };