OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 eval = global_eval; | 51 eval = global_eval; |
52 | 52 |
53 //Test eval with different number of parameters. | 53 //Test eval with different number of parameters. |
54 global_eval = eval; | 54 global_eval = eval; |
55 eval = function(x, y) { return x + y; }; | 55 eval = function(x, y) { return x + y; }; |
56 assertEquals(4, eval(2, 2)); | 56 assertEquals(4, eval(2, 2)); |
57 eval = global_eval; | 57 eval = global_eval; |
58 | 58 |
59 // Test that un-aliased eval reads from local context. | 59 // Test that un-aliased eval reads from local context. |
60 foo = 0; | 60 foo = 0; |
61 result = | 61 result = |
62 (function() { | 62 (function() { |
63 var foo = 2; | 63 var foo = 2; |
64 return eval('foo'); | 64 return eval('foo'); |
65 })(); | 65 })(); |
66 assertEquals(2, result); | 66 assertEquals(2, result); |
67 | 67 |
68 //Test that un-aliased eval writes to local context. | 68 // Test that un-aliased eval writes to local context. |
69 foo = 0; | 69 foo = 0; |
70 result = | 70 result = |
71 (function() { | 71 (function() { |
72 var foo = 1; | 72 var foo = 1; |
73 eval('foo = 2'); | 73 eval('foo = 2'); |
74 return foo; | 74 return foo; |
75 })(); | 75 })(); |
76 assertEquals(2, result); | 76 assertEquals(2, result); |
77 assertEquals(0, foo); | 77 assertEquals(0, foo); |
78 | 78 |
79 // Test that un-aliased eval has right receiver. | 79 // Test that un-aliased eval has right receiver. |
80 function MyObject() { this.self = eval('this'); } | 80 function MyObject() { this.self = eval('this'); } |
81 var o = new MyObject(); | 81 var o = new MyObject(); |
82 assertTrue(o === o.self); | 82 assertTrue(o === o.self); |
83 | 83 |
84 // Test that aliased eval reads from global context. | 84 // Test that aliased eval reads from global context. |
85 var e = eval; | 85 var e = eval; |
86 foo = 0; | 86 foo = 0; |
87 result = | 87 result = |
88 (function() { | 88 (function() { |
89 var foo = 2; | 89 var foo = 2; |
90 return e('foo'); | 90 return e('foo'); |
91 })(); | 91 })(); |
92 assertEquals(0, result); | 92 assertEquals(0, result); |
93 | 93 |
94 // Test that aliased eval writes to global context. | 94 // Test that aliased eval writes to global context. |
95 var e = eval; | 95 var e = eval; |
96 foo = 0; | 96 foo = 0; |
97 (function() { e('var foo = 2;'); })(); | 97 (function() { e('var foo = 2;'); })(); |
98 assertEquals(2, foo); | 98 assertEquals(2, foo); |
99 | 99 |
100 // Test that aliased eval has right receiver. | 100 // Test that aliased eval has right receiver. |
101 function MyOtherObject() { this.self = e('this'); } | 101 function MyOtherObject() { this.self = e('this'); } |
102 var o = new MyOtherObject(); | 102 var o = new MyOtherObject(); |
103 assertTrue(this === o.self); | 103 assertTrue(this === o.self); |
104 | 104 |
105 // Try to cheat the 'aliased eval' detection. | 105 // Try to cheat the 'aliased eval' detection. |
106 var x = this; | 106 var x = this; |
107 foo = 0; | 107 foo = 0; |
108 result = | 108 result = |
109 (function() { | 109 (function() { |
110 var foo = 2; | 110 var foo = 2; |
111 return x.eval('foo'); | 111 return x.eval('foo'); |
112 })(); | 112 })(); |
113 assertEquals(0, result); | 113 assertEquals(0, result); |
114 | 114 |
115 foo = 0; | 115 foo = 0; |
116 result = | 116 result = |
117 (function() { | 117 (function() { |
118 var eval = function(x) { return x; }; | 118 var eval = function(x) { return x; }; |
119 var foo = eval(2); | 119 var foo = eval(2); |
120 return e('foo'); | 120 return e('foo'); |
121 })(); | 121 })(); |
122 assertEquals(0, result); | 122 assertEquals(0, result); |
123 | 123 |
124 result = | 124 result = |
125 (function() { | 125 (function() { |
126 var eval = function(x) { return 2 * x; }; | 126 var eval = function(x) { return 2 * x; }; |
127 return (function() { return eval(2); })(); | 127 return (function() { return eval(2); })(); |
128 })(); | 128 })(); |
129 assertEquals(4, result); | 129 assertEquals(4, result); |
130 | 130 |
| 131 result = |
| 132 (function() { |
| 133 eval("var eval = function(s) { return this; }"); |
| 134 return eval("42"); // Should return the global object |
| 135 })(); |
| 136 assertEquals(this, result); |
| 137 |
| 138 result = |
| 139 (function() { |
| 140 var obj = { f: function(eval) { return eval("this"); } }; |
| 141 return obj.f(eval); |
| 142 })(); |
| 143 assertEquals(this, result); |
| 144 |
| 145 result = |
| 146 (function() { |
| 147 var obj = { f: function(eval) { arguments; return eval("this"); } }; |
| 148 return obj.f(eval); |
| 149 })(); |
| 150 assertEquals(this, result); |
| 151 |
131 eval = function(x) { return 2 * x; }; | 152 eval = function(x) { return 2 * x; }; |
132 result = | 153 result = |
133 (function() { | 154 (function() { |
134 return (function() { return eval(2); })(); | 155 return (function() { return eval(2); })(); |
135 })(); | 156 })(); |
136 assertEquals(4, result); | 157 assertEquals(4, result); |
OLD | NEW |