1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:37:34 +00:00

LibJS+LibWeb: Implement resizable ArrayBuffer support for TypedArray

This is (part of) a normative change in the ECMA-262 spec. See:
a9ae96e
This commit is contained in:
Timothy Flynn 2023-12-24 14:55:10 -05:00 committed by Andreas Kling
parent c7fec9424c
commit 9258d7b98a
47 changed files with 2059 additions and 884 deletions

View file

@ -38,6 +38,12 @@ ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
if (is<JS::TypedArrayBase>(buffer_source)) {
auto const& es_buffer_source = static_cast<JS::TypedArrayBase const&>(buffer_source);
auto typed_array_record = JS::make_typed_array_with_buffer_witness_record(es_buffer_source, JS::ArrayBuffer::Order::SeqCst);
// AD-HOC: The WebIDL spec has not been updated for resizable ArrayBuffer objects. This check follows the behavior of step 7.
if (JS::is_typed_array_out_of_bounds(typed_array_record))
return ByteBuffer {};
// 1. Set esArrayBuffer to esBufferSource.[[ViewedArrayBuffer]].
es_array_buffer = es_buffer_source.viewed_array_buffer();
@ -45,7 +51,7 @@ ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
offset = es_buffer_source.byte_offset();
// 3. Set length to esBufferSource.[[ByteLength]].
length = es_buffer_source.byte_length();
length = JS::typed_array_byte_length(typed_array_record);
} else if (is<JS::DataView>(buffer_source)) {
auto const& es_buffer_source = static_cast<JS::DataView const&>(buffer_source);

View file

@ -13,7 +13,16 @@ namespace Web::WebIDL {
u32 BufferableObjectBase::byte_length() const
{
return m_bufferable_object.visit([](auto& obj) { return static_cast<u32>(obj->byte_length()); });
return m_bufferable_object.visit(
[](JS::NonnullGCPtr<JS::TypedArrayBase> typed_array) {
auto typed_array_record = JS::make_typed_array_with_buffer_witness_record(typed_array, JS::ArrayBuffer::Order::SeqCst);
return JS::typed_array_byte_length(typed_array_record);
},
[](JS::NonnullGCPtr<JS::DataView> data_view) {
auto view_record = JS::make_data_view_with_buffer_witness_record(data_view, JS::ArrayBuffer::Order::SeqCst);
return JS::get_view_byte_length(view_record);
},
[](JS::NonnullGCPtr<JS::ArrayBuffer> array_buffer) { return static_cast<u32>(array_buffer->byte_length()); });
}
JS::NonnullGCPtr<JS::Object> BufferableObjectBase::raw_object()