From fca93efb624af48a212a4597a116bfcde8b7192f Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Tue, 4 Apr 2017 21:51:51 +0200 Subject: Use lua registry instead of named globals This is slightly cleaner while keeping performance approximately the same. --- src/Text/Pandoc/Lua.hs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs index d754b43b8..a68810bd7 100644 --- a/src/Text/Pandoc/Lua.hs +++ b/src/Text/Pandoc/Lua.hs @@ -48,8 +48,11 @@ runLuaFilter :: (MonadIO m) runLuaFilter filterPath args pd = liftIO $ do lua <- newstate Lua.openlibs lua + -- create table in registry to store filter functions + Lua.push lua ("PANDOC_FILTER_FUNCTIONS"::String) Lua.newtable lua - Lua.setglobal lua "PANDOC_FILTER_FUNCTIONS" -- hack, store functions here + Lua.rawset lua Lua.registryindex + -- store module in global "pandoc" pushPandocModule lua Lua.setglobal lua "pandoc" status <- Lua.loadfile lua filterPath @@ -171,12 +174,14 @@ runLuaFilterFunction lua lf inline = do Lua.pop lua 1 return res --- FIXME: use registry +-- | Push the filter function to the top of the stack. pushFilterFunction :: Lua.LuaState -> LuaFilterFunction a -> IO () pushFilterFunction lua lf = do - Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS" + -- The function is stored in a lua registry table, retrieve it from there. + push lua ("PANDOC_FILTER_FUNCTIONS"::String) + Lua.rawget lua Lua.registryindex Lua.rawgeti lua (-1) (functionIndex lf) - Lua.remove lua (-2) -- remove global from stack + Lua.remove lua (-2) -- remove registry table from stack instance StackValue (LuaFilterFunction a) where valuetype _ = Lua.TFUNCTION @@ -185,7 +190,8 @@ instance StackValue (LuaFilterFunction a) where isFn <- Lua.isfunction lua i when (not isFn) (error $ "Not a function at index " ++ (show i)) Lua.pushvalue lua i - Lua.getglobal lua "PANDOC_FILTER_FUNCTIONS" + push lua ("PANDOC_FILTER_FUNCTIONS"::String) + Lua.rawget lua Lua.registryindex len <- Lua.objlen lua (-1) Lua.insert lua (-2) Lua.rawseti lua (-2) (len + 1) -- cgit v1.2.3