diff --git a/Jint/Native/JsTypedArray.cs b/Jint/Native/JsTypedArray.cs index 3be0551385..2ef0879d71 100644 --- a/Jint/Native/JsTypedArray.cs +++ b/Jint/Native/JsTypedArray.cs @@ -426,8 +426,18 @@ internal bool IsValidIntegerIndex(int index) return true; } + internal T[] ToNativeArray() { + if (_byteOffset == 0 && typeof(T) == typeof(byte) && _arrayElementType == TypedArrayElementType.Uint8) + { + var result = _viewedArrayBuffer._arrayBufferData.AsSpan().Slice(0, (int) GetLength()).ToArray() as T[]; + if (result != null) + { + return result; + } + } + var conversionType = typeof(T); var elementSize = _arrayElementType.GetElementSize(); var byteOffset = _byteOffset; diff --git a/Jint/Native/TypedArray/TypedArrayConstructor.Uint8Array.cs b/Jint/Native/TypedArray/TypedArrayConstructor.Uint8Array.cs index 16f9555af5..16d25cd919 100644 --- a/Jint/Native/TypedArray/TypedArrayConstructor.Uint8Array.cs +++ b/Jint/Native/TypedArray/TypedArrayConstructor.Uint8Array.cs @@ -33,7 +33,7 @@ protected override void Initialize() public JsTypedArray Construct(ReadOnlySpan values) { var array = (JsTypedArray) base.Construct([values.Length], this); - FillTypedArrayInstance(array, values); + values.CopyTo(array._viewedArrayBuffer._arrayBufferData.AsSpan()); return array; } diff --git a/Jint/Runtime/Interop/MethodInfoFunction.cs b/Jint/Runtime/Interop/MethodInfoFunction.cs index b7fba23944..72a6f9c5bc 100644 --- a/Jint/Runtime/Interop/MethodInfoFunction.cs +++ b/Jint/Runtime/Interop/MethodInfoFunction.cs @@ -185,6 +185,7 @@ static JsCallArguments ArgumentProvider(MethodDescriptor method, MethodResolverS var methodParameter = methodParameters[i]; var parameterType = methodParameter.ParameterType; var argument = arguments.Length > i ? arguments[i] : null; + object? argumentObject = null; if (typeof(JsValue).IsAssignableFrom(parameterType)) { @@ -195,9 +196,11 @@ static JsCallArguments ArgumentProvider(MethodDescriptor method, MethodResolverS // optional parameters[i] = System.Type.Missing; } - else if (IsGenericParameter(argument.ToObject(), parameterType)) // don't think we need the condition preface of (argument == null) because of earlier condition + else if (IsGenericParameter(argumentObject = argument.ToObject(), parameterType)) // don't think we need the condition preface of (argument == null) because of earlier condition { - parameters[i] = argument.ToObject(); + // ^ this is the best way I could think of to only eval argument.ToObject() when needed. + // But it's very cursed, I'm sorry. + parameters[i] = argumentObject; } else if (parameterType == typeof(JsValue[]) && argument.IsArray()) { @@ -215,7 +218,7 @@ static JsCallArguments ArgumentProvider(MethodDescriptor method, MethodResolverS else { if (!ReflectionExtensions.TryConvertViaTypeCoercion(parameterType, _engine.Options.Interop.ValueCoercion, argument, out parameters[i]) - && !converter.TryConvert(argument.ToObject(), parameterType, CultureInfo.InvariantCulture, out parameters[i])) + && !converter.TryConvert(argumentObject, parameterType, CultureInfo.InvariantCulture, out parameters[i])) { argumentsMatch = false; break;