Skip to content

Commit 041098e

Browse files
authored
clean up stdlib with --jsbigint64 (#24255)
refs #6978, refs #6752, refs #21613, refs #24234 The `jsNoInt64`, `whenHasBigInt64`, `whenJsNoBigInt64` templates are replaced with bool constants to use with `when`. Weird that I didn't do this in the first place. The `whenJsNoBigInt64` template was also slightly misleading. The first branch was compiled for both no bigint64 on JS as well as on C/C++. It seems only `trandom` depended on this by mistake. The workaround for #6752 added in #6978 to `times` is also removed with `--jsbigint64:on`, but #24233 was also encountered with this, so this PR depends on #24234.
1 parent 93c24fe commit 041098e

File tree

14 files changed

+74
-95
lines changed

14 files changed

+74
-95
lines changed

lib/js/jscore.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ proc newDate*(): DateTime {.
7777
proc newDate*(date: int|string): DateTime {.
7878
importcpp: "new Date(#)".}
7979

80-
whenJsNoBigInt64:
80+
when jsNoBigInt64:
8181
proc newDate*(date: int64): DateTime {.
8282
importcpp: "new Date(#)".}
83-
do:
83+
else:
8484
proc newDate*(date: int64): DateTime {.
8585
importcpp: "new Date(Number(#))".}
8686

lib/pure/hashes.nim

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ runnableExamples:
6565
## * `sha1 module <sha1.html>`_ for the SHA-1 checksum algorithm
6666
## * `tables module <tables.html>`_ for hash tables
6767

68-
import std/private/since
68+
import std/private/[since, jsutils]
6969

7070
when defined(nimPreviewSlimSystem):
7171
import std/assertions
@@ -518,17 +518,10 @@ proc hashFarm(s: openArray[byte]): uint64 {.inline.} =
518518
swap z, x
519519
len16 len16(v[0],w[0],mul) + shiftMix(y)*k0 + z, len16(v[1],w[1],mul) + x, mul
520520

521-
template jsNoInt64: untyped =
522-
when defined js:
523-
when compiles(compileOption("jsbigint64")):
524-
when not compileOption("jsbigint64"): true
525-
else: false
526-
else: false
527-
else: false
528-
const sHash2 = (when defined(nimStringHash2) or jsNoInt64(): true else: false)
521+
const sHash2 = defined(nimStringHash2) or jsNoBigInt64
529522

530523
template maybeFailJS_Number =
531-
when jsNoInt64() and not defined(nimStringHash2):
524+
when jsNoBigInt64 and not defined(nimStringHash2):
532525
{.error: "Must use `-d:nimStringHash2` when using `--jsbigint64:off`".}
533526

534527
proc hash*(x: string): Hash =

lib/pure/random.nim

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,13 @@ when defined(nimPreviewSlimSystem):
7979

8080
include system/inclrtl
8181
{.push debugger: off.}
82-
template whenHasBigInt64(yes64, no64): untyped =
83-
when defined(js):
84-
when compiles(compileOption("jsbigint64")):
85-
when compileOption("jsbigint64"):
86-
yes64
87-
else:
88-
no64
89-
else:
90-
no64
91-
else:
92-
yes64
9382

9483

95-
whenHasBigInt64:
84+
when hasWorkingInt64:
9685
type Ui = uint64
9786

9887
const randMax = 18_446_744_073_709_551_615u64
99-
do:
88+
else:
10089
type Ui = uint32
10190

10291
const randMax = 4_294_967_295u32
@@ -118,14 +107,14 @@ type
118107
## generator are **not** thread-safe!
119108
a0, a1: Ui
120109

121-
whenHasBigInt64:
110+
when hasWorkingInt64:
122111
const DefaultRandSeed = Rand(
123112
a0: 0x69B4C98CB8530805u64,
124113
a1: 0xFED1DD3004688D67CAu64)
125114

126115
# racy for multi-threading but good enough for now:
127116
var state = DefaultRandSeed # global for backwards compatibility
128-
do:
117+
else:
129118
var state = Rand(
130119
a0: 0x69B4C98Cu32,
131120
a1: 0xFED1DD30u32) # global for backwards compatibility
@@ -221,9 +210,9 @@ proc skipRandomNumbers*(s: var Rand) =
221210
doAssert vals == [501737, 497901, 500683, 500157]
222211

223212

224-
whenHasBigInt64:
213+
when hasWorkingInt64:
225214
const helper = [0xbeac0467eba5facbu64, 0xd86b048b86aa9922u64]
226-
do:
215+
else:
227216
const helper = [0xbeac0467u32, 0xd86b048bu32]
228217
var
229218
s0 = Ui 0
@@ -359,9 +348,9 @@ proc rand*[T: Ordinal or SomeFloat](r: var Rand; x: HSlice[T, T]): T =
359348
when T is SomeFloat:
360349
result = rand(r, x.b - x.a) + x.a
361350
else: # Integers and Enum types
362-
whenJsNoBigInt64:
351+
when jsNoBigInt64:
363352
result = cast[T](rand(r, cast[uint](x.b) - cast[uint](x.a)) + cast[uint](x.a))
364-
do:
353+
else:
365354
result = cast[T](rand(r, cast[uint64](x.b) - cast[uint64](x.a)) + cast[uint64](x.a))
366355

367356
proc rand*[T: Ordinal or SomeFloat](x: HSlice[T, T]): T =
@@ -402,9 +391,9 @@ proc rand*[T: Ordinal](r: var Rand; t: typedesc[T]): T {.since: (1, 7, 1).} =
402391
elif T is bool:
403392
result = r.next < randMax div 2
404393
else:
405-
whenJsNoBigInt64:
394+
when jsNoBigInt64:
406395
result = cast[T](r.next shr (sizeof(uint)*8 - sizeof(T)*8))
407-
do:
396+
else:
408397
result = cast[T](r.next shr (sizeof(uint64)*8 - sizeof(T)*8))
409398

410399
proc rand*[T: Ordinal](t: typedesc[T]): T =

lib/pure/strutils.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,9 @@ func toHex*[T: SomeInteger](x: T, len: Positive): string =
998998
doAssert b.toHex(4) == "1001"
999999
doAssert toHex(62, 3) == "03E"
10001000
doAssert toHex(-8, 6) == "FFFFF8"
1001-
whenJsNoBigInt64:
1001+
when jsNoBigInt64:
10021002
toHexImpl(cast[BiggestUInt](x), len, x < 0)
1003-
do:
1003+
else:
10041004
when T is SomeSignedInt:
10051005
toHexImpl(cast[BiggestUInt](BiggestInt(x)), len, x < 0)
10061006
else:
@@ -1011,9 +1011,9 @@ func toHex*[T: SomeInteger](x: T): string =
10111011
runnableExamples:
10121012
doAssert toHex(1984'i64) == "00000000000007C0"
10131013
doAssert toHex(1984'i16) == "07C0"
1014-
whenJsNoBigInt64:
1014+
when jsNoBigInt64:
10151015
toHexImpl(cast[BiggestUInt](x), 2*sizeof(T), x < 0)
1016-
do:
1016+
else:
10171017
when T is SomeSignedInt:
10181018
toHexImpl(cast[BiggestUInt](BiggestInt(x)), 2*sizeof(T), x < 0)
10191019
else:

lib/pure/times.nim

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,29 @@ when defined(nimPreviewSlimSystem):
215215

216216
when defined(js):
217217
import std/jscore
218-
219-
# This is really bad, but overflow checks are broken badly for
220-
# ints on the JS backend. See #6752.
221-
{.push overflowChecks: off.}
222-
proc `*`(a, b: int64): int64 =
223-
system.`*`(a, b)
224-
proc `*`(a, b: int): int =
225-
system.`*`(a, b)
226-
proc `+`(a, b: int64): int64 =
227-
system.`+`(a, b)
228-
proc `+`(a, b: int): int =
229-
system.`+`(a, b)
230-
proc `-`(a, b: int64): int64 =
231-
system.`-`(a, b)
232-
proc `-`(a, b: int): int =
233-
system.`-`(a, b)
234-
proc inc(a: var int, b: int) =
235-
system.inc(a, b)
236-
proc inc(a: var int64, b: int) =
237-
system.inc(a, b)
238-
{.pop.}
218+
import std/private/jsutils
219+
220+
when jsNoBigInt64:
221+
# This is really bad, but overflow checks are broken badly for
222+
# ints on the JS backend. See #6752.
223+
{.push overflowChecks: off.}
224+
proc `*`(a, b: int64): int64 =
225+
system.`*`(a, b)
226+
proc `*`(a, b: int): int =
227+
system.`*`(a, b)
228+
proc `+`(a, b: int64): int64 =
229+
system.`+`(a, b)
230+
proc `+`(a, b: int): int =
231+
system.`+`(a, b)
232+
proc `-`(a, b: int64): int64 =
233+
system.`-`(a, b)
234+
proc `-`(a, b: int): int =
235+
system.`-`(a, b)
236+
proc inc(a: var int, b: int) =
237+
system.inc(a, b)
238+
proc inc(a: var int64, b: int) =
239+
system.inc(a, b)
240+
{.pop.}
239241

240242
elif defined(posix):
241243
import std/posix

lib/std/private/jsutils.nim

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,21 @@ when defined(js):
8383
assert 9007199254740991.toJs.isSafeInteger
8484
assert not 9007199254740992.toJs.isSafeInteger
8585

86-
template whenJsNoBigInt64*(no64, yes64): untyped =
86+
const jsNoBigInt64* =
8787
when defined(js):
8888
when compiles(compileOption("jsbigint64")):
89-
when compileOption("jsbigint64"):
90-
yes64
91-
else:
92-
no64
89+
not compileOption("jsbigint64")
9390
else:
94-
no64
91+
true
9592
else:
96-
no64
93+
false
94+
95+
const hasWorkingInt64* =
96+
# equal to `not jsNoBigInt64`, but define it by itself anyway
97+
when defined(js):
98+
when compiles(compileOption("jsbigint64")):
99+
compileOption("jsbigint64")
100+
else:
101+
false
102+
else:
103+
true

tests/int/tints.nim

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ template test(opr, a, b, c: untyped): untyped =
2727

2828
test(`+`, 12'i8, -13'i16, -1'i16)
2929
test(`shl`, 0b11, 0b100, 0b110000)
30-
whenJsNoBigInt64: discard
31-
do:
30+
when hasWorkingInt64:
3231
test(`shl`, 0b11'i64, 0b100'i64, 0b110000'i64)
3332
when not defined(js):
3433
# mixed type shr needlessly complicates codegen with bigint
@@ -39,25 +38,21 @@ test(`shl`, 0b11'i32, 0b100'i32, 0b110000'i32)
3938
test(`or`, 0xf0f0'i16, 0x0d0d'i16, 0xfdfd'i16)
4039
test(`and`, 0xf0f0'i16, 0xfdfd'i16, 0xf0f0'i16)
4140

42-
whenJsNoBigInt64: discard
43-
do:
41+
when hasWorkingInt64:
4442
test(`shr`, 0xffffffffffffffff'i64, 0x4'i64, 0xffffffffffffffff'i64)
4543
test(`shr`, 0xffff'i16, 0x4'i16, 0xffff'i16)
4644
test(`shr`, 0xff'i8, 0x4'i8, 0xff'i8)
4745

48-
whenJsNoBigInt64: discard
49-
do:
46+
when hasWorkingInt64:
5047
test(`shr`, 0xffffffff'i64, 0x4'i64, 0x0fffffff'i64)
5148
test(`shr`, 0xffffffff'i32, 0x4'i32, 0xffffffff'i32)
5249

53-
whenJsNoBigInt64: discard
54-
do:
50+
when hasWorkingInt64:
5551
test(`shl`, 0xffffffffffffffff'i64, 0x4'i64, 0xfffffffffffffff0'i64)
5652
test(`shl`, 0xffff'i16, 0x4'i16, 0xfff0'i16)
5753
test(`shl`, 0xff'i8, 0x4'i8, 0xf0'i8)
5854

59-
whenJsNoBigInt64: discard
60-
do:
55+
when hasWorkingInt64:
6156
test(`shl`, 0xffffffff'i64, 0x4'i64, 0xffffffff0'i64)
6257
test(`shl`, 0xffffffff'i32, 0x4'i32, 0xfffffff0'i32)
6358

tests/js/ttypedarray.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ proc main()=
1010
doAssert fn(array[2, uint8].default) == "Uint8Array"
1111
doAssert fn(array[2, byte].default) == "Uint8Array"
1212
doAssert fn(array[2, char].default) == "Uint8Array"
13-
whenJsNoBigInt64: discard
14-
do:
13+
when not jsNoBigInt64:
1514
doAssert fn(array[2, uint64].default) == "BigUint64Array"
1615
doAssert fn([1'u8]) == "Uint8Array"
1716
doAssert fn([1'u16]) == "Uint16Array"

tests/lexer/tunary_minus.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ template main =
6161
doAssert -2147483648'i32 == int32.low
6262
when int.sizeof > 4:
6363
doAssert -9223372036854775808 == int.low
64-
whenJsNoBigInt64: discard
65-
do:
64+
when hasWorkingInt64:
6665
doAssert -9223372036854775808 == int64.low
6766

6867
block: # check when a minus (-) is an unary op

tests/pragmas/thintprocessing.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ discard """
33
matrix: "--hint:processing"
44
nimout: '''
55
compile start
6-
..
6+
...
77
warn_module.nim(6, 6) Hint: 'test' is declared but not used [XDeclaredButNotUsed]
88
compile end
99
'''

0 commit comments

Comments
 (0)