1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 22:57:44 +00:00

LibJS: Add all of the DataView.prototype.set* methods

This commit is contained in:
Idan Horowitz 2021-06-14 02:02:53 +03:00 committed by Linus Groh
parent c54b9a6920
commit d7a70eb77c
8 changed files with 203 additions and 0 deletions

View file

@ -40,6 +40,8 @@ public:
};
template<typename type>
Value get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
template<typename type>
Value set_value(size_t byte_index, Value value, bool is_typed_array, Order, bool is_little_endian = true);
private:
virtual void visit_edges(Visitor&) override;
@ -108,4 +110,59 @@ Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_a
return raw_bytes_to_numeric<T>(global_object(), 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)
{
ByteBuffer raw_bytes = ByteBuffer::create_uninitialized(sizeof(T));
auto flip_if_needed = [&]() {
if (is_little_endian)
return;
VERIFY(sizeof(T) % 2 == 0);
for (size_t i = 0; i < sizeof(T) / 2; ++i)
swap(raw_bytes[i], raw_bytes[sizeof(T) - 1 - i]);
};
if constexpr (IsSame<T, float>) {
float raw_value = value.to_double(global_object);
ReadonlyBytes { &raw_value, sizeof(float) }.copy_to(raw_bytes);
flip_if_needed();
return raw_bytes;
}
if constexpr (IsSame<T, double>) {
double raw_value = value.to_double(global_object);
ReadonlyBytes { &raw_value, sizeof(double) }.copy_to(raw_bytes);
flip_if_needed();
return raw_bytes;
}
if constexpr (!IsIntegral<T>)
VERIFY_NOT_REACHED();
if constexpr (sizeof(T) == 8) {
u64 int_value = value.as_bigint().big_integer().to_u64();
ReadonlyBytes { &int_value, sizeof(u64) }.copy_to(raw_bytes);
flip_if_needed();
return raw_bytes;
} else {
T int_value;
if constexpr (IsSigned<T>)
int_value = value.to_i32(global_object);
else
int_value = value.to_u32(global_object);
ReadonlyBytes { &int_value, sizeof(T) }.copy_to(raw_bytes);
flip_if_needed();
return raw_bytes;
}
}
// 25.1.2.12 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-setvalueinbuffer
template<typename T>
Value 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);
// FIXME: Check for shared buffer
raw_bytes.span().copy_to(buffer_impl().span().slice(byte_index));
return js_undefined();
}
}

View file

@ -234,15 +234,25 @@ namespace JS {
P(round) \
P(seal) \
P(set) \
P(setBigInt64) \
P(setBigUint64) \
P(setDate) \
P(setFloat32) \
P(setFloat64) \
P(setFullYear) \
P(setHours) \
P(setInt8) \
P(setInt16) \
P(setInt32) \
P(setMilliseconds) \
P(setMinutes) \
P(setMonth) \
P(setPrototypeOf) \
P(setSeconds) \
P(setTime) \
P(setUint8) \
P(setUint16) \
P(setUint32) \
P(setUTCDate) \
P(setUTCFullYear) \
P(setUTCHours) \

View file

@ -29,6 +29,16 @@ void DataViewPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.getUint8, get_uint_8, 1, attr);
define_native_function(vm.names.getUint16, get_uint_16, 1, attr);
define_native_function(vm.names.getUint32, get_uint_32, 1, attr);
define_native_function(vm.names.setBigInt64, set_big_int_64, 2, attr);
define_native_function(vm.names.setBigUint64, set_big_uint_64, 2, attr);
define_native_function(vm.names.setFloat32, set_float_32, 2, attr);
define_native_function(vm.names.setFloat64, set_float_64, 2, attr);
define_native_function(vm.names.setInt8, set_int_8, 2, attr);
define_native_function(vm.names.setInt16, set_int_16, 2, attr);
define_native_function(vm.names.setInt32, set_int_32, 2, attr);
define_native_function(vm.names.setUint8, set_uint_8, 2, attr);
define_native_function(vm.names.setUint16, set_uint_16, 2, attr);
define_native_function(vm.names.setUint32, set_uint_32, 2, attr);
define_native_accessor(vm.names.buffer, buffer_getter, {}, Attribute::Configurable);
define_native_accessor(vm.names.byteLength, byte_length_getter, {}, Attribute::Configurable);
@ -85,6 +95,48 @@ static Value get_view_value(GlobalObject& global_object, Value request_index, Va
return buffer->get_value<T>(buffer_index, false, ArrayBuffer::Order::Unordered, little_endian);
}
// 25.3.1.2 SetViewValue ( view, requestIndex, isLittleEndian, type, value ), https://tc39.es/ecma262/#sec-setviewvalue
template<typename T>
static Value set_view_value(GlobalObject& global_object, Value request_index, Value is_little_endian, Value value)
{
auto& vm = global_object.vm();
auto* view = typed_this(vm, global_object);
if (!view)
return {};
auto get_index = request_index.to_index(global_object);
if (vm.exception())
return {};
Value number_value;
if constexpr (IsIntegral<T> && sizeof(T) == 8)
number_value = value.to_bigint(global_object);
else
number_value = value.to_number(global_object);
if (vm.exception())
return {};
auto little_endian = is_little_endian.to_boolean();
auto buffer = view->viewed_array_buffer();
if (buffer->is_detached()) {
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
return {};
}
auto view_offset = view->byte_offset();
auto view_size = view->byte_length();
auto element_size = sizeof(T);
if (get_index + element_size > view_size) {
vm.throw_exception<RangeError>(global_object, ErrorType::DataViewOutOfRangeByteOffset, get_index, view_size);
return {};
}
auto buffer_index = get_index + view_offset;
return buffer->set_value<T>(buffer_index, number_value, false, ArrayBuffer::Order::Unordered, little_endian);
}
// 25.3.4.5 DataView.prototype.getBigInt64 ( byteOffset [ , littleEndian ] ), https://tc39.es/ecma262/#sec-dataview.prototype.getbigint64
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::get_big_int_64)
{
@ -145,6 +197,57 @@ JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::get_uint_32)
return get_view_value<u32>(global_object, vm.argument(0), vm.argument(1));
}
// 25.3.4.15 DataView.prototype.setBigInt64 ( byteOffset, value [ , littleEndian ] ), https://tc39.es/ecma262/#sec-dataview.prototype.setbigint64
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_big_int_64)
{
return set_view_value<i64>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_big_uint_64)
{
return set_view_value<u64>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_float_32)
{
return set_view_value<float>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_float_64)
{
return set_view_value<double>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_int_8)
{
return set_view_value<i8>(global_object, vm.argument(0), Value(true), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_int_16)
{
return set_view_value<i16>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_int_32)
{
return set_view_value<i32>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_uint_8)
{
return set_view_value<u8>(global_object, vm.argument(0), Value(true), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_uint_16)
{
return set_view_value<u16>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
JS_DEFINE_NATIVE_FUNCTION(DataViewPrototype::set_uint_32)
{
return set_view_value<u32>(global_object, vm.argument(0), vm.argument(2), vm.argument(1));
}
// 25.3.4.1 get DataView.prototype.buffer, https://tc39.es/ecma262/#sec-get-dataview.prototype.buffer
JS_DEFINE_NATIVE_GETTER(DataViewPrototype::buffer_getter)
{

View file

@ -29,6 +29,16 @@ private:
JS_DECLARE_NATIVE_FUNCTION(get_uint_8);
JS_DECLARE_NATIVE_FUNCTION(get_uint_16);
JS_DECLARE_NATIVE_FUNCTION(get_uint_32);
JS_DECLARE_NATIVE_FUNCTION(set_big_int_64);
JS_DECLARE_NATIVE_FUNCTION(set_big_uint_64);
JS_DECLARE_NATIVE_FUNCTION(set_float_32);
JS_DECLARE_NATIVE_FUNCTION(set_float_64);
JS_DECLARE_NATIVE_FUNCTION(set_int_8);
JS_DECLARE_NATIVE_FUNCTION(set_int_16);
JS_DECLARE_NATIVE_FUNCTION(set_int_32);
JS_DECLARE_NATIVE_FUNCTION(set_uint_8);
JS_DECLARE_NATIVE_FUNCTION(set_uint_16);
JS_DECLARE_NATIVE_FUNCTION(set_uint_32);
JS_DECLARE_NATIVE_GETTER(buffer_getter);
JS_DECLARE_NATIVE_GETTER(byte_length_getter);