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