{-|
Module      : HsLua.Module.SystemUtils
Copyright   : © 2019-2023 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <tarleb@hslua.org>

Utility functions and types for HsLua's system module.
-}
module HsLua.Module.SystemUtils
  ( Callback (..)
  , peekCallback
  , invoke
  , invokeWithFilePath
  , ioToLua
  )
where

import Control.Exception (IOException, try)
import HsLua.Core hiding (try)
import HsLua.Marshalling

-- | Lua callback function. This type is similar to @'AnyValue'@, and
-- the same caveats apply.
newtype Callback = Callback StackIndex

peekCallback :: Peeker e Callback
peekCallback :: forall e. Peeker e Callback
peekCallback = forall e a. Name -> (StackIndex -> LuaE e (Maybe a)) -> Peeker e a
reportValueOnFailure Name
"function" forall a b. (a -> b) -> a -> b
$ \StackIndex
idx -> do
  StackIndex
idx' <- forall e. StackIndex -> LuaE e StackIndex
absindex StackIndex
idx
  Bool
isFn <- forall e. StackIndex -> LuaE e Bool
isfunction StackIndex
idx'
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
isFn
           then forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ StackIndex -> Callback
Callback StackIndex
idx'
           else forall a. Maybe a
Nothing

pushCallback :: Pusher e Callback
pushCallback :: forall e. Pusher e Callback
pushCallback (Callback StackIndex
idx) = forall e. StackIndex -> LuaE e ()
pushvalue StackIndex
idx

-- | Call Lua callback function and return all of its results.
invoke :: LuaError e
       => Callback -> LuaE e NumResults
invoke :: forall e. LuaError e => Callback -> LuaE e NumResults
invoke Callback
callback = do
  StackIndex
oldTop <- forall e. LuaE e StackIndex
gettop
  forall e. Pusher e Callback
pushCallback Callback
callback
  forall e. LuaError e => NumArgs -> NumResults -> LuaE e ()
call NumArgs
0 NumResults
multret
  StackIndex
newTop <- forall e. LuaE e StackIndex
gettop
  forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> NumResults
NumResults forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> CInt
fromStackIndex forall a b. (a -> b) -> a -> b
$
    StackIndex
newTop forall a. Num a => a -> a -> a
- StackIndex
oldTop

-- | Call Lua callback function with the given filename as its argument.
invokeWithFilePath :: LuaError e
                   => Callback -> FilePath -> LuaE e NumResults
invokeWithFilePath :: forall e. LuaError e => Callback -> FilePath -> LuaE e NumResults
invokeWithFilePath Callback
callback FilePath
filename = do
  StackIndex
oldTop <- forall e. LuaE e StackIndex
gettop
  forall e. Pusher e Callback
pushCallback Callback
callback
  forall e. FilePath -> LuaE e ()
pushString FilePath
filename
  forall e. LuaError e => NumArgs -> NumResults -> LuaE e ()
call (CInt -> NumArgs
NumArgs CInt
1) NumResults
multret
  StackIndex
newTop <- forall e. LuaE e StackIndex
gettop
  forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> NumResults
NumResults forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> CInt
fromStackIndex forall a b. (a -> b) -> a -> b
$
    StackIndex
newTop forall a. Num a => a -> a -> a
- StackIndex
oldTop

-- | Convert a System IO operation to a Lua operation.
ioToLua :: LuaError e => IO a -> LuaE e a
ioToLua :: forall e a. LuaError e => IO a -> LuaE e a
ioToLua IO a
action = do
  Either IOException a
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => IO a -> IO (Either e a)
try IO a
action)
  case Either IOException a
result of
    Right a
result' -> forall (m :: * -> *) a. Monad m => a -> m a
return a
result'
    Left IOException
err      -> forall e a. LuaError e => FilePath -> LuaE e a
failLua (forall a. Show a => a -> FilePath
show (IOException
err :: IOException))