hslua-classes-2.1.0: Type classes for HsLua

Copyright | © 2007–2012 Gracjan Polak; © 2012–2016 Ömer Sinan Ağacan; © 2017-2022 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb+hslua@zeitkraut.de>
Stability | beta
Portability | non-portable (depends on GHC)
Safe Haskell | None
Language | Haskell2010

HsLua.Classes

Contents

- Receiving values from Lua stack (Lua → Haskell)
- Pushing values to Lua stack (Haskell → Lua)
- Calling Functions
- Utility functions and types
  - Retrieving values

Description

Convenience module which re-exports all classes and utility functions provided by the hslua-classes package.

Synopsis

- class Peekable a where
  - safepeek :: LuaError e => Peeker e a
- peekEither :: (LuaError e, Peekable a) => StackIndex -> LuaE e (Either e a)
- class Pushable a where
  - push :: LuaError e => a -> LuaE e ()
- pushList :: (LuaError e, Pushable a) => [a] -> LuaE e ()
- class LuaError e => Exposable e a where
  - partialApply :: StackIndex -> a -> Peek e NumResults
- toHaskellFunction :: forall e a. Exposable e a => a -> HaskellFunction e
- invoke :: Invokable a => Name -> a
- registerHaskellFunction :: Exposable e a => Name -> a -> LuaE e ()
- raiseError :: (LuaError e, Pushable a) => a -> LuaE e NumResults
- newtype Optional a = Optional {
  - fromOptional :: Maybe a
  }
- popValue :: (LuaError e, Peekable a) => LuaE e a

# Receiving values from Lua stack (Lua → Haskell)

class Peekable a where #

A value that can be read from the Lua stack.

Methods

safepeek :: LuaError e => Peeker e a #

Function that retrieves a value from the Lua stack.

#### Instances

Instances details

Peekable Bool # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Bool #
Peekable Double # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Double #
Peekable Float # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Float #
Peekable Int # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Int #
Peekable Integer # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Integer #
Peekable () # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e () #
Peekable ByteString # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e ByteString #
Peekable ByteString # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e ByteString #
Peekable Text # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Text #
Peekable Number # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Number #
Peekable Integer # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e Integer #
Peekable CFunction # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e CFunction #
Peekable State # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e State #
Peekable [Char] # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e [Char] #
Peekable a => Peekable [a] # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e [a] #
Peekable (Ptr a) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (Ptr a) #
(Ord a, Peekable a) => Peekable (Set a) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (Set a) #
Peekable a => Peekable (Optional a) # |
Instance details Defined in HsLua.Class.Util Methods safepeek :: LuaError e => Peeker e (Optional a) #
(Peekable a, Peekable b) => Peekable (a, b) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (a, b) #
(Ord a, Peekable a, Peekable b) => Peekable (Map a b) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (Map a b) #
(Peekable a, Peekable b, Peekable c) => Peekable (a, b, c) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (a, b, c) #
(Peekable a, Peekable b, Peekable c, Peekable d) => Peekable (a, b, c, d) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e => Peeker e (a, b, c, d) #
(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e) => Peekable (a, b, c, d, e) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e0 => Peeker e0 (a, b, c, d, e) #
(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e, Peekable f) => Peekable (a, b, c, d, e, f) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e0 => Peeker e0 (a, b, c, d, e, f) #
(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e, Peekable f, Peekable g) => Peekable (a, b, c, d, e, f, g) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e0 => Peeker e0 (a, b, c, d, e, f, g) #
(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e, Peekable f, Peekable g, Peekable h) => Peekable (a, b, c, d, e, f, g, h) # |
Instance details Defined in HsLua.Class.Peekable Methods safepeek :: LuaError e0 => Peeker e0 (a, b, c, d, e, f, g, h) #

peekEither :: (LuaError e, Peekable a) => StackIndex -> LuaE e (Either e a) #

Try to convert the value at the given stack index to a Haskell value. Returns `Left` with the error on failure.

# Pushing values to Lua stack (Haskell → Lua)

class Pushable a where #

A value that can be pushed to the Lua stack.

Methods

push :: LuaError e => a -> LuaE e () #

Pushes a value onto Lua stack, casting it into meaningfully nearest Lua type.

#### Instances

Instances details

Pushable Bool # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Bool -> LuaE e () #
Pushable Double # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Double -> LuaE e () #
Pushable Float # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Float -> LuaE e () #
Pushable Int # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Int -> LuaE e () #
Pushable Integer # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Integer -> LuaE e () #
Pushable () # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => () -> LuaE e () #
Pushable ByteString # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => ByteString -> LuaE e () #
Pushable ByteString # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => ByteString -> LuaE e () #
Pushable Text # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Text -> LuaE e () #
Pushable Number # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Number -> LuaE e () #
Pushable Integer # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Integer -> LuaE e () #
Pushable CFunction # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => CFunction -> LuaE e () #
Pushable [Char] # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => [Char] -> LuaE e () #
Pushable a => Pushable [a] # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => [a] -> LuaE e () #
Pushable (Ptr a) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Ptr a -> LuaE e () #
Pushable a => Pushable (Set a) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Set a -> LuaE e () #
Pushable a => Pushable (Optional a) # |
Instance details Defined in HsLua.Class.Util Methods push :: LuaError e => Optional a -> LuaE e () #
(Pushable a, Pushable b) => Pushable (a, b) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => (a, b) -> LuaE e () #
(Pushable a, Pushable b) => Pushable (Map a b) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => Map a b -> LuaE e () #
(Pushable a, Pushable b, Pushable c) => Pushable (a, b, c) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => (a, b, c) -> LuaE e () #
(Pushable a, Pushable b, Pushable c, Pushable d) => Pushable (a, b, c, d) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e => (a, b, c, d) -> LuaE e () #
(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e) => Pushable (a, b, c, d, e) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e0 => (a, b, c, d, e) -> LuaE e0 () #
(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e, Pushable f) => Pushable (a, b, c, d, e, f) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e0 => (a, b, c, d, e, f) -> LuaE e0 () #
(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e, Pushable f, Pushable g) => Pushable (a, b, c, d, e, f, g) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e0 => (a, b, c, d, e, f, g) -> LuaE e0 () #
(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e, Pushable f, Pushable g, Pushable h) => Pushable (a, b, c, d, e, f, g, h) # |
Instance details Defined in HsLua.Class.Pushable Methods push :: LuaError e0 => (a, b, c, d, e, f, g, h) -> LuaE e0 () #

pushList :: (LuaError e, Pushable a) => [a] -> LuaE e () #

Push list as numerically indexed table.

# Calling Functions

class LuaError e => Exposable e a where #

Operations and functions that can be pushed to the Lua stack. This is a helper function not intended to be used directly. Use the ``toHaskellFunction`` wrapper instead.

Methods

partialApply :: StackIndex -> a -> Peek e NumResults #

Helper function, called by ``toHaskellFunction``. Should do a partial application of the argument at the given index to the underlying function. Recurses if necessary, causing further partial applications until the operation is a easily exposable to Lua.

#### Instances

Instances details

LuaError e => Exposable e (HaskellFunction e) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> HaskellFunction e -> Peek e NumResults #
(Peekable a, Exposable e b) => Exposable e (a -> b) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> (a -> b) -> Peek e NumResults #
(LuaError e, Pushable a) => Exposable e (Peek e a) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> Peek e a -> Peek e NumResults #
(LuaError e, Pushable a) => Exposable e (LuaE e a) # |
Instance details Defined in HsLua.Class.Exposable Methods partialApply :: StackIndex -> LuaE e a -> Peek e NumResults #

toHaskellFunction :: forall e a. Exposable e a => a -> HaskellFunction e #

Convert a Haskell function to a function type directly exposable to Lua. Any Haskell function can be converted provided that:

- all arguments are instances of ``Peekable``
- return type is `LuaE e a`, where `a` is an instance of ``Pushable``

Any exception of type `e` will be caught.

Important: this does not catch exceptions other than `e`; exception handling must be done by the Haskell function. Failure to do so will cause the program to crash.

E.g., the following code could be used to handle an Exception of type FooException, if that type is an instance of `MonadCatch` and `Pushable`:

    toHaskellFunction (myFun `catchM` (\e -> raiseError (e :: FooException)))

invoke :: Invokable a => Name -> a #

Invoke a Lua function. Use as:

    v <- invoke "proc" "abc" (1::Int) (5.0::Double)

registerHaskellFunction :: Exposable e a => Name -> a -> LuaE e () #

Imports a Haskell function and registers it at global name.

# Utility functions and types

raiseError :: (LuaError e, Pushable a) => a -> LuaE e NumResults #

Raise a Lua error, using the given value as the error object.

newtype Optional a #

Newtype wrapper intended to be used for optional Lua values. Nesting this type is strongly discouraged as missing values on inner levels are indistinguishable from missing values on an outer level; wrong values would be the likely result.

Constructors

Optional |
Fields fromOptional :: Maybe a

#### Instances

Instances details

Peekable a => Peekable (Optional a) # |
Instance details Defined in HsLua.Class.Util Methods safepeek :: LuaError e => Peeker e (Optional a) #
Pushable a => Pushable (Optional a) # |
Instance details Defined in HsLua.Class.Util Methods push :: LuaError e => Optional a -> LuaE e () #

## Retrieving values

popValue :: (LuaError e, Peekable a) => LuaE e a #

Get, then pop the value at the top of the stack. The pop operation is executed even if the retrieval operation failed.