package ppx_ezlua
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=068f8c0b85e191a3ee4dc7542107b12469a4f28b059df2998a6ec433da8417cc
sha512=3415748b360863c48519ded9506b6d58e70cc84936b33964bb33e336588b30fb0e1899f2f0789d142c3bbc153b834005af1c42dc6d59c03b3e1efd91427b7aa5
Description
Added to opam-repository:
README
ezlua 
Trying to make it easier to use Lua from OCaml.
ppx
ppx_ezlua provides two features: automatic encoder/decoder derivation for custom types, and a let%lua syntax for wrapping OCaml functions as Lua callbacks.
[@@deriving ezlua]
Annotate a type declaration to generate <name>_to_lua and <name>_of_lua functions automatically.
Records become Lua tables keyed by field name. Variants become tables with a "tag" field (and an optional "value" or "values" field for payloads).
type point = {
x: float;
y: float;
}
[@@deriving ezlua]
(* generates:
val point_to_lua : point Ezlua.to_lua
val point_of_lua : point Ezlua.of_lua *)
let () =
let state = Ezlua.create () in
let p = { x = 1.0; y = 2.5 } in
Ezlua.set_global state "p" point_to_lua p;
ignore (Ezlua.run state "assert(p.x == 1.0 and p.y == 2.5)")let%lua
Define an OCaml function and get a matching <name>_lua callback suitable for Ezlua.add_function. All parameters and the return type must be annotated.
let%lua add (x : int) (y : int) : int = x + y
(* generates:
val add : int -> int -> int
val add_lua : Lua_api_lib.oCamlFunction *)
let () =
let state = Ezlua.create () in
Ezlua.add_function state "add" add_lua;
ignore (Ezlua.run state "assert(add(1, 2) == 3)")Argument decoding errors are reported back to Lua via LuaL.error with a message that includes the argument position and name.
Encoding conventions
This table describes how OCaml values map to Lua values.
OCaml type | Lua representation |
|---|---|
| number (integer) |
| number |
| string |
| boolean |
|
|
| table |
|
|
| table |
N-tuple ( | table |
record | table keyed by field name, e.g. |
variant (no payload) |
|
variant (single payload) |
|
variant (tuple payload) |
|
Pairs use the Encode.pair/Decode.pair library functions. Larger tuples generate inline table code directly.