2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-12-17 10:54:08 +00:00
|
|
|
// Flags: --expose-wasm --expose-gc --allow-natives-syntax
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-11-28 10:26:30 +00:00
|
|
|
var assertTraps = function(messageId, code) {
|
|
|
|
assertThrows(code, WebAssembly.RuntimeError, kTrapMsgs[messageId]);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
function makeBinop(opcode) {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("main", kSig_i_ii)
|
2016-04-29 09:15:26 +00:00
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, // --
|
|
|
|
kExprLocalGet, 1, // --
|
2016-04-29 09:15:26 +00:00
|
|
|
opcode, // --
|
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
return builder.instantiate().exports.main;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
var divs = makeBinop(kExprI32DivS);
|
|
|
|
var divu = makeBinop(kExprI32DivU);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
assertEquals( 33, divs( 333, 10));
|
|
|
|
assertEquals(-33, divs(-336, 10));
|
|
|
|
|
|
|
|
assertEquals( 44, divu( 445, 10));
|
|
|
|
assertEquals(429496685, divu(-446, 10));
|
|
|
|
|
|
|
|
assertTraps(kTrapDivByZero, "divs(100, 0);");
|
|
|
|
assertTraps(kTrapDivByZero, "divs(-1009, 0);");
|
|
|
|
|
|
|
|
assertTraps(kTrapDivByZero, "divu(200, 0);");
|
|
|
|
assertTraps(kTrapDivByZero, "divu(-2009, 0);");
|
|
|
|
|
|
|
|
assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
|
|
|
|
assertEquals(0, divu(0x80000000, -1));
|
|
|
|
|
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
var rems = makeBinop(kExprI32RemS);
|
|
|
|
var remu = makeBinop(kExprI32RemU);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
assertEquals( 3, rems( 333, 10));
|
|
|
|
assertEquals(-6, rems(-336, 10));
|
|
|
|
|
|
|
|
assertEquals( 5, remu( 445, 10));
|
|
|
|
assertEquals( 3, remu(-443, 10));
|
|
|
|
|
|
|
|
assertTraps(kTrapRemByZero, "rems(100, 0);");
|
|
|
|
assertTraps(kTrapRemByZero, "rems(-1009, 0);");
|
|
|
|
|
|
|
|
assertTraps(kTrapRemByZero, "remu(200, 0);");
|
|
|
|
assertTraps(kTrapRemByZero, "remu(-2009, 0);");
|
|
|
|
|
|
|
|
assertEquals(-2147483648, remu(0x80000000, -1));
|
|
|
|
assertEquals(0, rems(0x80000000, -1));
|