summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorkonsumlamm <44230978+konsumlamm@users.noreply.github.com>2023-07-25 17:56:14 +0200
committerGitHub <noreply@github.com>2023-07-25 17:56:14 +0200
commitc0994c2dbdaaa6276b91c206d3377d68789f49ec (patch)
treed7d7ac8078a6bb5271097287e44aef35a349de54 /compiler
parent1c2ccfad08191e936fadd52450b53dfea105a34d (diff)
downloadNim-c0994c2dbdaaa6276b91c206d3377d68789f49ec.tar.gz
[JS] Fix casting to ints (#22327)
* [JS] Fix casting to ints

* Simplify `genCast` by using `asUintN`/`asIntN`
Diffstat (limited to 'compiler')
-rw-r--r--compiler/jsgen.nim26
1 files changed, 7 insertions, 19 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 1fbf6c74c..4a62cbf9e 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: