Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
171 changes: 103 additions & 68 deletions lib/lua/compiler/bytecode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)}

Expand All @@ -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}}

Expand Down Expand Up @@ -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:
#
Expand Down Expand Up @@ -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 ────────────────────

Expand Down
32 changes: 18 additions & 14 deletions lib/lua/vm/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 | _] ->
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 | _] ->
Expand Down
Loading
Loading