2020-12-02 20:49:31 +00:00
|
|
|
/*
|
2022-05-02 20:54:39 +02:00
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2020-12-02 20:49:31 +00:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-02 20:49:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2021-07-11 08:57:54 -04:00
|
|
|
#include <AK/Function.h>
|
2021-05-17 21:39:31 +04:30
|
|
|
#include <AK/Variant.h>
|
2023-01-28 11:26:03 -05:00
|
|
|
#include <LibJS/Runtime/BigInt.h>
|
2021-10-09 12:31:13 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-06-14 01:57:15 +03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-12-02 20:49:31 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-06-17 00:43:24 +01:00
|
|
|
struct ClampedU8 {
|
|
|
|
};
|
|
|
|
|
2021-07-11 08:57:54 -04:00
|
|
|
// 25.1.1 Notation (read-modify-write modification function), https://tc39.es/ecma262/#sec-arraybuffer-notation
|
|
|
|
using ReadWriteModifyFunction = Function<ByteBuffer(ByteBuffer, ByteBuffer)>;
|
|
|
|
|
2023-06-30 18:00:56 +01:00
|
|
|
enum class PreserveResizability {
|
|
|
|
FixedLength,
|
|
|
|
PreserveResizability
|
|
|
|
};
|
|
|
|
|
2020-12-02 20:49:31 +00:00
|
|
|
class ArrayBuffer : public Object {
|
|
|
|
JS_OBJECT(ArrayBuffer, Object);
|
|
|
|
|
|
|
|
public:
|
2022-12-13 20:49:50 +00:00
|
|
|
static ThrowCompletionOr<NonnullGCPtr<ArrayBuffer>> create(Realm&, size_t);
|
|
|
|
static NonnullGCPtr<ArrayBuffer> create(Realm&, ByteBuffer);
|
|
|
|
static NonnullGCPtr<ArrayBuffer> create(Realm&, ByteBuffer*);
|
2020-12-02 20:49:31 +00:00
|
|
|
|
2022-03-14 10:25:06 -06:00
|
|
|
virtual ~ArrayBuffer() override = default;
|
2020-12-02 20:49:31 +00:00
|
|
|
|
2023-06-30 19:44:22 +01:00
|
|
|
size_t byte_length() const
|
|
|
|
{
|
|
|
|
if (is_detached())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return buffer_impl().size();
|
|
|
|
}
|
2023-06-28 18:27:43 +12:00
|
|
|
|
|
|
|
// [[ArrayBufferData]]
|
2021-05-17 21:39:31 +04:30
|
|
|
ByteBuffer& buffer() { return buffer_impl(); }
|
2022-04-01 20:58:27 +03:00
|
|
|
ByteBuffer const& buffer() const { return buffer_impl(); }
|
2020-12-02 20:49:31 +00:00
|
|
|
|
2021-10-09 12:31:13 +01:00
|
|
|
// Used by allocate_array_buffer() to attach the data block after construction
|
|
|
|
void set_buffer(ByteBuffer buffer) { m_buffer = move(buffer); }
|
|
|
|
|
2021-06-10 22:44:17 +03:00
|
|
|
Value detach_key() const { return m_detach_key; }
|
2021-06-11 00:59:24 +03:00
|
|
|
void set_detach_key(Value detach_key) { m_detach_key = detach_key; }
|
|
|
|
|
2021-06-10 22:44:17 +03:00
|
|
|
void detach_buffer() { m_buffer = Empty {}; }
|
2023-06-28 17:20:44 +12:00
|
|
|
|
|
|
|
// 25.1.2.2 IsDetachedBuffer ( arrayBuffer ), https://tc39.es/ecma262/#sec-isdetachedbuffer
|
|
|
|
bool is_detached() const
|
|
|
|
{
|
|
|
|
// 1. If arrayBuffer.[[ArrayBufferData]] is null, return true.
|
|
|
|
if (m_buffer.has<Empty>())
|
|
|
|
return true;
|
|
|
|
// 2. Return false.
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-10 22:44:17 +03:00
|
|
|
|
2021-06-14 01:57:15 +03:00
|
|
|
enum Order {
|
|
|
|
SeqCst,
|
|
|
|
Unordered
|
|
|
|
};
|
|
|
|
template<typename type>
|
|
|
|
Value get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
|
2021-06-14 02:02:53 +03:00
|
|
|
template<typename type>
|
2022-05-02 20:54:39 +02:00
|
|
|
void set_value(size_t byte_index, Value value, bool is_typed_array, Order, bool is_little_endian = true);
|
2021-07-11 08:57:54 -04:00
|
|
|
template<typename T>
|
2023-06-28 20:28:32 +12:00
|
|
|
ThrowCompletionOr<Value> get_modify_set_value(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian = true);
|
2021-06-14 01:57:15 +03:00
|
|
|
|
2020-12-02 20:49:31 +00:00
|
|
|
private:
|
2022-08-28 23:51:28 +02:00
|
|
|
ArrayBuffer(ByteBuffer buffer, Object& prototype);
|
|
|
|
ArrayBuffer(ByteBuffer* buffer, Object& prototype);
|
|
|
|
|
2021-06-11 00:59:24 +03:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
2021-05-17 21:39:31 +04:30
|
|
|
ByteBuffer& buffer_impl()
|
|
|
|
{
|
|
|
|
ByteBuffer* ptr { nullptr };
|
2021-06-10 22:44:17 +03:00
|
|
|
m_buffer.visit([&](Empty) { VERIFY_NOT_REACHED(); }, [&](auto* pointer) { ptr = pointer; }, [&](auto& value) { ptr = &value; });
|
2021-05-17 21:39:31 +04:30
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ByteBuffer const& buffer_impl() const { return const_cast<ArrayBuffer*>(this)->buffer_impl(); }
|
2021-05-17 21:39:31 +04:30
|
|
|
|
2021-06-10 22:44:17 +03:00
|
|
|
Variant<Empty, ByteBuffer, ByteBuffer*> m_buffer;
|
|
|
|
// The various detach related members of ArrayBuffer are not used by any ECMA262 functionality,
|
|
|
|
// but are required to be available for the use of various harnesses like the Test262 test runner.
|
|
|
|
Value m_detach_key;
|
2020-12-02 20:49:31 +00:00
|
|
|
};
|
|
|
|
|
2023-06-17 21:03:31 +02:00
|
|
|
void copy_data_block_bytes(ByteBuffer& to_block, u64 to_index, ByteBuffer& from_block, u64 from_index, u64 count);
|
2022-08-21 17:25:31 +01:00
|
|
|
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);
|
2023-06-30 18:00:56 +01:00
|
|
|
ThrowCompletionOr<ArrayBuffer*> array_buffer_copy_and_detach(VM&, ArrayBuffer& array_buffer, Value new_length, PreserveResizability preserve_resizability);
|
2023-07-01 16:07:26 +12:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<ArrayBuffer>> allocate_shared_array_buffer(VM&, FunctionObject& constructor, size_t byte_length);
|
2021-10-09 12:31:13 +01:00
|
|
|
|
2021-06-14 01:57:15 +03:00
|
|
|
// 25.1.2.9 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric
|
|
|
|
template<typename T>
|
2022-08-21 17:25:31 +01:00
|
|
|
static Value raw_bytes_to_numeric(VM& vm, ByteBuffer raw_value, bool is_little_endian)
|
2021-06-14 01:57:15 +03:00
|
|
|
{
|
|
|
|
if (!is_little_endian) {
|
|
|
|
VERIFY(raw_value.size() % 2 == 0);
|
|
|
|
for (size_t i = 0; i < raw_value.size() / 2; ++i)
|
|
|
|
swap(raw_value[i], raw_value[raw_value.size() - 1 - i]);
|
|
|
|
}
|
2021-06-17 00:43:24 +01:00
|
|
|
using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
|
|
|
|
if constexpr (IsSame<UnderlyingBufferDataType, float>) {
|
2021-06-14 01:57:15 +03:00
|
|
|
float value;
|
|
|
|
raw_value.span().copy_to({ &value, sizeof(float) });
|
|
|
|
if (isnan(value))
|
|
|
|
return js_nan();
|
|
|
|
return Value(value);
|
|
|
|
}
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (IsSame<UnderlyingBufferDataType, double>) {
|
2021-06-14 01:57:15 +03:00
|
|
|
double value;
|
|
|
|
raw_value.span().copy_to({ &value, sizeof(double) });
|
|
|
|
if (isnan(value))
|
|
|
|
return js_nan();
|
|
|
|
return Value(value);
|
|
|
|
}
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (!IsIntegral<UnderlyingBufferDataType>)
|
2021-06-14 01:57:15 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-06-17 00:43:24 +01:00
|
|
|
UnderlyingBufferDataType int_value = 0;
|
|
|
|
raw_value.span().copy_to({ &int_value, sizeof(UnderlyingBufferDataType) });
|
|
|
|
if constexpr (sizeof(UnderlyingBufferDataType) == 8) {
|
2022-08-26 00:49:50 +02:00
|
|
|
if constexpr (IsSigned<UnderlyingBufferDataType>) {
|
|
|
|
static_assert(IsSame<UnderlyingBufferDataType, i64>);
|
2022-12-06 22:03:52 +00:00
|
|
|
return BigInt::create(vm, Crypto::SignedBigInteger { int_value });
|
2022-08-26 00:49:50 +02:00
|
|
|
} else {
|
|
|
|
static_assert(IsOneOf<UnderlyingBufferDataType, u64, double>);
|
2022-12-06 22:03:52 +00:00
|
|
|
return BigInt::create(vm, Crypto::SignedBigInteger { Crypto::UnsignedBigInteger { int_value } });
|
2022-08-26 00:49:50 +02:00
|
|
|
}
|
2021-06-14 01:57:15 +03:00
|
|
|
} else {
|
|
|
|
return Value(int_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
// Implementation for 25.1.2.10 GetValueFromBuffer, used in TypedArray<T>::get_value_from_buffer().
|
2021-06-14 01:57:15 +03:00
|
|
|
template<typename T>
|
|
|
|
Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
|
|
|
|
{
|
2022-08-21 17:25:31 +01:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2021-06-14 01:57:15 +03:00
|
|
|
auto element_size = sizeof(T);
|
|
|
|
|
|
|
|
// FIXME: Check for shared buffer
|
|
|
|
|
2022-06-13 05:20:42 -07:00
|
|
|
// FIXME: Propagate errors.
|
|
|
|
auto raw_value = MUST(buffer_impl().slice(byte_index, element_size));
|
2022-08-21 17:25:31 +01:00
|
|
|
return raw_bytes_to_numeric<T>(vm, move(raw_value), is_little_endian);
|
2021-06-14 01:57:15 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 02:02:53 +03:00
|
|
|
// 25.1.2.11 NumericToRawBytes ( type, value, isLittleEndian ), https://tc39.es/ecma262/#sec-numerictorawbytes
|
|
|
|
template<typename T>
|
2023-06-28 19:44:00 +12:00
|
|
|
static ThrowCompletionOr<ByteBuffer> numeric_to_raw_bytes(VM& vm, Value value, bool is_little_endian)
|
2021-06-14 02:02:53 +03:00
|
|
|
{
|
2021-10-16 21:56:14 +03:00
|
|
|
VERIFY(value.is_number() || value.is_bigint());
|
2021-06-17 00:43:24 +01:00
|
|
|
using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
|
2023-06-28 19:44:00 +12:00
|
|
|
ByteBuffer raw_bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(sizeof(UnderlyingBufferDataType)));
|
2021-06-14 02:02:53 +03:00
|
|
|
auto flip_if_needed = [&]() {
|
|
|
|
if (is_little_endian)
|
|
|
|
return;
|
2021-06-17 00:43:24 +01:00
|
|
|
VERIFY(sizeof(UnderlyingBufferDataType) % 2 == 0);
|
|
|
|
for (size_t i = 0; i < sizeof(UnderlyingBufferDataType) / 2; ++i)
|
|
|
|
swap(raw_bytes[i], raw_bytes[sizeof(UnderlyingBufferDataType) - 1 - i]);
|
2021-06-14 02:02:53 +03:00
|
|
|
};
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (IsSame<UnderlyingBufferDataType, float>) {
|
2022-08-21 14:00:56 +01:00
|
|
|
float raw_value = MUST(value.to_double(vm));
|
2021-06-14 02:02:53 +03:00
|
|
|
ReadonlyBytes { &raw_value, sizeof(float) }.copy_to(raw_bytes);
|
|
|
|
flip_if_needed();
|
|
|
|
return raw_bytes;
|
|
|
|
}
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (IsSame<UnderlyingBufferDataType, double>) {
|
2022-08-21 14:00:56 +01:00
|
|
|
double raw_value = MUST(value.to_double(vm));
|
2021-06-14 02:02:53 +03:00
|
|
|
ReadonlyBytes { &raw_value, sizeof(double) }.copy_to(raw_bytes);
|
|
|
|
flip_if_needed();
|
|
|
|
return raw_bytes;
|
|
|
|
}
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (!IsIntegral<UnderlyingBufferDataType>)
|
2021-06-14 02:02:53 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2021-06-17 00:43:24 +01:00
|
|
|
if constexpr (sizeof(UnderlyingBufferDataType) == 8) {
|
|
|
|
UnderlyingBufferDataType int_value;
|
|
|
|
|
|
|
|
if constexpr (IsSigned<UnderlyingBufferDataType>)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_bigint_int64(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_bigint_uint64(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
|
|
|
|
ReadonlyBytes { &int_value, sizeof(UnderlyingBufferDataType) }.copy_to(raw_bytes);
|
2021-06-14 02:02:53 +03:00
|
|
|
flip_if_needed();
|
|
|
|
return raw_bytes;
|
|
|
|
} else {
|
2021-06-17 00:43:24 +01:00
|
|
|
UnderlyingBufferDataType int_value;
|
|
|
|
if constexpr (IsSigned<UnderlyingBufferDataType>) {
|
|
|
|
if constexpr (sizeof(UnderlyingBufferDataType) == 4)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_i32(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_i16(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_i8(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
} else {
|
|
|
|
if constexpr (sizeof(UnderlyingBufferDataType) == 4)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_u32(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_u16(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else if constexpr (!IsSame<T, ClampedU8>)
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_u8(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
else
|
2022-08-21 14:00:56 +01:00
|
|
|
int_value = MUST(value.to_u8_clamp(vm));
|
2021-06-17 00:43:24 +01:00
|
|
|
}
|
|
|
|
ReadonlyBytes { &int_value, sizeof(UnderlyingBufferDataType) }.copy_to(raw_bytes);
|
|
|
|
if constexpr (sizeof(UnderlyingBufferDataType) % 2 == 0)
|
|
|
|
flip_if_needed();
|
2021-06-14 02:02:53 +03:00
|
|
|
return raw_bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 25.1.2.12 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-setvalueinbuffer
|
|
|
|
template<typename T>
|
2022-05-02 20:54:39 +02:00
|
|
|
void ArrayBuffer::set_value(size_t byte_index, Value value, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
|
2021-06-14 02:02:53 +03:00
|
|
|
{
|
2022-08-21 17:25:31 +01:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2023-06-28 18:28:39 +12:00
|
|
|
// 1. Assert: IsDetachedBuffer(arrayBuffer) is false.
|
|
|
|
VERIFY(!is_detached());
|
|
|
|
|
|
|
|
// 2. Assert: There are sufficient bytes in arrayBuffer starting at byteIndex to represent a value of type.
|
|
|
|
VERIFY(buffer().bytes().slice(byte_index).size() >= sizeof(T));
|
|
|
|
|
|
|
|
// 3. Assert: value is a BigInt if IsBigIntElementType(type) is true; otherwise, value is a Number.
|
|
|
|
if constexpr (IsIntegral<T> && sizeof(T) == 8)
|
|
|
|
VERIFY(value.is_bigint());
|
|
|
|
else
|
|
|
|
VERIFY(value.is_number());
|
|
|
|
|
|
|
|
// 4. Let block be arrayBuffer.[[ArrayBufferData]].
|
|
|
|
auto& block = buffer();
|
|
|
|
|
|
|
|
// FIXME: 5. Let elementSize be the Element Size value specified in Table 70 for Element Type type.
|
|
|
|
|
|
|
|
// 6. If isLittleEndian is not present, set isLittleEndian to the value of the [[LittleEndian]] field of the surrounding agent's Agent Record.
|
|
|
|
// NOTE: Done by default parameter at declaration of this function.
|
|
|
|
|
|
|
|
// 7. Let rawBytes be NumericToRawBytes(type, value, isLittleEndian).
|
2023-06-28 19:44:00 +12:00
|
|
|
auto raw_bytes = numeric_to_raw_bytes<T>(vm, value, is_little_endian).release_allocated_value_but_fixme_should_propagate_errors();
|
2021-06-14 02:02:53 +03:00
|
|
|
|
2023-06-28 18:28:39 +12:00
|
|
|
// FIXME 8. If IsSharedArrayBuffer(arrayBuffer) is true, then
|
|
|
|
if (false) {
|
|
|
|
// FIXME: a. Let execution be the [[CandidateExecution]] field of the surrounding agent's Agent Record.
|
|
|
|
// FIXME: b. Let eventsRecord be the Agent Events Record of execution.[[EventsRecords]] whose [[AgentSignifier]] is AgentSignifier().
|
|
|
|
// FIXME: c. If isTypedArray is true and IsNoTearConfiguration(type, order) is true, let noTear be true; otherwise let noTear be false.
|
|
|
|
// FIXME: d. Append WriteSharedMemory { [[Order]]: order, [[NoTear]]: noTear, [[Block]]: block, [[ByteIndex]]: byteIndex, [[ElementSize]]: elementSize, [[Payload]]: rawBytes } to eventsRecord.[[EventList]].
|
|
|
|
}
|
|
|
|
// 9. Else,
|
|
|
|
else {
|
|
|
|
// a. Store the individual bytes of rawBytes into block, starting at block[byteIndex].
|
|
|
|
raw_bytes.span().copy_to(block.span().slice(byte_index));
|
|
|
|
}
|
2021-06-14 02:02:53 +03:00
|
|
|
|
2023-06-28 18:28:39 +12:00
|
|
|
// 10. Return unused.
|
2021-06-14 02:02:53 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 08:57:54 -04:00
|
|
|
// 25.1.2.13 GetModifySetValueInBuffer ( arrayBuffer, byteIndex, type, value, op [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getmodifysetvalueinbuffer
|
|
|
|
template<typename T>
|
2023-06-28 20:28:32 +12:00
|
|
|
ThrowCompletionOr<Value> ArrayBuffer::get_modify_set_value(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian)
|
2021-07-11 08:57:54 -04:00
|
|
|
{
|
2022-08-21 17:25:31 +01:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2023-06-28 20:28:32 +12:00
|
|
|
auto raw_bytes = MUST_OR_THROW_OOM(numeric_to_raw_bytes<T>(vm, value, is_little_endian));
|
2021-07-11 08:57:54 -04:00
|
|
|
|
|
|
|
// FIXME: Check for shared buffer
|
|
|
|
|
2023-06-28 20:28:32 +12:00
|
|
|
auto raw_bytes_read = TRY_OR_THROW_OOM(vm, buffer_impl().slice(byte_index, sizeof(T)));
|
2021-07-11 08:57:54 -04:00
|
|
|
auto raw_bytes_modified = operation(raw_bytes_read, raw_bytes);
|
|
|
|
raw_bytes_modified.span().copy_to(buffer_impl().span().slice(byte_index));
|
|
|
|
|
2022-08-21 17:25:31 +01:00
|
|
|
return raw_bytes_to_numeric<T>(vm, raw_bytes_read, is_little_endian);
|
2021-07-11 08:57:54 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 20:49:31 +00:00
|
|
|
}
|