1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
28 assertEquals(void 0, eval());
29 assertEquals(4, eval(4));
31 function f() { return 'The f function'; };
32 assertTrue(f === eval(f));
34 function g(x, y) { return 4; };
37 assertEquals(4, eval('2 + 2', count++));
38 assertEquals(1, count);
41 eval('hest 7 &*^*&^');
42 assertUnreachable('Did not throw on syntax error.');
44 assertEquals('SyntaxError', e.name);
48 // eval has special evaluation order for consistency with other browsers.
50 assertEquals(void 0, eval(eval("var eval = function f(x) { return 'hest';}")))
53 // Test eval with different number of parameters.
55 eval = function(x, y) { return x + y; };
56 assertEquals(4, eval(2, 2));
59 // Test that un-aliased eval reads from local context.
66 assertEquals(2, result);
68 // Test that un-aliased eval writes to local context.
76 assertEquals(2, result);
79 // Test that un-aliased eval has right receiver.
80 function MyObject() { this.self = eval('this'); }
81 var o = new MyObject();
82 assertTrue(o === o.self);
84 // Test that aliased eval reads from global context.
92 assertEquals(0, result);
94 // Test that aliased eval writes to global context.
97 (function() { e('var foo = 2;'); })();
100 // Test that aliased eval has right receiver.
101 function MyOtherObject() { this.self = e('this'); }
102 var o = new MyOtherObject();
103 assertTrue(this === o.self);
105 // Try to cheat the 'aliased eval' detection.
111 // Should be non-direct call.
112 return x.eval('foo');
114 assertEquals(0, result);
120 // Should be non-direct call.
121 return (1,eval)('foo');
123 assertEquals(0, result);
128 var eval = function(x) { return x; };
130 // Should be non-direct call.
133 assertEquals(0, result);
139 // Should be direct call.
140 with ({ eval : e }) {
144 assertEquals(2, result);
148 var eval = function(x) { return 2 * x; };
149 return (function() { return eval(2); })();
151 assertEquals(4, result);
155 eval("var eval = function(s) { return this; }");
156 return eval("42"); // Should return the global object
158 assertEquals(this, result);
161 var obj = { f: function(eval) { return eval("this"); } };
162 result = obj.f(eval);
163 assertEquals(obj, result);
167 var obj = { f: function(eval) { arguments; return eval("this"); } };
168 result = obj.f(eval);
169 assertEquals(obj, result);
172 eval = function(x) { return 2 * x; };
175 return (function() { return eval(2); })();
177 assertEquals(4, result);
182 // 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.
186 var eval = function (x) { return this; };
187 with ({}) { return eval('ignore'); }
189 assertEquals(this, result);