nimeval.nim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## exposes the Nim VM to clients.
  10. import
  11. ast, modules, condsyms,
  12. options, llstream, lineinfos, vm,
  13. vmdef, modulegraphs, idents, os, pathutils,
  14. scriptconfig, std/compilesettings
  15. import pipelines
  16. when defined(nimPreviewSlimSystem):
  17. import std/[assertions, syncio]
  18. type
  19. Interpreter* = ref object ## Use Nim as an interpreter with this object
  20. mainModule: PSym
  21. graph: ModuleGraph
  22. scriptName: string
  23. idgen: IdGenerator
  24. iterator exportedSymbols*(i: Interpreter): PSym =
  25. assert i != nil
  26. assert i.mainModule != nil, "no main module selected"
  27. for s in modulegraphs.allSyms(i.graph, i.mainModule):
  28. yield s
  29. proc selectUniqueSymbol*(i: Interpreter; name: string;
  30. symKinds: set[TSymKind] = {skLet, skVar}): PSym =
  31. ## Can be used to access a unique symbol of ``name`` and
  32. ## the given ``symKinds`` filter.
  33. assert i != nil
  34. assert i.mainModule != nil, "no main module selected"
  35. let n = getIdent(i.graph.cache, name)
  36. var it: ModuleIter
  37. var s = initModuleIter(it, i.graph, i.mainModule, n)
  38. result = nil
  39. while s != nil:
  40. if s.kind in symKinds:
  41. if result == nil: result = s
  42. else: return nil # ambiguous
  43. s = nextModuleIter(it, i.graph)
  44. proc selectRoutine*(i: Interpreter; name: string): PSym =
  45. ## Selects a declared routine (proc/func/etc) from the main module.
  46. ## The routine needs to have the export marker ``*``. The only matching
  47. ## routine is returned and ``nil`` if it is overloaded.
  48. result = selectUniqueSymbol(i, name, {skTemplate, skMacro, skFunc,
  49. skMethod, skProc, skConverter})
  50. proc callRoutine*(i: Interpreter; routine: PSym; args: openArray[PNode]): PNode =
  51. assert i != nil
  52. result = vm.execProc(PCtx i.graph.vm, routine, args)
  53. proc getGlobalValue*(i: Interpreter; letOrVar: PSym): PNode =
  54. result = vm.getGlobalValue(PCtx i.graph.vm, letOrVar)
  55. proc setGlobalValue*(i: Interpreter; letOrVar: PSym, val: PNode) =
  56. ## Sets a global value to a given PNode, does not do any type checking.
  57. vm.setGlobalValue(PCtx i.graph.vm, letOrVar, val)
  58. proc implementRoutine*(i: Interpreter; pkg, module, name: string;
  59. impl: proc (a: VmArgs) {.closure, gcsafe.}) =
  60. assert i != nil
  61. let vm = PCtx(i.graph.vm)
  62. vm.registerCallback(pkg & "." & module & "." & name, impl)
  63. proc evalScript*(i: Interpreter; scriptStream: PLLStream = nil) =
  64. ## This can also be used to *reload* the script.
  65. assert i != nil
  66. assert i.mainModule != nil, "no main module selected"
  67. initStrTables(i.graph, i.mainModule)
  68. i.mainModule.ast = nil
  69. let s = if scriptStream != nil: scriptStream
  70. else: llStreamOpen(findFile(i.graph.config, i.scriptName), fmRead)
  71. discard processPipelineModule(i.graph, i.mainModule, i.idgen, s)
  72. proc findNimStdLib*(): string =
  73. ## Tries to find a path to a valid "system.nim" file.
  74. ## Returns "" on failure.
  75. try:
  76. let nimexe = os.findExe("nim")
  77. # this can't work with choosenim shims, refs https://github.com/dom96/choosenim/issues/189
  78. # it'd need `nim dump --dump.format:json . | jq -r .libpath`
  79. # which we should simplify as `nim dump --key:libpath`
  80. if nimexe.len == 0: return ""
  81. result = nimexe.splitPath()[0] /../ "lib"
  82. if not fileExists(result / "system.nim"):
  83. when defined(unix):
  84. result = nimexe.expandSymlink.splitPath()[0] /../ "lib"
  85. if not fileExists(result / "system.nim"): return ""
  86. except OSError, ValueError:
  87. return ""
  88. proc findNimStdLibCompileTime*(): string =
  89. ## Same as `findNimStdLib` but uses source files used at compile time,
  90. ## and asserts on error.
  91. result = querySetting(libPath)
  92. doAssert fileExists(result / "system.nim"), "result:" & result
  93. proc createInterpreter*(scriptName: string;
  94. searchPaths: openArray[string];
  95. flags: TSandboxFlags = {},
  96. defines = @[("nimscript", "true")],
  97. registerOps = true): Interpreter =
  98. var conf = newConfigRef()
  99. var cache = newIdentCache()
  100. var graph = newModuleGraph(cache, conf)
  101. connectPipelineCallbacks(graph)
  102. initDefines(conf.symbols)
  103. for define in defines:
  104. defineSymbol(conf.symbols, define[0], define[1])
  105. for p in searchPaths:
  106. conf.searchPaths.add(AbsoluteDir p)
  107. if conf.libpath.isEmpty: conf.libpath = AbsoluteDir p
  108. var m = graph.makeModule(scriptName)
  109. incl(m.flags, sfMainModule)
  110. var idgen = idGeneratorFromModule(m)
  111. var vm = newCtx(m, cache, graph, idgen)
  112. vm.mode = emRepl
  113. vm.features = flags
  114. if registerOps:
  115. vm.registerAdditionalOps() # Required to register parts of stdlib modules
  116. graph.vm = vm
  117. setPipeLinePass(graph, EvalPass)
  118. graph.compilePipelineSystemModule()
  119. result = Interpreter(mainModule: m, graph: graph, scriptName: scriptName, idgen: idgen)
  120. proc destroyInterpreter*(i: Interpreter) =
  121. ## destructor.
  122. discard "currently nothing to do."
  123. proc registerErrorHook*(i: Interpreter, hook:
  124. proc (config: ConfigRef; info: TLineInfo; msg: string;
  125. severity: Severity) {.gcsafe.}) =
  126. i.graph.config.structuredErrorHook = hook
  127. proc runRepl*(r: TLLRepl;
  128. searchPaths: openArray[string];
  129. supportNimscript: bool) =
  130. ## deadcode but please don't remove... might be revived
  131. var conf = newConfigRef()
  132. var cache = newIdentCache()
  133. var graph = newModuleGraph(cache, conf)
  134. for p in searchPaths:
  135. conf.searchPaths.add(AbsoluteDir p)
  136. if conf.libpath.isEmpty: conf.libpath = AbsoluteDir p
  137. conf.cmd = cmdInteractive # see also `setCmd`
  138. conf.setErrorMaxHighMaybe
  139. initDefines(conf.symbols)
  140. defineSymbol(conf.symbols, "nimscript")
  141. if supportNimscript: defineSymbol(conf.symbols, "nimconfig")
  142. when hasFFI: defineSymbol(graph.config.symbols, "nimffi")
  143. var m = graph.makeStdinModule()
  144. incl(m.flags, sfMainModule)
  145. var idgen = idGeneratorFromModule(m)
  146. if supportNimscript: graph.vm = setupVM(m, cache, "stdin", graph, idgen)
  147. setPipeLinePass(graph, InterpreterPass)
  148. graph.compilePipelineSystemModule()
  149. discard processPipelineModule(graph, m, idgen, llStreamOpenStdIn(r))