blob: 9787ae34c9d5dca2b87643f47fdae9f6b7f54ac6 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5// Flags: --expose-wasm --expose-gc --allow-natives-syntax
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9function assertTraps(code, msg) {
10 var threwException = true;
11 try {
12 if (typeof code === 'function') {
13 code();
14 } else {
15 eval(code);
16 }
17 threwException = false;
18 } catch (e) {
19 if (typeof type_opt === 'function') {
20 assertInstanceof(e, type_opt);
21 }
22 if (arguments.length >= 3) {
23 assertEquals(e.type, cause_opt);
24 }
25 // Success.
26 return;
27 }
28 throw new MjsUnitAssertionError("Did not throw exception");
29}
30
31
32function makeDivRem(opcode) {
33 var kBodySize = 5;
34 var kNameMainOffset = 6 + 11 + kBodySize + 1;
35
36 var data = bytes(
37 // signatures
38 kDeclSignatures, 1,
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
55 var module = _WASMEXP_.instantiateModule(data);
56
57 assertEquals("function", typeof module.main);
58
59 return module.main;
60}
61
62var divs = makeDivRem(kExprI32DivS);
63var divu = makeDivRem(kExprI32DivU);
64
65assertEquals( 33, divs( 333, 10));
66assertEquals(-33, divs(-336, 10));
67
68assertEquals( 44, divu( 445, 10));
69assertEquals(429496685, divu(-446, 10));
70
71assertTraps(kTrapDivByZero, "divs(100, 0);");
72assertTraps(kTrapDivByZero, "divs(-1009, 0);");
73
74assertTraps(kTrapDivByZero, "divu(200, 0);");
75assertTraps(kTrapDivByZero, "divu(-2009, 0);");
76
77assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
78assertEquals(0, divu(0x80000000, -1));
79
80
81var rems = makeDivRem(kExprI32RemS);
82var remu = makeDivRem(kExprI32RemU);
83
84assertEquals( 3, rems( 333, 10));
85assertEquals(-6, rems(-336, 10));
86
87assertEquals( 5, remu( 445, 10));
88assertEquals( 3, remu(-443, 10));
89
90assertTraps(kTrapRemByZero, "rems(100, 0);");
91assertTraps(kTrapRemByZero, "rems(-1009, 0);");
92
93assertTraps(kTrapRemByZero, "remu(200, 0);");
94assertTraps(kTrapRemByZero, "remu(-2009, 0);");
95
96assertEquals(-2147483648, remu(0x80000000, -1));
97assertEquals(0, rems(0x80000000, -1));