Skip to content

Commit

Permalink
fixed crash when nim std path can't be found
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimaoth committed Jan 7, 2024
1 parent 983832f commit ad7c1ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
9 changes: 6 additions & 3 deletions src/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ proc setTheme*(self: App, path: string) =
self.platform.requestRender()

when not defined(js):
var createScriptContext: proc(filepath: string, searchPaths: seq[string]): Future[ScriptContext] = nil
var createScriptContext: proc(filepath: string, searchPaths: seq[string]): Future[Option[ScriptContext]] = nil

proc getCommandLineTextEditor*(self: App): TextDocumentEditor = self.commandLineTextEditor.TextDocumentEditor

Expand Down Expand Up @@ -625,7 +625,10 @@ proc initScripting(self: App) {.async.} =
path = fs.getApplicationFilePath(rest)

when not defined(js):
self.scriptContext = await createScriptContext("./config/absytree_config.nim", searchPaths)
if createScriptContext("./config/absytree_config.nim", searchPaths).await.getSome(scriptContext):
self.scriptContext = scriptContext
else:
log lvlError, "Failed to create nim script context"

withScriptContext self, self.scriptContext:
log(lvlInfo, fmt"init nim script config")
Expand Down Expand Up @@ -2022,7 +2025,7 @@ template createNimScriptContextConstructorAndGenerateBindings*(): untyped =

createScriptContextConstructor(addins)

proc createScriptContextImpl(filepath: string, searchPaths: seq[string]): Future[ScriptContext] = createScriptContextNim(filepath, searchPaths)
proc createScriptContextImpl(filepath: string, searchPaths: seq[string]): Future[Option[ScriptContext]] = createScriptContextNim(filepath, searchPaths)
createScriptContext = createScriptContextImpl

createEditorWasmImportConstructor()
46 changes: 27 additions & 19 deletions src/scripting/scripting_nim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,45 @@ proc mySafeLoadScriptWithState*(self: ScriptContextNim, modules: seq[string]) {.
if state.getSome(state):
inter.loadState(state)

proc myFindNimStdLib(): string =
proc myFindNimStdLib(): Option[string] =
## Tries to find a path to a valid "system.nim" file.
## Returns "" on failure.

let customNimStdLib = getAppDir() / "scripting" / "nim" / "lib"
if dirExists(customNimStdLib):
log lvlInfo, fmt"Using custom nim std lib '{customNimStdLib}'"
return customNimStdLib
return customNimStdLib.some

try:
log lvlInfo, "Searching for nim std lib directory using 'nim --verbosity:0 dump --dump.format:json .'"
let nimdump = execProcess("nim", ".", ["--verbosity:0", "dump", "--dump.format:json", "."], options={poUsePath, poDaemon})
let nimdumpJson = nimdump.parseJson()
return nimdumpJson["libpath"].getStr ""
let nimdumpJson = try:
nimdump.parseJson()
except:
log lvlError, &"Failed to parse output of nim dump: {getCurrentExceptionMsg()}\n{nimdump}"
return string.none

return nimdumpJson["libpath"].getStr("").some
except OSError, ValueError:
log lvlError, fmt"Failed to find nim std path using nim dump: {getCurrentExceptionMsg()}"
return ""

proc newScriptContext*(path: string, apiModule: string, addins: VMAddins, postCodeAdditions: string, searchPaths: seq[string]): Future[ScriptContextNim] {.async.} =
new result
result.script = NimScriptPath(path)
result.apiModule = apiModule
result.addins = addins
result.postCodeAdditions = postCodeAdditions
result.searchPaths = searchPaths

result.stdPath = myFindNimStdLib()
if result.stdPath == "":
log lvlError, &"Failed to find nim std path using nim dump: {getCurrentExceptionMsg()}"
return string.none

proc newScriptContext*(path: string, apiModule: string, addins: VMAddins, postCodeAdditions: string, searchPaths: seq[string]): Future[Option[ScriptContext]] {.async.} =
let stdPath = myFindNimStdLib().getOr:
log lvlError, "Failed to find nim std path"
return ScriptContext.none

let res = ScriptContextNim()
res.script = NimScriptPath(path)
res.apiModule = apiModule
res.addins = addins
res.postCodeAdditions = postCodeAdditions
res.searchPaths = searchPaths
res.stdPath = stdPath

log lvlInfo, fmt"Creating new script context (search paths: {searchPaths}, std path: {res.stdPath})"

log lvlInfo, fmt"Creating new script context (search paths: {searchPaths}, std path: {result.stdPath})"
return res.ScriptContext.some

method init*(self: ScriptContextNim, path: string): Future[void] {.async.} =
self.state = Initializing
Expand Down Expand Up @@ -294,7 +302,7 @@ proc generateScriptingApi*(addins: VMAddins) {.compileTime.} =
generateScriptingApiPerModule()

template createScriptContextConstructor*(addins: untyped): untyped =
proc createScriptContextNim(filepath: string, searchPaths: seq[string]): Future[ScriptContext] {.async.} =
proc createScriptContextNim(filepath: string, searchPaths: seq[string]): Future[Option[ScriptContext]] {.async.} =
return await newScriptContext(filepath, "absytree_internal", addins, "include absytree_runtime_impl", searchPaths)

macro invoke*(self: ScriptContext; pName: untyped;
Expand Down

0 comments on commit ad7c1ab

Please sign in to comment.