-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathInvokable.hs
More file actions
49 lines (43 loc) · 1.52 KB
/
Invokable.hs
File metadata and controls
49 lines (43 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module : HsLua.Class.Invokable
Copyright : © 2007–2012 Gracjan Polak,
2012–2016 Ömer Sinan Ağacan,
2017-2021 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Stability : beta
Portability : FlexibleInstances, ForeignFunctionInterface, ScopedTypeVariables
Call Lua functions from Haskell.
-}
module HsLua.Class.Invokable
( Invokable (..)
, invoke
) where
import Data.ByteString (append)
import HsLua.Core as Lua
import HsLua.Class.Peekable
import HsLua.Class.Pushable
import HsLua.Class.Util (popValue)
-- | Helper class used to make Lua functions useable from Haskell.
class PeekError e => Invokable e a where
addArg :: Name -> LuaE e () -> NumArgs -> a
instance (PeekError e, Peekable a) => Invokable e (LuaE e a) where
addArg fnName pushArgs nargs = do
_ <- dostring $ "return " `append` Lua.fromName fnName
pushArgs
call nargs 1
popValue
instance (Pushable a, PeekError e, Invokable e b) => Invokable e (a -> b) where
addArg fnName pushArgs nargs x =
addArg fnName (pushArgs *> push x) (nargs + 1)
-- | Invoke a Lua function. Use as:
--
-- > v <- invoke "proc" "abc" (1::Int) (5.0::Double)
invoke :: forall e a. Invokable e a => Name -> a
invoke fname = addArg @e fname (return ()) 0