diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /js/src/tests/non262/Iterator/from | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/non262/Iterator/from')
20 files changed, 445 insertions, 0 deletions
diff --git a/js/src/tests/non262/Iterator/from/Iterator.from-descriptor.js b/js/src/tests/non262/Iterator/from/Iterator.from-descriptor.js new file mode 100644 index 0000000000..19bdde852b --- /dev/null +++ b/js/src/tests/non262/Iterator/from/Iterator.from-descriptor.js @@ -0,0 +1,12 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Descriptor property of Iterator.from +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(Iterator, 'from'); +assertEq(propDesc.writable, true); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/Iterator.from-length.js b/js/src/tests/non262/Iterator/from/Iterator.from-length.js new file mode 100644 index 0000000000..0833538da1 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/Iterator.from-length.js @@ -0,0 +1,17 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + The `length` property of Iterator.from. +info: | + ES7 section 17: Unless otherwise specified, the length property of a built-in + Function object has the attributes { [[Writable]]: false, [[Enumerable]]: + false, [[Configurable]]: true }. +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(Iterator.from, 'length'); +assertEq(propDesc.value, 1); +assertEq(propDesc.writable, false); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/Iterator.from-name.js b/js/src/tests/non262/Iterator/from/Iterator.from-name.js new file mode 100644 index 0000000000..6f48d3e891 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/Iterator.from-name.js @@ -0,0 +1,13 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + `name` property of Iterator.from. +---*/ + +const propDesc = Reflect.getOwnPropertyDescriptor(Iterator.from, 'name'); +assertEq(propDesc.value, 'from'); +assertEq(propDesc.writable, false); +assertEq(propDesc.enumerable, false); +assertEq(propDesc.configurable, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/call-from-with-different-this.js b/js/src/tests/non262/Iterator/from/call-from-with-different-this.js new file mode 100644 index 0000000000..7016f7759b --- /dev/null +++ b/js/src/tests/non262/Iterator/from/call-from-with-different-this.js @@ -0,0 +1,19 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const iter = { + next() { + assertEq(arguments.length, 0); + return {done: false, value: 0}; + }, +}; +const wrap = Iterator.from.call(undefined, iter); + +const result = wrap.next("next argument is ignored"); +assertEq(result.done, false); +assertEq(result.value, 0); + +const returnResult = wrap.return("return argument is ignored"); +assertEq(returnResult.done, true); +assertEq(returnResult.value, undefined); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/iterator-not-callable-throws.js b/js/src/tests/non262/Iterator/from/iterator-not-callable-throws.js new file mode 100644 index 0000000000..a7c98f9e2a --- /dev/null +++ b/js/src/tests/non262/Iterator/from/iterator-not-callable-throws.js @@ -0,0 +1,13 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Iterator.from throws when called with an object with a non-callable @@iterator property. +---*/ + +assertThrowsInstanceOf(() => Iterator.from({ [Symbol.iterator]: 0 }), TypeError); +assertThrowsInstanceOf(() => Iterator.from({ [Symbol.iterator]: false }), TypeError); +assertThrowsInstanceOf(() => Iterator.from({ [Symbol.iterator]: "" }), TypeError); +assertThrowsInstanceOf(() => Iterator.from({ [Symbol.iterator]: {} }), TypeError); +assertThrowsInstanceOf(() => Iterator.from({ [Symbol.iterator]: Symbol('') }), TypeError); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/modify-next.js b/js/src/tests/non262/Iterator/from/modify-next.js new file mode 100644 index 0000000000..d347abd2cd --- /dev/null +++ b/js/src/tests/non262/Iterator/from/modify-next.js @@ -0,0 +1,15 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const iter = { + next: () => ({ done: false, value: 0 }), +}; + +const wrap = Iterator.from(iter); + +iter.next = () => ({ done: true, value: undefined }); + +let {done, value} = wrap.next(); +assertEq(done, false); +assertEq(value, 0); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/modify-return.js b/js/src/tests/non262/Iterator/from/modify-return.js new file mode 100644 index 0000000000..dfda4dbe89 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/modify-return.js @@ -0,0 +1,22 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const iter = { + next: () => ({ done: false, value: 0 }), + return: (value = "old return") => ({ done: true, value }), +}; + +const wrap = Iterator.from(iter); + +let {done, value} = wrap.return("return argument ignored"); +assertEq(done, true); +assertEq(value, "old return"); + +iter.return = () => { throw new Error(); }; +assertThrowsInstanceOf(wrap.return, Error); + +iter.return = null; +let nullResult = wrap.return("return argument ignored"); +assertEq(nullResult.done, true); +assertEq(nullResult.value, undefined); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/o-not-object-throws.js b/js/src/tests/non262/Iterator/from/o-not-object-throws.js new file mode 100644 index 0000000000..3e6c00e59a --- /dev/null +++ b/js/src/tests/non262/Iterator/from/o-not-object-throws.js @@ -0,0 +1,13 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Iterator.from throws when called with a non-object. +---*/ + +assertThrowsInstanceOf(() => Iterator.from(undefined), TypeError); +assertThrowsInstanceOf(() => Iterator.from(null), TypeError); +assertThrowsInstanceOf(() => Iterator.from(0), TypeError); +assertThrowsInstanceOf(() => Iterator.from(false), TypeError); +assertThrowsInstanceOf(() => Iterator.from(Symbol('')), TypeError); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/proxy-not-wrapped.js b/js/src/tests/non262/Iterator/from/proxy-not-wrapped.js new file mode 100644 index 0000000000..df38e0fc86 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/proxy-not-wrapped.js @@ -0,0 +1,34 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const log = []; +const handlerProxy = new Proxy({}, { + get: (target, key, receiver) => (...args) => { + log.push(`${key}: ${args[1]?.toString()}`); + + const item = Reflect[key](...args); + if (typeof item === 'function') + return (...args) => new Proxy(item.apply(receiver, args), handlerProxy); + return item; + }, +}); + +class Iter extends Iterator { + [Symbol.iterator]() { + return this; + } + next() { + return { done: false, value: 0 }; + } +} +const iter = new Iter(); +const proxy = new Proxy(iter, handlerProxy); +const wrap = Iterator.from(proxy); + +assertEq( + log.join('\n'), + `get: Symbol(Symbol.iterator) +get: next +getPrototypeOf: undefined` +); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/proxy-wrap-next.js b/js/src/tests/non262/Iterator/from/proxy-wrap-next.js new file mode 100644 index 0000000000..6be4e4b27b --- /dev/null +++ b/js/src/tests/non262/Iterator/from/proxy-wrap-next.js @@ -0,0 +1,30 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const log = []; +const handlerProxy = new Proxy({}, { + get: (target, key, receiver) => (...args) => { + log.push(`${key}: ${args[1]?.toString()}`); + + const item = Reflect[key](...args); + if (typeof item === 'function') + return item.bind(receiver); + return item; + }, +}); +const iter = new Proxy({ + next: () => ({ done: false, value: 0 }), +}, handlerProxy); + +const wrap = Iterator.from(iter); +// Call next multiple times. Should not call `get` on proxy. +wrap.next(); +wrap.next(); +wrap.next(); + +assertEqArray(log, [ + "get: Symbol(Symbol.iterator)", + "get: next", + "getPrototypeOf: undefined", +]); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/proxy-wrap-return.js b/js/src/tests/non262/Iterator/from/proxy-wrap-return.js new file mode 100644 index 0000000000..ff72cbc231 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/proxy-wrap-return.js @@ -0,0 +1,31 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const log = []; +const handlerProxy = new Proxy({}, { + get: (target, key, receiver) => (...args) => { + log.push(`${key}: ${args[1]?.toString()}`); + + const item = Reflect[key](...args); + if (typeof item === 'function') + return item.bind(receiver); + return item; + }, +}); +const iter = new Proxy({ + next: () => ({ done: false, value: 0 }), + return: (value) => ({ done: true, value }), +}, handlerProxy); + +const wrap = Iterator.from(iter); +wrap.return(); +wrap.return(); + +assertEqArray(log, [ + "get: Symbol(Symbol.iterator)", + "get: next", + "getPrototypeOf: undefined", + "get: return", + "get: return", +]); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/return-iterator-if-iterable.js b/js/src/tests/non262/Iterator/from/return-iterator-if-iterable.js new file mode 100644 index 0000000000..e83e25185d --- /dev/null +++ b/js/src/tests/non262/Iterator/from/return-iterator-if-iterable.js @@ -0,0 +1,25 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Iterator.from returns O if it is iterable, an iterator, and an instance of Iterator. +---*/ + +class TestIterator extends Iterator { + [Symbol.iterator]() { + return this; + } + + next() { + return { done: false, value: this.value++ }; + } + + value = 0; +} + +const iter = new TestIterator(); +assertEq(iter, Iterator.from(iter)); + +const arrayIter = [1, 2, 3][Symbol.iterator](); +assertEq(arrayIter, Iterator.from(arrayIter)); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterable.js b/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterable.js new file mode 100644 index 0000000000..09b974f327 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterable.js @@ -0,0 +1,28 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Iterator.from returns an iterator wrapper if O is not an iterable. +---*/ + +class TestIterator { + next() { + return { done: false, value: 0 }; + } +} + +const iter = new TestIterator(); +assertEq( + Symbol.iterator in iter, + false, + 'iter is not an iterable.' +); + +const wrapper = Iterator.from(iter); +assertEq(iter !== wrapper, true); +assertEq( + Symbol.iterator in wrapper, + true, + 'wrapper is an iterable.' +); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterator-instance.js b/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterator-instance.js new file mode 100644 index 0000000000..4045801277 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/return-wrapper-if-not-iterator-instance.js @@ -0,0 +1,24 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +/*--- + Iterator.from returns an iterator wrapper if O is not an instance of Iterator. +---*/ + +class TestIterator { + [Symbol.iterator]() { + return this; + } + + next() { + return { done: false, value: 0 }; + } +} + +const iter = new TestIterator(); +assertEq(iter instanceof Iterator, false); + +const wrapper = Iterator.from(iter); +assertEq(iter !== wrapper, true); +assertEq(wrapper instanceof Iterator, true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-functions-on-other-global.js b/js/src/tests/non262/Iterator/from/wrap-functions-on-other-global.js new file mode 100644 index 0000000000..a3d60d4b48 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-functions-on-other-global.js @@ -0,0 +1,29 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) + +class TestError extends Error {} + +function checkIterResult({done, value}, expectedDone, expectedValue) { + assertEq(done, expectedDone); + assertEq(value, expectedValue); +} + +const iter = { + next(value) { + return {done: false, value: arguments.length}; + }, + return() { + throw new TestError(); + }, + throw: (value) => ({done: true, value}), +}; +const thisWrap = Iterator.from(iter); +const otherGlobal = newGlobal({newCompartment: true}); +const otherWrap = otherGlobal.Iterator.from(iter); + +checkIterResult(thisWrap.next.call(otherWrap), false, 0); +checkIterResult(thisWrap.next.call(otherWrap, 'value'), false, 0); + +assertThrowsInstanceOf(thisWrap.return.bind(otherWrap), TestError); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-method-with-non-wrap-this-throws.js b/js/src/tests/non262/Iterator/from/wrap-method-with-non-wrap-this-throws.js new file mode 100644 index 0000000000..7ff8539923 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-method-with-non-wrap-this-throws.js @@ -0,0 +1,42 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +// All methods on %WrapForValidIteratorPrototype% require an [[Iterated]] +// internal slot on the `this` object. + +class TestIterator { + next() { + return { + done: false, + value: 0, + }; + } +} + +const nextMethod = Iterator.from(new TestIterator()).next; +assertThrowsInstanceOf(() => nextMethod.call(undefined), TypeError); +assertThrowsInstanceOf(() => nextMethod.call(null), TypeError); +assertThrowsInstanceOf(() => nextMethod.call(0), TypeError); +assertThrowsInstanceOf(() => nextMethod.call(false), TypeError); +assertThrowsInstanceOf(() => nextMethod.call('test'), TypeError); +assertThrowsInstanceOf(() => nextMethod.call(Object(1)), TypeError); +assertThrowsInstanceOf(() => nextMethod.call({}), TypeError); + +const returnMethod = Iterator.from(new TestIterator()).next; +assertThrowsInstanceOf(() => returnMethod.call(undefined), TypeError); +assertThrowsInstanceOf(() => returnMethod.call(null), TypeError); +assertThrowsInstanceOf(() => returnMethod.call(0), TypeError); +assertThrowsInstanceOf(() => returnMethod.call(false), TypeError); +assertThrowsInstanceOf(() => returnMethod.call('test'), TypeError); +assertThrowsInstanceOf(() => returnMethod.call(Object(1)), TypeError); +assertThrowsInstanceOf(() => returnMethod.call({}), TypeError); + +const throwMethod = Iterator.from(new TestIterator()).next; +assertThrowsInstanceOf(() => throwMethod.call(undefined), TypeError); +assertThrowsInstanceOf(() => throwMethod.call(null), TypeError); +assertThrowsInstanceOf(() => throwMethod.call(0), TypeError); +assertThrowsInstanceOf(() => throwMethod.call(false), TypeError); +assertThrowsInstanceOf(() => throwMethod.call('test'), TypeError); +assertThrowsInstanceOf(() => throwMethod.call(Object(1)), TypeError); +assertThrowsInstanceOf(() => throwMethod.call({}), TypeError); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-new-global.js b/js/src/tests/non262/Iterator/from/wrap-new-global.js new file mode 100644 index 0000000000..a9a26b41b0 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-new-global.js @@ -0,0 +1,9 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const otherGlobal = newGlobal({newCompartment: true}); + +const iter = [1, 2, 3].values(); +assertEq(iter, Iterator.from(iter)); +assertEq(iter !== otherGlobal.Iterator.from(iter), true); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-next-forwards-value.js b/js/src/tests/non262/Iterator/from/wrap-next-forwards-value.js new file mode 100644 index 0000000000..bc7c1c4086 --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-next-forwards-value.js @@ -0,0 +1,17 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +class Iter { + next(value) { + assertEq(arguments.length, 0); + return { done: false, value }; + } +} + +const iter = new Iter(); +const wrap = Iterator.from(iter); +assertEq(iter !== wrap, true); + +assertEq(iter.v, undefined); +wrap.next(1); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-next-not-object-throws.js b/js/src/tests/non262/Iterator/from/wrap-next-not-object-throws.js new file mode 100644 index 0000000000..c2bc5442cf --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-next-not-object-throws.js @@ -0,0 +1,18 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +const iter = (value) => Iterator.from({ + next: () => value, +}); + +for (let value of [ + undefined, + null, + 0, + false, + "test", + Symbol(""), +]) { + assertEq(iter(value).next(), value); +} + +if (typeof reportCompare === 'function') + reportCompare(0, 0); diff --git a/js/src/tests/non262/Iterator/from/wrap-return-closes-iterator.js b/js/src/tests/non262/Iterator/from/wrap-return-closes-iterator.js new file mode 100644 index 0000000000..c5d104db5c --- /dev/null +++ b/js/src/tests/non262/Iterator/from/wrap-return-closes-iterator.js @@ -0,0 +1,34 @@ +// |reftest| skip-if(!this.hasOwnProperty('Iterator')) -- Iterator is not enabled unconditionally +class Iter { + next() { + if (this.closed) + return { done: true, value: undefined }; + return { done: false, value: 0 }; + } + + return(value) { + assertEq(arguments.length, 0); + this.closed = true; + return { done: true, value: 42 }; + } +} + +const iter = new Iter(); +const wrap = Iterator.from(iter); +assertEq(iter.closed, undefined); + +let result = wrap.next(); +assertEq(result.done, false); +assertEq(result.value, 0); + +result = wrap.return(1); +assertEq(result.done, true); +assertEq(result.value, 42); + +assertEq(iter.closed, true); +result = wrap.next(); +assertEq(result.done, true); +assertEq(result.value, undefined); + +if (typeof reportCompare === 'function') + reportCompare(0, 0); |