{-# LANGUAGE RankNTypes #-}
module HsLua.Core.Userdata
( newhsuserdata
, newudmetatable
, fromuserdata
, putuserdata
) where
import HsLua.Core.Types (LuaE, Name (..), StackIndex, liftLua, fromLuaBool)
import Lua.Userdata
( hslua_fromuserdata
, hslua_newhsuserdata
, hslua_newudmetatable
, hslua_putuserdata
)
import qualified Data.ByteString as B
newhsuserdata :: forall a e. a -> LuaE e ()
newhsuserdata :: a -> LuaE e ()
newhsuserdata = (State -> IO ()) -> LuaE e ()
forall a e. (State -> IO a) -> LuaE e a
liftLua ((State -> IO ()) -> LuaE e ())
-> (a -> State -> IO ()) -> a -> LuaE e ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State -> a -> IO ()) -> a -> State -> IO ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip State -> a -> IO ()
forall a. State -> a -> IO ()
hslua_newhsuserdata
{-# INLINABLE newhsuserdata #-}
newudmetatable :: Name -> LuaE e Bool
newudmetatable :: Name -> LuaE e Bool
newudmetatable (Name ByteString
name) = (State -> IO Bool) -> LuaE e Bool
forall a e. (State -> IO a) -> LuaE e a
liftLua ((State -> IO Bool) -> LuaE e Bool)
-> (State -> IO Bool) -> LuaE e Bool
forall a b. (a -> b) -> a -> b
$ \State
l ->
ByteString -> (CString -> IO Bool) -> IO Bool
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString ByteString
name ((LuaBool -> Bool) -> IO LuaBool -> IO Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LuaBool -> Bool
fromLuaBool (IO LuaBool -> IO Bool)
-> (CString -> IO LuaBool) -> CString -> IO Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. State -> CString -> IO LuaBool
hslua_newudmetatable State
l)
{-# INLINABLE newudmetatable #-}
fromuserdata :: forall a e.
StackIndex
-> Name
-> LuaE e (Maybe a)
fromuserdata :: StackIndex -> Name -> LuaE e (Maybe a)
fromuserdata StackIndex
idx (Name ByteString
name) = (State -> IO (Maybe a)) -> LuaE e (Maybe a)
forall a e. (State -> IO a) -> LuaE e a
liftLua ((State -> IO (Maybe a)) -> LuaE e (Maybe a))
-> (State -> IO (Maybe a)) -> LuaE e (Maybe a)
forall a b. (a -> b) -> a -> b
$ \State
l ->
ByteString -> (CString -> IO (Maybe a)) -> IO (Maybe a)
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString ByteString
name (State -> StackIndex -> CString -> IO (Maybe a)
forall a. State -> StackIndex -> CString -> IO (Maybe a)
hslua_fromuserdata State
l StackIndex
idx)
{-# INLINABLE fromuserdata #-}
putuserdata :: forall a e.
StackIndex
-> Name
-> a
-> LuaE e Bool
putuserdata :: StackIndex -> Name -> a -> LuaE e Bool
putuserdata StackIndex
idx (Name ByteString
name) a
x = (State -> IO Bool) -> LuaE e Bool
forall a e. (State -> IO a) -> LuaE e a
liftLua ((State -> IO Bool) -> LuaE e Bool)
-> (State -> IO Bool) -> LuaE e Bool
forall a b. (a -> b) -> a -> b
$ \State
l ->
ByteString -> (CString -> IO Bool) -> IO Bool
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString ByteString
name ((CString -> IO Bool) -> IO Bool)
-> (CString -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \CString
namePtr ->
State -> StackIndex -> CString -> a -> IO Bool
forall a. State -> StackIndex -> CString -> a -> IO Bool
hslua_putuserdata State
l StackIndex
idx CString
namePtr a
x
{-# INLINABLE putuserdata #-}