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 15var f = new Function (''); 16assert (f () === undefined); 17 18var f = new Function ('"use strict"; f = undefined;'); 19assert (f () === undefined && f === undefined); 20 21var f = new Function ('"use strict"; q = undefined;'); 22try 23{ 24 f (); 25 assert (false); 26} 27catch (e) 28{ 29 assert (e instanceof ReferenceError); 30} 31 32var singleArgFunction = new Function ('arg', 'return arg'); 33 34assert (singleArgFunction (5) === 5); 35 36for (i = 1; i < 10; i ++) 37{ 38 var f = new Function ('a', 'b', 'var q = a; b++; function f (k) {return q + k + b++;}; return f;'); 39 40 var fns = new Array (); 41 42 for (var n = 0; n < 10; n++) 43 { 44 var r = f (1, 7); 45 fns[n] = r; 46 47 var check_value = 10; 48 49 for (var m = 0; m < 100; m++) 50 { 51 var value = r (1); 52 assert (check_value++ === value); 53 } 54 } 55 56 var check_value = 109; 57 for (var n = 0; n < 11; n++) 58 { 59 for (var m = 0; m < 10; m++) 60 { 61 var value = fns [m] (m * n); 62 assert (value == check_value + m * n); 63 } 64 65 check_value++; 66 } 67} 68 69var f = new Function ("a,b", "c", "return a + b + c;"); 70assert (f (1,2,3) === 6); 71 72f = new Function ("a,b", "c,d", "return a + b + c + d;"); 73assert (f (1,2,3,4) === 10); 74 75f = new Function ("a" , "b", "c,d", "return a + b + c + d;"); 76assert (f (1,2,3,4) === 10); 77 78var f = new Function (" a\t , b", "\u0020c", "return a + b + c;"); 79assert (f (1,2,3) === 6); 80 81f = new Function ("a, \n b \u0020", "c \t, d\n", "return a + b + c + d;"); 82assert (f (1,2,3,4) === 10); 83 84f = new Function (" a\t" , "\nb ", " \u0020c , d ", "return a + b + c + d;"); 85assert (f (1,2,3,4) === 10); 86 87try 88{ 89 new Function ({ 90 toString : function () { 91 throw new TypeError(); 92 }, 93 valueOf : function () { 94 throw new TypeError(); 95 } 96 }); 97 98 assert (false); 99} 100catch (e) 101{ 102 assert (e instanceof TypeError); 103} 104 105var p = { toString : function() { throw 1; } }; 106var body = { toString : function() { throw "body"; } }; 107 108try 109{ 110 new Function (p, body); 111 // Should not be reached. 112 assert (false); 113} 114catch (e) 115{ 116 assert (e === 1); 117} 118 119// Check SyntaxError handling 120try 121{ 122 new Function ('var var;'); 123 assert (false); 124} 125catch (e) 126{ 127 assert (e instanceof SyntaxError); 128} 129 130try 131{ 132 new Function ('a;b', 'return;'); 133 assert (false); 134} 135catch (e) 136{ 137 assert (e instanceof SyntaxError); 138} 139