diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2017-04-04 21:51:51 +0200 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2017-04-04 21:51:51 +0200 |
commit | fca93efb624af48a212a4597a116bfcde8b7192f (patch) | |
tree | ddddb63dfc15167a05310115dd238628c87f9454 /src/Text/Pandoc | |
parent | 6b0d3d1582bd764a8496b4074608da11ae9349d4 (diff) | |
download | pandoc-fca93efb624af48a212a4597a116bfcde8b7192f.tar.gz |
Use lua registry instead of named globals
This is slightly cleaner while keeping performance approximately the
same.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r-- | src/Text/Pandoc/Lua.hs | 16 |
1 files changed, 11 insertions, 5 deletions
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) |