Skip to content

Commit a3a317b

Browse files
authored
$ now works for unsigned intergers with nim js (#14122)
* $(unsigned) now works for js * move NimMajor+friends closer to NimVersion according as per reviewer feedback
1 parent 0e6eb96 commit a3a317b

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
- Added high-level `asyncnet.sendTo` and `asyncnet.recvFrom`. UDP functionality.
4545

46+
- `dollars.$` now works for unsigned ints with `nim js`
47+
4648
## Language changes
4749
- 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:
4850
```nim

lib/system.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,10 +2049,6 @@ template unlikely*(val: bool): bool =
20492049
else:
20502050
unlikelyProc(val)
20512051

2052-
2053-
import system/dollars
2054-
export dollars
2055-
20562052
const
20572053
NimMajor* {.intdefine.}: int = 1
20582054
## is the major number of Nim's version. Example:
@@ -2063,10 +2059,15 @@ const
20632059

20642060
NimMinor* {.intdefine.}: int = 3
20652061
## is the minor number of Nim's version.
2062+
## Odd for devel, even for releases.
20662063

20672064
NimPatch* {.intdefine.}: int = 1
20682065
## is the patch number of Nim's version.
20692066

2067+
import system/dollars
2068+
export dollars
2069+
2070+
const
20702071
NimVersion*: string = $NimMajor & "." & $NimMinor & "." & $NimPatch
20712072
## is the version of Nim as a string.
20722073

lib/system/dollars.nim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1+
include system/inclrtl
2+
13
proc `$`*(x: int): string {.magic: "IntToStr", noSideEffect.}
24
## The stringify operator for an integer argument. Returns `x`
35
## converted to a decimal string. ``$`` is Nim's general way of
46
## spelling `toString`:idx:.
57

8+
when defined(js):
9+
since (1, 3):
10+
proc `$`*(x: uint): string =
11+
## Caveat: currently implemented as $(cast[int](x)), tied to current
12+
## semantics of js' Number type.
13+
# for c, see strmantle.`$`
14+
$(cast[int](x))
15+
16+
proc `$`*(x: uint64): string =
17+
## Compatibility note:
18+
## the results may change in future releases if/when js target implements
19+
## 64bit ints.
20+
# pending https://github.com/nim-lang/RFCs/issues/187
21+
$(cast[int](x))
22+
623
proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
724
## The stringify operator for an integer argument. Returns `x`
825
## converted to a decimal string.

tests/system/tdollars.nim

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
discard """
2+
targets: "c cpp js"
3+
"""
4+
5+
#[
6+
if https://github.com/nim-lang/Nim/pull/14043 is merged (or at least its
7+
tests/system/tostring.nim diff subset), merge
8+
tests/system/tostring.nim into this file, named after dollars.nim
9+
10+
The goal is to increase test coverage across backends while minimizing test code
11+
duplication (which always results in weaker test coverage in practice).
12+
]#
13+
14+
import std/unittest
15+
template test[T](a: T, expected: string) =
16+
check $a == expected
17+
var b = a
18+
check $b == expected
19+
static:
20+
doAssert $a == expected
21+
22+
template testType(T: typedesc) =
23+
when T is bool:
24+
test true, "true"
25+
test false, "false"
26+
elif T is char:
27+
test char, "\0"
28+
test char.high, static($T.high)
29+
else:
30+
test T.default, "0"
31+
test 1.T, "1"
32+
test T.low, static($T.low)
33+
test T.high, static($T.high)
34+
35+
block: # `$`(SomeInteger)
36+
# direct tests
37+
check $0'u8 == "0"
38+
check $255'u8 == "255"
39+
check $(-127'i8) == "-127"
40+
41+
# known limitation: Error: number out of range: '128'i8',
42+
# see https://github.com/timotheecour/Nim/issues/125
43+
# check $(-128'i8) == "-128"
44+
45+
check $int8.low == "-128"
46+
check $int8(-128) == "-128"
47+
when not defined js: # pending https://github.com/nim-lang/Nim/issues/14127
48+
check $cast[int8](-128) == "-128"
49+
50+
var a = 12345'u16
51+
check $a == "12345"
52+
check $12345678'u64 == "12345678"
53+
check $12345678'i64 == "12345678"
54+
check $(-12345678'i64) == "-12345678"
55+
56+
# systematic tests
57+
testType uint8
58+
testType uint16
59+
testType uint32
60+
testType uint
61+
62+
testType int8
63+
testType int16
64+
testType int32
65+
66+
testType int
67+
testType bool
68+
69+
when not defined(js): # requires BigInt support
70+
testType uint64
71+
testType int64
72+
testType BiggestInt

0 commit comments

Comments
 (0)