Wed, 31 Dec 2014 06:09:35 +0100
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 | |
michael@0 | 3 | assertAsmTypeFail(USE_ASM + "function f(i,j) { i=i|0;j=+j; if (i) return j; return j } return f"); |
michael@0 | 4 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=+j; if (i) return j; return +~~i } return f"))(1,1.4), 1.4); |
michael@0 | 5 | assertAsmTypeFail(USE_ASM + "function f(i,j) { i=i|0;j=j|0; if (i) return j^0; return i^0 } return f"); |
michael@0 | 6 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=j|0; if (i) return j^0; return i|0 } return f"))(1,8), 8); |
michael@0 | 7 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; if ((i|0) == 0) return 10; else if ((i|0) == 1) return 12; else if ((i|0) == 2) return 14; return 0} return f"))(2), 14); |
michael@0 | 8 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; if ((i|0) == 0) return 10; else if ((i|0) == 1) return 12; else if ((i|0) == 2) return 14; else return 16; return 0} return f"))(3), 16); |
michael@0 | 9 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; if ((i|0) == 0) i = 10; else if ((i|0) == 1) return 12; return (i|0) } return f"))(0), 10); |
michael@0 | 10 | |
michael@0 | 11 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; } return f"); |
michael@0 | 12 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; else return 1 } return f"); |
michael@0 | 13 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; return 1.0 } return f"); |
michael@0 | 14 | assertAsmTypeFail(USE_ASM + "function f() { return 0; 1 } return f"); |
michael@0 | 15 | |
michael@0 | 16 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { while (0) {} return 0} return f"))(), 0); |
michael@0 | 17 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { for (;0;) {} return 0} return f"))(), 0); |
michael@0 | 18 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { do {} while(0); return 0} return f"))(), 0); |
michael@0 | 19 | |
michael@0 | 20 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { while (0) ; return 0} return f"))(), 0); |
michael@0 | 21 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { for (;0;) ; return 0} return f"))(), 0); |
michael@0 | 22 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { do ; while(0); return 0} return f"))(), 0); |
michael@0 | 23 | |
michael@0 | 24 | assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; while (d) {}; return 0} return f"); |
michael@0 | 25 | assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; for (;d;) {}; return 0} return f"); |
michael@0 | 26 | assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; do {} while (d); return 0} return f"); |
michael@0 | 27 | |
michael@0 | 28 | assertEq(asmLink(asmCompile(USE_ASM + "function f(j) {j=j|0; var i=0; while ((i|0) < (j|0)) i=(i+4)|0; return i|0} return f"))(6), 8); |
michael@0 | 29 | assertEq(asmLink(asmCompile(USE_ASM + "function f(j) {j=j|0; var i=0; for (;(i|0) < (j|0);) i=(i+4)|0; return i|0} return f"))(6), 8); |
michael@0 | 30 | assertEq(asmLink(asmCompile(USE_ASM + "function f(j) {j=j|0; var i=0; do { i=(i+4)|0; } while ((i|0) < (j|0)); return i|0} return f"))(6), 8); |
michael@0 | 31 | |
michael@0 | 32 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { while(1) return 42; return 0 } return f"))(), 42); |
michael@0 | 33 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { for(;1;) return 42; return 0 } return f"))(), 42); |
michael@0 | 34 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { do return 42; while(1); return 0 } return f"))(), 42); |
michael@0 | 35 | |
michael@0 | 36 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { if (i) return 13; return 42 } return 0 } return f"))(), 42); |
michael@0 | 37 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;1;) { if (i) return 13; return 42 } return 0 } return f"))(), 42); |
michael@0 | 38 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if (i) return 13; return 42 } while(1); return 0 } return f"))(), 42); |
michael@0 | 39 | |
michael@0 | 40 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { break; while(1) {} } return 42 } return f"))(), 42); |
michael@0 | 41 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;;) { break; for(;;) {} } return 42 } return f"))(), 42); |
michael@0 | 42 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { break; do {} while(1) {} } while(1); return 42 } return f"))(), 42); |
michael@0 | 43 | |
michael@0 | 44 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; while(1) { if (i) return 42; return 13 } return 0 } return f"))(), 42); |
michael@0 | 45 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; for(;1;) { if (i) return 42; return 13 } return 0 } return f"))(), 42); |
michael@0 | 46 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; do { if (i) return 42; return 13 } while(1); return 0 } return f"))(), 42); |
michael@0 | 47 | |
michael@0 | 48 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { if (i) return 13; else return 42; return 13 } return 0 } return f"))(), 42); |
michael@0 | 49 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;;) { if (i) return 13; else return 42; return 13 } return 0 } return f"))(), 42); |
michael@0 | 50 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if (i) return 13; else return 42; return 13 } while(1); return 0 } return f"))(), 42); |
michael@0 | 51 | |
michael@0 | 52 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while((i|0) < 3) { if (i) return 42; i=(i+1)|0 } return 0 } return f"))(), 42); |
michael@0 | 53 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;(i|0) < 3;) { if (i) return 42; i=(i+1)|0 } return 0 } return f"))(), 42); |
michael@0 | 54 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if (i) return 42; i=(i+1)|0 } while((i|0) < 3); return 0 } return f"))(), 42); |
michael@0 | 55 | |
michael@0 | 56 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while((i|0) < 3) { if (!i) i=(i+1)|0; return 42 } return 0 } return f"))(), 42); |
michael@0 | 57 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;(i|0) < 3;) { if (!i) i=(i+1)|0; return 42 } return 0 } return f"))(), 42); |
michael@0 | 58 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if (!i) i=(i+1)|0; return 42 } while((i|0) < 3); return 0 } return f"))(), 42); |
michael@0 | 59 | |
michael@0 | 60 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; while(1) {} return 0 } return f"))(), 42); |
michael@0 | 61 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; for(;1;) {} return 0 } return f"))(), 42); |
michael@0 | 62 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; do {} while(1); return 0 } return f"))(), 42); |
michael@0 | 63 | |
michael@0 | 64 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while((i|0) < 10) if ((i|0) == 4) break; else i=(i+1)|0; return i|0 } return f"))(), 4); |
michael@0 | 65 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(; (i|0) < 10;) if ((i|0) == 4) break; else i=(i+1)|0; return i|0 } return f"))(), 4); |
michael@0 | 66 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do if ((i|0) == 4) break; else i=(i+1)|0; while((i|0) < 10); return i|0 } return f"))(), 4); |
michael@0 | 67 | |
michael@0 | 68 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; while ((i=(i+1)|0)<2) { sum=(sum+1)|0; if ((i&1)==0) continue; sum=(sum+100)|0 } return sum|0 } return f"))(), 101); |
michael@0 | 69 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; for (;(i=(i+1)|0)<2;) { sum=(sum+1)|0; if ((i&1)==0) continue; sum=(sum+100)|0 } return sum|0 } return f"))(), 101); |
michael@0 | 70 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; do { sum=(sum+1)|0; if ((i&1)==0) continue; sum=(sum+100)|0 } while((i=(i+1)|0)<2); return sum|0 } return f"))(), 102); |
michael@0 | 71 | |
michael@0 | 72 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; x:a:y:while(1) { i=1; while(1) { i=2; break a; } i=3; } return i|0 } return f"))(), 2); |
michael@0 | 73 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; x:a:y:for(;;) { i=1; while(1) { i=2; break a; } i=3; } return i|0 } return f"))(), 2); |
michael@0 | 74 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; x:a:y:do { i=1; while(1) { i=2; break a; } i=3; } while(1); return i|0 } return f"))(), 2); |
michael@0 | 75 | |
michael@0 | 76 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; a:b:while((i|0) < 5) { i=(i+1)|0; while(1) continue b; } return i|0 } return f"))(), 5); |
michael@0 | 77 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; a:b:for(;(i|0) < 5;) { i=(i+1)|0; while(1) continue b; } return i|0 } return f"))(), 5); |
michael@0 | 78 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; a:b:do { i=(i+1)|0; while(1) continue b; } while((i|0) < 5); return i|0 } return f"))(), 5); |
michael@0 | 79 | |
michael@0 | 80 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; return 0; a:b:while((i|0) < 5) { i=(i+1)|0; while(1) continue b; } return i|0 } return f"))(), 0); |
michael@0 | 81 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; return 0; a:b:for(;(i|0) < 5;) { i=(i+1)|0; while(1) continue b; } return i|0 } return f"))(), 0); |
michael@0 | 82 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; return 0; a:b:do { i=(i+1)|0; while(1) continue b; } while((i|0) < 5); return i|0 } return f"))(), 0); |
michael@0 | 83 | |
michael@0 | 84 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; a:{ break a; i=2; } b:{ c:{ break b; i=3 } i=4 } return i|0 } return f"))(), 42); |
michael@0 | 85 | |
michael@0 | 86 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; a:b:for(;(i|0) < 5;i=(i+1)|0) { while(1) continue b; } return i|0 } return f"))(), 5); |
michael@0 | 87 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42,sum=0; for(i=1;(i|0)<4;i=(i+1)|0) sum=(sum+i)|0; return sum|0 } return f"))(), 6); |
michael@0 | 88 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42,sum=0; for(i=1;(i|0)<8;i=(i+1)|0) { if ((i&1) == 0) continue; sum=(sum+i)|0; } return sum|0 } return f"))(), 16); |
michael@0 | 89 | |
michael@0 | 90 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { i=(i+1)|0; if ((i|0) > 10) break; } return i|0 } return f"))(), 11); |
michael@0 | 91 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;1;i=(i+1)|0) { if ((i|0) > 10) break; } return i|0 } return f"))(), 11); |
michael@0 | 92 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if ((i|0) > 10) break; i=(i+1)|0 } while(1); return i|0 } return f"))(), 11); |
michael@0 | 93 | |
michael@0 | 94 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1){ if ((i|0)>0) break; while (1) { i=i+1|0; if ((i|0)==1) break; } } return i|0; } return f"))(), 1); |
michael@0 | 95 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;;){ if ((i|0)>0) break; while (1) { i=i+1|0; if ((i|0)==1) break; } } return i|0; } return f"))(), 1); |
michael@0 | 96 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do{ if ((i|0)>0) break; while (1) { i=i+1|0; if ((i|0)==1) break; } }while(1); return i|0; } return f"))(), 1); |
michael@0 | 97 | |
michael@0 | 98 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; while(1){ if ((i|0)>5) break; while (1) { i=i+1|0; sum=(sum+i)|0; if ((i|0)>3) break; } } return sum|0; } return f"))(), 21); |
michael@0 | 99 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; for(;;){ if ((i|0)>5) break; while (1) { i=i+1|0; sum=(sum+i)|0; if ((i|0)>3) break; } } return sum|0; } return f"))(), 21); |
michael@0 | 100 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0,sum=0; do{ if ((i|0)>5) break; while (1) { i=i+1|0; sum=(sum+i)|0; if ((i|0)>3) break; } }while(1); return sum|0; } return f"))(), 21); |
michael@0 | 101 | |
michael@0 | 102 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; while(1) { if (i) { break; } else { return i|0 } i = 1 } return i|0 } return f"))(3), 3); |
michael@0 | 103 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; for(;1;) { if (i) { break; } else { return i|0 } i = 1 } return i|0 } return f"))(3), 3); |
michael@0 | 104 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; do { if (i) { break; } else { return i|0 } i = 1 } while (0); return i|0 } return f"))(3), 3); |
michael@0 | 105 | |
michael@0 | 106 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; while(1) { if (i) { return i|0 } else { return i|0 } i = 1 } return i|0 } return f"))(3), 3); |
michael@0 | 107 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; for(;;) { if (i) { return i|0 } else { return i|0 } i = 1 } return i|0 } return f"))(3), 3); |
michael@0 | 108 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; do { if (i) { return i|0 } else { return i|0 } i = 1 } while (0); return i|0 } return f"))(3), 3); |
michael@0 | 109 | |
michael@0 | 110 | assertEq(asmLink(asmCompile(USE_ASM + "function f() {var j=1,i=0; while(j){ if(0) continue; j=i } return j|0 } return f"))(), 0); |
michael@0 | 111 | assertEq(asmLink(asmCompile(USE_ASM + "function f() {var j=1,i=0; for(;j;){ if(0) continue; j=i } return j|0 } return f"))(), 0); |
michael@0 | 112 | assertEq(asmLink(asmCompile(USE_ASM + "function f() {var j=1,i=0; do{ if(0) continue; j=i } while(j) return j|0 } return f"))(), 0); |
michael@0 | 113 | |
michael@0 | 114 | assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; for(;;) { return i|0 } return 0 } return f"))(42), 42); |
michael@0 | 115 | assertEq(asmLink(asmCompile(USE_ASM + "function f(n) { n=n|0; var i=0,s=0; for(;;i=(i+1)|0) { if (~~i==~~n) return s|0; s=(s+i)|0 } return 0 } return f"))(8), 28); |
michael@0 | 116 | |
michael@0 | 117 | var f = asmLink(asmCompile(USE_ASM + "function f(n,m) { n=n|0;m=m|0; var i=0,sum=0; while((n|0)>(m|0) ? ((i|0)<(n|0))|0 : ((i|0)<(m|0))|0) { sum = (sum+i)|0; i=(i+1)|0 } return sum|0 } return f")); |
michael@0 | 118 | assertEq(f(1,5), 10); |
michael@0 | 119 | assertEq(f(6,5), 15); |
michael@0 | 120 | |
michael@0 | 121 | var f = asmLink(asmCompile(USE_ASM + "function f(n,m) { n=n|0;m=m|0; var i=0,sum=0; for(; (n|0)>(m|0) ? ((i|0)<(n|0))|0 : ((i|0)<(m|0))|0; i=(i+1)|0) { sum = (sum+i)|0 } return sum|0 } return f")); |
michael@0 | 122 | assertEq(f(1,5), 10); |
michael@0 | 123 | assertEq(f(6,5), 15); |
michael@0 | 124 | |
michael@0 | 125 | var f = asmLink(asmCompile(USE_ASM + "function f(n,m) { n=n|0;m=m|0; var i=0,sum=0; do { sum = (sum+i)|0; i=(i+1)|0 } while((n|0)>(m|0) ? ((i|0)<(n|0))|0 : ((i|0)<(m|0))|0); return sum|0 } return f")); |
michael@0 | 126 | assertEq(f(1,5), 10); |
michael@0 | 127 | assertEq(f(6,5), 15); |
michael@0 | 128 | |
michael@0 | 129 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case 1: return 0; case 1: return 0 } return 0} return f"); |
michael@0 | 130 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case 1: return 0; case 2: return 0; case 1: return 0 } return 0} return f"); |
michael@0 | 131 | assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; switch(1) { case 1: return 0; case 1: return 0 } return 0} return f"); |
michael@0 | 132 | assertAsmTypeFail(USE_ASM + "function f() { var i=0; switch(i) {}; return i|0 } return f"); |
michael@0 | 133 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) {}; return i|0 } return f"))(), 0); |
michael@0 | 134 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { default: i=42 } return i|0 } return f"))(), 42); |
michael@0 | 135 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { default: i=42; break } return i|0 } return f"))(), 42); |
michael@0 | 136 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: i=42 } return i|0 } return f"))(), 42); |
michael@0 | 137 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: i=42; break } return i|0 } return f"))(), 42); |
michael@0 | 138 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: default: i=42 } return i|0 } return f"))(), 42); |
michael@0 | 139 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: default: i=42; break } return i|0 } return f"))(), 42); |
michael@0 | 140 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; switch(i|0) { case 0: case 2: break; default: i=42 } return i|0 } return f"))(), 42); |
michael@0 | 141 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; switch(i|0) { case 0: case 2: break; default: i=42; break } return i|0 } return f"))(), 42); |
michael@0 | 142 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { return 42; switch(1) { case 1: return 13 } return 14 } return f"))(), 42); |
michael@0 | 143 | |
michael@0 | 144 | var exp = asmLink(asmCompile(USE_ASM + "var x=0; function a() { return x|0 } function b(i) { i=i|0; x=i } function c(i) { i=i|0; if (i) b(i); } return {a:a,b:b,c:c}")); |
michael@0 | 145 | assertEq(exp.c(10), undefined); |
michael@0 | 146 | assertEq(exp.a(), 10); |
michael@0 | 147 | |
michael@0 | 148 | var f = asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case 1: i=-1; break; case 133742: i=2; break; default: i=42; break } return i|0 } return f")); |
michael@0 | 149 | assertEq(f(1), -1); |
michael@0 | 150 | assertEq(f(2), 42); |
michael@0 | 151 | assertEq(f(133742), 2); |
michael@0 | 152 | assertEq(f(133743), 42); |
michael@0 | 153 | |
michael@0 | 154 | var f = asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case 1: i=42; break; default: i=13 } return i|0 } return f")); |
michael@0 | 155 | assertEq(f(-1), 13); |
michael@0 | 156 | assertEq(f(0), 13); |
michael@0 | 157 | assertEq(f(1), 42); |
michael@0 | 158 | |
michael@0 | 159 | var f = asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case -1: i=42; break; default: i=13 } return i|0 } return f")); |
michael@0 | 160 | assertEq(f(-1), 42); |
michael@0 | 161 | assertEq(f(0), 13); |
michael@0 | 162 | assertEq(f(1), 13); |
michael@0 | 163 | assertEq(f(0xffffffff), 42); |
michael@0 | 164 | |
michael@0 | 165 | var f = asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; var sum=0; switch(i|0) { case -1: sum=(sum+1)|0; case 1: sum=(sum+1)|0; case 3: sum=(sum+1)|0; default: sum=(sum+100)|0; } return sum|0 } return f")); |
michael@0 | 166 | assertEq(f(-1), 103); |
michael@0 | 167 | assertEq(f(0), 100); |
michael@0 | 168 | assertEq(f(1), 102); |
michael@0 | 169 | assertEq(f(2), 100); |
michael@0 | 170 | assertEq(f(3), 101); |
michael@0 | 171 | |
michael@0 | 172 | var f = asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; var sum=0; switch(i|0) { case -1: sum=10; break; case 1: sum=11; break; case 3: sum=12; break; default: sum=13; } return sum|0 } return f")); |
michael@0 | 173 | assertEq(f(-1), 10); |
michael@0 | 174 | assertEq(f(0), 13); |
michael@0 | 175 | assertEq(f(1), 11); |
michael@0 | 176 | assertEq(f(2), 13); |
michael@0 | 177 | assertEq(f(3), 12); |
michael@0 | 178 | |
michael@0 | 179 | assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=8,sum=0; a:for(; (i|0)<20; i=(i+1)|0) { switch(i&3) { case 0:case 1:sum=(sum+i)|0;break;case 2:sum=(sum+100)|0;continue;default:break a} sum=(sum+10)|0; } sum=(sum+1000)|0; return sum|0 } return f"))(), 1137); |
michael@0 | 180 | assertEq(asmLink(asmCompile('g', USE_ASM + "function f() { g:{ return 42 } return 13 } return f"), null)(), 42); |
michael@0 | 181 | |
michael@0 | 182 | var imp = { ffi:function() { throw "Wrong" } }; |
michael@0 | 183 | assertEq(asmLink(asmCompile('glob','imp', USE_ASM + "var ffi=imp.ffi; function f() { var i=0; return (i+1)|0; return ffi(i|0)|0 } return f"), null, imp)(), 1); |
michael@0 | 184 | |
michael@0 | 185 | // Ternaries conditionals |
michael@0 | 186 | // |
michael@0 | 187 | // Basic ternaries |
michael@0 | 188 | var f = asmLink(asmCompile(USE_ASM + "function f() { return 0; if (1) return -1; return -2} return f")); |
michael@0 | 189 | assertEq(f(5), 0); |
michael@0 | 190 | |
michael@0 | 191 | var f = asmLink(asmCompile(USE_ASM + "function f(x) { x=x|0; var a=2;if(x?1:0)a=1;else a=0; return a|0 } return f")); |
michael@0 | 192 | assertEq(f(1), 1); |
michael@0 | 193 | assertEq(f(0), 0); |
michael@0 | 194 | |
michael@0 | 195 | var guard = (function() { |
michael@0 | 196 | var called_ = false; |
michael@0 | 197 | return { |
michael@0 | 198 | called: function(){ return called_ }, |
michael@0 | 199 | call: function(){ called_ = true } |
michael@0 | 200 | } |
michael@0 | 201 | })(); |
michael@0 | 202 | |
michael@0 | 203 | var f = asmLink(asmCompile('glob', 'ffi', USE_ASM + "var func=ffi.func; function f(x) { x=x|0; var a=2;if(x?1:1)a=1; else {func();a=0}return a|0 } return f"), this, {func: guard.call}); |
michael@0 | 204 | assertEq(f(1), 1); |
michael@0 | 205 | assertEq(f(0), 1); |
michael@0 | 206 | assertEq(guard.called(), false); |
michael@0 | 207 | |
michael@0 | 208 | var f = asmLink(asmCompile('glob', 'ffi', USE_ASM + "var func=ffi.func; function f(x) { x=x|0; var a=2;if(x?0:0){a=1; func()}else a=0;return a|0 } return f"), this, {func: guard.call}); |
michael@0 | 209 | assertEq(f(1), 0); |
michael@0 | 210 | assertEq(f(0), 0); |
michael@0 | 211 | assertEq(guard.called(), false); |
michael@0 | 212 | |
michael@0 | 213 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var a=2;if(x?0:y)a=1;else a=0; return a|0 } return f")); |
michael@0 | 214 | assertEq(f(1,1), 0); |
michael@0 | 215 | assertEq(f(1,0), 0); |
michael@0 | 216 | assertEq(f(0,0), 0); |
michael@0 | 217 | assertEq(f(0,1), 1); |
michael@0 | 218 | |
michael@0 | 219 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var a=2;if(x?y:1)a=1;else a=0; return a|0 } return f")); |
michael@0 | 220 | assertEq(f(1,1), 1); |
michael@0 | 221 | assertEq(f(1,0), 0); |
michael@0 | 222 | assertEq(f(0,0), 1); |
michael@0 | 223 | assertEq(f(0,1), 1); |
michael@0 | 224 | |
michael@0 | 225 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2;if(x?y:z)a=1;else a=0; return a|0 } return f")); |
michael@0 | 226 | for (var i = 0; i < 2; ++i) |
michael@0 | 227 | for (var j = 0; j < 2; ++j) |
michael@0 | 228 | for (var k = 0; k < 2; ++k) |
michael@0 | 229 | assertEq(f(i,j,k), ((i && j) || (!i && k))|0); |
michael@0 | 230 | |
michael@0 | 231 | // Complex ternaries |
michael@0 | 232 | function CheckTwoArgsTwoOptions(f) { |
michael@0 | 233 | function check(x,y) { |
michael@0 | 234 | return (x > 2 && y < 5) | 0; |
michael@0 | 235 | } |
michael@0 | 236 | for (var a = -10; a < 10; a++) |
michael@0 | 237 | for (var b = -10; b < 10; b++) |
michael@0 | 238 | assertEq(f(a,b), check(a,b)); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | function CheckThreeArgsTwoOptions(f) { |
michael@0 | 242 | function check(x,y,z) { |
michael@0 | 243 | return (x > 2 && y < 5 && z > -1) | 0; |
michael@0 | 244 | } |
michael@0 | 245 | for (var a = -10; a < 10; a++) |
michael@0 | 246 | for (var b = -10; b < 10; b++) |
michael@0 | 247 | for (var c = -10; c < 10; c++) |
michael@0 | 248 | assertEq(f(a,b,c), check(a,b,c)); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | // Ternaries with && semantics |
michael@0 | 252 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var z=0; if((x|0) > 2 ? (y|0) < 5 : 0) z=1; return z|0;} return f")); |
michael@0 | 253 | CheckTwoArgsTwoOptions(f); |
michael@0 | 254 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var z=2; if((x|0) > 2 ? (y|0) < 5 : 0) z=1; else z=0; return z|0;} return f")); |
michael@0 | 255 | CheckTwoArgsTwoOptions(f); |
michael@0 | 256 | |
michael@0 | 257 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=0; if((x|0) > 2 ? ((y|0) < 5 ? (z|0) > -1 : 0) : 0) a=1; return a|0;} return f")); |
michael@0 | 258 | CheckThreeArgsTwoOptions(f); |
michael@0 | 259 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if((x|0) > 2 ? ((y|0) < 5 ? (z|0) > -1 : 0) : 0) a=1; else a=0; return a|0;} return f")); |
michael@0 | 260 | CheckThreeArgsTwoOptions(f); |
michael@0 | 261 | |
michael@0 | 262 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=0; if((x|0) > 2 ? (y|0) < 5 : 0) {if ((z|0) > -1) a=1}; return a|0;} return f")); |
michael@0 | 263 | CheckThreeArgsTwoOptions(f); |
michael@0 | 264 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if((x|0) > 2 ? (y|0) < 5 : 0) {if ((z|0) > -1) a=1; else a=0;} else a=0; return a|0;} return f")); |
michael@0 | 265 | CheckThreeArgsTwoOptions(f); |
michael@0 | 266 | |
michael@0 | 267 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=0; if(((x|0) == 3 ? 1 : ((x|0) > 3)) ? ((y|0) < 5 ? (z|0) > -1 : 0) : 0) a=1; return a|0;} return f")); |
michael@0 | 268 | CheckThreeArgsTwoOptions(f); |
michael@0 | 269 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if(((x|0) == 3 ? 1 : ((x|0) > 3)) ? ((y|0) < 5 ? (z|0) > -1 : 0) : 0) a=1; else a=0; return a|0;} return f")); |
michael@0 | 270 | CheckThreeArgsTwoOptions(f); |
michael@0 | 271 | |
michael@0 | 272 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=0; if((x|0) == 3 ? 1 : (x|0) > 3) {if ((y|0) < 5 ? (z|0) > -1 : 0) a=1;} return a|0;} return f")); |
michael@0 | 273 | CheckThreeArgsTwoOptions(f); |
michael@0 | 274 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if((x|0) == 3 ? 1 : (x|0) > 3) {if ((y|0) < 5 ? (z|0) > -1 : 0) a=1; else a=0;} else a=0; return a|0;} return f")); |
michael@0 | 275 | CheckThreeArgsTwoOptions(f); |
michael@0 | 276 | |
michael@0 | 277 | // Ternaries with || semantics |
michael@0 | 278 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var z=1; if((x|0) <= 2 ? 1 : (y|0) >= 5) z=0; return z|0;} return f")); |
michael@0 | 279 | CheckTwoArgsTwoOptions(f); |
michael@0 | 280 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y) { x=x|0;y=y|0; var z=2; if((x|0) <= 2 ? 1 : (y|0) >= 5) z=0; else z=1; return z|0;} return f")); |
michael@0 | 281 | CheckTwoArgsTwoOptions(f); |
michael@0 | 282 | |
michael@0 | 283 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=1; if((x|0) <= 2 ? 1 : ((y|0) >= 5 ? 1 : (z|0) <= -1)) a=0; return a|0;} return f")); |
michael@0 | 284 | CheckThreeArgsTwoOptions(f); |
michael@0 | 285 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if((x|0) <= 2 ? 1 : ((y|0) >= 5 ? 1 : (z|0) <= -1)) a=0; else a=1; return a|0;} return f")); |
michael@0 | 286 | CheckThreeArgsTwoOptions(f); |
michael@0 | 287 | |
michael@0 | 288 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=1; if((x|0) <= 2 ? 1 : (y|0) >= 5) a=0; else if ((z|0) <= -1) a=0; return a|0;} return f")); |
michael@0 | 289 | CheckThreeArgsTwoOptions(f); |
michael@0 | 290 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if((x|0) <= 2 ? 1 : (y|0) >= 5) a=0; else if ((z|0) <= -1) a=0; else a=1; return a|0;} return f")); |
michael@0 | 291 | CheckThreeArgsTwoOptions(f); |
michael@0 | 292 | |
michael@0 | 293 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=1; if(((x|0) != 3 ? ((x|0) <= 3) : 0) ? 1 : ((y|0) >= 5 ? 1 : (z|0) <= -1)) a=0; return a|0;} return f")); |
michael@0 | 294 | CheckThreeArgsTwoOptions(f); |
michael@0 | 295 | var f = asmLink(asmCompile(USE_ASM + "function f(x,y,z) { x=x|0;y=y|0;z=z|0; var a=2; if(((x|0) != 3 ? ((x|0) <= 3) : 0) ? 1 : ((y|0) >= 5 ? 1 : (z|0) <= -1)) a=0; else a=1; return a|0;} return f")); |
michael@0 | 296 | CheckThreeArgsTwoOptions(f); |
michael@0 | 297 | |
michael@0 | 298 | // Massive test |
michael@0 | 299 | var code = '"use asm";\ |
michael@0 | 300 | function g(x,y) {\ |
michael@0 | 301 | x=x|0;\ |
michael@0 | 302 | y=y|0;\ |
michael@0 | 303 | var z = 0;\ |
michael@0 | 304 | if ((y|0) == 1337) {\ |
michael@0 | 305 | z = 1;\ |
michael@0 | 306 | } else if ((x|0) == 1 ? 1 : ((x|0) < 0 ? (y|0) == 1 : 0)) {\ |
michael@0 | 307 | z = 2;\ |
michael@0 | 308 | } else if ((x|0) == 2) {\ |
michael@0 | 309 | z = 3;\ |
michael@0 | 310 | } else if ((x|0) == 3 ? 1 : (x|0) == 4) {\ |
michael@0 | 311 | z = 4;\ |
michael@0 | 312 | } else if ((x|0) == 5 ? (y|0) > 5 : 0) {\ |
michael@0 | 313 | z = 5;\ |
michael@0 | 314 | } else {\ |
michael@0 | 315 | z = 6;\ |
michael@0 | 316 | }\ |
michael@0 | 317 | return z|0;\ |
michael@0 | 318 | }\ |
michael@0 | 319 | return g;'; |
michael@0 | 320 | |
michael@0 | 321 | var m = asmLink(asmCompile(code)); |
michael@0 | 322 | |
michael@0 | 323 | assertEq(m(0, 1337), 1); |
michael@0 | 324 | assertEq(m(0, 1338), 6); |
michael@0 | 325 | assertEq(m(0, 0), 6); |
michael@0 | 326 | assertEq(m(0, 1), 6); |
michael@0 | 327 | assertEq(m(0, 1336), 6); |
michael@0 | 328 | assertEq(m(1, 1337), 1); |
michael@0 | 329 | assertEq(m(2, 1337), 1); |
michael@0 | 330 | assertEq(m(3, 1337), 1); |
michael@0 | 331 | assertEq(m(4, 1337), 1); |
michael@0 | 332 | assertEq(m(5, 1337), 1); |
michael@0 | 333 | |
michael@0 | 334 | assertEq(m(1, 10), 2); |
michael@0 | 335 | assertEq(m(1, 1336), 2); |
michael@0 | 336 | assertEq(m(-1, 10), 6); |
michael@0 | 337 | assertEq(m(-1, 2), 6); |
michael@0 | 338 | assertEq(m(-1, -1), 6); |
michael@0 | 339 | assertEq(m(-1, 1), 2); |
michael@0 | 340 | assertEq(m(-9, 1), 2); |
michael@0 | 341 | |
michael@0 | 342 | assertEq(m(2, 1), 3); |
michael@0 | 343 | assertEq(m(2, 0), 3); |
michael@0 | 344 | assertEq(m(2, 6), 3); |
michael@0 | 345 | |
michael@0 | 346 | assertEq(m(3, 1), 4); |
michael@0 | 347 | assertEq(m(3, 0), 4); |
michael@0 | 348 | assertEq(m(3, 6), 4); |
michael@0 | 349 | assertEq(m(3, 3), 4); |
michael@0 | 350 | assertEq(m(4, 1), 4); |
michael@0 | 351 | assertEq(m(4, 0), 4); |
michael@0 | 352 | assertEq(m(4, 6), 4); |
michael@0 | 353 | assertEq(m(4, 3), 4); |
michael@0 | 354 | |
michael@0 | 355 | assertEq(m(5, -1), 6); |
michael@0 | 356 | assertEq(m(5, 4), 6); |
michael@0 | 357 | assertEq(m(5, 5), 6); |
michael@0 | 358 | assertEq(m(5, 6), 5); |
michael@0 | 359 | assertEq(m(5, 10), 5); |