• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3// found in the LICENSE file.
4
5// Flags: --expose-wasm --expose-gc --allow-natives-syntax
6
7load("test/mjsunit/wasm/wasm-constants.js");
8load("test/mjsunit/wasm/wasm-module-builder.js");
9
10function 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
33function makeBinop(opcode) {
34  var builder = new WasmModuleBuilder();
35
36  builder.addFunction("main", kSig_i_ii)
37    .addBody([
38      kExprGetLocal, 0,           // --
39      kExprGetLocal, 1,           // --
40      opcode,                     // --
41    ])
42    .exportFunc();
43
44  return builder.instantiate().exports.main;
45}
46
47var divs = makeBinop(kExprI32DivS);
48var divu = makeBinop(kExprI32DivU);
49
50assertEquals( 33, divs( 333, 10));
51assertEquals(-33, divs(-336, 10));
52
53assertEquals(       44, divu( 445, 10));
54assertEquals(429496685, divu(-446, 10));
55
56assertTraps(kTrapDivByZero, "divs(100, 0);");
57assertTraps(kTrapDivByZero, "divs(-1009, 0);");
58
59assertTraps(kTrapDivByZero, "divu(200, 0);");
60assertTraps(kTrapDivByZero, "divu(-2009, 0);");
61
62assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
63assertEquals(0, divu(0x80000000, -1));
64
65
66var rems = makeBinop(kExprI32RemS);
67var remu = makeBinop(kExprI32RemU);
68
69assertEquals( 3, rems( 333, 10));
70assertEquals(-6, rems(-336, 10));
71
72assertEquals( 5, remu( 445, 10));
73assertEquals( 3, remu(-443, 10));
74
75assertTraps(kTrapRemByZero, "rems(100, 0);");
76assertTraps(kTrapRemByZero, "rems(-1009, 0);");
77
78assertTraps(kTrapRemByZero, "remu(200, 0);");
79assertTraps(kTrapRemByZero, "remu(-2009, 0);");
80
81assertEquals(-2147483648, remu(0x80000000, -1));
82assertEquals(0, rems(0x80000000, -1));
83