1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15assert (eval () === undefined); 16assert (eval (undefined) === undefined); 17assert (eval (null) === null); 18assert (eval (true) === true); 19assert (eval (false) === false); 20assert (eval (1) === 1); 21assert (eval (eval) === eval); 22 23/* Indirect eval */ 24function f1() 25{ 26 var v1 = 'local value'; 27 28 assert (v1 === 'local value'); 29 assert (typeof (this.v1) === 'undefined'); 30 31 r = this.eval ('var v1 = "global value";'); 32 33 assert (v1 === 'local value'); 34 assert (this.v1 === 'global value'); 35 assert (r === undefined); 36}; 37 38f1 (); 39 40/* Direct eval from strict mode code */ 41function f2 (global) 42{ 43 'use strict'; 44 var v2 = 'local value'; 45 46 assert (v2 === 'local value'); 47 assert (typeof (global.v2) === 'undefined'); 48 49 r = eval ('var v2 = "global value";'); 50 51 assert (v2 === 'local value'); 52 assert (typeof (global.v2) === 'undefined'); 53 assert (r === undefined); 54 55 try 56 { 57 eval ('arguments = 1;'); 58 assert (false); 59 } 60 catch (e) 61 { 62 assert (e instanceof SyntaxError); 63 } 64} 65 66f2 (this); 67 68var y; 69 70for (var i = 0; i < 100; i++) 71{ 72 var r = eval ('var x =' + ' 1;'); 73 assert (typeof (x) === 'number'); 74 assert (r === undefined); 75 76 delete x; 77 assert (typeof (x) === 'undefined'); 78 79 r = eval ('"use ' + 's' + 't' + 'r' + 'i' + 'c' + 't"; va' + 'r x = 1;'); 80 assert (typeof (x) === 'undefined'); 81 assert (r === "use strict"); 82 83 y = 'str'; 84 assert (typeof (y) === 'string'); 85 86 delete y; 87 assert (typeof (y) === 'string'); 88 89 r = eval ('var y = "another ' + 'string";'); 90 assert (y === 'another string'); 91 assert (r == undefined); 92 93 delete y; 94 assert (typeof (y) === 'string'); 95 96 r = eval ('if (true) 3; else 5;'); 97 assert (r === 3); 98} 99 100// Check SyntaxError handling 101try 102{ 103 eval ('var var;'); 104 assert (false); 105} 106catch (e) 107{ 108 assert (e instanceof SyntaxError); 109} 110 111try 112{ 113 eval ("v_0 = {a: Math, /[]/};"); 114 assert (false); 115} 116catch(e) 117{ 118 assert (e instanceof SyntaxError); 119} 120 121// nested eval with function expressions 122code = 'eval("(function (){})")'; 123code = "eval ('" + code + "')"; 124eval (code); 125 126// Eval enclosed in brackets is still an eval. 127var p1 = 0; 128 129function f3() { 130 var p1 = 5; 131 (eval)("assert(p1 === 5)"); 132} 133f3(); 134 135function f4() { 136 var p1 = 6; 137 ((eval))("assert(p1 === 6)"); 138} 139f4(); 140 141function f5() { 142 var p1 = 7; 143 (((((eval)))("assert(p1 === 7)"))); 144} 145f5(); 146 147function f6() { 148 var p1 = 8; 149 var e = eval; 150 151 e("assert(p1 === 0)"); 152 (((((e)))("assert(p1 === 0)"))); 153} 154f6(); 155 156y = 1; 157function f7() { 158 function x() { return y; } 159 eval("assert(x()() === 5); function y() { return 5 } assert(x()() === 5)"); 160} 161f7() 162