1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/basic/expression-autopsy.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 1.4 +load(libdir + "asserts.js"); 1.5 + 1.6 +function check_one(expected, f, err) { 1.7 + var failed = true; 1.8 + try { 1.9 + f(); 1.10 + failed = false; 1.11 + } catch (ex) { 1.12 + var s = ex.toString(); 1.13 + assertEq(s.slice(0, 11), "TypeError: "); 1.14 + assertEq(s.slice(-err.length), err, "" + f); 1.15 + assertEq(s.slice(11, -err.length), expected); 1.16 + } 1.17 + if (!failed) 1.18 + throw new Error("didn't fail"); 1.19 +} 1.20 +ieval = eval; 1.21 +function check(expr, expected=expr) { 1.22 + var end, err; 1.23 + for ([end, err] of [[".random_prop", " is undefined"], ["()", " is not a function"]]) { 1.24 + var statement = "o = {};" + expr + end, f; 1.25 + var cases = [ 1.26 + // Global scope 1.27 + function () { 1.28 + ieval("var o, undef;\n" + statement); 1.29 + }, 1.30 + // Function scope 1.31 + Function("o", "undef", statement), 1.32 + // Function scope with variables 1.33 + Function("var o, undef;\n" + statement), 1.34 + // Function scope with some different arugments 1.35 + Function("arg1", "arg2", "var o, undef;\n" + statement), 1.36 + // Deoptimized function scope 1.37 + Function("o", "undef", "with (Object) {}\n" + statement), 1.38 + // Inside with 1.39 + Function("with (Object) { " + statement + " }"), 1.40 + // Closure 1.41 + Function("o", "undef", "function myfunc() { return o + undef; }\n" + statement), 1.42 + // Let definitions in a block 1.43 + Function("{ let o, undef;\n" + statement + "}"), 1.44 + // Let block 1.45 + Function("let (o, undef) { " + statement + " }"), 1.46 + // Let block with some other variables 1.47 + Function("var v1, v2; let (o, undef) { " + statement + " }"), 1.48 + // Shadowed let block 1.49 + Function("o", "undef", "let (o, undef) { " + statement + " }"), 1.50 + // Let in a switch 1.51 + Function("var x = 4; switch (x) { case 4: let o, undef;" + statement + "\ncase 6: break;}"), 1.52 + // The more lets the merrier 1.53 + Function("let (x=4, y=5) { x + y; }\nlet (a, b, c) { a + b - c; }\nlet (o, undef) {" + statement + " }"), 1.54 + // Let destructuring 1.55 + Function("o", "undef", "let ([] = 4) {} let (o, undef) { " + statement + " }"), 1.56 + // Try-catch blocks 1.57 + Function("o", "undef", "try { let q = 4; try { let p = 4; } catch (e) {} } catch (e) {} let (o, undef) { " + statement + " }") 1.58 + ]; 1.59 + 1.60 + try { 1.61 + // Let in for-in 1.62 + check_one(expected, 1.63 + Function("var undef, o; for (let z in [1, 2]) { " + statement + " }"), 1.64 + err); 1.65 + } catch (ex) { 1.66 + // Bug 831120. See bug 942804 comment 5. 1.67 + if (expected == 'undef' && err == ' is undefined') 1.68 + check_one(expected + end, 1.69 + Function("var undef, o; for (let z in [1, 2]) { " + statement + " }"), 1.70 + err); 1.71 + else 1.72 + throw ex; 1.73 + } 1.74 + 1.75 + for (var f of cases) { 1.76 + check_one(expected, f, err); 1.77 + } 1.78 + } 1.79 +} 1.80 + 1.81 +check("undef"); 1.82 +check("o.b"); 1.83 +check("o.length"); 1.84 +check("o[true]"); 1.85 +check("o[false]"); 1.86 +check("o[null]"); 1.87 +check("o[0]"); 1.88 +check("o[1]"); 1.89 +check("o[3]"); 1.90 +check("o[256]"); 1.91 +check("o[65536]"); 1.92 +check("o[268435455]"); 1.93 +check("o['1.1']"); 1.94 +check("o[4 + 'h']", "o['4h']"); 1.95 +check("this.x"); 1.96 +check("ieval(undef)", "ieval(...)"); 1.97 +check("ieval.call()", "ieval.call(...)"); 1.98 +check("ieval(...[])", "ieval(...)"); 1.99 +check("ieval(...[undef])", "ieval(...)"); 1.100 +check("ieval(...[undef, undef])", "ieval(...)"); 1.101 + 1.102 +for (let tok of ["|", "^", "&", "==", "!==", "===", "!==", "<", "<=", ">", ">=", 1.103 + ">>", "<<", ">>>", "+", "-", "*", "/", "%"]) { 1.104 + check("o[(undef " + tok + " 4)]"); 1.105 +} 1.106 + 1.107 +check("o[!(o)]"); 1.108 +check("o[~(o)]"); 1.109 +check("o[+ (o)]"); 1.110 +check("o[- (o)]"); 1.111 + 1.112 +// A few one off tests 1.113 +check_one("6", (function () { 6() }), " is not a function"); 1.114 +check_one("Array.prototype.reverse.call(...)", (function () { Array.prototype.reverse.call('123'); }), " is read-only"); 1.115 +check_one("null", function () { var [{ x }] = [null, {}]; }, " has no properties"); 1.116 +check_one("x", function () { ieval("let (x) { var [a, b, [c0, c1]] = [x, x, x]; }") }, " is undefined"); 1.117 + 1.118 +// Check fallback behavior 1.119 +assertThrowsInstanceOf(function () { for (let x of undefined) {} }, TypeError);