summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2018-01-23 21:29:52 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2018-01-23 21:29:52 +0100
commit2e0bb773345f489798666ac3c2d96d3873fa82a3 (patch)
tree0c984a361698c9a3247f92c1cc97d14dbe6957ba /src
parentf0671bf4c7f426a24eeb1a30f3597c17c7b80fd4 (diff)
Lua: move getTag from StackInstances to Util
Change: minor
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Lua/StackInstances.hs11
-rw-r--r--src/Text/Pandoc/Lua/Util.hs19
2 files changed, 17 insertions, 13 deletions
diff --git a/src/Text/Pandoc/Lua/StackInstances.hs b/src/Text/Pandoc/Lua/StackInstances.hs
index a504e5626..7e0dc20c4 100644
--- a/src/Text/Pandoc/Lua/StackInstances.hs
+++ b/src/Text/Pandoc/Lua/StackInstances.hs
@@ -42,8 +42,7 @@ import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
ToLuaStack (push), Type (..), throwLuaError, tryLua)
import Text.Pandoc.Definition
import Text.Pandoc.Extensions (Extensions)
-import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor,
- typeCheck)
+import Text.Pandoc.Lua.Util (getTable, getTag, pushViaConstructor, typeCheck)
import Text.Pandoc.Options (ReaderOptions (..), TrackChanges)
import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
@@ -300,14 +299,6 @@ peekInline idx = defineHowTo "get Inline value" $ do
elementContent :: FromLuaStack a => Lua a
elementContent = getTable idx "c"
-getTag :: StackIndex -> Lua String
-getTag idx = do
- top <- Lua.gettop
- hasMT <- Lua.getmetatable idx
- push "tag"
- if hasMT then Lua.rawget (-2) else Lua.rawget (idx `adjustIndexBy` 1)
- peek Lua.stackTop `finally` Lua.settop top
-
withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
withAttr f (attributes, x) = f (fromLuaAttr attributes) x
diff --git a/src/Text/Pandoc/Lua/Util.hs b/src/Text/Pandoc/Lua/Util.hs
index a3af155c9..f82ec4753 100644
--- a/src/Text/Pandoc/Lua/Util.hs
+++ b/src/Text/Pandoc/Lua/Util.hs
@@ -29,7 +29,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Lua utility functions.
-}
module Text.Pandoc.Lua.Util
- ( adjustIndexBy
+ ( getTag
, getTable
, addValue
, addFunction
@@ -47,6 +47,7 @@ module Text.Pandoc.Lua.Util
) where
import Control.Monad (when)
+import Control.Monad.Catch (finally)
import Data.ByteString.Char8 (unpack)
import Foreign.Lua (FromLuaStack (..), NumResults, Lua, NumArgs, StackIndex,
ToLuaStack (..), ToHaskellFunction, getglobal')
@@ -163,11 +164,23 @@ loadScriptFromDataDir datadir scriptFile = do
-- to @require@, the a new loader function was created which then become
-- garbage. If that function is collected at an inopportune times, i.e. when the
-- Lua API is called via a function that doesn't allow calling back into Haskell
--- (getraw, setraw, …). The function's finalizer, and the full program, hangs
--- when that happens.
+-- (getraw, setraw, …), then the function's finalizer, and the full program,
+-- will hang.
dostring' :: String -> Lua Status
dostring' script = do
loadRes <- Lua.loadstring script
if loadRes == Lua.OK
then Lua.pcall 0 1 Nothing <* Lua.gc Lua.GCCOLLECT 0
else return loadRes
+
+-- | Get the tag of a value. This is an optimized and specialized version of
+-- @Lua.getfield idx "tag"@. It only checks for the field on the table at index
+-- @idx@ and on its metatable, also ignoring any @__index@ value on the
+-- metatable.
+getTag :: StackIndex -> Lua String
+getTag idx = do
+ top <- Lua.gettop
+ hasMT <- Lua.getmetatable idx
+ push "tag"
+ if hasMT then Lua.rawget (-2) else Lua.rawget (idx `adjustIndexBy` 1)
+ peek Lua.stackTop `finally` Lua.settop top