From 036c52f0b626277970f7173327616e7536d871d1 Mon Sep 17 00:00:00 2001 From: cooldome Date: Tue, 9 Oct 2018 14:19:21 +0100 Subject: [PATCH] fixes #9222 (#9224) --- compiler/cgen.nim | 7 ++++++- tests/cpp/amodule.nim | 10 ++++++++++ tests/cpp/t8241.nim | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/cpp/amodule.nim diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 3040f98da..517e53947 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -567,7 +567,12 @@ proc loadDynamicLib(m: BModule, lib: PLib) = var p = newProc(nil, m) p.options = p.options - {optStackTrace, optEndb} var dest: TLoc - initLocExpr(p, lib.path, dest) + initLoc(dest, locTemp, lib.path, OnStack) + dest.r = getTempName(m) + appcg(m, m.s[cfsDynLibInit],"$1 $2;$n", + [getTypeDesc(m, lib.path.typ), rdLoc(dest)]) + expr(p, lib.path, dest) + add(m.s[cfsVars], p.s(cpsLocals)) add(m.s[cfsDynLibInit], p.s(cpsInit)) add(m.s[cfsDynLibInit], p.s(cpsStmts)) diff --git a/tests/cpp/amodule.nim b/tests/cpp/amodule.nim new file mode 100644 index 000000000..2f3e34051 --- /dev/null +++ b/tests/cpp/amodule.nim @@ -0,0 +1,10 @@ +import os + +proc findlib: string = + let path = getEnv("MYLIB_DOES_NOT_EXIST_PATH") + if path.len > 0 and dirExists(path): + path / "alib_does_not_matter.dll" + else: + "alib_does_not_matter.dll" + +proc imported_func*(a: cint): cstring {.importc, dynlib: findlib().} \ No newline at end of file diff --git a/tests/cpp/t8241.nim b/tests/cpp/t8241.nim index 8e98fda10..cbee1d85a 100644 --- a/tests/cpp/t8241.nim +++ b/tests/cpp/t8241.nim @@ -5,3 +5,19 @@ discard """ proc foo(): cstring {.importcpp: "", dynlib: "".} echo foo() + + +## bug #9222 +import os +import amodule +proc findlib2: string = + let path = getEnv("MYLIB2_DOES_NOT_EXIST_PATH") + if path.len > 0 and dirExists(path): + path / "alib_does_not_matter.dll" + else: + "alib_does_not_matter.dll" + +proc imported_func2*(a: cint): cstring {.importc, dynlib: findlib2().} + +echo imported_func(1) +echo imported_func2(1) \ No newline at end of file