Home | History | Annotate | Download | only in wasm
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Flags: --expose-wasm
      6 
      7 function EmptyTest() {
      8   "use asm";
      9   function caller() {
     10     empty();
     11     return 11;
     12   }
     13   function empty() {
     14   }
     15   return {caller: caller};
     16 }
     17 
     18 assertEquals(11, _WASMEXP_.asmCompileRun(EmptyTest.toString()));
     19 
     20 function IntTest() {
     21   "use asm";
     22   function sum(a, b) {
     23     a = a|0;
     24     b = b|0;
     25     var c = (b + 1)|0
     26     var d = 3.0;
     27     var e = d | 0;  // double conversion
     28     return (a + c + 1)|0;
     29   }
     30 
     31   function caller() {
     32     return sum(77,22) | 0;
     33   }
     34 
     35   return {caller: caller};
     36 }
     37 
     38 assertEquals(101, _WASMEXP_.asmCompileRun(IntTest.toString()));
     39 
     40 function Float64Test() {
     41   "use asm";
     42   function sum(a, b) {
     43     a = +a;
     44     b = +b;
     45     return +(a + b);
     46   }
     47 
     48   function caller() {
     49     var a = +sum(70.1,10.2);
     50     var ret = 0|0;
     51     if (a == 80.3) {
     52       ret = 1|0;
     53     } else {
     54       ret = 0|0;
     55     }
     56     return ret|0;
     57   }
     58 
     59   return {caller: caller};
     60 }
     61 
     62 assertEquals(1, _WASMEXP_.asmCompileRun(Float64Test.toString()));
     63 
     64 function BadModule() {
     65   "use asm";
     66   function caller(a, b) {
     67     a = a|0;
     68     b = b+0;
     69     var c = (b + 1)|0
     70     return (a + c + 1)|0;
     71   }
     72 
     73   function caller() {
     74     return call(1, 2)|0;
     75   }
     76 
     77   return {caller: caller};
     78 }
     79 
     80 assertThrows(function() {
     81   _WASMEXP_.asmCompileRun(BadModule.toString())
     82 });
     83 
     84 function TestReturnInBlock() {
     85   "use asm";
     86 
     87   function caller() {
     88     if(1) {
     89       {
     90         {
     91           return 1;
     92         }
     93       }
     94     }
     95     return 0;
     96   }
     97 
     98   return {caller: caller};
     99 }
    100 
    101 assertEquals(1, _WASMEXP_.asmCompileRun(TestReturnInBlock.toString()));
    102 
    103 function TestWhileSimple() {
    104   "use asm";
    105 
    106   function caller() {
    107     var x = 0;
    108     while(x < 5) {
    109       x = (x + 1)|0;
    110     }
    111     return x|0;
    112   }
    113 
    114   return {caller: caller};
    115 }
    116 
    117 assertEquals(5, _WASMEXP_.asmCompileRun(TestWhileSimple.toString()));
    118 
    119 function TestWhileWithoutBraces() {
    120   "use asm";
    121 
    122   function caller() {
    123     var x = 0;
    124     while(x <= 3)
    125       x = (x + 1)|0;
    126     return x|0;
    127   }
    128 
    129   return {caller: caller};
    130 }
    131 
    132 assertEquals(4, _WASMEXP_.asmCompileRun(TestWhileWithoutBraces.toString()));
    133 
    134 function TestReturnInWhile() {
    135   "use asm";
    136 
    137   function caller() {
    138     var x = 0;
    139     while(x < 10) {
    140       x = (x + 6)|0;
    141       return x|0;
    142     }
    143     return x|0;
    144   }
    145 
    146   return {caller: caller};
    147 }
    148 
    149 assertEquals(6, _WASMEXP_.asmCompileRun(TestReturnInWhile.toString()));
    150 
    151 function TestReturnInWhileWithoutBraces() {
    152   "use asm";
    153 
    154   function caller() {
    155     var x = 0;
    156     while(x < 5)
    157       return 7;
    158     return x|0;
    159   }
    160 
    161   return {caller: caller};
    162 }
    163 
    164 assertEquals(7, _WASMEXP_.asmCompileRun(TestReturnInWhileWithoutBraces.toString()));
    165 
    166 function TestBreakInWhile() {
    167   "use asm";
    168 
    169   function caller() {
    170     while(1) {
    171       break;
    172     }
    173     return 8;
    174   }
    175 
    176   return {caller: caller};
    177 }
    178 
    179 assertEquals(8, _WASMEXP_.asmCompileRun(TestBreakInWhile.toString()));
    180 
    181 function TestBreakInNestedWhile() {
    182   "use asm";
    183 
    184   function caller() {
    185     var x = 1.0;
    186     while(x < 1.5) {
    187       while(1)
    188         break;
    189       x = +(x + 0.25);
    190     }
    191     var ret = 0;
    192     if (x == 1.5) {
    193       ret = 9;
    194     }
    195     return ret|0;
    196   }
    197 
    198   return {caller: caller};
    199 }
    200 
    201 assertEquals(9, _WASMEXP_.asmCompileRun(TestBreakInNestedWhile.toString()));
    202 
    203 function TestBreakInBlock() {
    204   "use asm";
    205 
    206   function caller() {
    207     var x = 0;
    208     abc: {
    209       x = 10;
    210       if (x == 10) {
    211         break abc;
    212       }
    213       x = 20;
    214     }
    215     return x|0;
    216   }
    217 
    218   return {caller: caller};
    219 }
    220 
    221 assertEquals(10, _WASMEXP_.asmCompileRun(TestBreakInBlock.toString()));
    222 
    223 function TestBreakInNamedWhile() {
    224   "use asm";
    225 
    226   function caller() {
    227     var x = 0;
    228     outer: while (1) {
    229       x = (x + 1)|0;
    230       while (x == 11) {
    231         break outer;
    232       }
    233     }
    234     return x|0;
    235   }
    236 
    237   return {caller: caller};
    238 }
    239 
    240 assertEquals(11, _WASMEXP_.asmCompileRun(TestBreakInNamedWhile.toString()));
    241 
    242 function TestContinue() {
    243   "use asm";
    244 
    245   function caller() {
    246     var x = 5;
    247     var ret = 0;
    248     while (x >= 0) {
    249       x = (x - 1)|0;
    250       if (x == 2) {
    251         continue;
    252       }
    253       ret = (ret - 1)|0;
    254     }
    255     return ret|0;
    256   }
    257 
    258   return {caller: caller};
    259 }
    260 
    261 assertEquals(-5, _WASMEXP_.asmCompileRun(TestContinue.toString()));
    262 
    263 function TestContinueInNamedWhile() {
    264   "use asm";
    265 
    266   function caller() {
    267     var x = 5;
    268     var y = 0;
    269     var ret = 0;
    270     outer: while (x > 0) {
    271       x = (x - 1)|0;
    272       y = 0;
    273       while (y < 5) {
    274         if (x == 3) {
    275           continue outer;
    276         }
    277         ret = (ret + 1)|0;
    278         y = (y + 1)|0;
    279       }
    280     }
    281     return ret|0;
    282   }
    283 
    284   return {caller: caller};
    285 }
    286 
    287 assertEquals(20, _WASMEXP_.asmCompileRun(TestContinueInNamedWhile.toString()));
    288 
    289 function TestNot() {
    290   "use asm";
    291 
    292   function caller() {
    293     var a = !(2 > 3);
    294     return a | 0;
    295   }
    296 
    297   return {caller:caller};
    298 }
    299 
    300 assertEquals(1, _WASMEXP_.asmCompileRun(TestNot.toString()));
    301 
    302 function TestNotEquals() {
    303   "use asm";
    304 
    305   function caller() {
    306     var a = 3;
    307     if (a != 2) {
    308       return 21;
    309     }
    310     return 0;
    311   }
    312 
    313   return {caller:caller};
    314 }
    315 
    316 assertEquals(21, _WASMEXP_.asmCompileRun(TestNotEquals.toString()));
    317 
    318 function TestUnsignedComparison() {
    319   "use asm";
    320 
    321   function caller() {
    322     var a = 0xffffffff;
    323     if ((a>>>0) > (0>>>0)) {
    324       return 22;
    325     }
    326     return 0;
    327   }
    328 
    329   return {caller:caller};
    330 }
    331 
    332 assertEquals(22, _WASMEXP_.asmCompileRun(TestUnsignedComparison.toString()));
    333 
    334 function TestMixedAdd() {
    335   "use asm";
    336 
    337   function caller() {
    338     var a = 0x80000000;
    339     var b = 0x7fffffff;
    340     var c = 0;
    341     c = ((a>>>0) + b)|0;
    342     if ((c >>> 0) > (0>>>0)) {
    343       if (c < 0) {
    344         return 23;
    345       }
    346     }
    347     return 0;
    348   }
    349 
    350   return {caller:caller};
    351 }
    352 
    353 assertEquals(23, _WASMEXP_.asmCompileRun(TestMixedAdd.toString()));
    354 
    355 function TestInt32HeapAccess(stdlib, foreign, buffer) {
    356   "use asm";
    357 
    358   var m = new stdlib.Int32Array(buffer);
    359   function caller() {
    360     var i = 4;
    361 
    362     m[0] = (i + 1) | 0;
    363     m[i >> 2] = ((m[0]|0) + 1) | 0;
    364     m[2] = ((m[i >> 2]|0) + 1) | 0;
    365     return m[2] | 0;
    366   }
    367 
    368   return {caller: caller};
    369 }
    370 
    371 assertEquals(7, _WASMEXP_.asmCompileRun(TestInt32HeapAccess.toString()));
    372 
    373 function TestHeapAccessIntTypes() {
    374   var types = [
    375     ['Int8Array', '>> 0'],
    376     ['Uint8Array', '>> 0'],
    377     ['Int16Array', '>> 1'],
    378     ['Uint16Array', '>> 1'],
    379     ['Int32Array', '>> 2'],
    380     ['Uint32Array', '>> 2'],
    381   ];
    382   for (var i = 0; i < types.length; i++) {
    383     var code = TestInt32HeapAccess.toString();
    384     code = code.replace('Int32Array', types[i][0]);
    385     code = code.replace(/>> 2/g, types[i][1]);
    386     assertEquals(7, _WASMEXP_.asmCompileRun(code));
    387   }
    388 }
    389 
    390 TestHeapAccessIntTypes();
    391 
    392 function TestFloatHeapAccess(stdlib, foreign, buffer) {
    393   "use asm";
    394 
    395   var f32 = new stdlib.Float32Array(buffer);
    396   var f64 = new stdlib.Float64Array(buffer);
    397   var fround = stdlib.Math.fround;
    398   function caller() {
    399     var i = 8;
    400     var j = 8;
    401     var v = 6.0;
    402 
    403     // TODO(bradnelson): Add float32 when asm-wasm supports it.
    404     f64[2] = v + 1.0;
    405     f64[i >> 3] = +f64[2] + 1.0;
    406     f64[j >> 3] = +f64[j >> 3] + 1.0;
    407     i = +f64[i >> 3] == 9.0;
    408     return i|0;
    409   }
    410 
    411   return {caller: caller};
    412 }
    413 
    414 assertEquals(1, _WASMEXP_.asmCompileRun(TestFloatHeapAccess.toString()));
    415 
    416 function TestConvertI32() {
    417   "use asm";
    418 
    419   function caller() {
    420     var a = 1.5;
    421     if ((~~(a + a)) == 3) {
    422       return 24;
    423     }
    424     return 0;
    425   }
    426 
    427   return {caller:caller};
    428 }
    429 
    430 assertEquals(24, _WASMEXP_.asmCompileRun(TestConvertI32.toString()));
    431 
    432 function TestConvertF64FromInt() {
    433   "use asm";
    434 
    435   function caller() {
    436     var a = 1;
    437     if ((+(a + a)) > 1.5) {
    438       return 25;
    439     }
    440     return 0;
    441   }
    442 
    443   return {caller:caller};
    444 }
    445 
    446 assertEquals(25, _WASMEXP_.asmCompileRun(TestConvertF64FromInt.toString()));
    447 
    448 function TestConvertF64FromUnsigned() {
    449   "use asm";
    450 
    451   function caller() {
    452     var a = 0xffffffff;
    453     if ((+(a>>>0)) > 0.0) {
    454       if((+a) < 0.0) {
    455         return 26;
    456       }
    457     }
    458     return 0;
    459   }
    460 
    461   return {caller:caller};
    462 }
    463 
    464 assertEquals(26, _WASMEXP_.asmCompileRun(TestConvertF64FromUnsigned.toString()));
    465 
    466 function TestModInt() {
    467   "use asm";
    468 
    469   function caller() {
    470     var a = -83;
    471     var b = 28;
    472     return ((a|0)%(b|0))|0;
    473   }
    474 
    475   return {caller:caller};
    476 }
    477 
    478 assertEquals(-27, _WASMEXP_.asmCompileRun(TestModInt.toString()));
    479 
    480 function TestModUnsignedInt() {
    481   "use asm";
    482 
    483   function caller() {
    484     var a = 0x80000000;  //2147483648
    485     var b = 10;
    486     return ((a>>>0)%(b>>>0))|0;
    487   }
    488 
    489   return {caller:caller};
    490 }
    491 
    492 assertEquals(8, _WASMEXP_.asmCompileRun(TestModUnsignedInt.toString()));
    493 
    494 function TestModDouble() {
    495   "use asm";
    496 
    497   function caller() {
    498     var a = 5.25;
    499     var b = 2.5;
    500     if (a%b == 0.25) {
    501       return 28;
    502     }
    503     return 0;
    504   }
    505 
    506   return {caller:caller};
    507 }
    508 
    509 assertEquals(28, _WASMEXP_.asmCompileRun(TestModDouble.toString()));
    510 
    511 /*
    512 TODO: Fix parsing of negative doubles
    513       Fix code to use trunc instead of casts
    514 function TestModDoubleNegative() {
    515   "use asm";
    516 
    517   function caller() {
    518     var a = -34359738368.25;
    519     var b = 2.5;
    520     if (a%b == -0.75) {
    521       return 28;
    522     }
    523     return 0;
    524   }
    525 
    526   return {caller:caller};
    527 }
    528 
    529 assertEquals(28, _WASMEXP_.asmCompileRun(TestModDoubleNegative.toString()));
    530 */
    531 
    532 function TestNamedFunctions() {
    533   "use asm";
    534 
    535   var a = 0.0;
    536   var b = 0.0;
    537 
    538   function add() {
    539     return +(a + b);
    540   }
    541 
    542   function init() {
    543     a = 43.25;
    544     b = 34.25;
    545   }
    546 
    547   return {init:init,
    548           add:add};
    549 }
    550 
    551 var module = _WASMEXP_.instantiateModuleFromAsm(TestNamedFunctions.toString());
    552 module.init();
    553 assertEquals(77.5, module.add());
    554 
    555 function TestGlobalsWithInit() {
    556   "use asm";
    557 
    558   var a = 43.25;
    559   var b = 34.25;
    560 
    561   function add() {
    562     return +(a + b);
    563   }
    564 
    565   return {add:add};
    566 }
    567 
    568 var module = _WASMEXP_.instantiateModuleFromAsm(TestGlobalsWithInit.toString());
    569 module.__init__();
    570 assertEquals(77.5, module.add());
    571 
    572 function TestForLoop() {
    573   "use asm"
    574 
    575   function caller() {
    576     var ret = 0;
    577     var i = 0;
    578     for (i = 2; i <= 10; i = (i+1)|0) {
    579       ret = (ret + i) | 0;
    580     }
    581     return ret|0;
    582   }
    583 
    584   return {caller:caller};
    585 }
    586 
    587 assertEquals(54, _WASMEXP_.asmCompileRun(TestForLoop.toString()));
    588 
    589 function TestForLoopWithoutInit() {
    590   "use asm"
    591 
    592   function caller() {
    593     var ret = 0;
    594     var i = 0;
    595     for (; i < 10; i = (i+1)|0) {
    596       ret = (ret + 10) | 0;
    597     }
    598     return ret|0;
    599   }
    600 
    601   return {caller:caller};
    602 }
    603 
    604 assertEquals(100, _WASMEXP_.asmCompileRun(TestForLoopWithoutInit.toString()));
    605 
    606 function TestForLoopWithoutCondition() {
    607   "use asm"
    608 
    609   function caller() {
    610     var ret = 0;
    611     var i = 0;
    612     for (i=1;; i = (i+1)|0) {
    613       ret = (ret + i) | 0;
    614       if (i == 11) {
    615         break;
    616       }
    617     }
    618     return ret|0;
    619   }
    620 
    621   return {caller:caller};
    622 }
    623 
    624 assertEquals(66, _WASMEXP_.asmCompileRun(TestForLoopWithoutCondition.toString()));
    625 
    626 function TestForLoopWithoutNext() {
    627   "use asm"
    628 
    629   function caller() {
    630     var i = 0;
    631     for (i=1; i < 41;) {
    632       i = (i + 1) | 0;
    633     }
    634     return i|0;
    635   }
    636 
    637   return {caller:caller};
    638 }
    639 
    640 assertEquals(41, _WASMEXP_.asmCompileRun(TestForLoopWithoutNext.toString()));
    641 
    642 function TestForLoopWithoutBody() {
    643   "use asm"
    644 
    645   function caller() {
    646     var i = 0;
    647     for (i=1; i < 45 ; i = (i+1)|0) {
    648     }
    649     return i|0;
    650   }
    651 
    652   return {caller:caller};
    653 }
    654 
    655 assertEquals(45, _WASMEXP_.asmCompileRun(TestForLoopWithoutBody.toString()));
    656 
    657 function TestDoWhile() {
    658   "use asm"
    659 
    660   function caller() {
    661     var i = 0;
    662     var ret = 21;
    663     do {
    664       ret = (ret + ret)|0;
    665       i = (i + 1)|0;
    666     } while (i < 2);
    667     return ret|0;
    668   }
    669 
    670   return {caller:caller};
    671 }
    672 
    673 assertEquals(84, _WASMEXP_.asmCompileRun(TestDoWhile.toString()));
    674 
    675 function TestConditional() {
    676   "use asm"
    677 
    678   function caller() {
    679     var x = 1;
    680     return ((x > 0) ? 41 : 71)|0;
    681   }
    682 
    683   return {caller:caller};
    684 }
    685 
    686 assertEquals(41, _WASMEXP_.asmCompileRun(TestConditional.toString()));
    687 
    688 function TestSwitch() {
    689   "use asm"
    690 
    691   function caller() {
    692     var ret = 0;
    693     var x = 7;
    694     switch (x) {
    695       case 1: return 0;
    696       case 7: {
    697         ret = 12;
    698         break;
    699       }
    700       default: return 0;
    701     }
    702     switch (x) {
    703       case 1: return 0;
    704       case 8: return 0;
    705       default: ret = (ret + 11)|0;
    706     }
    707     return ret|0;
    708   }
    709 
    710   return {caller:caller};
    711 }
    712 
    713 assertEquals(23, _WASMEXP_.asmCompileRun(TestSwitch.toString()));
    714 
    715 function TestSwitchFallthrough() {
    716   "use asm"
    717 
    718   function caller() {
    719     var x = 17;
    720     var ret = 0;
    721     switch (x) {
    722       case 17:
    723       case 14: ret = 39;
    724       case 1: ret = (ret + 3)|0;
    725       case 4: break;
    726       default: ret = (ret + 1)|0;
    727     }
    728     return ret|0;
    729   }
    730 
    731   return {caller:caller};
    732 }
    733 
    734 assertEquals(42, _WASMEXP_.asmCompileRun(TestSwitchFallthrough.toString()));
    735 
    736 function TestNestedSwitch() {
    737   "use asm"
    738 
    739   function caller() {
    740     var x = 3;
    741     var y = -13;
    742     switch (x) {
    743       case 1: return 0;
    744       case 3: {
    745         switch (y) {
    746           case 2: return 0;
    747           case -13: return 43;
    748           default: return 0;
    749         }
    750       }
    751       default: return 0;
    752     }
    753     return 0;
    754   }
    755 
    756   return {caller:caller};
    757 }
    758 
    759 assertEquals(43, _WASMEXP_.asmCompileRun(TestNestedSwitch.toString()));
    760 
    761 function TestInitFunctionWithNoGlobals() {
    762   "use asm";
    763   function caller() {
    764     return 51;
    765   }
    766   return {caller};
    767 }
    768 
    769 var module = _WASMEXP_.instantiateModuleFromAsm(
    770     TestInitFunctionWithNoGlobals.toString());
    771 module.__init__();
    772 assertEquals(51, module.caller());
    773 
    774 function TestExportNameDifferentFromFunctionName() {
    775   "use asm";
    776   function caller() {
    777     return 55;
    778   }
    779   return {alt_caller:caller};
    780 }
    781 
    782 var module = _WASMEXP_.instantiateModuleFromAsm(
    783     TestExportNameDifferentFromFunctionName.toString());
    784 module.__init__();
    785 assertEquals(55, module.alt_caller());
    786