2 This page verifies that eval has two meanings:
4 <li>An operator: executes a script in local scope with the local scope's variable object and
"this" object.
</li>
5 <li>A global function: executes a script in global scope with the global scope's variable object and
"this" object.
</li>
7 Meaning #
2 should remain constant even if the global eval function is copied
8 into a global variable (
"globalEval") or a local variable (
"localEval").
10 <p>If the test passes, you'll see a series of pass messages below.
</p>
12 <pre id=
"console"></pre>
15 if (window
.testRunner
)
16 testRunner
.dumpAsText();
18 var globalEval
= eval
;
22 document
.getElementById("console").appendChild(document
.createTextNode(s
+ "\n"));
25 function shouldBe(aDescription
, a
, b
)
28 log("PASS: " + aDescription
+ " should be " + b
+ " and is.");
30 log("FAIL: " + aDescription
+ " should be " + b
+ " but instead is " + a
+ ".");
37 var localEval
= window
.eval
;
41 shouldBe('eval("x")', eval("x"), 1);
43 shouldBe('window.eval("x")', window
.eval("x"), 0);
44 shouldBe('globalEval("x")', globalEval("x"), 0);
45 shouldBe('localEval("x")', localEval("x"), 0);
47 // Per ES5 15.1.2.11 & 10.2.2.1 any reference that hits an enviromment record (i.e. does not have a base)
48 // and has a reference name of "eval" is treated as a direct eval - the assignment to a var makes no difference.
49 shouldBe('(function() { var eval = window.eval; return eval("x"); })()', (function() { var eval
= window
.eval
; return eval("x"); })(), 1);
54 var localEval
= window
.eval
;
56 shouldBe('eval("var y; \"y\" in window")', eval("var y; \"y\" in window"), false);
59 shouldBe('window.eval("var y; \"y\" in window")', window
.eval("var y; \"y\" in window"), true);
62 shouldBe('globalEval("var y; \"y\" in window")', globalEval("var y; \"y\" in window"), true);
65 shouldBe('localEval("var y; \"y\" in window")', localEval("var y; \"y\" in window"), true);
68 // Per ES5 15.1.2.11 & 10.2.2.1 any reference that hits an enviromment record (i.e. does not have a base)
69 // and has a reference name of "eval" is treated as a direct eval - the assignment to a var makes no difference.
70 shouldBe('(function() { var eval = window.eval; return eval("var y; \"y\" in window"); })()', (function() { var eval
= window
.eval
; return eval("var y; \"y\" in window"); })(), false);
77 var localEval
= window
.eval
;
79 shouldBe('eval("z = 1; window.z")', eval("z = 1; window.z"), 0);
81 shouldBe('window.eval("z = 2; window.z")', window
.eval("z = 2; window.z"), 2);
83 shouldBe('globalEval("z = 3; window.z")', globalEval("z = 3; window.z"), 3);
85 shouldBe('localEval("z = 4; window.z")', localEval("z = 4; window.z"), 4);
87 // Per ES5 15.1.2.11 & 10.2.2.1 any reference that hits an enviromment record (i.e. does not have a base)
88 // and has a reference name of "eval" is treated as a direct eval - the assignment to a var makes no difference.
89 shouldBe('(function() { var eval = window.eval; return eval("z = 5; window.z"); })()', (function() { var eval
= window
.eval
; return eval("z = 5; window.z"); })(), 4);
94 var localEval
= window
.eval
;
96 shouldBe('eval("this")', eval("this"), this);
97 shouldBe('window.eval("this")', window
.eval("this"), window
);
98 shouldBe('globalEval("this")', globalEval("this"), window
);
99 shouldBe('localEval("this")', localEval("this"), window
);
100 shouldBe('(function() { var eval = window.eval; return eval("this"); })()', (function() { var eval
= window
.eval
; return eval("this"); })(), window
);
103 log("\n----- Scope Chain for Getters: -----\n");
105 log("\n----- Variable Object: -----\n");
107 log("\n----- Scope Chain for Setters: -----\n");
109 log("\n----- This Object: -----\n");
110 testThis
.call({ toString: function() { return "[\"this\" object passed to .call()]"; } });