public static void InvokeJS( string methodName, object paramStruct, [System.Runtime.CompilerServices.CallerMemberName] string?memberName = null ) { var paramStructType = paramStruct.GetType(); var paramSize = MarshalSizeOfHelper.SizeOf(paramStructType); 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 object InvokeJS( string methodName, object paramStruct, Type retStructType, [System.Runtime.CompilerServices.CallerMemberName] string?memberName = null ) { var paramStructType = paramStruct.GetType(); var returnSize = MarshalSizeOfHelper.SizeOf(retStructType); var paramSize = MarshalSizeOfHelper.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); } }