js/src/jit-test/tests/basic/testCallApply.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 function script1() { return arguments.length; }
michael@0 2 function script2(x) { return x; }
michael@0 3 function script3(x) { var o = arguments; return o[0]; }
michael@0 4 function genClosure() { var x = 3; eval("x = 4"); return function(y) { return x + y } };
michael@0 5 var closed1 = genClosure();
michael@0 6 var closed2 = genClosure();
michael@0 7 var closed3 = genClosure();
michael@0 8 var native1 = String.prototype.search;
michael@0 9 var native2 = String.prototype.match;
michael@0 10 var tricky1 = { call:function(x,y) { return y }, apply:function(x,y) { return y } };
michael@0 11
michael@0 12 test0();
michael@0 13 test1();
michael@0 14 test2();
michael@0 15 test3();
michael@0 16
michael@0 17 function test0() {
michael@0 18 assertEq(script1.call(null), 0);
michael@0 19 assertEq(script1.call(null, 1), 1);
michael@0 20 assertEq(script1.call(null, 1,2), 2);
michael@0 21 assertEq(native1.call("aabc", /b/), 2);
michael@0 22 assertEq(native1.call("abc"), 0);
michael@0 23 assertEq(tricky1.call(null, 9), 9);
michael@0 24 assertEq(script1.apply(null), 0);
michael@0 25 assertEq(script1.apply(null, [1]), 1);
michael@0 26 assertEq(script1.apply(null, [1,2]), 2);
michael@0 27 assertEq(native1.apply("aabc", [/b/]), 2);
michael@0 28 assertEq(native1.apply("abc"), 0);
michael@0 29 assertEq(tricky1.apply(null, 1), 1);
michael@0 30 }
michael@0 31 test0();
michael@0 32
michael@0 33 function test1() {
michael@0 34 function f(arr) {
michael@0 35 for (var i = 0; i < 10; ++i) {
michael@0 36 for (var j = 0; j < arr.length; ++j) {
michael@0 37 arr[j].call('a');
michael@0 38 arr[j].apply('a', []);
michael@0 39 var arg0 = [];
michael@0 40 arr[j].apply('a', arg0);
michael@0 41 (function() { arr[j].apply('a', arguments); })();
michael@0 42
michael@0 43 arr[j].call('a', 1);
michael@0 44 arr[j].apply('a', [1]);
michael@0 45 var arg0 = [1];
michael@0 46 arr[j].apply('a', arg0);
michael@0 47 (function() { arr[j].apply('a', arguments); })(1);
michael@0 48
michael@0 49 arr[j].call('a', 1,'g');
michael@0 50 arr[j].apply('a', [1,'g']);
michael@0 51 var arg0 = [1,'g'];
michael@0 52 arr[j].apply('a', arg0);
michael@0 53 (function() { arr[j].apply('a', arguments); })(1,'g');
michael@0 54
michael@0 55 arr[j].call('a', 1,'g',3,4,5,6,7,8,9);
michael@0 56 arr[j].apply('a', [1,'g',3,4,5,6,7,8,9]);
michael@0 57 var arg0 = [1,'g',3,4,5,6,7,8,9];
michael@0 58 arr[j].apply('a', arg0);
michael@0 59 (function() { arr[j].apply('a', arguments); })(1,'g',3,4,5,6,7,8,9);
michael@0 60 }
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 f([script1, script1, script1, script1, script2, script2, script1, script2]);
michael@0 65 f([script1, script2, script3, script1, script2, script3, script3, script3]);
michael@0 66 f([script1, script2, script2, script2, script2, script3, script1, script2]);
michael@0 67 f([script1, script1, script1, native1, native1, native1, native1, script1]);
michael@0 68 f([native1, native1, native1, native2, native2, native2, native2, native1]);
michael@0 69 f([native1, native2, native1, native2, native1, native2, native1, native2]);
michael@0 70 f([native1, native1, native1, script1, script2, script2, native1, script3]);
michael@0 71 f([closed1, closed1, closed1, closed2, closed2, closed2, script3, script3]);
michael@0 72 f([closed1, closed2, closed1, closed2, closed1, closed2, closed1, closed2]);
michael@0 73 f([closed1, closed2, closed3, closed1, closed2, closed3, script1, script2]);
michael@0 74 f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]);
michael@0 75 f([closed1, closed1, closed1, closed2, closed2, closed2, native1, native2]);
michael@0 76 f([native1, native1, native1, closed1, closed2, script1, script2, native2]);
michael@0 77 }
michael@0 78
michael@0 79 // test things that break our speculation
michael@0 80 function test2() {
michael@0 81 var threw = false;
michael@0 82 try {
michael@0 83 (3).call(null, 1,2);
michael@0 84 } catch (e) {
michael@0 85 threw = true;
michael@0 86 }
michael@0 87 assertEq(threw, true);
michael@0 88
michael@0 89 var threw = false;
michael@0 90 try {
michael@0 91 (3).apply(null, [1,2]);
michael@0 92 } catch (e) {
michael@0 93 threw = true;
michael@0 94 }
michael@0 95 assertEq(threw, true);
michael@0 96
michael@0 97 var threw = false;
michael@0 98 try {
michael@0 99 var arr = [1,2];
michael@0 100 (3).apply(null, arr);
michael@0 101 } catch (e) {
michael@0 102 threw = true;
michael@0 103 }
michael@0 104 assertEq(threw, true);
michael@0 105
michael@0 106 function tryAndFail(o) {
michael@0 107 var threw = false;
michael@0 108 try {
michael@0 109 o.call(null, 1,2);
michael@0 110 } catch(e) {
michael@0 111 threw = true;
michael@0 112 }
michael@0 113 assertEq(threw, true);
michael@0 114 threw = false;
michael@0 115 try {
michael@0 116 o.apply(null, [1,2]);
michael@0 117 } catch(e) {
michael@0 118 threw = true;
michael@0 119 }
michael@0 120 assertEq(threw, true);
michael@0 121 }
michael@0 122
michael@0 123 tryAndFail(1);
michael@0 124 tryAndFail({});
michael@0 125 tryAndFail({call:{}, apply:{}});
michael@0 126 tryAndFail({call:function() { throw "not js_fun_call"}, apply:function(){ throw "not js_fun_apply" }});
michael@0 127 }
michael@0 128
michael@0 129 // hit the stubs::CompileFunction path
michael@0 130 function test3() {
michael@0 131 function genFreshFunction(s) { return new Function(s, "return " + s); }
michael@0 132
michael@0 133 function callIt(f) {
michael@0 134 assertEq(f.call(null, 1,2), 1);
michael@0 135 }
michael@0 136 callIt(script2); callIt(script2); callIt(script2); callIt(script2);
michael@0 137 callIt(genFreshFunction("x"));
michael@0 138 callIt(genFreshFunction("y"));
michael@0 139 callIt(genFreshFunction("z"));
michael@0 140
michael@0 141 function applyIt(f) {
michael@0 142 var arr = [1,2];
michael@0 143 assertEq(f.apply(null, arr), 1);
michael@0 144 }
michael@0 145 applyIt(script2); applyIt(script2); applyIt(script2); applyIt(script2);
michael@0 146 applyIt(genFreshFunction("x"));
michael@0 147 applyIt(genFreshFunction("y"));
michael@0 148 applyIt(genFreshFunction("z"));
michael@0 149
michael@0 150 function applyIt1(f) {
michael@0 151 function g() {
michael@0 152 assertEq(f.apply(null, arguments), 1);
michael@0 153 }
michael@0 154 g(1,2);
michael@0 155 }
michael@0 156 applyIt1(script2); applyIt1(script2); applyIt1(script2); applyIt1(script2);
michael@0 157 applyIt1(genFreshFunction("x"));
michael@0 158 applyIt1(genFreshFunction("y"));
michael@0 159 applyIt1(genFreshFunction("z"));
michael@0 160
michael@0 161 function applyIt2(f) {
michael@0 162 assertEq(f.apply(null, [1,2]), 1);
michael@0 163 }
michael@0 164 applyIt2(script2); applyIt2(script2); applyIt2(script2); applyIt2(script2);
michael@0 165 applyIt2(genFreshFunction("x"));
michael@0 166 applyIt2(genFreshFunction("y"));
michael@0 167 applyIt2(genFreshFunction("z"));
michael@0 168 }

mercurial