summaryrefslogtreecommitdiffstats
path: root/js/src/tests
diff options
context:
space:
mode:
authorMoonchild <mcwerewolf@gmail.com>2018-03-16 08:36:30 +0100
committerGitHub <noreply@github.com>2018-03-16 08:36:30 +0100
commit11bdaa144d8a38ecd897dde278cb1db9b8313961 (patch)
tree1ca375db23843dcd4c593bcb28d7173856bddc10 /js/src/tests
parent0d3ee51ad2d61bffba14232b2df913d3a773d771 (diff)
parent114794557687aebca601c38ba0f0a52a43b44d4a (diff)
downloadUXP-11bdaa144d8a38ecd897dde278cb1db9b8313961.tar
UXP-11bdaa144d8a38ecd897dde278cb1db9b8313961.tar.gz
UXP-11bdaa144d8a38ecd897dde278cb1db9b8313961.tar.lz
UXP-11bdaa144d8a38ecd897dde278cb1db9b8313961.tar.xz
UXP-11bdaa144d8a38ecd897dde278cb1db9b8313961.zip
Merge pull request #66 from janekptacijarabaci/js_iterable_closures_1
Close iterator after error in: Map, Set, WeakMap, WeakSet, Array.from, Promise.{all,race}
Diffstat (limited to 'js/src/tests')
-rw-r--r--js/src/tests/ecma_6/Array/from-iterator-close.js183
-rw-r--r--js/src/tests/ecma_6/Map/constructor-iterator-close.js253
-rw-r--r--js/src/tests/ecma_6/Promise/iterator-close.js234
3 files changed, 670 insertions, 0 deletions
diff --git a/js/src/tests/ecma_6/Array/from-iterator-close.js b/js/src/tests/ecma_6/Array/from-iterator-close.js
new file mode 100644
index 000000000..fa97ea282
--- /dev/null
+++ b/js/src/tests/ecma_6/Array/from-iterator-close.js
@@ -0,0 +1,183 @@
+var BUGNUMBER = 1180306;
+var summary = 'Array.from should close iterator on error';
+
+print(BUGNUMBER + ": " + summary);
+
+function test(ctor, { mapVal=undefined,
+ nextVal=undefined,
+ nextThrowVal=undefined,
+ modifier=undefined,
+ exceptionVal=undefined,
+ exceptionType=undefined,
+ closed=true }) {
+ let iterable = {
+ closed: false,
+ [Symbol.iterator]() {
+ let iterator = {
+ first: true,
+ next() {
+ if (this.first) {
+ this.first = false;
+ if (nextThrowVal)
+ throw nextThrowVal;
+ return nextVal;
+ }
+ return { value: undefined, done: true };
+ },
+ return() {
+ iterable.closed = true;
+ return {};
+ }
+ };
+ if (modifier)
+ modifier(iterator, iterable);
+
+ return iterator;
+ }
+ };
+ if (exceptionVal) {
+ let caught = false;
+ try {
+ ctor.from(iterable, mapVal);
+ } catch (e) {
+ assertEq(e, exceptionVal);
+ caught = true;
+ }
+ assertEq(caught, true);
+ } else if (exceptionType) {
+ assertThrowsInstanceOf(() => ctor.from(iterable, mapVal), exceptionType);
+ } else {
+ ctor.from(iterable, mapVal);
+ }
+ assertEq(iterable.closed, closed);
+}
+
+// == Error cases with close ==
+
+// ES 2017 draft 22.1.2.1 step 5.e.i.1.
+// Cannot test.
+
+// ES 2017 draft 22.1.2.1 step 5.e.vi.2.
+test(Array, {
+ mapVal: () => { throw "map throws"; },
+ nextVal: { value: 1, done: false },
+ exceptionVal: "map throws",
+ closed: true,
+});
+
+// ES 2017 draft 22.1.2.1 step 5.e.ix.
+class MyArray extends Array {
+ constructor() {
+ return new Proxy({}, {
+ defineProperty() {
+ throw "defineProperty throws";
+ }
+ });
+ }
+}
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ exceptionVal: "defineProperty throws",
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 step 3.
+// if GetMethod fails, the thrown value should be used.
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ throw "return getter throws";
+ }
+ });
+ },
+ exceptionVal: "return getter throws",
+ closed: true,
+});
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ return "non object";
+ }
+ });
+ },
+ exceptionType: TypeError,
+ closed: true,
+});
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ // Non callable.
+ return {};
+ }
+ });
+ },
+ exceptionType: TypeError,
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 steps 6.
+// if return method throws, the thrown value should be ignored.
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ throw "return throws";
+ };
+ },
+ exceptionVal: "defineProperty throws",
+ closed: true,
+});
+
+test(MyArray, {
+ nextVal: { value: 1, done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ return "non object";
+ };
+ },
+ exceptionVal: "defineProperty throws",
+ closed: true,
+});
+
+// == Error cases without close ==
+
+// ES 2017 draft 22.1.2.1 step 5.e.iii.
+test(Array, {
+ nextThrowVal: "next throws",
+ exceptionVal: "next throws",
+ closed: false,
+});
+
+test(Array, {
+ nextVal: { value: {}, get done() { throw "done getter throws"; } },
+ exceptionVal: "done getter throws",
+ closed: false,
+});
+
+// ES 2017 draft 22.1.2.1 step 5.e.v.
+test(Array, {
+ nextVal: { get value() { throw "value getter throws"; }, done: false },
+ exceptionVal: "value getter throws",
+ closed: false,
+});
+
+// == Successful cases ==
+
+test(Array, {
+ nextVal: { value: 1, done: false },
+ closed: false,
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(true, true);
diff --git a/js/src/tests/ecma_6/Map/constructor-iterator-close.js b/js/src/tests/ecma_6/Map/constructor-iterator-close.js
new file mode 100644
index 000000000..ba4bee4a7
--- /dev/null
+++ b/js/src/tests/ecma_6/Map/constructor-iterator-close.js
@@ -0,0 +1,253 @@
+var BUGNUMBER = 1180306;
+var summary = 'Map/Set/WeakMap/WeakSet constructor should close iterator on error';
+
+print(BUGNUMBER + ": " + summary);
+
+function test(ctors, { nextVal=undefined,
+ nextThrowVal=undefined,
+ modifier=undefined,
+ exceptionVal=undefined,
+ exceptionType=undefined,
+ closed=true }) {
+ function getIterable() {
+ let iterable = {
+ closed: false,
+ [Symbol.iterator]() {
+ let iterator = {
+ first: true,
+ next() {
+ if (this.first) {
+ this.first = false;
+ if (nextThrowVal)
+ throw nextThrowVal;
+ return nextVal;
+ }
+ return { value: undefined, done: true };
+ },
+ return() {
+ iterable.closed = true;
+ return {};
+ }
+ };
+ if (modifier)
+ modifier(iterator, iterable);
+
+ return iterator;
+ }
+ };
+ return iterable;
+ }
+
+ for (let ctor of ctors) {
+ let iterable = getIterable();
+ if (exceptionVal) {
+ let caught = false;
+ try {
+ new ctor(iterable);
+ } catch (e) {
+ assertEq(e, exceptionVal);
+ caught = true;
+ }
+ assertEq(caught, true);
+ } else if (exceptionType) {
+ assertThrowsInstanceOf(() => new ctor(iterable), exceptionType);
+ } else {
+ new ctor(iterable);
+ }
+ assertEq(iterable.closed, closed);
+ }
+}
+
+// == Error cases with close ==
+
+// ES 2017 draft 23.1.1.1 Map step 8.d.ii.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.d.ii.
+test([Map, WeakMap], {
+ nextVal: { value: "non object", done: false },
+ exceptionType: TypeError,
+ closed: true,
+});
+
+// ES 2017 draft 23.1.1.1 Map step 8.f.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.f.
+test([Map, WeakMap], {
+ nextVal: { value: { get 0() { throw "0 getter throws"; } }, done: false },
+ exceptionVal: "0 getter throws",
+ closed: true,
+});
+
+// ES 2017 draft 23.1.1.1 Map step 8.h.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.h.
+test([Map, WeakMap], {
+ nextVal: { value: { 0: {}, get 1() { throw "1 getter throws"; } }, done: false },
+ exceptionVal: "1 getter throws",
+ closed: true,
+});
+
+// ES 2017 draft 23.1.1.1 Map step 8.j.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.j.
+class MyMap extends Map {
+ set(k, v) {
+ throw "setter throws";
+ }
+}
+class MyWeakMap extends WeakMap {
+ set(k, v) {
+ throw "setter throws";
+ }
+}
+test([MyMap, MyWeakMap], {
+ nextVal: { value: [{}, {}], done: false },
+ exceptionVal: "setter throws",
+ closed: true,
+});
+
+// ES 2017 draft 23.2.1.1 Set step 8.e.
+// ES 2017 draft 23.4.1.1 WeakSet step 8.e.
+class MySet extends Set {
+ add(v) {
+ throw "adder throws";
+ }
+}
+class MyWeakSet extends WeakSet {
+ add(v) {
+ throw "adder throws";
+ }
+}
+test([MySet, MyWeakSet], {
+ nextVal: { value: {}, done: false },
+ exceptionVal: "adder throws",
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 step 3.
+// if GetMethod fails, the thrown value should be used.
+test([MyMap, MySet, MyWeakMap, MyWeakSet], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ throw "return getter throws";
+ }
+ });
+ },
+ exceptionVal: "return getter throws",
+ closed: true,
+});
+test([MyMap, MySet, MyWeakMap, MyWeakSet], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ return "non object";
+ }
+ });
+ },
+ exceptionType: TypeError,
+ closed: true,
+});
+test([MyMap, MySet, MyWeakMap, MyWeakSet], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ // Non callable.
+ return {};
+ }
+ });
+ },
+ exceptionType: TypeError,
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 steps 6.
+// if return method throws, the thrown value should be ignored.
+test([MyMap, MyWeakMap], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ throw "return throws";
+ };
+ },
+ exceptionVal: "setter throws",
+ closed: true,
+});
+test([MySet, MyWeakSet], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ throw "return throws";
+ };
+ },
+ exceptionVal: "adder throws",
+ closed: true,
+});
+
+test([MyMap, MyWeakMap], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ return "non object";
+ };
+ },
+ exceptionVal: "setter throws",
+ closed: true,
+});
+test([MySet, MyWeakSet], {
+ nextVal: { value: [{}, {}], done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ return "non object";
+ };
+ },
+ exceptionVal: "adder throws",
+ closed: true,
+});
+
+// == Error cases without close ==
+
+// ES 2017 draft 23.1.1.1 Map step 8.a.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.a.
+// ES 2017 draft 23.2.1.1 Set step 8.a.
+// ES 2017 draft 23.4.1.1 WeakSet step 8.a.
+test([Map, WeakMap, Set, WeakSet], {
+ nextThrowVal: "next throws",
+ exceptionVal: "next throws",
+ closed: false,
+});
+test([Map, WeakMap, Set, WeakSet], {
+ nextVal: { value: {}, get done() { throw "done getter throws"; } },
+ exceptionVal: "done getter throws",
+ closed: false,
+});
+
+// ES 2017 draft 23.1.1.1 Map step 8.c.
+// ES 2017 draft 23.3.1.1 WeakMap step 8.c.
+// ES 2017 draft 23.2.1.1 Set step 8.c.
+// ES 2017 draft 23.4.1.1 WeakSet step 8.c.
+test([Map, WeakMap, Set, WeakSet], {
+ nextVal: { get value() { throw "value getter throws"; }, done: false },
+ exceptionVal: "value getter throws",
+ closed: false,
+});
+
+// == Successful cases ==
+
+test([Map, WeakMap], {
+ nextVal: { value: [{}, {}], done: false },
+ closed: false,
+});
+test([Set, WeakSet], {
+ nextVal: { value: {}, done: false },
+ closed: false,
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(true, true);
diff --git a/js/src/tests/ecma_6/Promise/iterator-close.js b/js/src/tests/ecma_6/Promise/iterator-close.js
new file mode 100644
index 000000000..f17260d05
--- /dev/null
+++ b/js/src/tests/ecma_6/Promise/iterator-close.js
@@ -0,0 +1,234 @@
+// |reftest| skip-if(!xulRuntime.shell) -- needs drainJobQueue
+
+var BUGNUMBER = 1180306;
+var summary = 'Promise.{all,race} should close iterator on error';
+
+print(BUGNUMBER + ": " + summary);
+
+function test(ctor, props, { nextVal=undefined,
+ nextThrowVal=undefined,
+ modifier=undefined,
+ rejectReason=undefined,
+ rejectType=undefined,
+ closed=true }) {
+ function getIterable() {
+ let iterable = {
+ closed: false,
+ [Symbol.iterator]() {
+ let iterator = {
+ first: true,
+ next() {
+ if (this.first) {
+ this.first = false;
+ if (nextThrowVal)
+ throw nextThrowVal;
+ return nextVal;
+ }
+ return { value: undefined, done: true };
+ },
+ return() {
+ iterable.closed = true;
+ return {};
+ }
+ };
+ if (modifier)
+ modifier(iterator, iterable);
+
+ return iterator;
+ }
+ };
+ return iterable;
+ }
+ for (let prop of props) {
+ let iterable = getIterable();
+ let e;
+ ctor[prop](iterable).catch(e_ => { e = e_; });
+ drainJobQueue();
+ if(rejectType)
+ assertEq(e instanceof rejectType, true);
+ else
+ assertEq(e, rejectReason);
+ assertEq(iterable.closed, closed);
+ }
+}
+
+// == Error cases with close ==
+
+// ES 2017 draft 25.4.4.1.1 step 6.i.
+// ES 2017 draft 25.4.4.3.1 step 3.h.
+class MyPromiseStaticResolveGetterThrows extends Promise {
+ static get resolve() {
+ throw "static resolve getter throws";
+ }
+};
+test(MyPromiseStaticResolveGetterThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ rejectReason: "static resolve getter throws",
+ closed: true,
+});
+
+class MyPromiseStaticResolveThrows extends Promise {
+ static resolve() {
+ throw "static resolve throws";
+ }
+};
+test(MyPromiseStaticResolveThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ rejectReason: "static resolve throws",
+ closed: true,
+});
+
+// ES 2017 draft 25.4.4.1.1 step 6.q.
+// ES 2017 draft 25.4.4.3.1 step 3.i.
+class MyPromiseThenGetterThrows extends Promise {
+ static resolve() {
+ return {
+ get then() {
+ throw "then getter throws";
+ }
+ };
+ }
+};
+test(MyPromiseThenGetterThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ rejectReason: "then getter throws",
+ closed: true,
+});
+
+class MyPromiseThenThrows extends Promise {
+ static resolve() {
+ return {
+ then() {
+ throw "then throws";
+ }
+ };
+ }
+};
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ rejectReason: "then throws",
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 step 3.
+// if GetMethod fails, the thrown value should be used.
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ throw "return getter throws";
+ }
+ });
+ },
+ rejectReason: "return getter throws",
+ closed: true,
+});
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ return "non object";
+ }
+ });
+ },
+ rejectType: TypeError,
+ closed: true,
+});
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ modifier: (iterator, iterable) => {
+ Object.defineProperty(iterator, "return", {
+ get: function() {
+ iterable.closed = true;
+ // Non callable.
+ return {};
+ }
+ });
+ },
+ rejectType: TypeError,
+ closed: true,
+});
+
+// ES 2017 draft 7.4.6 steps 6.
+// if return method throws, the thrown value should be ignored.
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ throw "return throws";
+ };
+ },
+ rejectReason: "then throws",
+ closed: true,
+});
+
+test(MyPromiseThenThrows, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ modifier: (iterator, iterable) => {
+ iterator.return = function() {
+ iterable.closed = true;
+ return "non object";
+ };
+ },
+ rejectReason: "then throws",
+ closed: true,
+});
+
+// == Error cases without close ==
+
+// ES 2017 draft 25.4.4.1.1 step 6.a.
+test(Promise, ["all", "race"], {
+ nextThrowVal: "next throws",
+ rejectReason: "next throws",
+ closed: false,
+});
+
+test(Promise, ["all", "race"], {
+ nextVal: { value: {}, get done() { throw "done getter throws"; } },
+ rejectReason: "done getter throws",
+ closed: false,
+});
+
+// ES 2017 draft 25.4.4.1.1 step 6.e.
+test(Promise, ["all", "race"], {
+ nextVal: { get value() { throw "value getter throws"; }, done: false },
+ rejectReason: "value getter throws",
+ closed: false,
+});
+
+// ES 2017 draft 25.4.4.1.1 step 6.d.iii.2.
+let first = true;
+class MyPromiseResolveThrows extends Promise {
+ constructor(executer) {
+ if (first) {
+ first = false;
+ super((resolve, reject) => {
+ executer(() => {
+ throw "resolve throws";
+ }, reject);
+ });
+ return;
+ }
+ super(executer);
+ }
+};
+test(MyPromiseResolveThrows, ["all"], {
+ nextVal: { value: undefined, done: true },
+ rejectReason: "resolve throws",
+ closed: false,
+});
+
+// == Successful cases ==
+
+test(Promise, ["all", "race"], {
+ nextVal: { value: Promise.resolve(1), done: false },
+ closed: false,
+});
+
+if (typeof reportCompare === 'function')
+ reportCompare(true, true);