summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/basic/expression-autopsy.js
blob: bbdcaf8bea68e716ea42d1be49984903deec6492 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
load(libdir + "asserts.js");
load(libdir + "iteration.js");

function check_one(expected, f, err) {
    var failed = true;
    try {
        f();
        failed = false;
    } catch (ex) {
        assertEq(ex.constructor.name, "TypeError");
        var s = ex.message;
        assertEq(s.slice(-err.length), err);
        assertEq(s.slice(-(err.length + expected.length), -err.length), expected);
    }
    if (!failed)
        throw new Error("didn't fail");
}
ieval = eval;
function check(expr, expected=expr, testStrict=true) {
    var end, err;
    for ([end, err] of [[".random_prop", " is undefined"], ["()", " is not a function"]]) {
        var statement = "o = {};" + expr + end, f;
        var cases = [
            // Global scope
            function () {
                ieval("var o, undef;\n" + statement);
            },
            // Function scope
            Function("o", "undef", statement),
            // Function scope with variables
            Function("var o, undef;\n" + statement),
            // Function scope with some different arugments
            Function("arg1", "arg2", "var o, undef;\n" + statement),
            // Deoptimized function scope
            Function("o", "undef", "with (Object) {}\n" + statement),
            // Inside with
            Function("with (Object) { " + statement + " }"),
            // Closure
            Function("o", "undef", "function myfunc() { return o + undef; }\n" + statement),
            // Let definitions in a block
            Function("{ let o, undef;\n" + statement + "}"),
            // Let in a switch
            Function("var x = 4; switch (x) { case 4: let o, undef;" + statement + "\ncase 6: break;}"),
            // Try-catch blocks
            Function("o", "undef", "try { let q = 4; try { let p = 4; } catch (e) {} } catch (e) {} { let o, undef; " + statement + " }"),
            // Let in for-in (uses with to prevent jit compilation: bug 942804, bug 831120 and bug 1041586)
            Function("with ({}) {} var undef, o; for (let z in [1, 2]) { " + statement + " }"),
        ];

        if (testStrict) {
            // Strict mode.
            cases.push(Function("o", "undef", "\"use strict\";\n" + statement));
        }

        for (var f of cases) {
            check_one(expected, f, err);
        }
    }
}

check("undef");
check("o.b");
check("o.length");
check("o[true]");
check("o[false]");
check("o[null]");
check("o[0]");
check("o[1]");
check("o[3]");
check("o[256]");
check("o[65536]");
check("o[268435455]");
check("o['1.1']");
check("o[4 + 'h']", "o['4h']");
check("ieval(undef)", "ieval(...)");
check("ieval.call()", "ieval.call()");
check("ieval(...[])", "ieval(...)");
check("ieval(...[undef])", "ieval(...)");
check("ieval(...[undef, undef])", "ieval(...)");
check("(o[0] = 4).foo", "o[0].foo");
// NOTE: This one produces different output in strict mode since "this" is
// undefined in that case.
check("this.x", "this.x", false);

for (let tok of ["|", "^", "&", "==", "!==", "===", "!==", "<", "<=", ">", ">=",
                 ">>", "<<", ">>>", "+", "-", "*", "/", "%"]) {
    check("o[(undef " + tok + " 4)]");
}

check("o[(!o)]");
check("o[(~o)]");
check("o[(+ o)]");
check("o[(- o)]");

check("o[(!(o + 1))]");
check("o[(~(o + 1))]");
check("o[(+ (o + 1))]");
check("o[(- (o + 1))]");

// A few one off tests
check_one("6", (function () { 6() }), " is not a function");
check_one("4", (function() { (4||eval)(); }), " is not a function");
check_one("0", (function () { Array.prototype.reverse.call('123'); }), " is read-only");
check_one("[...][Symbol.iterator]().next().value",
          function () { ieval("{ let x; var [a, b, [c0, c1]] = [x, x, x]; }") }, " is undefined");
check_one("(void 1)", function() { (void 1)(); }, " is not a function");
check_one("(void o[1])", function() { var o = []; (void o[1])() }, " is not a function");

check_one("(typeof 1)", function() { (typeof 1)(); }, " is not a function");
check_one("(typeof o[1])", function() { var o = []; (typeof o[1])() }, " is not a function");

check_one("(delete foo)",
          function() { (delete foo)(); },
          " is not a function");
check_one("(delete obj.foo)",
          function() { var obj = {}; (delete obj.foo)(); },
          " is not a function");
check_one("(delete obj.foo)",
          function() { "use strict"; var obj = {}; (delete obj.foo)(); },
          " is not a function");
check_one("(delete obj[y])",
          function() { var obj = {}, y = {}; (delete obj[y])(); },
          " is not a function");
check_one("(delete obj[y])",
          function() { "use strict"; var obj = {}, y = {}; (delete obj[y])(); },
          " is not a function");

check_one("foo.apply()",
          function() { function foo() {} foo.apply()(); },
          " is not a function");

check_one("super()",
          function() {
            class X extends Object {
              constructor() {
                super()();
              }
            }
            new X();
          },
          " is not a function");
check_one("super(...)",
          function() {
            class X extends Object {
              constructor() {
                super(...[])();
              }
            }
            new X();
          },
          " is not a function");

check_one("super.a",
          function() {
            class X extends Object {
              test() {
                super.a();
              }
            }
            var x = new X();
            x.test();
          },
          " is not a function");

check_one("super[a]",
          function() {
            var a = "a";
            class X extends Object {
              test() {
                super[a]();
              }
            }
            var x = new X();
            x.test();
          },
          " is not a function");

check_one("super.a()",
          function() {
            class Y {
              a() {
                return 5;
              }
            }

            class X extends Y {
              test() {
                super.a()();
              }
            }

            var x = new X();
            x.test();
          },
          " is not a function");

check_one("super[a]()",
          function() {
            class Y {
              a() {
                return 5;
              }
            }

            var a = "a";
            class X extends Y {
              test() {
                super[a]()();
              }
            }

            var x = new X();
            x.test();
          },
          " is not a function");

check_one("super[1]",
          function() {
            class X extends Object {
              foo() {
                return super[1]();
              }
            }
            new X().foo();
          },
          " is not a function");

check_one("eval(...)",
          function() { eval("")(); },
          " is not a function");
check_one("eval(...)",
          function() { "use strict"; eval("")(); },
          " is not a function");
check_one("eval(...)",
          function() { eval(...[""])(); },
          " is not a function");
check_one("eval(...)",
          function() { "use strict"; eval(...[""])(); },
          " is not a function");

check_one("(new foo())",
          function() { function foo() {}; new foo()(); },
          " is not a function");
check_one("(new foo(...))",
          function() { function foo() {}; new foo(...[])(); },
          " is not a function");
check_one("(new foo.x())",
          function() { var foo = { x: function() {} }; new foo.x()(); },
          " is not a function");
check_one("(new foo.x(...))",
          function() { var foo = { x: function() {} }; new foo.x(...[])(); },
          " is not a function");

check_one("[...].foo",
          function() { [undefined].foo(); },
          " is not a function");
check_one("[...].foo",
          function() { [undefined, ...[]].foo(); },
          " is not a function");

check_one("[...][Symbol.iterator]().next().value",
          function () { var [{x}] = [null, {}]; }, " is null");
check_one("[...][Symbol.iterator]().next().value",
          function () { var [{x}] = [void 0, {}]; }, " is undefined");

check_one("(a += b)",
    function() { var a, b; (a += b)(); },
    " is not a function");

try {
  (function() {
    "use strict";
    var o = [];
    Object.freeze(o);
    o[0] = "foo";
  }());
  throw new Error("didn't throw");
} catch (e) {
  assertEq(e instanceof TypeError, true,
           "expected TypeError, got " + e);
  assertEq(e.message,
           "can't define array index property past the end of an array with non-writable length");
}

// Check fallback behavior
assertThrowsInstanceOf(function () { for (let x of undefined) {} }, TypeError);