1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

LibJS: Use AllocateArrayBuffer where the spec tells us to

This commit is contained in:
Linus Groh 2021-10-09 12:32:26 +01:00
parent 1fba5ca8c3
commit 5b61b60bbd
3 changed files with 7 additions and 16 deletions

View file

@ -48,7 +48,7 @@ Value ArrayBufferConstructor::call()
}
// 25.1.3.1 ArrayBuffer ( length ), https://tc39.es/ecma262/#sec-arraybuffer-length
Value ArrayBufferConstructor::construct(FunctionObject&)
Value ArrayBufferConstructor::construct(FunctionObject& new_target)
{
auto& vm = this->vm();
auto byte_length = vm.argument(0).to_index(global_object());
@ -60,11 +60,7 @@ Value ArrayBufferConstructor::construct(FunctionObject&)
}
return {};
}
auto array_buffer = ArrayBuffer::create(global_object(), byte_length);
if (!array_buffer)
return {};
return array_buffer;
return TRY_OR_DISCARD(allocate_array_buffer(global_object(), new_target, byte_length));
}
// 25.1.4.1 ArrayBuffer.isView ( arg ), https://tc39.es/ecma262/#sec-arraybuffer.isview

View file

@ -8,6 +8,7 @@
#include <AK/Checked.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/ArrayBufferConstructor.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
@ -186,9 +187,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_typed_array(GlobalObj
// 15. Else,
// a. Let data be ? AllocateArrayBuffer(bufferConstructor, byteLength).
auto data = ArrayBuffer::create(global_object, byte_length.value());
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto* data = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), byte_length.value()));
// b. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
if (src_data->is_detached())
@ -266,9 +265,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_array_like(GlobalObje
auto byte_length = element_size * length;
// 5. Let data be ? AllocateArrayBuffer(%ArrayBuffer%, byteLength).
auto* data = ArrayBuffer::create(global_object, byte_length);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto* data = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), byte_length));
// 6. Set O.[[ViewedArrayBuffer]] to data.
typed_array.set_viewed_array_buffer(data);
@ -319,9 +316,7 @@ static ThrowCompletionOr<void> initialize_typed_array_from_list(GlobalObject& gl
if (Checked<size_t>::multiplication_would_overflow(element_size, length))
return vm.template throw_completion<RangeError>(global_object, ErrorType::InvalidLength, "typed array");
auto byte_length = element_size * length;
auto array_buffer = ArrayBuffer::create(global_object, byte_length);
if (auto* exception = vm.exception())
return throw_completion(exception->value());
auto array_buffer = TRY(allocate_array_buffer(global_object, *global_object.array_buffer_constructor(), byte_length));
typed_array.set_viewed_array_buffer(array_buffer);
typed_array.set_byte_length(byte_length);

View file

@ -55,7 +55,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::buffer_getter)
if (!memory)
return JS::js_undefined();
auto array_buffer = JS::ArrayBuffer::create(global_object, &memory->data());
auto array_buffer = TRY_OR_DISCARD(JS::ArrayBuffer::create(global_object, &memory->data()));
array_buffer->set_detach_key(JS::js_string(vm, "WebAssembly.Memory"));
return array_buffer;
}