Make `pairsFromTable` a separate function
@@ -43,6 +43,7 @@ module Foreign.Lua.Types.FromLuaStack
( FromLuaStack (..)
, Result
, peekEither
+ , pairsFromTable
) where
import Data.ByteString (ByteString)
@@ -112,7 +113,11 @@ instance FromLuaStack a => FromLuaStack [a] where
amendError err = throwLuaError ("Could not read list: " ++ show err)
instance (Ord a, FromLuaStack a, FromLuaStack b) => FromLuaStack (Map a b) where
- peek idx = fromList <$> do
+ peek idx = fromList <$> pairsFromTable idx
+
+-- | Read a table into a list of pairs.
+pairsFromTable :: (FromLuaStack a, FromLuaStack b) => StackIndex -> Lua [(a, b)]
+pairsFromTable idx = do
pushnil
remainingPairs
where
@@ -123,9 +128,6 @@ instance (Ord a, FromLuaStack a, FromLuaStack b) => FromLuaStack (Map a b) where
Just a -> (a:) <$> remainingPairs
-- | Get the next key-value pair from a table.
---
--- TODO: This function shows that there's a problem with the way we handle
--- errors.
nextPair :: (FromLuaStack a, FromLuaStack b)
=> StackIndex -> Lua (Maybe (a, b))
nextPair idx = do
