Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / eval-keyword-vs-function.html
blobcac457976a392fe222332280f5d569b64666b79b
1 <p>
2 This page verifies that eval has two meanings:
3 <ol>
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>
6 </ol>
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").
9 </p>
10 <p>If the test passes, you'll see a series of pass messages below.</p>
11 <hr>
12 <pre id="console"></pre>
14 <script>
15 if (window.testRunner)
16 testRunner.dumpAsText();
18 var globalEval = eval;
20 function log(s)
22 document.getElementById("console").appendChild(document.createTextNode(s + "\n"));
25 function shouldBe(aDescription, a, b)
27 if (a === b) {
28 log("PASS: " + aDescription + " should be " + b + " and is.");
29 } else {
30 log("FAIL: " + aDescription + " should be " + b + " but instead is " + a + ".");
34 function testGetX()
36 var x = 1;
37 var localEval = window.eval;
39 window.x = 0;
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);
52 function testVarY()
54 var localEval = window.eval;
56 shouldBe('eval("var y; \"y\" in window")', eval("var y; \"y\" in window"), false);
57 delete window.y;
59 shouldBe('window.eval("var y; \"y\" in window")', window.eval("var y; \"y\" in window"), true);
60 delete window.y;
62 shouldBe('globalEval("var y; \"y\" in window")', globalEval("var y; \"y\" in window"), true);
63 delete window.y;
65 shouldBe('localEval("var y; \"y\" in window")', localEval("var y; \"y\" in window"), true);
66 delete window.y;
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);
73 function testSetZ()
75 var z = 0;
76 window.z = 0;
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);
92 function testThis()
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");
104 testGetX();
105 log("\n----- Variable Object: -----\n");
106 testVarY();
107 log("\n----- Scope Chain for Setters: -----\n");
108 testSetZ();
109 log("\n----- This Object: -----\n");
110 testThis.call({ toString: function() { return "[\"this\" object passed to .call()]"; } });
111 </script>