summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2018-10-11 00:44:35 -0700
committerAndreas Rumpf <rumpf_a@web.de>2018-10-11 09:44:35 +0200
commita58c982a494fbc8712067828f53b5c58ab6b3279 (patch)
tree73da18a23a55c2f4dadca171559c50aacb02c975
parent8fc7cecfa21d666f6ae0f778570f14c92a8d223a (diff)
downloadNim-a58c982a494fbc8712067828f53b5c58ab6b3279.tar.gz
test case for #9180 and re-enables the disabled tcompilerapi test (#9181)
* add findNimStdLibCompileTime and un-disable tcompilerapi test; add test case for #9180

* address comments
-rw-r--r--compiler/nimeval.nim7
-rw-r--r--tests/compilerapi/tcompilerapi.nim44
2 files changed, 29 insertions, 22 deletions
diff --git a/compiler/nimeval.nim b/compiler/nimeval.nim
index 841c38a46..1d7157cdf 100644
--- a/compiler/nimeval.nim
+++ b/compiler/nimeval.nim
@@ -89,6 +89,13 @@ proc findNimStdLib*(): string =
   except OSError, ValueError:
     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;
                         searchPaths: openArray[string];
                         flags: TSandboxFlags = {}): Interpreter =
diff --git a/tests/compilerapi/tcompilerapi.nim b/tests/compilerapi/tcompilerapi.nim
index 3d7d6b85f..30007eff0 100644
--- a/tests/compilerapi/tcompilerapi.nim
+++ b/tests/compilerapi/tcompilerapi.nim
@@ -2,20 +2,19 @@ discard """
   output: '''top level statements are executed!
 2.0
 my secret
+11
+12
 '''
 """
 
 ## Example program that demonstrates how to use the
 ## 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]
 
 proc main() =
-  let std = findNimStdLib()
-  if std.len == 0:
-    quit "cannot find Nim's standard library"
-
+  let std = findNimStdLibCompileTime()
   var intr = createInterpreter("myscript.nim", [std, getAppDir()])
   intr.implementRoutine("*", "exposed", "addFloats", proc (a: VmArgs) =
     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'"
   let res = intr.callRoutine(foreignProc, [newFloatNode(nkFloatLit, 0.9),
                                            newFloatNode(nkFloatLit, 0.1)])
-  if res.kind == nkFloatLit:
-    echo res.floatVal
-  else:
-    echo "bug!"
+  doAssert res.kind == nkFloatLit
+  echo res.floatVal
 
   let foreignValue = selectUniqueSymbol(intr, "hostProgramWantsThis")
   if foreignValue == nil:
     quit "script does not export a global of the name: hostProgramWantsThis"
   let val = intr.getGlobalValue(foreignValue)
-  if val.kind in {nkStrLit..nkTripleStrLit}:
-    echo val.strVal
-  else:
-    echo "bug!"
-
+  doAssert val.kind in {nkStrLit..nkTripleStrLit}
+  echo val.strVal
   destroyInterpreter(intr)
 
-if existsEnv("NIM_EXE_NOT_IN_PATH"):
-  # effectively disable this test as 'nim' is not in the PATH so tcompilerapi
-  # cannot find Nim's standard library:
-  echo "top level statements are executed!"
-  echo "2.0"
-  echo "my secret"
-else:
-  main()
+main()
+
+block issue9180:
+  proc evalString(code: string, moduleName = "script.nim") =
+    let stream = llStreamOpen(code)
+    let std = findNimStdLibCompileTime()
+    var intr = createInterpreter(moduleName, [std])
+    intr.evalScript(stream)
+    destroyInterpreter(intr)
+    llStreamClose(stream)
+
+  evalString("echo 10+1")
+  evalString("echo 10+2")
+