diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2017-12-22 20:08:45 +0100 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2017-12-22 20:08:45 +0100 |
commit | 9ddf84072b27553e9ffd578ae40003108f51015a (patch) | |
tree | 51314d418af7c25edafc0da17340d7378823c9a5 /src | |
parent | 9758720a24d7cc9782579bb237d747d32cf72835 (diff) | |
download | pandoc-9ddf84072b27553e9ffd578ae40003108f51015a.tar.gz |
Lua.Util: avoid altering the stack if peeking fails
The stack now remains unaltered if `getRawInt` or `getTable` fail. This
is important when those functions are used in an operation that is part
of an Alternative.
Change: minor
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Lua/Util.hs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Lua/Util.hs b/src/Text/Pandoc/Lua/Util.hs index e688ad255..28d09d339 100644 --- a/src/Text/Pandoc/Lua/Util.hs +++ b/src/Text/Pandoc/Lua/Util.hs @@ -67,7 +67,7 @@ getTable :: (ToLuaStack a, FromLuaStack b) => StackIndex -> a -> Lua b getTable idx key = do push key rawget (idx `adjustIndexBy` 1) - peek (-1) <* pop 1 + popValue -- | Add a key-value pair to the table at the top of the stack. addValue :: (ToLuaStack a, ToLuaStack b) => a -> b -> Lua () @@ -86,10 +86,9 @@ addFunction name fn = do -- | Get value behind key from table at given index. getRawInt :: FromLuaStack a => StackIndex -> Int -> Lua a -getRawInt idx key = +getRawInt idx key = do rawgeti idx key - *> peek (-1) - <* pop 1 + popValue -- | Set numeric key/value in table at the given index setRawInt :: ToLuaStack a => StackIndex -> Int -> a -> Lua () @@ -106,6 +105,15 @@ raiseError e = do Lua.push e fromIntegral <$> Lua.lerror +-- | Get, then pop the value at the top of the stack. +popValue :: FromLuaStack a => Lua a +popValue = do + resOrError <- Lua.peekEither (-1) + pop 1 + case resOrError of + Left err -> Lua.throwLuaError err + Right x -> return x + -- | Newtype wrapper intended to be used for optional Lua values. Nesting this -- type is strongly discouraged and will likely lead to a wrong result. newtype OrNil a = OrNil { toMaybe :: Maybe a } |