js/src/jit-test/tests/basic/expression-autopsy.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 load(libdir + "asserts.js");
michael@0 2
michael@0 3 function check_one(expected, f, err) {
michael@0 4 var failed = true;
michael@0 5 try {
michael@0 6 f();
michael@0 7 failed = false;
michael@0 8 } catch (ex) {
michael@0 9 var s = ex.toString();
michael@0 10 assertEq(s.slice(0, 11), "TypeError: ");
michael@0 11 assertEq(s.slice(-err.length), err, "" + f);
michael@0 12 assertEq(s.slice(11, -err.length), expected);
michael@0 13 }
michael@0 14 if (!failed)
michael@0 15 throw new Error("didn't fail");
michael@0 16 }
michael@0 17 ieval = eval;
michael@0 18 function check(expr, expected=expr) {
michael@0 19 var end, err;
michael@0 20 for ([end, err] of [[".random_prop", " is undefined"], ["()", " is not a function"]]) {
michael@0 21 var statement = "o = {};" + expr + end, f;
michael@0 22 var cases = [
michael@0 23 // Global scope
michael@0 24 function () {
michael@0 25 ieval("var o, undef;\n" + statement);
michael@0 26 },
michael@0 27 // Function scope
michael@0 28 Function("o", "undef", statement),
michael@0 29 // Function scope with variables
michael@0 30 Function("var o, undef;\n" + statement),
michael@0 31 // Function scope with some different arugments
michael@0 32 Function("arg1", "arg2", "var o, undef;\n" + statement),
michael@0 33 // Deoptimized function scope
michael@0 34 Function("o", "undef", "with (Object) {}\n" + statement),
michael@0 35 // Inside with
michael@0 36 Function("with (Object) { " + statement + " }"),
michael@0 37 // Closure
michael@0 38 Function("o", "undef", "function myfunc() { return o + undef; }\n" + statement),
michael@0 39 // Let definitions in a block
michael@0 40 Function("{ let o, undef;\n" + statement + "}"),
michael@0 41 // Let block
michael@0 42 Function("let (o, undef) { " + statement + " }"),
michael@0 43 // Let block with some other variables
michael@0 44 Function("var v1, v2; let (o, undef) { " + statement + " }"),
michael@0 45 // Shadowed let block
michael@0 46 Function("o", "undef", "let (o, undef) { " + statement + " }"),
michael@0 47 // Let in a switch
michael@0 48 Function("var x = 4; switch (x) { case 4: let o, undef;" + statement + "\ncase 6: break;}"),
michael@0 49 // The more lets the merrier
michael@0 50 Function("let (x=4, y=5) { x + y; }\nlet (a, b, c) { a + b - c; }\nlet (o, undef) {" + statement + " }"),
michael@0 51 // Let destructuring
michael@0 52 Function("o", "undef", "let ([] = 4) {} let (o, undef) { " + statement + " }"),
michael@0 53 // Try-catch blocks
michael@0 54 Function("o", "undef", "try { let q = 4; try { let p = 4; } catch (e) {} } catch (e) {} let (o, undef) { " + statement + " }")
michael@0 55 ];
michael@0 56
michael@0 57 try {
michael@0 58 // Let in for-in
michael@0 59 check_one(expected,
michael@0 60 Function("var undef, o; for (let z in [1, 2]) { " + statement + " }"),
michael@0 61 err);
michael@0 62 } catch (ex) {
michael@0 63 // Bug 831120. See bug 942804 comment 5.
michael@0 64 if (expected == 'undef' && err == ' is undefined')
michael@0 65 check_one(expected + end,
michael@0 66 Function("var undef, o; for (let z in [1, 2]) { " + statement + " }"),
michael@0 67 err);
michael@0 68 else
michael@0 69 throw ex;
michael@0 70 }
michael@0 71
michael@0 72 for (var f of cases) {
michael@0 73 check_one(expected, f, err);
michael@0 74 }
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 check("undef");
michael@0 79 check("o.b");
michael@0 80 check("o.length");
michael@0 81 check("o[true]");
michael@0 82 check("o[false]");
michael@0 83 check("o[null]");
michael@0 84 check("o[0]");
michael@0 85 check("o[1]");
michael@0 86 check("o[3]");
michael@0 87 check("o[256]");
michael@0 88 check("o[65536]");
michael@0 89 check("o[268435455]");
michael@0 90 check("o['1.1']");
michael@0 91 check("o[4 + 'h']", "o['4h']");
michael@0 92 check("this.x");
michael@0 93 check("ieval(undef)", "ieval(...)");
michael@0 94 check("ieval.call()", "ieval.call(...)");
michael@0 95 check("ieval(...[])", "ieval(...)");
michael@0 96 check("ieval(...[undef])", "ieval(...)");
michael@0 97 check("ieval(...[undef, undef])", "ieval(...)");
michael@0 98
michael@0 99 for (let tok of ["|", "^", "&", "==", "!==", "===", "!==", "<", "<=", ">", ">=",
michael@0 100 ">>", "<<", ">>>", "+", "-", "*", "/", "%"]) {
michael@0 101 check("o[(undef " + tok + " 4)]");
michael@0 102 }
michael@0 103
michael@0 104 check("o[!(o)]");
michael@0 105 check("o[~(o)]");
michael@0 106 check("o[+ (o)]");
michael@0 107 check("o[- (o)]");
michael@0 108
michael@0 109 // A few one off tests
michael@0 110 check_one("6", (function () { 6() }), " is not a function");
michael@0 111 check_one("Array.prototype.reverse.call(...)", (function () { Array.prototype.reverse.call('123'); }), " is read-only");
michael@0 112 check_one("null", function () { var [{ x }] = [null, {}]; }, " has no properties");
michael@0 113 check_one("x", function () { ieval("let (x) { var [a, b, [c0, c1]] = [x, x, x]; }") }, " is undefined");
michael@0 114
michael@0 115 // Check fallback behavior
michael@0 116 assertThrowsInstanceOf(function () { for (let x of undefined) {} }, TypeError);

mercurial