LibJS: Replace GlobalObject with VM in ArrayBuffer AOs [Part 11/19]

This commit is contained in:
Linus Groh 2022-08-21 17:25:31 +01:00
parent b037894287
commit 26366d5930
Notes: sideshowbarker 2024-07-17 08:00:15 +09:00
8 changed files with 37 additions and 32 deletions

View file

@ -84,7 +84,7 @@ TESTJS_GLOBAL_FUNCTION(detach_array_buffer, detachArrayBuffer)
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "ArrayBuffer");
auto& array_buffer_object = static_cast<JS::ArrayBuffer&>(array_buffer.as_object());
TRY(JS::detach_array_buffer(global_object, array_buffer_object, vm.argument(1)));
TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
return JS::js_null();
}

View file

@ -74,7 +74,7 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)
return vm.throw_completion<TypeError>();
auto& array_buffer_object = static_cast<ArrayBuffer&>(array_buffer.as_object());
TRY(JS::detach_array_buffer(global_object, array_buffer_object, vm.argument(1)));
TRY(JS::detach_array_buffer(vm, array_buffer_object, vm.argument(1)));
return js_null();
}

View file

@ -51,15 +51,18 @@ void ArrayBuffer::visit_edges(Cell::Visitor& visitor)
}
// 25.1.2.1 AllocateArrayBuffer ( constructor, byteLength ), https://tc39.es/ecma262/#sec-allocatearraybuffer
ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject& global_object, FunctionObject& constructor, size_t byte_length)
ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(VM& vm, FunctionObject& constructor, size_t byte_length)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBuffer.prototype%", « [[ArrayBufferData]], [[ArrayBufferByteLength]], [[ArrayBufferDetachKey]] »).
auto* obj = TRY(ordinary_create_from_constructor<ArrayBuffer>(global_object, constructor, &GlobalObject::array_buffer_prototype, nullptr));
// 2. Let block be ? CreateByteDataBlock(byteLength).
auto block = ByteBuffer::create_zeroed(byte_length);
if (block.is_error())
return global_object.vm().throw_completion<RangeError>(ErrorType::NotEnoughMemoryToAllocate, byte_length);
return vm.throw_completion<RangeError>(ErrorType::NotEnoughMemoryToAllocate, byte_length);
// 3. Set obj.[[ArrayBufferData]] to block.
obj->set_buffer(block.release_value());
@ -71,10 +74,8 @@ ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject& global_objec
}
// 25.1.2.3 DetachArrayBuffer ( arrayBuffer [ , key ] ), https://tc39.es/ecma262/#sec-detacharraybuffer
ThrowCompletionOr<void> detach_array_buffer(GlobalObject& global_object, ArrayBuffer& array_buffer, Optional<Value> key)
ThrowCompletionOr<void> detach_array_buffer(VM& vm, ArrayBuffer& array_buffer, Optional<Value> key)
{
auto& vm = global_object.vm();
// 1. Assert: IsSharedArrayBuffer(arrayBuffer) is false.
// FIXME: Check for shared buffer
@ -95,13 +96,15 @@ ThrowCompletionOr<void> detach_array_buffer(GlobalObject& global_object, ArrayBu
}
// 25.1.2.4 CloneArrayBuffer ( srcBuffer, srcByteOffset, srcLength, cloneConstructor ), https://tc39.es/ecma262/#sec-clonearraybuffer
ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(GlobalObject& global_object, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length)
ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(VM& vm, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length)
{
auto& realm = *vm.current_realm();
// 1. Assert: IsDetachedBuffer(srcBuffer) is false.
VERIFY(!source_buffer.is_detached());
// 2. Let targetBuffer be ? AllocateArrayBuffer(%ArrayBuffer%, srcLength).
auto* target_buffer = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), source_length));
auto* target_buffer = TRY(allocate_array_buffer(vm, *realm.global_object().array_buffer_constructor(), source_length));
// 3. Let srcBlock be srcBuffer.[[ArrayBufferData]].
auto& source_block = source_buffer.buffer();

View file

@ -75,13 +75,13 @@ private:
Value m_detach_key;
};
ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject&, FunctionObject& constructor, size_t byte_length);
ThrowCompletionOr<void> detach_array_buffer(GlobalObject&, ArrayBuffer& array_buffer, Optional<Value> key = {});
ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(GlobalObject&, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length);
ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(VM&, FunctionObject& constructor, size_t byte_length);
ThrowCompletionOr<void> detach_array_buffer(VM&, ArrayBuffer& array_buffer, Optional<Value> key = {});
ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(VM&, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length);
// 25.1.2.9 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric
template<typename T>
static Value raw_bytes_to_numeric(GlobalObject& global_object, ByteBuffer raw_value, bool is_little_endian)
static Value raw_bytes_to_numeric(VM& vm, ByteBuffer raw_value, bool is_little_endian)
{
if (!is_little_endian) {
VERIFY(raw_value.size() % 2 == 0);
@ -109,9 +109,9 @@ static Value raw_bytes_to_numeric(GlobalObject& global_object, ByteBuffer raw_va
raw_value.span().copy_to({ &int_value, sizeof(UnderlyingBufferDataType) });
if constexpr (sizeof(UnderlyingBufferDataType) == 8) {
if constexpr (IsSigned<UnderlyingBufferDataType>)
return js_bigint(global_object.heap(), Crypto::SignedBigInteger::create_from(int_value));
return js_bigint(vm, Crypto::SignedBigInteger::create_from(int_value));
else
return js_bigint(global_object.heap(), Crypto::SignedBigInteger { Crypto::UnsignedBigInteger::create_from(int_value) });
return js_bigint(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger::create_from(int_value) });
} else {
return Value(int_value);
}
@ -121,21 +121,22 @@ static Value raw_bytes_to_numeric(GlobalObject& global_object, ByteBuffer raw_va
template<typename T>
Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
{
auto& vm = this->vm();
auto element_size = sizeof(T);
// FIXME: Check for shared buffer
// FIXME: Propagate errors.
auto raw_value = MUST(buffer_impl().slice(byte_index, element_size));
return raw_bytes_to_numeric<T>(global_object(), move(raw_value), is_little_endian);
return raw_bytes_to_numeric<T>(vm, move(raw_value), is_little_endian);
}
// 25.1.2.11 NumericToRawBytes ( type, value, isLittleEndian ), https://tc39.es/ecma262/#sec-numerictorawbytes
template<typename T>
static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value, bool is_little_endian)
static ByteBuffer numeric_to_raw_bytes(VM& vm, Value value, bool is_little_endian)
{
VERIFY(value.is_number() || value.is_bigint());
auto& vm = global_object.vm();
using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
ByteBuffer raw_bytes = ByteBuffer::create_uninitialized(sizeof(UnderlyingBufferDataType)).release_value_but_fixme_should_propagate_errors(); // FIXME: Handle possible OOM situation.
auto flip_if_needed = [&]() {
@ -200,7 +201,9 @@ static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value,
template<typename T>
void ArrayBuffer::set_value(size_t byte_index, Value value, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
{
auto raw_bytes = numeric_to_raw_bytes<T>(global_object(), value, is_little_endian);
auto& vm = this->vm();
auto raw_bytes = numeric_to_raw_bytes<T>(vm, value, is_little_endian);
// FIXME: Check for shared buffer
@ -211,7 +214,9 @@ void ArrayBuffer::set_value(size_t byte_index, Value value, [[maybe_unused]] boo
template<typename T>
Value ArrayBuffer::get_modify_set_value(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian)
{
auto raw_bytes = numeric_to_raw_bytes<T>(global_object(), value, is_little_endian);
auto& vm = this->vm();
auto raw_bytes = numeric_to_raw_bytes<T>(vm, value, is_little_endian);
// FIXME: Check for shared buffer
@ -220,7 +225,7 @@ Value ArrayBuffer::get_modify_set_value(size_t byte_index, Value value, ReadWrit
auto raw_bytes_modified = operation(raw_bytes_read, raw_bytes);
raw_bytes_modified.span().copy_to(buffer_impl().span().slice(byte_index));
return raw_bytes_to_numeric<T>(global_object(), raw_bytes_read, is_little_endian);
return raw_bytes_to_numeric<T>(vm, raw_bytes_read, is_little_endian);
}
}

View file

@ -56,7 +56,7 @@ ThrowCompletionOr<Object*> ArrayBufferConstructor::construct(FunctionObject& new
}
return error;
}
return TRY(allocate_array_buffer(global_object(), new_target, byte_length_or_error.release_value()));
return TRY(allocate_array_buffer(vm, new_target, byte_length_or_error.release_value()));
}
// 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview

View file

@ -235,10 +235,10 @@ static ThrowCompletionOr<Value> atomic_compare_exchange_impl(GlobalObject& globa
constexpr bool is_little_endian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
// 11. Let expectedBytes be NumericToRawBytes(elementType, expected, isLittleEndian).
auto expected_bytes = numeric_to_raw_bytes<T>(global_object, expected, is_little_endian);
auto expected_bytes = numeric_to_raw_bytes<T>(vm, expected, is_little_endian);
// 12. Let replacementBytes be NumericToRawBytes(elementType, replacement, isLittleEndian).
auto replacement_bytes = numeric_to_raw_bytes<T>(global_object, replacement, is_little_endian);
auto replacement_bytes = numeric_to_raw_bytes<T>(vm, replacement, is_little_endian);
// FIXME: Implement SharedArrayBuffer case.
// 13. If IsSharedArrayBuffer(buffer) is true, then
@ -263,7 +263,7 @@ static ThrowCompletionOr<Value> atomic_compare_exchange_impl(GlobalObject& globa
}
// 15. Return RawBytesToNumeric(elementType, rawBytesRead, isLittleEndian).
return raw_bytes_to_numeric<T>(global_object, raw_bytes_read, is_little_endian);
return raw_bytes_to_numeric<T>(vm, raw_bytes_read, is_little_endian);
}
// 25.4.5 Atomics.compareExchange ( typedArray, index, expectedValue, replacementValue ), https://tc39.es/ecma262/#sec-atomics.compareexchange

View file

@ -164,12 +164,12 @@ static ThrowCompletionOr<void> initialize_typed_array_from_typed_array(VM& vm, T
// 10. If elementType is the same as srcType, then
if (dest_array.element_name() == src_array.element_name()) {
// a. Let data be ? CloneArrayBuffer(srcData, srcByteOffset, byteLength).
data = TRY(clone_array_buffer(global_object, *src_data, src_byte_offset, byte_length.value()));
data = TRY(clone_array_buffer(vm, *src_data, src_byte_offset, byte_length.value()));
}
// 11. Else,
else {
// a. Let data be ? AllocateArrayBuffer(bufferConstructor, byteLength).
data = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), byte_length.value()));
data = TRY(allocate_array_buffer(vm, *global_object.array_buffer_constructor(), byte_length.value()));
// b. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
if (src_data->is_detached())
@ -242,7 +242,7 @@ static ThrowCompletionOr<void> allocate_typed_array_buffer(VM& vm, TypedArray<T>
auto byte_length = element_size * length;
// 4. Let data be ? AllocateArrayBuffer(%ArrayBuffer%, byteLength).
auto* data = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), byte_length));
auto* data = TRY(allocate_array_buffer(vm, *global_object.array_buffer_constructor(), byte_length));
// 5. Set O.[[ViewedArrayBuffer]] to data.
typed_array.set_viewed_array_buffer(data);

View file

@ -897,9 +897,6 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::reverse)
// 23.2.3.26.1 SetTypedArrayFromTypedArray ( target, targetOffset, source ), https://tc39.es/ecma262/#sec-settypedarrayfromtypedarray
static ThrowCompletionOr<void> set_typed_array_from_typed_array(VM& vm, TypedArrayBase& target, double target_offset, TypedArrayBase& source)
{
auto& realm = *vm.current_realm();
auto& global_object = realm.global_object();
// 1. Let targetBuffer be target.[[ViewedArrayBuffer]].
auto* target_buffer = target.viewed_array_buffer();
@ -962,7 +959,7 @@ static ThrowCompletionOr<void> set_typed_array_from_typed_array(VM& vm, TypedArr
auto source_byte_length = source.byte_length();
// b. Set srcBuffer to ? CloneArrayBuffer(srcBuffer, srcByteOffset, srcByteLength).
source_buffer = TRY(clone_array_buffer(global_object, *source_buffer, source_byte_offset, source_byte_length));
source_buffer = TRY(clone_array_buffer(vm, *source_buffer, source_byte_offset, source_byte_length));
// c. Let srcByteIndex be 0.
source_byte_index = 0;