summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLuke <luke.wilde@live.co.uk>2021-05-21 21:25:09 +0100
committerLinus Groh <mail@linusgroh.de>2021-05-21 22:52:35 +0100
commit6f1688279a976ea382e229de08cd34f48dfd7e47 (patch)
tree5dfc20317f58ee5c49f472f10ad2237675264da0 /Userland
parent7d12f49e7488399846228acc89a62b4466d755c6 (diff)
downloadserenity-6f1688279a976ea382e229de08cd34f48dfd7e47.zip
LibJS: Expose BYTES_PER_ELEMENT on each TypedArray
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/TypedArray.cpp2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.BYTES_PER_ELEMENT.js18
2 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp
index eaf542a9f6..a9bfef012d 100644
--- a/Userland/Libraries/LibJS/Runtime/TypedArray.cpp
+++ b/Userland/Libraries/LibJS/Runtime/TypedArray.cpp
@@ -194,6 +194,8 @@ void TypedArrayBase::visit_edges(Visitor& visitor)
PrototypeName::PrototypeName(GlobalObject& global_object) \
: Object(*global_object.typed_array_prototype()) \
{ \
+ auto& vm = this->vm(); \
+ define_property(vm.names.BYTES_PER_ELEMENT, Value((i32)sizeof(Type)), 0); \
} \
PrototypeName::~PrototypeName() { } \
\
diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.BYTES_PER_ELEMENT.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.BYTES_PER_ELEMENT.js
new file mode 100644
index 0000000000..3d221eebd9
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.BYTES_PER_ELEMENT.js
@@ -0,0 +1,18 @@
+// Update when more typed arrays get added
+const TYPED_ARRAYS = [
+ { array: Uint8Array, expected: 1 },
+ { array: Uint16Array, expected: 2 },
+ { array: Uint32Array, expected: 4 },
+ { array: Int8Array, expected: 1 },
+ { array: Int16Array, expected: 2 },
+ { array: Int32Array, expected: 4 },
+ { array: Float32Array, expected: 4 },
+ { array: Float64Array, expected: 8 },
+];
+
+test("basic functionality", () => {
+ TYPED_ARRAYS.forEach(T => {
+ const typedArray = new T.array();
+ expect(typedArray.BYTES_PER_ELEMENT).toBe(T.expected);
+ });
+});