Skip to content

Commit 86a3001

Browse files
committed
fix(NIM-BUG): not compile when JS: random.randbytes
1 parent 9d5ec6f commit 86a3001

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/pylib/Lib/random_impl/proc_others.nim

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,27 @@ const PyLittleEndian = cpuEndian == littleEndian
6060
6161
const bytePerWord = 4
6262
type U8 = uint8 | char
63-
func fromU32sImpl(res: var seq[U8]|string; wordarray: openArray[uint32]){.inline.} =
63+
func fromU32sImpl[S: seq[U8]|string](res: var S; wordarray: openArray[uint32]){.inline.} =
6464
when declared(copyMem):
6565
copyMem(res[0].addr, wordarray[0].addr, res.len)
6666
else:
67-
template unpack[T: U8|char](u: uint32, a, b, c, d: var T) =
68-
d = cast[T](u)
69-
c = cast[T](u shr 8)
70-
b = cast[T](u shr 16)
71-
a = cast[T](u shr 24)
7267
when PyLittleEndian:
73-
var i = 0
74-
for u32 in wordarray:
75-
unpack u32, res[i], res[i+1], res[i+2], res[i+3]
76-
i.inc per
68+
template rng: untyped = 0..wordarray.high
69+
template `[]=`(res; i, o, v): untyped = res[i + (3 - o)] = v
7770
else:
78-
for i in countdown(wordarray.high, 0):
79-
let ii = i * per
80-
unpack wordarray[i], res[ii+3], res[ii+2], res[ii+1], res[ii]
71+
template rng: untyped = countdown(wordarray.high, 0)
72+
template `[]=`(res; i, o, v): untyped = res[i + o] = v
73+
for i in rng:
74+
let ii = i * bytePerWord
75+
#unpack wordarray[i], res[ii+3], res[ii+2], res[ii+1], res[ii]
76+
let u = wordarray[i]
77+
type T = typeof(res[0])
78+
template asgnPart(o) =
79+
res[ii, o] = cast[T](u shr (8 * o))
80+
asgnPart 0
81+
asgnPart 1
82+
asgnPart 2
83+
asgnPart 3
8184
8285
func fromU32s(res: var seq[U8]; wordarray: openArray[uint32]) =
8386
res = newSeqMayUninit[U8] wordarray.len * bytePerWord

0 commit comments

Comments
 (0)