ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 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> |
| 9 | |
| 10 | <hr> |
| 11 | <pre id="console"></pre> |
| 12 | <iframe style="width:0; height:0"></iframe> |
| 13 | |
| 14 | <script> |
rniwa@webkit.org | 03b9c6d | 2012-07-16 01:41:53 +0000 | [diff] [blame] | 15 | if (window.testRunner) |
| 16 | testRunner.dumpAsText(); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 17 | |
| 18 | var topEval = eval; |
| 19 | var frameEval = frames[0].eval; |
| 20 | |
| 21 | function log(s) |
| 22 | { |
| 23 | document.getElementById("console").appendChild(document.createTextNode(s + "\n")); |
| 24 | } |
| 25 | |
| 26 | function shouldBe(aDescription, a, b) |
| 27 | { |
| 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 + "."); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | function testGetX() |
| 36 | { |
| 37 | window.x = 0; |
| 38 | frames[0].x = 1; |
| 39 | var x = 2; |
| 40 | |
| 41 | shouldBe('window.eval("x")', window.eval("x"), 0); |
| 42 | shouldBe('frames[0].eval("x")', frames[0].eval("x"), 1); |
| 43 | |
| 44 | window.eval = frameEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 45 | shouldBe('window.eval("x")', (function() { try { return window.eval("x") } catch(e) { return e.name; } })(), 1); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 46 | window.eval = topEval; |
| 47 | |
| 48 | frames[0].eval = topEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 49 | shouldBe('frames[0].eval("x")', (function() { try { frames[0].eval("x") } catch(e) { return e.name; } })(), undefined); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 50 | frames[0].eval = frameEval; |
| 51 | } |
| 52 | |
| 53 | function testGetXX() |
| 54 | { |
| 55 | var xx = 0; |
| 56 | |
| 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"); |
| 59 | |
| 60 | window.eval = frameEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 61 | shouldBe('window.eval("xx")', (function() { try { return window.eval("xx") } catch(e) { return e.name; } })(), "ReferenceError"); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 62 | window.eval = topEval; |
| 63 | |
| 64 | frames[0].eval = topEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 65 | shouldBe('frames[0].eval("xx")', (function() { try { return frames[0].eval("xx") } catch(e) { return e.name; } })(), "ReferenceError"); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 66 | frames[0].eval = frameEval; |
| 67 | } |
| 68 | |
| 69 | function testVarY() |
| 70 | { |
| 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; |
| 74 | |
| 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; |
| 78 | |
| 79 | window.eval = frameEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 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); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 81 | delete window.y; |
| 82 | delete frames[0].y; |
| 83 | window.eval = topEval; |
| 84 | |
| 85 | frames[0].eval = topEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 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); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 87 | delete window.y; |
| 88 | delete frames[0].y; |
| 89 | frames[0].eval = frameEval; |
| 90 | } |
| 91 | |
| 92 | function testSetZ() |
| 93 | { |
| 94 | window.z = 0; |
| 95 | frames[0].z = 0; |
| 96 | var z = 0; |
| 97 | |
| 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); |
| 100 | |
| 101 | window.eval = frameEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 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); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 103 | window.eval = topEval; |
| 104 | |
| 105 | frames[0].eval = topEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 106 | shouldBe('frames[0].eval("z = 4; top.z")', (function() { try { frames[0].eval("z = 4; top.z") } catch(e) { return e.name; } })(), undefined); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 107 | frames[0].eval = frameEval; |
| 108 | } |
| 109 | |
| 110 | function testThis() |
| 111 | { |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 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]); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 114 | |
| 115 | window.eval = frameEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 116 | shouldBe('window.eval("this")', (function() { try { window.eval.call("wrong", "this"), frames[0] } catch(e) { return e.name; } })(), undefined); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 117 | window.eval = topEval; |
| 118 | |
| 119 | frames[0].eval = topEval; |
barraclough@apple.com | 1e61b89 | 2012-09-26 23:25:21 +0000 | [diff] [blame] | 120 | shouldBe('frames[0].eval("this")', (function() { try { frames[0].eval.call("wrong", "this"), window } catch(e) { return e.name; } })(), undefined); |
ggaren@apple.com | 6171673 | 2008-03-07 19:46:33 +0000 | [diff] [blame] | 121 | frames[0].eval = frameEval; |
| 122 | } |
| 123 | |
| 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> |