diff options
author | Luke <luke.wilde@live.co.uk> | 2021-05-21 21:26:18 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-21 22:52:35 +0100 |
commit | 8004a2dc777debc61d3098282a8ab856ed16de76 (patch) | |
tree | 21d8b85bbb005e4b62de9d3ddbaa14073492c955 | |
parent | 6f1688279a976ea382e229de08cd34f48dfd7e47 (diff) | |
download | serenity-8004a2dc777debc61d3098282a8ab856ed16de76.zip |
LibJS: Expose TypedArray.prototype.buffer
4 files changed, 45 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index cf740626c2..c70217660d 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -57,6 +57,7 @@ namespace JS { P(atan2) \ P(atanh) \ P(bind) \ + P(buffer) \ P(byteLength) \ P(call) \ P(callee) \ diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 66f833ff4a..b7404615c0 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -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); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h index 85987d2ba2..95d7989abf 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_GETTER(length_getter); + JS_DECLARE_NATIVE_GETTER(buffer_getter); JS_DECLARE_NATIVE_FUNCTION(at); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.buffer.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.buffer.js new file mode 100644 index 0000000000..905a549ccd --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.buffer.js @@ -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]); + }); +}); |