Skip to content

Commit 6a88aba

Browse files
committed
Lua: keep CommonState object in the registry
The state is an internal value and should be treated as such. The `PANDOC_STATE` global is merely a copy; unsetting the global no longer breaks the Lua engine.
1 parent d50bd30 commit 6a88aba

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,12 @@ instance PandocMonad PandocLua where
7777
getModificationTime = IO.getModificationTime
7878

7979
getCommonState = PandocLua $ do
80-
Lua.getglobal "PANDOC_STATE"
80+
Lua.getfield registryindex "PANDOC_STATE"
8181
forcePeek $ peekCommonState Lua.top `lastly` pop 1
8282
putCommonState cst = PandocLua $ do
8383
pushCommonState cst
84+
Lua.pushvalue Lua.top
85+
Lua.setfield registryindex "PANDOC_STATE"
8486
Lua.setglobal "PANDOC_STATE"
8587

8688
logOutput = IO.logOutput

pandoc-lua-engine/test/lua/module/globals.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,11 @@ return {
147147
test('has property "verbosity"', function ()
148148
assert.are_equal(type(PANDOC_STATE.verbosity), 'string')
149149
end),
150+
test('can be deleted without breaking PandocLua monad functions', function()
151+
local state = PANDOC_STATE
152+
PANDOC_STATE = nil
153+
assert.is_nil(pandoc.mediabag.lookup('does-not-exist'))
154+
PANDOC_STATE = state
155+
end),
150156
},
151157
}

0 commit comments

Comments
 (0)