Calling poll on an dynamically loaded module results in
Traceback (most recent call last)
module.nim(15) init
asyncmacro.nim(393) foo
asyncmacro.nim(43) cb0
asyncfutures.nim(210) callback=
asyncfutures.nim(181) clearCallbacks
gc.nim(271) asgnRef
gc.nim(224) decRef
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
file loader.nim
import options, dynlib
import state
type
InitProc = proc(state:State): State {.nimcall.}
PollProc = proc(state:State): State {.nimcall.}
Module* = object
init*: InitProc
poll*: PollProc
lib: LibHandle
proc loadModule*(path:string): Option[Module] =
let lib = loadLib(path)
if lib != nil:
let initAddr = lib.symAddr("init")
let pollAddr = lib.symAddr("poll")
if initAddr != nil and pollAddr != nil:
let init = cast[InitProc](initAddr)
let poll = cast[PollProc](pollAddr)
var module = Module()
module.init = init
module.poll = poll
result = some module
else:
echo "Could not find procs"
return
var module = loadModule("./libmodule.so").get()
var tststate = State()
tststate.foo = "test"
tststate = module.init(tststate) # register async func
while true:
tststate = module.poll(tststate)
file module.nim
# module (as shared object):
# nim c --app:lib module.nim
import asyncdispatch
import state
proc foo(): Future[void] {.async.} =
echo "foo started"
while true:
await sleepAsync(250)
{.push dynlib exportc.}
proc init*(state: State): State =
asyncCheck foo()
return state
proc poll*(state: State): State =
poll()
return state
{.pop.}
file state.nim
type State* = object
foo*: string
nim c --app:lib module.nim
nim c -r loader.nim
foo started
Traceback (most recent call last)
module.nim(15) init
asyncmacro.nim(393) foo
asyncmacro.nim(43) cb0
asyncfutures.nim(210) callback=
asyncfutures.nim(181) clearCallbacks
gc.nim(271) asgnRef
gc.nim(224) decRef
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/home/z/loader'
What have i done wrong?
i created the nimrtl dynamic library and also copied it to the project folder. then when compiled like so:
nim c --app:lib -d:useNimRtl module.nim
this gets thrown:
Hint: used config file '/home/z/Nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: module [Processing]
Hint: asyncdispatch [Processing]
Hint: os [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: algorithm [Processing]
Hint: times [Processing]
Hint: posix [Processing]
Hint: ospaths [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: heapqueue [Processing]
Hint: lists [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
Hint: asyncstreams [Processing]
Hint: asyncfutures [Processing]
Hint: deques [Processing]
Hint: nativesockets [Processing]
Hint: net [Processing]
Hint: sets [Processing]
Hint: selectors [Processing]
Hint: epoll [Processing]
Hint: macros [Processing]
lib/pure/asyncdispatch.nim(1545, 61) template/generic instantiation from here
lib/pure/asyncmacro.nim(301, 55) Error: cannot 'importc' variable at compile time
This happens also to a one line test file
import asyncdispatch
edit: opened issue https://github.com/nim-lang/Nim/issues/6855