hslua-1.0.2: Bindings to Lua, an embeddable scripting language

Copyright© 2007–2012 Gracjan Polak
2012–2016 Ömer Sinan Ağacan
2017-2019 Albert Krewinkel
LicenseMIT
MaintainerAlbert Krewinkel <tarleb+hslua@zeitkraut.de>
Stabilitybeta
PortabilityFlexibleInstances, ForeignFunctionInterface, ScopedTypeVariables
Safe HaskellNone
LanguageHaskell2010

Foreign.Lua.FunctionCalling

Description

Call haskell functions from Lua, and vice versa.

Synopsis

Documentation

class Peekable a where #

A value that can be read from the Lua stack.

Minimal complete definition

peek

Methods

peek :: StackIndex -> Lua a #

Check if at index n there is a convertible Lua value and if so return it. Throws a Exception otherwise.

Instances

Peekable Bool # 

Methods

peek :: StackIndex -> Lua Bool #

Peekable Double # 

Methods

peek :: StackIndex -> Lua Double #

Peekable Float # 

Methods

peek :: StackIndex -> Lua Float #

Peekable Int # 

Methods

peek :: StackIndex -> Lua Int #

Peekable Integer # 

Methods

peek :: StackIndex -> Lua Integer #

Peekable () # 

Methods

peek :: StackIndex -> Lua () #

Peekable ByteString # 
Peekable ByteString # 
Peekable Text # 

Methods

peek :: StackIndex -> Lua Text #

Peekable Number # 

Methods

peek :: StackIndex -> Lua Number #

Peekable Integer # 

Methods

peek :: StackIndex -> Lua Integer #

Peekable CFunction # 
Peekable State # 

Methods

peek :: StackIndex -> Lua State #

Peekable [Char] # 

Methods

peek :: StackIndex -> Lua [Char] #

Peekable a => Peekable [a] # 

Methods

peek :: StackIndex -> Lua [a] #

Peekable (Ptr a) # 

Methods

peek :: StackIndex -> Lua (Ptr a) #

(Ord a, Peekable a) => Peekable (Set a) # 

Methods

peek :: StackIndex -> Lua (Set a) #

Peekable a => Peekable (Optional a) # 

Methods

peek :: StackIndex -> Lua (Optional a) #

(Peekable a, Peekable b) => Peekable (a, b) # 

Methods

peek :: StackIndex -> Lua (a, b) #

(Ord a, Peekable a, Peekable b) => Peekable (Map a b) # 

Methods

peek :: StackIndex -> Lua (Map a b) #

(Peekable a, Peekable b, Peekable c) => Peekable (a, b, c) # 

Methods

peek :: StackIndex -> Lua (a, b, c) #

(Peekable a, Peekable b, Peekable c, Peekable d) => Peekable (a, b, c, d) # 

Methods

peek :: StackIndex -> Lua (a, b, c, d) #

(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e) => Peekable (a, b, c, d, e) # 

Methods

peek :: StackIndex -> Lua (a, b, c, d, e) #

(Peekable a, Peekable b, Peekable c, Peekable d, Peekable e, Peekable f) => Peekable (a, b, c, d, e, f) # 

Methods

peek :: StackIndex -> Lua (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) # 

Methods

peek :: StackIndex -> Lua (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) # 

Methods

peek :: StackIndex -> Lua (a, b, c, d, e, f, g, h) #

class LuaCallFunc a where #

Helper class used to make lua functions useable from haskell

Minimal complete definition

callFunc'

Methods

callFunc' :: String -> Lua () -> NumArgs -> a #

Instances

Peekable a => LuaCallFunc (Lua a) # 

Methods

callFunc' :: String -> Lua () -> NumArgs -> Lua a #

(Pushable a, LuaCallFunc b) => LuaCallFunc (a -> b) # 

Methods

callFunc' :: String -> Lua () -> NumArgs -> a -> b #

class ToHaskellFunction 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.

Minimal complete definition

toHsFun

Methods

toHsFun :: StackIndex -> a -> Lua NumResults #

Helper function, called by toHaskellFunction

type HaskellFunction = Lua NumResults #

Haskell function that can be called from Lua.

class Pushable a where #

A value that can be pushed to the Lua stack.

Minimal complete definition

push

Methods

push :: a -> Lua () #

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

Instances

Pushable Bool # 

Methods

push :: Bool -> Lua () #

Pushable Double # 

Methods

push :: Double -> Lua () #

Pushable Float # 

Methods

push :: Float -> Lua () #

Pushable Int # 

Methods

push :: Int -> Lua () #

Pushable Integer # 

Methods

push :: Integer -> Lua () #

Pushable () # 

Methods

push :: () -> Lua () #

Pushable ByteString # 

Methods

push :: ByteString -> Lua () #

Pushable ByteString # 

Methods

push :: ByteString -> Lua () #

Pushable Text # 

Methods

push :: Text -> Lua () #

Pushable Number # 

Methods

push :: Number -> Lua () #

Pushable Integer # 

Methods

push :: Integer -> Lua () #

Pushable CFunction # 

Methods

push :: CFunction -> Lua () #

Pushable [Char] # 

Methods

push :: [Char] -> Lua () #

Pushable a => Pushable [a] # 

Methods

push :: [a] -> Lua () #

Pushable (Ptr a) # 

Methods

push :: Ptr a -> Lua () #

Pushable a => Pushable (Set a) # 

Methods

push :: Set a -> Lua () #

Pushable a => Pushable (Optional a) # 

Methods

push :: Optional a -> Lua () #

(Pushable a, Pushable b) => Pushable (a, b) # 

Methods

push :: (a, b) -> Lua () #

(Pushable a, Pushable b) => Pushable (Map a b) # 

Methods

push :: Map a b -> Lua () #

(Pushable a, Pushable b, Pushable c) => Pushable (a, b, c) # 

Methods

push :: (a, b, c) -> Lua () #

(Pushable a, Pushable b, Pushable c, Pushable d) => Pushable (a, b, c, d) # 

Methods

push :: (a, b, c, d) -> Lua () #

(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e) => Pushable (a, b, c, d, e) # 

Methods

push :: (a, b, c, d, e) -> Lua () #

(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e, Pushable f) => Pushable (a, b, c, d, e, f) # 

Methods

push :: (a, b, c, d, e, f) -> Lua () #

(Pushable a, Pushable b, Pushable c, Pushable d, Pushable e, Pushable f, Pushable g) => Pushable (a, b, c, d, e, f, g) # 

Methods

push :: (a, b, c, d, e, f, g) -> Lua () #

(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) # 

Methods

push :: (a, b, c, d, e, f, g, h) -> Lua () #

type PreCFunction = State -> IO NumResults #

Type of raw Haskell functions that can be made into CFunctions.

toHaskellFunction :: ToHaskellFunction a => a -> HaskellFunction #

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

  • all arguments are instances of Peekable
  • return type is Lua a, where a is an instance of Pushable

Any Exception will be converted to a string and returned as Lua error.

Important: this does not catch exceptions other than Exception; exception handling must be done by the converted 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)))

callFunc :: LuaCallFunc a => String -> a #

Call a Lua function. Use as:

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

freeCFunction :: CFunction -> Lua () #

Free function pointer created with newcfunction.

newCFunction :: ToHaskellFunction a => a -> Lua CFunction #

Create new foreign Lua function. Function created can be called by Lua engine. Remeber to free the pointer with freecfunction.

pushHaskellFunction :: ToHaskellFunction a => a -> Lua () #

Pushes Haskell function as a callable userdata. All values created will be garbage collected. Use as:

pushHaskellFunction myfun
setglobal "myfun"

Error conditions should be indicated by raising a Lua Exception or by returning the result of error.

registerHaskellFunction :: ToHaskellFunction a => String -> a -> Lua () #

Imports a Haskell function and registers it at global name.