OLD | NEW |
(Empty) | |
| 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-gc --allow-natives-syntax |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 |
| 9 function 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 |
| 32 function 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 = WASM.instantiateModule(data); |
| 56 |
| 57 assertEquals("function", typeof module.main); |
| 58 |
| 59 return module.main; |
| 60 } |
| 61 |
| 62 var divs = makeDivRem(kExprI32DivS); |
| 63 var divu = makeDivRem(kExprI32DivU); |
| 64 |
| 65 assertEquals( 33, divs( 333, 10)); |
| 66 assertEquals(-33, divs(-336, 10)); |
| 67 |
| 68 assertEquals( 44, divu( 445, 10)); |
| 69 assertEquals(429496685, divu(-446, 10)); |
| 70 |
| 71 assertTraps(kTrapDivByZero, "divs(100, 0);"); |
| 72 assertTraps(kTrapDivByZero, "divs(-1009, 0);"); |
| 73 |
| 74 assertTraps(kTrapDivByZero, "divu(200, 0);"); |
| 75 assertTraps(kTrapDivByZero, "divu(-2009, 0);"); |
| 76 |
| 77 assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)"); |
| 78 assertEquals(0, divu(0x80000000, -1)); |
| 79 |
| 80 |
| 81 var rems = makeDivRem(kExprI32RemS); |
| 82 var remu = makeDivRem(kExprI32RemU); |
| 83 |
| 84 assertEquals( 3, rems( 333, 10)); |
| 85 assertEquals(-6, rems(-336, 10)); |
| 86 |
| 87 assertEquals( 5, remu( 445, 10)); |
| 88 assertEquals( 3, remu(-443, 10)); |
| 89 |
| 90 assertTraps(kTrapRemByZero, "rems(100, 0);"); |
| 91 assertTraps(kTrapRemByZero, "rems(-1009, 0);"); |
| 92 |
| 93 assertTraps(kTrapRemByZero, "remu(200, 0);"); |
| 94 assertTraps(kTrapRemByZero, "remu(-2009, 0);"); |
| 95 |
| 96 assertEquals(-2147483648, remu(0x80000000, -1)); |
| 97 assertEquals(0, rems(0x80000000, -1)); |
OLD | NEW |