eval.js revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1// Copyright 2008 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 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 &*^*&^'); 42 assertTrue(false, 'Did not throw on syntax error.'); 43} 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 53//Test eval with different number of parameters. 54global_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; 61result = 62 (function() { 63 var foo = 2; 64 return eval('foo'); 65 })(); 66assertEquals(2, result); 67 68//Test that un-aliased eval writes to local context. 69foo = 0; 70result = 71 (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; 87result = 88 (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; 108result = 109 (function() { 110 var foo = 2; 111 return x.eval('foo'); 112 })(); 113assertEquals(0, result); 114 115foo = 0; 116result = 117 (function() { 118 var eval = function(x) { return x; }; 119 var foo = eval(2); 120 return e('foo'); 121 })(); 122assertEquals(0, result); 123 124result = 125 (function() { 126 var eval = function(x) { return 2 * x; }; 127 return (function() { return eval(2); })(); 128 })(); 129assertEquals(4, result); 130 131eval = function(x) { return 2 * x; }; 132result = 133 (function() { 134 return (function() { return eval(2); })(); 135 })(); 136assertEquals(4, result); 137