1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jit-test/tests/basic/testCallApply.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +function script1() { return arguments.length; } 1.5 +function script2(x) { return x; } 1.6 +function script3(x) { var o = arguments; return o[0]; } 1.7 +function genClosure() { var x = 3; eval("x = 4"); return function(y) { return x + y } }; 1.8 +var closed1 = genClosure(); 1.9 +var closed2 = genClosure(); 1.10 +var closed3 = genClosure(); 1.11 +var native1 = String.prototype.search; 1.12 +var native2 = String.prototype.match; 1.13 +var tricky1 = { call:function(x,y) { return y }, apply:function(x,y) { return y } }; 1.14 + 1.15 +test0(); 1.16 +test1(); 1.17 +test2(); 1.18 +test3(); 1.19 + 1.20 +function test0() { 1.21 + assertEq(script1.call(null), 0); 1.22 + assertEq(script1.call(null, 1), 1); 1.23 + assertEq(script1.call(null, 1,2), 2); 1.24 + assertEq(native1.call("aabc", /b/), 2); 1.25 + assertEq(native1.call("abc"), 0); 1.26 + assertEq(tricky1.call(null, 9), 9); 1.27 + assertEq(script1.apply(null), 0); 1.28 + assertEq(script1.apply(null, [1]), 1); 1.29 + assertEq(script1.apply(null, [1,2]), 2); 1.30 + assertEq(native1.apply("aabc", [/b/]), 2); 1.31 + assertEq(native1.apply("abc"), 0); 1.32 + assertEq(tricky1.apply(null, 1), 1); 1.33 +} 1.34 +test0(); 1.35 + 1.36 +function test1() { 1.37 + function f(arr) { 1.38 + for (var i = 0; i < 10; ++i) { 1.39 + for (var j = 0; j < arr.length; ++j) { 1.40 + arr[j].call('a'); 1.41 + arr[j].apply('a', []); 1.42 + var arg0 = []; 1.43 + arr[j].apply('a', arg0); 1.44 + (function() { arr[j].apply('a', arguments); })(); 1.45 + 1.46 + arr[j].call('a', 1); 1.47 + arr[j].apply('a', [1]); 1.48 + var arg0 = [1]; 1.49 + arr[j].apply('a', arg0); 1.50 + (function() { arr[j].apply('a', arguments); })(1); 1.51 + 1.52 + arr[j].call('a', 1,'g'); 1.53 + arr[j].apply('a', [1,'g']); 1.54 + var arg0 = [1,'g']; 1.55 + arr[j].apply('a', arg0); 1.56 + (function() { arr[j].apply('a', arguments); })(1,'g'); 1.57 + 1.58 + arr[j].call('a', 1,'g',3,4,5,6,7,8,9); 1.59 + arr[j].apply('a', [1,'g',3,4,5,6,7,8,9]); 1.60 + var arg0 = [1,'g',3,4,5,6,7,8,9]; 1.61 + arr[j].apply('a', arg0); 1.62 + (function() { arr[j].apply('a', arguments); })(1,'g',3,4,5,6,7,8,9); 1.63 + } 1.64 + } 1.65 + } 1.66 + 1.67 + f([script1, script1, script1, script1, script2, script2, script1, script2]); 1.68 + f([script1, script2, script3, script1, script2, script3, script3, script3]); 1.69 + f([script1, script2, script2, script2, script2, script3, script1, script2]); 1.70 + f([script1, script1, script1, native1, native1, native1, native1, script1]); 1.71 + f([native1, native1, native1, native2, native2, native2, native2, native1]); 1.72 + f([native1, native2, native1, native2, native1, native2, native1, native2]); 1.73 + f([native1, native1, native1, script1, script2, script2, native1, script3]); 1.74 + f([closed1, closed1, closed1, closed2, closed2, closed2, script3, script3]); 1.75 + f([closed1, closed2, closed1, closed2, closed1, closed2, closed1, closed2]); 1.76 + f([closed1, closed2, closed3, closed1, closed2, closed3, script1, script2]); 1.77 + f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]); 1.78 + f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]); 1.79 + f([native1, native1, native1, closed1, closed2, script1, script2, native2]); 1.80 +} 1.81 + 1.82 +// test things that break our speculation 1.83 +function test2() { 1.84 + var threw = false; 1.85 + try { 1.86 + (3).call(null, 1,2); 1.87 + } catch (e) { 1.88 + threw = true; 1.89 + } 1.90 + assertEq(threw, true); 1.91 + 1.92 + var threw = false; 1.93 + try { 1.94 + (3).apply(null, [1,2]); 1.95 + } catch (e) { 1.96 + threw = true; 1.97 + } 1.98 + assertEq(threw, true); 1.99 + 1.100 + var threw = false; 1.101 + try { 1.102 + var arr = [1,2]; 1.103 + (3).apply(null, arr); 1.104 + } catch (e) { 1.105 + threw = true; 1.106 + } 1.107 + assertEq(threw, true); 1.108 + 1.109 + function tryAndFail(o) { 1.110 + var threw = false; 1.111 + try { 1.112 + o.call(null, 1,2); 1.113 + } catch(e) { 1.114 + threw = true; 1.115 + } 1.116 + assertEq(threw, true); 1.117 + threw = false; 1.118 + try { 1.119 + o.apply(null, [1,2]); 1.120 + } catch(e) { 1.121 + threw = true; 1.122 + } 1.123 + assertEq(threw, true); 1.124 + } 1.125 + 1.126 + tryAndFail(1); 1.127 + tryAndFail({}); 1.128 + tryAndFail({call:{}, apply:{}}); 1.129 + tryAndFail({call:function() { throw "not js_fun_call"}, apply:function(){ throw "not js_fun_apply" }}); 1.130 +} 1.131 + 1.132 +// hit the stubs::CompileFunction path 1.133 +function test3() { 1.134 + function genFreshFunction(s) { return new Function(s, "return " + s); } 1.135 + 1.136 + function callIt(f) { 1.137 + assertEq(f.call(null, 1,2), 1); 1.138 + } 1.139 + callIt(script2); callIt(script2); callIt(script2); callIt(script2); 1.140 + callIt(genFreshFunction("x")); 1.141 + callIt(genFreshFunction("y")); 1.142 + callIt(genFreshFunction("z")); 1.143 + 1.144 + function applyIt(f) { 1.145 + var arr = [1,2]; 1.146 + assertEq(f.apply(null, arr), 1); 1.147 + } 1.148 + applyIt(script2); applyIt(script2); applyIt(script2); applyIt(script2); 1.149 + applyIt(genFreshFunction("x")); 1.150 + applyIt(genFreshFunction("y")); 1.151 + applyIt(genFreshFunction("z")); 1.152 + 1.153 + function applyIt1(f) { 1.154 + function g() { 1.155 + assertEq(f.apply(null, arguments), 1); 1.156 + } 1.157 + g(1,2); 1.158 + } 1.159 + applyIt1(script2); applyIt1(script2); applyIt1(script2); applyIt1(script2); 1.160 + applyIt1(genFreshFunction("x")); 1.161 + applyIt1(genFreshFunction("y")); 1.162 + applyIt1(genFreshFunction("z")); 1.163 + 1.164 + function applyIt2(f) { 1.165 + assertEq(f.apply(null, [1,2]), 1); 1.166 + } 1.167 + applyIt2(script2); applyIt2(script2); applyIt2(script2); applyIt2(script2); 1.168 + applyIt2(genFreshFunction("x")); 1.169 + applyIt2(genFreshFunction("y")); 1.170 + applyIt2(genFreshFunction("z")); 1.171 +}