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 a9c700f8..403bb226 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -122,7 +122,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)} @@ -135,26 +135,103 @@ 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`. + # + # `: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([], acc), do: {:ok, Enum.reverse(acc)} + defp encode_list([instr | rest], acc, current_line) do + case encode(instr, current_line) do + {:ok, encoded} -> + encode_list(rest, [annotate_line(encoded, current_line) | acc], current_line) - # `: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) + :fallback -> + :fallback + end end - defp encode_list([instr | rest], acc) do - case encode(instr) do - {:ok, encoded} -> encode_list(rest, [encoded | acc]) - :fallback -> :fallback + # 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 ───────────────────────────────────────────────── + # + # 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 - # ── Per-opcode encoding ───────────────────────────────────────────────── + 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 +293,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, []), - {:ok, else_enc} <- encode_list(else_body, []) 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: # @@ -320,50 +389,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, []) 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, []), - {:ok, body_enc} <- encode_list(loop_body, []) 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 - {: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, []) 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/dispatcher.ex b/lib/lua/vm/dispatcher.ex index 26500a89..fdbebaac 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 @@ -663,7 +665,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 @@ -711,12 +713,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 @@ -772,7 +774,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 @@ -994,17 +996,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 | _] -> @@ -1018,7 +1022,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 @@ -1146,7 +1150,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 = @@ -1207,7 +1211,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 @@ -1375,7 +1379,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 @@ -1385,7 +1389,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 af4172dc..cdf72e12 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -233,61 +233,66 @@ 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 # Bitwise bridges mirror the `:bitwise_*` / `:shift_*` interpreter clauses @@ -352,21 +357,21 @@ defmodule Lua.VM.Executor do @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 @@ -393,30 +398,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 @@ -427,7 +433,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 @@ -438,7 +444,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` @@ -483,24 +489,28 @@ 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 # 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: @@ -516,7 +526,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 @@ -527,34 +537,32 @@ 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), + 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), + 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) 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({: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) @@ -563,14 +571,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 @@ -584,7 +592,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 10ce91ed..2c798098 100644 --- a/test/lua/compiler/bytecode_test.exs +++ b/test/lua/compiler/bytecode_test.exs @@ -316,4 +316,78 @@ 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 + 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..9e6b2d92 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,19 +182,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 + # `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 @@ -298,4 +291,92 @@ 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 + + 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 + + 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 ea6ed826..326b6ec1 100644 --- a/test/lua/vm/pcall_state_preservation_test.exs +++ b/test/lua/vm/pcall_state_preservation_test.exs @@ -65,13 +65,11 @@ 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. `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 @@ -331,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(