From 339ebe958dad77caa04c50161a00288da8c3cab3 Mon Sep 17 00:00:00 2001 From: flywind <43030857+xflywind@users.noreply.github.com> Date: Mon, 15 Feb 2021 07:22:46 -0600 Subject: [PATCH] JS: fix div uint64 no truncation (#16899) --- compiler/jsgen.nim | 5 ++++- tests/arithm/tdiv.nim | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/arithm/tdiv.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 04009250e8..5d96907b56 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -671,7 +671,10 @@ proc arith(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = of mAddU: binaryUintExpr(p, n, r, "+") of mSubU: binaryUintExpr(p, n, r, "-") of mMulU: binaryUintExpr(p, n, r, "*") - of mDivU: binaryUintExpr(p, n, r, "/") + of mDivU: + binaryUintExpr(p, n, r, "/") + if n[1].typ.skipTypes(abstractRange).size == 8: + r.res = "Math.trunc($1)" % [r.res] of mDivI: arithAux(p, n, r, op) of mModI: diff --git a/tests/arithm/tdiv.nim b/tests/arithm/tdiv.nim new file mode 100644 index 0000000000..5d8eed84dc --- /dev/null +++ b/tests/arithm/tdiv.nim @@ -0,0 +1,19 @@ +discard """ + targets: "c js" +""" + + +block divUint64: + proc divTest() = + let x1 = 12'u16 + let y = x1 div 5'u16 + let x2 = 1345567'u32 + let z = x2 div 5'u32 + let a = 1345567'u64 div uint64(x1) + doAssert y == 2 + doAssert z == 269113 + doAssert a == 112130 + + static: divTest() + divTest() +