diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj index 7ef580cd5ea70..f67b0cd3b0346 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj @@ -29,6 +29,7 @@ + diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs index 2fbf3f448f5cd..e98a845b758f2 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs @@ -8,7 +8,7 @@ namespace System.Runtime.InteropServices.JavaScript { // this maps to src\mono\wasm\runtime\corebindings.ts - // the methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction + // the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction internal static unsafe partial class JavaScriptExports { [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/LegacyExports.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/LegacyExports.cs index 757eddc9aef45..6aa4a5acf2e57 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/LegacyExports.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/LegacyExports.cs @@ -10,7 +10,7 @@ namespace System.Runtime.InteropServices.JavaScript { // this maps to src\mono\wasm\runtime\legacy\corebindings.ts - // the methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction + // the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction internal static unsafe partial class LegacyExports { [MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 @@ -56,7 +56,7 @@ public static IntPtr TryGetCSOwnedObjectJSHandleRef(in object rawObj, int should } [MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 - public static void CreateCSOwnedProxyRef(nint jsHandle, JSHostImplementation.MappedType mappedType, int shouldAddInflight, out JSObject jsObject) + public static void CreateCSOwnedProxyRef(nint jsHandle, LegacyHostImplementation.MappedType mappedType, int shouldAddInflight, out JSObject jsObject) { JSObject? res = null; @@ -69,12 +69,12 @@ public static void CreateCSOwnedProxyRef(nint jsHandle, JSHostImplementation.Map #pragma warning disable CS0612 // Type or member is obsolete res = mappedType switch { - JSHostImplementation.MappedType.JSObject => new JSObject(jsHandle), - JSHostImplementation.MappedType.Array => new Array(jsHandle), - JSHostImplementation.MappedType.ArrayBuffer => new ArrayBuffer(jsHandle), - JSHostImplementation.MappedType.DataView => new DataView(jsHandle), - JSHostImplementation.MappedType.Function => new Function(jsHandle), - JSHostImplementation.MappedType.Uint8Array => new Uint8Array(jsHandle), + LegacyHostImplementation.MappedType.JSObject => new JSObject(jsHandle), + LegacyHostImplementation.MappedType.Array => new Array(jsHandle), + LegacyHostImplementation.MappedType.ArrayBuffer => new ArrayBuffer(jsHandle), + LegacyHostImplementation.MappedType.DataView => new DataView(jsHandle), + LegacyHostImplementation.MappedType.Function => new Function(jsHandle), + LegacyHostImplementation.MappedType.Uint8Array => new Uint8Array(jsHandle), _ => throw new ArgumentOutOfRangeException(nameof(mappedType)) }; #pragma warning restore CS0612 // Type or member is obsolete @@ -265,8 +265,8 @@ public static string GetCallSignatureRef(IntPtr _methodHandle, in object objForR for (int i = 0; i < parmsLength; i++) { Type t = parms[i].ParameterType; - var mt = JSHostImplementation.GetMarshalTypeFromType(t); - result[i] = JSHostImplementation.GetCallSignatureCharacterForMarshalType(mt, null); + var mt = LegacyHostImplementation.GetMarshalTypeFromType(t); + result[i] = LegacyHostImplementation.GetCallSignatureCharacterForMarshalType(mt, null); } return new string(result); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs index 3009089133e70..83247d1976027 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Types.cs @@ -24,61 +24,5 @@ public struct IntPtrAndHandle [FieldOffset(0)] internal RuntimeTypeHandle typeHandle; } - - // see src/mono/wasm/driver.c MARSHAL_TYPE_xxx - public enum MarshalType : int - { - NULL = 0, - INT = 1, - FP64 = 2, - STRING = 3, - VT = 4, - DELEGATE = 5, - TASK = 6, - OBJECT = 7, - BOOL = 8, - ENUM = 9, - URI = 22, - SAFEHANDLE = 23, - ARRAY_BYTE = 10, - ARRAY_UBYTE = 11, - ARRAY_UBYTE_C = 12, - ARRAY_SHORT = 13, - ARRAY_USHORT = 14, - ARRAY_INT = 15, - ARRAY_UINT = 16, - ARRAY_FLOAT = 17, - ARRAY_DOUBLE = 18, - FP32 = 24, - UINT32 = 25, - INT64 = 26, - UINT64 = 27, - CHAR = 28, - STRING_INTERNED = 29, - VOID = 30, - ENUM64 = 31, - POINTER = 32 - } - - // see src/mono/wasm/driver.c MARSHAL_ERROR_xxx - public enum MarshalError : int - { - BUFFER_TOO_SMALL = 512, - NULL_CLASS_POINTER = 513, - NULL_TYPE_POINTER = 514, - UNSUPPORTED_TYPE = 515, - FIRST = BUFFER_TOO_SMALL - } - - // please keep BINDING wasm_type_symbol in sync - public enum MappedType - { - JSObject = 0, - Array = 1, - ArrayBuffer = 2, - DataView = 3, - Function = 4, - Uint8Array = 11, - } } } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs index 52876f984a02d..6994ca399aa74 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs @@ -13,21 +13,12 @@ namespace System.Runtime.InteropServices.JavaScript internal static partial class JSHostImplementation { private const string TaskGetResultName = "get_Result"; - private static readonly MethodInfo s_taskGetResultMethodInfo = typeof(Task<>).GetMethod(TaskGetResultName)!; + private static MethodInfo? s_taskGetResultMethodInfo; // we use this to maintain identity of JSHandle for a JSObject proxy public static readonly Dictionary> s_csOwnedObjects = new Dictionary>(); // we use this to maintain identity of GCHandle for a managed object public static Dictionary s_gcHandleFromJSOwnedObject = new Dictionary(ReferenceEqualityComparer.Instance); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void RegisterCSOwnedObject(JSObject proxy) - { - lock (s_csOwnedObjects) - { - s_csOwnedObjects[(int)proxy.JSHandle] = new WeakReference(proxy, trackResurrection: true); - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void ReleaseCSOwnedObject(nint jsHandle) { @@ -41,8 +32,7 @@ public static void ReleaseCSOwnedObject(nint jsHandle) } } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static object? GetTaskResult(Task task) + public static object? GetTaskResultDynamic(Task task) { MethodInfo method = GetTaskResultMethodInfo(task.GetType()); if (method != null) @@ -91,145 +81,6 @@ public static RuntimeMethodHandle GetMethodHandleFromIntPtr(IntPtr ptr) return temp.methodHandle; } - public static MarshalType GetMarshalTypeFromType(Type type) - { - if (type is null) - return MarshalType.VOID; - - var typeCode = Type.GetTypeCode(type); - if (type.IsEnum) - { - switch (typeCode) - { - case TypeCode.Int32: - case TypeCode.UInt32: - return MarshalType.ENUM; - case TypeCode.Int64: - case TypeCode.UInt64: - return MarshalType.ENUM64; - default: - throw new JSException($"Unsupported enum underlying type {typeCode}"); - } - } - - switch (typeCode) - { - case TypeCode.SByte: - case TypeCode.Int16: - case TypeCode.Int32: - return MarshalType.INT; - case TypeCode.Byte: - case TypeCode.UInt16: - case TypeCode.UInt32: - return MarshalType.UINT32; - case TypeCode.Boolean: - return MarshalType.BOOL; - case TypeCode.Int64: - return MarshalType.INT64; - case TypeCode.UInt64: - return MarshalType.UINT64; - case TypeCode.Single: - return MarshalType.FP32; - case TypeCode.Double: - return MarshalType.FP64; - case TypeCode.String: - return MarshalType.STRING; - case TypeCode.Char: - return MarshalType.CHAR; - } - - if (type.IsArray) - { - if (!type.IsSZArray) - throw new JSException("Only single-dimensional arrays with a zero lower bound can be marshaled to JS"); - - var elementType = type.GetElementType(); - switch (Type.GetTypeCode(elementType)) - { - case TypeCode.Byte: - return MarshalType.ARRAY_UBYTE; - case TypeCode.SByte: - return MarshalType.ARRAY_BYTE; - case TypeCode.Int16: - return MarshalType.ARRAY_SHORT; - case TypeCode.UInt16: - return MarshalType.ARRAY_USHORT; - case TypeCode.Int32: - return MarshalType.ARRAY_INT; - case TypeCode.UInt32: - return MarshalType.ARRAY_UINT; - case TypeCode.Single: - return MarshalType.ARRAY_FLOAT; - case TypeCode.Double: - return MarshalType.ARRAY_DOUBLE; - default: - throw new JSException($"Unsupported array element type {elementType}"); - } - } - else if (type == typeof(IntPtr)) - return MarshalType.POINTER; - else if (type == typeof(UIntPtr)) - return MarshalType.POINTER; - else if (type == typeof(SafeHandle)) - return MarshalType.SAFEHANDLE; - else if (typeof(Delegate).IsAssignableFrom(type)) - return MarshalType.DELEGATE; - else if ((type == typeof(Task)) || typeof(Task).IsAssignableFrom(type)) - return MarshalType.TASK; - else if (type.FullName == "System.Uri") - return MarshalType.URI; - else if (type.IsPointer) - return MarshalType.POINTER; - - if (type.IsValueType) - return MarshalType.VT; - else - return MarshalType.OBJECT; - } - - public static char GetCallSignatureCharacterForMarshalType(MarshalType t, char? defaultValue) - { - switch (t) - { - case MarshalType.BOOL: - return 'b'; - case MarshalType.UINT32: - case MarshalType.POINTER: - return 'I'; - case MarshalType.INT: - return 'i'; - case MarshalType.UINT64: - return 'L'; - case MarshalType.INT64: - return 'l'; - case MarshalType.FP32: - return 'f'; - case MarshalType.FP64: - return 'd'; - case MarshalType.STRING: - return 's'; - case MarshalType.URI: - return 'u'; - case MarshalType.SAFEHANDLE: - return 'h'; - case MarshalType.ENUM: - return 'j'; // this is wrong for uint enums - case MarshalType.ENUM64: - return 'k'; // this is wrong for ulong enums - case MarshalType.TASK: - case MarshalType.DELEGATE: - case MarshalType.OBJECT: - return 'o'; - case MarshalType.VT: - return 'a'; - default: - if (defaultValue.HasValue) - return defaultValue.Value; - else - throw new JSException($"Unsupported marshal type {t}"); - } - } - /// /// Gets the MethodInfo for the Task{T}.Result property getter. /// @@ -242,15 +93,19 @@ public static char GetCallSignatureCharacterForMarshalType(MarshalType t, char? /// ensuring that trimming doesn't change the application's behavior. /// [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070:UnrecognizedReflectionPattern", - Justification = "Task.Result is preserved by the ILLinker because _taskGetResultMethodInfo was initialized with it.")] + Justification = "Task.Result is preserved by the ILLinker because s_taskGetResultMethodInfo was initialized with it.")] public static MethodInfo GetTaskResultMethodInfo(Type taskType) { if (taskType != null) { - MethodInfo? result = taskType.GetMethod(TaskGetResultName); - if (result != null && result.HasSameMetadataDefinitionAs(s_taskGetResultMethodInfo)) + if (s_taskGetResultMethodInfo == null) + { + s_taskGetResultMethodInfo = typeof(Task<>).GetMethod(TaskGetResultName); + } + MethodInfo? getter = taskType.GetMethod(TaskGetResultName); + if (getter != null && getter.HasSameMetadataDefinitionAs(s_taskGetResultMethodInfo!)) { - return result; + return getter; } } @@ -269,7 +124,7 @@ public static void ThrowException(ref JSMarshalerArgument arg) throw new InvalidProgramException(); } - public static async Task ImportAsync(string moduleName, string moduleUrl, CancellationToken cancellationToken ) + public static async Task ImportAsync(string moduleName, string moduleUrl, CancellationToken cancellationToken) { Task modulePromise = JavaScriptImports.DynamicImport(moduleName, moduleUrl); var wrappedTask = CancelationHelper(modulePromise, cancellationToken); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Array.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Array.cs index 83ae782f8feae..47464304bcb0e 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Array.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Array.cs @@ -18,7 +18,7 @@ public class Array : JSObject public Array(params object[] _params) : base(JavaScriptImports.CreateCSOwnedObject(nameof(Array), _params)) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } /// diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/ArrayBuffer.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/ArrayBuffer.cs index 972bfea02f15b..e19dd5ffc6df8 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/ArrayBuffer.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/ArrayBuffer.cs @@ -13,7 +13,7 @@ public class ArrayBuffer : JSObject public ArrayBuffer(int length) : base(JavaScriptImports.CreateCSOwnedObject(nameof(ArrayBuffer), new object[] { length })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } /// diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/DataView.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/DataView.cs index 8cbdd9c493864..f43583e512a18 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/DataView.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/DataView.cs @@ -17,7 +17,7 @@ public class DataView : JSObject public DataView(ArrayBuffer buffer) : base(JavaScriptImports.CreateCSOwnedObject(nameof(DataView), new object[] { buffer })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } /// @@ -28,7 +28,7 @@ public DataView(ArrayBuffer buffer) public DataView(ArrayBuffer buffer, int byteOffset) : base(JavaScriptImports.CreateCSOwnedObject(nameof(DataView), new object[] { buffer, byteOffset })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } /// @@ -40,7 +40,7 @@ public DataView(ArrayBuffer buffer, int byteOffset) public DataView(ArrayBuffer buffer, int byteOffset, int byteLength) : base(JavaScriptImports.CreateCSOwnedObject(nameof(DataView), new object[] { buffer, byteOffset, byteLength })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } /// diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Function.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Function.cs index c6d0e89d156c0..2c778706b497e 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Function.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Function.cs @@ -18,7 +18,7 @@ public class Function : JSObject public Function(params object[] args) : base(JavaScriptImports.CreateCSOwnedObject(nameof(Function), args)) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } internal Function(IntPtr jsHandle) : base(jsHandle) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/LegacyHostImplementation.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/LegacyHostImplementation.cs new file mode 100644 index 0000000000000..815592ac8f7f7 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/LegacyHostImplementation.cs @@ -0,0 +1,217 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; +using System.Runtime.Versioning; +using System.Threading.Tasks; + +namespace System.Runtime.InteropServices.JavaScript +{ + [SupportedOSPlatform("browser")] + internal static class LegacyHostImplementation + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void RegisterCSOwnedObject(JSObject proxy) + { + lock (JSHostImplementation.s_csOwnedObjects) + { + JSHostImplementation.s_csOwnedObjects[(int)proxy.JSHandle] = new WeakReference(proxy, trackResurrection: true); + } + } + + public static MarshalType GetMarshalTypeFromType(Type type) + { + if (type is null) + return MarshalType.VOID; + + var typeCode = Type.GetTypeCode(type); + if (type.IsEnum) + { + switch (typeCode) + { + case TypeCode.Int32: + case TypeCode.UInt32: + return MarshalType.ENUM; + case TypeCode.Int64: + case TypeCode.UInt64: + return MarshalType.ENUM64; + default: + throw new JSException($"Unsupported enum underlying type {typeCode}"); + } + } + + switch (typeCode) + { + case TypeCode.SByte: + case TypeCode.Int16: + case TypeCode.Int32: + return MarshalType.INT; + case TypeCode.Byte: + case TypeCode.UInt16: + case TypeCode.UInt32: + return MarshalType.UINT32; + case TypeCode.Boolean: + return MarshalType.BOOL; + case TypeCode.Int64: + return MarshalType.INT64; + case TypeCode.UInt64: + return MarshalType.UINT64; + case TypeCode.Single: + return MarshalType.FP32; + case TypeCode.Double: + return MarshalType.FP64; + case TypeCode.String: + return MarshalType.STRING; + case TypeCode.Char: + return MarshalType.CHAR; + } + + if (type.IsArray) + { + if (!type.IsSZArray) + throw new JSException("Only single-dimensional arrays with a zero lower bound can be marshaled to JS"); + + var elementType = type.GetElementType(); + switch (Type.GetTypeCode(elementType)) + { + case TypeCode.Byte: + return MarshalType.ARRAY_UBYTE; + case TypeCode.SByte: + return MarshalType.ARRAY_BYTE; + case TypeCode.Int16: + return MarshalType.ARRAY_SHORT; + case TypeCode.UInt16: + return MarshalType.ARRAY_USHORT; + case TypeCode.Int32: + return MarshalType.ARRAY_INT; + case TypeCode.UInt32: + return MarshalType.ARRAY_UINT; + case TypeCode.Single: + return MarshalType.ARRAY_FLOAT; + case TypeCode.Double: + return MarshalType.ARRAY_DOUBLE; + default: + throw new JSException($"Unsupported array element type {elementType}"); + } + } + else if (type == typeof(IntPtr)) + return MarshalType.POINTER; + else if (type == typeof(UIntPtr)) + return MarshalType.POINTER; + else if (type == typeof(SafeHandle)) + return MarshalType.SAFEHANDLE; + else if (typeof(Delegate).IsAssignableFrom(type)) + return MarshalType.DELEGATE; + else if ((type == typeof(Task)) || typeof(Task).IsAssignableFrom(type)) + return MarshalType.TASK; + else if (type.FullName == "System.Uri") + return MarshalType.URI; + else if (type.IsPointer) + return MarshalType.POINTER; + + if (type.IsValueType) + return MarshalType.VT; + else + return MarshalType.OBJECT; + } + + public static char GetCallSignatureCharacterForMarshalType(MarshalType t, char? defaultValue) + { + switch (t) + { + case MarshalType.BOOL: + return 'b'; + case MarshalType.UINT32: + case MarshalType.POINTER: + return 'I'; + case MarshalType.INT: + return 'i'; + case MarshalType.UINT64: + return 'L'; + case MarshalType.INT64: + return 'l'; + case MarshalType.FP32: + return 'f'; + case MarshalType.FP64: + return 'd'; + case MarshalType.STRING: + return 's'; + case MarshalType.URI: + return 'u'; + case MarshalType.SAFEHANDLE: + return 'h'; + case MarshalType.ENUM: + return 'j'; // this is wrong for uint enums + case MarshalType.ENUM64: + return 'k'; // this is wrong for ulong enums + case MarshalType.TASK: + case MarshalType.DELEGATE: + case MarshalType.OBJECT: + return 'o'; + case MarshalType.VT: + return 'a'; + default: + if (defaultValue.HasValue) + return defaultValue.Value; + else + throw new JSException($"Unsupported marshal type {t}"); + } + } + + // see src/mono/wasm/driver.c MARSHAL_TYPE_xxx + public enum MarshalType : int + { + NULL = 0, + INT = 1, + FP64 = 2, + STRING = 3, + VT = 4, + DELEGATE = 5, + TASK = 6, + OBJECT = 7, + BOOL = 8, + ENUM = 9, + URI = 22, + SAFEHANDLE = 23, + ARRAY_BYTE = 10, + ARRAY_UBYTE = 11, + ARRAY_UBYTE_C = 12, + ARRAY_SHORT = 13, + ARRAY_USHORT = 14, + ARRAY_INT = 15, + ARRAY_UINT = 16, + ARRAY_FLOAT = 17, + ARRAY_DOUBLE = 18, + FP32 = 24, + UINT32 = 25, + INT64 = 26, + UINT64 = 27, + CHAR = 28, + STRING_INTERNED = 29, + VOID = 30, + ENUM64 = 31, + POINTER = 32 + } + + // see src/mono/wasm/driver.c MARSHAL_ERROR_xxx + public enum MarshalError : int + { + BUFFER_TOO_SMALL = 512, + NULL_CLASS_POINTER = 513, + NULL_TYPE_POINTER = 514, + UNSUPPORTED_TYPE = 515, + FIRST = BUFFER_TOO_SMALL + } + + // please keep BINDING wasm_type_symbol in sync + public enum MappedType + { + JSObject = 0, + Array = 1, + ArrayBuffer = 2, + DataView = 3, + Function = 4, + Uint8Array = 11, + } + } +} diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Runtime.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Runtime.cs index d89584533bfdb..560469891f291 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Runtime.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Runtime.cs @@ -9,8 +9,6 @@ namespace System.Runtime.InteropServices.JavaScript [Obsolete] public static class Runtime { - [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.JavaScriptExports", "System.Runtime.InteropServices.JavaScript")] - [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.LegacyExports", "System.Runtime.InteropServices.JavaScript")] public static object GetGlobalObject(string str) => JavaScriptImports.GetGlobalObject(str); @@ -35,7 +33,6 @@ public static object GetGlobalObject(string str) /// /// [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 - [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.JavaScriptExports", "System.Runtime.InteropServices.JavaScript")] public static object Invoke(this JSObject self, string method, params object?[] args) { ArgumentNullException.ThrowIfNull(self); @@ -71,7 +68,6 @@ public static object Invoke(this JSObject self, string method, params object?[] /// /// [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 - [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.JavaScriptExports", "System.Runtime.InteropServices.JavaScript")] public static object GetObjectProperty(this JSObject self, string name) { ArgumentNullException.ThrowIfNull(self); @@ -97,7 +93,6 @@ public static object GetObjectProperty(this JSObject self, string name) /// Defaults to and creates the property on the javascript object if not found, if set to it will not create the property if it does not exist. If the property exists, the value is updated with the provided value. /// [MethodImpl(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425 - [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.JavaScriptExports", "System.Runtime.InteropServices.JavaScript")] public static void SetObjectProperty(this JSObject self, string name, object? value, bool createIfNotExists = true, bool hasOwnProperty = false) { ArgumentNullException.ThrowIfNull(self); diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Uint8Array.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Uint8Array.cs index 6bea6d54d5d02..e01202b239834 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Uint8Array.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Uint8Array.cs @@ -11,13 +11,13 @@ public sealed class Uint8Array : JSObject public Uint8Array(int length) : base(JavaScriptImports.CreateCSOwnedObject(nameof(Uint8Array), new object[] { length })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } public Uint8Array(ArrayBuffer buffer) : base(JavaScriptImports.CreateCSOwnedObject(nameof(Uint8Array), new object[] { buffer })) { - JSHostImplementation.RegisterCSOwnedObject(this); + LegacyHostImplementation.RegisterCSOwnedObject(this); } internal Uint8Array(IntPtr jsHandle) : base(jsHandle) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Task.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Task.cs index 9c3edf3790ec6..cfb135fc8a8d2 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Task.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Marshaling/JSMarshalerArgument.Task.cs @@ -128,7 +128,7 @@ internal void ToJSDynamic(Task? value) } else { - object? result = JSHostImplementation.GetTaskResult(task); + object? result = JSHostImplementation.GetTaskResultDynamic(task); slot.JSHandle = CreateResolvedPromise(result, MarshalResult); return; } @@ -161,7 +161,7 @@ void Complete() } else { - object? result = JSHostImplementation.GetTaskResult(task); + object? result = JSHostImplementation.GetTaskResultDynamic(task); ResolvePromise(promise, result, MarshalResult); }