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 --expose-gc --allow-natives-syntax | 5 // Flags: --expose-wasm --expose-gc --allow-natives-syntax |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
8 | 9 |
9 function assertTraps(code, msg) { | 10 function assertTraps(code, msg) { |
10 var threwException = true; | 11 var threwException = true; |
11 try { | 12 try { |
12 if (typeof code === 'function') { | 13 if (typeof code === 'function') { |
13 code(); | 14 code(); |
14 } else { | 15 } else { |
15 eval(code); | 16 eval(code); |
16 } | 17 } |
17 threwException = false; | 18 threwException = false; |
18 } catch (e) { | 19 } catch (e) { |
19 if (typeof type_opt === 'function') { | 20 if (typeof type_opt === 'function') { |
20 assertInstanceof(e, type_opt); | 21 assertInstanceof(e, type_opt); |
21 } | 22 } |
22 if (arguments.length >= 3) { | 23 if (arguments.length >= 3) { |
23 assertEquals(e.type, cause_opt); | 24 assertEquals(e.type, cause_opt); |
24 } | 25 } |
25 // Success. | 26 // Success. |
26 return; | 27 return; |
27 } | 28 } |
28 throw new MjsUnitAssertionError("Did not throw exception"); | 29 throw new MjsUnitAssertionError("Did not throw exception"); |
29 } | 30 } |
30 | 31 |
31 | 32 |
32 function makeDivRem(opcode) { | 33 function makeBinop(opcode) { |
33 var kBodySize = 5; | 34 var builder = new WasmModuleBuilder(); |
34 var kNameMainOffset = kHeaderSize + 6 + 11 + kBodySize + 1; | |
35 | 35 |
36 var data = bytesWithHeader( | 36 builder.addFunction("main", [kAstI32, kAstI32, kAstI32]) |
37 // signatures | 37 .addBody([opcode, kExprGetLocal, 0, kExprGetLocal, 1]) |
38 kDeclSignatures, 1, | 38 .exportFunc(); |
39 2, kAstI32, kAstI32, kAstI32, // (int,int) -> int | |
40 // -- main function | |
41 kDeclFunctions, 1, | |
42 kDeclFunctionName | kDeclFunctionExport, | |
43 0, 0, | |
44 kNameMainOffset, 0, 0, 0, // name offset | |
45 kBodySize, 0, | |
46 // main body | |
47 opcode, // -- | |
48 kExprGetLocal, 0, // -- | |
49 kExprGetLocal, 1, // -- | |
50 // names | |
51 kDeclEnd, | |
52 'm', 'a', 'i', 'n', 0 // -- | |
53 ); | |
54 | 39 |
55 var module = _WASMEXP_.instantiateModule(data); | 40 return builder.instantiate().exports.main; |
56 | |
57 assertEquals("function", typeof module.main); | |
58 | |
59 return module.main; | |
60 } | 41 } |
61 | 42 |
62 var divs = makeDivRem(kExprI32DivS); | 43 var divs = makeBinop(kExprI32DivS); |
63 var divu = makeDivRem(kExprI32DivU); | 44 var divu = makeBinop(kExprI32DivU); |
64 | 45 |
65 assertEquals( 33, divs( 333, 10)); | 46 assertEquals( 33, divs( 333, 10)); |
66 assertEquals(-33, divs(-336, 10)); | 47 assertEquals(-33, divs(-336, 10)); |
67 | 48 |
68 assertEquals( 44, divu( 445, 10)); | 49 assertEquals( 44, divu( 445, 10)); |
69 assertEquals(429496685, divu(-446, 10)); | 50 assertEquals(429496685, divu(-446, 10)); |
70 | 51 |
71 assertTraps(kTrapDivByZero, "divs(100, 0);"); | 52 assertTraps(kTrapDivByZero, "divs(100, 0);"); |
72 assertTraps(kTrapDivByZero, "divs(-1009, 0);"); | 53 assertTraps(kTrapDivByZero, "divs(-1009, 0);"); |
73 | 54 |
74 assertTraps(kTrapDivByZero, "divu(200, 0);"); | 55 assertTraps(kTrapDivByZero, "divu(200, 0);"); |
75 assertTraps(kTrapDivByZero, "divu(-2009, 0);"); | 56 assertTraps(kTrapDivByZero, "divu(-2009, 0);"); |
76 | 57 |
77 assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)"); | 58 assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)"); |
78 assertEquals(0, divu(0x80000000, -1)); | 59 assertEquals(0, divu(0x80000000, -1)); |
79 | 60 |
80 | 61 |
81 var rems = makeDivRem(kExprI32RemS); | 62 var rems = makeBinop(kExprI32RemS); |
82 var remu = makeDivRem(kExprI32RemU); | 63 var remu = makeBinop(kExprI32RemU); |
83 | 64 |
84 assertEquals( 3, rems( 333, 10)); | 65 assertEquals( 3, rems( 333, 10)); |
85 assertEquals(-6, rems(-336, 10)); | 66 assertEquals(-6, rems(-336, 10)); |
86 | 67 |
87 assertEquals( 5, remu( 445, 10)); | 68 assertEquals( 5, remu( 445, 10)); |
88 assertEquals( 3, remu(-443, 10)); | 69 assertEquals( 3, remu(-443, 10)); |
89 | 70 |
90 assertTraps(kTrapRemByZero, "rems(100, 0);"); | 71 assertTraps(kTrapRemByZero, "rems(100, 0);"); |
91 assertTraps(kTrapRemByZero, "rems(-1009, 0);"); | 72 assertTraps(kTrapRemByZero, "rems(-1009, 0);"); |
92 | 73 |
93 assertTraps(kTrapRemByZero, "remu(200, 0);"); | 74 assertTraps(kTrapRemByZero, "remu(200, 0);"); |
94 assertTraps(kTrapRemByZero, "remu(-2009, 0);"); | 75 assertTraps(kTrapRemByZero, "remu(-2009, 0);"); |
95 | 76 |
96 assertEquals(-2147483648, remu(0x80000000, -1)); | 77 assertEquals(-2147483648, remu(0x80000000, -1)); |
97 assertEquals(0, rems(0x80000000, -1)); | 78 assertEquals(0, rems(0x80000000, -1)); |
OLD | NEW |