You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When executing Nim code via nimscript (nim e file), for some reason the code in the else branch of when nimvm attempts to resolve its symbols, which, in the case of system.copyMem do example below, is not declared for nimscript. This results in an error.
const num =0x807060504030201'u64const num2 =0x102030405060708'u64proctoByte(x: uint64): array[8, byte] =when nimVm:
for i in0..7:
let p = (x shr (i *8)) and0xff'u64result[i] =byte(p)
else:
copyMem(addrresult, unsafeAddr(x), 8)
const ctByteNum =toByte(num)
let rtByteNum2 =toByte(num2)
echo ctByteNum
echo rtByteNum2
Nim Version
Nim Compiler Version 1.9.3 [Windows: amd64]
Compiled at 2023-04-17
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 9dc1f2d
active boot switches: -d:release
Current Output
nim e bug.nim
Hint: used config file 'E:\Nim\config\nim.cfg' [Conf]
Hint: used config file 'E:\Nim\config\config.nims' [Conf]
C:\Users\josep\desktop\bug.nim(10, 5) Error: undeclared identifier: 'copyMem'
candidates (edit distance, scope distance); see '--spellSuggest':
(4, 2): 'toByte'
(4, 4): '=copy'
(4, 4): 'compiles'
(4, 4): 'ctypes'
(4, 4): 'open'
Expected Output
[1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1]
Possible Solution
A stopgap solution is to prepend this:
proctoByte(x: uint64): array[8, byte] =when nimVm:
for i in0..7:
let p = (x shr (i *8)) and0xff'u64result[i] =byte(p)
else:
whennotdefined(nimscript):
copyMem(addrresult, unsafeAddr(x), 8)
This solution seems to work for all versions from 1.0.0
Additional Information
The code works normally for c and cpp backend, demonstrating that during compile time toByte is executed correctly and the when nimvm branch as well.
The text was updated successfully, but these errors were encountered:
when nimvm still "compiles" both branches. It just decides which branch to use when generating code for the VM or for the backend. Your workaround is used in the standard library, see string cmp, similar workaround here in lexbase, here in streams etc.
Maybe we could change when nimvm to always be true on NimScript and never compile the bottom branch, but we would have to see if it works well
Description
When executing Nim code via nimscript (
nim e file
), for some reason the code in theelse
branch ofwhen nimvm
attempts to resolve its symbols, which, in the case ofsystem.copyMem
do example below, is not declared for nimscript. This results in an error.Nim Version
Nim Compiler Version 1.9.3 [Windows: amd64]
Compiled at 2023-04-17
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 9dc1f2d
active boot switches: -d:release
Current Output
Expected Output
Possible Solution
A stopgap solution is to prepend this:
This solution seems to work for all versions from 1.0.0
Additional Information
The code works normally for c and cpp backend, demonstrating that during compile time
toByte
is executed correctly and thewhen nimvm
branch as well.The text was updated successfully, but these errors were encountered: