* add findNimStdLibCompileTime and un-disable tcompilerapi test; add test case for #9180 * address comments
This commit is contained in:
parent
8fc7cecfa2
commit
a58c982a49
2 changed files with 29 additions and 22 deletions
|
|
@ -89,6 +89,13 @@ proc findNimStdLib*(): string =
|
||||||
except OSError, ValueError:
|
except OSError, ValueError:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
proc findNimStdLibCompileTime*(): string =
|
||||||
|
## Same as ``findNimStdLib`` but uses source files used at compile time,
|
||||||
|
## and asserts on error.
|
||||||
|
const sourcePath = currentSourcePath()
|
||||||
|
result = sourcePath.parentDir.parentDir / "lib"
|
||||||
|
doAssert fileExists(result / "system.nim"), "result:" & result
|
||||||
|
|
||||||
proc createInterpreter*(scriptName: string;
|
proc createInterpreter*(scriptName: string;
|
||||||
searchPaths: openArray[string];
|
searchPaths: openArray[string];
|
||||||
flags: TSandboxFlags = {}): Interpreter =
|
flags: TSandboxFlags = {}): Interpreter =
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,19 @@ discard """
|
||||||
output: '''top level statements are executed!
|
output: '''top level statements are executed!
|
||||||
2.0
|
2.0
|
||||||
my secret
|
my secret
|
||||||
|
11
|
||||||
|
12
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
## Example program that demonstrates how to use the
|
## Example program that demonstrates how to use the
|
||||||
## compiler as an API to embed into your own projects.
|
## compiler as an API to embed into your own projects.
|
||||||
|
|
||||||
import "../../compiler" / [ast, vmdef, vm, nimeval]
|
import "../../compiler" / [ast, vmdef, vm, nimeval, llstream]
|
||||||
import std / [os]
|
import std / [os]
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
let std = findNimStdLib()
|
let std = findNimStdLibCompileTime()
|
||||||
if std.len == 0:
|
|
||||||
quit "cannot find Nim's standard library"
|
|
||||||
|
|
||||||
var intr = createInterpreter("myscript.nim", [std, getAppDir()])
|
var intr = createInterpreter("myscript.nim", [std, getAppDir()])
|
||||||
intr.implementRoutine("*", "exposed", "addFloats", proc (a: VmArgs) =
|
intr.implementRoutine("*", "exposed", "addFloats", proc (a: VmArgs) =
|
||||||
setResult(a, getFloat(a, 0) + getFloat(a, 1) + getFloat(a, 2))
|
setResult(a, getFloat(a, 0) + getFloat(a, 1) + getFloat(a, 2))
|
||||||
|
|
@ -28,27 +27,28 @@ proc main() =
|
||||||
quit "script does not export a proc of the name: 'hostProgramRunsThis'"
|
quit "script does not export a proc of the name: 'hostProgramRunsThis'"
|
||||||
let res = intr.callRoutine(foreignProc, [newFloatNode(nkFloatLit, 0.9),
|
let res = intr.callRoutine(foreignProc, [newFloatNode(nkFloatLit, 0.9),
|
||||||
newFloatNode(nkFloatLit, 0.1)])
|
newFloatNode(nkFloatLit, 0.1)])
|
||||||
if res.kind == nkFloatLit:
|
doAssert res.kind == nkFloatLit
|
||||||
echo res.floatVal
|
echo res.floatVal
|
||||||
else:
|
|
||||||
echo "bug!"
|
|
||||||
|
|
||||||
let foreignValue = selectUniqueSymbol(intr, "hostProgramWantsThis")
|
let foreignValue = selectUniqueSymbol(intr, "hostProgramWantsThis")
|
||||||
if foreignValue == nil:
|
if foreignValue == nil:
|
||||||
quit "script does not export a global of the name: hostProgramWantsThis"
|
quit "script does not export a global of the name: hostProgramWantsThis"
|
||||||
let val = intr.getGlobalValue(foreignValue)
|
let val = intr.getGlobalValue(foreignValue)
|
||||||
if val.kind in {nkStrLit..nkTripleStrLit}:
|
doAssert val.kind in {nkStrLit..nkTripleStrLit}
|
||||||
echo val.strVal
|
echo val.strVal
|
||||||
else:
|
|
||||||
echo "bug!"
|
|
||||||
|
|
||||||
destroyInterpreter(intr)
|
destroyInterpreter(intr)
|
||||||
|
|
||||||
if existsEnv("NIM_EXE_NOT_IN_PATH"):
|
main()
|
||||||
# effectively disable this test as 'nim' is not in the PATH so tcompilerapi
|
|
||||||
# cannot find Nim's standard library:
|
block issue9180:
|
||||||
echo "top level statements are executed!"
|
proc evalString(code: string, moduleName = "script.nim") =
|
||||||
echo "2.0"
|
let stream = llStreamOpen(code)
|
||||||
echo "my secret"
|
let std = findNimStdLibCompileTime()
|
||||||
else:
|
var intr = createInterpreter(moduleName, [std])
|
||||||
main()
|
intr.evalScript(stream)
|
||||||
|
destroyInterpreter(intr)
|
||||||
|
llStreamClose(stream)
|
||||||
|
|
||||||
|
evalString("echo 10+1")
|
||||||
|
evalString("echo 10+2")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue