diff options
author | Linus Groh <mail@linusgroh.de> | 2021-10-09 12:31:13 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-09 12:36:28 +0100 |
commit | 1fba5ca8c370ac1c42627761ddfeb8d3557e656b (patch) | |
tree | d73b95c90604fb8fef15cc12429fb8a650ccfd81 /Userland/Libraries/LibJS | |
parent | f8489da8ee7408d8a4a2dda163fb36867c16ad45 (diff) | |
download | serenity-1fba5ca8c370ac1c42627761ddfeb8d3557e656b.zip |
LibJS: Implement the AllocateArrayBuffer() AO
This should be used instead of ArrayBuffer::create() in most places, as
it uses OrdinaryCreateFromConstructor to allow for a custom prototype.
The data block (ByteBuffer) is allocated separately and attached
afterwards, if we didn't fail due to OOM.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp | 29 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ArrayBuffer.h | 8 |
2 files changed, 32 insertions, 5 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp index b2d04c610f..6b57a80f5a 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp @@ -1,19 +1,20 @@ /* - * Copyright (c) 2020, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibJS/Runtime/AbstractOperations.h> #include <LibJS/Runtime/ArrayBuffer.h> #include <LibJS/Runtime/GlobalObject.h> namespace JS { -ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_size) +ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, size_t byte_length) { - auto buffer = ByteBuffer::create_zeroed(byte_size); + auto buffer = ByteBuffer::create_zeroed(byte_length); if (!buffer.has_value()) { - global_object.vm().throw_exception<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_size); + global_object.vm().throw_exception<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length); return nullptr; } return global_object.heap().allocate<ArrayBuffer>(global_object, buffer.release_value(), *global_object.array_buffer_prototype()); @@ -48,4 +49,24 @@ void ArrayBuffer::visit_edges(Cell::Visitor& visitor) visitor.visit(m_detach_key); } +// 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) +{ + // 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.has_value()) + return global_object.vm().throw_completion<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length); + + // 3. Set obj.[[ArrayBufferData]] to block. + obj->set_buffer(move(*block)); + + // 4. Set obj.[[ArrayBufferByteLength]] to byteLength. + + // 5. Return obj. + return obj; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h index 2242a1644a..504e0b59f4 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -9,6 +9,7 @@ #include <AK/ByteBuffer.h> #include <AK/Function.h> #include <AK/Variant.h> +#include <LibJS/Runtime/Completion.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Object.h> @@ -35,6 +36,9 @@ public: ByteBuffer& buffer() { return buffer_impl(); } const ByteBuffer& buffer() const { return buffer_impl(); } + // Used by allocate_array_buffer() to attach the data block after construction + void set_buffer(ByteBuffer buffer) { m_buffer = move(buffer); } + Value detach_key() const { return m_detach_key; } void set_detach_key(Value detach_key) { m_detach_key = detach_key; } @@ -70,6 +74,8 @@ private: Value m_detach_key; }; +ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject&, FunctionObject& constructor, size_t byte_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) |