Chapter IV ·
Lua.ex integration
/
Lesson 22 of 27
Host integration
Calling Lua from Elixir
You'll learn: Define a Lua function in a snippet, then invoke it from Elixir with
call_function!.
Lua.call_function!(lua, [:name], [arg1, arg2]) calls a Lua
function from Elixir. Path tuples work too — [:string,
:upper] reaches stdlib. This is the pattern when your script
is *configuration*: it defines hooks (on_request, pricing,
…) and your Elixir code drives the loop.
Elixir · your app
Reference only
{_, lua} = Lua.eval!(Lua.new(), """
function greet(name) return "hi, " .. name end
""")
Lua.call_function!(lua, [:greet], ["Ada"])
# => ["hi, Ada"]
# Stdlib paths work too:
Lua.call_function!(lua, [:string, :upper], ["heya"])
# => ["HEYA"]
Output
idle
Hit Run to execute.
Try it:
Define a second function
farewell(name) that returns "bye, " .. name. The host could now drive a request/response cycle through two hooks defined in one script.