module HsLua.Core.Closures
( pushPreCFunction
, pushHaskellFunction
) where
import Prelude hiding (error)
import HsLua.Core.Error (LuaError (..))
import HsLua.Core.Primary (error)
import HsLua.Core.Types (LuaE, PreCFunction, HaskellFunction, liftLua, runWith)
import Lua.Call (hslua_pushhsfunction)
import qualified Control.Monad.Catch as Catch
pushPreCFunction :: PreCFunction -> LuaE e ()
pushPreCFunction :: forall e. PreCFunction -> LuaE e ()
pushPreCFunction PreCFunction
preCFn = forall a e. (State -> IO a) -> LuaE e a
liftLua forall a b. (a -> b) -> a -> b
$ \State
l ->
State -> PreCFunction -> IO ()
hslua_pushhsfunction State
l PreCFunction
preCFn
{-# INLINABLE pushPreCFunction #-}
pushHaskellFunction :: LuaError e => HaskellFunction e -> LuaE e ()
pushHaskellFunction :: forall e. LuaError e => HaskellFunction e -> LuaE e ()
pushHaskellFunction HaskellFunction e
fn = do
let preCFn :: PreCFunction
preCFn State
l = forall e a. State -> LuaE e a -> IO a
runWith State
l (forall e. LuaError e => HaskellFunction e -> HaskellFunction e
exceptionToError HaskellFunction e
fn)
forall e. PreCFunction -> LuaE e ()
pushPreCFunction PreCFunction
preCFn
{-# INLINABLE pushHaskellFunction #-}
exceptionToError :: LuaError e => HaskellFunction e -> HaskellFunction e
exceptionToError :: forall e. LuaError e => HaskellFunction e -> HaskellFunction e
exceptionToError HaskellFunction e
op = HaskellFunction e
op forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
`Catch.catch` \e
e -> forall e. LuaError e => e -> LuaE e ()
pushException e
e forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall e. LuaE e NumResults
error
{-# INLINABLE exceptionToError #-}