From 84936c9ab69db94375097d431a81a0e6d79d90b3 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 27 Feb 2024 09:19:58 -0500 Subject: [PATCH] LibJS: Inline the ArrayBufferByteLength implementation Note: When we better support SharedArrayBuffer, that part of this AO might not be inlined, as it looks a bit expensive. On https://cyxx.github.io/another_js, this reduces the runtime of IsValidIntegerIndex from 12.5% to 11.5%. --- .../Libraries/LibJS/Runtime/ArrayBuffer.cpp | 16 ---------------- Userland/Libraries/LibJS/Runtime/ArrayBuffer.h | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp index d238b3e70c..64ab722f5c 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp @@ -181,22 +181,6 @@ ThrowCompletionOr allocate_array_buffer(VM& vm, FunctionObject& co return obj.ptr(); } -// 25.1.3.2 ArrayBufferByteLength ( arrayBuffer, order ), https://tc39.es/ecma262/#sec-arraybufferbytelength -size_t array_buffer_byte_length(ArrayBuffer const& array_buffer, ArrayBuffer::Order) -{ - // FIXME: 1. If IsSharedArrayBuffer(arrayBuffer) is true and arrayBuffer has an [[ArrayBufferByteLengthData]] internal slot, then - // FIXME: a. Let bufferByteLengthBlock be arrayBuffer.[[ArrayBufferByteLengthData]]. - // FIXME: b. Let rawLength be GetRawBytesFromSharedBlock(bufferByteLengthBlock, 0, biguint64, true, order). - // FIXME: c. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record. - // FIXME: d. Return ℝ(RawBytesToNumeric(biguint64, rawLength, isLittleEndian)). - - // 2. Assert: IsDetachedBuffer(arrayBuffer) is false. - VERIFY(!array_buffer.is_detached()); - - // 3. Return arrayBuffer.[[ArrayBufferByteLength]]. - return array_buffer.byte_length(); -} - // 25.1.3.4 DetachArrayBuffer ( arrayBuffer [ , key ] ), https://tc39.es/ecma262/#sec-detacharraybuffer ThrowCompletionOr detach_array_buffer(VM& vm, ArrayBuffer& array_buffer, Optional key) { diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h index fc5d6837d4..ab861d9f21 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -146,13 +146,28 @@ private: ThrowCompletionOr create_byte_data_block(VM& vm, size_t size); void copy_data_block_bytes(ByteBuffer& to_block, u64 to_index, ByteBuffer const& from_block, u64 from_index, u64 count); ThrowCompletionOr allocate_array_buffer(VM&, FunctionObject& constructor, size_t byte_length, Optional const& max_byte_length = {}); -size_t array_buffer_byte_length(ArrayBuffer const&, ArrayBuffer::Order); ThrowCompletionOr detach_array_buffer(VM&, ArrayBuffer& array_buffer, Optional key = {}); ThrowCompletionOr> get_array_buffer_max_byte_length_option(VM&, Value options); ThrowCompletionOr clone_array_buffer(VM&, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length); ThrowCompletionOr array_buffer_copy_and_detach(VM&, ArrayBuffer& array_buffer, Value new_length, PreserveResizability preserve_resizability); ThrowCompletionOr> allocate_shared_array_buffer(VM&, FunctionObject& constructor, size_t byte_length); +// 25.1.3.2 ArrayBufferByteLength ( arrayBuffer, order ), https://tc39.es/ecma262/#sec-arraybufferbytelength +inline size_t array_buffer_byte_length(ArrayBuffer const& array_buffer, ArrayBuffer::Order) +{ + // FIXME: 1. If IsSharedArrayBuffer(arrayBuffer) is true and arrayBuffer has an [[ArrayBufferByteLengthData]] internal slot, then + // FIXME: a. Let bufferByteLengthBlock be arrayBuffer.[[ArrayBufferByteLengthData]]. + // FIXME: b. Let rawLength be GetRawBytesFromSharedBlock(bufferByteLengthBlock, 0, biguint64, true, order). + // FIXME: c. Let isLittleEndian be the value of the [[LittleEndian]] field of the surrounding agent's Agent Record. + // FIXME: d. Return ℝ(RawBytesToNumeric(biguint64, rawLength, isLittleEndian)). + + // 2. Assert: IsDetachedBuffer(arrayBuffer) is false. + VERIFY(!array_buffer.is_detached()); + + // 3. Return arrayBuffer.[[ArrayBufferByteLength]]. + return array_buffer.byte_length(); +} + // 25.1.3.13 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric template static Value raw_bytes_to_numeric(VM& vm, Bytes raw_value, bool is_little_endian)