{-# 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 -> PreCFunction -> IO ()
hslua_pushhsfunction State
l PreCFunction
preCFn =
PreCFunction -> IO (StablePtr PreCFunction)
forall a. a -> IO (StablePtr a)
newStablePtr PreCFunction
preCFn IO (StablePtr PreCFunction)
-> (StablePtr PreCFunction -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= State -> StablePtr PreCFunction -> IO ()
forall a. State -> StablePtr a -> IO ()
hslua_newhsfunction State
l
{-# INLINABLE hslua_pushhsfunction #-}
hslua_callhsfun :: PreCFunction
hslua_callhsfun :: PreCFunction
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
PreCFunction
fn <- Ptr (StablePtr PreCFunction) -> IO (StablePtr PreCFunction)
forall a. Storable a => Ptr a -> IO a
peek (Ptr () -> Ptr (StablePtr PreCFunction)
forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) IO (StablePtr PreCFunction)
-> (StablePtr PreCFunction -> IO PreCFunction) -> IO PreCFunction
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= StablePtr PreCFunction -> IO PreCFunction
forall a. StablePtr a -> IO a
deRefStablePtr
PreCFunction
fn State
l
foreign export ccall hslua_callhsfun :: PreCFunction