aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert.krewinkel@tourstream.eu>2017-06-27 17:11:42 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2017-06-27 17:11:42 +0200
commitbeb78a552cb3480d55b8eca8c0c77bccd5804506 (patch)
tree5345d2d2434e61dcaa1b93b15c7c0dc5dc3ec745
parent4fab0d2c3092623b80e62e38886ef95963a9f836 (diff)
downloadpandoc-beb78a552cb3480d55b8eca8c0c77bccd5804506.tar.gz
Text.Pandoc.Lua: simplify filter function runner
The code still allowed to pass an arbitrary number of arguments to the filter function, as element properties were passed as function arguments at some point. Now we only pass the element as the single arg, so the code to handle multiple arguments is no longer necessary.
-rw-r--r--src/Text/Pandoc/Lua.hs36
1 files changed, 11 insertions, 25 deletions
diff --git a/src/Text/Pandoc/Lua.hs b/src/Text/Pandoc/Lua.hs
index 90f72d685..858212df1 100644
--- a/src/Text/Pandoc/Lua.hs
+++ b/src/Text/Pandoc/Lua.hs
@@ -188,34 +188,20 @@ instance StackValue LuaFilter where
push = undefined
peek lua idx = fmap (LuaFilter lua) <$> Lua.peek lua idx
--- | Helper class for pushing a single value to the stack via a lua function.
--- See @pushViaCall@.
-class PushViaFilterFunction a where
- pushViaFilterFunction' :: LuaState -> LuaFilterFunction -> IO () -> Int -> a
-
-instance StackValue a => PushViaFilterFunction (IO a) where
- pushViaFilterFunction' lua lf pushArgs num = do
- pushFilterFunction lua lf
- pushArgs
- Lua.call lua num 1
- mbres <- Lua.peek lua (-1)
- case mbres of
- Nothing -> throwIO $ LuaException
- ("Error while trying to get a filter's return "
- ++ "value from lua stack.")
- Just res -> res <$ Lua.pop lua 1
-
-instance (StackValue a, PushViaFilterFunction b) =>
- PushViaFilterFunction (a -> b) where
- pushViaFilterFunction' lua lf pushArgs num x =
- pushViaFilterFunction' lua lf (pushArgs *> push lua x) (num + 1)
-
-- | Push a value to the stack via a lua filter function. The filter function is
-- called with all arguments that are passed to this function and is expected to
-- return a single value.
-runFilterFunction :: PushViaFilterFunction a
- => LuaState -> LuaFilterFunction -> a
-runFilterFunction lua lf = pushViaFilterFunction' lua lf (return ()) 0
+runFilterFunction :: StackValue a => LuaState -> LuaFilterFunction -> a -> IO a
+runFilterFunction lua lf x = do
+ pushFilterFunction lua lf
+ Lua.push lua x
+ Lua.call lua 1 1
+ mbres <- Lua.peek lua (-1)
+ case mbres of
+ Nothing -> throwIO $ LuaException
+ ("Error while trying to get a filter's return "
+ ++ "value from lua stack.")
+ Just res -> res <$ Lua.pop lua 1
-- | Push the filter function to the top of the stack.
pushFilterFunction :: Lua.LuaState -> LuaFilterFunction -> IO ()