git.iko.soy/typeable/pandoc
on master help sign in
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.

commitbeb78a552cb3480d55b8eca8c0c77bccd5804506 authorAlbert Krewinkel <albert.krewinkel@tourstream.eu> dateTue 27 Jun 2017, 17:11 (9 years ago) parent4fab0d2
1 file changed+11−25
src/Text/Pandoc/Lua.hs+11−25
src/Text/Pandoc/Lua.hs+11−25
@@ -188,34 +188,20 @@ instance StackValue LuaFilter where
188188 push = undefined
189189 peek lua idx = fmap (LuaFilter lua) <$> Lua.peek lua idx
190190
191−-- | Helper class for pushing a single value to the stack via a lua function.
192−-- See @pushViaCall@.
193−class PushViaFilterFunction a where
194− pushViaFilterFunction' :: LuaState -> LuaFilterFunction -> IO () -> Int -> a
195−
196−instance StackValue a => PushViaFilterFunction (IO a) where
197− pushViaFilterFunction' lua lf pushArgs num = do
198− pushFilterFunction lua lf
199− pushArgs
200− Lua.call lua num 1
201− mbres <- Lua.peek lua (-1)
202− case mbres of
203− Nothing -> throwIO $ LuaException
204− ("Error while trying to get a filter's return "
205− ++ "value from lua stack.")
206− Just res -> res <$ Lua.pop lua 1
207−
208−instance (StackValue a, PushViaFilterFunction b) =>
209− PushViaFilterFunction (a -> b) where
210− pushViaFilterFunction' lua lf pushArgs num x =
211− pushViaFilterFunction' lua lf (pushArgs *> push lua x) (num + 1)
212−
213191-- | Push a value to the stack via a lua filter function. The filter function is
214192-- called with all arguments that are passed to this function and is expected to
215193-- return a single value.
216−runFilterFunction :: PushViaFilterFunction a
217− => LuaState -> LuaFilterFunction -> a
218−runFilterFunction lua lf = pushViaFilterFunction' lua lf (return ()) 0
194+runFilterFunction :: StackValue a => LuaState -> LuaFilterFunction -> a -> IO a
195+runFilterFunction lua lf x = do
196+ pushFilterFunction lua lf
197+ Lua.push lua x
198+ Lua.call lua 1 1
199+ mbres <- Lua.peek lua (-1)
200+ case mbres of
201+ Nothing -> throwIO $ LuaException
202+ ("Error while trying to get a filter's return "
203+ ++ "value from lua stack.")
204+ Just res -> res <$ Lua.pop lua 1
219205
220206-- | Push the filter function to the top of the stack.
221207pushFilterFunction :: Lua.LuaState -> LuaFilterFunction -> IO ()