diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-07-02 16:51:02 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-07-02 15:53:51 +0100 |
commit | f8f3ff65fe58f2a0f21f384c4ac85936781a4163 (patch) | |
tree | 6c2dc02d084e550edcab790dd32fe46e41d8edac /Userland/Libraries/LibJS | |
parent | e6f0b2d817d04c6af88c6f9f61f04c40e343f64e (diff) | |
download | serenity-f8f3ff65fe58f2a0f21f384c4ac85936781a4163.zip |
LibJS: Bring %TypedArray%.prototype.set slightly closer to spec
Specifically, instead of using the internal {get, put}_by_index methods
we now use the GetValueFromBuffer and SetValueInBuffer abstract
operations, as required by the specification.
While i was here i also replaced a couple custom detached array buffer
error messages with the existing ErrorType::DetachedArrayBuffer.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArray.h | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp | 14 |
2 files changed, 15 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.h b/Userland/Libraries/LibJS/Runtime/TypedArray.h index 247c10ab4f..c5ef9a28e7 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArray.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArray.h @@ -35,8 +35,11 @@ public: virtual size_t element_size() const = 0; virtual String element_name() const = 0; - virtual bool put_by_index(u32 property_index, Value value) override = 0; - virtual Value get_by_index(u32 property_index, AllowSideEffects = AllowSideEffects::Yes) const override = 0; + + // 25.1.2.10 GetValueFromBuffer ( arrayBuffer, byteIndex, type, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getvaluefrombuffer + virtual Value get_value_from_buffer(size_t byte_index, ArrayBuffer::Order, bool is_little_endian = true) const = 0; + // 25.1.2.12 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-setvalueinbuffer + virtual void set_value_in_buffer(size_t byte_index, Value, ArrayBuffer::Order, bool is_little_endian = true) = 0; protected: explicit TypedArrayBase(Object& prototype) @@ -140,6 +143,9 @@ public: virtual size_t element_size() const override { return sizeof(UnderlyingBufferDataType); }; + Value get_value_from_buffer(size_t byte_index, ArrayBuffer::Order order, bool is_little_endian = true) const override { return viewed_array_buffer()->template get_value<T>(byte_index, true, order, is_little_endian); } + void set_value_in_buffer(size_t byte_index, Value value, ArrayBuffer::Order order, bool is_little_endian = true) override { viewed_array_buffer()->template set_value<T>(byte_index, value, true, order, is_little_endian); } + protected: TypedArray(u32 array_length, Object& prototype) : TypedArrayBase(prototype) diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 591c8f0888..e7b3e5bab1 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -343,7 +343,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) // 23.2.3.23.1 SetTypedArrayFromTypedArray ( target, targetOffset, source ), https://tc39.es/ecma262/#sec-settypedarrayfromtypedarray auto target_buffer = typed_array->viewed_array_buffer(); if (target_buffer->is_detached()) { - vm.throw_exception<JS::TypeError>(global_object, "Target array is detached"); + vm.throw_exception<JS::TypeError>(global_object, ErrorType::DetachedArrayBuffer); return {}; } auto target_length = typed_array->array_length(); @@ -351,7 +351,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) auto source_buffer = source_typed_array.viewed_array_buffer(); if (source_buffer->is_detached()) { - vm.throw_exception<JS::TypeError>(global_object, "Source array is detached"); + vm.throw_exception<JS::TypeError>(global_object, ErrorType::DetachedArrayBuffer); return {}; } auto source_length = source_typed_array.array_length(); @@ -407,8 +407,8 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) target_buffer->buffer().overwrite(target_byte_index, source_buffer->buffer().data(), limit - target_byte_index); } else { while (target_byte_index < limit) { - auto value = source_typed_array.get_by_index(source_byte_index); - typed_array->put_by_index(target_byte_index, value); + auto value = source_typed_array.get_value_from_buffer(source_byte_index, ArrayBuffer::Unordered); + typed_array->set_value_in_buffer(target_byte_index, value, ArrayBuffer::Unordered); source_byte_index += source_typed_array.element_size(); target_byte_index += typed_array->element_size(); } @@ -417,7 +417,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) // 23.2.3.23.2 SetTypedArrayFromArrayLike ( target, targetOffset, source ), https://tc39.es/ecma262/#sec-settypedarrayfromarraylike auto target_buffer = typed_array->viewed_array_buffer(); if (target_buffer->is_detached()) { - vm.throw_exception<JS::TypeError>(global_object, "Target array is detached"); + vm.throw_exception<JS::TypeError>(global_object, ErrorType::DetachedArrayBuffer); return {}; } auto target_length = typed_array->array_length(); @@ -474,11 +474,11 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set) return {}; if (target_buffer->is_detached()) { - vm.throw_exception<JS::TypeError>(global_object, "Target array is detached"); + vm.throw_exception<JS::TypeError>(global_object, ErrorType::DetachedArrayBuffer); return {}; } - typed_array->put_by_index(target_byte_index, value); + typed_array->set_value_in_buffer(target_byte_index, value, ArrayBuffer::Unordered); ++k; target_byte_index += typed_array->element_size(); } |