summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/AsyncIterator/prototype/async-iterator-helpers-from-other-global.js
blob: f8b8d719cd397b1dcadcdaf79219bd1edf8a309f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))

class TestError extends Error {}

class TestIterator extends AsyncIterator {
  async next() {
    return {done: false, value: 'value'};
  }

  closed = false;
  async return(value) {
    this.closed = true;
    return {done: true, value};
  }
}

function checkIterResult({done, value}, expectedDone, expectedValue) {
  assertEq(done, expectedDone);
  assertEq(Array.isArray(value) ? value[1] : value, expectedValue);
}

const otherGlobal = newGlobal({newCompartment: true});

const methods = [
  ["map", x => x],
  ["filter", x => true],
  ["take", Infinity],
  ["drop", 0],
  ["asIndexedPairs", undefined],
  ["flatMap", async function*(x) { yield x; }],
];

const {next: otherNext, return: otherReturn, throw: otherThrow} =
  Object.getPrototypeOf(otherGlobal.eval("(async function*() {})().map(x => x)"));

(async () => {
  for (const [method, arg] of methods) {
    const iterator = new TestIterator();
    const helper = iterator[method](arg);
    checkIterResult(await otherNext.call(helper), false, 'value');
  }

  for (const [method, arg] of methods) {
    const iterator = new TestIterator();
    const helper = iterator[method](arg);
    assertEq(iterator.closed, false);
    checkIterResult(await otherReturn.call(helper), true, undefined);
    assertEq(iterator.closed, true);
  }

  for (const [method, arg] of methods) {
    const iterator = new TestIterator();
    const helper = iterator[method](arg);
    try {
      await otherThrow.call(helper, new TestError());
      assertEq(true, false, 'Expected exception');
    } catch (exc) {
      assertEq(exc instanceof TestError, true);
    }
    checkIterResult(await helper.next(), true, undefined);
  }
})();

if (typeof reportCompare === 'function')
  reportCompare(0, 0);