diff options
author | Juan M Gómez <info@jmgomez.me> | 2023-06-30 22:03:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-30 23:03:25 +0200 |
commit | 2f109595e982bd68b4f4f2fabf934409947b0ee1 (patch) | |
tree | 6312c2ff1276c6d2aad5d1badb13e2cf4cddcbd4 | |
parent | 4d2ebbb87703bdca1e20f4d94d92b07d97b5c07e (diff) | |
download | Nim-2f109595e982bd68b4f4f2fabf934409947b0ee1.tar.gz |
reset macrocache after each script evaluation (#22195)
-rw-r--r-- | compiler/nimeval.nim | 5 | ||||
-rw-r--r-- | tests/compilerapi/tcompilerapi.nim | 27 |
2 files changed, 31 insertions, 1 deletions
diff --git a/compiler/nimeval.nim b/compiler/nimeval.nim index 8e8f4ac7b..e98de7e62 100644 --- a/compiler/nimeval.nim +++ b/compiler/nimeval.nim @@ -12,7 +12,7 @@ import ast, modules, condsyms, options, llstream, lineinfos, vm, vmdef, modulegraphs, idents, os, pathutils, - scriptconfig, std/compilesettings + scriptconfig, std/[compilesettings, tables] import pipelines @@ -78,6 +78,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 diff --git a/tests/compilerapi/tcompilerapi.nim b/tests/compilerapi/tcompilerapi.nim index 828dcd0d9..2d6cfa0ca 100644 --- a/tests/compilerapi/tcompilerapi.nim +++ b/tests/compilerapi/tcompilerapi.nim @@ -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) \ No newline at end of file |