HsLua.ObjectOrientation

hslua-objectorientation-2.3.1: Object orientation tools for HsLua

Copyright | © 2021-2024 Albert Krewinkel
License | MIT
Maintainer | Albert Krewinkel <tarleb@hslua.org>
Safe Haskell | None
Language | Haskell2010

HsLua.ObjectOrientation

Contents

- Defining types
  - Methods
  - Properties
  - Aliases
- Marshaling
- Type docs
- Helper types for building

Description

This module provides types and functions to use Haskell values as userdata objects in Lua. These objects wrap a Haskell value and provide methods and properties to interact with the Haskell value.

The terminology in this module refers to the userdata values as /UD objects, and to their type as UD type/.

Synopsis

- type UDType e fn a = UDTypeWithList e fn a Void
- data UDTypeWithList e fn a itemtype = UDTypeWithList {
  - udName :: Name
  - udOperations :: [(Operation, fn)]
  - udProperties :: Map Name (Property e a)
  - udMethods :: Map Name fn
  - udAliases :: Map AliasIndex Alias
  - udListSpec :: Maybe (ListSpec e a itemtype)
  - udFnPusher :: fn -> LuaE e ()
  }
- deftypeGeneric :: Pusher e fn -> Name -> [(Operation, fn)] -> [Member e fn a] -> UDType e fn a
- deftypeGeneric' :: Pusher e fn -> Name -> [(Operation, fn)] -> [Member e fn a] -> Maybe (ListSpec e a itemtype) -> UDTypeWithList e fn a itemtype
- methodGeneric :: Name -> fn -> Member e fn a
- property :: LuaError e => Name -> Text -> (Pusher e b, a -> b) -> (Peeker e b, a -> b -> a) -> Member e fn a
- property' :: LuaError e => Name -> TypeSpec -> Text -> (Pusher e b, a -> b) -> (Peeker e b, a -> b -> a) -> Member e fn a
- possibleProperty :: LuaError e => Name -> Text -> (Pusher e b, a -> Possible b) -> (Peeker e b, a -> b -> Possible a) -> Member e fn a
- possibleProperty' :: LuaError e => Name -> TypeSpec -> Text -> (Pusher e b, a -> Possible b) -> (Peeker e b, a -> b -> Possible a) -> Member e fn a
- readonly :: Name -> Text -> (Pusher e b, a -> b) -> Member e fn a
- readonly' :: Name -> TypeSpec -> Text -> (Pusher e b, a -> b) -> Member e fn a
- alias :: AliasIndex -> Text -> [AliasIndex] -> Member e fn a
- peekUDGeneric :: LuaError e => UDTypeWithList e fn a itemtype -> Peeker e a
- pushUDGeneric :: LuaError e => (UDTypeWithList e fn a itemtype -> LuaE e ()) -> UDTypeWithList e fn a itemtype -> a -> LuaE e ()
- initTypeGeneric :: LuaError e => (UDTypeWithList e fn a itemtype -> LuaE e ()) -> UDTypeWithList e fn a itemtype -> LuaE e Name
- udDocs :: UDTypeWithList e fn a itemtype -> TypeDocs
- udTypeSpec :: UDTypeWithList e fn a itemtype -> TypeSpec
- data Member e fn a
- data Property e a = Property {
  - propertyGet :: a -> LuaE e NumResults
  - propertySet :: Maybe (StackIndex -> a -> LuaE e a)
  - propertyDescription :: Text
  - propertyType :: TypeSpec
  }
- data Operation
  - = Add
  - | Sub
  - | Mul
  - | Div
  - | Mod
  - | Pow
  - | Unm
  - | Idiv
  - | Band
  - | Bor
  - | Bxor
  - | Bnot
  - | Shl
  - | Shr
  - | Concat
  - | Len
  - | Eq
  - | Lt
  - | Le
  - | Index
  - | Newindex
  - | Call
  - | Tostring
  - | Pairs
  - | CustomOperation Name
- type ListSpec e a itemtype = ((Pusher e itemtype, a -> [itemtype]), (Peeker e itemtype, a -> [itemtype] -> a))
- data Possible a
  - = Actual a
  - | Absent
- type Alias = [AliasIndex]
- data AliasIndex
  - = StringIndex Name
  - | IntegerIndex Integer

# Documentation

type UDType e fn a = UDTypeWithList e fn a Void #

A userdata type, capturing the behavior of Lua objects that wrap Haskell values. The type name must be unique; once the type has been used to push or retrieve a value, the behavior can no longer be modified through this type.

data UDTypeWithList e fn a itemtype #

A userdata type, capturing the behavior of Lua objects that wrap Haskell values. The type name must be unique; once the type has been used to push or retrieve a value, the behavior can no longer be modified through this type.

This type includes methods to define how the object should behave as a read-only list of type `itemtype`.

Constructors

UDTypeWithList |
Fields udName :: Name udOperations :: [(Operation, fn)] udProperties :: Map Name (Property e a) udMethods :: Map Name fn udAliases :: Map AliasIndex Alias udListSpec :: Maybe (ListSpec e a itemtype) udFnPusher :: fn -> LuaE e ()

# Defining types

deftypeGeneric #

Arguments

:: Pusher e fn | function pusher
-> Name | type name
-> [(Operation, fn)] | operations
-> [Member e fn a] | methods
-> UDType e fn a |

Defines a new type, defining the behavior of objects in Lua. Note that the type name must be unique.

deftypeGeneric' #

Arguments

:: Pusher e fn | function pusher
-> Name | type name
-> [(Operation, fn)] | operations
-> [Member e fn a] | methods
-> Maybe (ListSpec e a itemtype) | list access
-> UDTypeWithList e fn a itemtype |

Defines a new type that could also be treated as a list; defines the behavior of objects in Lua. Note that the type name must be unique.

## Methods

methodGeneric :: Name -> fn -> Member e fn a #

Use a documented function as an object method.

## Properties

property #

Arguments

:: LuaError e |
=> Name | property name
-> Text | property description
-> (Pusher e b, a -> b) | how to get the property value
-> (Peeker e b, a -> b -> a) | how to set a new property value
-> Member e fn a |

Declares a new read- and writable property.

property' #

Arguments

:: LuaError e |
=> Name | property name
-> TypeSpec | property type
-> Text | property description
-> (Pusher e b, a -> b) | how to get the property value
-> (Peeker e b, a -> b -> a) | how to set a new property value
-> Member e fn a |

Declares a new read- and writable typed property.

possibleProperty #

Arguments

:: LuaError e |
=> Name | property name
-> Text | property description
-> (Pusher e b, a -> Possible b) | how to get the property value
-> (Peeker e b, a -> b -> Possible a) | how to set a new property value
-> Member e fn a |

Declares a new read- and writable property which is not always available.

possibleProperty' #

Arguments

:: LuaError e |
=> Name | property name
-> TypeSpec | type of the property value
-> Text | property description
-> (Pusher e b, a -> Possible b) | how to get the property value
-> (Peeker e b, a -> b -> Possible a) | how to set a new property value
-> Member e fn a |

Declares a new read- and writable property which is not always available.

readonly #

Arguments

:: Name | property name
-> Text | property description
-> (Pusher e b, a -> b) | how to get the property value
-> Member e fn a |

Creates a read-only object property. Attempts to set the value will cause an error.

readonly' #

Arguments

:: Name | property name
-> TypeSpec | property type
-> Text | property description
-> (Pusher e b, a -> b) | how to get the property value
-> Member e fn a |

Creates a read-only object property. Attempts to set the value will cause an error.

## Aliases

alias #

Arguments

:: AliasIndex | property alias
-> Text | description
-> [AliasIndex] | sequence of nested properties
-> Member e fn a |

Define an alias for another, possibly nested, property.

# Marshaling

peekUDGeneric :: LuaError e => UDTypeWithList e fn a itemtype -> Peeker e a #

Retrieves a userdata value of the given type.

pushUDGeneric #

Arguments

:: LuaError e |
=> (UDTypeWithList e fn a itemtype -> LuaE e ()) | push docs
-> UDTypeWithList e fn a itemtype | userdata type
-> a | value to push
-> LuaE e () |

Pushes a userdata value of the given type.

initTypeGeneric :: LuaError e => (UDTypeWithList e fn a itemtype -> LuaE e ()) -> UDTypeWithList e fn a itemtype -> LuaE e Name #

Ensures that the type has been fully initialized, i.e., that all metatables have been created and stored in the registry. Returns the name of the initialized type.

The `hook` can be used to perform additional setup operations. The function is called as the last step after the type metatable has been initialized: the fully initialized metatable will be at the top of the stack at that point. Note that the hook will not be called if the type's metatable already existed before this function was invoked.

# Type docs

udDocs :: UDTypeWithList e fn a itemtype -> TypeDocs #

Returns documentation for this type.

udTypeSpec :: UDTypeWithList e fn a itemtype -> TypeSpec #

Type specifier for a UDType

# Helper types for building

data Member e fn a #

A type member, either a method or a variable.

data Property e a #

A read- and writable property on a UD object.

Constructors

Property |
Fields propertyGet :: a -> LuaE e NumResults propertySet :: Maybe (StackIndex -> a -> LuaE e a) propertyDescription :: Text propertyType :: TypeSpec

data Operation #

Lua metadata operation types.

Constructors

Add | the addition (`+`) operation. If any operand for an addition is not a number (nor a string coercible to a number), Lua will try to call a metamethod. First, Lua will check the first operand (even if it is valid). If that operand does not define a metamethod for `__add`, then Lua will check the second operand. If Lua can find a metamethod, it calls the metamethod with the two operands as arguments, and the result of the call (adjusted to one value) is the result of the operation. Otherwise, it raises an error.
Sub | the subtraction (`-`) operation. Behavior similar to the addition operation.
Mul | the multiplication (`*`) operation. Behavior similar to the addition operation.
Div | the division (`/`) operation. Behavior similar to the addition operation.
Mod | the modulo (`%`) operation. Behavior similar to the addition operation.
Pow | the exponentiation (`^`) operation. Behavior similar to the addition operation.
Unm | the negation (unary `-`) operation. Behavior similar to the addition operation.
Idiv | the floor division (`//`) operation. Behavior similar to the addition operation.
Band | the bitwise AND (`&`) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither an integer nor a value coercible to an integer (see §3.4.3).
Bor | the bitwise OR (`|`) operation. Behavior similar to the bitwise AND operation.
Bxor | the bitwise exclusive OR (binary `~`) operation. Behavior similar to the bitwise AND operation.
Bnot | the bitwise NOT (unary `~`) operation. Behavior similar to the bitwise AND operation.
Shl | the bitwise left shift (`<<`) operation. Behavior similar to the bitwise AND operation.
Shr | the bitwise right shift (`>>`) operation. Behavior similar to the bitwise AND operation.
Concat | the concatenation (`..`) operation. Behavior similar to the addition operation, except that Lua will try a metamethod if any operand is neither a string nor a number (which is always coercible to a string).
Len | the length (`#`) operation. If the object is not a string, Lua will try its metamethod. If there is a metamethod, Lua calls it with the object as argument, and the result of the call (always adjusted to one value) is the result of the operation. If there is no metamethod but the object is a table, then Lua uses the table length operation (see §3.4.7). Otherwise, Lua raises an error.
Eq | the equal (`==`) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are either both tables or both full userdata and they are not primitively equal. The result of the call is always converted to a boolean.
Lt | the less than (`<`) operation. Behavior similar to the addition operation, except that Lua will try a metamethod only when the values being compared are neither both numbers nor both strings. The result of the call is always converted to a boolean.
Le | the less equal (`<=`) operation. Unlike other operations, the less-equal operation can use two different events. First, Lua looks for the `__le` metamethod in both operands, like in the less than operation. If it cannot find such a metamethod, then it will try the `__lt` metamethod, assuming that a <= b is equivalent to not (b < a). As with the other comparison operators, the result is always a boolean. (This use of the `__lt` event can be removed in future versions; it is also slower than a real __le metamethod.)
Index | The indexing access operation `table[key]`. This event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.
Newindex | The indexing assignment `table[key] = value`. Like the index event, this event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.
Call | The call operation `func(args)`. This event happens when Lua tries to call a non-function value (that is, func is not a function). The metamethod is looked up in func. If present, the metamethod is called with func as its first argument, followed by the arguments of the original call (args). All results of the call are the result of the operation. (This is the only metamethod that allows multiple results.)
Tostring | The operation used to create a string representation of the object.
Pairs | the operation of iterating over the object's key-value pairs.
CustomOperation Name | a custom operation, with the metamethod name as parameter.

#### Instances

Instances details

Show Operation # |
Instance details Defined in HsLua.ObjectOrientation.Operation Methods showsPrec :: Int -> Operation -> ShowS # show :: Operation -> String # showList :: [Operation] -> ShowS #
Eq Operation # |
Instance details Defined in HsLua.ObjectOrientation.Operation Methods (==) :: Operation -> Operation -> Bool # (/=) :: Operation -> Operation -> Bool #
Ord Operation # |
Instance details Defined in HsLua.ObjectOrientation.Operation Methods compare :: Operation -> Operation -> Ordering # (<) :: Operation -> Operation -> Bool # (<=) :: Operation -> Operation -> Bool # (>) :: Operation -> Operation -> Bool # (>=) :: Operation -> Operation -> Bool # max :: Operation -> Operation -> Operation # min :: Operation -> Operation -> Operation #

type ListSpec e a itemtype = ((Pusher e itemtype, a -> [itemtype]), (Peeker e itemtype, a -> [itemtype] -> a)) #

Pair of pairs, describing how a type can be used as a Lua list. The first pair describes how to push the list items, and how the list is extracted from the type; the second pair contains a method to retrieve list items, and defines how the list is used to create an updated value.

data Possible a #

A property or method which may be available in some instances but not in others.

Constructors

Actual a |
Absent |

type Alias = [AliasIndex] #

Alias for a different property of this or of a nested object.

data AliasIndex #

Index types allowed in aliases (strings and integers)

Constructors

StringIndex Name |
IntegerIndex Integer |

#### Instances

Instances details

IsString AliasIndex # |
Instance details Defined in HsLua.ObjectOrientation Methods fromString :: String -> AliasIndex #
Eq AliasIndex # |
Instance details Defined in HsLua.ObjectOrientation Methods (==) :: AliasIndex -> AliasIndex -> Bool # (/=) :: AliasIndex -> AliasIndex -> Bool #
Ord AliasIndex # |
Instance details Defined in HsLua.ObjectOrientation Methods compare :: AliasIndex -> AliasIndex -> Ordering # (<) :: AliasIndex -> AliasIndex -> Bool # (<=) :: AliasIndex -> AliasIndex -> Bool # (>) :: AliasIndex -> AliasIndex -> Bool # (>=) :: AliasIndex -> AliasIndex -> Bool # max :: AliasIndex -> AliasIndex -> AliasIndex # min :: AliasIndex -> AliasIndex -> AliasIndex #