@@ -67,7 +67,7 @@ getTable :: (ToLuaStack a, FromLuaStack b) => StackIndex -> a -> Lua b
6767getTable idx key = do
6868 push key
6969 rawget (idx `adjustIndexBy` 1)
70− peek (-1) <* pop 1
70+ popValue
7171
7272-- | Add a key-value pair to the table at the top of the stack.
7373addValue :: (ToLuaStack a, ToLuaStack b) => a -> b -> Lua ()
@@ -86,10 +86,9 @@ addFunction name fn = do
8686
8787-- | Get value behind key from table at given index.
8888getRawInt :: FromLuaStack a => StackIndex -> Int -> Lua a
89−getRawInt idx key =
89+getRawInt idx key = do
9090 rawgeti idx key
91− *> peek (-1)
92− <* pop 1
91+ popValue
9392
9493-- | Set numeric key/value in table at the given index
9594setRawInt :: ToLuaStack a => StackIndex -> Int -> a -> Lua ()
@@ -106,6 +105,15 @@ raiseError e = do
106105 Lua.push e
107106 fromIntegral <$> Lua.lerror
108107
108+-- | Get, then pop the value at the top of the stack.
109+popValue :: FromLuaStack a => Lua a
110+popValue = do
111+ resOrError <- Lua.peekEither (-1)
112+ pop 1
113+ case resOrError of
114+ Left err -> Lua.throwLuaError err
115+ Right x -> return x
116+
109117-- | Newtype wrapper intended to be used for optional Lua values. Nesting this
110118-- type is strongly discouraged and will likely lead to a wrong result.
111119newtype OrNil a = OrNil { toMaybe :: Maybe a }