diff options
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.@@iterator.js | 89 |
2 files changed, 92 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp index 0a6ba327c4..336b9c5e80 100644 --- a/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/TypedArrayPrototype.cpp @@ -61,6 +61,9 @@ void TypedArrayPrototype::initialize(GlobalObject& object) // 23.2.3.30 %TypedArray%.prototype.toString ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.tostring define_direct_property(vm.names.toString, global_object().array_prototype()->get_without_side_effects(vm.names.toString), attr); + + // 23.2.3.32 %TypedArray%.prototype [ @@iterator ] ( ), https://tc39.es/ecma262/#sec-%typedarray%.prototype-@@iterator + define_direct_property(*vm.well_known_symbol_iterator(), get_without_side_effects(vm.names.values), attr); } TypedArrayPrototype::~TypedArrayPrototype() diff --git a/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.@@iterator.js b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.@@iterator.js new file mode 100644 index 0000000000..049733c574 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/TypedArray/TypedArray.prototype.@@iterator.js @@ -0,0 +1,89 @@ +const TYPED_ARRAYS = [ + Uint8Array, + Uint8ClampedArray, + Uint16Array, + Uint32Array, + Int8Array, + Int16Array, + Int32Array, + Float32Array, + Float64Array, +]; + +const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array]; + +const ALL_TYPED_ARRAYS = TYPED_ARRAYS.concat(BIGINT_TYPED_ARRAYS); + +describe("correct behavior", () => { + test("length is 0", () => { + ALL_TYPED_ARRAYS.forEach(T => { + expect(T.prototype[Symbol.iterator]).toHaveLength(0); + }); + }); + + test("same value as %TypedArray%.prototype.values", () => { + ALL_TYPED_ARRAYS.forEach(T => { + expect(T.prototype[Symbol.iterator]).toBe(T.prototype.values); + }); + }); + + test("basic functionality", () => { + TYPED_ARRAYS.forEach(T => { + const typedArray = new T([1, 2, 3]); + const iterator = typedArray[Symbol.iterator](); + expect(iterator.next()).toEqual({ value: 1, done: false }); + expect(iterator.next()).toEqual({ value: 2, done: false }); + expect(iterator.next()).toEqual({ value: 3, done: false }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + const typedArray = new T([1n, 2n, 3n]); + const iterator = typedArray[Symbol.iterator](); + expect(iterator.next()).toEqual({ value: 1n, done: false }); + expect(iterator.next()).toEqual({ value: 2n, done: false }); + expect(iterator.next()).toEqual({ value: 3n, done: false }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + expect(iterator.next()).toEqual({ value: undefined, done: true }); + }); + }); + + test("can be iterated with for-of", () => { + TYPED_ARRAYS.forEach(T => { + const typedArray = new T([1, 2, 3]); + const result = []; + + for (const value of typedArray) result.push(value); + + expect(result).toHaveLength(3); + expect(result[0]).toBe(1); + expect(result[1]).toBe(2); + expect(result[2]).toBe(3); + }); + + BIGINT_TYPED_ARRAYS.forEach(T => { + const typedArray = new T([1n, 2n, 3n]); + const result = []; + + for (const value of typedArray) result.push(value); + + expect(result).toHaveLength(3); + expect(result[0]).toBe(1n); + expect(result[1]).toBe(2n); + expect(result[2]).toBe(3n); + }); + }); +}); + +describe("errors", () => { + test("this value must be a TypedArray object", () => { + ALL_TYPED_ARRAYS.forEach(T => { + expect(() => { + T.prototype[Symbol.iterator].call({}); + }).toThrowWithMessage(TypeError, "Not an object of type TypedArray"); + }); + }); +}); |