From 293480c28663631fd9058ce2cb77de2a56ec8829 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:09:04 -0400 Subject: [PATCH 1/3] fix(vm): return terse error value for ArgumentError at protected-call boundary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Lua.VM.ProtectedCall.error_value/1` had no clause for `Lua.VM.ArgumentError`, so it fell through to `Exception.message/1` — which calls `ErrorFormatter.format/3` and embeds ANSI escape codes plus the `at ::` header in the returned reason. PR #337 plugged this for the `:value`-bearing exceptions (`RuntimeError`, `TypeError`, `AssertionError`) but `ArgumentError` builds its message from individual fields and has no `:value` slot, so it was missed. Affected both `Lua.call_function/3` and `pcall`/`xpcall`: any stdlib bad-argument raise (e.g. `pairs("asdf")`) returned the rendered string instead of the bare §6.1 error value. Promote `ArgumentError.build_base/5` to a public `raw_message/1` and add a clause to `error_value/1` that returns it. Regression tests added for both protected-call boundaries. Fixes #342. --- lib/lua/vm/argument_error.ex | 18 +++++++++++--- lib/lua/vm/protected_call.ex | 12 ++++++--- test/lua/call_function_error_value_test.exs | 27 +++++++++++++++++++++ test/lua/vm/pcall_error_value_test.exs | 16 ++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/lib/lua/vm/argument_error.ex b/lib/lua/vm/argument_error.ex index bb329575..d68b927e 100644 --- a/lib/lua/vm/argument_error.ex +++ b/lib/lua/vm/argument_error.ex @@ -83,15 +83,27 @@ defmodule Lua.VM.ArgumentError do @impl true def message(%__MODULE__{} = e) do - base = build_base(e.function_name, e.arg_num, e.expected, e.got, e.details) - - Lua.VM.ErrorFormatter.format(:type_error, base, + Lua.VM.ErrorFormatter.format(:type_error, raw_message(e), source: e.source, line: e.line, call_stack: e.call_stack ) end + @doc """ + Returns the unformatted Lua-facing error value — the bare + `"bad argument #N to 'F' (expected, got got)"` string with no location + header, no ANSI, no stack trace. + + This is the §6.1 error value that `pcall`/`xpcall` and + `Lua.call_function/3` hand back at a protected-call boundary. See + `Lua.VM.ProtectedCall.error_value/1`. + """ + @spec raw_message(t()) :: String.t() + def raw_message(%__MODULE__{} = e) do + build_base(e.function_name, e.arg_num, e.expected, e.got, e.details) + end + defp build_base(function_name, arg_num, expected, got, details) do base = if arg_num do diff --git a/lib/lua/vm/protected_call.ex b/lib/lua/vm/protected_call.ex index dbb5681a..25427269 100644 --- a/lib/lua/vm/protected_call.ex +++ b/lib/lua/vm/protected_call.ex @@ -9,10 +9,16 @@ defmodule Lua.VM.ProtectedCall do # 1. `error()`'s §6.1-prefixed string view (`RuntimeError.lua_value`). # 2. Raw passthrough on KEY PRESENCE — `value: nil` (from `error()`) and # `value: false` must match here so the boundary returns nil/false like - # PUC-Lua, never an `is_nil`-guarded fallthrough to clause 3. - # 3. `ArgumentError` (no `:value` field) and plain Elixir exceptions keep - # their message string. + # PUC-Lua, never an `is_nil`-guarded fallthrough to the next clause. + # 3. `ArgumentError` builds its message from individual fields and has no + # `:value` slot, so it gets its own clause that returns the raw + # `"bad argument #N to 'F' ..."` string — never the terminal-rendered + # `Exception.message/1` (ANSI + location header). + # 4. Plain Elixir exceptions keep their message string as a last resort. + alias Lua.VM.ArgumentError + def error_value(%{lua_value: lv}) when not is_nil(lv), do: lv def error_value(%{value: value}), do: value + def error_value(%ArgumentError{} = e), do: ArgumentError.raw_message(e) def error_value(e), do: Exception.message(e) end diff --git a/test/lua/call_function_error_value_test.exs b/test/lua/call_function_error_value_test.exs index 19e3ccd0..792e1be8 100644 --- a/test/lua/call_function_error_value_test.exs +++ b/test/lua/call_function_error_value_test.exs @@ -54,6 +54,20 @@ defmodule Lua.CallFunctionErrorValueTest do refute reason =~ "\e[" refute reason =~ "\n" end + + test "a stdlib bad-argument ArgumentError stays a terse one-line string" do + {ref, lua} = fun!(~S|function() for i in pairs("asdf") do end end|) + + assert {:error, reason, %Lua{}} = Lua.call_function(lua, ref, []) + + assert is_binary(reason) + assert reason =~ "bad argument #1 to 'pairs'" + assert reason =~ "table expected" + assert reason =~ "got string" + refute reason =~ "\e[" + refute reason =~ "at -no-source-" + refute reason =~ "\n" + end end describe "call_function/3 passes non-string error objects through (pcall parity)" do @@ -96,6 +110,19 @@ defmodule Lua.CallFunctionErrorValueTest do refute reason =~ "\e[" end + + test "ArgumentError reason has no escape codes when ANSI is enabled" do + previous = Application.get_env(:elixir, :ansi_enabled) + Application.put_env(:elixir, :ansi_enabled, true) + on_exit(fn -> Application.put_env(:elixir, :ansi_enabled, previous) end) + + assert IO.ANSI.enabled?() + + {ref, lua} = fun!(~S|function() pairs("asdf") end|) + assert {:error, reason, %Lua{}} = Lua.call_function(lua, ref, []) + + refute reason =~ "\e[" + end end describe "call_function!/3 keeps the rich render" do diff --git a/test/lua/vm/pcall_error_value_test.exs b/test/lua/vm/pcall_error_value_test.exs index a6677ed0..29f2ea88 100644 --- a/test/lua/vm/pcall_error_value_test.exs +++ b/test/lua/vm/pcall_error_value_test.exs @@ -148,6 +148,22 @@ defmodule Lua.VM.PcallErrorValueTest do assert results == [false, true] end + + test "stdlib bad-argument error returns the terse raw string" do + {results, _state} = + run( + """ + local ok, err = pcall(function() + pairs("asdf") + end) + return ok, type(err), err + """, + @engine + ) + + assert [false, "string", err] = results + assert err == "bad argument #1 to 'pairs' (table expected, got string)" + end end describe "error() position prefix on string messages (#{engine} engine)" do From 1e0ad232847a2111abe1a58c92681c8b4f90186e Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:14:31 -0400 Subject: [PATCH 2/3] docs(vm): drop autolink to hidden ProtectedCall.error_value/1 `Lua.VM.ProtectedCall` is `@moduledoc false`, so the autolink from `ArgumentError.raw_message/1`'s public doc tripped `mix docs --warnings-as-errors` on CI. --- lib/lua/vm/argument_error.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/lua/vm/argument_error.ex b/lib/lua/vm/argument_error.ex index d68b927e..3325073e 100644 --- a/lib/lua/vm/argument_error.ex +++ b/lib/lua/vm/argument_error.ex @@ -96,8 +96,7 @@ defmodule Lua.VM.ArgumentError do header, no ANSI, no stack trace. This is the §6.1 error value that `pcall`/`xpcall` and - `Lua.call_function/3` hand back at a protected-call boundary. See - `Lua.VM.ProtectedCall.error_value/1`. + `Lua.call_function/3` hand back at a protected-call boundary. """ @spec raw_message(t()) :: String.t() def raw_message(%__MODULE__{} = e) do From af123e0f7fd5ec4fbed7e11b39ec49177d41b35e Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:18:20 -0400 Subject: [PATCH 3/3] test(api): lock call_function!/3's structured-exception passthrough MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Lua.call_function!/3` raises `Lua.RuntimeException` whose `:original` field carries the underlying VM exception verbatim. That's the escape hatch for Elixir callers who want to pattern-match on the structured error (e.g. `%Lua.VM.ArgumentError{function_name: "pairs", arg_num: 1, expected: "table", got: "string"}`) instead of parsing the terse §6.1 string that `call_function/3` returns. The contract was already implemented (RuntimeException.exception/1 line 66 stores `original: error`) but wasn't pinned by a test, so a future refactor that wrapped or stringified the underlying exception would have silently regressed it. --- test/lua/call_function_error_value_test.exs | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/lua/call_function_error_value_test.exs b/test/lua/call_function_error_value_test.exs index 792e1be8..bb763f7a 100644 --- a/test/lua/call_function_error_value_test.exs +++ b/test/lua/call_function_error_value_test.exs @@ -142,4 +142,44 @@ defmodule Lua.CallFunctionErrorValueTest do assert message =~ "runtime error:" end end + + describe "call_function!/3 preserves the original VM exception via :original" do + test "ArgumentError passes through with all structured fields" do + {ref, lua} = fun!(~S|function() pairs("asdf") end|) + + error = + assert_raise Lua.RuntimeException, fn -> + Lua.call_function!(lua, ref, []) + end + + assert %Lua.VM.ArgumentError{ + function_name: "pairs", + arg_num: 1, + expected: "table", + got: "string" + } = error.original + end + + test "RuntimeError from error() passes through with its Lua value" do + {ref, lua} = fun!("function() error('boom') end") + + error = + assert_raise Lua.RuntimeException, fn -> + Lua.call_function!(lua, ref, []) + end + + assert %Lua.VM.RuntimeError{value: "boom"} = error.original + end + + test "TypeError passes through with error_kind for programmatic dispatch" do + {ref, lua} = fun!("function() local t = nil; return t.x end") + + error = + assert_raise Lua.RuntimeException, fn -> + Lua.call_function!(lua, ref, []) + end + + assert %Lua.VM.TypeError{error_kind: :index_non_table} = error.original + end + end end