OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-wasm | 5 // Flags: --validate-asm --allow-natives-syntax |
6 | 6 |
7 var stdlib = this; | 7 var stdlib = this; |
8 | 8 |
| 9 function assertValidAsm(func) { |
| 10 assertTrue(%IsAsmWasmCode(func)); |
| 11 } |
| 12 |
9 function assertWasm(expected, func, ffi) { | 13 function assertWasm(expected, func, ffi) { |
10 print("Testing " + func.name + "..."); | 14 print("Testing " + func.name + "..."); |
11 assertEquals(expected, Wasm.instantiateModuleFromAsm( | 15 assertEquals( |
12 func.toString(), stdlib, ffi).caller()); | 16 expected, func(stdlib, ffi, new ArrayBuffer(1024)).caller()); |
| 17 assertValidAsm(func); |
13 } | 18 } |
14 | 19 |
15 function EmptyTest() { | 20 function EmptyTest(a, b, c) { |
16 "use asm"; | 21 "use asm"; |
17 function caller() { | 22 function caller() { |
18 empty(); | 23 empty(); |
19 return 11; | 24 return 11; |
20 } | 25 } |
21 function empty() { | 26 function empty() { |
22 } | 27 } |
23 return {caller: caller}; | 28 return {caller: caller}; |
24 } | 29 } |
25 | 30 |
26 assertWasm(11, EmptyTest); | 31 assertWasm(11, EmptyTest); |
27 | 32 |
28 function VoidReturnTest() { | 33 function VoidReturnTest(a, b, c) { |
29 "use asm"; | 34 "use asm"; |
30 function caller() { | 35 function caller() { |
31 empty(); | 36 empty(); |
32 return 19; | 37 return 19; |
33 } | 38 } |
34 function empty() { | 39 function empty() { |
35 var x = 0; | 40 var x = 0; |
36 if (x) return; | 41 if (x) return; |
37 } | 42 } |
38 return {caller: caller}; | 43 return {caller: caller}; |
39 } | 44 } |
40 | 45 |
41 assertWasm(19, VoidReturnTest); | 46 assertWasm(19, VoidReturnTest); |
42 | 47 |
43 function IntTest() { | 48 function IntTest(a, b, c) { |
44 "use asm"; | 49 "use asm"; |
45 function sum(a, b) { | 50 function sum(a, b) { |
46 a = a|0; | 51 a = a|0; |
47 b = b|0; | 52 b = b|0; |
48 var c = 0; | 53 var c = 0; |
49 var d = 3.0; | 54 var d = 3.0; |
50 var e = 0; | 55 var e = 0; |
51 e = ~~d; // double conversion | 56 e = ~~d; // double conversion |
52 c = (b + 1)|0 | 57 c = (b + 1)|0 |
53 return (a + c + 1)|0; | 58 return (a + c + 1)|0; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return (a + c + 1)|0; | 104 return (a + c + 1)|0; |
100 } | 105 } |
101 | 106 |
102 function caller() { | 107 function caller() { |
103 return call(1, 2)|0; | 108 return call(1, 2)|0; |
104 } | 109 } |
105 | 110 |
106 return {caller: caller}; | 111 return {caller: caller}; |
107 } | 112 } |
108 | 113 |
109 assertThrows(function() { | 114 assertTrue(%IsNotAsmWasmCode(BadModule)); |
110 Wasm.instantiateModuleFromAsm(BadModule.toString(), stdlib).caller(); | |
111 }); | |
112 | 115 |
113 | 116 |
114 function TestReturnInBlock() { | 117 function TestReturnInBlock() { |
115 "use asm"; | 118 "use asm"; |
116 | 119 |
117 function caller() { | 120 function caller() { |
118 if(1) { | 121 if(1) { |
119 { | 122 { |
120 { | 123 { |
121 return 1; | 124 return 1; |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 | 496 |
494 return {caller: caller}; | 497 return {caller: caller}; |
495 } | 498 } |
496 | 499 |
497 assertWasm(7, TestInt32HeapAccess); | 500 assertWasm(7, TestInt32HeapAccess); |
498 | 501 |
499 | 502 |
500 function TestInt32HeapAccessExternal() { | 503 function TestInt32HeapAccessExternal() { |
501 var memory = new ArrayBuffer(1024); | 504 var memory = new ArrayBuffer(1024); |
502 var memory_int32 = new Int32Array(memory); | 505 var memory_int32 = new Int32Array(memory); |
503 var module = Wasm.instantiateModuleFromAsm( | 506 var module_decl = eval('(' + TestInt32HeapAccess.toString() + ')'); |
504 TestInt32HeapAccess.toString(), stdlib, null, memory); | 507 var module = module_decl(stdlib, null, memory); |
| 508 assertValidAsm(module_decl); |
505 assertEquals(7, module.caller()); | 509 assertEquals(7, module.caller()); |
506 assertEquals(7, memory_int32[2]); | 510 assertEquals(7, memory_int32[2]); |
507 } | 511 } |
508 | 512 |
509 TestInt32HeapAccessExternal(); | 513 TestInt32HeapAccessExternal(); |
510 | 514 |
511 | 515 |
512 function TestHeapAccessIntTypes() { | 516 function TestHeapAccessIntTypes() { |
513 var types = [ | 517 var types = [ |
514 [Int8Array, 'Int8Array', '>> 0'], | 518 [Int8Array, 'Int8Array', '>> 0'], |
515 [Uint8Array, 'Uint8Array', '>> 0'], | 519 [Uint8Array, 'Uint8Array', '>> 0'], |
516 [Int16Array, 'Int16Array', '>> 1'], | 520 [Int16Array, 'Int16Array', '>> 1'], |
517 [Uint16Array, 'Uint16Array', '>> 1'], | 521 [Uint16Array, 'Uint16Array', '>> 1'], |
518 [Int32Array, 'Int32Array', '>> 2'], | 522 [Int32Array, 'Int32Array', '>> 2'], |
519 [Uint32Array, 'Uint32Array', '>> 2'], | 523 [Uint32Array, 'Uint32Array', '>> 2'], |
520 ]; | 524 ]; |
521 for (var i = 0; i < types.length; i++) { | 525 for (var i = 0; i < types.length; i++) { |
522 var code = TestInt32HeapAccess.toString(); | 526 var code = TestInt32HeapAccess.toString(); |
523 code = code.replace('Int32Array', types[i][1]); | 527 code = code.replace('Int32Array', types[i][1]); |
524 code = code.replace(/>> 2/g, types[i][2]); | 528 code = code.replace(/>> 2/g, types[i][2]); |
525 var memory = new ArrayBuffer(1024); | 529 var memory = new ArrayBuffer(1024); |
526 var memory_view = new types[i][0](memory); | 530 var memory_view = new types[i][0](memory); |
527 var module = Wasm.instantiateModuleFromAsm(code, stdlib, null, memory); | 531 var module_decl = eval('(' + code + ')'); |
| 532 var module = module_decl(stdlib, null, memory); |
| 533 assertValidAsm(module_decl); |
528 assertEquals(7, module.caller()); | 534 assertEquals(7, module.caller()); |
529 assertEquals(7, memory_view[2]); | 535 assertEquals(7, memory_view[2]); |
530 assertEquals(7, Wasm.instantiateModuleFromAsm(code, stdlib).caller()); | 536 assertEquals(7, module_decl(stdlib).caller()); |
| 537 assertValidAsm(module_decl); |
531 } | 538 } |
532 } | 539 } |
533 | 540 |
534 TestHeapAccessIntTypes(); | 541 TestHeapAccessIntTypes(); |
535 | 542 |
536 | 543 |
537 function TestFloatHeapAccess(stdlib, foreign, buffer) { | 544 function TestFloatHeapAccess(stdlib, foreign, buffer) { |
538 "use asm"; | 545 "use asm"; |
539 | 546 |
540 var f32 = new stdlib.Float32Array(buffer); | 547 var f32 = new stdlib.Float32Array(buffer); |
541 var f64 = new stdlib.Float64Array(buffer); | 548 var f64 = new stdlib.Float64Array(buffer); |
542 var fround = stdlib.Math.fround; | 549 var fround = stdlib.Math.fround; |
543 function caller() { | 550 function caller() { |
544 var i = 8; | 551 var i = 8; |
545 var j = 8; | 552 var j = 8; |
546 var v = 6.0; | 553 var v = 6.0; |
547 | 554 |
548 f64[2] = v + 1.0; | 555 f64[2] = v + 1.0; |
549 f64[i >> 3] = +f64[2] + 1.0; | 556 f64[i >> 3] = +f64[2] + 1.0; |
550 f64[j >> 3] = +f64[j >> 3] + 1.0; | 557 f64[j >> 3] = +f64[j >> 3] + 1.0; |
551 i = +f64[i >> 3] == 9.0; | 558 i = +f64[i >> 3] == 9.0; |
552 return i|0; | 559 return i|0; |
553 } | 560 } |
554 | 561 |
555 return {caller: caller}; | 562 return {caller: caller}; |
556 } | 563 } |
557 | 564 |
558 assertEquals(1, Wasm.instantiateModuleFromAsm( | 565 assertWasm(1, TestFloatHeapAccess); |
559 TestFloatHeapAccess.toString(), stdlib).caller()); | |
560 | 566 |
561 | 567 |
562 function TestFloatHeapAccessExternal() { | 568 function TestFloatHeapAccessExternal() { |
563 var memory = new ArrayBuffer(1024); | 569 var memory = new ArrayBuffer(1024); |
564 var memory_float64 = new Float64Array(memory); | 570 var memory_float64 = new Float64Array(memory); |
565 var module = Wasm.instantiateModuleFromAsm( | 571 var module_decl = eval('(' + TestFloatHeapAccess.toString() + ')'); |
566 TestFloatHeapAccess.toString(), stdlib, null, memory); | 572 var module = module_decl(stdlib, null, memory); |
| 573 assertValidAsm(module_decl); |
567 assertEquals(1, module.caller()); | 574 assertEquals(1, module.caller()); |
568 assertEquals(9.0, memory_float64[1]); | 575 assertEquals(9.0, memory_float64[1]); |
569 } | 576 } |
570 | 577 |
571 TestFloatHeapAccessExternal(); | 578 TestFloatHeapAccessExternal(); |
572 | 579 |
573 | 580 |
574 function TestConvertI32() { | 581 function TestConvertI32() { |
575 "use asm"; | 582 "use asm"; |
576 | 583 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
703 | 710 |
704 function init() { | 711 function init() { |
705 a = 43.25; | 712 a = 43.25; |
706 b = 34.25; | 713 b = 34.25; |
707 } | 714 } |
708 | 715 |
709 return {init:init, | 716 return {init:init, |
710 add:add}; | 717 add:add}; |
711 } | 718 } |
712 | 719 |
713 var module = Wasm.instantiateModuleFromAsm( | 720 var module_decl = eval('(' + TestNamedFunctions.toString() + ')'); |
714 TestNamedFunctions.toString(), stdlib); | 721 var module = module_decl(stdlib); |
| 722 assertValidAsm(module_decl); |
715 module.init(); | 723 module.init(); |
716 assertEquals(77.5, module.add()); | 724 assertEquals(77.5, module.add()); |
717 })(); | 725 })(); |
718 | 726 |
719 | 727 |
720 (function () { | 728 (function () { |
721 function TestGlobalsWithInit() { | 729 function TestGlobalsWithInit() { |
722 "use asm"; | 730 "use asm"; |
723 | 731 |
724 var a = 43.25; | 732 var a = 43.25; |
725 var b = 34.25; | 733 var b = 34.25; |
726 | 734 |
727 function add() { | 735 function add() { |
728 return +(a + b); | 736 return +(a + b); |
729 } | 737 } |
730 | 738 |
731 return {add:add}; | 739 return {add:add}; |
732 } | 740 } |
733 | 741 |
734 var module = Wasm.instantiateModuleFromAsm( | 742 var module_decl = eval('(' + TestGlobalsWithInit.toString() + ')'); |
735 TestGlobalsWithInit.toString(), stdlib); | 743 var module = module_decl(stdlib); |
| 744 assertValidAsm(module_decl); |
736 assertEquals(77.5, module.add()); | 745 assertEquals(77.5, module.add()); |
737 })(); | 746 })(); |
738 | 747 |
739 function TestForLoop() { | 748 function TestForLoop() { |
740 "use asm" | 749 "use asm" |
741 | 750 |
742 function caller() { | 751 function caller() { |
743 var ret = 0; | 752 var ret = 0; |
744 var i = 0; | 753 var i = 0; |
745 for (i = 2; (i|0) <= 10; i = (i+1)|0) { | 754 for (i = 2; (i|0) <= 10; i = (i+1)|0) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 var x = 1; | 861 var x = 1; |
853 return (((x|0) > 0) ? 41 : 71)|0; | 862 return (((x|0) > 0) ? 41 : 71)|0; |
854 } | 863 } |
855 | 864 |
856 return {caller:caller}; | 865 return {caller:caller}; |
857 } | 866 } |
858 | 867 |
859 assertWasm(41, TestConditional); | 868 assertWasm(41, TestConditional); |
860 | 869 |
861 | 870 |
862 (function () { | |
863 function TestInitFunctionWithNoGlobals() { | 871 function TestInitFunctionWithNoGlobals() { |
864 "use asm"; | 872 "use asm"; |
865 function caller() { | 873 function caller() { |
866 return 51; | 874 return 51; |
867 } | 875 } |
868 return {caller}; | 876 return {caller}; |
869 } | 877 } |
870 | 878 |
871 var module = Wasm.instantiateModuleFromAsm( | 879 assertWasm(51, TestInitFunctionWithNoGlobals); |
872 TestInitFunctionWithNoGlobals.toString(), stdlib); | 880 |
873 assertEquals(51, module.caller()); | |
874 })(); | |
875 | 881 |
876 (function () { | 882 (function () { |
877 function TestExportNameDifferentFromFunctionName() { | 883 function TestExportNameDifferentFromFunctionName() { |
878 "use asm"; | 884 "use asm"; |
879 function caller() { | 885 function caller() { |
880 return 55; | 886 return 55; |
881 } | 887 } |
882 return {alt_caller:caller}; | 888 return {alt_caller:caller}; |
883 } | 889 } |
884 | 890 |
885 var module = Wasm.instantiateModuleFromAsm( | 891 var module_decl = eval( |
886 TestExportNameDifferentFromFunctionName.toString(), stdlib); | 892 '(' + TestExportNameDifferentFromFunctionName.toString() + ')'); |
| 893 var module = module_decl(stdlib); |
| 894 assertValidAsm(module_decl); |
887 assertEquals(55, module.alt_caller()); | 895 assertEquals(55, module.alt_caller()); |
888 })(); | 896 })(); |
889 | 897 |
| 898 |
890 function TestFunctionTableSingleFunction() { | 899 function TestFunctionTableSingleFunction() { |
891 "use asm"; | 900 "use asm"; |
892 | 901 |
893 function dummy() { | 902 function dummy() { |
894 return 71; | 903 return 71; |
895 } | 904 } |
896 | 905 |
897 function caller() { | 906 function caller() { |
898 // TODO(jpp): the parser optimizes function_table[0&0] to function table[0]. | 907 // TODO(jpp): the parser optimizes function_table[0&0] to function table[0]. |
899 var v = 0; | 908 var v = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 | 942 |
934 var function_table = [inc1, inc2] | 943 var function_table = [inc1, inc2] |
935 | 944 |
936 return {caller:caller}; | 945 return {caller:caller}; |
937 } | 946 } |
938 | 947 |
939 assertWasm(73, TestFunctionTableMultipleFunctions); | 948 assertWasm(73, TestFunctionTableMultipleFunctions); |
940 | 949 |
941 | 950 |
942 (function () { | 951 (function () { |
943 function TestFunctionTable() { | 952 function TestFunctionTable(stdlib, foreign, buffer) { |
944 "use asm"; | 953 "use asm"; |
945 | 954 |
946 function add(a, b) { | 955 function add(a, b) { |
947 a = a|0; | 956 a = a|0; |
948 b = b|0; | 957 b = b|0; |
949 return (a+b)|0; | 958 return (a+b)|0; |
950 } | 959 } |
951 | 960 |
952 function sub(a, b) { | 961 function sub(a, b) { |
953 a = a|0; | 962 a = a|0; |
(...skipping 18 matching lines...) Expand all Loading... |
972 } | 981 } |
973 return 0; | 982 return 0; |
974 } | 983 } |
975 | 984 |
976 var funBin = [add, sub, sub, add]; | 985 var funBin = [add, sub, sub, add]; |
977 var fun = [inc]; | 986 var fun = [inc]; |
978 | 987 |
979 return {caller:caller}; | 988 return {caller:caller}; |
980 } | 989 } |
981 | 990 |
982 var module = Wasm.instantiateModuleFromAsm( | 991 var module = TestFunctionTable(stdlib); |
983 TestFunctionTable.toString(), stdlib); | |
984 assertEquals(55, module.caller(0, 0, 33, 22)); | 992 assertEquals(55, module.caller(0, 0, 33, 22)); |
985 assertEquals(11, module.caller(0, 1, 33, 22)); | 993 assertEquals(11, module.caller(0, 1, 33, 22)); |
986 assertEquals(9, module.caller(0, 2, 54, 45)); | 994 assertEquals(9, module.caller(0, 2, 54, 45)); |
987 assertEquals(99, module.caller(0, 3, 54, 45)); | 995 assertEquals(99, module.caller(0, 3, 54, 45)); |
988 assertEquals(23, module.caller(0, 4, 12, 11)); | 996 assertEquals(23, module.caller(0, 4, 12, 11)); |
989 assertEquals(31, module.caller(1, 0, 30, 11)); | 997 assertEquals(31, module.caller(1, 0, 30, 11)); |
990 })(); | 998 })(); |
991 | 999 |
992 | 1000 |
993 function TestForeignFunctions() { | 1001 function TestForeignFunctions() { |
(...skipping 25 matching lines...) Expand all Loading... |
1019 | 1027 |
1020 function setVal(new_val) { | 1028 function setVal(new_val) { |
1021 val = new_val; | 1029 val = new_val; |
1022 } | 1030 } |
1023 | 1031 |
1024 return {getVal:getVal, setVal:setVal}; | 1032 return {getVal:getVal, setVal:setVal}; |
1025 } | 1033 } |
1026 | 1034 |
1027 var foreign = new ffi(23); | 1035 var foreign = new ffi(23); |
1028 | 1036 |
1029 var module = Wasm.instantiateModuleFromAsm( | 1037 var module = AsmModule({Math: Math}, foreign, null); |
1030 AsmModule.toString(), stdlib, foreign, null); | 1038 assertValidAsm(AsmModule); |
1031 | 1039 |
1032 assertEquals(103, module.caller(23, 103)); | 1040 assertEquals(103, module.caller(23, 103)); |
1033 } | 1041 } |
1034 | 1042 |
1035 TestForeignFunctions(); | 1043 TestForeignFunctions(); |
1036 | 1044 |
1037 | 1045 |
1038 function TestForeignFunctionMultipleUse() { | 1046 function TestForeignFunctionMultipleUse() { |
1039 function AsmModule(stdlib, foreign, buffer) { | 1047 function AsmModule(stdlib, foreign, buffer) { |
1040 "use asm"; | 1048 "use asm"; |
(...skipping 17 matching lines...) Expand all Loading... |
1058 function ffi() { | 1066 function ffi() { |
1059 function getVal() { | 1067 function getVal() { |
1060 return 83.25; | 1068 return 83.25; |
1061 } | 1069 } |
1062 | 1070 |
1063 return {getVal:getVal}; | 1071 return {getVal:getVal}; |
1064 } | 1072 } |
1065 | 1073 |
1066 var foreign = new ffi(); | 1074 var foreign = new ffi(); |
1067 | 1075 |
1068 var module = Wasm.instantiateModuleFromAsm( | 1076 var module_decl = eval('(' + AsmModule.toString() + ')'); |
1069 AsmModule.toString(), stdlib, foreign, null); | 1077 var module = module_decl(stdlib, foreign, null); |
| 1078 assertValidAsm(module_decl); |
1070 | 1079 |
1071 assertEquals(89, module.caller(83, 83.25)); | 1080 assertEquals(89, module.caller(83, 83.25)); |
1072 } | 1081 } |
1073 | 1082 |
1074 TestForeignFunctionMultipleUse(); | 1083 TestForeignFunctionMultipleUse(); |
1075 | 1084 |
1076 | 1085 |
1077 function TestForeignVariables() { | 1086 function TestForeignVariables() { |
1078 function AsmModule(stdlib, foreign, buffer) { | 1087 function AsmModule(stdlib, foreign, buffer) { |
1079 "use asm"; | 1088 "use asm"; |
(...skipping 17 matching lines...) Expand all Loading... |
1097 | 1106 |
1098 function getf2() { | 1107 function getf2() { |
1099 return +f2; | 1108 return +f2; |
1100 } | 1109 } |
1101 | 1110 |
1102 return {geti1:geti1, getf1:getf1, geti2:geti2, getf2:getf2}; | 1111 return {geti1:geti1, getf1:getf1, geti2:geti2, getf2:getf2}; |
1103 } | 1112 } |
1104 | 1113 |
1105 function TestCase(env, i1, f1, i2, f2) { | 1114 function TestCase(env, i1, f1, i2, f2) { |
1106 print("Testing foreign variables..."); | 1115 print("Testing foreign variables..."); |
1107 var module = Wasm.instantiateModuleFromAsm( | 1116 var module_decl = eval('(' + AsmModule.toString() + ')'); |
1108 AsmModule.toString(), stdlib, env); | 1117 var module = module_decl(stdlib, env); |
| 1118 assertValidAsm(module_decl); |
1109 assertEquals(i1, module.geti1()); | 1119 assertEquals(i1, module.geti1()); |
1110 assertEquals(f1, module.getf1()); | 1120 assertEquals(f1, module.getf1()); |
1111 assertEquals(i2, module.geti2()); | 1121 assertEquals(i2, module.geti2()); |
1112 assertEquals(f2, module.getf2()); | 1122 assertEquals(f2, module.getf2()); |
1113 } | 1123 } |
1114 | 1124 |
1115 // Check normal operation. | 1125 // Check normal operation. |
1116 TestCase({foo: 123, bar: 234.5, baz: 345.7}, 123, 234.5, 345, 345.7); | 1126 TestCase({foo: 123, bar: 234.5, baz: 345.7}, 123, 234.5, 345, 345.7); |
1117 // Check partial operation. | 1127 // Check partial operation. |
1118 TestCase({baz: 345.7}, 0, NaN, 345, 345.7); | 1128 TestCase({baz: 345.7}, 0, NaN, 345, 345.7); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1189 } | 1199 } |
1190 | 1200 |
1191 function iload(i) { | 1201 function iload(i) { |
1192 i = i | 0; | 1202 i = i | 0; |
1193 return HEAP8[HEAP32[i >> 2] | 0] | 0; | 1203 return HEAP8[HEAP32[i >> 2] | 0] | 0; |
1194 } | 1204 } |
1195 | 1205 |
1196 return {load: load, iload: iload, store: store, storeb: storeb}; | 1206 return {load: load, iload: iload, store: store, storeb: storeb}; |
1197 } | 1207 } |
1198 | 1208 |
1199 var m = Wasm.instantiateModuleFromAsm( | 1209 var module_decl = eval('(' + TestByteHeapAccessCompat.toString() + ')'); |
1200 TestByteHeapAccessCompat.toString(), stdlib); | 1210 var m = module_decl(stdlib); |
| 1211 assertValidAsm(module_decl); |
1201 m.store(0, 20); | 1212 m.store(0, 20); |
1202 m.store(4, 21); | 1213 m.store(4, 21); |
1203 m.store(8, 22); | 1214 m.store(8, 22); |
1204 m.storeb(20, 123); | 1215 m.storeb(20, 123); |
1205 m.storeb(21, 42); | 1216 m.storeb(21, 42); |
1206 m.storeb(22, 77); | 1217 m.storeb(22, 77); |
1207 assertEquals(123, m.load(20)); | 1218 assertEquals(123, m.load(20)); |
1208 assertEquals(42, m.load(21)); | 1219 assertEquals(42, m.load(21)); |
1209 assertEquals(77, m.load(22)); | 1220 assertEquals(77, m.load(22)); |
1210 assertEquals(123, m.iload(0)); | 1221 assertEquals(123, m.iload(0)); |
(...skipping 28 matching lines...) Expand all Loading... |
1239 | 1250 |
1240 function dfunc(a, b) { | 1251 function dfunc(a, b) { |
1241 a = a | 0; | 1252 a = a | 0; |
1242 b = +b; | 1253 b = +b; |
1243 return +(a, b); | 1254 return +(a, b); |
1244 } | 1255 } |
1245 | 1256 |
1246 return {ifunc: ifunc, dfunc: dfunc}; | 1257 return {ifunc: ifunc, dfunc: dfunc}; |
1247 } | 1258 } |
1248 | 1259 |
1249 var m = Wasm.instantiateModuleFromAsm( | 1260 var module_decl = eval('(' + CommaModule.toString() + ')'); |
1250 CommaModule.toString(), stdlib); | 1261 var m = module_decl(stdlib); |
| 1262 assertValidAsm(module_decl); |
1251 assertEquals(123, m.ifunc(456.7, 123)); | 1263 assertEquals(123, m.ifunc(456.7, 123)); |
1252 assertEquals(123.4, m.dfunc(456, 123.4)); | 1264 assertEquals(123.4, m.dfunc(456, 123.4)); |
1253 })(); | 1265 })(); |
1254 | 1266 |
1255 | 1267 |
1256 function TestFloatAsDouble(stdlib) { | 1268 function TestFloatAsDouble(stdlib) { |
1257 "use asm"; | 1269 "use asm"; |
1258 var fround = stdlib.Math.fround; | 1270 var fround = stdlib.Math.fround; |
1259 function func() { | 1271 function func() { |
1260 var x = fround(1.0); | 1272 var x = fround(1.0); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1297 var x = 3; | 1309 var x = 3; |
1298 var y = 2; | 1310 var y = 2; |
1299 return (x ^ y) | 0; | 1311 return (x ^ y) | 0; |
1300 } | 1312 } |
1301 return {caller: func}; | 1313 return {caller: func}; |
1302 } | 1314 } |
1303 | 1315 |
1304 assertWasm(1, TestXor); | 1316 assertWasm(1, TestXor); |
1305 | 1317 |
1306 | 1318 |
1307 (function TestIntishAssignment() { | 1319 function TestIntishAssignment(stdlib, foreign, heap) { |
1308 function Module(stdlib, foreign, heap) { | 1320 "use asm"; |
1309 "use asm"; | 1321 var HEAP32 = new stdlib.Int32Array(heap); |
1310 var HEAP32 = new stdlib.Int32Array(heap); | 1322 function func() { |
1311 function func() { | 1323 var a = 1; |
1312 var a = 1; | 1324 var b = 2; |
1313 var b = 2; | 1325 HEAP32[0] = a + b; |
1314 HEAP32[0] = a + b; | 1326 return HEAP32[0] | 0; |
1315 return HEAP32[0] | 0; | |
1316 } | |
1317 return {func: func}; | |
1318 } | 1327 } |
| 1328 return {caller: func}; |
| 1329 } |
1319 | 1330 |
1320 var m = Wasm.instantiateModuleFromAsm( | 1331 assertWasm(3, TestIntishAssignment); |
1321 Module.toString(), stdlib); | |
1322 assertEquals(3, m.func()); | |
1323 })(); | |
1324 | 1332 |
1325 | 1333 |
1326 (function TestFloatishAssignment() { | 1334 function TestFloatishAssignment(stdlib, foreign, heap) { |
1327 function Module(stdlib, foreign, heap) { | 1335 "use asm"; |
1328 "use asm"; | 1336 var HEAPF32 = new stdlib.Float32Array(heap); |
1329 var HEAPF32 = new stdlib.Float32Array(heap); | 1337 var fround = stdlib.Math.fround; |
1330 var fround = stdlib.Math.fround; | 1338 function func() { |
1331 function func() { | 1339 var a = fround(1.0); |
1332 var a = fround(1.0); | 1340 var b = fround(2.0); |
1333 var b = fround(2.0); | 1341 HEAPF32[0] = a + b; |
1334 HEAPF32[0] = a + b; | 1342 return +HEAPF32[0]; |
1335 return +HEAPF32[0]; | |
1336 } | |
1337 return {func: func}; | |
1338 } | 1343 } |
| 1344 return {caller: func}; |
| 1345 } |
1339 | 1346 |
1340 var m = Wasm.instantiateModuleFromAsm( | 1347 assertWasm(3, TestFloatishAssignment); |
1341 Module.toString(), stdlib); | |
1342 assertEquals(3, m.func()); | |
1343 })(); | |
1344 | 1348 |
1345 | 1349 |
1346 (function TestDoubleToFloatAssignment() { | 1350 function TestDoubleToFloatAssignment(stdlib, foreign, heap) { |
1347 function Module(stdlib, foreign, heap) { | 1351 "use asm"; |
1348 "use asm"; | 1352 var HEAPF32 = new stdlib.Float32Array(heap); |
1349 var HEAPF32 = new stdlib.Float32Array(heap); | 1353 var fround = stdlib.Math.fround; |
1350 var fround = stdlib.Math.fround; | 1354 function func() { |
1351 function func() { | 1355 var a = 1.23; |
1352 var a = 1.23; | 1356 HEAPF32[0] = a; |
1353 HEAPF32[0] = a; | 1357 return +HEAPF32[0]; |
1354 return +HEAPF32[0]; | |
1355 } | |
1356 return {func: func}; | |
1357 } | 1358 } |
| 1359 return {caller: func}; |
| 1360 } |
1358 | 1361 |
1359 var m = Wasm.instantiateModuleFromAsm( | 1362 assertWasm(Math.fround(1.23), TestDoubleToFloatAssignment); |
1360 Module.toString(), stdlib); | |
1361 assertEquals(1.23, m.func()); | |
1362 }); | |
1363 | 1363 |
1364 | 1364 |
1365 (function TestIntegerMultiplyBothWays() { | 1365 function TestIntegerMultiplyBothWays(stdlib, foreign, heap) { |
1366 function Module(stdlib, foreign, heap) { | 1366 "use asm"; |
1367 "use asm"; | 1367 function func() { |
1368 function func() { | 1368 var a = 1; |
1369 var a = 1; | 1369 return (((a * 3)|0) + ((4 * a)|0)) | 0; |
1370 return (((a * 3)|0) + ((4 * a)|0)) | 0; | |
1371 } | |
1372 return {func: func}; | |
1373 } | 1370 } |
| 1371 return {caller: func}; |
| 1372 } |
1374 | 1373 |
1375 var m = Wasm.instantiateModuleFromAsm( | 1374 assertWasm(7, TestIntegerMultiplyBothWays); |
1376 Module.toString(), stdlib); | |
1377 assertEquals(7, m.func()); | |
1378 })(); | |
1379 | 1375 |
1380 | 1376 |
1381 (function TestBadAssignDoubleFromIntish() { | 1377 (function TestBadAssignDoubleFromIntish() { |
1382 function Module(stdlib, foreign, heap) { | 1378 function Module(stdlib, foreign, heap) { |
1383 "use asm"; | 1379 "use asm"; |
1384 function func() { | 1380 function func() { |
1385 var a = 1; | 1381 var a = 1; |
1386 var b = 3.0; | 1382 var b = 3.0; |
1387 b = a; | 1383 b = a; |
1388 } | 1384 } |
1389 return {func: func}; | 1385 return {func: func}; |
1390 } | 1386 } |
1391 assertThrows(function() { | 1387 Module(stdlib); |
1392 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1388 assertTrue(%IsNotAsmWasmCode(Module)); |
1393 }); | |
1394 })(); | 1389 })(); |
1395 | 1390 |
1396 | 1391 |
1397 (function TestBadAssignIntFromDouble() { | 1392 (function TestBadAssignIntFromDouble() { |
1398 function Module(stdlib, foreign, heap) { | 1393 function Module(stdlib, foreign, heap) { |
1399 "use asm"; | 1394 "use asm"; |
1400 function func() { | 1395 function func() { |
1401 var a = 1; | 1396 var a = 1; |
1402 var b = 3.0; | 1397 var b = 3.0; |
1403 a = b; | 1398 a = b; |
1404 } | 1399 } |
1405 return {func: func}; | 1400 return {func: func}; |
1406 } | 1401 } |
1407 assertThrows(function() { | 1402 Module(stdlib); |
1408 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1403 assertTrue(%IsNotAsmWasmCode(Module)); |
1409 }); | |
1410 })(); | 1404 })(); |
1411 | 1405 |
1412 | 1406 |
1413 (function TestBadMultiplyIntish() { | 1407 (function TestBadMultiplyIntish() { |
1414 function Module(stdlib, foreign, heap) { | 1408 function Module(stdlib, foreign, heap) { |
1415 "use asm"; | 1409 "use asm"; |
1416 function func() { | 1410 function func() { |
1417 var a = 1; | 1411 var a = 1; |
1418 return ((a + a) * 4) | 0; | 1412 return ((a + a) * 4) | 0; |
1419 } | 1413 } |
1420 return {func: func}; | 1414 return {func: func}; |
1421 } | 1415 } |
1422 assertThrows(function() { | 1416 Module(stdlib); |
1423 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1417 assertTrue(%IsNotAsmWasmCode(Module)); |
1424 }); | |
1425 })(); | 1418 })(); |
1426 | 1419 |
1427 | 1420 |
1428 (function TestBadCastFromInt() { | 1421 (function TestBadCastFromInt() { |
1429 function Module(stdlib, foreign, heap) { | 1422 function Module(stdlib, foreign, heap) { |
1430 "use asm"; | 1423 "use asm"; |
1431 function func() { | 1424 function func() { |
1432 var a = 1; | 1425 var a = 1; |
1433 return +a; | 1426 return +a; |
1434 } | 1427 } |
1435 return {func: func}; | 1428 return {func: func}; |
1436 } | 1429 } |
1437 assertThrows(function() { | 1430 Module(stdlib); |
1438 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1431 assertTrue(%IsNotAsmWasmCode(Module)); |
1439 }); | |
1440 })(); | 1432 })(); |
1441 | 1433 |
1442 | 1434 |
1443 (function TestAndNegative() { | 1435 function TestAndNegative() { |
1444 function Module() { | 1436 "use asm"; |
1445 "use asm"; | 1437 function func() { |
1446 function func() { | 1438 var x = 1; |
1447 var x = 1; | 1439 var y = 2; |
1448 var y = 2; | 1440 var z = 0; |
1449 var z = 0; | 1441 z = x + y & -1; |
1450 z = x + y & -1; | 1442 return z | 0; |
1451 return z | 0; | |
1452 } | |
1453 return {func: func}; | |
1454 } | 1443 } |
| 1444 return {caller: func}; |
| 1445 } |
1455 | 1446 |
1456 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1447 assertWasm(3, TestAndNegative); |
1457 assertEquals(3, m.func()); | |
1458 })(); | |
1459 | 1448 |
1460 | 1449 |
1461 (function TestNegativeDouble() { | 1450 function TestNegativeDouble() { |
1462 function Module() { | 1451 "use asm"; |
1463 "use asm"; | 1452 function func() { |
1464 function func() { | 1453 var x = -(34359738368.25); |
1465 var x = -(34359738368.25); | 1454 var y = -2.5; |
1466 var y = -2.5; | 1455 return +(x + y); |
1467 return +(x + y); | |
1468 } | |
1469 return {func: func}; | |
1470 } | 1456 } |
| 1457 return {caller: func}; |
| 1458 } |
1471 | 1459 |
1472 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1460 assertWasm(-34359738370.75, TestNegativeDouble); |
1473 assertEquals(-34359738370.75, m.func()); | |
1474 })(); | |
1475 | 1461 |
1476 | 1462 |
1477 (function TestBadAndDouble() { | 1463 (function TestBadAndDouble() { |
1478 function Module() { | 1464 function Module() { |
1479 "use asm"; | 1465 "use asm"; |
1480 function func() { | 1466 function func() { |
1481 var x = 1.0; | 1467 var x = 1.0; |
1482 var y = 2.0; | 1468 var y = 2.0; |
1483 return (x & y) | 0; | 1469 return (x & y) | 0; |
1484 } | 1470 } |
1485 return {func: func}; | 1471 return {func: func}; |
1486 } | 1472 } |
1487 | 1473 |
1488 assertThrows(function() { | 1474 Module(stdlib); |
1489 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1475 assertTrue(%IsNotAsmWasmCode(Module)); |
1490 }); | |
1491 })(); | 1476 })(); |
1492 | 1477 |
1493 | 1478 |
1494 (function TestAndIntAndHeapValue() { | 1479 function TestAndIntAndHeapValue(stdlib, foreign, buffer) { |
1495 function Module(stdlib, foreign, buffer) { | 1480 "use asm"; |
1496 "use asm"; | 1481 var HEAP32 = new stdlib.Int32Array(buffer); |
1497 var HEAP32 = new stdlib.Int32Array(buffer); | 1482 function func() { |
1498 function func() { | 1483 var x = 0; |
1499 var x = 0; | 1484 x = HEAP32[0] & -1; |
1500 x = HEAP32[0] & -1; | 1485 return x | 0; |
1501 return x | 0; | |
1502 } | |
1503 return {func: func}; | |
1504 } | 1486 } |
| 1487 return {caller: func}; |
| 1488 } |
1505 | 1489 |
1506 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); | 1490 assertWasm(0, TestAndIntAndHeapValue); |
1507 assertEquals(0, m.func()); | |
1508 })(); | |
1509 | 1491 |
1510 (function TestOutOfBoundsConversion() { | 1492 |
1511 function asmModule($a,$b,$c){'use asm'; | 1493 function TestOutOfBoundsConversion($a,$b,$c){'use asm'; |
1512 function aaa() { | 1494 function aaa() { |
1513 var f = 0.0; | 1495 var f = 0.0; |
1514 var a = 0; | 1496 var a = 0; |
1515 f = 5616315000.000001; | 1497 f = 5616315000.000001; |
1516 a = ~~f >>>0; | 1498 a = ~~f >>>0; |
1517 return a | 0; | 1499 return a | 0; |
1518 } | |
1519 return { main : aaa }; | |
1520 } | 1500 } |
1521 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); | 1501 return { caller : aaa }; |
1522 assertEquals(1321347704, wasm.main()); | 1502 } |
1523 })(); | 1503 |
| 1504 assertWasm(1321347704, TestOutOfBoundsConversion); |
| 1505 |
1524 | 1506 |
1525 (function TestUnsignedLiterals() { | 1507 (function TestUnsignedLiterals() { |
1526 function asmModule() { | 1508 function asmModule() { |
1527 "use asm"; | 1509 "use asm"; |
1528 function u0xffffffff() { | 1510 function u0xffffffff() { |
1529 var f = 0xffffffff; | 1511 var f = 0xffffffff; |
1530 return +(f >>> 0); | 1512 return +(f >>> 0); |
1531 } | 1513 } |
1532 function u0x80000000() { | 1514 function u0x80000000() { |
1533 var f = 0x80000000; | 1515 var f = 0x80000000; |
1534 return +(f >>> 0); | 1516 return +(f >>> 0); |
1535 } | 1517 } |
1536 function u0x87654321() { | 1518 function u0x87654321() { |
1537 var f = 0x87654321; | 1519 var f = 0x87654321; |
1538 return +(f >>> 0); | 1520 return +(f >>> 0); |
1539 } | 1521 } |
1540 return { | 1522 return { |
1541 u0xffffffff: u0xffffffff, | 1523 u0xffffffff: u0xffffffff, |
1542 u0x80000000: u0x80000000, | 1524 u0x80000000: u0x80000000, |
1543 u0x87654321: u0x87654321, | 1525 u0x87654321: u0x87654321, |
1544 }; | 1526 }; |
1545 } | 1527 } |
1546 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); | 1528 var decl = eval('(' + asmModule.toString() + ')'); |
| 1529 var wasm = decl(stdlib); |
| 1530 assertValidAsm(decl); |
1547 assertEquals(0xffffffff, wasm.u0xffffffff()); | 1531 assertEquals(0xffffffff, wasm.u0xffffffff()); |
1548 assertEquals(0x80000000, wasm.u0x80000000()); | 1532 assertEquals(0x80000000, wasm.u0x80000000()); |
1549 assertEquals(0x87654321, wasm.u0x87654321()); | 1533 assertEquals(0x87654321, wasm.u0x87654321()); |
1550 })(); | 1534 })(); |
1551 | 1535 |
1552 (function TestBadNoDeclaration() { | |
1553 assertThrows(function() { | |
1554 Wasm.instantiateModuleFromAsm('33;', stdlib); | |
1555 }); | |
1556 })(); | |
1557 | 1536 |
1558 (function TestBadVarDeclaration() { | 1537 function TestIfWithUnsigned() { |
1559 assertThrows(function() { | 1538 "use asm"; |
1560 Wasm.instantiateModuleFromAsm('var x = 3;', stdlib); | 1539 function main() { |
1561 }); | 1540 if (2147483658) { // 2^31 + 10 |
1562 })(); | 1541 return 231; |
| 1542 } |
| 1543 return 0; |
| 1544 } |
| 1545 return {caller:main}; |
| 1546 } |
1563 | 1547 |
1564 (function TestIfWithUnsigned() { | 1548 assertWasm(231, TestIfWithUnsigned); |
1565 function asmModule() { | 1549 |
1566 "use asm"; | 1550 |
1567 function main() { | 1551 function TestLoopsWithUnsigned() { |
1568 if (2147483658) { // 2^31 + 10 | 1552 "use asm"; |
1569 return 231; | 1553 function main() { |
1570 } | 1554 var val = 1; |
1571 return 0; | 1555 var count = 0; |
| 1556 for (val = 2147483648; 2147483648;) { |
| 1557 val = 2147483649; |
| 1558 break; |
1572 } | 1559 } |
1573 return {main:main}; | 1560 while (val>>>0) { |
1574 } | 1561 val = (val + 1) | 0; |
1575 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); | 1562 count = (count + 1)|0; |
1576 assertEquals(231, wasm.main()); | 1563 if ((count|0) == 9) { |
1577 })(); | |
1578 | |
1579 (function TestLoopsWithUnsigned() { | |
1580 function asmModule() { | |
1581 "use asm"; | |
1582 function main() { | |
1583 var val = 1; | |
1584 var count = 0; | |
1585 for (val = 2147483648; 2147483648;) { | |
1586 val = 2147483649; | |
1587 break; | 1564 break; |
1588 } | 1565 } |
1589 while (val>>>0) { | 1566 } |
1590 val = (val + 1) | 0; | 1567 count = 0; |
1591 count = (count + 1)|0; | 1568 do { |
1592 if ((count|0) == 9) { | 1569 val = (val + 2) | 0; |
1593 break; | 1570 count = (count + 1)|0; |
1594 } | 1571 if ((count|0) == 5) { |
| 1572 break; |
1595 } | 1573 } |
1596 count = 0; | 1574 } while (0xffffffff); |
1597 do { | 1575 if ((val>>>0) == 2147483668) { |
1598 val = (val + 2) | 0; | 1576 return 323; |
1599 count = (count + 1)|0; | |
1600 if ((count|0) == 5) { | |
1601 break; | |
1602 } | |
1603 } while (0xffffffff); | |
1604 if ((val>>>0) == 2147483668) { | |
1605 return 323; | |
1606 } | |
1607 return 0; | |
1608 } | 1577 } |
1609 return {main:main}; | 1578 return 0; |
1610 } | 1579 } |
1611 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); | 1580 return {caller:main}; |
1612 assertEquals(323, wasm.main()); | 1581 } |
1613 })(); | 1582 |
| 1583 assertWasm(323, TestLoopsWithUnsigned); |
OLD | NEW |