package ppx_ezlua

All checked builds pass
  1. Overview
  2. Docs
PPX deriving for Lua (ezlua)

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ezlua-0.1.tbz
sha256=068f8c0b85e191a3ee4dc7542107b12469a4f28b059df2998a6ec433da8417cc
sha512=3415748b360863c48519ded9506b6d58e70cc84936b33964bb33e336588b30fb0e1899f2f0789d142c3bbc153b834005af1c42dc6d59c03b3e1efd91427b7aa5

Description

Added to opam-repository:

README

ezlua build

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

int

number (integer)

float

number

string

string

bool

boolean

unit

nil

'a list / 'a array

table {v1, v2, ...} (1-indexed)

'a option

nil for None; {v} (single-element table) for Some v

'a * 'b (pair)

table {a, b} via Encode.pair/Decode.pair

N-tuple ('a * 'b * 'c * ...)

table {v1, v2, ..., vN} (1-indexed, inline)

record

table keyed by field name, e.g. {x=1.0, y=2.5}

variant (no payload)

{tag="Ctor"}

variant (single payload)

{tag="Ctor", value=v}

variant (tuple payload)

{tag="Ctor", value={v1, v2, ...}}

Pairs use the Encode.pair/Decode.pair library functions. Larger tuples generate inline table code directly.

Dependencies (4)

  1. ezlua
  2. ppxlib >= "0.36.0"
  3. dune >= "3.0"
  4. ocaml >= "4.14"

Dev Dependencies (4)

  1. odoc with-doc
  2. alcotest with-test
  3. ppx_deriving with-test
  4. base-unix with-test

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building ppx_ezlua with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.1 (this version) n/a good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure