From c0994c2dbdaaa6276b91c206d3377d68789f49ec Mon Sep 17 00:00:00 2001 From: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Date: Tue, 25 Jul 2023 17:56:14 +0200 Subject: [PATCH] [JS] Fix casting to ints (#22327) * [JS] Fix casting to ints * Simplify `genCast` by using `asUintN`/`asIntN` --- compiler/jsgen.nim | 26 +++++++------------------- tests/cast/tcast.nim | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 tests/cast/tcast.nim diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 1fbf6c74c7..4a62cbf9eb 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -2727,26 +2727,14 @@ proc genCast(p: PProc, n: PNode, r: var TCompRes) = let fromInt = (src.kind in tyInt..tyInt32) let fromUint = (src.kind in tyUInt..tyUInt32) - if toUint and (fromInt or fromUint): - let trimmer = unsignedTrimmer(dest.size) - r.res = "($1 $2)" % [r.res, trimmer] - elif toUint and src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions: - r.res = "Number(BigInt.asUintN($1, $2))" % [$(dest.size * 8), r.res] + if toUint: + if fromInt or fromUint: + r.res = "Number(BigInt.asUintN($1, BigInt($2)))" % [$(dest.size * 8), r.res] + elif src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions: + r.res = "Number(BigInt.asUintN($1, $2))" % [$(dest.size * 8), r.res] elif toInt: - if fromInt: - return - elif fromUint: - if src.size == 4 and dest.size == 4: - # XXX prevent multi evaluations - r.res = "($1 | 0)" % [r.res] - else: - let trimmer = unsignedTrimmer(dest.size) - let minuend = case dest.size - of 1: "0xfe" - of 2: "0xfffe" - of 4: "0xfffffffe" - else: "" - r.res = "($1 - ($2 $3))" % [rope minuend, r.res, trimmer] + if fromInt or fromUint: + r.res = "Number(BigInt.asIntN($1, BigInt($2)))" % [$(dest.size * 8), r.res] elif src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions: r.res = "Number(BigInt.asIntN($1, $2))" % [$(dest.size * 8), r.res] elif dest.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions: diff --git a/tests/cast/tcast.nim b/tests/cast/tcast.nim new file mode 100644 index 0000000000..205444ea3a --- /dev/null +++ b/tests/cast/tcast.nim @@ -0,0 +1,21 @@ +discard """ + targets: "c cpp js" +""" + +proc main() = + block: # bug #16806 + let + a = 42u16 + b = cast[int16](a) + doAssert a.int16 == 42 + doAssert b in int16.low..int16.high + + block: # bug #16808 + doAssert cast[int8](cast[uint8](int8(-12))) == int8(-12) + doAssert cast[int16](cast[uint16](int16(-12))) == int16(-12) + doAssert cast[int32](cast[uint32](int32(-12))) == int32(-12) + + doAssert cast[int8](int16.high) == -1 + +static: main() +main()