public unsafe void ToJS(Exception?value)
        {
            if (value == null)
            {
                slot.Type = MarshalerType.None;
            }
            else
            {
                Exception cpy = value;
                if (cpy is AggregateException ae && ae.InnerExceptions.Count == 1)
                {
                    cpy = ae.InnerExceptions[0];
                }

                var jse = cpy as JSException;
                if (jse != null && jse.jsException != null)
                {
                    // this is JSException roundtrip
                    if (jse.jsException.IsDisposed)
                    {
                        throw new ObjectDisposedException(nameof(value));
                    }
                    slot.Type     = MarshalerType.JSException;
                    slot.JSHandle = jse.jsException.JSHandle;
                }
                else
                {
                    ToJS(cpy.Message);
                    slot.Type     = MarshalerType.Exception;
                    slot.GCHandle = JSHostImplementation.GetJSOwnedObjectGCHandle(cpy);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Gets or sets the Array with the index specified by <paramref name="i" />.
        /// </summary>
        /// <param name="i">The index.</param>
        public object this[int i]
        {
            [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
            get
            {
                this.AssertNotDisposed();

                Interop.Runtime.GetByIndexRef(JSHandle, i, out int exception, out object indexValue);

                if (exception != 0)
                {
                    throw new JSException((string)indexValue);
                }
                JSHostImplementation.ReleaseInFlight(indexValue);
                return(indexValue);
            }
            [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
            set
            {
                this.AssertNotDisposed();

                Interop.Runtime.SetByIndexRef(JSHandle, i, value, out int exception, out object res);

                if (exception != 0)
                {
                    throw new JSException((string)res);
                }
            }
        }
        public unsafe void ToManaged(out Exception?value)
        {
            if (slot.Type == MarshalerType.None)
            {
                value = null;
                return;
            }
            if (slot.Type == MarshalerType.Exception)
            {
                // this is managed exception round-trip
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
                value = (Exception)((GCHandle)slot.GCHandle).Target;
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
                return;
            }

            JSObject?jsException = null;
            if (slot.JSHandle != IntPtr.Zero)
            {
                // this is JSException round-trip
                jsException = JSHostImplementation.CreateCSOwnedProxy(slot.JSHandle);
            }

            string?message;
            ToManaged(out message);

            value = new JSException(message !, jsException);
        }
Example #4
0
 public unsafe void ToManaged(out JSObject?value)
 {
     if (slot.Type == MarshalerType.None)
     {
         value = null;
         return;
     }
     value = JSHostImplementation.CreateCSOwnedProxy(slot.JSHandle);
 }
Example #5
0
 public static object Invoke(this JSObject self, string method, params object?[] args)
 {
     ArgumentNullException.ThrowIfNull(self);
     ObjectDisposedException.ThrowIf(self.IsDisposed, self);
     Interop.Runtime.InvokeJSWithArgsRef(self.JSHandle, method, args, out int exception, out object res);
     if (exception != 0)
     {
         throw new JSException((string)res);
     }
     JSHostImplementation.ReleaseInFlight(res);
     return(res);
 }
Example #6
0
        [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);
                }
            }
Example #7
0
        public static object GetObjectProperty(this JSObject self, string name)
        {
            ArgumentNullException.ThrowIfNull(self);
            ObjectDisposedException.ThrowIf(self.IsDisposed, self);

            Interop.Runtime.GetObjectPropertyRef(self.JSHandle, name, out int exception, out object propertyValue);
            if (exception != 0)
            {
                throw new JSException((string)propertyValue);
            }
            JSHostImplementation.ReleaseInFlight(propertyValue);
            return(propertyValue);
        }
        /// <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 ToJS(ArraySegment <byte> value)
        {
            if (value.Array == null)
            {
                slot.Type = MarshalerType.None;
                return;
            }
            slot.Type     = MarshalerType.ArraySegment;
            slot.GCHandle = JSHostImplementation.GetJSOwnedObjectGCHandleRef(value.Array, GCHandleType.Pinned);
            var refPtr = (IntPtr)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(value.Array));

            slot.IntPtrValue = refPtr + value.Offset;
            slot.Length      = value.Count;
        }
Example #9
0
        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);
                }
            }
Example #10
0
        public static object GetGlobalObject(string?str = null)
        {
            int exception;

            Interop.Runtime.GetGlobalObjectRef(str, out exception, out object jsObj);

            if (exception != 0)
            {
                throw new JSException($"Error obtaining a handle to global {str}");
            }

            JSHostImplementation.ReleaseInFlight(jsObj);
            return(jsObj);
        }
Example #11
0
 /// <summary>
 ///   Invoke a named method of the object, or throws a JSException on error.
 /// </summary>
 /// <param name="self">thisArg</param>
 /// <param name="method">The name of the method to invoke.</param>
 /// <param name="args">The argument list to pass to the invoke command.</param>
 /// <returns>
 ///   <para>
 ///     The return value can either be a primitive (string, int, double), a JSObject for JavaScript objects, a
 ///     System.Threading.Tasks.Task(object) for JavaScript promises, an array of
 ///     a byte, int or double (for Javascript objects typed as ArrayBuffer) or a
 ///     System.Func to represent JavaScript functions.  The specific version of
 ///     the Func that will be returned depends on the parameters of the Javascript function
 ///     and return value.
 ///   </para>
 ///   <para>
 ///     The value of a returned promise (The Task(object) return) can in turn be any of the above
 ///     valuews.
 ///   </para>
 /// </returns>
 public static object Invoke(this JSObject self, string method, params object?[] args)
 {
     if (self == null)
     {
         throw new ArgumentNullException(nameof(self));
     }
     if (self.IsDisposed)
     {
         throw new ObjectDisposedException($"Cannot access a disposed {self.GetType().Name}.");
     }
     Interop.Runtime.InvokeJSWithArgsRef(self.JSHandle, method, args, out int exception, out object res);
     if (exception != 0)
     {
         throw new JSException((string)res);
     }
     JSHostImplementation.ReleaseInFlight(res);
     return(res);
 }
Example #12
0
        /// <summary>
        ///   Returns the named property from the object, or throws a JSException on error.
        /// </summary>
        /// <param name="self">thisArg</param>
        /// <param name="name">The name of the property to lookup</param>
        /// <remarks>
        ///   This method can raise a JSException if fetching the property in Javascript raises an exception.
        /// </remarks>
        /// <returns>
        ///   <para>
        ///     The return value can either be a primitive (string, int, double), a
        ///     JSObject for JavaScript objects, a
        ///     System.Threading.Tasks.Task (object) for JavaScript promises, an array of
        ///     a byte, int or double (for Javascript objects typed as ArrayBuffer) or a
        ///     System.Func to represent JavaScript functions.  The specific version of
        ///     the Func that will be returned depends on the parameters of the Javascript function
        ///     and return value.
        ///   </para>
        ///   <para>
        ///     The value of a returned promise (The Task(object) return) can in turn be any of the above
        ///     valuews.
        ///   </para>
        /// </returns>
        public static object GetObjectProperty(this JSObject self, string name)
        {
            if (self == null)
            {
                throw new ArgumentNullException(nameof(self));
            }
            if (self.IsDisposed)
            {
                throw new ObjectDisposedException($"Cannot access a disposed {self.GetType().Name}.");
            }

            Interop.Runtime.GetObjectPropertyRef(self.JSHandle, name, out int exception, out object propertyValue);
            if (exception != 0)
            {
                throw new JSException((string)propertyValue);
            }
            JSHostImplementation.ReleaseInFlight(propertyValue);
            return(propertyValue);
        }
Example #13
0
 public Uint8Array(ArrayBuffer buffer)
     : base(JavaScriptImports.CreateCSOwnedObject(nameof(Uint8Array), new object[] { buffer }))
 {
     JSHostImplementation.RegisterCSOwnedObject(this);
 }
Example #14
0
 public Uint8Array(int length)
     : base(JavaScriptImports.CreateCSOwnedObject(nameof(Uint8Array), new object[] { length }))
 {
     JSHostImplementation.RegisterCSOwnedObject(this);
 }
Example #15
0
 /// <summary>
 /// Initializes a new instance of the Array class.
 /// </summary>
 /// <param name="_params">Parameters.</param>
 public Array(params object[] _params)
     : base(JavaScriptImports.CreateCSOwnedObject(nameof(Array), _params))
 {
     JSHostImplementation.RegisterCSOwnedObject(this);
 }
Example #16
0
 public static Task <JSObject> ImportAsync(string moduleName, string moduleUrl, CancellationToken cancellationToken = default)
 {
     return(JSHostImplementation.ImportAsync(moduleName, moduleUrl, cancellationToken));
 }