asm-wasm.js 21.7 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --validate-asm --allow-natives-syntax
6

7
var stdlib = this;
8
let kMinHeapSize = 4096;
9

10
function assertValidAsm(func) {
11
  assertTrue(%IsAsmWasmCode(func), "must be valid asm code");
12 13
}

14 15
function assertWasm(expected, func, ffi) {
  print("Testing " + func.name + "...");
16
  assertEquals(
17
      expected, func(stdlib, ffi, new ArrayBuffer(kMinHeapSize)).caller());
18
  assertValidAsm(func);
19 20
}

21
function EmptyTest(a, b, c) {
22 23 24 25 26 27 28 29 30 31
  "use asm";
  function caller() {
    empty();
    return 11;
  }
  function empty() {
  }
  return {caller: caller};
}

32
assertWasm(11, EmptyTest);
33

34
function VoidReturnTest(a, b, c) {
35 36 37 38 39 40 41 42 43 44 45 46 47
  "use asm";
  function caller() {
    empty();
    return 19;
  }
  function empty() {
    var x = 0;
    if (x) return;
  }
  return {caller: caller};
}

assertWasm(19, VoidReturnTest);
48

49
function IntTest(a, b, c) {
50 51 52 53
  "use asm";
  function sum(a, b) {
    a = a|0;
    b = b|0;
54
    var c = 0;
55
    var d = 3.0;
56 57 58
    var e = 0;
    e = ~~d;  // double conversion
    c = (b + 1)|0
59 60 61 62 63 64 65 66 67 68
    return (a + c + 1)|0;
  }

  function caller() {
    return sum(77,22) | 0;
  }

  return {caller: caller};
}

69
assertWasm(101,IntTest);
70

71

72 73 74 75 76 77 78 79 80
function Float64Test() {
  "use asm";
  function sum(a, b) {
    a = +a;
    b = +b;
    return +(a + b);
  }

  function caller() {
81
    var a = 0.0;
82
    var ret = 0;
83
    a = +sum(70.1,10.2);
84 85 86 87 88 89 90 91 92 93 94
    if (a == 80.3) {
      ret = 1|0;
    } else {
      ret = 0|0;
    }
    return ret|0;
  }

  return {caller: caller};
}

95
assertWasm(1, Float64Test);
96

97

98 99 100 101 102
function BadModule() {
  "use asm";
  function caller(a, b) {
    a = a|0;
    b = b+0;
103 104
    var c = 0;
    c = (b + 1)|0
105 106 107 108 109 110 111 112 113 114
    return (a + c + 1)|0;
  }

  function caller() {
    return call(1, 2)|0;
  }

  return {caller: caller};
}

115
assertFalse(%IsAsmWasmCode(BadModule));
116

117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
function TestReturnInBlock() {
  "use asm";

  function caller() {
    if(1) {
      {
        {
          return 1;
        }
      }
    }
    return 0;
  }

  return {caller: caller};
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
assertWasm(1, TestReturnInBlock);


function TestAddSimple() {
  "use asm";

  function caller() {
    var x = 0;
    x = (x + 1)|0;
    return x|0;
  }

  return {caller: caller};
}

assertWasm(1, TestAddSimple);
151

152

153 154 155 156 157
function TestWhileSimple() {
  "use asm";

  function caller() {
    var x = 0;
158
    while((x|0) < 5) {
159 160 161 162 163 164 165 166
      x = (x + 1)|0;
    }
    return x|0;
  }

  return {caller: caller};
}

167
assertWasm(5, TestWhileSimple);
168

169

170 171 172 173 174
function TestWhileWithoutBraces() {
  "use asm";

  function caller() {
    var x = 0;
175
    while((x|0) <= 3)
176 177 178 179 180 181 182
      x = (x + 1)|0;
    return x|0;
  }

  return {caller: caller};
}

183
assertWasm(4, TestWhileWithoutBraces);
184

185

186 187 188 189 190
function TestReturnInWhile() {
  "use asm";

  function caller() {
    var x = 0;
191
    while((x|0) < 10) {
192 193 194 195 196 197 198 199 200
      x = (x + 6)|0;
      return x|0;
    }
    return x|0;
  }

  return {caller: caller};
}

201
assertWasm(6, TestReturnInWhile);
202

203

204 205 206 207 208
function TestReturnInWhileWithoutBraces() {
  "use asm";

  function caller() {
    var x = 0;
209
    while((x|0) < 5)
210 211 212 213 214 215 216
      return 7;
    return x|0;
  }

  return {caller: caller};
}

217
assertWasm(7, TestReturnInWhileWithoutBraces);
218

219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
function TestBreakInIf() {
  "use asm";

  function caller() {
    label: {
      if(1) break label;
      return 11;
    }
    return 12;
  }

  return {caller: caller};
}

assertWasm(12, TestBreakInIf);

function TestBreakInIfInDoWhileFalse() {
  "use asm";

  function caller() {
    do {
      if(1) break;
      return 11;
    } while(0);
    return 12;
  }

  return {caller: caller};
}

assertWasm(12, TestBreakInIfInDoWhileFalse);

function TestBreakInElse() {
  "use asm";

  function caller() {
    do {
      if(0) ;
      else break;
      return 14;
    } while(0);
    return 15;
  }

  return {caller: caller};
}

assertWasm(15, TestBreakInElse);

269 270 271 272 273 274 275 276 277 278 279 280 281
function TestBreakInWhile() {
  "use asm";

  function caller() {
    while(1) {
      break;
    }
    return 8;
  }

  return {caller: caller};
}

282
assertWasm(8, TestBreakInWhile);
283

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

function TestBreakInIfInWhile() {
  "use asm";

  function caller() {
    while(1) {
      if (1) break;
      else break;
    }
    return 8;
  }

  return {caller: caller};
}

assertWasm(8, TestBreakInIfInWhile);
300

301 302 303 304 305
function TestBreakInNestedWhile() {
  "use asm";

  function caller() {
    var x = 1.0;
306
    var ret = 0;
307 308 309 310 311 312 313 314 315 316 317 318 319 320
    while(x < 1.5) {
      while(1)
        break;
      x = +(x + 0.25);
    }
    if (x == 1.5) {
      ret = 9;
    }
    return ret|0;
  }

  return {caller: caller};
}

321
assertWasm(9, TestBreakInNestedWhile);
322

323

324 325 326 327 328 329 330
function TestBreakInBlock() {
  "use asm";

  function caller() {
    var x = 0;
    abc: {
      x = 10;
331
      if ((x|0) == 10) {
332 333 334 335 336 337 338 339 340 341
        break abc;
      }
      x = 20;
    }
    return x|0;
  }

  return {caller: caller};
}

342
assertWasm(10, TestBreakInBlock);
343

344

345 346 347 348 349 350 351
function TestBreakInNamedWhile() {
  "use asm";

  function caller() {
    var x = 0;
    outer: while (1) {
      x = (x + 1)|0;
352
      while ((x|0) == 11) {
353 354 355 356 357 358 359 360 361
        break outer;
      }
    }
    return x|0;
  }

  return {caller: caller};
}

362
assertWasm(11, TestBreakInNamedWhile);
363

364

365 366 367 368 369 370
function TestContinue() {
  "use asm";

  function caller() {
    var x = 5;
    var ret = 0;
371
    while ((x|0) >= 0) {
372
      x = (x - 1)|0;
373
      if ((x|0) == 2) {
374 375 376 377 378 379 380 381 382 383
        continue;
      }
      ret = (ret - 1)|0;
    }
    return ret|0;
  }

  return {caller: caller};
}

384
assertWasm(-5, TestContinue);
385

386

387 388 389 390 391 392 393
function TestContinueInNamedWhile() {
  "use asm";

  function caller() {
    var x = 5;
    var y = 0;
    var ret = 0;
394
    outer: while ((x|0) > 0) {
395 396
      x = (x - 1)|0;
      y = 0;
397 398
      while ((y|0) < 5) {
        if ((x|0) == 3) {
399 400 401 402 403 404 405 406 407 408 409 410
          continue outer;
        }
        ret = (ret + 1)|0;
        y = (y + 1)|0;
      }
    }
    return ret|0;
  }

  return {caller: caller};
}

411
assertWasm(20, TestContinueInNamedWhile);
412

413

414 415 416 417 418 419
function TestContinueInDoWhileFalse() {
  "use asm";

  function caller() {
    do {
      continue;
420
    } while (0);
421 422 423 424 425 426 427 428 429
    return 47;
  }

  return {caller: caller};
}

assertWasm(47, TestContinueInDoWhileFalse);


430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
function TestContinueInForLoop() {
  "use asm";

  function caller() {
    var i = 0;
    for (; (i|0) < 10; i = (i+1)|0) {
      continue;
    }
    return 4711;
  }

  return {caller: caller};
}

assertWasm(4711, TestContinueInForLoop);


447 448 449 450
function TestNot() {
  "use asm";

  function caller() {
451 452
    var a = 0;
    a = !(2 > 3);
453 454 455 456 457 458
    return a | 0;
  }

  return {caller:caller};
}

459
assertWasm(1, TestNot);
460

461

462 463 464 465 466
function TestNotEquals() {
  "use asm";

  function caller() {
    var a = 3;
467
    if ((a|0) != 2) {
468 469 470 471 472 473 474 475
      return 21;
    }
    return 0;
  }

  return {caller:caller};
}

476
assertWasm(21, TestNotEquals);
477

478

479 480 481 482 483 484 485 486 487 488 489 490 491 492
function TestUnsignedComparison() {
  "use asm";

  function caller() {
    var a = 0xffffffff;
    if ((a>>>0) > (0>>>0)) {
      return 22;
    }
    return 0;
  }

  return {caller:caller};
}

493
assertWasm(22, TestUnsignedComparison);
494

495

496 497 498 499 500 501 502 503 504
function TestMixedAdd() {
  "use asm";

  function caller() {
    var a = 0x80000000;
    var b = 0x7fffffff;
    var c = 0;
    c = ((a>>>0) + b)|0;
    if ((c >>> 0) > (0>>>0)) {
505
      if ((c|0) < 0) {
506 507 508 509 510 511 512 513 514
        return 23;
      }
    }
    return 0;
  }

  return {caller:caller};
}

515
assertWasm(23, TestMixedAdd);
516

517

518 519


520 521 522 523 524 525 526 527 528 529 530 531 532 533
function TestConvertI32() {
  "use asm";

  function caller() {
    var a = 1.5;
    if ((~~(a + a)) == 3) {
      return 24;
    }
    return 0;
  }

  return {caller:caller};
}

534
assertWasm(24, TestConvertI32);
535

536

537 538 539 540 541
function TestConvertF64FromInt() {
  "use asm";

  function caller() {
    var a = 1;
542
    if ((+((a + a)|0)) > 1.5) {
543 544 545 546 547 548 549 550
      return 25;
    }
    return 0;
  }

  return {caller:caller};
}

551
assertWasm(25, TestConvertF64FromInt);
552

553

554 555 556 557 558 559
function TestConvertF64FromUnsigned() {
  "use asm";

  function caller() {
    var a = 0xffffffff;
    if ((+(a>>>0)) > 0.0) {
560
      if((+(a|0)) < 0.0) {
561 562 563 564 565 566 567 568 569
        return 26;
      }
    }
    return 0;
  }

  return {caller:caller};
}

570
assertWasm(26, TestConvertF64FromUnsigned);
571

572

573 574 575 576 577 578 579 580 581 582 583 584
function TestModInt() {
  "use asm";

  function caller() {
    var a = -83;
    var b = 28;
    return ((a|0)%(b|0))|0;
  }

  return {caller:caller};
}

585
assertWasm(-27,TestModInt);
586

587

588 589 590 591 592 593 594 595 596 597 598 599
function TestModUnsignedInt() {
  "use asm";

  function caller() {
    var a = 0x80000000;  //2147483648
    var b = 10;
    return ((a>>>0)%(b>>>0))|0;
  }

  return {caller:caller};
}

600
assertWasm(8, TestModUnsignedInt);
601

602

603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
function TestModDouble() {
  "use asm";

  function caller() {
    var a = 5.25;
    var b = 2.5;
    if (a%b == 0.25) {
      return 28;
    }
    return 0;
  }

  return {caller:caller};
}

618
assertWasm(28, TestModDouble);
619

620

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
function TestModDoubleNegative() {
  "use asm";

  function caller() {
    var a = -34359738368.25;
    var b = 2.5;
    if (a%b == -0.75) {
      return 28;
    }
    return 0;
  }

  return {caller:caller};
}

636
assertWasm(28, TestModDoubleNegative);
637

638

639
(function () {
640
function TestNamedFunctions() {
641 642 643 644 645 646
  "use asm";

  var a = 0.0;
  var b = 0.0;

  function add() {
647
    return +(a + b);
648 649
  }

650 651 652
  function init() {
    a = 43.25;
    b = 34.25;
653 654
  }

655 656
  return {init:init,
          add:add};
657 658
}

659 660 661
var module_decl = eval('(' + TestNamedFunctions.toString() + ')');
var module = module_decl(stdlib);
assertValidAsm(module_decl);
662 663
module.init();
assertEquals(77.5, module.add());
664
})();
665

666

667
(function () {
668 669 670
function TestGlobalsWithInit() {
  "use asm";

671 672
  var a = 43.25;
  var b = 34.25;
673 674 675 676 677

  function add() {
    return +(a + b);
  }

678
  return {add:add};
679 680
}

681 682 683
var module_decl = eval('(' + TestGlobalsWithInit.toString() + ')');
var module = module_decl(stdlib);
assertValidAsm(module_decl);
684
assertEquals(77.5, module.add());
685
})();
686

687 688 689 690 691 692
function TestForLoop() {
  "use asm"

  function caller() {
    var ret = 0;
    var i = 0;
693
    for (i = 2; (i|0) <= 10; i = (i+1)|0) {
694 695 696 697 698 699 700 701
      ret = (ret + i) | 0;
    }
    return ret|0;
  }

  return {caller:caller};
}

702
assertWasm(54, TestForLoop);
703

704

705 706 707 708 709 710
function TestForLoopWithoutInit() {
  "use asm"

  function caller() {
    var ret = 0;
    var i = 0;
711
    for (; (i|0) < 10; i = (i+1)|0) {
712 713 714 715 716 717 718 719
      ret = (ret + 10) | 0;
    }
    return ret|0;
  }

  return {caller:caller};
}

720
assertWasm(100,TestForLoopWithoutInit);
721

722

723 724 725 726 727 728 729 730
function TestForLoopWithoutCondition() {
  "use asm"

  function caller() {
    var ret = 0;
    var i = 0;
    for (i=1;; i = (i+1)|0) {
      ret = (ret + i) | 0;
731
      if ((i|0) == 11) {
732 733 734 735 736 737 738 739 740
        break;
      }
    }
    return ret|0;
  }

  return {caller:caller};
}

741
assertWasm(66, TestForLoopWithoutCondition);
742

743

744 745 746 747 748
function TestForLoopWithoutNext() {
  "use asm"

  function caller() {
    var i = 0;
749
    for (i=1; (i|0) < 41;) {
750 751 752 753 754 755 756 757
      i = (i + 1) | 0;
    }
    return i|0;
  }

  return {caller:caller};
}

758
assertWasm(41, TestForLoopWithoutNext);
759

760

761 762 763 764 765
function TestForLoopWithoutBody() {
  "use asm"

  function caller() {
    var i = 0;
766
    for (i=1; (i|0) < 45 ; i = (i+1)|0) {
767 768 769 770 771 772 773
    }
    return i|0;
  }

  return {caller:caller};
}

774
assertWasm(45, TestForLoopWithoutBody);
775

776

777 778 779 780 781 782 783 784 785
function TestDoWhile() {
  "use asm"

  function caller() {
    var i = 0;
    var ret = 21;
    do {
      ret = (ret + ret)|0;
      i = (i + 1)|0;
786
    } while ((i|0) < 2);
787 788 789 790 791 792
    return ret|0;
  }

  return {caller:caller};
}

793
assertWasm(84, TestDoWhile);
794

795

796 797 798 799 800
function TestConditional() {
  "use asm"

  function caller() {
    var x = 1;
801
    return (((x|0) > 0) ? 41 : 71)|0;
802 803 804 805 806
  }

  return {caller:caller};
}

807
assertWasm(41, TestConditional);
aseemgarg's avatar
aseemgarg committed
808

809

810 811 812 813 814
function TestInitFunctionWithNoGlobals() {
  "use asm";
  function caller() {
    return 51;
  }
815
  return {caller:caller};
816 817
}

818 819
assertWasm(51, TestInitFunctionWithNoGlobals);

820

821
(function () {
822 823 824 825 826 827 828 829
function TestExportNameDifferentFromFunctionName() {
  "use asm";
  function caller() {
    return 55;
  }
  return {alt_caller:caller};
}

830 831 832 833
var module_decl = eval(
  '(' + TestExportNameDifferentFromFunctionName.toString() + ')');
var module = module_decl(stdlib);
assertValidAsm(module_decl);
834
assertEquals(55, module.alt_caller());
835
})();
836

837

838 839 840 841 842 843 844 845
function TestFunctionTableSingleFunction() {
  "use asm";

  function dummy() {
    return 71;
  }

  function caller() {
846 847 848
    // TODO(jpp): the parser optimizes function_table[0&0] to function table[0].
    var v = 0;
    return function_table[v&0]() | 0;
849 850 851 852 853 854 855
  }

  var function_table = [dummy]

  return {caller:caller};
}

856
assertWasm(71, TestFunctionTableSingleFunction);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872


function TestFunctionTableMultipleFunctions() {
  "use asm";

  function inc1(x) {
    x = x|0;
    return (x+1)|0;
  }

  function inc2(x) {
    x = x|0;
    return (x+2)|0;
  }

  function caller() {
873 874 875
    var i = 0, j = 1;
    if ((function_table[i&1](50)|0) == 51) {
      if ((function_table[j&1](60)|0) == 62) {
876 877 878 879 880 881 882 883 884 885 886
        return 73;
      }
    }
    return 0;
  }

  var function_table = [inc1, inc2]

  return {caller:caller};
}

887
assertWasm(73, TestFunctionTableMultipleFunctions);
888 889


890
(function () {
891
function TestFunctionTable(stdlib, foreign, buffer) {
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
  "use asm";

  function add(a, b) {
    a = a|0;
    b = b|0;
    return (a+b)|0;
  }

  function sub(a, b) {
    a = a|0;
    b = b|0;
    return (a-b)|0;
  }

  function inc(a) {
    a = a|0;
    return (a+1)|0;
  }

  function caller(table_id, fun_id, arg1, arg2) {
    table_id = table_id|0;
    fun_id = fun_id|0;
    arg1 = arg1|0;
    arg2 = arg2|0;
916
    if ((table_id|0) == 0) {
917
      return funBin[fun_id&3](arg1, arg2)|0;
918
    } else if ((table_id|0) == 1) {
919 920 921 922 923 924 925 926 927 928 929
      return fun[fun_id&0](arg1)|0;
    }
    return 0;
  }

  var funBin = [add, sub, sub, add];
  var fun = [inc];

  return {caller:caller};
}

930
print("TestFunctionTable...");
931
var module = TestFunctionTable(stdlib);
932 933 934 935 936 937
assertEquals(55, module.caller(0, 0, 33, 22));
assertEquals(11, module.caller(0, 1, 33, 22));
assertEquals(9, module.caller(0, 2, 54, 45));
assertEquals(99, module.caller(0, 3, 54, 45));
assertEquals(23, module.caller(0, 4, 12, 11));
assertEquals(31, module.caller(1, 0, 30, 11));
938
})();
939 940


941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
(function TestComma() {
  function CommaModule() {
    "use asm";

    function ifunc(a, b) {
      a = +a;
      b = b | 0;
      return (a, b) | 0;
    }

    function dfunc(a, b) {
      a = a | 0;
      b = +b;
      return +(a, b);
    }

    return {ifunc: ifunc, dfunc: dfunc};
  }

960 961 962
  var module_decl = eval('(' + CommaModule.toString() + ')');
  var m = module_decl(stdlib);
  assertValidAsm(module_decl);
963 964 965
  assertEquals(123, m.ifunc(456.7, 123));
  assertEquals(123.4, m.dfunc(456, 123.4));
})();
966 967


968 969 970 971 972 973
function TestFloatAsDouble(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  function func() {
    var x = fround(1.0);
    return +fround(x);
974
  }
975 976 977
  return {caller: func};
}
assertWasm(1, TestFloatAsDouble);
978 979


980 981 982 983 984 985
function TestOr() {
  "use asm";
  function func() {
    var x = 1;
    var y = 2;
    return (x | y) | 0;
986
  }
987 988
  return {caller: func};
}
989

990
assertWasm(3, TestOr);
991 992


993 994 995 996 997 998
function TestAnd() {
  "use asm";
  function func() {
    var x = 3;
    var y = 2;
    return (x & y) | 0;
999
  }
1000 1001
  return {caller: func};
}
1002

1003
assertWasm(2, TestAnd);
1004 1005


1006 1007 1008 1009 1010 1011
function TestXor() {
  "use asm";
  function func() {
    var x = 3;
    var y = 2;
    return (x ^ y) | 0;
1012
  }
1013 1014
  return {caller: func};
}
1015

1016
assertWasm(1, TestXor);
1017 1018


1019 1020 1021 1022 1023
function TestIntegerMultiplyBothWays(stdlib, foreign, heap) {
  "use asm";
  function func() {
    var a = 1;
    return (((a * 3)|0) + ((4 * a)|0)) | 0;
1024
  }
1025 1026
  return {caller: func};
}
1027

1028
assertWasm(7, TestIntegerMultiplyBothWays);
1029 1030


1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
(function TestBadAssignDoubleFromIntish() {
  function Module(stdlib, foreign, heap) {
    "use asm";
    function func() {
      var a = 1;
      var b = 3.0;
      b = a;
    }
    return {func: func};
  }
1041
  print("TestBadAssignDoubleFromIntish...");
1042
  Module(stdlib);
1043
  assertFalse(%IsAsmWasmCode(Module));
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
})();


(function TestBadAssignIntFromDouble() {
  function Module(stdlib, foreign, heap) {
    "use asm";
    function func() {
      var a = 1;
      var b = 3.0;
      a = b;
    }
    return {func: func};
  }
1057
  print("TestBadAssignIntFromDouble...");
1058
  Module(stdlib);
1059
  assertFalse(%IsAsmWasmCode(Module));
1060 1061 1062
})();


1063 1064 1065 1066 1067 1068 1069 1070 1071
(function TestBadMultiplyIntish() {
  function Module(stdlib, foreign, heap) {
    "use asm";
    function func() {
      var a = 1;
      return ((a + a) * 4) | 0;
    }
    return {func: func};
  }
1072
  print("TestBadMultiplyIntish...");
1073
  Module(stdlib);
1074
  assertFalse(%IsAsmWasmCode(Module));
1075
})();
1076 1077


1078 1079 1080 1081 1082 1083 1084 1085 1086
(function TestBadCastFromInt() {
  function Module(stdlib, foreign, heap) {
    "use asm";
    function func() {
      var a = 1;
      return +a;
    }
    return {func: func};
  }
1087
  print("TestBadCastFromInt...");
1088
  Module(stdlib);
1089
  assertFalse(%IsAsmWasmCode(Module));
1090 1091 1092
})();


1093 1094 1095 1096 1097 1098 1099 1100
function TestAndNegative() {
  "use asm";
  function func() {
    var x = 1;
    var y = 2;
    var z = 0;
    z = x + y & -1;
    return z | 0;
1101
  }
1102 1103
  return {caller: func};
}
1104

1105
assertWasm(3, TestAndNegative);
1106 1107


1108 1109 1110
function TestNegativeDouble() {
  "use asm";
  function func() {
1111
    var x = -34359738368.25;
1112 1113
    var y = -2.5;
    return +(x + y);
1114
  }
1115 1116
  return {caller: func};
}
1117

1118
assertWasm(-34359738370.75, TestNegativeDouble);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131


(function TestBadAndDouble() {
  function Module() {
    "use asm";
    function func() {
      var x = 1.0;
      var y = 2.0;
      return (x & y) | 0;
    }
    return {func: func};
  }

1132
  Module(stdlib);
1133
  assertFalse(%IsAsmWasmCode(Module));
1134
})();
1135 1136


1137 1138 1139 1140 1141 1142 1143 1144 1145
(function TestBadExportKey() {
  function Module() {
    "use asm";
    function func() {
    }
    return {123: func};
  }

  Module(stdlib);
1146
  assertFalse(%IsAsmWasmCode(Module));
1147 1148 1149
})();


1150 1151 1152
/*
// TODO(bradnelson): Technically invalid, but useful to cover unicode, revises
// and re-enable.
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
(function TestUnicodeExportKey() {
  function Module() {
    "use asm";
    function func() {
      return 42;
    }
    return {"\u00d1\u00e6": func};
  }

  var m = Module(stdlib);
  assertEquals(42, m.Ñæ());
  assertValidAsm(Module);
})();
1166
*/
1167 1168


1169 1170 1171 1172 1173 1174 1175
function TestAndIntAndHeapValue(stdlib, foreign, buffer) {
  "use asm";
  var HEAP32 = new stdlib.Int32Array(buffer);
  function func() {
    var x = 0;
    x = HEAP32[0] & -1;
    return x | 0;
1176
  }
1177 1178
  return {caller: func};
}
1179

1180
assertWasm(0, TestAndIntAndHeapValue);
1181

1182 1183 1184 1185 1186 1187 1188 1189

function TestOutOfBoundsConversion($a,$b,$c){'use asm';
  function aaa() {
    var f = 0.0;
    var a = 0;
    f = 5616315000.000001;
    a = ~~f >>>0;
    return a | 0;
1190
  }
1191 1192 1193 1194 1195
  return { caller : aaa };
}

assertWasm(1321347704, TestOutOfBoundsConversion);

1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

(function TestUnsignedLiterals() {
  function asmModule() {
    "use asm";
    function u0xffffffff() {
      var f = 0xffffffff;
      return +(f >>> 0);
    }
    function u0x80000000() {
      var f = 0x80000000;
      return +(f >>> 0);
    }
    function u0x87654321() {
      var f = 0x87654321;
      return +(f >>> 0);
    }
    return {
      u0xffffffff: u0xffffffff,
      u0x80000000: u0x80000000,
      u0x87654321: u0x87654321,
    };
  }
1218 1219 1220
  var decl = eval('(' + asmModule.toString() + ')');
  var wasm = decl(stdlib);
  assertValidAsm(decl);
1221 1222 1223 1224
  assertEquals(0xffffffff, wasm.u0xffffffff());
  assertEquals(0x80000000, wasm.u0x80000000());
  assertEquals(0x87654321, wasm.u0x87654321());
})();
1225

1226

1227 1228 1229 1230 1231
function TestIfWithUnsigned() {
  "use asm";
  function main() {
    if (2147483658) { // 2^31 + 10
      return 231;
1232
    }
1233
    return 0;
1234
  }
1235 1236
  return {caller:main};
}
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
assertWasm(231, TestIfWithUnsigned);


function TestLoopsWithUnsigned() {
  "use asm";
  function main() {
    var val = 1;
    var count = 0;
    for (val = 2147483648; 2147483648;) {
      val = 2147483649;
      break;
    }
    while (val>>>0) {
      val = (val + 1) | 0;
      count = (count + 1)|0;
      if ((count|0) == 9) {
1254 1255
        break;
      }
1256 1257 1258 1259 1260 1261 1262
    }
    count = 0;
    do {
      val = (val + 2) | 0;
      count = (count + 1)|0;
      if ((count|0) == 5) {
        break;
1263
      }
1264 1265 1266
    } while (0xffffffff);
    if ((val>>>0) == 2147483668) {
      return 323;
1267
    }
1268
    return 0;
1269
  }
1270 1271 1272 1273
  return {caller:main};
}

assertWasm(323, TestLoopsWithUnsigned);
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286


function TestSingleFunctionModule() {
  "use asm";
  function add(a, b) {
    a = a | 0;
    b = b | 0;
    return (a + b) | 0;
  }
  return add;
}

assertEquals(7, TestSingleFunctionModule()(3, 4));
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318


function TestNotZero() {
  "use asm";
  function caller() {
    if (!0) {
      return 44;
    } else {
      return 55;
    }
    return 0;
  }
  return {caller: caller};
}

assertWasm(44, TestNotZero);


function TestNotOne() {
  "use asm";
  function caller() {
    if (!1) {
      return 44;
    } else {
      return 55;
    }
    return 0;
  }
  return {caller: caller};
}

assertWasm(55, TestNotOne);
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333


function TestDotfulFloat(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  var foo = fround(55.0);
  function caller() {
    return +foo;
  }
  return {caller: caller};
}

assertWasm(55, TestDotfulFloat);


1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
function TestDotfulLocalFloat(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  function caller() {
    var foo = fround(55.0);
    return +foo;
  }
  return {caller: caller};
}

assertWasm(55, TestDotfulLocalFloat);


1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
function TestDotlessFloat(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  var foo = fround(55);
  function caller() {
    return +foo;
  }
  return {caller: caller};
}

assertWasm(55, TestDotlessFloat);


1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
function TestDotlessLocalFloat(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  function caller() {
    var foo = fround(55);
    return +foo;
  }
  return {caller: caller};
}

assertWasm(55, TestDotlessLocalFloat);


1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
function TestFloatGlobals(stdlib) {
  "use asm";
  var fround = stdlib.Math.fround;
  var foo = fround(1.25);
  function caller() {
    foo = fround(foo + fround(1.0));
    foo = fround(foo + fround(1.0));
    return +foo;
  }
  return {caller: caller};
}

assertWasm(3.25, TestFloatGlobals);
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399


(function TestExportTwice() {
  function asmModule() {
    "use asm";
    function foo() {
      return 42;
    }
    return {bar: foo, baz: foo};
  }
  var m = asmModule();
  assertEquals(42, m.bar());
  assertEquals(42, m.baz());
})();
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423

(function TestGenerator() {
  function* asmModule() {
    "use asm";
    function foo() {
      return 42;
    }
    return {foo: foo};
  }
  asmModule();
  assertFalse(%IsAsmWasmCode(asmModule));
})();

(function TestAsyncFunction() {
  async function asmModule() {
    "use asm";
    function foo() {
      return 42;
    }
    return {foo: foo};
  }
  asmModule();
  assertFalse(%IsAsmWasmCode(asmModule));
})();