From beb78a552cb3480d55b8eca8c0c77bccd5804506 Mon Sep 17 00:00:00 2001
From: Albert Krewinkel <albert.krewinkel@tourstream.eu>
Date: Tue, 27 Jun 2017 17:11:42 +0200
Subject: [PATCH] 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.
---
 src/Text/Pandoc/Lua.hs | 36 +++++++++++-------------------------
 1 file 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 ()