From 4787c7c2554f4a1010fb682bd810494b99d37956 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Tue, 22 Oct 2024 18:51:26 -0400 Subject: [PATCH] Add a test for external functions with callbacks This tracks https://github.com/rvirding/luerl/pull/191 which fixes a decoder bug causing state issues when mixing Elixir and Lua functions. --- mix.exs | 2 +- test/lua_test.exs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index c309b79..9c48328 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/lua_test.exs b/test/lua_test.exs index 84b0fb5..758f55c 100644 --- a/test/lua_test.exs +++ b/test/lua_test.exs @@ -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