module HsLua.Core.Run
( run
, runEither
, runWith
, GCManagedState
, newGCManagedState
, closeGCManagedState
, withGCManagedState
) where
import Control.Exception (bracket, try)
import Control.Monad ((<$!>))
import Foreign.ForeignPtr
(ForeignPtr, finalizeForeignPtr, newForeignPtr, withForeignPtr)
import HsLua.Core.Types (LuaE, runWith)
import Lua.Primary (lua_close_ptr)
import Lua (State (..))
import qualified Control.Monad.Catch as Catch
import qualified HsLua.Core.Auxiliary as Lua
import qualified HsLua.Core.Primary as Lua
run :: LuaE e a -> IO a
run :: LuaE e a -> IO a
run = (IO State
Lua.newstate IO State -> (State -> IO ()) -> (State -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
`bracket` State -> IO ()
Lua.close) ((State -> IO a) -> IO a)
-> (LuaE e a -> State -> IO a) -> LuaE e a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (State -> LuaE e a -> IO a) -> LuaE e a -> State -> IO a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State -> LuaE e a -> IO a
forall e a. State -> LuaE e a -> IO a
runWith (LuaE e a -> State -> IO a)
-> (LuaE e a -> LuaE e a) -> LuaE e a -> State -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LuaE e a -> LuaE e a
forall (m :: * -> *) a. MonadMask m => m a -> m a
Catch.mask_
{-# INLINABLE run #-}
runEither :: Catch.Exception e => LuaE e a -> IO (Either e a)
runEither :: LuaE e a -> IO (Either e a)
runEither = IO a -> IO (Either e a)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO a -> IO (Either e a))
-> (LuaE e a -> IO a) -> LuaE e a -> IO (Either e a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LuaE e a -> IO a
forall e a. LuaE e a -> IO a
run
{-# INLINABLE runEither #-}
newtype GCManagedState = GCManagedState (ForeignPtr ())
newGCManagedState :: IO GCManagedState
newGCManagedState :: IO GCManagedState
newGCManagedState = do
(State Ptr ()
lptr) <- IO State
Lua.newstate
ForeignPtr () -> GCManagedState
GCManagedState (ForeignPtr () -> GCManagedState)
-> IO (ForeignPtr ()) -> IO GCManagedState
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> FinalizerPtr () -> Ptr () -> IO (ForeignPtr ())
forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
newForeignPtr FinalizerPtr ()
lua_close_ptr Ptr ()
lptr
closeGCManagedState :: GCManagedState -> IO ()
closeGCManagedState :: GCManagedState -> IO ()
closeGCManagedState (GCManagedState ForeignPtr ()
fptr) = ForeignPtr () -> IO ()
forall a. ForeignPtr a -> IO ()
finalizeForeignPtr ForeignPtr ()
fptr
withGCManagedState :: GCManagedState
-> LuaE e a
-> IO a
withGCManagedState :: GCManagedState -> LuaE e a -> IO a
withGCManagedState (GCManagedState ForeignPtr ()
fptr) LuaE e a
action =
ForeignPtr () -> (Ptr () -> IO a) -> IO a
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr ()
fptr ((Ptr () -> IO a) -> IO a) -> (Ptr () -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \Ptr ()
lptr ->
State -> LuaE e a -> IO a
forall e a. State -> LuaE e a -> IO a
runWith (Ptr () -> State
State Ptr ()
lptr) LuaE e a
action