blob: 100f21653fb2943a63c88473e45de6f263d445ad [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28assertEquals(void 0, eval());
29assertEquals(4, eval(4));
30
31function f() { return 'The f function'; };
32assertTrue(f === eval(f));
33
34function g(x, y) { return 4; };
35
36count = 0;
37assertEquals(4, eval('2 + 2', count++));
38assertEquals(1, count);
39
40try {
41 eval('hest 7 &*^*&^');
Ben Murdoch3ef787d2012-04-12 10:51:47 +010042 assertUnreachable('Did not throw on syntax error.');
Steve Blocka7e24c12009-10-30 11:49:00 +000043} catch (e) {
44 assertEquals('SyntaxError', e.name);
45}
46
47
48// eval has special evaluation order for consistency with other browsers.
49global_eval = eval;
50assertEquals(void 0, eval(eval("var eval = function f(x) { return 'hest';}")))
51eval = global_eval;
52
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010053// Test eval with different number of parameters.
Steve Blocka7e24c12009-10-30 11:49:00 +000054global_eval = eval;
55eval = function(x, y) { return x + y; };
56assertEquals(4, eval(2, 2));
57eval = global_eval;
58
59// Test that un-aliased eval reads from local context.
60foo = 0;
Leon Clarkee46be812010-01-19 14:06:41 +000061result =
Steve Blocka7e24c12009-10-30 11:49:00 +000062 (function() {
63 var foo = 2;
64 return eval('foo');
65 })();
66assertEquals(2, result);
67
Leon Clarkee46be812010-01-19 14:06:41 +000068// Test that un-aliased eval writes to local context.
Steve Blocka7e24c12009-10-30 11:49:00 +000069foo = 0;
Leon Clarkee46be812010-01-19 14:06:41 +000070result =
Steve Blocka7e24c12009-10-30 11:49:00 +000071 (function() {
72 var foo = 1;
73 eval('foo = 2');
74 return foo;
75 })();
76assertEquals(2, result);
77assertEquals(0, foo);
78
79// Test that un-aliased eval has right receiver.
80function MyObject() { this.self = eval('this'); }
81var o = new MyObject();
82assertTrue(o === o.self);
83
84// Test that aliased eval reads from global context.
85var e = eval;
86foo = 0;
Leon Clarkee46be812010-01-19 14:06:41 +000087result =
Steve Blocka7e24c12009-10-30 11:49:00 +000088 (function() {
89 var foo = 2;
90 return e('foo');
91 })();
92assertEquals(0, result);
93
94// Test that aliased eval writes to global context.
95var e = eval;
96foo = 0;
97(function() { e('var foo = 2;'); })();
98assertEquals(2, foo);
99
100// Test that aliased eval has right receiver.
101function MyOtherObject() { this.self = e('this'); }
102var o = new MyOtherObject();
103assertTrue(this === o.self);
104
105// Try to cheat the 'aliased eval' detection.
106var x = this;
107foo = 0;
Leon Clarkee46be812010-01-19 14:06:41 +0000108result =
Steve Blocka7e24c12009-10-30 11:49:00 +0000109 (function() {
110 var foo = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111 // Should be non-direct call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 return x.eval('foo');
113 })();
114assertEquals(0, result);
115
116foo = 0;
Leon Clarkee46be812010-01-19 14:06:41 +0000117result =
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 (function() {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100119 var foo = 2;
120 // Should be non-direct call.
121 return (1,eval)('foo');
122 })();
123assertEquals(0, result);
124
125foo = 0;
126result =
127 (function() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 var eval = function(x) { return x; };
129 var foo = eval(2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100130 // Should be non-direct call.
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 return e('foo');
132 })();
133assertEquals(0, result);
134
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100135foo = 0;
136result =
137 (function() {
138 var foo = 2;
139 // Should be direct call.
140 with ({ eval : e }) {
141 return eval('foo');
142 }
143 })();
144assertEquals(2, result);
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146result =
147 (function() {
148 var eval = function(x) { return 2 * x; };
149 return (function() { return eval(2); })();
150 })();
151assertEquals(4, result);
152
Leon Clarkee46be812010-01-19 14:06:41 +0000153result =
154 (function() {
155 eval("var eval = function(s) { return this; }");
156 return eval("42"); // Should return the global object
157 })();
158assertEquals(this, result);
159
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100160(function() {
161 var obj = { f: function(eval) { return eval("this"); } };
162 result = obj.f(eval);
163 assertEquals(obj, result);
164})();
Leon Clarkee46be812010-01-19 14:06:41 +0000165
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100166(function() {
167 var obj = { f: function(eval) { arguments; return eval("this"); } };
168 result = obj.f(eval);
169 assertEquals(obj, result);
170})();
Leon Clarkee46be812010-01-19 14:06:41 +0000171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172eval = function(x) { return 2 * x; };
Leon Clarkee46be812010-01-19 14:06:41 +0000173result =
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 (function() {
175 return (function() { return eval(2); })();
176 })();
177assertEquals(4, result);
Ben Murdoch257744e2011-11-30 15:57:28 +0000178
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100179
180
181
Ben Murdoch257744e2011-11-30 15:57:28 +0000182// Regression test: calling a function named eval found in a context that is
183// not the global context should get the global object as receiver.
184result =
185 (function () {
186 var eval = function (x) { return this; };
187 with ({}) { return eval('ignore'); }
188 })();
189assertEquals(this, result);