js/src/jit-test/tests/asm.js/testFFI.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 load(libdir + "asm.js");
michael@0 2 load(libdir + "asserts.js");
michael@0 3
michael@0 4 function ffi(a,b,c,d) {
michael@0 5 return a+b+c+d;
michael@0 6 }
michael@0 7
michael@0 8 var f = asmLink(asmCompile('global','imp', USE_ASM + 'var ffi=imp.ffi; function g() { return 1 } function f() { var i=0; i=g()|0; return ((ffi(4,5,6,7)|0)+i)|0 } return f'), null, {ffi:ffi});
michael@0 9 assertEq(f(1), 23);
michael@0 10
michael@0 11 var counter = 0;
michael@0 12 function inc() { return counter++ }
michael@0 13 function add1(x) { return x+1 }
michael@0 14 function add2(x,y) { return x+y }
michael@0 15 function add3(x,y,z) { return x+y+z }
michael@0 16 function addN() {
michael@0 17 var sum = 0;
michael@0 18 for (var i = 0; i < arguments.length; i++)
michael@0 19 sum += arguments[i];
michael@0 20 return sum;
michael@0 21 }
michael@0 22 var imp = { inc:inc, add1:add1, add2:add2, add3:add3, addN:addN, identity: x => x };
michael@0 23
michael@0 24 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { incc() } return f');
michael@0 25 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { var i = 0; return (i + inc)|0 } return f');
michael@0 26 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { inc = 0 } return f');
michael@0 27 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return (inc() + 1)|0 } return f');
michael@0 28 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return +((inc()|0) + 1.1) } return f');
michael@0 29 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return +(inc() + 1.1) } return f');
michael@0 30 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return (+inc() + 1)|0 } return f');
michael@0 31 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { var i = 0; inc(i>>>0) } return f');
michael@0 32 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return inc(); return } return f');
michael@0 33 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { inc(inc()) } return f');
michael@0 34 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { g(inc()) } function g() {} return f');
michael@0 35 assertAsmTypeFail('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { inc()|inc() } return f');
michael@0 36
michael@0 37 assertAsmLinkFail(asmCompile('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return inc()|0 } return f'), null, {});
michael@0 38 assertAsmLinkFail(asmCompile('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return inc()|0 } return f'), null, {inc:0});
michael@0 39 assertAsmLinkFail(asmCompile('glob', 'imp', USE_ASM + 'var inc=imp.inc; function f() { return inc()|0 } return f'), null, {inc:{}});
michael@0 40
michael@0 41 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var inc=imp.inc; function g() { inc() } return g'), null, imp)(), undefined);
michael@0 42 assertEq(counter, 1);
michael@0 43
michael@0 44 var f = asmLink(asmCompile('glob', 'imp', USE_ASM + 'var inc=imp.inc; function g() { return inc()|0 } return g'), null, imp);
michael@0 45 assertEq(f(), 1);
michael@0 46 assertEq(counter, 2);
michael@0 47 assertEq(f(), 2);
michael@0 48 assertEq(counter, 3);
michael@0 49
michael@0 50 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var add1=imp.add1; function g(i) { i=i|0; return add1(i|0)|0 } return g'), null, imp)(9), 10);
michael@0 51 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'const add1=imp.add1; function g(i) { i=i|0; return add1(i|0)|0 } return g'), null, imp)(9), 10);
michael@0 52 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var add3=imp.add3; function g() { var i=1,j=3,k=9; return add3(i|0,j|0,k|0)|0 } return g'), null, imp)(), 13);
michael@0 53 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'const add3=imp.add3; function g() { var i=1,j=3,k=9; return add3(i|0,j|0,k|0)|0 } return g'), null, imp)(), 13);
michael@0 54 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var add3=imp.add3; function g() { var i=1.4,j=2.3,k=32.1; return +add3(i,j,k) } return g'), null, imp)(), 1.4+2.3+32.1);
michael@0 55 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'const add3=imp.add3; function g() { var i=1.4,j=2.3,k=32.1; return +add3(i,j,k) } return g'), null, imp)(), 1.4+2.3+32.1);
michael@0 56
michael@0 57 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var add3=imp.add3; function f(i,j,k) { i=i|0;j=+j;k=k|0; return add3(i|0,j,k|0)|0 } return f'), null, imp)(1, 2.5, 3), 6);
michael@0 58 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var addN=imp.addN; function f() { return +addN(1,2,3,4.1,5,6.1,7,8.1,9.1,10,11.1,12,13,14.1,15.1,16.1,17.1,18.1) } return f'), null, imp)(), 1+2+3+4.1+5+6.1+7+8.1+9.1+10+11.1+12+13+14.1+15.1+16.1+17.1+18.1);
michael@0 59
michael@0 60 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var add2=imp.add2; function f(i,j) { i=i|0;j=+j; return +(+(add2(i|0,1)|0) + +add2(j,1) + +add2(+~~i,j)) } return f'), null, imp)(2, 5.5), 3+(5.5+1)+(2+5.5));
michael@0 61 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var addN=imp.addN; function f(i,j) { i=i|0;j=+j; return +(+addN(i|0,j,3,j,i|0) + +addN() + +addN(j,j,j)) } return f'), null, imp)(1, 2.2), (1+2.2+3+2.2+1)+(2.2+2.2+2.2));
michael@0 62
michael@0 63 counter = 0;
michael@0 64 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var addN=imp.addN,inc=imp.inc; function f() { return ((addN(inc()|0,inc(3.3)|0,inc()|0)|0) + (addN(inc(0)|0)|0))|0 } return f'), null, imp)(), 6);
michael@0 65 assertEq(counter, 4);
michael@0 66
michael@0 67 var recurse = function(i,j) { if (i == 0) return j; return f(i-1,j+1)+j }
michael@0 68 imp.recurse = recurse;
michael@0 69 var f = asmLink(asmCompile('glob', 'imp', USE_ASM + 'var r=imp.recurse; function f(i,j) { i=i|0;j=+j; return +r(i|0,j) } return f'), null, imp);
michael@0 70 assertEq(f(0,3.3), 3.3);
michael@0 71 assertEq(f(1,3.3), 3.3+4.3);
michael@0 72 assertEq(f(2,3.3), 3.3+4.3+5.3);
michael@0 73
michael@0 74 function maybeThrow(i, j) {
michael@0 75 if (i == 0)
michael@0 76 throw j;
michael@0 77 try {
michael@0 78 return f(i-1, j);
michael@0 79 } catch(e) {
michael@0 80 assertEq(typeof e, "number");
michael@0 81 return e;
michael@0 82 }
michael@0 83 }
michael@0 84 var f = asmLink(asmCompile('glob', 'imp', USE_ASM + 'var ffi=imp.ffi; function f(i, j) { i=i|0;j=j|0; return ffi(i|0, (j+1)|0)|0 } return f'), null, {ffi:maybeThrow});
michael@0 85 assertThrowsValue(function() { f(0,0) }, 1);
michael@0 86 assertThrowsValue(function() { f(0,Math.pow(2,31)-1) }, -Math.pow(2,31));
michael@0 87 assertEq(f(1,0), 2);
michael@0 88 assertEq(f(2,0), 3);
michael@0 89 assertEq(f(3,0), 4);
michael@0 90 assertEq(f(4,5), 10);
michael@0 91
michael@0 92 var recurse = function(i,j) { if (i == 0) throw j; f(i-1,j) }
michael@0 93 var f = asmLink(asmCompile('glob', 'imp', USE_ASM + 'var ffi=imp.ffi; function g(i,j,k) { i=i|0;j=+j;k=k|0; if (!(k|0)) ffi(i|0,j)|0; else g(i, j+1.0, (k-1)|0) } function f(i,j) { i=i|0;j=+j; g(i,j,4) } return f'), null, {ffi:recurse});
michael@0 94 assertThrowsValue(function() { f(0,2.4) }, 2.4+4);
michael@0 95 assertThrowsValue(function() { f(1,2.4) }, 2.4+8);
michael@0 96 assertThrowsValue(function() { f(8,2.4) }, 2.4+36);
michael@0 97
michael@0 98 assertEq(asmLink(asmCompile('glob', 'imp', USE_ASM + 'var identity=imp.identity; function g(x) { x=+x; return +identity(x) } return g'), null, imp)(13.37), 13.37);

mercurial