{-# LANGUAGE CPP #-}
module Lua.Call
( hslua_pushhsfunction
) where
import Foreign.C (CInt (CInt))
import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.StablePtr (StablePtr, deRefStablePtr, newStablePtr)
import Foreign.Storable (peek)
import Lua.Types
( NumResults (NumResults)
, PreCFunction
, State (State)
)
#ifdef ALLOW_UNSAFE_GC
#define SAFTY unsafe
#else
#define SAFTY safe
#endif
foreign import ccall SAFTY "hslcall.c hslua_extracthsfun"
:: State -> IO (Ptr ())
foreign import ccall SAFTY "hslcall.c hslua_newhsfunction"
hslua_newhsfunction :: State -> StablePtr a -> IO ()
hslua_pushhsfunction :: State -> PreCFunction -> IO ()
hslua_pushhsfunction :: State -> (State -> IO NumResults) -> IO ()
hslua_pushhsfunction State
l State -> IO NumResults
preCFn =
(State -> IO NumResults) -> IO (StablePtr (State -> IO NumResults))
forall a. a -> IO (StablePtr a)
newStablePtr State -> IO NumResults
preCFn IO (StablePtr (State -> IO NumResults))
-> (StablePtr (State -> IO NumResults) -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= State -> StablePtr (State -> IO NumResults) -> IO ()
forall a. State -> StablePtr a -> IO ()
hslua_newhsfunction State
l
{-# INLINABLE hslua_pushhsfunction #-}
hslua_callhsfun :: PreCFunction
hslua_callhsfun :: State -> IO NumResults
hslua_callhsfun State
l = do
Ptr ()
udPtr <- State -> IO (Ptr ())
hslua_extracthsfun State
l
if Ptr ()
udPtr Ptr () -> Ptr () -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr ()
forall a. Ptr a
nullPtr
then [Char] -> IO NumResults
forall a. HasCallStack => [Char] -> a
error [Char]
"Cannot call function; corrupted Lua object!"
else do
State -> IO NumResults
fn <- Ptr (StablePtr (State -> IO NumResults))
-> IO (StablePtr (State -> IO NumResults))
forall a. Storable a => Ptr a -> IO a
peek (Ptr () -> Ptr (StablePtr (State -> IO NumResults))
forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) IO (StablePtr (State -> IO NumResults))
-> (StablePtr (State -> IO NumResults)
-> IO (State -> IO NumResults))
-> IO (State -> IO NumResults)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= StablePtr (State -> IO NumResults) -> IO (State -> IO NumResults)
forall a. StablePtr a -> IO a
deRefStablePtr
State -> IO NumResults
fn State
l
foreign export ccall hslua_callhsfun :: PreCFunction