mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-25 19:02:07 -05:00
LibJS: Expose TypedArray.prototype.buffer
This commit is contained in:
parent
6f1688279a
commit
8004a2dc77
4 changed files with 45 additions and 0 deletions
|
@ -57,6 +57,7 @@ namespace JS {
|
|||
P(atan2) \
|
||||
P(atanh) \
|
||||
P(bind) \
|
||||
P(buffer) \
|
||||
P(byteLength) \
|
||||
P(call) \
|
||||
P(callee) \
|
||||
|
|
|
@ -23,6 +23,7 @@ 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);
|
||||
define_native_function(vm.names.at, at, 1, attr);
|
||||
}
|
||||
|
||||
|
@ -73,4 +74,15 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::at)
|
|||
return typed_array->get(index.value());
|
||||
}
|
||||
|
||||
// https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.buffer
|
||||
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::buffer_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);
|
||||
return Value(array_buffer);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ public:
|
|||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_GETTER(length_getter);
|
||||
JS_DECLARE_NATIVE_GETTER(buffer_getter);
|
||||
|
||||
JS_DECLARE_NATIVE_FUNCTION(at);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
// 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();
|
||||
|
||||
const buffer = typedArray.buffer;
|
||||
expect(buffer).toBeInstanceOf(ArrayBuffer);
|
||||
expect(buffer.byteLength).toBe(typedArray.byteLength);
|
||||
expect(buffer.byteLength).toBe(typedArray.length * typedArray.BYTES_PER_ELEMENT);
|
||||
|
||||
const arrayFromBuffer = new T(buffer);
|
||||
expect(arrayFromBuffer.length).toBe(typedArray.length);
|
||||
expect(arrayFromBuffer.byteLength).toBe(typedArray.byteLength);
|
||||
expect(arrayFromBuffer.byteOffset).toBe(typedArray.byteOffset);
|
||||
expect(arrayFromBuffer[0]).toBe(typedArray[0]);
|
||||
expect(arrayFromBuffer[1]).toBe(typedArray[1]);
|
||||
expect(arrayFromBuffer[2]).toBe(typedArray[2]);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue