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
905 changes: 588 additions & 317 deletions lib/lua/vm/dispatcher.ex

Large diffs are not rendered by default.

61 changes: 30 additions & 31 deletions lib/lua/vm/executor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ defmodule Lua.VM.Executor do

def call_function({:compiled_closure, callee_proto, callee_upvalues}, args, state) do
# Compiled callees route through the dispatcher. The dispatcher manages
# its own register file setup, vararg routing, and open-upvalue save/
# restore — `Dispatcher.execute/4` mirrors the semantics of this
# function for the bytecode-encoded path.
# its own register file setup and vararg routing, and isolates open
# upvalues by threading them as a dispatch-loop parameter seeded empty
# at this boundary — `Dispatcher.execute/4` mirrors the semantics of
# this function for the bytecode-encoded path.
Dispatcher.execute(callee_proto, args, callee_upvalues, state)
end

Expand Down Expand Up @@ -498,12 +499,6 @@ defmodule Lua.VM.Executor do
coerce_numeric_for_controls(init, limit, step, state)
end

@doc false
@spec dispatcher_close_open_upvalues_at_or_above(State.t(), non_neg_integer()) :: State.t()
def dispatcher_close_open_upvalues_at_or_above(state, threshold) do
close_open_upvalues_at_or_above(state, threshold)
end

# ── Dispatcher bridges: B5c-v2 ──────────────────────────────────────────
#
# `:self` method resolution. Wraps `index_value/6` so __index metamethod
Expand Down Expand Up @@ -579,14 +574,24 @@ defmodule Lua.VM.Executor 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(line, proto.source)
#
# Every compiled-mode stdlib call lands here, so the bridge talks to the
# process dictionary through the BIFs instead of the `Process.*`
# wrappers. The restore must run on raise too — not every entry into
# this bridge sits under `execute/5`'s `after restore_position/1` net
# (e.g. `Lua.call_function/3` invoking a compiled closure), so a
# raise-skipped restore would leak a stale position into later,
# unrelated evaluations' diagnostics.
prev_pos = :erlang.get(@position_key)
:erlang.put(@position_key, {line, proto.source})

try do
call_function(nf, args, state)
after
restore_position(prev_pos)
case prev_pos do
:undefined -> :erlang.erase(@position_key)
pos -> :erlang.put(@position_key, pos)
end
end
end

Expand Down Expand Up @@ -622,18 +627,12 @@ defmodule Lua.VM.Executor do
end
end

@doc false
@spec dispatcher_call_info(term(), term(), non_neg_integer()) :: call_frame()
def dispatcher_call_info(proto, name_hint, line) do
# Hot path: every Lua call pushes one of these. Keep it a flat 3-tuple
# carrying the raw `name_hint` tag, and defer the `hint_name`/
# `hint_namewhat` decoding to the cold readers (`frame_name/1`,
# `frame_namewhat/1`) that only run during traceback formatting and
# `debug.getinfo`. A tuple is ~4 words vs ~7 for the old 4-key map, and
# skips two function calls per call frame.
{proto.source, line, name_hint}
end

# Every Lua call pushes one call frame, so the runtime shape is a flat
# 3-tuple `{source, line, name_hint}` carrying the raw `name_hint` tag;
# the `hint_name` / `hint_namewhat` decoding is deferred to the cold
# readers (`frame_name/1`, `frame_namewhat/1`) that only run during
# traceback formatting and `debug.getinfo`. A tuple is ~4 words against
# ~7 for a 4-key map, and skips two function calls per frame.
@typedoc false
@type call_frame() :: {term(), non_neg_integer(), term()} | map()

Expand Down Expand Up @@ -949,7 +948,7 @@ defmodule Lua.VM.Executor do
instruction_count
) do
cell_ref = elem(upvalues, index)
value = Map.get(state.upvalue_cells, cell_ref)
value = :maps.get(cell_ref, state.upvalue_cells, nil)
regs = put_elem(regs, dest, value)
do_execute(rest, regs, upvalues, proto, state, cont, frames, line, instruction_count)
end
Expand Down Expand Up @@ -991,9 +990,9 @@ defmodule Lua.VM.Executor do
instruction_count
) do
value =
case Map.get(state.open_upvalues, reg) do
case :maps.get(reg, state.open_upvalues, nil) do
nil -> elem(regs, reg)
cell_ref -> Map.get(state.upvalue_cells, cell_ref)
cell_ref -> :maps.get(cell_ref, state.upvalue_cells, nil)
end

regs = put_elem(regs, dest, value)
Expand Down Expand Up @@ -1043,7 +1042,7 @@ defmodule Lua.VM.Executor do
instruction_count
) do
state =
case Map.get(state.open_upvalues, reg) do
case :maps.get(reg, state.open_upvalues, nil) do
nil ->
state

Expand Down Expand Up @@ -1287,7 +1286,7 @@ defmodule Lua.VM.Executor do
{captured_upvalues_reversed, state} =
Enum.reduce(nested_proto.upvalue_descriptors, {[], state}, fn
{:parent_local, reg, _name}, {cells, state} ->
case Map.get(state.open_upvalues, reg) do
case :maps.get(reg, state.open_upvalues, nil) do
nil ->
cell_ref = make_ref()
value = elem(regs, reg)
Expand Down Expand Up @@ -3751,7 +3750,7 @@ defmodule Lua.VM.Executor do
# environment lives in upvalue slot 0 when present (a chunk loaded via
# `load(..., env)`), otherwise default to the global table `_G`.
defp load_env_value(upvalues, state) when tuple_size(upvalues) > 0 do
Map.get(state.upvalue_cells, elem(upvalues, 0))
:maps.get(elem(upvalues, 0), state.upvalue_cells, nil)
end

defp load_env_value(_upvalues, state), do: State.g_ref(state)
Expand Down
9 changes: 9 additions & 0 deletions lib/lua/vm/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ defmodule Lua.VM.State do
alias Lua.VM.RuntimeError
alias Lua.VM.Table

# `call_stack`, `call_depth` and `open_upvalues` are control state, not
# heap state. The interpreter keeps them here; the dispatcher threads
# them as loop parameters (the same discipline as `instruction_count`
# below) so an in-mode Lua call allocates no struct at all, and writes
# them back into these fields whenever execution leaves the dispatch
# loop — an `Executor` bridge, a native callback, a raise site. Anything
# outside the loop that reads them (`debug.getinfo`, `error(msg, level)`,
# traceback formatting, `check_call_depth!/1`) therefore still sees live
# values.
defstruct call_stack: [],
# Call depth tracked as an O(1) counter that moves in lockstep
# with `call_stack` — `length(call_stack)` would be O(depth) per
Expand Down
27 changes: 26 additions & 1 deletion lib/lua/vm/table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ defmodule Lua.VM.Table do
encode) get split storage with no extra effort.
"""
@spec from_data(map()) :: t()
# Every `{}` constructor allocates through here; an empty map has nothing
# to sort and nothing to fold, so skip straight to the empty struct.
def from_data(data) when map_size(data) == 0, do: %__MODULE__{}

def from_data(data) when is_map(data) do
split_from_map(%__MODULE__{}, data)
end
Expand Down Expand Up @@ -198,14 +202,35 @@ defmodule Lua.VM.Table do
end
end

# No dead keys means the resurrection branch cannot apply, so the write is
# either an in-place overwrite or a fresh append. This is the shape every
# `t.field = v` / `t[k] = v` on a table that has never had a key removed
# takes, so it skips the `dead` probe entirely.
defp insert_hash(%__MODULE__{data: data, order_tail: order_tail, dead: dead} = table, key, value)
when map_size(dead) == 0 do
table = if positive_int?(key), do: %{table | border: :dirty}, else: table

if :maps.is_key(key, data) do
%{table | data: :maps.put(key, value, data)}
else
%{
table
| data: :maps.put(key, value, data),
order_tail: [key | order_tail],
order_index: nil,
order_arr: nil
}
end
end

defp insert_hash(%__MODULE__{data: data, order: order, order_tail: order_tail, dead: dead} = table, key, value) do
# A positive-integer hash key sits adjacent to the probe range and can
# change #t once a contiguous fill reaches it, so invalidate the cache.
# Non-integer keys (strings/floats/bools) never affect #t — leave it.
table = if positive_int?(key), do: %{table | border: :dirty}, else: table

cond do
Map.has_key?(dead, key) ->
:maps.is_key(key, dead) ->
merged_order = order ++ Enum.reverse(order_tail)
new_order = Enum.reject(merged_order, &(&1 === key))

Expand Down
56 changes: 56 additions & 0 deletions test/lua/call_function_error_value_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,60 @@ defmodule Lua.CallFunctionErrorValueTest do
assert Lua.format_exception(error) =~ "regression.lua:2:"
end
end

describe "call_function/3 position hygiene across evaluations" do
test "a raise inside a compiled closure's stdlib call does not leak its position" do
# The dispatcher publishes {line, source} around every native stdlib
# call so raise sites attribute to the right call site. That position
# must be restored even when the native call raises: `call_function/3`
# has no outer save/restore net, so a leak here surfaces in a *later*,
# unrelated evaluation whose own error carries no line info.
{[ref], lua} =
Lua.eval!(
Lua.new(),
"""
local function boom()


error("kaboom")
end
return boom
""",
decode: false,
source: "first.lua"
)

# Pin that `boom` bytecode-compiled: the leak under test lives in the
# dispatcher's native-call bridge, not the interpreter's.
assert {:compiled_closure, _, _} = ref.ref

assert {:error, %RuntimeException{kind: :error}, %Lua{} = lua} =
Lua.call_function(lua, ref, [])

error =
assert_raise RuntimeException, fn ->
Lua.eval!(
lua,
"""
local function g()
local t = nil
return t.x
end
return g()
""",
source: "second.lua"
)
end

# Indexing nil inside a compiled body raises without line info, and
# the exception falls back to `current_position/0`. A leaked position
# would attribute this error to first.lua:4 instead of no line.
assert %TypeError{error_kind: :index_non_table, line: nil, source: "second.lua"} =
error.original

message = Lua.format_exception(error)
refute message =~ "first.lua"
refute message =~ ":4:"
end
end
end
45 changes: 45 additions & 0 deletions test/lua/vm/dispatcher_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,51 @@ defmodule Lua.VM.DispatcherTest do
assert results == [18]
end

test ":set_field — nil assignment deletes the key" do
{proto, results} =
run!("""
function f()
local t = { x = 1, y = 2 }
t.x = nil
local count = 0
for _ in pairs(t) do count = count + 1 end
return t.x, t.y, count
end
return f()
""")

assert first_sub(proto).bytecode
assert results == [nil, 2, 1]
end

test ":set_field — __newindex function handler (slow-path bridge)" do
{proto, results} =
run!("""
function f(t, v) t.x = v end
local log = {}
local t = setmetatable({}, {__newindex = function(_, k, v) log[k] = v + 1 end})
f(t, 41)
return rawget(t, "x"), log.x
""")

assert first_sub(proto).bytecode
assert results == [nil, 42]
end

test ":set_field — __newindex table handler (slow-path bridge)" do
{proto, results} =
run!("""
function f(t, v) t.x = v end
local backing = {}
local t = setmetatable({}, {__newindex = backing})
f(t, 7)
return rawget(t, "x"), backing.x
""")

assert first_sub(proto).bytecode
assert results == [nil, 7]
end

test ":get_table — integer-key fast path" do
{proto, results} =
run!("""
Expand Down
40 changes: 40 additions & 0 deletions test/lua/vm/require_open_upvalue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,44 @@ defmodule Lua.VM.RequireOpenUpvalueTest do

assert {["registered:ok"], _} = eval_with_path(code, tmp_dir)
end

test "compiled caller's open upvalues survive a require in its body", %{tmp_dir: tmp_dir} do
# Same invariant, entered from the bytecode dispatcher rather than the
# interpreter. The dispatcher carries its open upvalues as a loop
# parameter, so a nested evaluation cannot reach them; this pins that
# a `require` between capturing a local and reading it back through
# the cell still yields the caller's value.
File.write!(Path.join(tmp_dir, "inner.lua"), """
local inner_local = "inner_value"

local function captures_it()
return inner_local
end

return { tag = "inner", fn = captures_it }
""")

# The body runs on the dispatcher: `outer/0` is reached by a call, and
# its own body creates the closure over `mine`.
code = ~S"""
local function outer()
local mine = "outer_value"
local function read_mine() return mine end
local m = require("inner")
mine = "reassigned"
return read_mine(), m.tag, m.fn()
end

return outer()
"""

# Pin that `outer` actually bytecode-compiles, so this exercises the
# dispatcher rather than silently degrading into an interpreter test.
{:ok, ast} = Lua.Parser.parse(code)
{:ok, proto} = Lua.Compiler.compile(ast, source: "test.lua")
assert %Lua.Compiler.Prototype{prototypes: [outer_proto | _]} = proto
assert outer_proto.bytecode

assert {["reassigned", "inner", "inner_value"], _} = eval_with_path(code, tmp_dir)
end
end
Loading