mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-05 04:27:44 +00:00
BUGFIX: better error message when loading of dynamic lib proc fails
This commit is contained in:
@@ -60,6 +60,7 @@ when defined(posix):
|
||||
|
||||
proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
|
||||
result = dlsym(lib, name)
|
||||
if result == nil: nimLoadLibraryError($name)
|
||||
|
||||
elif defined(windows) or defined(dos):
|
||||
#
|
||||
@@ -84,6 +85,7 @@ elif defined(windows) or defined(dos):
|
||||
|
||||
proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
|
||||
result = GetProcAddress(cast[THINSTANCE](lib), name)
|
||||
if result == nil: nimLoadLibraryError($name)
|
||||
|
||||
elif defined(mac):
|
||||
#
|
||||
@@ -114,11 +116,12 @@ elif defined(mac):
|
||||
NSDestroyObjectFileImage(img)
|
||||
result = TLibHandle(modul)
|
||||
|
||||
proc nimGetProcAddr(lib: TLibHandle, cname: string): TProcAddr =
|
||||
proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
|
||||
var
|
||||
nss: NSSymbol
|
||||
nss = NSLookupSymbolInModule(NSModule(lib), name)
|
||||
result = TProcAddr(NSAddressOfSymbol(nss))
|
||||
if result == nil: nimLoadLibraryError($name)
|
||||
|
||||
else:
|
||||
{.error: "no implementation for dyncalls".}
|
||||
|
||||
24
rod/cgen.nim
24
rod/cgen.nim
@@ -319,11 +319,8 @@ proc genConstPrototype(m: BModule, sym: PSym)
|
||||
proc genProc(m: BModule, prc: PSym)
|
||||
proc genStmts(p: BProc, t: PNode)
|
||||
proc genProcPrototype(m: BModule, sym: PSym)
|
||||
include
|
||||
"ccgexprs.nim"
|
||||
|
||||
include
|
||||
"ccgstmts.nim"
|
||||
include "ccgexprs.nim", "ccgstmts.nim"
|
||||
|
||||
# ----------------------------- dynamic library handling -----------------
|
||||
# We don't finalize dynamic libs as this does the OS for us.
|
||||
@@ -367,8 +364,9 @@ proc loadDynamicLib(m: BModule, lib: PLib) =
|
||||
getTypeDesc(m, getSysType(tyString)), toRope(m.labels)])
|
||||
appff(m.s[cfsDynLibInit],
|
||||
"if (!($1)) nimLoadLibraryError((NimStringDesc*) &$2);$n",
|
||||
"XXX too implement", [loadlib, getStrLit(m, lib.path)]) #appf(m.s[cfsDynLibDeinit],
|
||||
# 'if ($1 != NIM_NIL) nimUnloadLibrary($1);$n', [tmp]);
|
||||
"XXX too implement", [loadlib, getStrLit(m, lib.path)])
|
||||
#appf(m.s[cfsDynLibDeinit], "if ($1 != NIM_NIL) nimUnloadLibrary($1);$n",
|
||||
# [tmp])
|
||||
useMagic(m, "nimLoadLibrary")
|
||||
useMagic(m, "nimUnloadLibrary")
|
||||
useMagic(m, "NimStringDesc")
|
||||
@@ -388,13 +386,14 @@ proc SymInDynamicLib(m: BModule, sym: PSym) =
|
||||
sym.loc.r = tmp # from now on we only need the internal name
|
||||
sym.typ.sym = nil # generate a new name
|
||||
inc(m.labels, 2)
|
||||
appff(m.s[cfsDynLibInit], "$1 = ($2) nimGetProcAddr($3, $4);$n", "%MOC$5 = load i8* $3$n" &
|
||||
appff(m.s[cfsDynLibInit],
|
||||
"$1 = ($2) nimGetProcAddr($3, $4);$n", "%MOC$5 = load i8* $3$n" &
|
||||
"%MOC$6 = call $2 @nimGetProcAddr(i8* %MOC$5, i8* $4)$n" &
|
||||
"store $2 %MOC$6, $2* $1$n", [tmp, getTypeDesc(m, sym.typ), lib.name, cstringLit(
|
||||
m, m.s[cfsDynLibInit], ropeToStr(extname)), toRope(m.labels),
|
||||
toRope(m.labels - 1)])
|
||||
"store $2 %MOC$6, $2* $1$n", [tmp, getTypeDesc(m, sym.typ),
|
||||
lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname)),
|
||||
toRope(m.labels), toRope(m.labels - 1)])
|
||||
appff(m.s[cfsVars], "$2 $1;$n", "$1 = linkonce global $2 zeroinitializer$n",
|
||||
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
||||
[sym.loc.r, getTypeDesc(m, sym.loc.t)])
|
||||
|
||||
proc UseMagic(m: BModule, name: string) =
|
||||
var sym: PSym
|
||||
@@ -409,9 +408,8 @@ proc UseMagic(m: BModule, name: string) =
|
||||
rawMessage(errSystemNeeds, name) # don't be too picky here
|
||||
|
||||
proc generateHeaders(m: BModule) =
|
||||
var it: PStrEntry
|
||||
app(m.s[cfsHeaders], "#include \"nimbase.h\"" & tnl & tnl)
|
||||
it = PStrEntry(m.headerFiles.head)
|
||||
var it = PStrEntry(m.headerFiles.head)
|
||||
while it != nil:
|
||||
if not (it.data[0] in {'\"', '<'}):
|
||||
appf(m.s[cfsHeaders], "#include \"$1\"$n", [toRope(it.data)])
|
||||
|
||||
14
tests/tsplit.nim
Executable file
14
tests/tsplit.nim
Executable file
@@ -0,0 +1,14 @@
|
||||
import strutils
|
||||
|
||||
var s = ""
|
||||
for w in split("|abc|xy|z", {'|'}):
|
||||
s.add("#")
|
||||
s.add(w)
|
||||
|
||||
if s == "#abc#xy#z":
|
||||
echo "true"
|
||||
else:
|
||||
echo "false"
|
||||
|
||||
#OUT true
|
||||
|
||||
16
tests/ttuple1.nim
Executable file
16
tests/ttuple1.nim
Executable file
@@ -0,0 +1,16 @@
|
||||
const romanNumbers = [
|
||||
("M", 1000), ("D", 500), ("C", 100),
|
||||
("L", 50), ("X", 10), ("V", 5), ("I", 1) ]
|
||||
|
||||
var c = 0
|
||||
for key, val in items(romanNumbers):
|
||||
inc(c)
|
||||
stdout.write(key & "=" & $val)
|
||||
if c < romanNumbers.len: stdout.write(", ") else: echo""
|
||||
#echo""
|
||||
|
||||
proc PrintBiTuple(t: tuple[k: string, v: int]): int =
|
||||
stdout.write(t.k & "=" & $t.v & ", ")
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user