public static void InvokeJS <TParam>(
            string methodName,
            TParam paramStruct,
            [System.Runtime.CompilerServices.CallerMemberName] string memberName = null
            )
        {
            if (_logger.Value.IsEnabled(LogLevel.Debug))
            {
                _logger.Value.LogDebug($"InvokeJS for {memberName}/{typeof(TParam)}");
            }

            var pParms = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TParam)));

            try
            {
                Marshal.StructureToPtr(paramStruct, pParms, false);

                var ret = WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms);
            }
            catch (Exception e)
            {
                if (_logger.Value.IsEnabled(LogLevel.Error))
                {
                    _logger.Value.LogError($"Failed InvokeJS for {memberName}/{typeof(TParam)}: {e}");
                }
                throw;
            }
            finally
            {
                Marshal.DestroyStructure(pParms, typeof(TParam));
                Marshal.FreeHGlobal(pParms);
            }
        }
Beispiel #2
0
        public static void InvokeJS(
            string methodName,
            object paramStruct,
            [System.Runtime.CompilerServices.CallerMemberName] string?memberName = null
            )
        {
            var paramStructType = paramStruct.GetType();
            var paramSize       = Marshal.SizeOf(paramStruct);

            if (_logger.Value.IsEnabled(LogLevel.Trace))
            {
                _logger.Value.LogTrace($"InvokeJS for {memberName}/{paramStructType} (Alloc: {paramSize})");
            }

            var pParms = Marshal.AllocHGlobal(paramSize);

            DumpStructureLayout(paramStructType);

            Marshal.StructureToPtr(paramStruct, pParms, false);

            WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms, out var exception);

            Marshal.DestroyStructure(pParms, paramStructType);
            Marshal.FreeHGlobal(pParms);

            if (exception != null)
            {
                if (_logger.Value.IsEnabled(LogLevel.Error))
                {
                    _logger.Value.LogError($"Failed InvokeJS for {memberName}/{paramStructType}: {exception}");
                }

                throw exception;
            }
        }
        public static HandleRef <T> Allocate <T>(string propertySetterName, string?propertyResetName = null)
            where T : struct
        {
            var value = new HandleRef <T>(propertyResetName);

            try
            {
                WebAssemblyRuntime.InvokeJSUnmarshalled(propertySetterName, value.Handle);
            }
            catch (Exception e)
            {
                try
                {
                    value.Dispose();
                }
                // If the allocation failed, the dispose will most likely also fail,
                // but we want to propagate the real exception of the allocation!
                catch (Exception) { }

                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.Debug($"Failed Allocate {propertySetterName}/{value.Type}: {e}");
                }

                throw;
            }

            return(value);
        }
Beispiel #4
0
        public static TRet InvokeJS <TParam, TRet>(string methodName, TParam paramStruct) where TRet : new()
        {
            var pParms       = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TParam)));
            var pReturnValue = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TRet)));

            var returnValue = new TRet();

            try
            {
                Marshal.StructureToPtr(paramStruct, pParms, false);
                Marshal.StructureToPtr(returnValue, pReturnValue, false);

                var ret = WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms, pReturnValue);

                returnValue = (TRet)Marshal.PtrToStructure(pReturnValue, typeof(TRet));
                return(returnValue);
            }
            finally
            {
                Marshal.DestroyStructure(pParms, typeof(TParam));
                Marshal.FreeHGlobal(pParms);

                Marshal.DestroyStructure(pReturnValue, typeof(TRet));
                Marshal.FreeHGlobal(pReturnValue);
            }
        }
Beispiel #5
0
 partial void EnqueueNative()
 {
     if (DispatchOverride == null)
     {
         WebAssemblyRuntime.InvokeJSUnmarshalled("CoreDispatcher:WakeUp", IntPtr.Zero);
     }
     else
     {
         DispatchOverride(() => DispatchItems());
     }
 }
Beispiel #6
0
        public static object InvokeJS(
            string methodName,
            object paramStruct,
            Type retStructType,
            [System.Runtime.CompilerServices.CallerMemberName] string?memberName = null
            )
        {
            var paramStructType = paramStruct.GetType();

            var returnSize = Marshal.SizeOf(retStructType);
            var paramSize  = Marshal.SizeOf(paramStructType);

            if (_logger.Value.IsEnabled(LogLevel.Trace))
            {
                _logger.Value.LogTrace($"InvokeJS for {memberName}/{paramStructType}/{retStructType} (paramSize: {paramSize}, returnSize: {returnSize}");
            }

            DumpStructureLayout(paramStructType);
            DumpStructureLayout(retStructType);

            var pParms       = Marshal.AllocHGlobal(paramSize);
            var pReturnValue = Marshal.AllocHGlobal(returnSize);

            try
            {
                Marshal.StructureToPtr(paramStruct, pParms, false);

                var ret = WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms, pReturnValue);

                var returnValue = Marshal.PtrToStructure(pReturnValue, retStructType);
                return(returnValue);
            }
            catch (Exception e)
            {
                if (_logger.Value.IsEnabled(LogLevel.Error))
                {
                    _logger.Value.LogDebug($"Failed InvokeJS for {memberName}/{paramStructType}: {e}");
                }
                throw;
            }
            finally
            {
                Marshal.DestroyStructure(pParms, paramStructType);
                Marshal.FreeHGlobal(pParms);

                Marshal.DestroyStructure(pReturnValue, retStructType);
                Marshal.FreeHGlobal(pReturnValue);
            }
        }
Beispiel #7
0
        public static TRet InvokeJS <TParam, TRet>(
            string methodName,
            TParam paramStruct,
            [System.Runtime.CompilerServices.CallerMemberName] string memberName = null
            )
        {
            var returnSize = MarshalSizeOf <TRet> .Size;
            var paramSize  = MarshalSizeOf <TParam> .Size;

            if (_logger.Value.IsEnabled(LogLevel.Trace))
            {
                _logger.Value.LogTrace($"InvokeJS for {memberName}/{typeof(TParam)}/{typeof(TRet)} (paramSize: {paramSize}, returnSize: {returnSize}");
            }

            DumpStructureLayout <TParam>();
            DumpStructureLayout <TRet>();

            var pParms       = Marshal.AllocHGlobal(paramSize);
            var pReturnValue = Marshal.AllocHGlobal(returnSize);

            TRet returnValue = default;

            try
            {
                Marshal.StructureToPtr(paramStruct, pParms, false);
                Marshal.StructureToPtr(returnValue, pReturnValue, false);

                var ret = WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms, pReturnValue);

                returnValue = (TRet)Marshal.PtrToStructure(pReturnValue, typeof(TRet));
                return(returnValue);
            }
            catch (Exception e)
            {
                if (_logger.Value.IsEnabled(LogLevel.Error))
                {
                    _logger.Value.LogDebug($"Failed InvokeJS for {memberName}/{typeof(TParam)}: {e}");
                }
                throw;
            }
            finally
            {
                Marshal.DestroyStructure(pParms, typeof(TParam));
                Marshal.FreeHGlobal(pParms);

                Marshal.DestroyStructure(pReturnValue, typeof(TRet));
                Marshal.FreeHGlobal(pReturnValue);
            }
        }
Beispiel #8
0
        partial void EnqueueNative()
        {
            if (typeof(CoreDispatcher).Log().IsEnabled(LogLevel.Trace))
            {
                typeof(CoreDispatcher).Log().Trace($"[tid:{Thread.CurrentThread.ManagedThreadId}]: CoreDispatcher.EnqueueNative()");
            }

            if (DispatchOverride == null)
            {
                if (!IsThreadingSupported || (IsThreadingSupported && GetHasThreadAccess()))
                {
                    WebAssemblyRuntime.InvokeJSUnmarshalled("CoreDispatcher:WakeUp", IntPtr.Zero);
                }
                else
                {
Beispiel #9
0
        public static void InvokeJS <TParam>(string methodName, TParam paramStruct)
        {
            var pParms = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TParam)));

            try
            {
                Marshal.StructureToPtr(paramStruct, pParms, false);

                var ret = WebAssemblyRuntime.InvokeJSUnmarshalled(methodName, pParms);
            }
            finally
            {
                Marshal.DestroyStructure(pParms, typeof(TParam));
                Marshal.FreeHGlobal(pParms);
            }
        }
Beispiel #10
0
 partial void EnqueueNative()
 {
     if (DispatchOverride == null)
     {
         if (!IsThreadingSupported)
         {
             WebAssemblyRuntime.InvokeJSUnmarshalled("CoreDispatcher:WakeUp", IntPtr.Zero);
         }
         else
         {
             // The _backgroundWakeupTimer will do the dispatching.
         }
     }
     else
     {
         DispatchOverride(() => DispatchItems());
     }
 }