Skip to content

Commit

Permalink
Add a test for external functions with callbacks
Browse files Browse the repository at this point in the history
This tracks rvirding/luerl#191 which fixes a
decoder bug causing state issues when mixing Elixir and Lua functions.
  • Loading branch information
davydog187 committed Oct 22, 2024
1 parent 5423eba commit 4787c7c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Lua.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:luerl, "~> 1.2"},
{:luerl, path: "../../oss/luerl"},
{:ex_doc, "~> 0.31", only: :dev, runtime: false}
]
end
Expand Down
30 changes: 30 additions & 0 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,36 @@ defmodule LuaTest do
assert lua |> Lua.decode!(table) |> Lua.Table.as_map() == my_table
end

test "it can register functions that take callbacks that modify state" do
require Logger

lua = ~LUA"""
state = {}
function assignFoo()
state["foo"] = "bar"
end
function assignBar()
state["bar"] = "foo"
end
assignBar()
run(assignFoo)
return state
"""

assert {[ret], _lua} =
Lua.new()
|> Lua.set!([:run], fn [callback], lua ->
Lua.call_function!(lua, callback, [])
end)
|> Lua.eval!(lua)

assert Lua.Table.as_map(ret) == %{"foo" => "bar", "bar" => "foo"}
end

test "it can evaluate chunks" do
assert %Lua.Chunk{} = chunk = ~LUA[return 2 + 2]c

Expand Down

0 comments on commit 4787c7c

Please sign in to comment.