Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / js / eval-cross-window.html
blob0343a553ad307ec8518ec06789a3704e11737f7a
1 <p>
2 This page verifies that eval, when called as a function, uses the "this" object
3 provided by the call as its variable object, scope chain, and "this" object.
4 However, if the "this" object is not the global object eval was originally
5 associated with, eval throws an exception.
6 </p>
7 <p>If the test passes, you'll see a series of pass messages below.</p>
8 <pre id="console"></pre>
10 <hr>
11 <pre id="console"></pre>
12 <iframe style="width:0; height:0"></iframe>
14 <script>
15 if (window.testRunner)
16 testRunner.dumpAsText();
18 var topEval = eval;
19 var frameEval = frames[0].eval;
21 function log(s)
23 document.getElementById("console").appendChild(document.createTextNode(s + "\n"));
26 function shouldBe(aDescription, a, b)
28 if (a === b) {
29 log("PASS: " + aDescription + " should be " + b + " and is.");
30 } else {
31 log("FAIL: " + aDescription + " should be " + b + " but instead is " + a + ".");
35 function testGetX()
37 window.x = 0;
38 frames[0].x = 1;
39 var x = 2;
41 shouldBe('window.eval("x")', window.eval("x"), 0);
42 shouldBe('frames[0].eval("x")', frames[0].eval("x"), 1);
44 window.eval = frameEval;
45 shouldBe('window.eval("x")', (function() { try { return window.eval("x") } catch(e) { return e.name; } })(), 1);
46 window.eval = topEval;
48 frames[0].eval = topEval;
49 shouldBe('frames[0].eval("x")', (function() { try { frames[0].eval("x") } catch(e) { return e.name; } })(), undefined);
50 frames[0].eval = frameEval;
53 function testGetXX()
55 var xx = 0;
57 shouldBe('window.eval("xx")', (function() { try { return window.eval("xx") } catch(e) { return e.name; } })(), "ReferenceError");
58 shouldBe('frames[0].eval("xx")', (function() { try { return frames[0].eval("xx") } catch(e) { return e.name; } })(), "ReferenceError");
60 window.eval = frameEval;
61 shouldBe('window.eval("xx")', (function() { try { return window.eval("xx") } catch(e) { return e.name; } })(), "ReferenceError");
62 window.eval = topEval;
64 frames[0].eval = topEval;
65 shouldBe('frames[0].eval("xx")', (function() { try { return frames[0].eval("xx") } catch(e) { return e.name; } })(), "ReferenceError");
66 frames[0].eval = frameEval;
69 function testVarY()
71 shouldBe('window.eval("var y; \"y\" in top")', window.eval("var y; \"y\" in top"), true);
72 delete window.y;
73 delete frames[0].y;
75 shouldBe('frames[0].eval("var y; \"y\" in top.frames[0]")', frames[0].eval("var y; \"y\" in top.frames[0]"), true);
76 delete window.y;
77 delete frames[0].y;
79 window.eval = frameEval;
80 shouldBe('window.eval("var y; \"y\" in top.frames[0]")', (function() { try { window.eval("var y; \"y\" in top.frames[0]") } catch(e) { return e.name; } })(), undefined);
81 delete window.y;
82 delete frames[0].y;
83 window.eval = topEval;
85 frames[0].eval = topEval;
86 shouldBe('frames[0].eval("var y; \"y\" in top")', (function() { try { frames[0].eval("var y; \"y\" in top") } catch(e) { return e.name; } })(), undefined);
87 delete window.y;
88 delete frames[0].y;
89 frames[0].eval = frameEval;
92 function testSetZ()
94 window.z = 0;
95 frames[0].z = 0;
96 var z = 0;
98 shouldBe('window.eval("z = 1; top.z")', window.eval("z = 1; top.z"), 1);
99 shouldBe('frames[0].eval("z = 2; top.frames[0].z")', frames[0].eval("z = 2; top.frames[0].z"), 2);
101 window.eval = frameEval;
102 shouldBe('window.eval("z = 3; top.frames[0].z")', (function() { try { window.eval("z = 3; top.frames[0].z") } catch(e) { return e.name; } })(), undefined);
103 window.eval = topEval;
105 frames[0].eval = topEval;
106 shouldBe('frames[0].eval("z = 4; top.z")', (function() { try { frames[0].eval("z = 4; top.z") } catch(e) { return e.name; } })(), undefined);
107 frames[0].eval = frameEval;
110 function testThis()
112 shouldBe('window.eval("this")', window.eval.call("wrong", "this"), window);
113 shouldBe('frames[0].eval("this")', frames[0].eval.call("wrong", "this"), frames[0]);
115 window.eval = frameEval;
116 shouldBe('window.eval("this")', (function() { try { window.eval.call("wrong", "this"), frames[0] } catch(e) { return e.name; } })(), undefined);
117 window.eval = topEval;
119 frames[0].eval = topEval;
120 shouldBe('frames[0].eval("this")', (function() { try { frames[0].eval.call("wrong", "this"), window } catch(e) { return e.name; } })(), undefined);
121 frames[0].eval = frameEval;
124 log("\n----- Scope Chain Head for Getters: -----\n");
125 testGetX();
126 log("\n----- Scope Chain for Getters: -----\n");
127 testGetXX();
128 log("\n----- Variable Object: -----\n");
129 testVarY();
130 log("\n----- Scope Chain for Setters: -----\n");
131 testSetZ();
132 log("\n----- This Object: -----\n");
133 testThis.call({});
134 </script>