aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2022-10-02 21:27:05 +0200
committerJohn MacFarlane <[email protected]>2022-10-03 08:47:32 -0700
commit132931a49190e9ed7706f0e58d6e965b68c188c2 (patch)
treed5a27beafb36b1b8d735682eec2bbec1af3a5a4f /pandoc-lua-engine/src/Text
parentc8d08b945c308b597321a7412484ad1811859134 (diff)
Lua: Support running Lua with a GC-collected Lua state.
Diffstat (limited to 'pandoc-lua-engine/src/Text')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs21
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs11
2 files changed, 24 insertions, 8 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
index caa490d52..649bad04c 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
@@ -13,6 +13,7 @@ Functions to initialize the Lua interpreter.
module Text.Pandoc.Lua.Init
( runLua
, runLuaNoEnv
+ , runLuaWith
) where
import Control.Monad (forM, forM_, when)
@@ -20,10 +21,11 @@ import Control.Monad.Catch (throwM, try)
import Control.Monad.Trans (MonadIO (..))
import Data.Maybe (catMaybes)
import HsLua as Lua hiding (status, try)
+import HsLua.Core.Run as Lua
import Text.Pandoc.Class (PandocMonad, readDataFile)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Lua.Marshal.List (newListMetatable, pushListModule)
-import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, runPandocLua)
+import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, runPandocLuaWith)
import qualified Data.ByteString.Char8 as Char8
import qualified Data.Text as T
import qualified Lua.LPeg as LPeg
@@ -38,20 +40,27 @@ import qualified Text.Pandoc.Lua.Module.Template as Pandoc.Template
import qualified Text.Pandoc.Lua.Module.Types as Pandoc.Types
import qualified Text.Pandoc.Lua.Module.Utils as Pandoc.Utils
--- | Run the lua interpreter, using pandoc's default way of environment
+-- | Run the Lua interpreter, using pandoc's default way of environment
-- initialization.
runLua :: (PandocMonad m, MonadIO m)
=> LuaE PandocError a -> m (Either PandocError a)
-runLua action =
- runPandocLua . try $ do
+runLua action = do
+ runPandocLuaWith Lua.run . try $ do
+ initLuaState
+ liftPandocLua action
+
+runLuaWith :: (PandocMonad m, MonadIO m)
+ => GCManagedState -> LuaE PandocError a -> m (Either PandocError a)
+runLuaWith luaState action = do
+ runPandocLuaWith (withGCManagedState luaState) . try $ do
initLuaState
liftPandocLua action
-- | Like 'runLua', but ignores all environment variables like @LUA_PATH@.
runLuaNoEnv :: (PandocMonad m, MonadIO m)
=> LuaE PandocError a -> m (Either PandocError a)
-runLuaNoEnv action =
- runPandocLua . try $ do
+runLuaNoEnv action = do
+ runPandocLuaWith Lua.run . try $ do
liftPandocLua $ do
-- This is undocumented, but works -- the code is adapted from the
-- `lua.c` sources for the default interpreter.
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs
index e07a91d61..6564ec91d 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/PandocLua.hs
@@ -21,6 +21,7 @@ uses Lua to handle state.
module Text.Pandoc.Lua.PandocLua
( PandocLua (..)
, runPandocLua
+ , runPandocLuaWith
, liftPandocLua
) where
@@ -55,10 +56,16 @@ liftPandocLua = PandocLua
-- | Evaluate a @'PandocLua'@ computation, running all contained Lua
-- operations..
runPandocLua :: (PandocMonad m, MonadIO m) => PandocLua a -> m a
-runPandocLua pLua = do
+runPandocLua = runPandocLuaWith Lua.run
+
+runPandocLuaWith :: (PandocMonad m, MonadIO m)
+ => (forall b. LuaE PandocError b -> IO b)
+ -> PandocLua a
+ -> m a
+runPandocLuaWith runner pLua = do
origState <- getCommonState
globals <- defaultGlobals
- (result, newState) <- liftIO . Lua.run . unPandocLua $ do
+ (result, newState) <- liftIO . runner . unPandocLua $ do
putCommonState origState
liftPandocLua $ setGlobals globals
r <- pLua