Skip to content

Commit 4a747f0

Browse files
authored
CT FFI: fix for windows; fix case transition; error msg shows more useful context (#13292)
* evalffi: fix case transition * evalffi: fix for windows * evallffi: `cannot import` errmsg now also shows which library it tried to import symbol from
1 parent bf22b44 commit 4a747f0

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

compiler/evalffi.nim

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## This file implements the FFI part of the evaluator for Nim code.
1111

1212
import ast, types, options, tables, dynlib, msgs, lineinfos
13+
from os import getAppFilename
1314
import pkg/libffi
1415

1516
when defined(windows):
@@ -27,7 +28,7 @@ var
2728
gDllCache = initTable[string, LibHandle]()
2829

2930
when defined(windows):
30-
var gExeHandle = loadLib(os.getAppFilename())
31+
var gExeHandle = loadLib(getAppFilename())
3132
else:
3233
var gExeHandle = loadLib()
3334

@@ -52,11 +53,13 @@ proc importcSymbol*(conf: ConfigRef, sym: PSym): PNode =
5253
# that contains the address instead:
5354
result = newNodeIT(nkPtrLit, sym.info, sym.typ)
5455
when true:
56+
var libPathMsg = ""
5557
let lib = sym.annex
5658
if lib != nil and lib.path.kind notin {nkStrLit..nkTripleStrLit}:
5759
globalError(conf, sym.info, "dynlib needs to be a string lit")
5860
var theAddr: pointer
5961
if (lib.isNil or lib.kind == libHeader) and not gExeHandle.isNil:
62+
libPathMsg = "current exe: " & getAppFilename() & " nor libc: " & libcDll
6063
# first try this exe itself:
6164
theAddr = gExeHandle.symAddr(name)
6265
# then try libc:
@@ -65,9 +68,11 @@ proc importcSymbol*(conf: ConfigRef, sym: PSym): PNode =
6568
theAddr = dllhandle.symAddr(name)
6669
elif not lib.isNil:
6770
let dll = if lib.kind == libHeader: libcDll else: lib.path.strVal
71+
libPathMsg = dll
6872
let dllhandle = getDll(conf, gDllCache, dll, sym.info)
6973
theAddr = dllhandle.symAddr(name)
70-
if theAddr.isNil: globalError(conf, sym.info, "cannot import: " & name)
74+
if theAddr.isNil: globalError(conf, sym.info,
75+
"cannot import symbol: " & name & " from " & libPathMsg)
7176
result.intVal = cast[ByteAddress](theAddr)
7277

7378
proc mapType(conf: ConfigRef, t: ast.PType): ptr libffi.Type =
@@ -320,7 +325,7 @@ proc unpack(conf: ConfigRef, x: pointer, typ: PType, n: PNode): PNode =
320325
else:
321326
reset n[]
322327
result = n
323-
result.kind = nkNilLit
328+
result[] = TNode(kind: nkNilLit)
324329
result.typ = typ
325330

326331
template awi(kind, v: untyped): untyped = aw(kind, v, intVal)

0 commit comments

Comments
 (0)