File size: 3,110 Bytes
ae4627d | 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 | function truth() {
return true;
}
noInline(truth);
function assert(cond) {
if (!cond)
throw new Error("broke assertion");
}
noInline(assert);
function shouldThrowTDZ(func) {
var hasThrown = false;
try {
func();
} catch(e) {
if (e.name.indexOf("ReferenceError") !== -1)
hasThrown = true;
}
assert(hasThrown);
}
noInline(shouldThrowTDZ);
// Tests
const NUM_LOOPS = 1000;
;(function() {
function foo() {
delete x;
const x = 20;
}
function bar() {
delete x;
const x = 20;
function capX() { return x; }
}
for (var i = 0; i < NUM_LOOPS; i++) {
shouldThrowTDZ(foo);
shouldThrowTDZ(bar);
}
})();
;(function() {
function foo() {
var hadError = false;
try {
x;
} catch(e) {
hadError = true;
}
assert(hadError);
if (truth()) {
// This eval is enterpreted as follows:
// eval("var x; x = 20");
// We first assign undefined to the "var x".
// Then, we interperet an assignment expression
// into the resolved variable x. x resolves to the lexical "const x;"
// Look at ECMA section 13.3.2.4 of the ES6 spec:
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-variable-statement-runtime-semantics-evaluation
// And also look at section 8.3.1 ResolveBinding:
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-resolvebinding
const x = 40;
let threw = false;
try {
eval("var x = 20;");
} catch(e) {
if (e.name.indexOf("TypeError") !== -1 && e.message.indexOf("readonly") !== -1)
threw = true;
}
assert(threw);
assert(x === 40);
}
assert(x === undefined);
}
function bar() {
var hadError = false;
try {
x;
} catch(e) {
hadError = true;
}
assert(hadError);
if (truth()) {
const x = 40;
function capX() { return x; }
let threw = false;
try {
eval("var x = 20;");
} catch(e) {
if (e.name.indexOf("TypeError") !== -1 && e.message.indexOf("readonly") !== -1)
threw = true;
}
assert(threw);
assert(x === 40);
}
assert(x === undefined);
}
function baz() {
if (truth()) {
const x = 40;
eval("const x = 20; assert(x === 20);");
assert(x === 40);
}
if (truth()) {
const x = 40;
function capX() { return x; }
eval("const x = 20; assert(x === 20);");
assert(x === 40);
}
}
function baz() {
// Test eval() caching.
const x = 20;
const evalString = "x;";
assert(eval(evalString) === 20);
if (truth()) {
const y = 60;
assert(eval(evalString) === 20);
assert(y === 60);
if (truth()) {
const y = 50, z = 70, x = 40;
assert(eval(evalString) === 40);
assert(y === 50 && z === 70);
}
}
}
for (var i = 0; i < NUM_LOOPS; i++) {
foo();
bar();
baz();
}
})();
|