From 293480c28663631fd9058ce2cb77de2a56ec8829 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:09:04 -0400 Subject: [PATCH 1/8] 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/8] 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/8] 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 From 38aff591d6fed60d731e8875651bc5827c01c8a0 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:49:06 -0400 Subject: [PATCH 4/8] feat(vm): plumb per-call source lines through the compiled dispatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dispatcher's call opcodes now carry the source line of their call site, baked in at encode time. `dispatcher_call_function/6` publishes that line via `current_position/0` so raise sites inside stdlib (e.g. `error()`'s §6.1 prefix, `pairs("asdf")`'s bad-argument check) attribute to the right line instead of suppressing the prefix. Previously the encoder stripped `:source_line` opcodes to avoid one no-op dispatch per source line (~228k extra cycles for fib(25)) and the dispatcher published `{nil, source}` at native-call boundaries to avoid mis-attribution. This was the deferred "B5d-v2" work referenced by both the bytecode-encoder comment and a relaxed `:compiled` assertion in `pcall_error_value_test.exs`. The fix: - Encoder tracks `current_line` while walking instructions; `:source_line` opcodes update it but stay stripped. `annotate_line/2` appends the line as a final slot to `:call_one`/`:call_zero`/`:call_multi` opcode tuples only. Other opcodes pass through unchanged. - Dispatcher's three call-opcode patterns gain a `line` slot and pass it to `Executor.dispatcher_call_function/6`. - Executor publishes `{line, source}` at the native-call boundary and uses the same line for the nil-call / non-function-call type errors. - Nested bodies (inside `:test`, loops, generic_for) work because the encoder seeds a fresh `current_line` per nested encode and the nested bodies carry their own `:source_line` opcodes, so a call inside an `if`/`for` body still gets its own line baked in. Tests: - Flipped the `:compiled` branches in `pcall_error_value_test.exs` and `pcall_state_preservation_test.exs` that previously asserted suppression — both engines now match `^test\.lua:\d+: boom$`. - Added regressions in `call_function_error_value_test.exs` and `bytecode_test.exs` pinning that calls in both outer and nested positions carry the right line. - Updated the `pcall(function() error("nope") end)` doctest in README to reflect the §6.1 prefix that now flows through the compiled engine. Benchmarks (manual, Apple M1 Pro): - fib(30) main → 0.63 ips · branch → 0.73 ips (no regression) - closures main → 1.37 K ips · branch → 1.40 K ips (no regression) Hot path is unchanged: per-instruction lookup is gone, and call opcodes gain one tuple slot read at the same dispatch step they already did. Follow-up: line attribution for non-call dispatcher raise sites (binops, indexing, concat) is still deferred — file as "B5d-v3" if needed. --- README.md | 4 +- lib/lua/compiler/bytecode.ex | 65 +++++++++++------ lib/lua/vm/dispatcher.ex | 12 +-- lib/lua/vm/executor.ex | 41 ++++++----- test/lua/call_function_error_value_test.exs | 43 +++++++++++ test/lua/compiler/bytecode_test.exs | 73 +++++++++++++++++++ test/lua/vm/pcall_error_value_test.exs | 18 ++--- test/lua/vm/pcall_state_preservation_test.exs | 10 +-- 8 files changed, 199 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 7d1e97c5..42477faf 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ end Lua-level error handling works too — `pcall` catches the error and returns it as a value: - iex> {[false, "nope"], _lua} = Lua.eval!(~S[return pcall(function() error("nope") end)]) + iex> {[false, err], _lua} = Lua.eval!(~S[return pcall(function() error("nope") end)]) + iex> err =~ "nope" + true ### Calling Elixir functions from Lua diff --git a/lib/lua/compiler/bytecode.ex b/lib/lua/compiler/bytecode.ex index bbdcac53..202d1b0e 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -110,7 +110,7 @@ defmodule Lua.Compiler.Bytecode do proto_with_children = %{proto | prototypes: compiled_children} - case encode_list(proto.instructions, []) do + case encode_list(proto.instructions, [], 0) do {:ok, encoded} -> %{proto_with_children | bytecode: List.to_tuple(encoded)} @@ -123,25 +123,46 @@ defmodule Lua.Compiler.Bytecode do # # Walks an instruction list, accumulating opcode tuples in reverse. On # the first uncovered opcode, the whole list bails out as `:fallback`. - - defp encode_list([], acc), do: {:ok, Enum.reverse(acc)} - - # `:source_line` opcodes are stripped from the bytecode entirely. They - # only feed error attribution, which is deferred to B5d-v2 for compiled - # prototypes. Keeping them in the bytecode would cost one no-op - # dispatch each — for fib(25), that's ~228k extra dispatch cycles - # against zero observable benefit at this stage. - defp encode_list([{:source_line, _, _} | rest], acc) do - encode_list(rest, acc) + # + # `:source_line` opcodes are stripped from the bytecode entirely — keeping + # them would cost one no-op dispatch each (~228k extra cycles for fib(25)) + # — and instead update the rolling `current_line`. Call opcodes + # (`:call_one`/`:call_zero`/`:call_multi`) bake that line into their own + # tuple so the dispatcher can pass it through to the executor at native- + # call boundaries without any parallel lookup table. Other opcodes ignore + # the line. + + defp encode_list([], acc, _current_line), do: {:ok, Enum.reverse(acc)} + + defp encode_list([{:source_line, n, _} | rest], acc, _current_line) do + encode_list(rest, acc, n) end - defp encode_list([instr | rest], acc) do + defp encode_list([instr | rest], acc, current_line) do case encode(instr) do - {:ok, encoded} -> encode_list(rest, [encoded | acc]) - :fallback -> :fallback + {:ok, encoded} -> + encode_list(rest, [annotate_line(encoded, current_line) | acc], current_line) + + :fallback -> + :fallback end end + # Bakes the current source line into the three call opcodes so the + # dispatcher can attribute native-call errors without a parallel lookup + # table. Other opcodes pass through unchanged — line attribution for + # non-call raise sites (binops, indexing, concat) is deferred. + defp annotate_line({@op_call_one, base, args, hint}, line), + do: {@op_call_one, base, args, hint, line} + + defp annotate_line({@op_call_zero, base, args, hint}, line), + do: {@op_call_zero, base, args, hint, line} + + defp annotate_line({@op_call_multi, base, args, results, hint}, line), + do: {@op_call_multi, base, args, results, hint, line} + + defp annotate_line(other, _line), do: other + # ── Per-opcode encoding ───────────────────────────────────────────────── defp encode({:load_constant, dest, value}), do: {:ok, {@op_load_constant, dest, value}} @@ -196,8 +217,8 @@ defmodule Lua.Compiler.Bytecode do # branches encode to an empty tuple so the dispatcher can distinguish # "no branch" from "fell off the end of a branch". defp encode({:test, reg, then_body, else_body}) do - with {:ok, then_enc} <- encode_list(then_body, []), - {:ok, else_enc} <- encode_list(else_body, []) do + with {:ok, then_enc} <- encode_list(then_body, [], 0), + {:ok, else_enc} <- encode_list(else_body, [], 0) do {:ok, {@op_test, reg, List.to_tuple(then_enc), List.to_tuple(else_enc)}} end end @@ -299,7 +320,7 @@ defmodule Lua.Compiler.Bytecode do # handles the unwind. defp encode({:numeric_for, base, loop_var, body}) do - case encode_list(body, []) do + case encode_list(body, [], 0) do {:ok, body_enc} -> {:ok, {@op_numeric_for, base, loop_var, List.to_tuple(body_enc)}} @@ -309,15 +330,15 @@ defmodule Lua.Compiler.Bytecode do end defp encode({:while_loop, cond_body, test_reg, loop_body}) do - with {:ok, cond_enc} <- encode_list(cond_body, []), - {:ok, body_enc} <- encode_list(loop_body, []) do + with {:ok, cond_enc} <- encode_list(cond_body, [], 0), + {:ok, body_enc} <- encode_list(loop_body, [], 0) do {:ok, {@op_while_loop, test_reg, List.to_tuple(cond_enc), List.to_tuple(body_enc)}} end end defp encode({:repeat_loop, loop_body, cond_body, test_reg}) do - with {:ok, body_enc} <- encode_list(loop_body, []), - {:ok, cond_enc} <- encode_list(cond_body, []) do + with {:ok, body_enc} <- encode_list(loop_body, [], 0), + {:ok, cond_enc} <- encode_list(cond_body, [], 0) do {:ok, {@op_repeat_loop, test_reg, List.to_tuple(body_enc), List.to_tuple(cond_enc)}} end end @@ -327,7 +348,7 @@ defmodule Lua.Compiler.Bytecode do # is small (typically one or two regs) so we encode it as a tuple for # the dispatcher to walk with `:erlang.element/2`. defp encode({:generic_for, base, var_regs, body}) when is_list(var_regs) do - case encode_list(body, []) do + case encode_list(body, [], 0) do {:ok, body_enc} -> {:ok, {@op_generic_for, base, List.to_tuple(var_regs), List.to_tuple(body_enc)}} diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index 72eaa6f6..9ddcb5c7 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -573,7 +573,7 @@ defmodule Lua.VM.Dispatcher do # "throw the return value away"; `return_one/3` skips its # setelement write when it sees it. - {@op_call_zero, base, arg_count, name_hint} -> + {@op_call_zero, base, arg_count, name_hint, line} -> func_value = :erlang.element(base + 1, regs) case func_value do @@ -621,12 +621,12 @@ defmodule Lua.VM.Dispatcher do args = collect_args(regs, base + 1, arg_count) {_results, state} = - Executor.dispatcher_call_function(func_value, args, state, proto, name_hint) + Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames) end - {@op_call_one, base, arg_count, name_hint} -> + {@op_call_one, base, arg_count, name_hint, line} -> func_value = :erlang.element(base + 1, regs) case func_value do @@ -682,7 +682,7 @@ defmodule Lua.VM.Dispatcher do args = collect_args(regs, base + 1, arg_count) {results, state} = - Executor.dispatcher_call_function(func_value, args, state, proto, name_hint) + Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) first = case results do @@ -1041,7 +1041,7 @@ defmodule Lua.VM.Dispatcher do # -2 → expand into consecutive regs at base, set multi_return_count. # n>1 → place first n results into regs starting at base. - {@op_call_multi, base, arg_count, result_count, name_hint} -> + {@op_call_multi, base, arg_count, result_count, name_hint, line} -> func_value = :erlang.element(base + 1, regs) total_args = @@ -1102,7 +1102,7 @@ defmodule Lua.VM.Dispatcher do args = collect_args(regs, base + 1, total_args) {results, state} = - Executor.dispatcher_call_function(func_value, args, state, proto, name_hint) + Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) apply_multi_call_result(result_count, base, results, code, pc + 1, regs, upvalues, proto, state, cont, frames) end diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index 8f930465..53a6212b 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -468,34 +468,39 @@ defmodule Lua.VM.Executor do # the callable shapes and inlines the type-error paths so the # dispatcher's error messages match the interpreter's `:call` opcode. @doc false - @spec dispatcher_call_function(term(), [term()], State.t(), term(), term()) :: + @spec dispatcher_call_function(term(), [term()], State.t(), term(), term(), non_neg_integer()) :: {[term()], State.t()} - def dispatcher_call_function(nil, _args, state, proto, name_hint) do + def dispatcher_call_function(nil, _args, state, proto, name_hint, line) do raise TypeError, value: "attempt to call a nil value" <> format_target_hint(name_hint), source: proto.source, call_stack: state.call_stack, - line: 0, + line: line, error_kind: :call_nil, value_type: nil, state: state end - def dispatcher_call_function({:lua_closure, _, _} = closure, args, state, _proto, _name_hint), - do: call_function(closure, args, state) - - def dispatcher_call_function({:compiled_closure, _, _} = closure, args, state, _proto, _name_hint), + def dispatcher_call_function({:lua_closure, _, _} = closure, args, state, _proto, _name_hint, _line), do: call_function(closure, args, state) - def dispatcher_call_function({:native_func, _} = nf, args, state, proto, _name_hint) do - # The dispatcher strips per-instruction line info at encode time, so any - # position visible here is a stale snapshot from an outer executor frame. - # Publish `{nil, source}` for the duration of the native call: raise - # sites that read `current_position/0` (e.g. `error()`'s §6.1 position - # prefix) then omit the line rather than attribute a wrong one. Restored - # after the call so nested invocations don't leak. + def dispatcher_call_function( + {:compiled_closure, _, _} = closure, + args, + state, + _proto, + _name_hint, + _line + ), + do: call_function(closure, args, state) + + def dispatcher_call_function({:native_func, _} = nf, args, state, proto, _name_hint, line) do + # Publish the source line baked into the call opcode by the encoder so + # raise sites reading `current_position/0` — `error()`'s §6.1 prefix, + # stdlib bad-argument raises — attribute to the right call site. + # Restored after the call so nested invocations don't leak. prev_pos = Process.get(@position_key, @unset) - set_position(nil, proto.source) + set_position(line, proto.source) try do call_function(nf, args, state) @@ -504,14 +509,14 @@ defmodule Lua.VM.Executor do end end - def dispatcher_call_function(other, args, state, proto, name_hint) do + def dispatcher_call_function(other, args, state, proto, name_hint, line) do case get_metatable(other, state) do nil -> raise TypeError, value: "attempt to call a #{Value.type_name(other)} value" <> format_target_hint(name_hint), source: proto.source, call_stack: state.call_stack, - line: 0, + line: line, error_kind: :call_non_function, value_type: value_type(other), state: state @@ -525,7 +530,7 @@ defmodule Lua.VM.Executor do value: "attempt to call a #{Value.type_name(other)} value" <> format_target_hint(name_hint), source: proto.source, call_stack: state.call_stack, - line: 0, + line: line, error_kind: :call_non_function, value_type: value_type(other), state: state diff --git a/test/lua/call_function_error_value_test.exs b/test/lua/call_function_error_value_test.exs index bb763f7a..ed404310 100644 --- a/test/lua/call_function_error_value_test.exs +++ b/test/lua/call_function_error_value_test.exs @@ -182,4 +182,47 @@ defmodule Lua.CallFunctionErrorValueTest do assert %Lua.VM.TypeError{error_kind: :index_non_table} = error.original end end + + describe "call_function!/3 rich render attributes the right source:line" do + test "ArgumentError raised from a stdlib call inside a compiled chunk" do + code = """ + function foo() + pairs("asdf") + end + """ + + {_, lua} = Lua.eval!(Lua.new(), code, source: "regression.lua") + + error = + assert_raise Lua.RuntimeException, fn -> + Lua.call_function!(lua, [:foo], []) + end + + message = Exception.message(error) + assert message =~ "regression.lua:2:" + assert message =~ "bad argument #1 to 'pairs'" + assert error.original.line == 2 + assert error.original.source == "regression.lua" + end + + test "ArgumentError raised inside a generic_for loop body attributes the call line" do + code = """ + function foo() + for i in pairs("asdf") do + print(i) + end + end + """ + + {_, lua} = Lua.eval!(Lua.new(), code, source: "regression.lua") + + error = + assert_raise Lua.RuntimeException, fn -> + Lua.call_function!(lua, [:foo], []) + end + + assert error.original.line == 2 + assert Exception.message(error) =~ "regression.lua:2:" + end + end end diff --git a/test/lua/compiler/bytecode_test.exs b/test/lua/compiler/bytecode_test.exs index 633704b0..008584d9 100644 --- a/test/lua/compiler/bytecode_test.exs +++ b/test/lua/compiler/bytecode_test.exs @@ -265,4 +265,77 @@ defmodule Lua.Compiler.BytecodeTest do assert fn_proto.bytecode == nil end end + + describe "call opcodes carry the source line" do + test "@op_call_one bakes the line of the call site into its tuple" do + # `pairs(x)` is a `:call` with result_count > 0 used as an rvalue, + # encoding to either @op_call_one (when used for one result) or + # @op_call_multi (when generic_for asks for 3 results). Both shapes + # MUST include the line. + proto = + compile!(""" + function f() + local x = pairs({1}) + end + """) + + [fn_proto] = proto.prototypes + + call_ops = + fn_proto.bytecode + |> Tuple.to_list() + |> Enum.filter(fn op -> + tag = :erlang.element(1, op) + tag in [Bytecode.op_call_one(), Bytecode.op_call_zero(), Bytecode.op_call_multi()] + end) + + # Every call opcode carries a positive source line at its last slot. + for op <- call_ops do + last = :erlang.element(tuple_size(op), op) + assert is_integer(last) and last > 0, + "call opcode #{inspect(op)} should end with a line number, got #{inspect(last)}" + end + end + + test "calls in nested bodies carry their own line, not the outer one" do + # The `print` call lives inside the `for`-body. Its line must be 3, + # not the line of the outer `pairs(...)` call (which is line 2). + proto = + compile!(""" + function f() + for i in pairs({1}) do + print(i) + end + end + """) + + [fn_proto] = proto.prototypes + + # The outer body has the pairs call (call_multi, line 2). + outer_lines = + fn_proto.bytecode + |> Tuple.to_list() + |> Enum.filter(fn op -> :erlang.element(1, op) == Bytecode.op_call_multi() end) + |> Enum.map(fn op -> :erlang.element(tuple_size(op), op) end) + + assert outer_lines == [2] + + # The generic_for body has the print call (call_zero, line 3). + generic_for_op = + fn_proto.bytecode + |> Tuple.to_list() + |> Enum.find(fn op -> :erlang.element(1, op) == 51 end) + + assert generic_for_op != nil + nested_body = :erlang.element(4, generic_for_op) + + nested_call_lines = + nested_body + |> Tuple.to_list() + |> Enum.filter(fn op -> :erlang.element(1, op) == Bytecode.op_call_zero() end) + |> Enum.map(fn op -> :erlang.element(tuple_size(op), op) end) + + assert nested_call_lines == [3] + end + end end diff --git a/test/lua/vm/pcall_error_value_test.exs b/test/lua/vm/pcall_error_value_test.exs index 29f2ea88..95c9b123 100644 --- a/test/lua/vm/pcall_error_value_test.exs +++ b/test/lua/vm/pcall_error_value_test.exs @@ -181,19 +181,11 @@ defmodule Lua.VM.PcallErrorValueTest do assert [false, "string", err] = results - case @engine do - :interpreted -> - # Source-first shape: a swapped `{line, source}` destructure - # (emitting `2:test.lua: boom`) must fail this, not just the - # loose `:\d+:` shape. - assert err =~ ~r/^test\.lua:\d+: boom$/ - - :compiled -> - # The dispatcher does not yet plumb a per-call line for native - # calls inside compiled closures, so the prefix is suppressed - # rather than attributed to a stale outer line. - assert err == "boom" - end + # Source-first shape: a swapped `{line, source}` destructure + # (emitting `2:test.lua: boom`) must fail this, not just the + # loose `:\d+:` shape. Both engines now plumb per-call lines + # through to native raise sites. + assert err =~ ~r/^test\.lua:\d+: boom$/ end test "level 0 suppresses the prefix" do diff --git a/test/lua/vm/pcall_state_preservation_test.exs b/test/lua/vm/pcall_state_preservation_test.exs index ea6ed826..04113001 100644 --- a/test/lua/vm/pcall_state_preservation_test.exs +++ b/test/lua/vm/pcall_state_preservation_test.exs @@ -65,13 +65,9 @@ defmodule Lua.VM.PcallStatePreservationTest do assert [2, false, err] = results - # §6.1 position prefix on string errors: correct source:line under - # the interpreter; suppressed (not mis-attributed) under the - # dispatcher, which lacks per-call line info. - case @engine do - :interpreted -> assert err =~ ~r/^test\.lua:\d+: boom$/ - :compiled -> assert err == "boom" - end + # §6.1 position prefix on string errors: both engines now plumb + # per-call line info through to native raise sites. + assert err =~ ~r/^test\.lua:\d+: boom$/ end test "table field write before error() is kept" do From 5427d21119ba96bef410bc4d7d126a44bc67f6e4 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 06:57:11 -0700 Subject: [PATCH 5/8] fix(vm): attribute native generic_for iterator errors to the for line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compiled :generic_for path hard-coded line 0 when invoking the iterator, so a native iterator that raised mid-step (e.g. `for x in error, "deep", nil do end`) leaked `test.lua:0:` while the interpreter attributed `test.lua:2:` — a line-accuracy/parity break. - Bake the for-statement line into the :generic_for opcode via annotate_line/2, mirroring the call opcodes. - Thread it through the dispatcher's :generic_for dispatch and the :cps_generic_for resume marker into dispatcher_call_value/5, which passes it to call_value/5 so the native-iterator boundary publishes the right position. - Pin exact lines in the dual-engine error()-prefix tests (test.lua:2 / test.lua:4) instead of a loose :\d+: regex, and add compiled-vs-interpreted byte-equality tests covering both error() and a native-iterator raise. - Refresh the stale pcall_error_value moduledoc and the dispatcher encode_list/3 comment to match the plumbed-line reality. --- lib/lua/compiler/bytecode.ex | 22 ++++---- lib/lua/vm/dispatcher.ex | 20 +++++--- lib/lua/vm/executor.ex | 22 ++++---- test/lua/vm/pcall_error_value_test.exs | 51 +++++++++++++++---- test/lua/vm/pcall_state_preservation_test.exs | 6 ++- 5 files changed, 80 insertions(+), 41 deletions(-) diff --git a/lib/lua/compiler/bytecode.ex b/lib/lua/compiler/bytecode.ex index 202d1b0e..d18dd72b 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -148,19 +148,23 @@ defmodule Lua.Compiler.Bytecode do end end - # Bakes the current source line into the three call opcodes so the - # dispatcher can attribute native-call errors without a parallel lookup - # table. Other opcodes pass through unchanged — line attribution for - # non-call raise sites (binops, indexing, concat) is deferred. - defp annotate_line({@op_call_one, base, args, hint}, line), - do: {@op_call_one, base, args, hint, line} - - defp annotate_line({@op_call_zero, base, args, hint}, line), - do: {@op_call_zero, base, args, hint, line} + # Bakes the current source line into the call opcodes and `:generic_for` + # so the dispatcher can attribute native-call errors without a parallel + # lookup table. `:generic_for` carries its own line because the iterator + # is invoked at the `for` statement before the body's `:source_line` + # opcodes run, so a native iterator raising mid-step (e.g. `error()`) + # would otherwise leak `:0:`. Other opcodes pass through unchanged — + # line attribution for non-call raise sites (binops, indexing, concat) + # is deferred. + defp annotate_line({@op_call_one, base, args, hint}, line), do: {@op_call_one, base, args, hint, line} + + defp annotate_line({@op_call_zero, base, args, hint}, line), do: {@op_call_zero, base, args, hint, line} defp annotate_line({@op_call_multi, base, args, results, hint}, line), do: {@op_call_multi, base, args, results, hint, line} + defp annotate_line({@op_generic_for, base, var_regs, body}, line), do: {@op_generic_for, base, var_regs, body, line} + defp annotate_line(other, _line), do: other # ── Per-opcode encoding ───────────────────────────────────────────────── diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index 9ddcb5c7..271723fb 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -75,8 +75,10 @@ defmodule Lua.VM.Dispatcher do @op_return_one 27 @op_return_zero 28 # `@op_source_line 29` is reserved but never reaches the dispatcher: the - # bytecode encoder strips `:source_line` entries in `encode_list/2`. - # Line tracking for compiled prototypes is B5d-v2. + # bytecode encoder strips `:source_line` entries in `encode_list/3`, + # baking the rolling line into the call opcodes (and `:generic_for`) + # instead so this dispatcher can attribute native-call errors without a + # parallel per-instruction lookup. @op_new_table 30 @op_get_table 31 @op_set_table 32 @@ -889,17 +891,19 @@ defmodule Lua.VM.Dispatcher do loop_exit = {:loop_exit, code, pc + 1} dispatch(body_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | cont], frames) - {@op_generic_for, base, var_regs, body_bc} -> + {@op_generic_for, base, var_regs, body_bc, line} -> # Iterator call follows the same shape as the executor: # results = iter_func(invariant_state, control). If the first # result is nil the loop terminates; otherwise control gets the - # first result, var_regs[i] each get results[i]. + # first result, var_regs[i] each get results[i]. `line` is baked in + # by the encoder so a native iterator raising mid-step attributes + # to the `for` statement rather than leaking `:0:`. iter_func = :erlang.element(base + 1, regs) invariant_state = :erlang.element(base + 2, regs) control = :erlang.element(base + 3, regs) {results, state} = - Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state) + Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state, line) case results do [nil | _] -> @@ -913,7 +917,7 @@ defmodule Lua.VM.Dispatcher do regs = assign_iter_results(regs, var_regs, results, 0) first_var_reg = :erlang.element(1, var_regs) state = Executor.dispatcher_close_open_upvalues_at_or_above(state, first_var_reg) - marker = {:cps_generic_for, base, var_regs, body_bc, code, pc + 1} + marker = {:cps_generic_for, base, var_regs, body_bc, line, code, pc + 1} loop_exit = {:loop_exit, code, pc + 1} dispatch(body_bc, 1, regs, upvalues, proto, state, [marker, loop_exit | cont], frames) end @@ -1270,7 +1274,7 @@ defmodule Lua.VM.Dispatcher do proto, state, [ - {:cps_generic_for, base, var_regs, body_bc, outer_code, outer_pc} = marker, + {:cps_generic_for, base, var_regs, body_bc, line, outer_code, outer_pc} = marker, {:loop_exit, _, _} = loop_exit | rest_cont ], frames @@ -1280,7 +1284,7 @@ defmodule Lua.VM.Dispatcher do control = :erlang.element(base + 3, regs) {results, state} = - Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state) + Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state, line) case results do [nil | _] -> diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index 53a6212b..84ab365a 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -436,12 +436,15 @@ defmodule Lua.VM.Executor do # `:generic_for` step: invoke the iterator function. The iterator can be # any callable value (Lua closure, compiled closure, native function, # value with `__call`), so we route through `call_value/5` which handles - # the whole shape via the interpreter's call machinery. + # the whole shape via the interpreter's call machinery. `line` is the + # source line baked into the `:generic_for` opcode so a native iterator + # that raises mid-iteration (e.g. `error()`) attributes to the `for` + # statement rather than leaking `:0:`. @doc false - @spec dispatcher_call_value(term(), [term()], term(), State.t()) :: + @spec dispatcher_call_value(term(), [term()], term(), State.t(), non_neg_integer()) :: {[term()], State.t()} - def dispatcher_call_value(callable, args, proto, state) do - call_value(callable, args, proto, state, 0) + def dispatcher_call_value(callable, args, proto, state, line) do + call_value(callable, args, proto, state, line) end # `:concatenate` slow path. Mirrors the interpreter's three-way fallback: @@ -484,15 +487,8 @@ defmodule Lua.VM.Executor do def dispatcher_call_function({:lua_closure, _, _} = closure, args, state, _proto, _name_hint, _line), do: call_function(closure, args, state) - def dispatcher_call_function( - {:compiled_closure, _, _} = closure, - args, - state, - _proto, - _name_hint, - _line - ), - do: call_function(closure, args, state) + def dispatcher_call_function({:compiled_closure, _, _} = closure, args, state, _proto, _name_hint, _line), + do: call_function(closure, args, state) def dispatcher_call_function({:native_func, _} = nf, args, state, proto, _name_hint, line) do # Publish the source line baked into the call opcode by the encoder so diff --git a/test/lua/vm/pcall_error_value_test.exs b/test/lua/vm/pcall_error_value_test.exs index 95c9b123..ffcba8d4 100644 --- a/test/lua/vm/pcall_error_value_test.exs +++ b/test/lua/vm/pcall_error_value_test.exs @@ -19,10 +19,11 @@ defmodule Lua.VM.PcallErrorValueTest do Each case runs under both execution engines: the default compile path (closures carry bytecode and run on the dispatcher) and with bytecode recursively stripped (closures run on the instruction interpreter). - The position prefix is asserted with the correct source and line under - the interpreter; under the dispatcher the per-call line is not yet - plumbed, so the prefix is suppressed rather than attributed to a stale - line. + Both engines emit the `source:line:` prefix: the per-call line is baked + into the call opcode at encode time, so the dispatcher publishes it via + `current_position/0` at native-call boundaries exactly as the + interpreter threads it. The prefix is pinned to the exact source line + under both engines. """ use ExUnit.Case, async: true @@ -181,11 +182,11 @@ defmodule Lua.VM.PcallErrorValueTest do assert [false, "string", err] = results - # Source-first shape: a swapped `{line, source}` destructure - # (emitting `2:test.lua: boom`) must fail this, not just the - # loose `:\d+:` shape. Both engines now plumb per-call lines - # through to native raise sites. - assert err =~ ~r/^test\.lua:\d+: boom$/ + # `error("boom")` sits on line 2 of the chunk. Pin the exact line + # (not a loose `:\d+:`) so an off-by-one or a swapped + # `{line, source}` destructure (`2:test.lua: boom`) fails here. + # Both engines plumb the per-call line to native raise sites. + assert err == "test.lua:2: boom" end test "level 0 suppresses the prefix" do @@ -290,4 +291,36 @@ defmodule Lua.VM.PcallErrorValueTest do end end end + + describe "compiled and interpreted engines agree on the error() prefix" do + test "an in-VM pcall over error() yields byte-identical strings on both engines" do + code = """ + local ok, err = pcall(function() + error("boom") + end) + return ok, type(err), err + """ + + {[false, "string", compiled], _} = run(code, :compiled) + {[false, "string", interpreted], _} = run(code, :interpreted) + + assert compiled == "test.lua:2: boom" + assert compiled == interpreted + end + + test "a native iterator raising mid-step yields byte-identical strings on both engines" do + code = """ + local ok, err = pcall(function() + for x in error, "deep", nil do end + end) + return ok, type(err), err + """ + + {[false, "string", compiled], _} = run(code, :compiled) + {[false, "string", interpreted], _} = run(code, :interpreted) + + assert compiled == "test.lua:2: deep" + assert compiled == interpreted + end + end end diff --git a/test/lua/vm/pcall_state_preservation_test.exs b/test/lua/vm/pcall_state_preservation_test.exs index 04113001..d5e97ba5 100644 --- a/test/lua/vm/pcall_state_preservation_test.exs +++ b/test/lua/vm/pcall_state_preservation_test.exs @@ -66,8 +66,10 @@ defmodule Lua.VM.PcallStatePreservationTest do assert [2, false, err] = results # §6.1 position prefix on string errors: both engines now plumb - # per-call line info through to native raise sites. - assert err =~ ~r/^test\.lua:\d+: boom$/ + # per-call line info through to native raise sites. `error("boom")` + # sits on line 4 (three statements precede it) — pin the exact line + # so an off-by-one regression fails here, not just a loose `:\d+:`. + assert err == "test.lua:4: boom" end test "table field write before error() is kept" do From 003229eb0d4a12300dcb2c879c9b9911221efde8 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:13:47 -0700 Subject: [PATCH 6/8] fix(vm): inherit enclosing line in compiled loop/condition bodies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bytecode encoder stripped `:source_line` opcodes into a rolling `current_line` baked into call opcodes, but every nested-body `encode_list` call hardcoded the seed to 0. A while/repeat condition body carries no `:source_line` opcode of its own, so a native call raising inside the condition (e.g. `while error('x') do end`) baked `:0:` into the §6.1 error VALUE, diverging from the interpreter, which threads the enclosing line through the body. Route body-carrying opcodes (`:test`, `:numeric_for`, `:while_loop`, `:repeat_loop`, `:generic_for`) through `encode/2` so their nested bodies inherit the in-scope `current_line`. Also pass `nil` (not the truthy literal `0`) from the dispatcher's non-call raise bridges (binop / compare / unop / index / set / concat / self) so `TypeError`/`RuntimeError.exception/1` fall back to `current_position/0` instead of rendering a bogus `:0:` in the rich render and `to_map`. The pcall error value was already byte-identical across engines for these; this aligns the rich render too. Per-opcode line baking for these sites remains deferred to B5d-v2. Adds while/repeat condition-body regressions asserting both engines agree with no `:0:` leak. --- lib/lua/compiler/bytecode.ex | 120 +++++++++++++------------ lib/lua/vm/executor.ex | 67 +++++++------- test/lua/vm/pcall_error_value_test.exs | 34 +++++++ 3 files changed, 136 insertions(+), 85 deletions(-) diff --git a/lib/lua/compiler/bytecode.ex b/lib/lua/compiler/bytecode.ex index d18dd72b..9f8ed2a6 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -139,7 +139,7 @@ defmodule Lua.Compiler.Bytecode do end defp encode_list([instr | rest], acc, current_line) do - case encode(instr) do + case encode(instr, current_line) do {:ok, encoded} -> encode_list(rest, [annotate_line(encoded, current_line) | acc], current_line) @@ -168,6 +168,58 @@ defmodule Lua.Compiler.Bytecode do defp annotate_line(other, _line), do: other # ── Per-opcode encoding ───────────────────────────────────────────────── + # + # Body-carrying opcodes (`:test` / `:numeric_for` / `:while_loop` / + # `:repeat_loop` / `:generic_for`) match `encode/2` so they can seed their + # nested-body `encode_list` with the in-scope `current_line`. Without it + # the nested body would restart at line 0 and a native call raising inside + # a while/repeat condition (which carries no `:source_line` opcode of its + # own) would bake `:0:` into the §6.1 error value, diverging from the + # interpreter, which threads the enclosing line through the body. Every + # other opcode is line-agnostic and delegates to `encode/1`. + + defp encode({:test, reg, then_body, else_body}, current_line) do + with {:ok, then_enc} <- encode_list(then_body, [], current_line), + {:ok, else_enc} <- encode_list(else_body, [], current_line) do + {:ok, {@op_test, reg, List.to_tuple(then_enc), List.to_tuple(else_enc)}} + end + end + + defp encode({:numeric_for, base, loop_var, body}, current_line) do + case encode_list(body, [], current_line) do + {:ok, body_enc} -> + {:ok, {@op_numeric_for, base, loop_var, List.to_tuple(body_enc)}} + + :fallback -> + :fallback + end + end + + defp encode({:while_loop, cond_body, test_reg, loop_body}, current_line) do + with {:ok, cond_enc} <- encode_list(cond_body, [], current_line), + {:ok, body_enc} <- encode_list(loop_body, [], current_line) do + {:ok, {@op_while_loop, test_reg, List.to_tuple(cond_enc), List.to_tuple(body_enc)}} + end + end + + defp encode({:repeat_loop, loop_body, cond_body, test_reg}, current_line) do + with {:ok, body_enc} <- encode_list(loop_body, [], current_line), + {:ok, cond_enc} <- encode_list(cond_body, [], current_line) do + {:ok, {@op_repeat_loop, test_reg, List.to_tuple(body_enc), List.to_tuple(cond_enc)}} + end + end + + defp encode({:generic_for, base, var_regs, body}, current_line) when is_list(var_regs) do + case encode_list(body, [], current_line) do + {:ok, body_enc} -> + {:ok, {@op_generic_for, base, List.to_tuple(var_regs), List.to_tuple(body_enc)}} + + :fallback -> + :fallback + end + end + + defp encode(instr, _current_line), do: encode(instr) defp encode({:load_constant, dest, value}), do: {:ok, {@op_load_constant, dest, value}} @@ -216,16 +268,8 @@ defmodule Lua.Compiler.Bytecode do defp encode({:not, dest, src}), do: {:ok, {@op_not, dest, src}} # `:test` carries nested instruction lists for the then/else branches. - # Both branches encode independently; either falling back collapses the - # whole test (and the enclosing prototype) to interpretation. Empty - # branches encode to an empty tuple so the dispatcher can distinguish - # "no branch" from "fell off the end of a branch". - defp encode({:test, reg, then_body, else_body}) do - with {:ok, then_enc} <- encode_list(then_body, [], 0), - {:ok, else_enc} <- encode_list(else_body, [], 0) do - {:ok, {@op_test, reg, List.to_tuple(then_enc), List.to_tuple(else_enc)}} - end - end + # It encodes via `encode/2` (above) so each branch inherits the enclosing + # `current_line`. # `:call` shapes: # @@ -316,50 +360,16 @@ defmodule Lua.Compiler.Bytecode do defp encode({:length, dest, source}), do: {:ok, {@op_length, dest, source}} - # `:numeric_for`, `:while_loop`, `:repeat_loop`, `:generic_for` each - # carry nested instruction lists for the loop body (and, for while / - # repeat, the condition). Bodies encode recursively. The B5b-v2 guard - # against `:break` inside `:numeric_for` is gone — `:break` is a - # first-class opcode now and the dispatcher's `find_loop_exit/1` - # handles the unwind. - - defp encode({:numeric_for, base, loop_var, body}) do - case encode_list(body, [], 0) do - {:ok, body_enc} -> - {:ok, {@op_numeric_for, base, loop_var, List.to_tuple(body_enc)}} - - :fallback -> - :fallback - end - end - - defp encode({:while_loop, cond_body, test_reg, loop_body}) do - with {:ok, cond_enc} <- encode_list(cond_body, [], 0), - {:ok, body_enc} <- encode_list(loop_body, [], 0) do - {:ok, {@op_while_loop, test_reg, List.to_tuple(cond_enc), List.to_tuple(body_enc)}} - end - end - - defp encode({:repeat_loop, loop_body, cond_body, test_reg}) do - with {:ok, body_enc} <- encode_list(loop_body, [], 0), - {:ok, cond_enc} <- encode_list(cond_body, [], 0) do - {:ok, {@op_repeat_loop, test_reg, List.to_tuple(body_enc), List.to_tuple(cond_enc)}} - end - end - - # `:generic_for` carries a list of variable register indices that - # receive successive iterator results plus the body. The variable list - # is small (typically one or two regs) so we encode it as a tuple for - # the dispatcher to walk with `:erlang.element/2`. - defp encode({:generic_for, base, var_regs, body}) when is_list(var_regs) do - case encode_list(body, [], 0) do - {:ok, body_enc} -> - {:ok, {@op_generic_for, base, List.to_tuple(var_regs), List.to_tuple(body_enc)}} - - :fallback -> - :fallback - end - end + # `:numeric_for`, `:while_loop`, `:repeat_loop`, `:generic_for` each carry + # nested instruction lists for the loop body (and, for while / repeat, the + # condition). They encode via `encode/2` (above) so the nested bodies + # inherit the enclosing `current_line` — the while/repeat condition body + # carries no `:source_line` opcode of its own, so without the inherited + # line a native call raising inside the condition would bake `:0:`. The + # B5b-v2 guard against `:break` inside `:numeric_for` is gone — `:break` is + # a first-class opcode now and the dispatcher's `find_loop_exit/1` handles + # the unwind. `:generic_for`'s variable-register list is encoded as a tuple + # for the dispatcher to walk with `:erlang.element/2`. # ── Closures, upvalues, varargs, self, concat, break ──────────────────── diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index 84ab365a..4ae5bfd1 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -233,81 +233,86 @@ defmodule Lua.VM.Executor do # current `proto` (for source attribution) and returns the same # `{value, state}` shape the interpreter clauses produce. # - # Line position for raise sites is passed as `0` because the dispatcher - # does not yet track per-instruction line numbers — error attribution - # for compiled prototypes is the subject of B5d-v2. Native callbacks - # invoked via metamethods still get accurate positions via the - # process-dictionary bridge installed at the call boundary. + # Line position for these non-call raise sites is passed as `nil` rather + # than a literal line: the dispatcher does not yet bake per-instruction + # lines into binop / compare / index / concat opcodes (that is B5d-v2's + # subject). `nil` lets `Lua.VM.TypeError.exception/1` / `RuntimeError` + # fall back to `current_position/0` instead of rendering a bogus `:0:`. + # The pcall error *value* is unaffected (TypeError's §6.1 value carries no + # source:line: prefix); this only keeps the rich render / `to_map` line in + # sync with the interpreter. Native callbacks invoked via metamethods + # still get accurate positions via the process-dictionary bridge installed + # at the call boundary. @doc false @spec dispatcher_binop(atom(), term(), term(), State.t(), term(), term(), term()) :: {term(), State.t()} def dispatcher_binop(:add, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__add", a, b, state, fn -> - safe_add(a, b, 0, proto.source, hint_a, hint_b, state) + safe_add(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:subtract, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__sub", a, b, state, fn -> - safe_subtract(a, b, 0, proto.source, hint_a, hint_b, state) + safe_subtract(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:multiply, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__mul", a, b, state, fn -> - safe_multiply(a, b, 0, proto.source, hint_a, hint_b, state) + safe_multiply(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:divide, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__div", a, b, state, fn -> - safe_divide(a, b, 0, proto.source, hint_a, hint_b, state) + safe_divide(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:floor_divide, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__idiv", a, b, state, fn -> - safe_floor_divide(a, b, 0, proto.source, hint_a, hint_b, state) + safe_floor_divide(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:modulo, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__mod", a, b, state, fn -> - safe_modulo(a, b, 0, proto.source, hint_a, hint_b, state) + safe_modulo(a, b, nil, proto.source, hint_a, hint_b, state) end) end def dispatcher_binop(:power, a, b, state, proto, hint_a, hint_b) do try_binary_metamethod("__pow", a, b, state, fn -> - safe_power(a, b, 0, proto.source, hint_a, hint_b, state) + safe_power(a, b, nil, proto.source, hint_a, hint_b, state) end) end @doc false @spec dispatcher_unop(atom(), term(), State.t(), term(), term()) :: {term(), State.t()} def dispatcher_unop(:negate, val, state, proto, hint) do - try_unary_metamethod("__unm", val, state, fn -> safe_negate(val, 0, proto.source, hint, state) end) + try_unary_metamethod("__unm", val, state, fn -> safe_negate(val, nil, proto.source, hint, state) end) end @doc false @spec dispatcher_cmp(atom(), term(), term(), State.t(), term()) :: {term(), State.t()} def dispatcher_cmp(:less_than, a, b, state, proto) do - try_binary_metamethod("__lt", a, b, state, fn -> safe_compare_lt(a, b, 0, proto.source, state) end) + try_binary_metamethod("__lt", a, b, state, fn -> safe_compare_lt(a, b, nil, proto.source, state) end) end def dispatcher_cmp(:less_equal, a, b, state, proto) do - compare_le(a, b, state, 0, proto.source) + compare_le(a, b, state, nil, proto.source) end def dispatcher_cmp(:greater_than, a, b, state, proto) do # Lua 5.3 §3.4.4: a > b dispatches __lt with swapped operands. - try_binary_metamethod("__lt", b, a, state, fn -> safe_compare_lt(b, a, 0, proto.source, state) end) + try_binary_metamethod("__lt", b, a, state, fn -> safe_compare_lt(b, a, nil, proto.source, state) end) end def dispatcher_cmp(:greater_equal, a, b, state, proto) do # Lua 5.3 §3.4.4: a >= b is rewritten to b <= a. - compare_le(b, a, state, 0, proto.source) + compare_le(b, a, state, nil, proto.source) end def dispatcher_cmp(:equal, a, b, state, _proto) do @@ -334,30 +339,31 @@ defmodule Lua.VM.Executor do _ -> case :erlang.map_get(:metatable, table) do nil -> {nil, state} - _ -> index_value(tref, name, state, 0, proto.source, name_hint) + _ -> index_value(tref, name, state, nil, proto.source, name_hint) end end end def dispatcher_get_field(value, name, state, proto, name_hint) do - index_value(value, name, state, 0, proto.source, name_hint) + index_value(value, name, state, nil, proto.source, name_hint) end # ── Dispatcher bridges: table opcodes ─────────────────────────────────── # # These wrap the same `defp` helpers that the interpreter's `:get_table` # / `:set_table` / `:set_field` / `:length` clauses call, so the - # dispatcher inherits metamethod fidelity for free. Line attribution - # is uniformly `0` here; threading honest line info into compiled - # prototypes is B5d-v2. Native callbacks reached via metamethods still - # see accurate positions via the process-dictionary bridge installed + # dispatcher inherits metamethod fidelity for free. Line attribution is + # uniformly `nil` here so the error render falls back to + # `current_position/0` instead of `:0:`; threading honest line info into + # compiled prototypes is B5d-v2. Native callbacks reached via metamethods + # still see accurate positions via the process-dictionary bridge installed # at the call boundary. @doc false @spec dispatcher_get_table(term(), term(), State.t(), term(), term()) :: {term(), State.t()} def dispatcher_get_table(value, key, state, proto, name_hint) do - index_value(value, key, state, 0, proto.source, name_hint) + index_value(value, key, state, nil, proto.source, name_hint) end @doc false @@ -368,7 +374,7 @@ defmodule Lua.VM.Executor do end def dispatcher_set_table(value, _key, _value, state, proto, name_hint) do - raise_index_type_error(value, 0, proto.source, name_hint, state) + raise_index_type_error(value, nil, proto.source, name_hint, state) end @doc false @@ -379,7 +385,7 @@ defmodule Lua.VM.Executor do end def dispatcher_set_field(value, _name, _value, state, proto, name_hint) do - raise_index_type_error(value, 0, proto.source, name_hint, state) + raise_index_type_error(value, nil, proto.source, name_hint, state) end # The `_proto` parameter is unused today because `try_unary_metamethod` @@ -424,13 +430,14 @@ defmodule Lua.VM.Executor do # # `:self` method resolution. Wraps `index_value/6` so __index metamethod # dispatch matches the interpreter clause-for-clause. Line attribution is - # `0` for the same reason as the other bridges — error positions are - # B5d-v2. + # `nil` for the same reason as the other non-call bridges — it falls back + # to `current_position/0` rather than rendering `:0:`; per-instruction line + # baking for these opcodes is B5d-v2. @doc false @spec dispatcher_index_method_target(term(), term(), State.t(), term(), term()) :: {term(), State.t()} def dispatcher_index_method_target(obj, method_name, state, proto, name_hint) do - index_value(obj, method_name, state, 0, proto.source, name_hint) + index_value(obj, method_name, state, nil, proto.source, name_hint) end # `:generic_for` step: invoke the iterator function. The iterator can be @@ -460,7 +467,7 @@ defmodule Lua.VM.Executor do src = proto.source try_binary_metamethod("__concat", left, right, state, fn -> - concat_checked(concat_coerce(left, 0, src, state), concat_coerce(right, 0, src, state), state) + concat_checked(concat_coerce(left, nil, src, state), concat_coerce(right, nil, src, state), state) end) end diff --git a/test/lua/vm/pcall_error_value_test.exs b/test/lua/vm/pcall_error_value_test.exs index ffcba8d4..6bfe8010 100644 --- a/test/lua/vm/pcall_error_value_test.exs +++ b/test/lua/vm/pcall_error_value_test.exs @@ -322,5 +322,39 @@ defmodule Lua.VM.PcallErrorValueTest do assert compiled == "test.lua:2: deep" assert compiled == interpreted end + + test "a native call raising inside a while condition keeps the condition's line" do + code = """ + local ok, err = pcall(function() + while error("wc") do end + end) + return ok, type(err), err + """ + + {[false, "string", compiled], _} = run(code, :compiled) + {[false, "string", interpreted], _} = run(code, :interpreted) + + assert compiled == "test.lua:2: wc" + assert compiled == interpreted + end + + test "a native call raising inside a repeat-until condition keeps the loop's line" do + code = """ + local ok, err = pcall(function() + repeat + until error("rc") + end) + return ok, type(err), err + """ + + {[false, "string", compiled], _} = run(code, :compiled) + {[false, "string", interpreted], _} = run(code, :interpreted) + + # The condition body carries no `:source_line` of its own, so it + # inherits the enclosing loop's line (the `repeat` at line 2) under + # both engines — the point is that they agree, with no `:0:` leak. + assert compiled == "test.lua:2: rc" + assert compiled == interpreted + end end end From 8d79061716adb3b95a121ad579a980f1f95f0bf8 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Thu, 11 Jun 2026 07:28:54 -0700 Subject: [PATCH 7/8] test(vm): pin cross-engine ArgumentError parity and outer-line restore after nested native catch --- test/lua/vm/pcall_error_value_test.exs | 22 +++++++++++++++++ test/lua/vm/pcall_state_preservation_test.exs | 24 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/test/lua/vm/pcall_error_value_test.exs b/test/lua/vm/pcall_error_value_test.exs index 6bfe8010..9e6b2d92 100644 --- a/test/lua/vm/pcall_error_value_test.exs +++ b/test/lua/vm/pcall_error_value_test.exs @@ -356,5 +356,27 @@ defmodule Lua.VM.PcallErrorValueTest do assert compiled == "test.lua:2: rc" assert compiled == interpreted end + + test "a stdlib ArgumentError yields byte-identical strings on both engines" do + # An in-VM `pcall` over a bad-argument stdlib call returns the raw Lua + # error value with no `source:line:` prefix (the prefix is added only + # by the Elixir-side rich render in `call_function!`, not by the + # native raise that `pcall` traps). The point pinned here is that the + # dispatcher and the interpreter agree byte-for-byte on that value, so + # the per-call line plumbing this PR adds cannot make the two engines + # diverge on the bad-argument render. + code = """ + local ok, err = pcall(function() + pairs("asdf") + end) + return ok, type(err), err + """ + + {[false, "string", compiled], _} = run(code, :compiled) + {[false, "string", interpreted], _} = run(code, :interpreted) + + assert compiled == "bad argument #1 to 'pairs' (table expected, got string)" + assert compiled == interpreted + end end end diff --git a/test/lua/vm/pcall_state_preservation_test.exs b/test/lua/vm/pcall_state_preservation_test.exs index d5e97ba5..326b6ec1 100644 --- a/test/lua/vm/pcall_state_preservation_test.exs +++ b/test/lua/vm/pcall_state_preservation_test.exs @@ -329,6 +329,30 @@ defmodule Lua.VM.PcallStatePreservationTest do assert [1, 2, 3, false] = results end + test "outer native error reports the outer line after an inner native error is caught" do + # The per-call position the dispatcher publishes is saved/restored + # around each native call. After the inner `error("inner")` (line 4) + # is trapped by the inner pcall, the outer `error("outer")` must + # report ITS OWN line (line 7) — a leaked inner position would + # surface as `test.lua:4:`. + {results, _state} = + run( + """ + local _, inner_err = pcall(function() + local _, deep_err = pcall(function() + error("inner") + end) + error("outer") + end) + return inner_err + """, + @engine + ) + + assert [outer_err] = results + assert outer_err == "test.lua:5: outer" + end + test "setmetatable before error is kept" do {results, _state} = run( From 8c7c7680ae4dcf535dfef3e3ca612cf729b5012a Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Sat, 13 Jun 2026 17:35:46 -0700 Subject: [PATCH 8/8] test(vm): mix format bytecode_test.exs --- test/lua/compiler/bytecode_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/lua/compiler/bytecode_test.exs b/test/lua/compiler/bytecode_test.exs index 008584d9..9e527b3f 100644 --- a/test/lua/compiler/bytecode_test.exs +++ b/test/lua/compiler/bytecode_test.exs @@ -292,6 +292,7 @@ defmodule Lua.Compiler.BytecodeTest do # Every call opcode carries a positive source line at its last slot. for op <- call_ops do last = :erlang.element(tuple_size(op), op) + assert is_integer(last) and last > 0, "call opcode #{inspect(op)} should end with a line number, got #{inspect(last)}" end @@ -326,7 +327,7 @@ defmodule Lua.Compiler.BytecodeTest do |> Tuple.to_list() |> Enum.find(fn op -> :erlang.element(1, op) == 51 end) - assert generic_for_op != nil + assert generic_for_op nested_body = :erlang.element(4, generic_for_op) nested_call_lines =