Well, I was very hopeful, but alas...
After a few hours at this (again) still no progress. It is a nim dll for a nim
exe, so no c declarations that can/should go wrong.
Compiles: nim c -d:useNimRtl --app:lib --noMain -mm:orc ReModule
proc NimMain() {.importc.}
proc initModule(md: ModuleData): bool {.exportc, dynlib.} =
NimMain() # Be sure to call this! It will set up garbage
collector and initialize any global memory
# first base data (paths to files/folders, language, etc..)
basedata = md
proc endModule() {.exportc, dynlib.} =
GC_FullCollect()
proc speechModule*(sd: SimData): SimData {.exportc, dynlib.} =
simdata = sd # copy to a global for this module
initTalk(simdata)
... more code
return
Run
The calling program:
type
SpeechModuleProc = proc(sd: ReData.SimData): ReData.SimData {.nimcall}
InitModuleProc = proc(mdl: ModuleData): bool {.nimcall}
EndModuleProc = proc() {.nimcall}
var
libModule: LibHandle
initModuleAddr: pointer
initModule: InitModuleProc
speechModuleAddr: pointer
speechModule: SpeechModuleProc
endModuleAddr: pointer
endModule: EndModuleProc
moduleFile: string
moduleLoaded: string = ""
moduleAvailable: bool = false
proc loadModule(moduleName: string): bool =
result = false
# create path to module (working dir if development version)
moduleFile = "ReModule" & moduleName & ".dll"
# try loading the module
try:
libModule = loadLib(moduleFile)
result = true
except:
logI(fmt("ERROR: could not load module {moduleName}."))
result = false
# get address(es) of the proc(s) in the dynamic lib
var moduleErrors: string = ""
if libModule != nil:
initModuleAddr = libModule.symAddr("initModule")
speechModuleAddr = libModule.symAddr("speechModule")
endModuleAddr = libModule.symAddr("endModule")
if initModuleAddr != nil:
initModule = cast[InitModuleProc] (initModuleAddr)
else:
moduleErrors = moduleErrors & ("- initModule not found\n")
if speechModuleAddr != nil:
speechModule = cast[SpeechModuleProc] (speechModuleAddr)
else:
moduleErrors = moduleErrors & ("- speechModule not found\n")
if endModuleAddr != nil:
endModule = cast[EndModuleProc] (endModuleAddr)
else:
moduleErrors = moduleErrors & ("- endModule not found\n")
else:
moduleErrors = moduleErrors & ("- something wrong with module")
if moduleErrors > "":
logP(fmt("ERRORS: loading {moduleName} gave problem(s):\n") &
moduleErrors)
result = false
if result:
moduleLoaded = moduleName
# get data needed for initialization of module
var mdl: ModuleData
mdl.lang = "en"
mdl.appname = APPNAME
mdl.appversion = VERSION
discard initModule(mdl) # initialize !
else:
moduleLoaded = ""
proc unloadModule(): bool =
if moduleLoaded != "":
endModule()
unloadLib(libModule)
proc projectModule(simd: ReData.SimData): ReData.SimData =
var
simdata = simd #: ReData.SimData
# not same lib module: unload old and load correct one
if moduleLoaded != simdata.mission.projectName:
discard unloadModule()
if loadModule(simdata.mission.projectName):
logP(fmt("Module {moduleLoaded} loaded successfully..."))
else:
logI(fmt("WARNING: Could not load the project
{simdata.mission.projectName} module: {moduleFile}."))
if libModule != nil:
try:
simdata = speechModule(simdata)
except:
logP("ERROR: Call to project module failed!")
else:
logP(fmt("ERROR: Module not loaded or defined! ({moduleFile})"))
return simdata
Run
But the app keeps crashing (even earlier than before, when I got into the
loadSpeechData proc called from the initModule
Now it crashes earlier. I'm using Fidget (100% pure nim) as the UI package.
When compiled with : **nim c -mm:orc --threads:on -d:useNimRtl ReVoice** (with
useNimRtl) The crash occurs even before the UI appears, at the loading of the
needed fonts (6 in total, crash on 3rd) Loading fonts Loading font IBM Plex
Sans (IBMPlexSans-Regular.ttf) Loading font IBM Plex Sans Bold
(IBMPlexSans-Bold.ttf) Loading font Typewriter (TT2020StyleE-Regular.ttf)
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Compiled with : **nim c -mm:orc --threads:on ReVoice** The the crash occurs
much later, only on the first call to projectModule in the dll Loading module
file ModuleTest.dll Loaded module Test.
Calling initModule with:
lang: "en", appname: "ReVoice", appversion: "0.639999a"
Traceback (most recent call last) C:UsersIvanGFRReVoice.nim(1385) ReVoice
C:UsersIvanGFRReVoice.nim(1213) main
C:UsersIvan.nimblepkgsfidget-0.7.8fidgetopenglbackend.nim(442) startFidget
C:UsersIvan.nimblepkgsfidget-0.7.8fidgetopenglbase.nim(216) updateLoop
C:UsersIvan.nimblepkgsfidget-0.7.8fidgetopenglbase.nim(167) drawAndSwap
C:UsersIvan.nimblepkgsfidget-0.7.8fidgetopenglbackend.nim(360) :anonymous
C:UsersIvanGoForReEntryReVoice.nim(1019) drawMain
C:UsersIvanGoForReEntryReVoice.nim(281) projectModule
C:UsersIvanGoForReEntryReVoice.nim(257) loadModule SIGSEGV: Illegal storage
access. (Attempt to read from nil?)