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.
7 <p>If the test passes, you'll see a series of pass messages below.
</p>
8 <pre id=
"console"></pre>
11 <pre id=
"console"></pre>
12 <iframe style=
"width:0; height:0"></iframe>
15 if (window
.testRunner
)
16 testRunner
.dumpAsText();
19 var frameEval
= frames
[0].eval
;
23 document
.getElementById("console").appendChild(document
.createTextNode(s
+ "\n"));
26 function shouldBe(aDescription
, a
, b
)
29 log("PASS: " + aDescription
+ " should be " + b
+ " and is.");
31 log("FAIL: " + aDescription
+ " should be " + b
+ " but instead is " + a
+ ".");
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
;
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
;
71 shouldBe('window.eval("var y; \"y\" in top")', window
.eval("var y; \"y\" in top"), true);
75 shouldBe('frames[0].eval("var y; \"y\" in top.frames[0]")', frames
[0].eval("var y; \"y\" in top.frames[0]"), true);
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);
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);
89 frames
[0].eval
= frameEval
;
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
;
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");
126 log("\n----- Scope Chain for Getters: -----\n");
128 log("\n----- Variable Object: -----\n");
130 log("\n----- Scope Chain for Setters: -----\n");
132 log("\n----- This Object: -----\n");