[superseded] VM: allow importc var in some cases #12840
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
/cc @Araq
before PR:
importc var variables doesn't work at CT (
Error: cannot evaluate at compile time:
), requiring user to compile a separate library to expose getters/setters instead of directly using importc var, which has its own issues (more complex, can't be inlined, etc). Eg: see example 2 in #12825getStdout
; ditto withgetErrno / setErrno
to get/set errno;after PR:
importc var now works at CT for stdin/stdout/stderr
example 1
print to stderr at CT: (requires building nim with -d:nimHasLibFFI and compiling with --experimental:compiletimeFFI for importc at CT)
example 2
illustrates why
if s.importcCondVar: return
is also needed in the PRnote 1
pre-existing BUG that can be fixed in future PR: as shown in snippet above, var variables at CT work for ptr types (eg cstderr) but for integral types (eg errno), they currently return the address instead of the value; help welcome to fix that; probably a bug inside
compiler/evalffi.nim
note 2
(related to #12824 but different)
in future PR, it would be nice also to allow importc
let
andconst
at CT:these can be worked around using an intermediate library exactly as shown in example2 in #12825:
but ideally would work directly, making interop simpler