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

LibJS: Expose TypedArray.prototype.buffer

This commit is contained in:
Luke 2021-05-21 21:26:18 +01:00 committed by Linus Groh
parent 6f1688279a
commit 8004a2dc77
4 changed files with 45 additions and 0 deletions

View file

@ -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]);
});
});