Skip to content

Commit

Permalink
Fix issue with variadic functions that have state (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored May 17, 2024
1 parent 8da1833 commit 065a0ee
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ defmodule Lua do

if with_state? do
fn args, state ->
execute_function(module, function_name, args ++ [wrap(state)])
execute_function(module, function_name, [args, wrap(state)])
end
else
fn args ->
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Lua.MixProject do
use Mix.Project

@url "https://github.com/tv-labs/lua"
@version "0.0.13"
@version "0.0.14"

def project do
[
Expand Down
20 changes: 20 additions & 0 deletions test/lua/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ defmodule Lua.APITest do
]
end

test "variadic functions can have state" do
assert [{module, _}] =
Code.compile_string("""
defmodule VariadicWithState do
use Lua.API
@variadic true
deflua foo(args), state do
{args, state}
end
end
""")

assert module.foo(["a", "b", "c"], :state) == {["a", "b", "c"], :state}

assert module.__lua_functions__() == [
{:foo, true, true}
]
end

test "if there is mixed usage of state for a function, it raises an error" do
error = "Five.foo() is inconsistently using state. Please make all clauses consistent"

Expand Down
10 changes: 10 additions & 0 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ defmodule LuaTest do
deflua test(a, b \\ "default") do
"#{a} #{b}"
end

@variadic true
deflua with_state(args), state do
{args, state}
end
end

setup do
Expand All @@ -565,6 +570,11 @@ defmodule LuaTest do
assert {["a-b-c"], _} = Lua.eval!(lua, "return scope.bar('a', 'b', 'c')")
end

test "inject a variadic function with state", %{lua: lua} do
lua = Lua.load_api(lua, TestModule, ["scope"])
assert {["a", "b", "c"], _} = Lua.eval!(lua, "return scope.with_state('a', 'b', 'c')")
end

test "injects Elixir functions that have multiple arities", %{lua: lua} do
lua = Lua.load_api(lua, TestModule, ["scope"])

Expand Down

0 comments on commit 065a0ee

Please sign in to comment.