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 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 function assertTraps(code, msg) { | |
11 var threwException = true; | |
12 try { | |
13 if (typeof code === 'function') { | |
14 code(); | |
15 } else { | |
16 eval(code); | |
17 } | |
18 threwException = false; | |
19 } catch (e) { | |
20 if (typeof type_opt === 'function') { | |
21 assertInstanceof(e, type_opt); | |
22 } | |
23 if (arguments.length >= 3) { | |
24 assertEquals(e.type, cause_opt); | |
25 } | |
26 // Success. | |
27 return; | |
28 } | |
29 throw new MjsUnitAssertionError("Did not throw exception"); | |
30 } | |
31 | |
32 | |
33 function makeBinop(opcode) { | 10 function makeBinop(opcode) { |
34 var builder = new WasmModuleBuilder(); | 11 var builder = new WasmModuleBuilder(); |
35 | 12 |
36 builder.addFunction("main", kSig_i_ii) | 13 builder.addFunction("main", kSig_i_ii) |
37 .addBody([ | 14 .addBody([ |
38 kExprGetLocal, 0, // -- | 15 kExprGetLocal, 0, // -- |
39 kExprGetLocal, 1, // -- | 16 kExprGetLocal, 1, // -- |
40 opcode, // -- | 17 opcode, // -- |
41 ]) | 18 ]) |
42 .exportFunc(); | 19 .exportFunc(); |
43 | 20 |
44 return builder.instantiate().exports.main; | 21 return builder.instantiate().exports.main; |
45 } | 22 } |
46 | 23 |
47 var divs = makeBinop(kExprI32DivS); | 24 var divs = makeBinop(kExprI32DivS); |
48 var divu = makeBinop(kExprI32DivU); | 25 var divu = makeBinop(kExprI32DivU); |
49 | 26 |
50 assertEquals( 33, divs( 333, 10)); | 27 assertEquals( 33, divs( 333, 10)); |
51 assertEquals(-33, divs(-336, 10)); | 28 assertEquals(-33, divs(-336, 10)); |
52 | 29 |
53 assertEquals( 44, divu( 445, 10)); | 30 assertEquals( 44, divu( 445, 10)); |
54 assertEquals(429496685, divu(-446, 10)); | 31 assertEquals(429496685, divu(-446, 10)); |
55 | 32 |
56 assertTraps(kTrapDivByZero, "divs(100, 0);"); | 33 assertThrows(() => divs(100, 0), WebAssembly.RuntimeError, |
titzer
2016/11/24 15:37:02
I think we can still keep the helper function, but
ahaas
2016/11/28 09:47:53
Done.
| |
57 assertTraps(kTrapDivByZero, "divs(-1009, 0);"); | 34 kTrapMsgs[kTrapDivByZero]); |
35 assertThrows(() => divs(-1009, 0), WebAssembly.RuntimeError, | |
36 kTrapMsgs[kTrapDivByZero]); | |
58 | 37 |
59 assertTraps(kTrapDivByZero, "divu(200, 0);"); | 38 assertThrows(() => divu(200, 0), WebAssembly.RuntimeError, |
60 assertTraps(kTrapDivByZero, "divu(-2009, 0);"); | 39 kTrapMsgs[kTrapDivByZero]); |
40 assertThrows(() => divu(-2009, 0), WebAssembly.RuntimeError, | |
41 kTrapMsgs[kTrapDivByZero]); | |
61 | 42 |
62 assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)"); | 43 assertThrows(() => divs(0x80000000, -1), WebAssembly.RuntimeError, |
44 kTrapMsgs[kTrapDivUnrepresentable]); | |
63 assertEquals(0, divu(0x80000000, -1)); | 45 assertEquals(0, divu(0x80000000, -1)); |
64 | 46 |
65 | 47 |
66 var rems = makeBinop(kExprI32RemS); | 48 var rems = makeBinop(kExprI32RemS); |
67 var remu = makeBinop(kExprI32RemU); | 49 var remu = makeBinop(kExprI32RemU); |
68 | 50 |
69 assertEquals( 3, rems( 333, 10)); | 51 assertEquals( 3, rems( 333, 10)); |
70 assertEquals(-6, rems(-336, 10)); | 52 assertEquals(-6, rems(-336, 10)); |
71 | 53 |
72 assertEquals( 5, remu( 445, 10)); | 54 assertEquals( 5, remu( 445, 10)); |
73 assertEquals( 3, remu(-443, 10)); | 55 assertEquals( 3, remu(-443, 10)); |
74 | 56 |
75 assertTraps(kTrapRemByZero, "rems(100, 0);"); | 57 assertThrows(() => rems(100, 0), WebAssembly.RuntimeError, |
76 assertTraps(kTrapRemByZero, "rems(-1009, 0);"); | 58 kTrapMsgs[kTrapRemByZero]); |
59 assertThrows(() => rems(-1009, 0), WebAssembly.RuntimeError, | |
60 kTrapMsgs[kTrapRemByZero]); | |
77 | 61 |
78 assertTraps(kTrapRemByZero, "remu(200, 0);"); | 62 assertThrows(() => remu(200, 0), WebAssembly.RuntimeError, |
79 assertTraps(kTrapRemByZero, "remu(-2009, 0);"); | 63 kTrapMsgs[kTrapRemByZero]); |
64 assertThrows(() => remu(-2009, 0), WebAssembly.RuntimeError, | |
65 kTrapMsgs[kTrapRemByZero]); | |
80 | 66 |
81 assertEquals(-2147483648, remu(0x80000000, -1)); | 67 assertEquals(-2147483648, remu(0x80000000, -1)); |
82 assertEquals(0, rems(0x80000000, -1)); | 68 assertEquals(0, rems(0x80000000, -1)); |
OLD | NEW |