mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-09 14:32:53 +00:00
reset macrocache after each script evaluation (#22195)
(cherry picked from commit 2f109595e9)
This commit is contained in:
@@ -12,7 +12,9 @@ import
|
||||
ast, astalgo, modules, passes, condsyms,
|
||||
options, sem, llstream, lineinfos, vm,
|
||||
vmdef, modulegraphs, idents, os, pathutils,
|
||||
passaux, scriptconfig, std/compilesettings
|
||||
passaux,
|
||||
scriptconfig, std/[compilesettings, tables]
|
||||
|
||||
|
||||
type
|
||||
Interpreter* = ref object ## Use Nim as an interpreter with this object
|
||||
@@ -72,6 +74,9 @@ proc evalScript*(i: Interpreter; scriptStream: PLLStream = nil) =
|
||||
assert i != nil
|
||||
assert i.mainModule != nil, "no main module selected"
|
||||
initStrTables(i.graph, i.mainModule)
|
||||
i.graph.cacheSeqs.clear()
|
||||
i.graph.cacheCounters.clear()
|
||||
i.graph.cacheTables.clear()
|
||||
i.mainModule.ast = nil
|
||||
|
||||
let s = if scriptStream != nil: scriptStream
|
||||
|
||||
@@ -69,3 +69,30 @@ block error_hook:
|
||||
|
||||
doAssertRaises(VMQuit):
|
||||
i.evalScript()
|
||||
|
||||
block resetmacrocache:
|
||||
let std = findNimStdLibCompileTime()
|
||||
let intr = createInterpreter("script.nim", [std, std / "pure", std / "core"])
|
||||
proc evalString(intr: Interpreter; code: string) =
|
||||
let stream = llStreamOpen(code)
|
||||
intr.evalScript(stream)
|
||||
llStreamClose(stream)
|
||||
let code = """
|
||||
import std/[macrocache, macros]
|
||||
static:
|
||||
let counter = CacheCounter"valTest"
|
||||
inc counter
|
||||
assert counter.value == 1
|
||||
|
||||
const mySeq = CacheSeq"addTest"
|
||||
mySeq.add(newLit(5))
|
||||
mySeq.add(newLit("hello ic"))
|
||||
assert mySeq.len == 2
|
||||
|
||||
const mcTable = CacheTable"subTest"
|
||||
mcTable["toAdd"] = newStmtList() #would crash if not empty
|
||||
assert mcTable.len == 1
|
||||
"""
|
||||
intr.evalString(code)
|
||||
intr.evalString(code)
|
||||
destroyInterpreter(intr)
|
||||
Reference in New Issue
Block a user