Skip to content

Commit 6938fce

Browse files
authored
stdlib: system: fix incorrect VM detection in substr impls (#25182)
...introduced by me in #24792. Sorry. This fix doesn't avoid copying the `restrictedBody` twice in the generated code but has the benefit of working. Proper fix needs a detection that can set a const bool for a module once. `when nimvm` is restricted in use and is difficult to dance around. Some details in: #12517, #12518, #13038 I might have copied the buggy solution from some discussion and it might have worked at some point, but it's small excuse.
1 parent e958f4a commit 6938fce

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/system.nim

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,11 +2847,15 @@ template once*(body: untyped): untyped =
28472847

28482848
{.pop.} # warning[GcMem]: off, warning[Uninit]: off
28492849

2850-
template NotJSnotVMnotNims(): static bool = # hack, see: #12517 #12518
2850+
template whenNotVmJsNims(normalBody, restrictedBody: untyped) =
2851+
## hack, see: #12517 #12518
28512852
when nimvm:
2852-
false
2853+
restrictedBody
28532854
else:
2854-
notJSnotNims
2855+
when notJSnotNims:
2856+
normalBody
2857+
else:
2858+
restrictedBody
28552859

28562860
proc substr*(a: openArray[char]): string =
28572861
## Returns a new string, copying contents of `a`.
@@ -2873,10 +2877,10 @@ proc substr*(a: openArray[char]): string =
28732877
assert a.toOpenArray(2, high(a)).substr() == "cdefgh" # From index 2 to `high(a)`
28742878
doAssertRaises(IndexDefect): discard a.toOpenArray(5, 99).substr()
28752879
result = newStringUninit(a.len)
2876-
when NotJSnotVMnotNims:
2880+
whenNotVmJsNims():
28772881
if a.len > 0:
28782882
copyMem(result[0].addr, a[0].unsafeAddr, a.len)
2879-
else:
2883+
do:
28802884
for i, ch in a:
28812885
result[i] = ch
28822886

@@ -2908,10 +2912,10 @@ proc substr*(s: string; first, last: int): string = # A bug with `magic: Slice`
29082912
last = min(last, high(s))
29092913
L = max(last - first + 1, 0)
29102914
result = newStringUninit(L)
2911-
when NotJSnotVMnotNims:
2915+
whenNotVmJsNims():
29122916
if L > 0:
29132917
copyMem(result[0].addr, s[first].unsafeAddr, L)
2914-
else:
2918+
do:
29152919
for i in 0..<L:
29162920
result[i] = s[i + first]
29172921

0 commit comments

Comments
 (0)