summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/arguments-iterator-unmapped.js
blob: 29b2163596c879d3db7d846a195765084dd0b984 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Test iteration with an unmapped arguments object.

function simple() {
  function f() {
    "use strict";

    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  for (var i = 0; i < 100; ++i) {
    assertEq(f(1, 2, 3), 6);
  }
}
simple();

function spreadCall() {
  function f() {
    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  function g() {
    "use strict";
    return f(...arguments);
  }

  for (var i = 0; i < 100; ++i) {
    assertEq(g(1, 2, 3), 6);
  }
}
spreadCall();

function spreadArray() {
  function f() {
    "use strict";

    var arr = [...arguments];
    var sum = 0;
    for (var v of arr) {
      sum += v;
    }
    return sum;
  }

  for (var i = 0; i < 100; ++i) {
    assertEq(f(1, 2, 3), 6);
  }
}
spreadArray();

function reifyIterator() {
  var reify = false;
  function f() {
    "use strict";

    if (reify) {
      // Redefining any property attributes will reify the iterator property.
      Object.defineProperty(arguments, Symbol.iterator, {
        writable: false
      });
    }

    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  for (var i = 0; i <= 100; ++i) {
    reify = i >= 50;
    assertEq(f(1, 2, 3), 6);
  }
}
reifyIterator();

function overwriteIterator() {
  var callCount = 0;
  function Iterator() {
    callCount += 1;
    return Array.prototype[Symbol.iterator].call(this);
  }

  var overwrite = false;
  function f() {
    "use strict";

    if (overwrite) {
      arguments[Symbol.iterator] = Iterator;
    }

    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  for (var i = 0; i <= 100; ++i) {
    overwrite = i > 50;
    assertEq(f(1, 2, 3), 6);
  }
  assertEq(callCount, 50);
}
overwriteIterator();

function deleteIterator() {
  var remove = false;
  function f() {
    "use strict";

    // Deleting Symbol.iterator won't change the shape of the arguments object.
    // That's why we need to use a separate guard instruction to check if the
    // iterator property was modified.
    if (remove) {
      delete arguments[Symbol.iterator];
    }

    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  var error;
  try {
    for (var i = 0; i <= 100; ++i) {
      remove = i === 100;
      assertEq(f(1, 2, 3), 6);
    }
  } catch (e) {
    error = e;
  }
  assertEq(error instanceof TypeError, true);
}
deleteIterator();

function deleteIteratorInherit() {
  var callCount = 0;
  function Iterator() {
    callCount += 1;
    return Array.prototype[Symbol.iterator].call(this);
  }

  Object.prototype[Symbol.iterator] = Iterator;

  var remove = false;
  function f() {
    "use strict";

    // Deleting Symbol.iterator won't change the shape of the arguments object.
    // That's why we need to use a separate guard instruction to check if the
    // iterator property was modified.
    if (remove) {
      delete arguments[Symbol.iterator];
    }

    var sum = 0;
    for (var v of arguments) {
      sum += v;
    }
    return sum;
  }

  for (var i = 0; i <= 100; ++i) {
    remove = i === 100;
    assertEq(f(1, 2, 3), 6);
  }
  assertEq(callCount, 1);

  delete Object.prototype[Symbol.iterator];
}
deleteIteratorInherit();

// Don't add tests below this point because |Object.prototype[Symbol.iterator]|
// was modified, which may lead to engine-wide deoptimisations.