summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSimon Hafner <hafnersimon@gmail.com>2015-01-29 05:30:55 -0600
committerSimon Hafner <hafnersimon@gmail.com>2015-01-29 05:30:55 -0600
commit11292a9e0b2d614948c8b7cca780a1545a2cce34 (patch)
treecfea340a6e46884e52527948041d02001aca982c
parentd4786976dd80ad7aef531b259cf04ab65e80dabc (diff)
downloadNim-11292a9e0b2d614948c8b7cca780a1545a2cce34.tar.gz
Fixes #1959, rounding floats in JS
-rw-r--r--compiler/jsgen.nim11
-rw-r--r--tests/js/tfloatround.nim7
2 files changed, 17 insertions, 1 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index c48bf8c91..beb84b00a 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1448,9 +1448,18 @@ proc genConv(p: PProc, n: PNode, r: var TCompRes) =
   var dest = skipTypes(n.typ, abstractVarRange)
   var src = skipTypes(n.sons[1].typ, abstractVarRange)
   gen(p, n.sons[1], r)
-  if (dest.kind != src.kind) and (src.kind == tyBool): 
+  if dest.kind == src.kind:
+    # no-op conversion
+    return
+  case dest.kind:
+  of tyBool:
     r.res = ropef("(($1)? 1:0)" | "toBool($#)", [r.res])
     r.kind = resExpr
+  of tyInt:
+    r.res = ropef("($1|0)", [r.res])
+  else:
+    # TODO: What types must we handle here?
+    discard
   
 proc upConv(p: PProc, n: PNode, r: var TCompRes) = 
   gen(p, n.sons[0], r)        # XXX
diff --git a/tests/js/tfloatround.nim b/tests/js/tfloatround.nim
new file mode 100644
index 000000000..7bc5430e6
--- /dev/null
+++ b/tests/js/tfloatround.nim
@@ -0,0 +1,7 @@
+discard """
+  output: '''
+3
+'''
+"""
+
+echo int(22 / 7)