summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/iterator-next-with-detached.js
blob: d6a31160a6f5ff304d1e9f16b4ef3620ea6dfd81 (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
66
67
68
69
70
71
72
73
function checkResult(actual, expected)
{
  assertEq(actual.value, expected.value);
  assertEq(actual.done, expected.done);
}

if (typeof detachArrayBuffer === "function" && typeof newGlobal === "function")
{
  var iteratorFunction = Uint8Array.prototype[Symbol.iterator];


  var thisGlobal = this;
  var otherGlobal = newGlobal();

  var thisNext = new Uint8Array()[Symbol.iterator]().next

  for (const constructor of typedArrayConstructors)
  {
    assertEq(new constructor()[Symbol.iterator]().next, thisNext);

    var globals =
      [
       [thisGlobal, thisGlobal],
       [thisGlobal, otherGlobal],
       [otherGlobal, otherGlobal],
       [otherGlobal, thisGlobal],
      ];

    for (const [arrayGlobal, bufferGlobal] of globals)
    {
      var arr, buffer, iterator;

      function arrayBufferIterator()
      {
        var byteLength = 2 * constructor.BYTES_PER_ELEMENT;
        var buf = new bufferGlobal.ArrayBuffer(byteLength);
        var tarray = new arrayGlobal[constructor.name](buf);

        tarray[0] = 1;
        tarray[1] = 2;

        return [tarray, buf, Reflect.apply(iteratorFunction, tarray, [])];
      }

      [arr, buffer, iterator] = arrayBufferIterator();
      checkResult(thisNext.call(iterator), {value: 1, done: false});
      checkResult(thisNext.call(iterator), {value: 2, done: false});
      checkResult(thisNext.call(iterator), {value: undefined, done: true});

      // Test an exhausted iterator.
      bufferGlobal.detachArrayBuffer(buffer);
      checkResult(thisNext.call(iterator), {value: undefined, done: true});

      // Test an all-but-exhausted iterator.
      [arr, buffer, iterator] = arrayBufferIterator();
      checkResult(thisNext.call(iterator), {value: 1, done: false});
      checkResult(thisNext.call(iterator), {value: 2, done: false});

      bufferGlobal.detachArrayBuffer(buffer);
      assertThrowsInstanceOf(() => thisNext.call(iterator), TypeError);

      // Test an unexhausted iterator.
      [arr, buffer, iterator] = arrayBufferIterator();
      checkResult(thisNext.call(iterator), {value: 1, done: false});

      bufferGlobal.detachArrayBuffer(buffer);
      assertThrowsInstanceOf(() => thisNext.call(iterator), TypeError);
    }
  }
}

if (typeof reportCompare === "function")
  reportCompare(true, true);