aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2024-06-27 18:56:18 +0200
committerJohn MacFarlane <[email protected]>2024-06-28 09:24:41 -0600
commit78405f51ae380a5fc48cd460e1ea332bec7f9645 (patch)
treeb15f88f13564911ee4967a6e670c44ba1b91e8ce /pandoc-lua-engine/src/Text
parent22ca9e7a201d48fa0d1bbf0f4065f9e3342cb9bc (diff)
Lua: keep `lpeg` and `re` as "loaded" modules.
The modules `lpeg` and `re` are now treated as if they had been loaded with `require`. Previously the modules were only assigned to global values, but could be loaded again via `require`, thereby allowing to use a system-wide installation. However, this proved to be confusing. The old behavior can be restored by adding the following lines to the top of Lua scripts, or to the `init.lua` in the data dir. debug.registry()['_LOADED'].lpeg = nil debug.registry()['_LOADED'].re = nil
Diffstat (limited to 'pandoc-lua-engine/src/Text')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Module.hs19
1 files changed, 3 insertions, 16 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module.hs
index d0abc8948..5302ab6fe 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module.hs
@@ -13,8 +13,7 @@ module Text.Pandoc.Lua.Module
( initModules
) where
-import Control.Monad (forM, forM_, when)
-import Data.Maybe (catMaybes)
+import Control.Monad (forM_, when)
import Data.Version (makeVersion)
import HsLua as Lua
import Text.Pandoc.Error (PandocError)
@@ -111,37 +110,25 @@ setGlobalModules = liftPandocLua $ do
[ ("lpeg", LPeg.luaopen_lpeg_ptr) -- must be loaded first
, ("re", LPeg.luaopen_re_ptr) -- re depends on lpeg
]
- loadedBuiltInModules <- fmap catMaybes . forM globalModules $
+ forM_ globalModules $
\(pkgname, luaopen) -> do
Lua.pushcfunction luaopen
- usedBuiltIn <- Lua.pcall 0 1 Nothing >>= \case
+ Lua.pcall 0 1 Nothing >>= \case
OK -> do -- all good, loading succeeded
-- register as loaded module so later modules can rely on this
Lua.getfield Lua.registryindex Lua.loaded
Lua.pushvalue (Lua.nth 2)
Lua.setfield (Lua.nth 2) pkgname
Lua.pop 1 -- pop _LOADED
- return True
_ -> do -- built-in library failed, load system lib
Lua.pop 1 -- ignore error message
-- Try loading via the normal package loading mechanism.
Lua.getglobal "require"
Lua.pushName pkgname
Lua.call 1 1 -- Throws an exception if loading failed again!
- return False
-- Module on top of stack. Register as global
Lua.setglobal pkgname
- return $ if usedBuiltIn then Just pkgname else Nothing
-
- -- Remove module entry from _LOADED table in registry if we used a
- -- built-in library. This ensures that later calls to @require@ will
- -- prefer the shared library, if any.
- forM_ loadedBuiltInModules $ \pkgname -> do
- Lua.getfield Lua.registryindex Lua.loaded
- Lua.pushnil
- Lua.setfield (Lua.nth 2) pkgname
- Lua.pop 1 -- registry
installLpegSearcher :: PandocLua ()
installLpegSearcher = liftPandocLua $ do