Powered by AppSignal & Oban Pro

Quickstart

guides/examples/quickstart.livemd

Quickstart

Mix.install([
  {:lua, "~> 1.0.0-rc.0"}
])

Evaluating a script

Lua.new/0 builds a fresh, sandboxed VM, and Lua.eval!/2 runs a script against it. eval!/1 is a shorthand that spins up a throwaway VM for one-off scripts. It returns {results, lua}, where results is the list of values the script returned, already decoded into Elixir terms.

{[answer], _lua} = Lua.eval!("return 6 * 7")
answer
42

Returning multiple values

A script can return multiple values; they come back as a list in order.

{[sum, product], _lua} =
  Lua.eval!("""
  local a, b = 3, 4
  return a + b, a * b
  """)

{sum, product}
{7, 12}

Decoding tables

Tables decode into Elixir key/value pairs. A Lua array is a table keyed by 1..n, so it decodes into a list of {index, value} tuples.

{[table], _lua} = Lua.eval!("return {10, 20, 30}")
table
[{1, 10}, {2, 20}, {3, 30}]