1 function script1() { return arguments.length; }
2 function script2(x) { return x; }
3 function script3(x) { var o = arguments; return o[0]; }
4 function genClosure() { var x = 3; eval("x = 4"); return function(y) { return x + y } };
5 var closed1 = genClosure();
6 var closed2 = genClosure();
7 var closed3 = genClosure();
8 var native1 = String.prototype.search;
9 var native2 = String.prototype.match;
10 var tricky1 = { call:function(x,y) { return y }, apply:function(x,y) { return y } };
18 assertEq(script1.call(null), 0);
19 assertEq(script1.call(null, 1), 1);
20 assertEq(script1.call(null, 1,2), 2);
21 assertEq(native1.call("aabc", /b/), 2);
22 assertEq(native1.call("abc"), -1);
23 assertEq(tricky1.call(null, 9), 9);
24 assertEq(script1.apply(null), 0);
25 assertEq(script1.apply(null, [1]), 1);
26 assertEq(script1.apply(null, [1,2]), 2);
27 assertEq(native1.apply("aabc", [/b/]), 2);
28 assertEq(native1.apply("abc"), -1);
29 assertEq(tricky1.apply(null, 1), 1);
35 for (var i = 0; i < 10; ++i) {
36 for (var j = 0; j < arr.length; ++j) {
38 arr[j].apply('a', []);
40 arr[j].apply('a', arg0);
41 (function() { arr[j].apply('a', arguments); })();
44 arr[j].apply('a', [1]);
46 arr[j].apply('a', arg0);
47 (function() { arr[j].apply('a', arguments); })(1);
49 arr[j].call('a', 1,'g');
50 arr[j].apply('a', [1,'g']);
52 arr[j].apply('a', arg0);
53 (function() { arr[j].apply('a', arguments); })(1,'g');
55 arr[j].call('a', 1,'g',3,4,5,6,7,8,9);
56 arr[j].apply('a', [1,'g',3,4,5,6,7,8,9]);
57 var arg0 = [1,'g',3,4,5,6,7,8,9];
58 arr[j].apply('a', arg0);
59 (function() { arr[j].apply('a', arguments); })(1,'g',3,4,5,6,7,8,9);
64 f([script1, script1, script1, script1, script2, script2, script1, script2]);
65 f([script1, script2, script3, script1, script2, script3, script3, script3]);
66 f([script1, script2, script2, script2, script2, script3, script1, script2]);
67 f([script1, script1, script1, native1, native1, native1, native1, script1]);
68 f([native1, native1, native1, native2, native2, native2, native2, native1]);
69 f([native1, native2, native1, native2, native1, native2, native1, native2]);
70 f([native1, native1, native1, script1, script2, script2, native1, script3]);
71 f([closed1, closed1, closed1, closed2, closed2, closed2, script3, script3]);
72 f([closed1, closed2, closed1, closed2, closed1, closed2, closed1, closed2]);
73 f([closed1, closed2, closed3, closed1, closed2, closed3, script1, script2]);
74 f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]);
75 f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]);
76 f([native1, native1, native1, closed1, closed2, script1, script2, native2]);
79 // test things that break our speculation
87 assertEq(threw, true);
91 (3).apply(null, [1,2]);
95 assertEq(threw, true);
100 (3).apply(null, arr);
104 assertEq(threw, true);
106 function tryAndFail(o) {
113 assertEq(threw, true);
116 o.apply(null, [1,2]);
120 assertEq(threw, true);
125 tryAndFail({call:{}, apply:{}});
126 tryAndFail({call:function() { throw "not js_fun_call"}, apply:function(){ throw "not js_fun_apply" }});
129 // hit the stubs::CompileFunction path
131 function genFreshFunction(s) { return new Function(s, "return " + s); }
134 assertEq(f.call(null, 1,2), 1);
136 callIt(script2); callIt(script2); callIt(script2); callIt(script2);
137 callIt(genFreshFunction("x"));
138 callIt(genFreshFunction("y"));
139 callIt(genFreshFunction("z"));
141 function applyIt(f) {
143 assertEq(f.apply(null, arr), 1);
145 applyIt(script2); applyIt(script2); applyIt(script2); applyIt(script2);
146 applyIt(genFreshFunction("x"));
147 applyIt(genFreshFunction("y"));
148 applyIt(genFreshFunction("z"));
150 function applyIt1(f) {
152 assertEq(f.apply(null, arguments), 1);
156 applyIt1(script2); applyIt1(script2); applyIt1(script2); applyIt1(script2);
157 applyIt1(genFreshFunction("x"));
158 applyIt1(genFreshFunction("y"));
159 applyIt1(genFreshFunction("z"));
161 function applyIt2(f) {
162 assertEq(f.apply(null, [1,2]), 1);
164 applyIt2(script2); applyIt2(script2); applyIt2(script2); applyIt2(script2);
165 applyIt2(genFreshFunction("x"));
166 applyIt2(genFreshFunction("y"));
167 applyIt2(genFreshFunction("z"));