Repository navigation
fix(api): deflua/2 preserves function name for guarded heads - #344
Conversation
For a guarded head like `deflua clamp(a) when is_integer(a) do ... end`,
the AST passed to deflua/2 is `{:when, _, [{:clamp, _, _} | _]}`. The
single binding `{name, _, _} = fa` matched the outer tuple and bound
`name` to `:when`, so the function was registered in `@lua_function`
(and `__lua_functions__/0`) as `:when`, making it unreachable from Lua.
Use the same case-based extraction deflua/3 already uses to unwrap the
`:when` node and reach the real function name, falling back to the plain
head for non-guarded definitions. The existing "deflua functions can
have guards" test only asserted the generated Elixir function worked and
never checked the Lua registration name, which is why this slipped
through; a new test asserts `__lua_functions__/0` contains the real name
(not `:when`) and that the guarded function is callable from Lua.
Closes #343
🤖 Ultracode workflow reviewRan a 3-lens adversarial review (correctness · test-quality · regression-risk) over the diff. Overall: approve-with-nits. Findingsnit · test-quality — Optional: drop the What was verified
No blocking issues. |
Bug
deflua/2(a guarded head with no state arg) registered the function under the name:wheninstead of its real name, making it unreachable from Lua. For example:would register
clampas:whenin__lua_functions__/0, so callingclamp(...)from a Lua chunk raised an undefined-function error.Root cause
In
lib/lua/api.ex, the 2-aritydefluamacro extracted the name with a single binding:For a guarded head the AST is
{:when, _, [{name, _, _} | _]}, so the match against the outer tuple boundnameto:when.deflua/3(the state-arg variant) already handled this correctly with acase, which is why guarded heads with a state arg were never affected.Fix
Use the same case-based extraction
deflua/3already uses to unwrap the:whennode and reach the real function name, falling back to the plain head for non-guarded definitions. Name-only change; thedef unquote(fa)body and@variadichandling are untouched.Regression test
The existing "deflua functions can have guards" test only asserted the generated Elixir function worked (
module.has_a_guard(1) == 1) and never checked the Lua registration name, which is exactly why the bug slipped through. A new test "guarded deflua heads register under their real name, not :when" asserts:__lua_functions__/0contains the real names (:has_a_guard,:with_state) and not:whenLua.load_api+Lua.eval!(return has_a_guard(1))Confirmed the new test fails against the unfixed macro (registers
:when) and passes with the fix.Validation
mix test test/lua/api_test.exs— 15 passedmix test(full suite) — 2247 passed, 19 skipped, 1 excludedmix format --check-formatted— cleanCloses #343