{-# LANGUAGE CPP #-}
module Lua.Userdata
( hslua_fromuserdata
, hslua_newhsuserdatauv
, hslua_newudmetatable
, hslua_putuserdata
) where
import Foreign.C (CInt (CInt), CString)
import Lua.Auxiliary (luaL_testudata)
import Lua.Primary (lua_newuserdatauv)
import Lua.Types
( LuaBool (..)
, StackIndex (..)
, State (..)
)
import Foreign.Ptr (castPtr, nullPtr)
import Foreign.StablePtr (newStablePtr, deRefStablePtr, freeStablePtr)
import Foreign.Storable (peek, poke, sizeOf)
#ifdef ALLOW_UNSAFE_GC
#define SAFTY unsafe
#else
#define SAFTY safe
#endif
foreign import ccall SAFTY "hsludata.h hslua_newudmetatable"
hslua_newudmetatable :: State
-> CString
-> IO LuaBool
hslua_newhsuserdatauv :: State
-> a
-> CInt
-> IO ()
hslua_newhsuserdatauv :: forall a. State -> a -> CInt -> IO ()
hslua_newhsuserdatauv State
l a
x CInt
nuvalue = do
StablePtr a
xPtr <- forall a. a -> IO (StablePtr a)
newStablePtr a
x
Ptr ()
udPtr <- State -> CSize -> CInt -> IO (Ptr ())
lua_newuserdatauv State
l (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall a. Storable a => a -> Int
sizeOf StablePtr a
xPtr) CInt
nuvalue
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) StablePtr a
xPtr
{-# INLINABLE hslua_newhsuserdatauv #-}
hslua_fromuserdata :: State
-> StackIndex
-> CString
-> IO (Maybe a)
hslua_fromuserdata :: forall a. State -> StackIndex -> CString -> IO (Maybe a)
hslua_fromuserdata State
l StackIndex
idx CString
name = do
Ptr ()
udPtr <- State -> StackIndex -> CString -> IO (Ptr ())
luaL_testudata State
l StackIndex
idx CString
name
if Ptr ()
udPtr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. StablePtr a -> IO a
deRefStablePtr)
{-# INLINABLE hslua_fromuserdata #-}
hslua_putuserdata :: State
-> StackIndex
-> CString
-> a
-> IO Bool
hslua_putuserdata :: forall a. State -> StackIndex -> CString -> a -> IO Bool
hslua_putuserdata State
l StackIndex
idx CString
name a
x = do
StablePtr a
xPtr <- forall a. a -> IO (StablePtr a)
newStablePtr a
x
Ptr ()
udPtr <- State -> StackIndex -> CString -> IO (Ptr ())
luaL_testudata State
l StackIndex
idx CString
name
if Ptr ()
udPtr forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else do
forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. StablePtr a -> IO ()
freeStablePtr
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr ()
udPtr) StablePtr a
xPtr
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
{-# INLINABLE hslua_putuserdata #-}