Make loadConfig available from NimScript (#24840)

fixes #24837

I really wanted to name the variable just `stream` and leave `defer:
...` and `result =...` out, but the compiler says the variable is
redefined, so this is the form.

(cherry picked from commit 2ed45eb848)
This commit is contained in:
la.panon.
2025-04-03 22:54:39 +09:00
committed by narimiran
parent 2de409cd0c
commit 975e8576ec

View File

@@ -540,10 +540,17 @@ proc loadConfig*(stream: Stream, filename: string = "[stream]"): Config =
proc loadConfig*(filename: string): Config =
## Loads the specified configuration file into a new Config instance.
let file = open(filename, fmRead)
let fileStream = newFileStream(file)
defer: fileStream.close()
result = fileStream.loadConfig(filename)
when nimvm:
# HACK: As a workaround,
# since open() using {.importc.} is not available on NimScript.
let stringStream = newStringStream(readFile(filename))
defer: stringStream.close()
result = stringStream.loadConfig(filename)
else:
let file = open(filename, fmRead)
let fileStream = newFileStream(file)
defer: fileStream.close()
result = fileStream.loadConfig(filename)
proc replace(s: string): string =
var d = ""