Chapter IV · Lua.ex integration / Lesson 19 of 27 Host integration

Embedding Lua in Elixir

You'll learn: Initialize a sandboxed VM and evaluate a Lua snippet from your Elixir app.

Lua.new() returns a sandboxed VM. Lua.eval!/2 runs a snippet and returns {results, lua} — the updated VM threads through, so every call yields a state you keep using. Multiple return values from Lua come back as an Elixir list.

Elixir · your app
Reference only
lua = Lua.new()

{results, lua} = Lua.eval!(lua, """
  return 1 + 2, "hello"
""")

results
# => [3, "hello"]
host-intro.lua
Output idle
Hit Run to execute.
Try it: Change the snippet to return three values, including a table { ok = true }. The Elixir caller gets a 3-element list with the table at the end.