BUGFIX: better error message when loading of dynamic lib proc fails

This commit is contained in:
Andreas Rumpf 2009-12-09 01:05:48 +01:00
commit 27d7e3a0ae
4 changed files with 45 additions and 14 deletions

View file

@ -60,6 +60,7 @@ when defined(posix):
proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr = proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
result = dlsym(lib, name) result = dlsym(lib, name)
if result == nil: nimLoadLibraryError($name)
elif defined(windows) or defined(dos): elif defined(windows) or defined(dos):
# #
@ -84,6 +85,7 @@ elif defined(windows) or defined(dos):
proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr = proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
result = GetProcAddress(cast[THINSTANCE](lib), name) result = GetProcAddress(cast[THINSTANCE](lib), name)
if result == nil: nimLoadLibraryError($name)
elif defined(mac): elif defined(mac):
# #
@ -114,11 +116,12 @@ elif defined(mac):
NSDestroyObjectFileImage(img) NSDestroyObjectFileImage(img)
result = TLibHandle(modul) result = TLibHandle(modul)
proc nimGetProcAddr(lib: TLibHandle, cname: string): TProcAddr = proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr =
var var
nss: NSSymbol nss: NSSymbol
nss = NSLookupSymbolInModule(NSModule(lib), name) nss = NSLookupSymbolInModule(NSModule(lib), name)
result = TProcAddr(NSAddressOfSymbol(nss)) result = TProcAddr(NSAddressOfSymbol(nss))
if result == nil: nimLoadLibraryError($name)
else: else:
{.error: "no implementation for dyncalls".} {.error: "no implementation for dyncalls".}

View file

@ -319,11 +319,8 @@ proc genConstPrototype(m: BModule, sym: PSym)
proc genProc(m: BModule, prc: PSym) proc genProc(m: BModule, prc: PSym)
proc genStmts(p: BProc, t: PNode) proc genStmts(p: BProc, t: PNode)
proc genProcPrototype(m: BModule, sym: PSym) proc genProcPrototype(m: BModule, sym: PSym)
include
"ccgexprs.nim"
include include "ccgexprs.nim", "ccgstmts.nim"
"ccgstmts.nim"
# ----------------------------- dynamic library handling ----------------- # ----------------------------- dynamic library handling -----------------
# We don't finalize dynamic libs as this does the OS for us. # 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)]) getTypeDesc(m, getSysType(tyString)), toRope(m.labels)])
appff(m.s[cfsDynLibInit], appff(m.s[cfsDynLibInit],
"if (!($1)) nimLoadLibraryError((NimStringDesc*) &$2);$n", "if (!($1)) nimLoadLibraryError((NimStringDesc*) &$2);$n",
"XXX too implement", [loadlib, getStrLit(m, lib.path)]) #appf(m.s[cfsDynLibDeinit], "XXX too implement", [loadlib, getStrLit(m, lib.path)])
# 'if ($1 != NIM_NIL) nimUnloadLibrary($1);$n', [tmp]); #appf(m.s[cfsDynLibDeinit], "if ($1 != NIM_NIL) nimUnloadLibrary($1);$n",
# [tmp])
useMagic(m, "nimLoadLibrary") useMagic(m, "nimLoadLibrary")
useMagic(m, "nimUnloadLibrary") useMagic(m, "nimUnloadLibrary")
useMagic(m, "NimStringDesc") useMagic(m, "NimStringDesc")
@ -388,11 +386,12 @@ proc SymInDynamicLib(m: BModule, sym: PSym) =
sym.loc.r = tmp # from now on we only need the internal name sym.loc.r = tmp # from now on we only need the internal name
sym.typ.sym = nil # generate a new name sym.typ.sym = nil # generate a new name
inc(m.labels, 2) 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" & "%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( "store $2 %MOC$6, $2* $1$n", [tmp, getTypeDesc(m, sym.typ),
m, m.s[cfsDynLibInit], ropeToStr(extname)), toRope(m.labels), lib.name, cstringLit(m, m.s[cfsDynLibInit], ropeToStr(extname)),
toRope(m.labels - 1)]) toRope(m.labels), toRope(m.labels - 1)])
appff(m.s[cfsVars], "$2 $1;$n", "$1 = linkonce global $2 zeroinitializer$n", 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)])
@ -409,9 +408,8 @@ proc UseMagic(m: BModule, name: string) =
rawMessage(errSystemNeeds, name) # don't be too picky here rawMessage(errSystemNeeds, name) # don't be too picky here
proc generateHeaders(m: BModule) = proc generateHeaders(m: BModule) =
var it: PStrEntry
app(m.s[cfsHeaders], "#include \"nimbase.h\"" & tnl & tnl) app(m.s[cfsHeaders], "#include \"nimbase.h\"" & tnl & tnl)
it = PStrEntry(m.headerFiles.head) var it = PStrEntry(m.headerFiles.head)
while it != nil: while it != nil:
if not (it.data[0] in {'\"', '<'}): if not (it.data[0] in {'\"', '<'}):
appf(m.s[cfsHeaders], "#include \"$1\"$n", [toRope(it.data)]) appf(m.s[cfsHeaders], "#include \"$1\"$n", [toRope(it.data)])

14
tests/tsplit.nim Executable file
View 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
View 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