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: --expose-wasm |
6 | 6 |
| 7 var stdlib = this; |
| 8 |
7 function assertWasm(expected, func, ffi) { | 9 function assertWasm(expected, func, ffi) { |
8 print("Testing " + func.name + "..."); | 10 print("Testing " + func.name + "..."); |
9 assertEquals(expected, Wasm.instantiateModuleFromAsm( | 11 assertEquals(expected, Wasm.instantiateModuleFromAsm( |
10 func.toString(), ffi).caller()); | 12 func.toString(), stdlib, ffi).caller()); |
11 } | 13 } |
12 | 14 |
13 function EmptyTest() { | 15 function EmptyTest() { |
14 "use asm"; | 16 "use asm"; |
15 function caller() { | 17 function caller() { |
16 empty(); | 18 empty(); |
17 return 11; | 19 return 11; |
18 } | 20 } |
19 function empty() { | 21 function empty() { |
20 } | 22 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 100 } |
99 | 101 |
100 function caller() { | 102 function caller() { |
101 return call(1, 2)|0; | 103 return call(1, 2)|0; |
102 } | 104 } |
103 | 105 |
104 return {caller: caller}; | 106 return {caller: caller}; |
105 } | 107 } |
106 | 108 |
107 assertThrows(function() { | 109 assertThrows(function() { |
108 Wasm.instantiateModuleFromAsm(BadModule.toString()).caller(); | 110 Wasm.instantiateModuleFromAsm(BadModule.toString(), stdlib).caller(); |
109 }); | 111 }); |
110 | 112 |
111 | 113 |
112 function TestReturnInBlock() { | 114 function TestReturnInBlock() { |
113 "use asm"; | 115 "use asm"; |
114 | 116 |
115 function caller() { | 117 function caller() { |
116 if(1) { | 118 if(1) { |
117 { | 119 { |
118 { | 120 { |
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 return {caller: caller}; | 494 return {caller: caller}; |
493 } | 495 } |
494 | 496 |
495 assertWasm(7, TestInt32HeapAccess); | 497 assertWasm(7, TestInt32HeapAccess); |
496 | 498 |
497 | 499 |
498 function TestInt32HeapAccessExternal() { | 500 function TestInt32HeapAccessExternal() { |
499 var memory = new ArrayBuffer(1024); | 501 var memory = new ArrayBuffer(1024); |
500 var memory_int32 = new Int32Array(memory); | 502 var memory_int32 = new Int32Array(memory); |
501 var module = Wasm.instantiateModuleFromAsm( | 503 var module = Wasm.instantiateModuleFromAsm( |
502 TestInt32HeapAccess.toString(), null, memory); | 504 TestInt32HeapAccess.toString(), stdlib, null, memory); |
503 assertEquals(7, module.caller()); | 505 assertEquals(7, module.caller()); |
504 assertEquals(7, memory_int32[2]); | 506 assertEquals(7, memory_int32[2]); |
505 } | 507 } |
506 | 508 |
507 TestInt32HeapAccessExternal(); | 509 TestInt32HeapAccessExternal(); |
508 | 510 |
509 | 511 |
510 function TestHeapAccessIntTypes() { | 512 function TestHeapAccessIntTypes() { |
511 var types = [ | 513 var types = [ |
512 [Int8Array, 'Int8Array', '>> 0'], | 514 [Int8Array, 'Int8Array', '>> 0'], |
513 [Uint8Array, 'Uint8Array', '>> 0'], | 515 [Uint8Array, 'Uint8Array', '>> 0'], |
514 [Int16Array, 'Int16Array', '>> 1'], | 516 [Int16Array, 'Int16Array', '>> 1'], |
515 [Uint16Array, 'Uint16Array', '>> 1'], | 517 [Uint16Array, 'Uint16Array', '>> 1'], |
516 [Int32Array, 'Int32Array', '>> 2'], | 518 [Int32Array, 'Int32Array', '>> 2'], |
517 [Uint32Array, 'Uint32Array', '>> 2'], | 519 [Uint32Array, 'Uint32Array', '>> 2'], |
518 ]; | 520 ]; |
519 for (var i = 0; i < types.length; i++) { | 521 for (var i = 0; i < types.length; i++) { |
520 var code = TestInt32HeapAccess.toString(); | 522 var code = TestInt32HeapAccess.toString(); |
521 code = code.replace('Int32Array', types[i][1]); | 523 code = code.replace('Int32Array', types[i][1]); |
522 code = code.replace(/>> 2/g, types[i][2]); | 524 code = code.replace(/>> 2/g, types[i][2]); |
523 var memory = new ArrayBuffer(1024); | 525 var memory = new ArrayBuffer(1024); |
524 var memory_view = new types[i][0](memory); | 526 var memory_view = new types[i][0](memory); |
525 var module = Wasm.instantiateModuleFromAsm(code, null, memory); | 527 var module = Wasm.instantiateModuleFromAsm(code, stdlib, null, memory); |
526 assertEquals(7, module.caller()); | 528 assertEquals(7, module.caller()); |
527 assertEquals(7, memory_view[2]); | 529 assertEquals(7, memory_view[2]); |
528 assertEquals(7, Wasm.instantiateModuleFromAsm(code).caller()); | 530 assertEquals(7, Wasm.instantiateModuleFromAsm(code, stdlib).caller()); |
529 } | 531 } |
530 } | 532 } |
531 | 533 |
532 TestHeapAccessIntTypes(); | 534 TestHeapAccessIntTypes(); |
533 | 535 |
534 | 536 |
535 function TestFloatHeapAccess(stdlib, foreign, buffer) { | 537 function TestFloatHeapAccess(stdlib, foreign, buffer) { |
536 "use asm"; | 538 "use asm"; |
537 | 539 |
538 var f32 = new stdlib.Float32Array(buffer); | 540 var f32 = new stdlib.Float32Array(buffer); |
539 var f64 = new stdlib.Float64Array(buffer); | 541 var f64 = new stdlib.Float64Array(buffer); |
540 var fround = stdlib.Math.fround; | 542 var fround = stdlib.Math.fround; |
541 function caller() { | 543 function caller() { |
542 var i = 8; | 544 var i = 8; |
543 var j = 8; | 545 var j = 8; |
544 var v = 6.0; | 546 var v = 6.0; |
545 | 547 |
546 f64[2] = v + 1.0; | 548 f64[2] = v + 1.0; |
547 f64[i >> 3] = +f64[2] + 1.0; | 549 f64[i >> 3] = +f64[2] + 1.0; |
548 f64[j >> 3] = +f64[j >> 3] + 1.0; | 550 f64[j >> 3] = +f64[j >> 3] + 1.0; |
549 i = +f64[i >> 3] == 9.0; | 551 i = +f64[i >> 3] == 9.0; |
550 return i|0; | 552 return i|0; |
551 } | 553 } |
552 | 554 |
553 return {caller: caller}; | 555 return {caller: caller}; |
554 } | 556 } |
555 | 557 |
556 assertEquals(1, Wasm.instantiateModuleFromAsm( | 558 assertEquals(1, Wasm.instantiateModuleFromAsm( |
557 TestFloatHeapAccess.toString()).caller()); | 559 TestFloatHeapAccess.toString(), stdlib).caller()); |
558 | 560 |
559 | 561 |
560 function TestFloatHeapAccessExternal() { | 562 function TestFloatHeapAccessExternal() { |
561 var memory = new ArrayBuffer(1024); | 563 var memory = new ArrayBuffer(1024); |
562 var memory_float64 = new Float64Array(memory); | 564 var memory_float64 = new Float64Array(memory); |
563 var module = Wasm.instantiateModuleFromAsm( | 565 var module = Wasm.instantiateModuleFromAsm( |
564 TestFloatHeapAccess.toString(), null, memory); | 566 TestFloatHeapAccess.toString(), stdlib, null, memory); |
565 assertEquals(1, module.caller()); | 567 assertEquals(1, module.caller()); |
566 assertEquals(9.0, memory_float64[1]); | 568 assertEquals(9.0, memory_float64[1]); |
567 } | 569 } |
568 | 570 |
569 TestFloatHeapAccessExternal(); | 571 TestFloatHeapAccessExternal(); |
570 | 572 |
571 | 573 |
572 function TestConvertI32() { | 574 function TestConvertI32() { |
573 "use asm"; | 575 "use asm"; |
574 | 576 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
701 | 703 |
702 function init() { | 704 function init() { |
703 a = 43.25; | 705 a = 43.25; |
704 b = 34.25; | 706 b = 34.25; |
705 } | 707 } |
706 | 708 |
707 return {init:init, | 709 return {init:init, |
708 add:add}; | 710 add:add}; |
709 } | 711 } |
710 | 712 |
711 var module = Wasm.instantiateModuleFromAsm(TestNamedFunctions.toString()); | 713 var module = Wasm.instantiateModuleFromAsm( |
| 714 TestNamedFunctions.toString(), stdlib); |
712 module.init(); | 715 module.init(); |
713 assertEquals(77.5, module.add()); | 716 assertEquals(77.5, module.add()); |
714 })(); | 717 })(); |
715 | 718 |
716 | 719 |
717 (function () { | 720 (function () { |
718 function TestGlobalsWithInit() { | 721 function TestGlobalsWithInit() { |
719 "use asm"; | 722 "use asm"; |
720 | 723 |
721 var a = 43.25; | 724 var a = 43.25; |
722 var b = 34.25; | 725 var b = 34.25; |
723 | 726 |
724 function add() { | 727 function add() { |
725 return +(a + b); | 728 return +(a + b); |
726 } | 729 } |
727 | 730 |
728 return {add:add}; | 731 return {add:add}; |
729 } | 732 } |
730 | 733 |
731 var module = Wasm.instantiateModuleFromAsm(TestGlobalsWithInit.toString()); | 734 var module = Wasm.instantiateModuleFromAsm( |
| 735 TestGlobalsWithInit.toString(), stdlib); |
732 assertEquals(77.5, module.add()); | 736 assertEquals(77.5, module.add()); |
733 })(); | 737 })(); |
734 | 738 |
735 function TestForLoop() { | 739 function TestForLoop() { |
736 "use asm" | 740 "use asm" |
737 | 741 |
738 function caller() { | 742 function caller() { |
739 var ret = 0; | 743 var ret = 0; |
740 var i = 0; | 744 var i = 0; |
741 for (i = 2; (i|0) <= 10; i = (i+1)|0) { | 745 for (i = 2; (i|0) <= 10; i = (i+1)|0) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
858 (function () { | 862 (function () { |
859 function TestInitFunctionWithNoGlobals() { | 863 function TestInitFunctionWithNoGlobals() { |
860 "use asm"; | 864 "use asm"; |
861 function caller() { | 865 function caller() { |
862 return 51; | 866 return 51; |
863 } | 867 } |
864 return {caller}; | 868 return {caller}; |
865 } | 869 } |
866 | 870 |
867 var module = Wasm.instantiateModuleFromAsm( | 871 var module = Wasm.instantiateModuleFromAsm( |
868 TestInitFunctionWithNoGlobals.toString()); | 872 TestInitFunctionWithNoGlobals.toString(), stdlib); |
869 assertEquals(51, module.caller()); | 873 assertEquals(51, module.caller()); |
870 })(); | 874 })(); |
871 | 875 |
872 (function () { | 876 (function () { |
873 function TestExportNameDifferentFromFunctionName() { | 877 function TestExportNameDifferentFromFunctionName() { |
874 "use asm"; | 878 "use asm"; |
875 function caller() { | 879 function caller() { |
876 return 55; | 880 return 55; |
877 } | 881 } |
878 return {alt_caller:caller}; | 882 return {alt_caller:caller}; |
879 } | 883 } |
880 | 884 |
881 var module = Wasm.instantiateModuleFromAsm( | 885 var module = Wasm.instantiateModuleFromAsm( |
882 TestExportNameDifferentFromFunctionName.toString()); | 886 TestExportNameDifferentFromFunctionName.toString(), stdlib); |
883 assertEquals(55, module.alt_caller()); | 887 assertEquals(55, module.alt_caller()); |
884 })(); | 888 })(); |
885 | 889 |
886 function TestFunctionTableSingleFunction() { | 890 function TestFunctionTableSingleFunction() { |
887 "use asm"; | 891 "use asm"; |
888 | 892 |
889 function dummy() { | 893 function dummy() { |
890 return 71; | 894 return 71; |
891 } | 895 } |
892 | 896 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 } | 972 } |
969 return 0; | 973 return 0; |
970 } | 974 } |
971 | 975 |
972 var funBin = [add, sub, sub, add]; | 976 var funBin = [add, sub, sub, add]; |
973 var fun = [inc]; | 977 var fun = [inc]; |
974 | 978 |
975 return {caller:caller}; | 979 return {caller:caller}; |
976 } | 980 } |
977 | 981 |
978 var module = Wasm.instantiateModuleFromAsm(TestFunctionTable.toString()); | 982 var module = Wasm.instantiateModuleFromAsm( |
| 983 TestFunctionTable.toString(), stdlib); |
979 assertEquals(55, module.caller(0, 0, 33, 22)); | 984 assertEquals(55, module.caller(0, 0, 33, 22)); |
980 assertEquals(11, module.caller(0, 1, 33, 22)); | 985 assertEquals(11, module.caller(0, 1, 33, 22)); |
981 assertEquals(9, module.caller(0, 2, 54, 45)); | 986 assertEquals(9, module.caller(0, 2, 54, 45)); |
982 assertEquals(99, module.caller(0, 3, 54, 45)); | 987 assertEquals(99, module.caller(0, 3, 54, 45)); |
983 assertEquals(23, module.caller(0, 4, 12, 11)); | 988 assertEquals(23, module.caller(0, 4, 12, 11)); |
984 assertEquals(31, module.caller(1, 0, 30, 11)); | 989 assertEquals(31, module.caller(1, 0, 30, 11)); |
985 })(); | 990 })(); |
986 | 991 |
987 | 992 |
988 function TestForeignFunctions() { | 993 function TestForeignFunctions() { |
(...skipping 25 matching lines...) Expand all Loading... |
1014 | 1019 |
1015 function setVal(new_val) { | 1020 function setVal(new_val) { |
1016 val = new_val; | 1021 val = new_val; |
1017 } | 1022 } |
1018 | 1023 |
1019 return {getVal:getVal, setVal:setVal}; | 1024 return {getVal:getVal, setVal:setVal}; |
1020 } | 1025 } |
1021 | 1026 |
1022 var foreign = new ffi(23); | 1027 var foreign = new ffi(23); |
1023 | 1028 |
1024 var module = Wasm.instantiateModuleFromAsm(AsmModule.toString(), | 1029 var module = Wasm.instantiateModuleFromAsm( |
1025 foreign, null); | 1030 AsmModule.toString(), stdlib, foreign, null); |
1026 | 1031 |
1027 assertEquals(103, module.caller(23, 103)); | 1032 assertEquals(103, module.caller(23, 103)); |
1028 } | 1033 } |
1029 | 1034 |
1030 TestForeignFunctions(); | 1035 TestForeignFunctions(); |
1031 | 1036 |
1032 | 1037 |
1033 function TestForeignFunctionMultipleUse() { | 1038 function TestForeignFunctionMultipleUse() { |
1034 function AsmModule(stdlib, foreign, buffer) { | 1039 function AsmModule(stdlib, foreign, buffer) { |
1035 "use asm"; | 1040 "use asm"; |
(...skipping 17 matching lines...) Expand all Loading... |
1053 function ffi() { | 1058 function ffi() { |
1054 function getVal() { | 1059 function getVal() { |
1055 return 83.25; | 1060 return 83.25; |
1056 } | 1061 } |
1057 | 1062 |
1058 return {getVal:getVal}; | 1063 return {getVal:getVal}; |
1059 } | 1064 } |
1060 | 1065 |
1061 var foreign = new ffi(); | 1066 var foreign = new ffi(); |
1062 | 1067 |
1063 var module = Wasm.instantiateModuleFromAsm(AsmModule.toString(), | 1068 var module = Wasm.instantiateModuleFromAsm( |
1064 foreign, null); | 1069 AsmModule.toString(), stdlib, foreign, null); |
1065 | 1070 |
1066 assertEquals(89, module.caller(83, 83.25)); | 1071 assertEquals(89, module.caller(83, 83.25)); |
1067 } | 1072 } |
1068 | 1073 |
1069 TestForeignFunctionMultipleUse(); | 1074 TestForeignFunctionMultipleUse(); |
1070 | 1075 |
1071 | 1076 |
1072 function TestForeignVariables() { | 1077 function TestForeignVariables() { |
1073 function AsmModule(stdlib, foreign, buffer) { | 1078 function AsmModule(stdlib, foreign, buffer) { |
1074 "use asm"; | 1079 "use asm"; |
(...skipping 18 matching lines...) Expand all Loading... |
1093 function getf2() { | 1098 function getf2() { |
1094 return +f2; | 1099 return +f2; |
1095 } | 1100 } |
1096 | 1101 |
1097 return {geti1:geti1, getf1:getf1, geti2:geti2, getf2:getf2}; | 1102 return {geti1:geti1, getf1:getf1, geti2:geti2, getf2:getf2}; |
1098 } | 1103 } |
1099 | 1104 |
1100 function TestCase(env, i1, f1, i2, f2) { | 1105 function TestCase(env, i1, f1, i2, f2) { |
1101 print("Testing foreign variables..."); | 1106 print("Testing foreign variables..."); |
1102 var module = Wasm.instantiateModuleFromAsm( | 1107 var module = Wasm.instantiateModuleFromAsm( |
1103 AsmModule.toString(), env); | 1108 AsmModule.toString(), stdlib, env); |
1104 assertEquals(i1, module.geti1()); | 1109 assertEquals(i1, module.geti1()); |
1105 assertEquals(f1, module.getf1()); | 1110 assertEquals(f1, module.getf1()); |
1106 assertEquals(i2, module.geti2()); | 1111 assertEquals(i2, module.geti2()); |
1107 assertEquals(f2, module.getf2()); | 1112 assertEquals(f2, module.getf2()); |
1108 } | 1113 } |
1109 | 1114 |
1110 // Check normal operation. | 1115 // Check normal operation. |
1111 TestCase({foo: 123, bar: 234.5, baz: 345.7}, 123, 234.5, 345, 345.7); | 1116 TestCase({foo: 123, bar: 234.5, baz: 345.7}, 123, 234.5, 345, 345.7); |
1112 // Check partial operation. | 1117 // Check partial operation. |
1113 TestCase({baz: 345.7}, 0, NaN, 345, 345.7); | 1118 TestCase({baz: 345.7}, 0, NaN, 345, 345.7); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1185 | 1190 |
1186 function iload(i) { | 1191 function iload(i) { |
1187 i = i | 0; | 1192 i = i | 0; |
1188 return HEAP8[HEAP32[i >> 2] | 0] | 0; | 1193 return HEAP8[HEAP32[i >> 2] | 0] | 0; |
1189 } | 1194 } |
1190 | 1195 |
1191 return {load: load, iload: iload, store: store, storeb: storeb}; | 1196 return {load: load, iload: iload, store: store, storeb: storeb}; |
1192 } | 1197 } |
1193 | 1198 |
1194 var m = Wasm.instantiateModuleFromAsm( | 1199 var m = Wasm.instantiateModuleFromAsm( |
1195 TestByteHeapAccessCompat.toString()); | 1200 TestByteHeapAccessCompat.toString(), stdlib); |
1196 m.store(0, 20); | 1201 m.store(0, 20); |
1197 m.store(4, 21); | 1202 m.store(4, 21); |
1198 m.store(8, 22); | 1203 m.store(8, 22); |
1199 m.storeb(20, 123); | 1204 m.storeb(20, 123); |
1200 m.storeb(21, 42); | 1205 m.storeb(21, 42); |
1201 m.storeb(22, 77); | 1206 m.storeb(22, 77); |
1202 assertEquals(123, m.load(20)); | 1207 assertEquals(123, m.load(20)); |
1203 assertEquals(42, m.load(21)); | 1208 assertEquals(42, m.load(21)); |
1204 assertEquals(77, m.load(22)); | 1209 assertEquals(77, m.load(22)); |
1205 assertEquals(123, m.iload(0)); | 1210 assertEquals(123, m.iload(0)); |
(...skipping 28 matching lines...) Expand all Loading... |
1234 | 1239 |
1235 function dfunc(a, b) { | 1240 function dfunc(a, b) { |
1236 a = a | 0; | 1241 a = a | 0; |
1237 b = +b; | 1242 b = +b; |
1238 return +(a, b); | 1243 return +(a, b); |
1239 } | 1244 } |
1240 | 1245 |
1241 return {ifunc: ifunc, dfunc: dfunc}; | 1246 return {ifunc: ifunc, dfunc: dfunc}; |
1242 } | 1247 } |
1243 | 1248 |
1244 var m = Wasm.instantiateModuleFromAsm(CommaModule.toString()); | 1249 var m = Wasm.instantiateModuleFromAsm( |
| 1250 CommaModule.toString(), stdlib); |
1245 assertEquals(123, m.ifunc(456.7, 123)); | 1251 assertEquals(123, m.ifunc(456.7, 123)); |
1246 assertEquals(123.4, m.dfunc(456, 123.4)); | 1252 assertEquals(123.4, m.dfunc(456, 123.4)); |
1247 })(); | 1253 })(); |
1248 | 1254 |
1249 | 1255 |
1250 function TestFloatAsDouble(stdlib) { | 1256 function TestFloatAsDouble(stdlib) { |
1251 "use asm"; | 1257 "use asm"; |
1252 var fround = stdlib.Math.fround; | 1258 var fround = stdlib.Math.fround; |
1253 function func() { | 1259 function func() { |
1254 var x = fround(1.0); | 1260 var x = fround(1.0); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1304 var HEAP32 = new stdlib.Int32Array(heap); | 1310 var HEAP32 = new stdlib.Int32Array(heap); |
1305 function func() { | 1311 function func() { |
1306 var a = 1; | 1312 var a = 1; |
1307 var b = 2; | 1313 var b = 2; |
1308 HEAP32[0] = a + b; | 1314 HEAP32[0] = a + b; |
1309 return HEAP32[0] | 0; | 1315 return HEAP32[0] | 0; |
1310 } | 1316 } |
1311 return {func: func}; | 1317 return {func: func}; |
1312 } | 1318 } |
1313 | 1319 |
1314 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1320 var m = Wasm.instantiateModuleFromAsm( |
| 1321 Module.toString(), stdlib); |
1315 assertEquals(3, m.func()); | 1322 assertEquals(3, m.func()); |
1316 })(); | 1323 })(); |
1317 | 1324 |
1318 | 1325 |
1319 (function TestFloatishAssignment() { | 1326 (function TestFloatishAssignment() { |
1320 function Module(stdlib, foreign, heap) { | 1327 function Module(stdlib, foreign, heap) { |
1321 "use asm"; | 1328 "use asm"; |
1322 var HEAPF32 = new stdlib.Float32Array(heap); | 1329 var HEAPF32 = new stdlib.Float32Array(heap); |
1323 var fround = stdlib.Math.fround; | 1330 var fround = stdlib.Math.fround; |
1324 function func() { | 1331 function func() { |
1325 var a = fround(1.0); | 1332 var a = fround(1.0); |
1326 var b = fround(2.0); | 1333 var b = fround(2.0); |
1327 HEAPF32[0] = a + b; | 1334 HEAPF32[0] = a + b; |
1328 return +HEAPF32[0]; | 1335 return +HEAPF32[0]; |
1329 } | 1336 } |
1330 return {func: func}; | 1337 return {func: func}; |
1331 } | 1338 } |
1332 | 1339 |
1333 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1340 var m = Wasm.instantiateModuleFromAsm( |
| 1341 Module.toString(), stdlib); |
1334 assertEquals(3, m.func()); | 1342 assertEquals(3, m.func()); |
1335 })(); | 1343 })(); |
1336 | 1344 |
1337 | 1345 |
1338 (function TestDoubleToFloatAssignment() { | 1346 (function TestDoubleToFloatAssignment() { |
1339 function Module(stdlib, foreign, heap) { | 1347 function Module(stdlib, foreign, heap) { |
1340 "use asm"; | 1348 "use asm"; |
1341 var HEAPF32 = new stdlib.Float32Array(heap); | 1349 var HEAPF32 = new stdlib.Float32Array(heap); |
1342 var fround = stdlib.Math.fround; | 1350 var fround = stdlib.Math.fround; |
1343 function func() { | 1351 function func() { |
1344 var a = 1.23; | 1352 var a = 1.23; |
1345 HEAPF32[0] = a; | 1353 HEAPF32[0] = a; |
1346 return +HEAPF32[0]; | 1354 return +HEAPF32[0]; |
1347 } | 1355 } |
1348 return {func: func}; | 1356 return {func: func}; |
1349 } | 1357 } |
1350 | 1358 |
1351 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1359 var m = Wasm.instantiateModuleFromAsm( |
| 1360 Module.toString(), stdlib); |
1352 assertEquals(1.23, m.func()); | 1361 assertEquals(1.23, m.func()); |
1353 }); | 1362 }); |
1354 | 1363 |
1355 | 1364 |
1356 (function TestIntegerMultiplyBothWays() { | 1365 (function TestIntegerMultiplyBothWays() { |
1357 function Module(stdlib, foreign, heap) { | 1366 function Module(stdlib, foreign, heap) { |
1358 "use asm"; | 1367 "use asm"; |
1359 function func() { | 1368 function func() { |
1360 var a = 1; | 1369 var a = 1; |
1361 return (((a * 3)|0) + ((4 * a)|0)) | 0; | 1370 return (((a * 3)|0) + ((4 * a)|0)) | 0; |
1362 } | 1371 } |
1363 return {func: func}; | 1372 return {func: func}; |
1364 } | 1373 } |
1365 | 1374 |
1366 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1375 var m = Wasm.instantiateModuleFromAsm( |
| 1376 Module.toString(), stdlib); |
1367 assertEquals(7, m.func()); | 1377 assertEquals(7, m.func()); |
1368 })(); | 1378 })(); |
1369 | 1379 |
1370 | 1380 |
1371 (function TestBadAssignDoubleFromIntish() { | 1381 (function TestBadAssignDoubleFromIntish() { |
1372 function Module(stdlib, foreign, heap) { | 1382 function Module(stdlib, foreign, heap) { |
1373 "use asm"; | 1383 "use asm"; |
1374 function func() { | 1384 function func() { |
1375 var a = 1; | 1385 var a = 1; |
1376 var b = 3.0; | 1386 var b = 3.0; |
1377 b = a; | 1387 b = a; |
1378 } | 1388 } |
1379 return {func: func}; | 1389 return {func: func}; |
1380 } | 1390 } |
1381 assertThrows(function() { | 1391 assertThrows(function() { |
1382 Wasm.instantiateModuleFromAsm(Module.toString()); | 1392 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1383 }); | 1393 }); |
1384 })(); | 1394 })(); |
1385 | 1395 |
1386 | 1396 |
1387 (function TestBadAssignIntFromDouble() { | 1397 (function TestBadAssignIntFromDouble() { |
1388 function Module(stdlib, foreign, heap) { | 1398 function Module(stdlib, foreign, heap) { |
1389 "use asm"; | 1399 "use asm"; |
1390 function func() { | 1400 function func() { |
1391 var a = 1; | 1401 var a = 1; |
1392 var b = 3.0; | 1402 var b = 3.0; |
1393 a = b; | 1403 a = b; |
1394 } | 1404 } |
1395 return {func: func}; | 1405 return {func: func}; |
1396 } | 1406 } |
1397 assertThrows(function() { | 1407 assertThrows(function() { |
1398 Wasm.instantiateModuleFromAsm(Module.toString()); | 1408 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1399 }); | 1409 }); |
1400 })(); | 1410 })(); |
1401 | 1411 |
1402 | 1412 |
1403 (function TestBadMultiplyIntish() { | 1413 (function TestBadMultiplyIntish() { |
1404 function Module(stdlib, foreign, heap) { | 1414 function Module(stdlib, foreign, heap) { |
1405 "use asm"; | 1415 "use asm"; |
1406 function func() { | 1416 function func() { |
1407 var a = 1; | 1417 var a = 1; |
1408 return ((a + a) * 4) | 0; | 1418 return ((a + a) * 4) | 0; |
1409 } | 1419 } |
1410 return {func: func}; | 1420 return {func: func}; |
1411 } | 1421 } |
1412 assertThrows(function() { | 1422 assertThrows(function() { |
1413 Wasm.instantiateModuleFromAsm(Module.toString()); | 1423 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1414 }); | 1424 }); |
1415 })(); | 1425 })(); |
1416 | 1426 |
1417 | 1427 |
1418 (function TestBadCastFromInt() { | 1428 (function TestBadCastFromInt() { |
1419 function Module(stdlib, foreign, heap) { | 1429 function Module(stdlib, foreign, heap) { |
1420 "use asm"; | 1430 "use asm"; |
1421 function func() { | 1431 function func() { |
1422 var a = 1; | 1432 var a = 1; |
1423 return +a; | 1433 return +a; |
1424 } | 1434 } |
1425 return {func: func}; | 1435 return {func: func}; |
1426 } | 1436 } |
1427 assertThrows(function() { | 1437 assertThrows(function() { |
1428 Wasm.instantiateModuleFromAsm(Module.toString()); | 1438 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1429 }); | 1439 }); |
1430 })(); | 1440 })(); |
1431 | 1441 |
1432 | 1442 |
1433 (function TestAndNegative() { | 1443 (function TestAndNegative() { |
1434 function Module() { | 1444 function Module() { |
1435 "use asm"; | 1445 "use asm"; |
1436 function func() { | 1446 function func() { |
1437 var x = 1; | 1447 var x = 1; |
1438 var y = 2; | 1448 var y = 2; |
1439 var z = 0; | 1449 var z = 0; |
1440 z = x + y & -1; | 1450 z = x + y & -1; |
1441 return z | 0; | 1451 return z | 0; |
1442 } | 1452 } |
1443 return {func: func}; | 1453 return {func: func}; |
1444 } | 1454 } |
1445 | 1455 |
1446 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1456 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1447 assertEquals(3, m.func()); | 1457 assertEquals(3, m.func()); |
1448 })(); | 1458 })(); |
1449 | 1459 |
1450 | 1460 |
1451 (function TestNegativeDouble() { | 1461 (function TestNegativeDouble() { |
1452 function Module() { | 1462 function Module() { |
1453 "use asm"; | 1463 "use asm"; |
1454 function func() { | 1464 function func() { |
1455 var x = -(34359738368.25); | 1465 var x = -(34359738368.25); |
1456 var y = -2.5; | 1466 var y = -2.5; |
1457 return +(x + y); | 1467 return +(x + y); |
1458 } | 1468 } |
1459 return {func: func}; | 1469 return {func: func}; |
1460 } | 1470 } |
1461 | 1471 |
1462 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1472 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1463 assertEquals(-34359738370.75, m.func()); | 1473 assertEquals(-34359738370.75, m.func()); |
1464 })(); | 1474 })(); |
1465 | 1475 |
1466 | 1476 |
1467 (function TestBadAndDouble() { | 1477 (function TestBadAndDouble() { |
1468 function Module() { | 1478 function Module() { |
1469 "use asm"; | 1479 "use asm"; |
1470 function func() { | 1480 function func() { |
1471 var x = 1.0; | 1481 var x = 1.0; |
1472 var y = 2.0; | 1482 var y = 2.0; |
1473 return (x & y) | 0; | 1483 return (x & y) | 0; |
1474 } | 1484 } |
1475 return {func: func}; | 1485 return {func: func}; |
1476 } | 1486 } |
1477 | 1487 |
1478 assertThrows(function() { | 1488 assertThrows(function() { |
1479 Wasm.instantiateModuleFromAsm(Module.toString()); | 1489 Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1480 }); | 1490 }); |
1481 })(); | 1491 })(); |
1482 | 1492 |
1483 | 1493 |
1484 (function TestAndIntAndHeapValue() { | 1494 (function TestAndIntAndHeapValue() { |
1485 function Module(stdlib, foreign, buffer) { | 1495 function Module(stdlib, foreign, buffer) { |
1486 "use asm"; | 1496 "use asm"; |
1487 var HEAP32 = new stdlib.Int32Array(buffer); | 1497 var HEAP32 = new stdlib.Int32Array(buffer); |
1488 function func() { | 1498 function func() { |
1489 var x = 0; | 1499 var x = 0; |
1490 x = HEAP32[0] & -1; | 1500 x = HEAP32[0] & -1; |
1491 return x | 0; | 1501 return x | 0; |
1492 } | 1502 } |
1493 return {func: func}; | 1503 return {func: func}; |
1494 } | 1504 } |
1495 | 1505 |
1496 var m = Wasm.instantiateModuleFromAsm(Module.toString()); | 1506 var m = Wasm.instantiateModuleFromAsm(Module.toString(), stdlib); |
1497 assertEquals(0, m.func()); | 1507 assertEquals(0, m.func()); |
1498 })(); | 1508 })(); |
1499 | 1509 |
1500 (function TestOutOfBoundsConversion() { | 1510 (function TestOutOfBoundsConversion() { |
1501 function asmModule($a,$b,$c){'use asm'; | 1511 function asmModule($a,$b,$c){'use asm'; |
1502 function aaa() { | 1512 function aaa() { |
1503 var f = 0.0; | 1513 var f = 0.0; |
1504 var a = 0; | 1514 var a = 0; |
1505 f = 5616315000.000001; | 1515 f = 5616315000.000001; |
1506 a = ~~f >>>0; | 1516 a = ~~f >>>0; |
1507 return a | 0; | 1517 return a | 0; |
1508 } | 1518 } |
1509 return { main : aaa }; | 1519 return { main : aaa }; |
1510 } | 1520 } |
1511 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); | 1521 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); |
1512 assertEquals(1321347704, wasm.main()); | 1522 assertEquals(1321347704, wasm.main()); |
1513 })(); | 1523 })(); |
1514 | 1524 |
1515 (function TestUnsignedLiterals() { | 1525 (function TestUnsignedLiterals() { |
1516 function asmModule() { | 1526 function asmModule() { |
1517 "use asm"; | 1527 "use asm"; |
1518 function u0xffffffff() { | 1528 function u0xffffffff() { |
1519 var f = 0xffffffff; | 1529 var f = 0xffffffff; |
1520 return +(f >>> 0); | 1530 return +(f >>> 0); |
1521 } | 1531 } |
1522 function u0x80000000() { | 1532 function u0x80000000() { |
1523 var f = 0x80000000; | 1533 var f = 0x80000000; |
1524 return +(f >>> 0); | 1534 return +(f >>> 0); |
1525 } | 1535 } |
1526 function u0x87654321() { | 1536 function u0x87654321() { |
1527 var f = 0x87654321; | 1537 var f = 0x87654321; |
1528 return +(f >>> 0); | 1538 return +(f >>> 0); |
1529 } | 1539 } |
1530 return { | 1540 return { |
1531 u0xffffffff: u0xffffffff, | 1541 u0xffffffff: u0xffffffff, |
1532 u0x80000000: u0x80000000, | 1542 u0x80000000: u0x80000000, |
1533 u0x87654321: u0x87654321, | 1543 u0x87654321: u0x87654321, |
1534 }; | 1544 }; |
1535 } | 1545 } |
1536 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); | 1546 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); |
1537 assertEquals(0xffffffff, wasm.u0xffffffff()); | 1547 assertEquals(0xffffffff, wasm.u0xffffffff()); |
1538 assertEquals(0x80000000, wasm.u0x80000000()); | 1548 assertEquals(0x80000000, wasm.u0x80000000()); |
1539 assertEquals(0x87654321, wasm.u0x87654321()); | 1549 assertEquals(0x87654321, wasm.u0x87654321()); |
1540 })(); | 1550 })(); |
1541 | 1551 |
1542 (function TestBadNoDeclaration() { | 1552 (function TestBadNoDeclaration() { |
1543 assertThrows(function() { | 1553 assertThrows(function() { |
1544 Wasm.instantiateModuleFromAsm('33;'); | 1554 Wasm.instantiateModuleFromAsm('33;', stdlib); |
1545 }); | 1555 }); |
1546 })(); | 1556 })(); |
1547 | 1557 |
1548 (function TestBadVarDeclaration() { | 1558 (function TestBadVarDeclaration() { |
1549 assertThrows(function() { | 1559 assertThrows(function() { |
1550 Wasm.instantiateModuleFromAsm('var x = 3;'); | 1560 Wasm.instantiateModuleFromAsm('var x = 3;', stdlib); |
1551 }); | 1561 }); |
1552 })(); | 1562 })(); |
1553 | 1563 |
1554 (function TestIfWithUnsigned() { | 1564 (function TestIfWithUnsigned() { |
1555 function asmModule() { | 1565 function asmModule() { |
1556 "use asm"; | 1566 "use asm"; |
1557 function main() { | 1567 function main() { |
1558 if (2147483658) { // 2^31 + 10 | 1568 if (2147483658) { // 2^31 + 10 |
1559 return 231; | 1569 return 231; |
1560 } | 1570 } |
1561 return 0; | 1571 return 0; |
1562 } | 1572 } |
1563 return {main:main}; | 1573 return {main:main}; |
1564 } | 1574 } |
1565 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); | 1575 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); |
1566 assertEquals(231, wasm.main()); | 1576 assertEquals(231, wasm.main()); |
1567 })(); | 1577 })(); |
1568 | 1578 |
1569 (function TestLoopsWithUnsigned() { | 1579 (function TestLoopsWithUnsigned() { |
1570 function asmModule() { | 1580 function asmModule() { |
1571 "use asm"; | 1581 "use asm"; |
1572 function main() { | 1582 function main() { |
1573 var val = 1; | 1583 var val = 1; |
1574 var count = 0; | 1584 var count = 0; |
1575 for (val = 2147483648; 2147483648;) { | 1585 for (val = 2147483648; 2147483648;) { |
(...skipping 15 matching lines...) Expand all Loading... |
1591 break; | 1601 break; |
1592 } | 1602 } |
1593 } while (0xffffffff); | 1603 } while (0xffffffff); |
1594 if ((val>>>0) == 2147483668) { | 1604 if ((val>>>0) == 2147483668) { |
1595 return 323; | 1605 return 323; |
1596 } | 1606 } |
1597 return 0; | 1607 return 0; |
1598 } | 1608 } |
1599 return {main:main}; | 1609 return {main:main}; |
1600 } | 1610 } |
1601 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); | 1611 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString(), stdlib); |
1602 assertEquals(323, wasm.main()); | 1612 assertEquals(323, wasm.main()); |
1603 })(); | 1613 })(); |
OLD | NEW |