Best JavaScript code snippet using playwright-internal
testControlFlow.js
Source:testControlFlow.js
1// |jit-test| test-also-noasmjs2load(libdir + "asm.js");3assertEq(asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=+j; if (i) return j; return j+1.0 } return f"))(0, 1.2), 1.2+1.0);4assertEq(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);5assertEq(asmLink(asmCompile(USE_ASM + "function f(i,j) { i=i|0;j=j|0; if (i) return j^0; return i^1 } return f"))(1, 1), 1);6assertEq(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);7assertEq(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);8assertEq(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);9assertEq(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);10assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; } return f");11assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; else return 1 } return f");12assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; if (i) return 0; return 1.0 } return f");13assertAsmTypeFail(USE_ASM + "function f() { if (0) return 0; 1 } return f");14assertEq(asmLink(asmCompile(USE_ASM + "function f() { while (0) {} return 0} return f"))(), 0);15assertEq(asmLink(asmCompile(USE_ASM + "function f() { for (;0;) {} return 0} return f"))(), 0);16assertEq(asmLink(asmCompile(USE_ASM + "function f() { do {} while(0); return 0} return f"))(), 0);17assertEq(asmLink(asmCompile(USE_ASM + "function f() { while (0) ; return 0} return f"))(), 0);18assertEq(asmLink(asmCompile(USE_ASM + "function f() { for (;0;) ; return 0} return f"))(), 0);19assertEq(asmLink(asmCompile(USE_ASM + "function f() { do ; while(0); return 0} return f"))(), 0);20assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; while (d) {}; return 0} return f");21assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; for (;d;) {}; return 0} return f");22assertAsmTypeFail(USE_ASM + "function f(d) {d=+d; do {} while (d); return 0} return f");23assertEq(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);24assertEq(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);25assertEq(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);26assertEq(asmLink(asmCompile(USE_ASM + "function f() { while(1) return 42; return 0 } return f"))(), 42);27assertEq(asmLink(asmCompile(USE_ASM + "function f() { for(;1;) return 42; return 0 } return f"))(), 42);28assertEq(asmLink(asmCompile(USE_ASM + "function f() { do return 42; while(1); return 0 } return f"))(), 42);29assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { if (i) return 13; return 42 } return 0 } return f"))(), 42);30assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;1;) { if (i) return 13; return 42 } return 0 } return f"))(), 42);31assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { if (i) return 13; return 42 } while(1); return 0 } return f"))(), 42);32assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; while(1) { break; while(1) {} } return 42 } return f"))(), 42);33assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;;) { break; for(;;) {} } return 42 } return f"))(), 42);34assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; do { break; do {} while(1) {} } while(1); return 42 } return f"))(), 42);35assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; while(1) { if (i) return 42; return 13 } return 0 } return f"))(), 42);36assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; for(;1;) { if (i) return 42; return 13 } return 0 } return f"))(), 42);37assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=1; do { if (i) return 42; return 13 } while(1); return 0 } return f"))(), 42);38assertEq(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);39assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; for(;;) { if (i) return 13; else return 42; return 13 } return 0 } return f"))(), 42);40assertEq(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);41assertEq(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);42assertEq(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);43assertEq(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);44assertEq(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);45assertEq(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);46assertEq(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);47assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; while(1) {} return 0 } return f"))(), 42);48assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; for(;1;) {} return 0 } return f"))(), 42);49assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=42; return i|0; do {} while(1); return 0 } return f"))(), 42);50assertEq(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);51assertEq(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);52assertEq(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);53assertEq(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);54assertEq(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);55assertEq(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);56assertEq(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);57assertEq(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);58assertEq(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);59assertEq(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);60assertEq(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);61assertEq(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);62assertEq(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);63assertEq(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);64assertEq(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);65assertEq(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);66assertEq(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);67assertEq(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);68assertEq(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);69assertEq(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);70assertEq(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);71assertEq(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);72assertEq(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);73assertEq(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);74assertEq(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);75assertEq(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);76assertEq(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);77assertEq(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);78assertEq(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);79assertEq(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);80assertEq(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);81assertEq(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);82assertEq(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);83assertEq(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);84assertEq(asmLink(asmCompile(USE_ASM + "function f() {var j=1,i=0; while(j){ if(0) continue; j=i } return j|0 } return f"))(), 0);85assertEq(asmLink(asmCompile(USE_ASM + "function f() {var j=1,i=0; for(;j;){ if(0) continue; j=i } return j|0 } return f"))(), 0);86assertEq(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);87assertEq(asmLink(asmCompile(USE_ASM + "function f(i) { i=i|0; for(;;) { return i|0 } return 0 } return f"))(42), 42);88assertEq(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);89var 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"));90assertEq(f(1,5), 10);91assertEq(f(6,5), 15);92var 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"));93assertEq(f(1,5), 10);94assertEq(f(6,5), 15);95var 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"));96assertEq(f(1,5), 10);97assertEq(f(6,5), 15);98assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; switch(i|0) { case 1: return 0; case 1: return 0 } return 0} return f");99assertAsmTypeFail(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");100assertAsmTypeFail(USE_ASM + "function f(i) { i=i|0; switch(1) { case 1: return 0; case 1: return 0 } return 0} return f");101assertAsmTypeFail(USE_ASM + "function f() { var i=0; switch(i) {}; return i|0 } return f");102assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) {}; return i|0 } return f"))(), 0);103assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { default: i=42 } return i|0 } return f"))(), 42);104assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { default: i=42; break } return i|0 } return f"))(), 42);105assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: i=42 } return i|0 } return f"))(), 42);106assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: i=42; break } return i|0 } return f"))(), 42);107assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: default: i=42 } return i|0 } return f"))(), 42);108assertEq(asmLink(asmCompile(USE_ASM + "function f() { var i=0; switch(i|0) { case 0: default: i=42; break } return i|0 } return f"))(), 42);109assertEq(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);110assertEq(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);111assertEq(asmLink(asmCompile(USE_ASM + "function f() { return 42; switch(1) { case 1: return 13 } return 14 } return f"))(), 42);112var 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}"));113assertEq(exp.c(10), undefined);114assertEq(exp.a(), 10);115var 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"));116assertEq(f(1), -1);117assertEq(f(2), 42);118assertEq(f(133742), 2);119assertEq(f(133743), 42);120var 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"));121assertEq(f(-1), 13);122assertEq(f(0), 13);123assertEq(f(1), 42);124var 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"));125assertEq(f(-1), 42);126assertEq(f(0), 13);127assertEq(f(1), 13);128assertEq(f(0xffffffff), 42);129var 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"));130assertEq(f(-1), 103);131assertEq(f(0), 100);132assertEq(f(1), 102);133assertEq(f(2), 100);134assertEq(f(3), 101);135var 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"));136assertEq(f(-1), 10);137assertEq(f(0), 13);138assertEq(f(1), 11);139assertEq(f(2), 13);140assertEq(f(3), 12);141assertEq(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);142assertEq(asmLink(asmCompile(USE_ASM + "function f() { a: do{break a;}while(0); a: do{break a;}while(0); return 42 } return f"))(), 42);143assertEq(asmLink(asmCompile('g', USE_ASM + "function f() { g:{ return 42 } return 13 } return f"), null)(), 42);144assertEq(asmLink(asmCompile(USE_ASM + "function f(x) {x=x|0;switch (x|0) {case 31:return 13;case 9: {x = 3;if ((x|0) <= 0) {return -1;}}}return 1;} return f"))(31), 13);145var imp = { ffi:function() { throw "Wrong" } };146assertEq(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);147assertEq(asmLink(asmCompile(USE_ASM + 'function f() {var k = 1;if (1) {for(k = 1; k|0; k=k-1|0) {}} return 1}; return f'))(), 1);148// Ternaries conditionals149//150// Basic ternaries151var f = asmLink(asmCompile(USE_ASM + "function f() { return 0; if (1) return -1; return -2} return f"));152assertEq(f(5), 0);153var 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"));154assertEq(f(1), 1);155assertEq(f(0), 0);156var guard = (function() {157 var called_ = false;158 return {159 called: function(){ return called_ },160 call: function(){ called_ = true }161 }162})();163var 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});164assertEq(f(1), 1);165assertEq(f(0), 1);166assertEq(guard.called(), false);167var 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});168assertEq(f(1), 0);169assertEq(f(0), 0);170assertEq(guard.called(), false);171var f = asmLink(asmCompile('glob', 'ffi', USE_ASM + "var func=ffi.func; function f(x,y) { x=x|0;y=y|0; var a=2;if(x?func()|0:y)a=1;else a=0; return a|0 } return f"), this, {func: guard.call});172assertEq(f(0,1), 1);173assertEq(guard.called(), false);174var f = asmLink(asmCompile('glob', 'ffi', USE_ASM + "var func=ffi.func; function f(x,y) { x=x|0;y=y|0; var a=2;if(x?y:func()|0)a=1;else a=0; return a|0 } return f"), this, {func: guard.call});175assertEq(f(1,0), 0);176assertEq(guard.called(), false);177var 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"));178assertEq(f(1,1), 0);179assertEq(f(1,0), 0);180assertEq(f(0,0), 0);181assertEq(f(0,1), 1);182var 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"));183assertEq(f(1,1), 1);184assertEq(f(1,0), 0);185assertEq(f(0,0), 1);186assertEq(f(0,1), 1);187var 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"));188for (var i = 0; i < 2; ++i)189 for (var j = 0; j < 2; ++j)190 for (var k = 0; k < 2; ++k)191 assertEq(f(i,j,k), ((i && j) || (!i && k))|0);192// Complex ternaries193function CheckTwoArgsTwoOptions(f) {194 function check(x,y) {195 return (x > 2 && y < 5) | 0;196 }197 for (var a = -10; a < 10; a++)198 for (var b = -10; b < 10; b++)199 assertEq(f(a,b), check(a,b));200}201function CheckThreeArgsTwoOptions(f) {202 function check(x,y,z) {203 return (x > 2 && y < 5 && z > -1) | 0;204 }205 for (var a = -10; a < 10; a++)206 for (var b = -10; b < 10; b++)207 for (var c = -10; c < 10; c++)208 assertEq(f(a,b,c), check(a,b,c));209}210// Ternaries with && semantics211var 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"));212CheckTwoArgsTwoOptions(f);213var 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"));214CheckTwoArgsTwoOptions(f);215var 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"));216CheckThreeArgsTwoOptions(f);217var 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"));218CheckThreeArgsTwoOptions(f);219var 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"));220CheckThreeArgsTwoOptions(f);221var 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"));222CheckThreeArgsTwoOptions(f);223var 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"));224CheckThreeArgsTwoOptions(f);225var 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"));226CheckThreeArgsTwoOptions(f);227var 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"));228CheckThreeArgsTwoOptions(f);229var 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"));230CheckThreeArgsTwoOptions(f);231// Ternaries with || semantics232var 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"));233CheckTwoArgsTwoOptions(f);234var 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"));235CheckTwoArgsTwoOptions(f);236var 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"));237CheckThreeArgsTwoOptions(f);238var 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"));239CheckThreeArgsTwoOptions(f);240var 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"));241CheckThreeArgsTwoOptions(f);242var 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"));243CheckThreeArgsTwoOptions(f);244var 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"));245CheckThreeArgsTwoOptions(f);246var 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"));247CheckThreeArgsTwoOptions(f);248// Massive test249var code = '"use asm";\250 function g(x,y) {\251 x=x|0;\252 y=y|0;\253 var z = 0;\254 if ((y|0) == 1337) {\255 z = 1;\256 } else if ((x|0) == 1 ? 1 : ((x|0) < 0 ? (y|0) == 1 : 0)) {\257 z = 2;\258 } else if ((x|0) == 2) {\259 z = 3;\260 } else if ((x|0) == 3 ? 1 : (x|0) == 4) {\261 z = 4;\262 } else if ((x|0) == 5 ? (y|0) > 5 : 0) {\263 z = 5;\264 } else {\265 z = 6;\266 }\267 return z|0;\268 }\269 return g;';270var m = asmLink(asmCompile(code));271assertEq(m(0, 1337), 1);272assertEq(m(0, 1338), 6);273assertEq(m(0, 0), 6);274assertEq(m(0, 1), 6);275assertEq(m(0, 1336), 6);276assertEq(m(1, 1337), 1);277assertEq(m(2, 1337), 1);278assertEq(m(3, 1337), 1);279assertEq(m(4, 1337), 1);280assertEq(m(5, 1337), 1);281assertEq(m(1, 10), 2);282assertEq(m(1, 1336), 2);283assertEq(m(-1, 10), 6);284assertEq(m(-1, 2), 6);285assertEq(m(-1, -1), 6);286assertEq(m(-1, 1), 2);287assertEq(m(-9, 1), 2);288assertEq(m(2, 1), 3);289assertEq(m(2, 0), 3);290assertEq(m(2, 6), 3);291assertEq(m(3, 1), 4);292assertEq(m(3, 0), 4);293assertEq(m(3, 6), 4);294assertEq(m(3, 3), 4);295assertEq(m(4, 1), 4);296assertEq(m(4, 0), 4);297assertEq(m(4, 6), 4);298assertEq(m(4, 3), 4);299assertEq(m(5, -1), 6);300assertEq(m(5, 4), 6);301assertEq(m(5, 5), 6);302assertEq(m(5, 6), 5);...
testMathLib.js
Source:testMathLib.js
1// |jit-test| test-also-noasmjs2load(libdir + "asm.js");3function testUnary(f, g) {4 var numbers = [NaN, Infinity, -Infinity, -10000, -3.4, -0, 0, 3.4, 10000];5 for (n of numbers)6 assertEq(f(n), g(n));7}8const FROUND = 'var fround=glob.Math.fround;';9assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sin; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sin:Math.sqrt}});10assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sin; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sin:null}});11testUnary(asmLink(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sin; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sin:Math.sin}}), Math.sin);12testUnary(asmLink(asmCompile('glob', USE_ASM + 'const sq=glob.Math.sin; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sin:Math.sin}}), Math.sin);13assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var co=glob.Math.cos; function f(d) { d=+d; return +co(d) } return f'), {Math:{cos:Math.sqrt}});14assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var co=glob.Math.cos; function f(d) { d=+d; return +co(d) } return f'), {Math:{cos:null}});15testUnary(asmLink(asmCompile('glob', USE_ASM + 'var co=glob.Math.cos; function f(d) { d=+d; return +co(d) } return f'), {Math:{cos:Math.cos}}), Math.cos);16assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ta=glob.Math.tan; function f(d) { d=+d; return +ta(d) } return f'), {Math:{tan:Math.sqrt}});17assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ta=glob.Math.tan; function f(d) { d=+d; return +ta(d) } return f'), {Math:{tan:null}});18testUnary(asmLink(asmCompile('glob', USE_ASM + 'var ta=glob.Math.tan; function f(d) { d=+d; return +ta(d) } return f'), {Math:{tan:Math.tan}}), Math.tan);19assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var as=glob.Math.asin; function f(d) { d=+d; return +as(d) } return f'), {Math:{asin:Math.sqrt}});20assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var as=glob.Math.asin; function f(d) { d=+d; return +as(d) } return f'), {Math:{asin:null}});21testUnary(asmLink(asmCompile('glob', USE_ASM + 'var as=glob.Math.asin; function f(d) { d=+d; return +as(d) } return f'), {Math:{asin:Math.asin}}), Math.asin);22assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ac=glob.Math.acos; function f(d) { d=+d; return +ac(d) } return f'), {Math:{acos:Math.sqrt}});23assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ac=glob.Math.acos; function f(d) { d=+d; return +ac(d) } return f'), {Math:{acos:null}});24testUnary(asmLink(asmCompile('glob', USE_ASM + 'var ac=glob.Math.acos; function f(d) { d=+d; return +ac(d) } return f'), {Math:{acos:Math.acos}}), Math.acos);25assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan; function f(d) { d=+d; return +at(d) } return f'), {Math:{atan:Math.sqrt}});26assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan; function f(d) { d=+d; return +at(d) } return f'), {Math:{atan:null}});27testUnary(asmLink(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan; function f(d) { d=+d; return +at(d) } return f'), {Math:{atan:Math.atan}}), Math.atan);28assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ce=glob.Math.ceil; function f(d) { d=+d; return +ce(d) } return f'), {Math:{ceil:Math.sqrt}});29assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var ce=glob.Math.ceil; function f(d) { d=+d; return +ce(d) } return f'), {Math:{ceil:null}});30testUnary(asmLink(asmCompile('glob', USE_ASM + 'var ce=glob.Math.ceil; function f(d) { d=+d; return +ce(d) } return f'), {Math:{ceil:Math.ceil}}), Math.ceil);31assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var fl=glob.Math.floor; function f(d) { d=+d; return +fl(d) } return f'), {Math:{floor:Math.sqrt}});32assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var fl=glob.Math.floor; function f(d) { d=+d; return +fl(d) } return f'), {Math:{floor:null}});33testUnary(asmLink(asmCompile('glob', USE_ASM + 'var fl=glob.Math.floor; function f(d) { d=+d; return +fl(d) } return f'), {Math:{floor:Math.floor}}), Math.floor);34assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var exq=glob.Math.exp; function f(d) { d=+d; return +exq(d) } return f'), {Math:{exp:Math.sqrt}});35assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var exq=glob.Math.exp; function f(d) { d=+d; return +exq(d) } return f'), {Math:{exp:null}});36testUnary(asmLink(asmCompile('glob', USE_ASM + 'var exq=glob.Math.exp; function f(d) { d=+d; return +exq(d) } return f'), {Math:{exp:Math.exp}}), Math.exp);37assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var lo=glob.Math.log; function f(d) { d=+d; return +lo(d) } return f'), {Math:{log:Math.sqrt}});38assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var lo=glob.Math.log; function f(d) { d=+d; return +lo(d) } return f'), {Math:{log:null}});39testUnary(asmLink(asmCompile('glob', USE_ASM + 'var lo=glob.Math.log; function f(d) { d=+d; return +lo(d) } return f'), {Math:{log:Math.log}}), Math.log);40assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sqrt; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sqrt:Math.sin}});41assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sqrt; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sqrt:null}});42testUnary(asmLink(asmCompile('glob', USE_ASM + 'var sq=glob.Math.sqrt; function f(d) { d=+d; return +sq(d) } return f'), {Math:{sqrt:Math.sqrt}}), Math.sqrt);43assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=+d; return +abs(d) } return f'), {Math:{abs:Math.sin}});44assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=+d; return +abs(d) } return f'), {Math:{abs:null}});45testUnary(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=+d; return +abs(d) } return f'), {Math:{abs:Math.abs}}), Math.abs);46var f = asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(i) { i=i|0; return abs(i|0)|0 } return f'), this);47for (n of [-Math.pow(2,31)-1, -Math.pow(2,31), -Math.pow(2,31)+1, -1, 0, 1, Math.pow(2,31)-2, Math.pow(2,31)-1, Math.pow(2,31)])48 assertEq(f(n), Math.abs(n|0)|0);49var f = asmLink(asmCompile('glob', USE_ASM + 'var clz32=glob.Math.clz32; function f(i) { i=i|0; return clz32(i)|0 } return f'), this);50for (n of [0, 1, 2, 15, 16, Math.pow(2,31)-1, Math.pow(2,31), Math.pow(2,31)+1, Math.pow(2,32)-1, Math.pow(2,32), Math.pow(2,32)+1])51 assertEq(f(n), Math.clz32(n|0));52assertEq(asmLink(asmCompile('glob', USE_ASM + 'var clz32=glob.Math.clz32; function f(i, j) { i=i|0;j=j|0; return (clz32(i) < (j|0))|0 } return f'), this)(0x1, 30), 0);53assertEq(asmLink(asmCompile('glob', USE_ASM + 'var clz32=glob.Math.clz32; function f(i, j) { i=i|0;j=j|0; return (clz32(i) < (j>>>0))|0 } return f'), this)(0x1, 30), 0);54var doubleNumbers = [NaN, Infinity, -Infinity, -10000, -3.4, -0, 0, 3.4, 10000];55var floatNumbers = [];56for (var x of doubleNumbers) floatNumbers.push(Math.fround(x));57var intNumbers = [-Math.pow(2,31), -10000, -3, -1, 0, 3, 10000, Math.pow(2,31), Math.pow(2,31)+1];58function testBinary(f, g, numbers) {59 for (n of numbers)60 for (o of numbers)61 assertEq(f(n,o), g(n,o));62}63assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var po=glob.Math.pow; function f(d,e) { d=+d;e=+e; return +po(d,e) } return f'), {Math:{pow:Math.sin}});64assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var po=glob.Math.pow; function f(d,e) { d=+d;e=+e; return +po(d,e) } return f'), {Math:{pow:null}});65testBinary(asmLink(asmCompile('glob', USE_ASM + 'var po=glob.Math.pow; function f(d,e) { d=+d;e=+e; return +po(d,e) } return f'), {Math:{pow:Math.pow}}), Math.pow, doubleNumbers);66assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan2; function f(d,e) { d=+d;e=+e; return +at(d,e) } return f'), {Math:{atan2:Math.sin}});67assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan2; function f(d,e) { d=+d;e=+e; return +at(d,e) } return f'), {Math:{atan2:null}});68testBinary(asmLink(asmCompile('glob', USE_ASM + 'var at=glob.Math.atan2; function f(d,e) { d=+d;e=+e; return +at(d,e) } return f'), {Math:{atan2:Math.atan2}}), Math.atan2, doubleNumbers);69function coercedMin(...args) { for (var i = 0; i < args.length; i++) args[i] = args[i]|0; return Math.min(...args) }70function coercedMax(...args) { for (var i = 0; i < args.length; i++) args[i] = args[i]|0; return Math.max(...args) }71assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(d) { d=+d; return +min(d) } return f');72assertAsmTypeFail('glob', 'ffi', 'heap', USE_ASM + 'var i32=new glob.Int32Array(heap); var min=glob.Math.min; function f() { return min(i32[0], 5)|0 } return f');73assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(x) { x=x|0; return min(3 + x, 5)|0 } return f');74assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(x) { x=x|0; return min(5, 3 + x)|0 } return f');75assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(x) { x=x|0; return min(x, 1)|0 } return f');76assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e) { d=+d;e=+e; return +min(d,e) } return f'), {Math:{min:Math.sin}});77assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e) { d=+d;e=+e; return +min(d,e) } return f'), {Math:{min:null}});78testBinary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e) { d=+d;e=+e; return +min(d,e) } return f'), {Math:{min:Math.min}}), Math.min, doubleNumbers);79testBinary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; var f32=glob.Math.fround; function f(d,e) { d=f32(d);e=f32(e); return f32(min(d,e)) } return f'), this), Math.min, floatNumbers);80testBinary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e) { d=d|0;e=e|0; return min(d|0,e|0)|0} return f'), {Math:{min:Math.min}}), coercedMin, intNumbers);81assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e) { d=+d;e=+e; return +max(d,e) } return f'), {Math:{max:Math.sin}});82assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e) { d=+d;e=+e; return +max(d,e) } return f'), {Math:{max:null}});83testBinary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e) { d=+d;e=+e; return +max(d,e) } return f'), {Math:{max:Math.max}}), Math.max, doubleNumbers);84testBinary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; var f32=glob.Math.fround; function f(d,e) { d=f32(d);e=f32(e); return f32(max(d,e)) } return f'), this), Math.max, floatNumbers);85testBinary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e) { d=d|0;e=e|0; return max(d|0,e|0)|0} return f'), {Math:{max:Math.max}}), coercedMax, intNumbers);86function testTernary(f, g, numbers) {87 for (n of numbers)88 for (o of numbers)89 for (p of numbers)90 assertEq(f(n,o,p), g(n,o,p));91}92assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e,g) { d=+d;e=+e;g=g|0; return +min(d,e,g) } return f');93assertAsmTypeFail('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e,g) { d=d|0;e=e|0;g=+g; return max(d,e,g)|0 } return f');94assertAsmTypeFail('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e,g) { d=+d;e=+e;g=+g; return min(d,e,g)|0 } return f');95testTernary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e,g) { d=d|0;e=e|0;g=g|0; return +max(d|0,e|0,g|0) } return f'), {Math:{max:Math.max}}), coercedMax, intNumbers);96testTernary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e,g) { d=d|0;e=e|0;g=g|0; return max(d|0,e|0,g|0)|0 } return f'), {Math:{max:Math.max}}), coercedMax, intNumbers);97testTernary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d,e,g) { d=+d;e=+e;g=+g; return +max(d,e,g) } return f'), {Math:{max:Math.max}}), Math.max, doubleNumbers);98testTernary(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; var _=glob.Math.fround; function f(d,e,g) { d=_(d);e=_(e);g=_(g); return _(max(d,e,g)) } return f'), this), Math.max, floatNumbers);99testTernary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e,g) { d=d|0;e=e|0;g=g|0; return min(d|0,e|0,g|0)|0 } return f'), {Math:{min:Math.min}}), coercedMin, intNumbers);100testTernary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d,e,g) { d=+d;e=+e;g=+g; return +min(d,e,g) } return f'), {Math:{min:Math.min}}), Math.min, doubleNumbers);101testTernary(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; var _=glob.Math.fround; function f(d,e,g) { d=_(d);e=_(e);g=_(g); return _(min(d,e,g)) } return f'), this), Math.min, floatNumbers);102// Implicit return coercions of math functions103assertEq(asmLink(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i) { i=i|0; i = im(i,i); return i|0 } return f'), this)(3), 9);104assertAsmTypeFail('glob', USE_ASM + 'var im=glob.Math.imul; function f(d) { d=+d; d = im(d, d) } return f');105assertAsmTypeFail('glob', USE_ASM + FROUND + 'var im=glob.Math.imul; function f(d) { d=fround(d); d = im(d, d) } return f');106assertEq(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=d|0; d = abs(d|0); return +(d>>>0) } return f'), this)(-1), 1);107assertEq(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=d|0; var m = 0; m = (-1)>>>0; return (abs(d|0) < (m>>>0))|0 } return f'), this)(42), 1);108assertEq(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=+d; d = abs(d); return +d } return f'), this)(-1.5), 1.5);109assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var abs=glob.Math.abs; function f(d) { d=fround(d); d = fround(abs(d)); return +d } return f'), this)(-1.5), 1.5);110assertAsmTypeFail('glob', USE_ASM + FROUND + 'var abs=glob.Math.abs; function f(d) { d=fround(d); d = abs(d); return +d } return f');111assertAsmTypeFail('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=d|0; var m = 0; m = -1; return (abs(d|0) < (m|0))|0 } return f');112assertEq(asmLink(asmCompile('glob', USE_ASM + 'var sqrt=glob.Math.sqrt; function f(d) { d=+d; d = sqrt(d); return +d } return f'), this)(256), 16);113assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var sqrt=glob.Math.sqrt; function f(d) { d=fround(d); d = fround(sqrt(d)); return +d } return f'), this)(13.37), Math.fround(Math.sqrt(Math.fround(13.37))));114assertAsmTypeFail('glob', USE_ASM + FROUND + 'var sqrt=glob.Math.sqrt; function f(d) { d=fround(d); d = sqrt(d); return fround(d) } return f');115assertAsmTypeFail('glob', USE_ASM + FROUND + 'var sqrt=glob.Math.sqrt; function f(d) { d=fround(d); d = sqrt(d); return d } return f');116assertAsmTypeFail('glob', USE_ASM + 'var sqrt=glob.Math.sqrt; function f(n) { n=n|0; var d=0.; d = sqrt(n|0) } return f');117assertAsmTypeFail('glob', USE_ASM + 'var sqrt=glob.Math.sqrt; function f(n) { n=n|0; var d=3.; n = sqrt(d)|0 } return f');118assertEq(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d) { d=+d; d = min(d, 13.); return +d } return f'), this)(12), 12);119assertEq(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d) { d=d|0; d = min(d|0, 11); return d|0 } return f'), this)(12), 11);120assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var min=glob.Math.min; function f(d) { d=fround(d); d = min(d, fround(13.37)); return fround(d) } return f'), this)(14), Math.fround(13.37));121assertEq(asmLink(asmCompile('glob', USE_ASM + 'var sin=glob.Math.sin; function f(d) { d=+d; d = sin(d); return +d } return f'), this)(Math.PI), Math.sin(Math.PI));122assertAsmTypeFail('glob', USE_ASM + FROUND + 'var sin=glob.Math.sin; function f(d) { d=fround(d); d = sin(d) } return f');123assertAsmTypeFail('glob', USE_ASM + 'var sin=glob.Math.sin; function f(d) { d=d|0; d = sin(d) } return f');124assertEq(asmLink(asmCompile('glob', USE_ASM + 'var pow=glob.Math.pow; function f(d) { d=+d; d = pow(d,d); return +d } return f'), this)(3), 27);125assertAsmTypeFail('glob', USE_ASM + FROUND + 'var pow=glob.Math.pow; function f(d) { d=fround(d); d = pow(d, d) } return f');126assertAsmTypeFail('glob', USE_ASM + 'var pow=glob.Math.pow; function f(d) { d=d|0; d = pow(d, d) } return f');127assertAsmTypeFail('glob', USE_ASM + 'var sin=glob.Math.sin; function f(d) { d=+d; var i=0; i = sin(d)|0; } return f');128assertAsmTypeFail('glob', USE_ASM + 'var pow=glob.Math.pow; function f(d) { d=+d; var i=0; i = pow(d,d)|0; } return f');129assertAsmTypeFail('glob', USE_ASM + 'var atan2=glob.Math.atan2; function f(d) { d=+d; var i=0; i = atan2(d,d)|0; } return f');130assertAsmTypeFail('glob', USE_ASM + 'var sqrt=glob.Math.sqrt; function f(d) { d=+d; sqrt(d)|0; } return f');131assertAsmTypeFail('glob', USE_ASM + 'var abs=glob.Math.abs; function f(d) { d=+d; abs(d)|0; } return f');132assertEq(asmLink(asmCompile('glob', USE_ASM + 'var im=glob.Math.imul; function f(i) { i=i|0; var d=0.0; d = +im(i,i); return +d } return f'), this)(42), Math.imul(42, 42));133assertEq(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(i) { i=i|0; var d=0.0; d = +abs(i|0); return +d } return f'), this)(-42), 42);134assertEq(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(i) { i=i|0; var d=0.0; d = +min(i|0, 0); return +d } return f'), this)(-42), -42);135assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var min=glob.Math.min; function f(i) { i=i|0; var d=fround(0); d = fround(min(i|0, 0)); return +d } return f'), this)(-42), -42);136assertEq(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(i) { i=i|0; var d=0.0; d = +max(i|0, 0); return +d } return f'), this)(-42), 0);137assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var max=glob.Math.max; function f(i) { i=i|0; var d=fround(0); d = fround(max(i|0, 0)); return +d } return f'), this)(-42), 0);138assertEq(asmLink(asmCompile('glob', USE_ASM + 'var min=glob.Math.min; function f(d) { d=+d; var i=0; i = ~~min(d, 0.)|0; return i|0 } return f'), this)(-42), -42);139assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var min=glob.Math.min; function f(d) { d=fround(d); var i=0; i = ~~min(d, fround(0))|0; return i|0 } return f'), this)(-42), -42);140assertEq(asmLink(asmCompile('glob', USE_ASM + 'var max=glob.Math.max; function f(d) { d=+d; var i=0; i = ~~max(d, 0.)|0; return i|0 } return f'), this)(-42), 0);141assertEq(asmLink(asmCompile('glob', USE_ASM + FROUND + 'var max=glob.Math.max; function f(d) { d=fround(d); var i=0; i = ~~max(d, fround(0))|0; return i|0 } return f'), this)(-42), 0);142assertEq(asmLink(asmCompile('glob', USE_ASM + 'var abs=glob.Math.abs; function f(i) { i=i|0; var d=0.0; return +d; +abs(i|0); return 3.0;} return f'), this)(-42), 0);143assertAsmTypeFail('glob', USE_ASM + 'var tau=glob.Math.TAU; function f() {} return f');144assertAsmTypeFail('glob', USE_ASM + 'var pi=glob.Math.PI; function f() { return pi | 0 } return f');145assertAsmTypeFail('glob', USE_ASM + 'var pi=glob.Math.PI; function f() { return +pi() } return f');146assertAsmTypeFail('glob', USE_ASM + 'var pi=glob.Math.PI; function f() { pi = +3; } return f');147assertAsmLinkAlwaysFail(asmCompile('glob', USE_ASM + 'var pi=glob.Math.PI; function f() {} return f'), {});148assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var pi=glob.Math.PI; function f() {} return f'), {Math: {}});149assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var pi=glob.Math.PI; function f() {} return f'), {Math: {PI: Math.cos}});150assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var pi=glob.Math.PI; function f() {} return f'), {Math: {PI: Math.SQRT2}});151for (var c of ['E', 'LN10', 'LN2', 'LOG2E', 'LOG10E', 'PI', 'SQRT1_2', 'SQRT2']) {152 var f = asmLink(asmCompile('glob', USE_ASM + 'var x=glob.Math.' + c +'; function f() { return +x } return f'), this);153 assertEq(f(), eval('Math.' + c));...
testGlobals.js
Source:testGlobals.js
1// |jit-test| test-also-noasmjs2load(libdir + "asm.js");3load(libdir + "asserts.js");4assertAsmTypeFail(USE_ASM + "var i; function f(){} return f");5assertEq(asmLink(asmCompile(USE_ASM + "var i=0; function f(){} return f"))(), undefined);6assertEq(asmLink(asmCompile(USE_ASM + "const i=0; function f(){} return f"))(), undefined);7assertEq(asmLink(asmCompile(USE_ASM + "var i=42; function f(){ return i|0 } return f"))(), 42);8assertEq(asmLink(asmCompile(USE_ASM + "const i=42; function f(){ return i|0 } return f"))(), 42);9assertEq(asmLink(asmCompile(USE_ASM + "var i=4.2; function f(){ return +i } return f"))(), 4.2);10assertEq(asmLink(asmCompile(USE_ASM + "const i=4.2; function f(){ return +i } return f"))(), 4.2);11assertAsmTypeFail(USE_ASM + "var i=42; function f(){ return +(i+.1) } return f");12assertAsmTypeFail(USE_ASM + "const i=42; function f(){ return +(i+.1) } return f");13assertAsmTypeFail(USE_ASM + "var i=1.2; function f(){ return i|0 } return f");14assertAsmTypeFail(USE_ASM + "const i=1.2; function f(){ return i|0 } return f");15assertAsmTypeFail(USE_ASM + "var i=0; function f(e){ e=+e; i=e } return f");16assertAsmTypeFail(USE_ASM + "var d=0.1; function f(i){ i=i|0; d=i } return f");17assertEq(asmLink(asmCompile(USE_ASM + "var i=13; function f(j) { j=j|0; i=j; return i|0 } return f"))(42), 42);18assertEq(asmLink(asmCompile(USE_ASM + "var d=.1; function f(e) { e=+e; d=e; return +e } return f"))(42.1), 42.1);19assertEq(asmLink(asmCompile(USE_ASM + "var i=13; function f(i, j) { i=i|0; j=j|0; i=j; return i|0 } return f"))(42,43), 43);20assertEq(asmLink(asmCompile(USE_ASM + "var i=13; function f(j) { j=j|0; var i=0; i=j; return i|0 } return f"))(42), 42);21var f = asmLink(asmCompile(USE_ASM + "var i=13; function f(j) { j=j|0; if ((j|0) != -1) { i=j } else { return i|0 } return 0 } return f"));22assertEq(f(-1), 13);23assertEq(f(42), 0);24assertEq(f(-1), 42);25assertAsmTypeFail(USE_ASM + "var i=13; function f() { var j=i; return j|0 } return f");26assertAsmTypeFail(USE_ASM + "var i=13.37; function f() { var j=i; return +j } return f");27assertAsmTypeFail('global', USE_ASM + "var f32 = global.Math.fround; var i=f32(13.37); function f() { var j=i; return f32(j) } return f");28assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { var j=i; return +j } return f'), {Infinity:Infinity})(), Infinity);29assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { var j=i; return +j } return f'), {NaN:NaN})(), NaN);30assertEq(asmLink(asmCompile(USE_ASM + "const i=13; function f() { var j=i; return j|0 } return f"))(), 13);31assertEq(asmLink(asmCompile(USE_ASM + "const i=13.37; function f() { var j=i; return +j } return f"))(), 13.37);32assertEq(asmLink(asmCompile('global', USE_ASM + "var f32 = global.Math.fround; const i=f32(13.37); function f() { var j=i; return f32(j) } return f"), this)(), Math.fround(13.37));33assertAsmTypeFail(USE_ASM + "function f() { var j=i; return j|0 } return f");34assertAsmTypeFail(USE_ASM + "function i(){} function f() { var j=i; return j|0 } return f");35assertAsmTypeFail(USE_ASM + "function f() { var j=i; return j|0 } var i = [f]; return f");36assertAsmTypeFail('global', USE_ASM + "var i=global.Math.fround; function f() { var j=i; return j|0 } return f");37assertAsmTypeFail('global', 'imp', USE_ASM + "var i=imp.f; function f() { var j=i; return j|0 } return f");38assertAsmTypeFail('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { var j=i; return j|0 } return f");39assertAsmTypeFail('global', 'imp', USE_ASM + "var i=+imp.i; function f() { var j=i; return +j } return f");40assertAsmTypeFail('global', 'imp', USE_ASM + "const i=imp.i|0; function f() { var j=i; return j|0 } return f");41assertAsmTypeFail('global', 'imp', USE_ASM + "const i=+imp.i; function f() { var j=i; return +j } return f");42assertAsmTypeFail('global', 'imp', 'heap', USE_ASM + "var i=new global.Float32Array(heap); function f() { var j=i; return +j } return f");43assertAsmTypeFail('global', USE_ASM + "var i=global; function f() { return i|0 } return f");44assertAsmTypeFail('global', USE_ASM + "const i=global; function f() { return i|0 } return f");45assertAsmTypeFail('global', USE_ASM + "var i=global|0; function f() { return i|0 } return f");46assertAsmTypeFail('global', USE_ASM + "const i=global|0; function f() { return i|0 } return f");47assertAsmTypeFail('global', USE_ASM + "var j=0;var i=j.i|0; function f() { return i|0 } return f");48assertAsmTypeFail('global', USE_ASM + "var i=global.i|0; function f() { return i|0 } return f");49assertAsmTypeFail('global', USE_ASM + "const i=global.i|0; function f() { return i|0 } return f");50assertAsmTypeFail('global', USE_ASM + "var i=global.i|0; function f() { return i|0 } return f");51assertAsmTypeFail('global', USE_ASM + 'var i=global.Infinity; function f() { i = 0.0 } return f');52assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), undefined);53assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'const i=global.Infinity; function f() { return +i } return f'), undefined);54assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), null);55assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'const i=global.Infinity; function f() { return +i } return f'), null);56assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), 3);57assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), {});58assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), {Infinity:NaN});59assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), {Infinity:-Infinity});60assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), {Infinity:Infinity})(), Infinity);61assertEq(asmLink(asmCompile('global', USE_ASM + 'const i=global.Infinity; function f() { return +i } return f'), {Infinity:Infinity})(), Infinity);62assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.Infinity; function f() { return +i } return f'), this)(), Infinity);63assertEq(asmLink(asmCompile('global', USE_ASM + 'const i=global.Infinity; function f() { return +i } return f'), this)(), Infinity);64assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), undefined);65assertAsmLinkAlwaysFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), null);66assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), 3);67assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), {});68assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), {Infinity:Infinity});69assertAsmLinkFail(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), {Infinity:-Infinity});70assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), {NaN:NaN})(), NaN);71assertEq(asmLink(asmCompile('global', USE_ASM + 'const i=global.NaN; function f() { return +i } return f'), {NaN:NaN})(), NaN);72assertEq(asmLink(asmCompile('global', USE_ASM + 'var i=global.NaN; function f() { return +i } return f'), this)(), NaN);73assertEq(asmLink(asmCompile('global', USE_ASM + 'const i=global.NaN; function f() { return +i } return f'), this)(), NaN);74assertAsmLinkFail(asmCompile('glob','foreign','buf', USE_ASM + 'var t = new glob.Int32Array(buf); function f() {} return f'), {get Int32Array(){return Int32Array}}, null, new ArrayBuffer(4096))75assertAsmLinkFail(asmCompile('glob','foreign','buf', USE_ASM + 'const t = new glob.Int32Array(buf); function f() {} return f'), {get Int32Array(){return Int32Array}}, null, new ArrayBuffer(4096))76assertAsmLinkFail(asmCompile('glob','foreign','buf', USE_ASM + 'var t = new glob.Int32Array(buf); function f() {} return f'), new Proxy(this, {}), null, new ArrayBuffer(4096))77assertAsmLinkFail(asmCompile('glob','foreign','buf', USE_ASM + 'const t = new glob.Int32Array(buf); function f() {} return f'), new Proxy(this, {}), null, new ArrayBuffer(4096))78assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Math.sin; function f() {} return f'), {get Math(){return Math}});79assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Math.sin; function f() {} return f'), new Proxy(this, {}));80assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Math.sin; function f() {} return f'), {Math:{get sin(){return Math.sin}}});81assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Math.sin; function f() {} return f'), {Math:new Proxy(Math, {})});82assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Infinity; function f() {} return f'), {get Infinity(){return Infinity}});83assertAsmLinkFail(asmCompile('glob', USE_ASM + 'var t = glob.Infinity; function f() {} return f'), new Proxy(this, {}));84assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x|0; function f() {} return f'), null, {get x(){return 42}})85assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = +foreign.x; function f() {} return f'), null, {get x(){return 42}})86assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x|0; function f() {} return f'), null, new Proxy({x:42}, {}));87assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = +foreign.x; function f() {} return f'), null, new Proxy({x:42}, {}));88assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x; function f() {} return f'), null, {get x(){return function(){}}})89assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x; function f() {} return f'), null, new Proxy({x:function(){}}, {}));90assertAsmTypeFail('global', 'imp', USE_ASM + "var i=imp; function f() { return i|0 } return f");91assertAsmTypeFail('global', 'imp', USE_ASM + "var j=0;var i=j.i|0; function f() { return i|0 } return f");92assertAsmLinkAlwaysFail(asmCompile('global','imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f"), null, undefined);93assertAsmLinkAlwaysFail(asmCompile('global','imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f"), this, undefined);94assertAsmLinkAlwaysFail(asmCompile('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f"), null, null);95assertAsmLinkAlwaysFail(asmCompile('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f"), this, null);96assertAsmLinkFail(asmCompile('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f"), this, 42);97assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f")(null, {i:42})), 42);98assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "const i=imp.i|0; function f() { return i|0 } return f")(null, {i:42})), 42);99assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "var i=imp.i|0; function f() { return i|0 } return f")(null, {i:1.4})), 1);100assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "const i=imp.i|0; function f() { return i|0 } return f")(null, {i:1.4})), 1);101assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "var i=+imp.i; function f() { return +i } return f")(null, {i:42})), 42);102assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "const i=+imp.i; function f() { return +i } return f")(null, {i:42})), 42);103assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "var i=+imp.i; function f() { return +i } return f")(this, {i:1.4})), 1.4);104assertEq(asmLink(asmCompile('global', 'imp', USE_ASM + "const i=+imp.i; function f() { return +i } return f")(this, {i:1.4})), 1.4);105assertEq(asmLink(asmCompile(USE_ASM + "var g=0; function f() { var i=42; while (1) { break; } g = i; return g|0 } return f"))(), 42);106assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = +foreign.x; function f() {} return f'), null, {x:{valueOf:function() { return 42 }}});107assertAsmLinkFail(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x|0; function f() {} return f'), null, {x:{valueOf:function() { return 42 }}});108assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.x|0; function f() { return i|0} return f'), null, {x:"blah"})(), 0);109assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var i = +foreign.x; function f() { return +i} return f'), null, {x:"blah"})(), NaN);110assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var tof = glob.Math.fround; var i = tof(foreign.x); function f() { return +i} return f'), this, {x:"blah"})(), NaN);111assertThrowsInstanceOf(() => asmCompile('glob','foreign',USE_ASM + 'var i = foreign.x|0; function f() { return i|0} return f')(null, {x:Symbol("blah")}), TypeError);112assertThrowsInstanceOf(() => asmCompile('glob','foreign',USE_ASM + 'var i = +foreign.x; function f() { return +i} return f')(null, {x:Symbol("blah")}), TypeError);113assertThrowsInstanceOf(() => asmCompile('glob','foreign',USE_ASM + 'var tof = glob.Math.fround; var i = tof(foreign.x); function f() { return +i} return f')(this, {x:Symbol("blah")}), TypeError);114// Temporary workaround; this test can be removed when Emscripten is fixed and115// the fix has propagated out to most apps:116assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var i = foreign.i|0; function f() { return i|0} return f'), null, {i:function(){}})(), 0);117assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var i = +foreign.i; function f() { return +i} return f'), null, {i:function(){}})(), NaN);118assertEq(asmLink(asmCompile('glob','foreign', USE_ASM + 'var tof = glob.Math.fround; var i = tof(foreign.i); function f() { return +i} return f'), this, {i:function(){}})(), NaN);119var module = asmCompile('glob','foreign', USE_ASM + 'var i = foreign.i|0; function f() { return i|0} return f');120var fun = function() {}121fun.valueOf = function() {}122assertAsmLinkFail(module, null, {i:fun});123var fun = function() {}124fun.toString = function() {}125assertAsmLinkFail(module, null, {i:fun});126var fun = function() {}127var origFunToString = Function.prototype.toString;128Function.prototype.toString = function() {}129assertAsmLinkFail(module, null, {i:fun});130Function.prototype.toString = origFunToString;131assertEq(asmLink(module, null, {i:fun})(), 0);132Function.prototype.valueOf = function() {}133assertAsmLinkFail(module, null, {i:fun});134delete Function.prototype.valueOf;135assertEq(asmLink(module, null, {i:fun})(), 0);136var origObjValueOf = Object.prototype.valueOf;137Object.prototype.valueOf = function() {}138assertAsmLinkFail(module, null, {i:fun});139Object.prototype.valueOf = origObjValueOf;140assertEq(asmLink(module, null, {i:fun})(), 0);141Object.prototype.toString = function() {}142assertEq(asmLink(module, null, {i:fun})(), 0);143var f1 = asmCompile('global', 'foreign', 'heap', USE_ASM + 'var i32 = new global.Int32Array(heap); function g() { return i32[4]|0 } return g');144var global = this;145var ab = new ArrayBuffer(4096);146var p = new Proxy(global,147 {has:function(name) { f1(global, null, ab); return true},148 getOwnPropertyDescriptor:function(name) { return {configurable:true, value:Int32Array}}});149new Int32Array(ab)[4] = 42;150assertEq(f1(p, null, ab)(), 42);151// GVN checks152assertEq(asmLink(asmCompile(USE_ASM + "var g=0; function f() { var x = 0; g = 1; x = g; return (x+g)|0 } return f"))(), 2);153assertEq(asmLink(asmCompile(USE_ASM + "var g=0; function f() { var x = 0; g = 1; x = g; g = 2; return (x+g)|0 } return f"))(), 3);...
compile.test.js
Source:compile.test.js
1import test from 'tape'2import { map } from 'lodash-es'3import { compileJavascript } from '../index'4test('compile: empty string', t => {5 let code = ''6 let actual = compileJavascript(code)7 let expected = {8 inputs: [],9 outputs: [],10 messages: []11 }12 _isFulfilled(t, actual, expected)13 t.end()14})15test('compile: syntax error', t => {16 let code, actual, expected17 code = 'foo bar()'18 actual = compileJavascript(code)19 expected = {20 messages: [{21 type: 'error',22 message: 'Syntax error in Javascript: Unexpected token (1:4)',23 line: 1,24 column: 425 }]26 }27 _isFulfilled(t, actual, expected)28 code = 'for(){'29 actual = compileJavascript(code)30 expected = {31 messages: [{32 type: 'error',33 message: 'Syntax error in Javascript: Unexpected token (1:4)',34 line: 1,35 column: 436 }]37 }38 _isFulfilled(t, actual, expected)39 t.end()40})41test('compile: Math.pi', t => {42 let code = 'Math.pi'43 let actual = compileJavascript(code)44 let expected = {45 inputs: [],46 outputs: [],47 messages: []48 }49 _isFulfilled(t, actual, expected)50 t.end()51})52test('compile: require()', t => {53 let code = 'const foo = require("foo")\nfoo.bar'54 let actual = compileJavascript(code)55 let expected = {56 inputs: [],57 outputs: [],58 messages: []59 }60 _isFulfilled(t, actual, expected)61 t.end()62})63test('compile: specialFunc()', t => {64 let code = 'const result = specialFunc()'65 let actual = compileJavascript(code)66 let expected = {67 inputs: [{name: 'specialFunc'}],68 outputs: [{name: 'result'}],69 messages: []70 }71 _isFulfilled(t, actual, expected)72 t.end()73})74test('compile: specialMath.pi', t => {75 let code = 'specialMath.pi'76 let actual = compileJavascript(code)77 let expected = {78 inputs: [{name: 'specialMath'}],79 outputs: [],80 messages: []81 }82 _isFulfilled(t, actual, expected)83 t.end()84})85test('compile: last statement is a declaration', t => {86 let code = 'var foo'87 let actual = compileJavascript(code)88 let expected = {89 inputs: [],90 outputs: [{name: 'foo'}],91 messages: []92 }93 _isFulfilled(t, actual, expected)94 code = 'const foo = 1'95 actual = compileJavascript(code)96 expected = {97 inputs: [],98 outputs: [{name: 'foo'}],99 messages: []100 }101 _isFulfilled(t, actual, expected)102 t.end()103})104test('compile: last statement is not locally declared', t => {105 let code = 'foo'106 let actual = compileJavascript(code)107 let expected = {108 inputs: [{name: 'foo'}],109 outputs: [],110 messages: []111 }112 _isFulfilled(t, actual, expected)113 t.end()114})115test('compile: last statement is a locally declared variable', t => {116 let code = 'var foo\nfoo'117 let actual = compileJavascript(code)118 let expected = {119 inputs: [],120 outputs: [{name: 'foo'}],121 messages: []122 }123 _isFulfilled(t, actual, expected)124 code = 'let foo\nfoo'125 actual = compileJavascript(code)126 _isFulfilled(t, actual, expected)127 code = 'const foo = 1\nfoo'128 actual = compileJavascript(code)129 _isFulfilled(t, actual, expected)130 code = 'var foo = 1\nfoo'131 actual = compileJavascript(code)132 _isFulfilled(t, actual, expected)133 t.end()134})135test('compile: last statement is not a declaration', t => {136 let code, actual, expected137 code = 'let foo\nfoo * 3'138 actual = compileJavascript(code)139 expected = {140 inputs: [],141 outputs: [],142 messages: []143 }144 _isFulfilled(t, actual, expected)145 code = 'let foo\n{let baz}'146 actual = compileJavascript(code)147 _isFulfilled(t, actual, expected)148 code = 'let foo\nlet baz\ntrue'149 actual = compileJavascript(code)150 _isFulfilled(t, actual, expected)151 t.end()152})153// Last statement is a declaration with multiple declarations (first identifier used)154test('compile: last statement has multiple declarations', t => {155 let code = 'foo\nbar\nlet baz, urg\n\n'156 let actual = compileJavascript(code)157 let expected = {158 inputs: [{name: 'foo'}, {name: 'bar'}],159 outputs: [{name: 'baz'}],160 messages: []161 }162 _isFulfilled(t, actual, expected)163 t.end()164})165// Only top level variable declarations are considered when166// determining cell inputs167test('compile: complex example with nested and shadowed variables', t => {168 let code = `169 let a;170 { let c };171 for (let b in [1,2,3]){};172 if (true) { const d = 1 };173 function f () { let e = 2 };174 a * b * c * d * e;175 `176 let actual = compileJavascript(code)177 let expected = {178 inputs: [{name: 'b'}, {name: 'c'}, {name: 'd'}, {name: 'e'}],179 outputs: [],180 messages: []181 }182 _isFulfilled(t, actual, expected)183 t.end()184})185// Last statement is not a declaration or identifier186test('compile: last statement is not a declaration or identifier', t => {187 let code = 'let foo\nbar\nlet baz\ntrue'188 let actual = compileJavascript(code)189 let expected = {190 inputs: [{name: 'bar'}],191 outputs: [],192 messages: []193 }194 _isFulfilled(t, actual, expected)195 t.end()196})197test('compile: anoymous arrow function', t => {198 let code = '[1,2,3].map(n => n*2)'199 let actual = compileJavascript(code)200 let expected = {201 inputs: [],202 outputs: [],203 messages: []204 }205 _isFulfilled(t, actual, expected)206 t.end()207})208test('compile: declaration after usage', t => {209 // ATTENTION: the former implementation of this test was expecting an input detected here210 // however this was not correct. While in ES6 variables are only initialised211 // after being declared explicitly, they declaration is hoisted within this block212 // i.e. shadowing access to the global variable213 // Thus the correct expectation is here to have no inputs.214 let code = 'foo\nlet foo\n'215 let actual = compileJavascript(code)216 let expected = {217 inputs: [],218 outputs: [{name: 'foo'}],219 messages: []220 }221 _isFulfilled(t, actual, expected)222 t.end()223})224test('compile: simple expressions', t => {225 let code, actual, expected226 code = '42'227 actual = compileJavascript(code, {expr: true})228 expected = {229 inputs: [],230 outputs: [],231 messages: []232 }233 _isFulfilled(t, actual, expected)234 code = 'x * 3'235 actual = compileJavascript(code, {expr: true})236 expected = {237 inputs: [{name: 'x'}],238 outputs: [],239 messages: []240 }241 _isFulfilled(t, actual, expected)242 t.end()243})244test('compile: not a simple expressions', t => {245 let code, actual, expected246 code = 'let y = x * 3'247 actual = compileJavascript(code, {expr: true})248 expected = {249 messages: [{ line: 0, column: 0, type: 'error', message: 'Error: Code is not a single, simple expression' }]250 }251 _isFulfilled(t, actual, expected)252 code = 'y = x * 3'253 actual = compileJavascript(code, {expr: true})254 _isFulfilled(t, actual, expected)255 code = 'x++'256 actual = compileJavascript(code, {expr: true})257 _isFulfilled(t, actual, expected)258 code = 'y--'259 actual = compileJavascript(code, {expr: true})260 _isFulfilled(t, actual, expected)261 code = 'function foo(){}'262 actual = compileJavascript(code, {expr: true})263 _isFulfilled(t, actual, expected)264 t.end()265})266test('compile: last statement is an expression', t => {267 let code, actual, expected268 code = 'true'269 actual = compileJavascript(code)270 expected = {271 inputs: [],272 outputs: [],273 messages: []274 }275 _isFulfilled(t, actual, expected)276 code = 'foo * 3'277 actual = compileJavascript(code)278 expected = {279 inputs: [{name: 'foo'}],280 outputs: [],281 messages: []282 }283 _isFulfilled(t, actual, expected)284 code = 'var foo = 1\nfoo * 3'285 actual = compileJavascript(code)286 expected = {287 inputs: [],288 outputs: [],289 messages: []290 }291 _isFulfilled(t, actual, expected)292 code = 'let z = x * y;\n(z * 2)'293 actual = compileJavascript(code)294 expected = {295 inputs: [{name: 'x'}, {name: 'y'}],296 outputs: [],297 messages: []298 }299 _isFulfilled(t, actual, expected)300 t.end()301})302test('compile: function', t => {303 let code, actual, expected304 code = 'function afunc (x, y) { return x * y }'305 actual = compileJavascript(code)306 expected = {307 inputs: [],308 outputs: [{309 name: 'afunc',310 spec: {311 type: 'function',312 code: code,313 name: 'afunc',314 methods: {315 'afunc(x, y)': {316 signature: 'afunc(x, y)',317 params: [318 { name: 'x' },319 { name: 'y' }320 ]321 }322 }323 }324 }],325 messages: []326 }327 _isFulfilled(t, actual, expected)328 t.end()329})330test('compile: multiple occurrence', t => {331 let code, actual, expected332 // should not result in multiple inputs of name 'x'333 code = 'x*x*x'334 actual = compileJavascript(code)335 expected = {336 inputs: [{ name: 'x' }],337 outputs: [],338 messages: []339 }340 _isFulfilled(t, actual, expected)341 // symbols should be in order of occurrence342 code = 'x**y*x*y*x'343 actual = compileJavascript(code)344 expected = {345 inputs: [{ name: 'x' }, { name: 'y' }],346 outputs: [],347 messages: []348 }349 _isFulfilled(t, actual, expected)350 t.end()351})352function _isFulfilled (t, cell, expected) {353 let actual = {}354 Object.keys(expected).forEach(n => {355 switch (n) {356 case 'inputs': {357 actual[n] = map(cell[n])358 break359 }360 default:361 actual[n] = cell[n]362 }363 })364 t.deepEqual(actual, expected)...
wrapper.js
Source:wrapper.js
...26 copyString(result.error, error);27 }28 };29 };30 // This calls compile() with args || cb31 var runWithReadCallback = function (readCallback, compile, args) {32 if (readCallback === undefined) {33 readCallback = function (path) {34 return {35 error: 'File import callback not supported'36 };37 };38 }39 var cb = soljson.Runtime.addFunction(wrapCallback(readCallback));40 var output;41 try {42 args.push(cb);43 output = compile.apply(undefined, args);44 } catch (e) {...
compile.js
Source:compile.js
12var crjs = require('..');34function compile(text, expected, test) {5 var result = crjs.compile(text);6 test.ok(result);7 test.equal(result, expected);8};910exports['compile integer'] = function (test) {11 compile('42', '42;', test);12 compile('42\n\n', '42;', test);13};1415exports['compile string'] = function (test) {16 compile('"foo"', '"foo";', test);17};1819exports['compile booleans'] = function (test) {20 compile('false', 'false;', test);21 compile('true', 'true;', test);22};2324exports['compile nil'] = function (test) {25 compile('nil', 'null;', test);26};2728exports['compile name'] = function (test) {29 compile('a', 'var a; a;', test);30};3132exports['compile assignment'] = function (test) {33 compile('a=1', 'var a; a = 1;', test);34};3536exports['compile assignment expression'] = function (test) {37 compile('a=1+2', 'var a; a = 1 + 2;', test);38};3940exports['compile assignments'] = function (test) {41 compile('a=1\nb=a', 'var a; var b; a = 1; b = a;', test);42};4344exports['compile assignments separated by carriage return'] = function (test) {45 compile('a=1\rb=a', 'var a; var b; a = 1; b = a;', test);46};4748exports['compile assignments separated by carriage return line feed'] = function (test) {49 compile('a=1\r\nb=a\r\n', 'var a; var b; a = 1; b = a;', test);50};5152exports['compile assignments separated by semi colon'] = function (test) {53 compile('a=1;b=a', 'var a; var b; a = 1; b = a;', test);54};5556exports['compile call'] = function (test) {57 compile('process "Hello, world"', 'process("Hello, world");', test);58};5960exports['compile runtime call'] = function (test) {61 compile('puts "Hello, world"', 'var $crysjs = require("crysjs"); var puts = $crysjs.runtime.puts; puts("Hello, world");', test);62};6364exports['compile def'] = function (test) {65 compile('def inc(a)\na+1\nend', 'function inc(a) { return a + 1; }', test);66};6768exports['compile qualified call without arguments'] = function (test) {69 compile('foo.bar', 'foo.bar();', test);70};7172exports['compile arithmetic expressions'] = function (test) {73 compile('1+2', '1 + 2;', test);74 compile('1-2', '1 - 2;', test);75 compile('3*2', '3 * 2;', test);76 compile('3/2', '3 / 2;', test);77 compile('1+2+3', '(1 + 2) + 3;', test);78 compile('1+2*3', '1 + (2 * 3);', test);79 compile('1/2+3', '(1 / 2) + 3;', test);80 compile('1/(2+3)', '1 / (2 + 3);', test);81};8283exports['compile module expression'] = function (test) {84 compile('1%2', '1 % 2;', test);85};8687exports['compile logical and expression'] = function (test) {88 compile('1 && 2', '1 && 2;', test);89};9091exports['compile logical or expression'] = function (test) {92 compile('1 || 2', '1 || 2;', test);93};9495exports['compile binary and expression'] = function (test) {96 compile('1 & 2', '1 & 2;', test);97};9899exports['compile binary or expression'] = function (test) {100 compile('1 | 2', '1 | 2;', test);101};102103exports['compile binary xor expression'] = function (test) {104 compile('1 ^ 2', '1 ^ 2;', test);105};106107exports['compile left shift expression'] = function (test) {108 compile('1 << 2', '1 << 2;', test);109};110111exports['compile right shift expression'] = function (test) {112 compile('1 >> 2', '1 >> 2;', test);113};114115exports['compile binary not expression'] = function (test) {116 compile('~1', '~1;', test);117};118119exports['compile power expression'] = function (test) {120 compile('2 ** 3', 'Math.pow(2, 3);', test);121};122123exports['compile equal expression'] = function (test) {124 compile('2 == 3', '2 == 3;', test);125};126127exports['compile not equal expression'] = function (test) {128 compile('2 != 3', '2 != 3;', test);129};130131exports['compile comparison expressions'] = function (test) {132 compile('1<2', '1 < 2;', test);133 compile('1>2', '1 > 2;', test);134 compile('1<=2', '1 <= 2;', test);135 compile('1>=2', '1 >= 2;', test);136 compile('1+2<3', '(1 + 2) < 3;', test);137 compile('1>2+3', '1 > (2 + 3);', test);138};139140exports['compile while expressions'] = function (test) {141 compile('while 1\na+1\nend', 'while (1) a + 1;', test);142 compile('while 1\na+1\nb+2\nend', 'while (1) { a + 1; b + 2; }', test);143 compile('while true\nputs 1\nend', 'var $crysjs = require("crysjs"); var puts = $crysjs.runtime.puts; while (true) puts(1);', test);144};145146exports['compile indexed expressions'] = function (test) {147 compile('foo[1]', 'foo[1];', test);148 compile('foo[1,2]', 'foo[1, 2];', test);149};150151exports['compile array expressions'] = function (test) {152 compile('[1,2]', '[1, 2];', test);153 compile('[1]', '[1];', test);154};155156exports['compile keyword expression'] = function (test) {157 compile(':foo', '":foo";', test);158};
...
index.js
Source:index.js
1'use strict';2const BbPromise = require('bluebird');3const validate = require('./lib/validate');4const compileApi = require('./lib/api');5const compileIntegrations = require('./lib/integrations');6const compileRouteResponses = require('./lib/routeResponses');7const compilePermissions = require('./lib/permissions');8const compileRoutes = require('./lib/routes');9const compileDeployment = require('./lib/deployment');10const compileStage = require('./lib/stage');11const compileAuthorizers = require('./lib/authorizers');12class AwsCompileWebsockets {13 constructor(serverless, options) {14 this.serverless = serverless;15 this.options = options;16 this.provider = this.serverless.getProvider('aws');17 Object.assign(18 this,19 validate,20 compileApi,21 compileIntegrations,22 compileRouteResponses,23 compileAuthorizers,24 compilePermissions,25 compileRoutes,26 compileDeployment,27 compileStage28 );29 this.hooks = {30 'package:compileEvents': () => {31 this.validated = this.validate();32 if (this.validated.events.length === 0) {33 return BbPromise.resolve();34 }35 return BbPromise.bind(this)36 .then(this.compileApi)37 .then(this.compileIntegrations)38 .then(this.compileRouteResponses)39 .then(this.compileAuthorizers)40 .then(this.compilePermissions)41 .then(this.compileRoutes)42 .then(this.compileStage)43 .then(this.compileDeployment);44 },45 };46 this.serverless.configSchemaHandler.defineFunctionEvent('aws', 'websocket', {47 anyOf: [48 { type: 'string' },49 {50 type: 'object',51 properties: {52 route: { type: 'string' },53 routeResponseSelectionExpression: {54 const: '$default',55 },56 authorizer: {57 anyOf: [58 { $ref: '#/definitions/awsArnString' },59 { $ref: '#/definitions/functionName' },60 {61 type: 'object',62 properties: {63 name: { $ref: '#/definitions/functionName' },64 arn: { $ref: '#/definitions/awsArn' },65 identitySource: { type: 'array', items: { type: 'string' } },66 },67 anyOf: [{ required: ['name'] }, { required: ['arn'] }],68 additionalProperties: false,69 },70 ],71 },72 },73 required: ['route'],74 additionalProperties: false,75 },76 ],77 });78 }79}...
function-toString-object-literals.js
Source:function-toString-object-literals.js
1description(2"This test checks that object literals are serialized properly. " +3"It's needed in part because JavaScriptCore converts numeric property names to string and back."4);5function compileAndSerialize(expression)6{7 var f = eval("(function () { return " + expression + "; })");8 var serializedString = f.toString();9 serializedString = serializedString.replace(/[ \t\r\n]+/g, " ");10 serializedString = serializedString.replace("function () { return ", "");11 serializedString = serializedString.replace("; }", "");12 return serializedString;13}14shouldBe("compileAndSerialize('a = { 1: null }')", "'a = { 1: null }'");15shouldBe("compileAndSerialize('a = { 0: null }')", "'a = { 0: null }'");16shouldBe("compileAndSerialize('a = { 1.0: null }')", "'a = { 1.0: null }'");17shouldBe("compileAndSerialize('a = { \"1.0\": null }')", "'a = { \"1.0\": null }'");18shouldBe("compileAndSerialize('a = { 1e-500: null }')", "'a = { 1e-500: null }'");19shouldBe("compileAndSerialize('a = { 1e-300: null }')", "'a = { 1e-300: null }'");20shouldBe("compileAndSerialize('a = { 1e300: null }')", "'a = { 1e300: null }'");21shouldBe("compileAndSerialize('a = { 1e500: null }')", "'a = { 1e500: null }'");22shouldBe("compileAndSerialize('a = { NaN: null }')", "'a = { NaN: null }'");23shouldBe("compileAndSerialize('a = { Infinity: null }')", "'a = { Infinity: null }'");24shouldBe("compileAndSerialize('a = { \"1\": null }')", "'a = { \"1\": null }'");25shouldBe("compileAndSerialize('a = { \"1hi\": null }')", "'a = { \"1hi\": null }'");26shouldBe("compileAndSerialize('a = { \"\\\'\": null }')", "'a = { \"\\\'\": null }'");27shouldBe("compileAndSerialize('a = { \"\\\\\"\": null }')", "'a = { \"\\\\\"\": null }'");28shouldBe("compileAndSerialize('a = { get x() { } }')", "'a = { get x() { } }'");29shouldBe("compileAndSerialize('a = { set x(y) { } }')", "'a = { set x(y) { } }'");30shouldThrow("compileAndSerialize('a = { --1: null }')");31shouldThrow("compileAndSerialize('a = { -NaN: null }')");32shouldThrow("compileAndSerialize('a = { -0: null }')");33shouldThrow("compileAndSerialize('a = { -0.0: null }')");34shouldThrow("compileAndSerialize('a = { -Infinity: null }')");...
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 const browser = await playwright['chromium'].launch();4 const page = await browser.newPage();5 await page.screenshot({ path: 'example.png' });6 await browser.close();7})();
Using AI Code Generation
1const { chromium } = require('playwright');2const fs = require('fs');3const path = require('path');4(async () => {5 const browser = await chromium.launch();6 const context = await browser.newContext();7 const page = await context.newPage();8 await page.screenshot({ path: 'screenshot.png' });9 const source = await page.mainFrame().evaluate(() => document.documentElement.outerHTML);10 const compiled = await page.mainFrame().compile(source);11 const compiledPath = path.join(__dirname, 'compiled.js');12 fs.writeFileSync(compiledPath, compiled, 'utf8');13 await browser.close();14})();15const { chromium } = require('playwright');16const fs = require('fs');17const path = require('path');18(async () => {19 const browser = await chromium.launch();20 const context = await browser.newContext();21 const page = await context.newPage();22 await page.screenshot({ path: 'screenshot.png' });23 await browser.close();24})();25const { chromium } = require('playwright');26(async () => {27 const browser = await chromium.launch();28 const context = await browser.newContext();29 const page = await context.newPage();30 await browser.close();31})();
Using AI Code Generation
1const { chromium } = require('playwright');2const { compile } = require('playwright/lib/server/supplements/recorder/recorderApp');3(async () => {4 const browser = await chromium.launch();5 const page = await browser.newPage();6 const recorderApp = await compile({7 launchOptions: {8 },9 });10 await recorderApp.listen(3000);11})();12const { chromium } = require('playwright');13const { expect } = require('@playwright/test');14(async () => {15 const browser = await chromium.launch();16 const context = await browser.newContext();17 const page = await context.newPage();
Using AI Code Generation
1const { chromium, firefox } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 const compiled = await page._client.send('Page.compileScript', {6 expression: 'document.querySelector("title").innerText',7 });8 await page._client.send('Runtime.runScript', {9 });10 await browser.close();11})();12const { chromium, firefox } = require('playwright');13(async () => {14 const browser = await chromium.launch();15 const page = await browser.newPage();16 const title = await page.evaluate(() => {17 return document.querySelector('title').innerText;18 });19 console.log(title);20 await browser.close();21})();22const { chromium, firefox } = require('playwright');23(async () => {24 const browser = await chromium.launch();25 const page = await browser.newPage();26 const title = await page.evaluate(() => {27 return document.querySelector('title').innerText;28 });29 console.log(title);30 await browser.close();31})();32const { chromium, firefox } = require('playwright');33(async () => {34 const browser = await chromium.launch();35 const page = await browser.newPage();36 await page.addScriptTag({37 content: 'document.querySelector("title").innerText',38 });39 await browser.close();40})();41const { chromium, firefox
Using AI Code Generation
1const playwright = require('playwright');2const { compile } = require('playwright/lib/server/compile');3const path = require('path');4const fs = require('fs');5const { chromium } = require('playwright');6(async () => {7 const compiledPath = await compile(`8 const { chromium } = require('playwright');9 (async () => {10 const browser = await chromium.launch();11 const page = await browser.newPage();12 await page.screenshot({ path: 'google.png' });13 await browser.close();14 })();15 `);16 require(compiledPath);17})();18const playwright = require('playwright');19const { compile } = require('playwright/lib/server/compile');20const path = require('path');21const fs = require('fs');22const { chromium } = require('playwright');23(async () => {24 const compiledPath = await compile(`25 const { chromium } = require('playwright');26 (async () => {27 const browser = await chromium.launch();28 const page = await browser.newPage();29 await page.screenshot({ path: 'google.png' });30 await browser.close();31 })();32 `);33 require(compiledPath);34})();35const playwright = require('playwright');36const { compile } = require('playwright/lib/server/compile');37const path = require('path');38const fs = require('fs');39const { chromium } = require('playwright');40(async () => {41 const compiledPath = await compile(`42 const { chromium } = require('playwright');43 (async () => {44 const browser = await chromium.launch();45 const page = await browser.newPage();46 await page.screenshot({ path: 'google.png' });47 await browser.close();48 })();49 `);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!