From e08702a235fbd8f4c0a9998cda44074fba8f605b Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 27 Jun 2021 20:58:43 +0100 Subject: [PATCH] LibJS: Add content type check to InitializeTypedArrayFromTypedArray() Resolves a FIXME. --- Userland/Libraries/LibJS/Runtime/ErrorTypes.h | 1 + Userland/Libraries/LibJS/Runtime/TypedArray.cpp | 6 +++++- .../Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h index fd25ea2077..1dbc7c71c8 100644 --- a/Userland/Libraries/LibJS/Runtime/ErrorTypes.h +++ b/Userland/Libraries/LibJS/Runtime/ErrorTypes.h @@ -154,6 +154,7 @@ M(ThisIsAlreadyInitialized, "|this| is already initialized") \ M(ToObjectNullOrUndefined, "ToObject on null or undefined") \ M(ToPrimitiveReturnedObject, "Can't convert {} to primitive with hint \"{}\", its @@toPrimitive method returned an object") \ + M(TypedArrayContentTypeMismatch, "Can't create {} from {}") \ M(TypedArrayInvalidBufferLength, "Invalid buffer length for {}: must be a multiple of {}, got {}") \ M(TypedArrayInvalidByteOffset, "Invalid byte offset for {}: must be a multiple of {}, got {}") \ M(TypedArrayOutOfRangeByteOffset, "Typed array byte offset {} is out of range for buffer with length {}") \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp index f42bc1fa3b..867a34a90a 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp @@ -103,7 +103,6 @@ static void initialize_typed_array_from_typed_array(GlobalObject& global_object, return; } - // FIXME: 17.c If src_array.[[ContentType]] != dest_array.[[ContentType]], throw a TypeError exception. auto data = ArrayBuffer::create(global_object, byte_length.value()); if (src_data->is_detached()) { @@ -111,6 +110,11 @@ static void initialize_typed_array_from_typed_array(GlobalObject& global_object, return; } + if (src_array.content_type() != dest_array.content_type()) { + vm.throw_exception(global_object, ErrorType::TypedArrayContentTypeMismatch, dest_array.class_name(), src_array.class_name()); + return; + } + dest_array.set_viewed_array_buffer(data); dest_array.set_byte_length(byte_length.value()); dest_array.set_byte_offset(0); diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js index a9fe088c52..65642f2ce7 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.js @@ -158,7 +158,7 @@ test("typed array from TypedArray", () => { BIGINT_TYPED_ARRAYS.forEach(T => { expect(() => { const newTypedArray = new T(u8Array); - }).toThrowWithMessage(TypeError, "Cannot convert number to BigInt"); + }).toThrowWithMessage(TypeError, `Can't create ${T.name} from Uint8Array`); const newBigIntTypedArray = new T(bigU64Array); expect(newBigIntTypedArray[0]).toBe(1n);