LibJS: Expose TypedArray.prototype.byteOffset
This commit is contained in:
parent
58afd71ad2
commit
4d34802f74
Notes:
sideshowbarker
2024-07-18 17:35:19 +09:00
Author: https://github.com/Lubrsi Commit: https://github.com/SerenityOS/serenity/commit/4d34802f74c Pull-request: https://github.com/SerenityOS/serenity/pull/7360 Reviewed-by: https://github.com/linusg
4 changed files with 43 additions and 4 deletions
|
@ -59,6 +59,7 @@ namespace JS {
|
|||
P(bind) \
|
||||
P(buffer) \
|
||||
P(byteLength) \
|
||||
P(byteOffset) \
|
||||
P(call) \
|
||||
P(callee) \
|
||||
P(cbrt) \
|
||||
|
|
|
@ -24,11 +24,8 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
|
|||
// FIXME: This should be an accessor property
|
||||
define_native_property(vm.names.length, length_getter, nullptr, Attribute::Configurable);
|
||||
define_native_property(vm.names.buffer, buffer_getter, nullptr, Attribute::Configurable);
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
define_native_property(vm.names.byteLength, byte_length_getter, nullptr, Attribute::Configurable);
|
||||
define_native_property(vm.names.BYTES_PER_ELEMENT, bytes_per_element_getter, nullptr, 0); // FIXME: This should just be a normal property and not a getter.
|
||||
>>>>>>> 6be44bc62... LibJS: Expose TypedArray.prototype.byteLength
|
||||
define_native_property(vm.names.byteOffset, byte_offset_getter, nullptr, Attribute::Configurable);
|
||||
define_native_function(vm.names.at, at, 1, attr);
|
||||
}
|
||||
|
||||
|
@ -102,4 +99,16 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::byte_length_getter)
|
|||
return Value(typed_array->byte_length());
|
||||
}
|
||||
|
||||
// https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.byteoffset
|
||||
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::byte_offset_getter)
|
||||
{
|
||||
auto typed_array = typed_array_from(vm, global_object);
|
||||
if (!typed_array)
|
||||
return {};
|
||||
auto* array_buffer = typed_array->viewed_array_buffer();
|
||||
VERIFY(array_buffer);
|
||||
// FIXME: If array_buffer is detached, return 0.
|
||||
return Value(typed_array->byte_offset());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ private:
|
|||
JS_DECLARE_NATIVE_GETTER(length_getter);
|
||||
JS_DECLARE_NATIVE_GETTER(buffer_getter);
|
||||
JS_DECLARE_NATIVE_GETTER(byte_length_getter);
|
||||
JS_DECLARE_NATIVE_GETTER(byte_offset_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(at);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
// Update when more typed arrays get added
|
||||
const TYPED_ARRAYS = [
|
||||
Uint8Array,
|
||||
Uint16Array,
|
||||
Uint32Array,
|
||||
Int8Array,
|
||||
Int16Array,
|
||||
Int32Array,
|
||||
Float32Array,
|
||||
Float64Array,
|
||||
];
|
||||
|
||||
test("basic functionality", () => {
|
||||
TYPED_ARRAYS.forEach(T => {
|
||||
const typedArray = new T([1, 2, 3]);
|
||||
expect(Object.hasOwn(typedArray, "byteOffset")).toBeFalse();
|
||||
expect(typedArray.byteOffset).toBe(0);
|
||||
expect(typedArray.length).toBe(3);
|
||||
|
||||
const buffer = typedArray.buffer;
|
||||
|
||||
const arrayFromOffset = new T(buffer, T.BYTES_PER_ELEMENT);
|
||||
expect(arrayFromOffset.byteOffset).toBe(T.BYTES_PER_ELEMENT);
|
||||
expect(arrayFromOffset.length).toBe(2);
|
||||
expect(arrayFromOffset[0]).toBe(2);
|
||||
expect(arrayFromOffset[1]).toBe(3);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue