summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--changelog.md2
-rw-r--r--lib/system.nim9
-rw-r--r--lib/system/dollars.nim17
-rw-r--r--tests/system/tdollars.nim72
4 files changed, 96 insertions, 4 deletions
diff --git a/changelog.md b/changelog.md
index 80f3c8e72..cf7c18a8b 100644
--- a/changelog.md
+++ b/changelog.md
@@ -43,6 +43,8 @@
 
 - Added high-level `asyncnet.sendTo` and `asyncnet.recvFrom`. UDP functionality.
 
+- `dollars.$` now works for unsigned ints with `nim js`
+
 ## Language changes
 - In newruntime it is now allowed to assign discriminator field without restrictions as long as case object doesn't have custom destructor. Discriminator value doesn't have to be a constant either. If you have custom destructor for case object and you do want to freely assign discriminator fields, it is recommended to refactor object into 2 objects like this:
   ```nim
diff --git a/lib/system.nim b/lib/system.nim
index 3ba2050b6..541c691eb 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2049,10 +2049,6 @@ template unlikely*(val: bool): bool =
     else:
       unlikelyProc(val)
 
-
-import system/dollars
-export dollars
-
 const
   NimMajor* {.intdefine.}: int = 1
     ## is the major number of Nim's version. Example:
@@ -2063,10 +2059,15 @@ const
 
   NimMinor* {.intdefine.}: int = 3
     ## is the minor number of Nim's version.
+    ## Odd for devel, even for releases.
 
   NimPatch* {.intdefine.}: int = 1
     ## is the patch number of Nim's version.
 
+import system/dollars
+export dollars
+
+const
   NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch
     ## is the version of Nim as a string.
 
diff --git a/lib/system/dollars.nim b/lib/system/dollars.nim
index 08f230c6a..836d5764c 100644
--- a/lib/system/dollars.nim
+++ b/lib/system/dollars.nim
@@ -1,8 +1,25 @@
+include system/inclrtl
+
 proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
   ## The stringify operator for an integer argument. Returns `x`
   ## converted to a decimal string. ``$`` is Nim's general way of
   ## spelling `toString`:idx:.
 
+when defined(js):
+  since (1, 3):
+    proc `$`*(x: uint): string =
+      ## Caveat: currently implemented as $(cast[int](x)), tied to current
+      ## semantics of js' Number type.
+      # for c, see strmantle.`$`
+      $(cast[int](x))
+
+    proc `$`*(x: uint64): string =
+      ## Compatibility note:
+      ## the results may change in future releases if/when js target implements
+      ## 64bit ints.
+      # pending https://github.com/nim-lang/RFCs/issues/187
+      $(cast[int](x))
+
 proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
   ## The stringify operator for an integer argument. Returns `x`
   ## converted to a decimal string.
diff --git a/tests/system/tdollars.nim b/tests/system/tdollars.nim
new file mode 100644
index 000000000..478a11b94
--- /dev/null
+++ b/tests/system/tdollars.nim
@@ -0,0 +1,72 @@
+discard """
+  targets: "c cpp js"
+"""
+
+#[
+if https://github.com/nim-lang/Nim/pull/14043 is merged (or at least its
+tests/system/tostring.nim diff subset), merge
+tests/system/tostring.nim into this file, named after dollars.nim
+
+The goal is to increase test coverage across backends while minimizing test code
+duplication (which always results in weaker test coverage in practice).
+]#
+
+import std/unittest
+template test[T](a: T, expected: string) =
+  check $a == expected
+  var b = a
+  check $b == expected
+  static:
+    doAssert $a == expected
+
+template testType(T: typedesc) =
+  when T is bool:
+    test true, "true"
+    test false, "false"
+  elif T is char:
+    test char, "\0"
+    test char.high, static($T.high)
+  else:
+    test T.default, "0"
+    test 1.T, "1"
+    test T.low, static($T.low)
+    test T.high, static($T.high)
+
+block: # `$`(SomeInteger)
+  # direct tests
+  check $0'u8 == "0"
+  check $255'u8 == "255"
+  check $(-127'i8) == "-127"
+
+  # known limitation: Error: number out of range: '128'i8',
+  # see https://github.com/timotheecour/Nim/issues/125
+  # check $(-128'i8) == "-128"
+
+  check $int8.low == "-128"
+  check $int8(-128) == "-128"
+  when not defined js: # pending https://github.com/nim-lang/Nim/issues/14127
+    check $cast[int8](-128) == "-128"
+
+  var a = 12345'u16
+  check $a == "12345"
+  check $12345678'u64 == "12345678"
+  check $12345678'i64 == "12345678"
+  check $(-12345678'i64) == "-12345678"
+
+  # systematic tests
+  testType uint8
+  testType uint16
+  testType uint32
+  testType uint
+
+  testType int8
+  testType int16
+  testType int32
+
+  testType int
+  testType bool
+
+  when not defined(js): # requires BigInt support
+    testType uint64
+    testType int64
+    testType BiggestInt