git.iko.soy/typeable/pandoc
on master help sign in
Lua: move getTag from StackInstances to Util

Change: minor

commit2e0bb773345f489798666ac3c2d96d3873fa82a3 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 23 Jan 2018, 21:29 (9 years ago) parentf0671bf
2 files changed+17−13
src/Text/Pandoc/Lua/StackInstances.hs+1−10 src/Text/Pandoc/Lua/Util.hs+16−3
src/Text/Pandoc/Lua/StackInstances.hs+1−10
@@ -42,8 +42,7 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
4242 ToLuaStack (push), Type (..), throwLuaError, tryLua)
4343import Text.Pandoc.Definition
4444import Text.Pandoc.Extensions (Extensions)
45−import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor,
46− typeCheck)
45+import Text.Pandoc.Lua.Util (getTable, getTag, pushViaConstructor, typeCheck)
4746import Text.Pandoc.Options (ReaderOptions (..), TrackChanges)
4847import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
4948
@@ -300,14 +299,6 @@ peekInline idx = defineHowTo "get Inline value" $ do
300299 elementContent :: FromLuaStack a => Lua a
301300 elementContent = getTable idx "c"
302301
303−getTag :: StackIndex -> Lua String
304−getTag idx = do
305− top <- Lua.gettop
306− hasMT <- Lua.getmetatable idx
307− push "tag"
308− if hasMT then Lua.rawget (-2) else Lua.rawget (idx `adjustIndexBy` 1)
309− peek Lua.stackTop `finally` Lua.settop top
310−
311302withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
312303withAttr f (attributes, x) = f (fromLuaAttr attributes) x
313304
src/Text/Pandoc/Lua/Util.hs+16−3
@@ -29,7 +29,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2929Lua utility functions.
3030-}
3131module Text.Pandoc.Lua.Util
32− ( adjustIndexBy
32+ ( getTag
3333 , getTable
3434 , addValue
3535 , addFunction
@@ -47,6 +47,7 @@ module Text.Pandoc.Lua.Util
4747 ) where
4848
4949import Control.Monad (when)
50+import Control.Monad.Catch (finally)
5051import Data.ByteString.Char8 (unpack)
5152import Foreign.Lua (FromLuaStack (..), NumResults, Lua, NumArgs, StackIndex,
5253 ToLuaStack (..), ToHaskellFunction, getglobal')
@@ -163,11 +164,23 @@ loadScriptFromDataDir datadir scriptFile = do
163164-- to @require@, the a new loader function was created which then become
164165-- garbage. If that function is collected at an inopportune times, i.e. when the
165166-- Lua API is called via a function that doesn't allow calling back into Haskell
166−-- (getraw, setraw, …). The function's finalizer, and the full program, hangs
167−-- when that happens.
167+-- (getraw, setraw, …), then the function's finalizer, and the full program,
168+-- will hang.
168169dostring' :: String -> Lua Status
169170dostring' script = do
170171 loadRes <- Lua.loadstring script
171172 if loadRes == Lua.OK
172173 then Lua.pcall 0 1 Nothing <* Lua.gc Lua.GCCOLLECT 0
173174 else return loadRes
175+
176+-- | Get the tag of a value. This is an optimized and specialized version of
177+-- @Lua.getfield idx "tag"@. It only checks for the field on the table at index
178+-- @idx@ and on its metatable, also ignoring any @__index@ value on the
179+-- metatable.
180+getTag :: StackIndex -> Lua String
181+getTag idx = do
182+ top <- Lua.gettop
183+ hasMT <- Lua.getmetatable idx
184+ push "tag"
185+ if hasMT then Lua.rawget (-2) else Lua.rawget (idx `adjustIndexBy` 1)
186+ peek Lua.stackTop `finally` Lua.settop top