diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index b0d375e..c9b5fd8 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -154,18 +154,28 @@ defmodule Lua.VM.Dispatcher do proto end - saved_open = state.open_upvalues - try do - state = %{state | open_upvalues: %{}} - - # Seed the dispatcher tally from the budget carried across the boundary - # so an alternating-engine call chain accumulates against one budget - # instead of resetting here; the terminals stamp the final tally back - # into `state.instruction_count`. - {results, state} = dispatch(proto.bytecode, 1, regs, upvalues, proto, state, [], [], state.instruction_count) + # Seed the loop-carried control parameters — instruction tally, call + # stack, call depth, open upvalues — from the state crossing the + # boundary. `open_upvalues` starts empty: cells are keyed by register + # index, so a nested evaluation must not see the caller's. The + # terminals stamp all four back into the struct on the way out. + {results, state} = + dispatch( + proto.bytecode, + 1, + regs, + upvalues, + proto, + state, + [], + [], + state.instruction_count, + state.call_stack, + state.call_depth, + %{} + ) - state = %{state | open_upvalues: saved_open} {results, state} rescue # Backstop net: any raise site missed by the per-site state @@ -215,55 +225,56 @@ defmodule Lua.VM.Dispatcher do # `Executor.call_function/3` instead, paying one Erlang stack frame # at the boundary. - defp dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) when pc > tuple_size(code) do - finish_body(regs, upvalues, proto, state, cont, frames, instruction_count) + defp dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) + when pc > tuple_size(code) do + finish_body(regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end - defp dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) do + defp dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) do case :erlang.element(pc, code) do {@op_load_constant, dest, value} -> regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_load_boolean, dest, value} -> regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_load_nil, dest, count} -> regs = clear_nils(regs, dest, count + 1) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_move, dest, src} -> v = :erlang.element(src + 1, regs) regs = :erlang.setelement(dest + 1, regs, v) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_load_env, dest} -> env = if tuple_size(upvalues) > 0 do - Map.get(state.upvalue_cells, :erlang.element(1, upvalues)) + :maps.get(:erlang.element(1, upvalues), state.upvalue_cells, nil) else State.g_ref(state) end regs = :erlang.setelement(dest + 1, regs, env) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_get_upvalue, dest, index} -> cell_ref = :erlang.element(index + 1, upvalues) - # Mirror the interpreter's `Map.get/2` (returns nil for a dangling + # Mirror the interpreter's defaulting read (nil for a dangling # cell) rather than `:erlang.map_get/2` (which raises `:badkey`). # Compiled closures should never carry stale cell refs, but the # invariant is the interpreter's, not ours, and the error shape # has to match where it does fire. - v = Map.get(state.upvalue_cells, cell_ref) + v = :maps.get(cell_ref, state.upvalue_cells, nil) regs = :erlang.setelement(dest + 1, regs, v) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_get_global, dest, name} -> v = State.get_global(state, name) regs = :erlang.setelement(dest + 1, regs, v) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_get_field, dest, table_reg, name, name_hint} -> table_val = :erlang.element(table_reg + 1, regs) @@ -279,29 +290,29 @@ defmodule Lua.VM.Dispatcher do case data do %{^name => value} -> regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> case :erlang.map_get(:metatable, table) do nil -> regs = :erlang.setelement(dest + 1, regs, nil) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> {value, state} = - Executor.dispatcher_get_field(table_val, name, state, proto, name_hint) + Executor.dispatcher_get_field(table_val, name, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end end _ -> {value, state} = - Executor.dispatcher_get_field(table_val, name, state, proto, name_hint) + Executor.dispatcher_get_field(table_val, name, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end # ── Arithmetic ────────────────────────────────────────────────── @@ -320,16 +331,16 @@ defmodule Lua.VM.Dispatcher do sum = va + vb wrapped = if sum >= @min_int and sum <= @max_int, do: sum, else: Numeric.to_signed_int64(sum) regs = :erlang.setelement(dest + 1, regs, wrapped) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va + vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_binop(:add, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_binop(:add, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_subtract, dest, a, b, hint_a, hint_b} -> @@ -341,16 +352,16 @@ defmodule Lua.VM.Dispatcher do diff = va - vb wrapped = if diff >= @min_int and diff <= @max_int, do: diff, else: Numeric.to_signed_int64(diff) regs = :erlang.setelement(dest + 1, regs, wrapped) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va - vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_binop(:subtract, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_binop(:subtract, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_multiply, dest, a, b, hint_a, hint_b} -> @@ -362,16 +373,16 @@ defmodule Lua.VM.Dispatcher do prod = va * vb wrapped = if prod >= @min_int and prod <= @max_int, do: prod, else: Numeric.to_signed_int64(prod) regs = :erlang.setelement(dest + 1, regs, wrapped) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va * vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_binop(:multiply, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_binop(:multiply, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_divide, dest, a, b, hint_a, hint_b} -> @@ -380,14 +391,14 @@ defmodule Lua.VM.Dispatcher do :divide, :erlang.element(a + 1, regs), :erlang.element(b + 1, regs), - state, + sync(state, cs, cd), proto, hint_a, hint_b ) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_floor_divide, dest, a, b, hint_a, hint_b} -> {value, state} = @@ -395,14 +406,14 @@ defmodule Lua.VM.Dispatcher do :floor_divide, :erlang.element(a + 1, regs), :erlang.element(b + 1, regs), - state, + sync(state, cs, cd), proto, hint_a, hint_b ) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_modulo, dest, a, b, hint_a, hint_b} -> {value, state} = @@ -410,14 +421,14 @@ defmodule Lua.VM.Dispatcher do :modulo, :erlang.element(a + 1, regs), :erlang.element(b + 1, regs), - state, + sync(state, cs, cd), proto, hint_a, hint_b ) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_power, dest, a, b, hint_a, hint_b} -> {value, state} = @@ -425,21 +436,21 @@ defmodule Lua.VM.Dispatcher do :power, :erlang.element(a + 1, regs), :erlang.element(b + 1, regs), - state, + sync(state, cs, cd), proto, hint_a, hint_b ) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_negate, dest, src, hint} -> {value, state} = - Executor.dispatcher_unop(:negate, :erlang.element(src + 1, regs), state, proto, hint) + Executor.dispatcher_unop(:negate, :erlang.element(src + 1, regs), sync(state, cs, cd), proto, hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Bitwise ───────────────────────────────────────────────────── # @@ -463,11 +474,11 @@ defmodule Lua.VM.Dispatcher do if is_integer(va) and is_integer(vb) do regs = :erlang.setelement(dest + 1, regs, Numeric.to_signed_int64(Bitwise.band(va, vb))) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) else - {value, state} = Executor.dispatcher_bitwise(:band, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_bitwise(:band, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_bitwise_or, dest, a, b, hint_a, hint_b} -> @@ -476,11 +487,11 @@ defmodule Lua.VM.Dispatcher do if is_integer(va) and is_integer(vb) do regs = :erlang.setelement(dest + 1, regs, Numeric.to_signed_int64(Bitwise.bor(va, vb))) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) else - {value, state} = Executor.dispatcher_bitwise(:bor, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_bitwise(:bor, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_bitwise_xor, dest, a, b, hint_a, hint_b} -> @@ -489,32 +500,32 @@ defmodule Lua.VM.Dispatcher do if is_integer(va) and is_integer(vb) do regs = :erlang.setelement(dest + 1, regs, Numeric.to_signed_int64(Bitwise.bxor(va, vb))) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) else - {value, state} = Executor.dispatcher_bitwise(:bxor, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_bitwise(:bxor, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_shift_left, dest, a, b, hint_a, hint_b} -> va = :erlang.element(a + 1, regs) vb = :erlang.element(b + 1, regs) - {value, state} = Executor.dispatcher_bitwise(:shl, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_bitwise(:shl, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_shift_right, dest, a, b, hint_a, hint_b} -> va = :erlang.element(a + 1, regs) vb = :erlang.element(b + 1, regs) - {value, state} = Executor.dispatcher_bitwise(:shr, va, vb, state, proto, hint_a, hint_b) + {value, state} = Executor.dispatcher_bitwise(:shr, va, vb, sync(state, cs, cd), proto, hint_a, hint_b) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_bitwise_not, dest, src, hint} -> val = :erlang.element(src + 1, regs) - {value, state} = Executor.dispatcher_bnot(val, state, proto, hint) + {value, state} = Executor.dispatcher_bnot(val, sync(state, cs, cd), proto, hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Comparisons ───────────────────────────────────────────────── @@ -525,16 +536,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va < vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va < vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:less_than, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:less_than, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_less_equal, dest, a, b} -> @@ -544,16 +555,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va <= vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va <= vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:less_equal, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:less_equal, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_greater_than, dest, a, b} -> @@ -563,16 +574,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va > vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va > vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:greater_than, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:greater_than, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_greater_equal, dest, a, b} -> @@ -582,16 +593,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va >= vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va >= vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:greater_equal, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:greater_equal, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_equal, dest, a, b} -> @@ -601,16 +612,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va == vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va == vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:equal, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:equal, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_not_equal, dest, a, b} -> @@ -620,16 +631,16 @@ defmodule Lua.VM.Dispatcher do cond do is_number(va) and is_number(vb) -> regs = :erlang.setelement(dest + 1, regs, va != vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) is_binary(va) and is_binary(vb) -> regs = :erlang.setelement(dest + 1, regs, va != vb) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) true -> - {value, state} = Executor.dispatcher_cmp(:not_equal, va, vb, state, proto) + {value, state} = Executor.dispatcher_cmp(:not_equal, va, vb, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_not, dest, src} -> @@ -638,7 +649,7 @@ defmodule Lua.VM.Dispatcher do # values. Saves a function call per `:not` opcode. result = v === nil or v === false regs = :erlang.setelement(dest + 1, regs, result) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Conditional branching ─────────────────────────────────────── # @@ -657,7 +668,7 @@ defmodule Lua.VM.Dispatcher do _ -> then_bc end - dispatch(branch, 1, regs, upvalues, proto, state, [{code, pc + 1} | cont], frames, instruction_count) + dispatch(branch, 1, regs, upvalues, proto, state, [{code, pc + 1} | cont], frames, instruction_count, cs, cd, ou) # ── Calls ─────────────────────────────────────────────────────── # @@ -679,24 +690,17 @@ defmodule Lua.VM.Dispatcher do case func_value do {:compiled_closure, callee_proto, callee_upvalues} -> callee_regs = init_callee_regs(callee_proto, regs, base + 1, arg_count) - # B5c-v2: compiled callees may now be vararg functions. The - # `is_vararg` check in `setup_vararg_proto/4` short-circuits for - # the common non-vararg case at one tuple-field read. - callee_proto = setup_vararg_proto(callee_proto, regs, base + 1, arg_count) - - frame = - {code, pc + 1, regs, upvalues, proto, cont, :discard, state.open_upvalues} - - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) - - state = %{ - state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, - open_upvalues: %{} - } + # Compiled callees may be vararg functions. Testing `is_vararg` + # here keeps the common non-vararg call to one field read. + callee_proto = + if callee_proto.is_vararg, + do: setup_vararg_proto(callee_proto, regs, base + 1, arg_count), + else: callee_proto + + frame = {code, pc + 1, regs, upvalues, proto, cont, :discard, ou} + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) dispatch( callee_proto.bytecode, @@ -707,38 +711,40 @@ defmodule Lua.VM.Dispatcher do state, [], [frame | frames], - instruction_count + instruction_count, + [call_info | cs], + cd + 1, + %{} ) {:lua_closure, _, _} = closure -> args = collect_args(regs, base + 1, arg_count) - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) state = %{ state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, + | call_stack: [call_info | cs], + call_depth: cd + 1, instruction_count: instruction_count } {_results, state} = Executor.call_function(closure, args, state) instruction_count = state.instruction_count - state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1} - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> args = collect_args(regs, base + 1, arg_count) - state = %{state | instruction_count: instruction_count} + state = %{state | call_stack: cs, call_depth: cd, instruction_count: instruction_count} {_results, state} = Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) instruction_count = state.instruction_count - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_call_one, base, arg_count, name_hint, line} -> @@ -747,24 +753,19 @@ defmodule Lua.VM.Dispatcher do case func_value do {:compiled_closure, callee_proto, callee_upvalues} -> callee_regs = init_callee_regs(callee_proto, regs, base + 1, arg_count) - callee_proto = setup_vararg_proto(callee_proto, regs, base + 1, arg_count) + + callee_proto = + if callee_proto.is_vararg, + do: setup_vararg_proto(callee_proto, regs, base + 1, arg_count), + else: callee_proto # Frame is a tuple, not a map: pattern-matching a tuple in - # `return_one/3` skips Map.fetch! lookups and lets the BEAM + # `return_one/7` skips Map.fetch! lookups and lets the BEAM # bind everything in a single `move` per slot. - frame = - {code, pc + 1, regs, upvalues, proto, cont, base, state.open_upvalues} - - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) - - state = %{ - state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, - open_upvalues: %{} - } + frame = {code, pc + 1, regs, upvalues, proto, cont, base, ou} + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) dispatch( callee_proto.bytecode, @@ -775,25 +776,27 @@ defmodule Lua.VM.Dispatcher do state, [], [frame | frames], - instruction_count + instruction_count, + [call_info | cs], + cd + 1, + %{} ) {:lua_closure, _, _} = closure -> args = collect_args(regs, base + 1, arg_count) - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) state = %{ state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, + | call_stack: [call_info | cs], + call_depth: cd + 1, instruction_count: instruction_count } {results, state} = Executor.call_function(closure, args, state) instruction_count = state.instruction_count - state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1} first = case results do @@ -802,12 +805,12 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(base + 1, regs, first) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> args = collect_args(regs, base + 1, arg_count) - state = %{state | instruction_count: instruction_count} + state = %{state | call_stack: cs, call_depth: cd, instruction_count: instruction_count} {results, state} = Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) @@ -821,7 +824,7 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(base + 1, regs, first) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end # ── Returns ───────────────────────────────────────────────────── @@ -834,10 +837,10 @@ defmodule Lua.VM.Dispatcher do # `call_function/3` contract. {@op_return_one, base} -> - return_one(:erlang.element(base + 1, regs), state, frames, instruction_count) + return_one(:erlang.element(base + 1, regs), state, frames, instruction_count, cs, cd, ou) {@op_return_zero} -> - return_one(nil, state, frames, instruction_count) + return_one(nil, state, frames, instruction_count, cs, cd, ou) # ── Table opcodes ─────────────────────────────────────────────── # @@ -849,7 +852,7 @@ defmodule Lua.VM.Dispatcher do {@op_new_table, dest} -> {tref, state} = State.alloc_table(state) regs = :erlang.setelement(dest + 1, regs, tref) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_get_table, dest, table_reg, key_reg, name_hint} -> table_val = :erlang.element(table_reg + 1, regs) @@ -864,19 +867,19 @@ defmodule Lua.VM.Dispatcher do case :erlang.map_get(:metatable, table) do nil -> regs = :erlang.setelement(dest + 1, regs, nil) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> {value, state} = - Executor.dispatcher_get_table(table_val, key, state, proto, name_hint) + Executor.dispatcher_get_table(table_val, key, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end value -> regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {:tref, id} when is_integer(key) or is_binary(key) -> @@ -886,43 +889,66 @@ defmodule Lua.VM.Dispatcher do case data do %{^key => value} -> regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> case :erlang.map_get(:metatable, table) do nil -> regs = :erlang.setelement(dest + 1, regs, nil) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> {value, state} = - Executor.dispatcher_get_table(table_val, key, state, proto, name_hint) + Executor.dispatcher_get_table(table_val, key, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end end _ -> {value, state} = - Executor.dispatcher_get_table(table_val, key, state, proto, name_hint) + Executor.dispatcher_get_table(table_val, key, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end {@op_set_table, table_reg, key_reg, value_reg, name_hint} -> table_val = :erlang.element(table_reg + 1, regs) key = :erlang.element(key_reg + 1, regs) value = :erlang.element(value_reg + 1, regs) - state = Executor.dispatcher_set_table(table_val, key, value, state, proto, name_hint) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) - + state = Executor.dispatcher_set_table(table_val, key, value, sync(state, cs, cd), proto, name_hint) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) + + # Mirrors `@op_get_field`'s shape: a tref whose table has no metatable + # has no `__newindex` to consult, so the write is a direct + # `Table.put/3` into `state.tables`. Anything else — a non-tref, or a + # table carrying a metatable — bridges so the `__newindex` chain and + # the index type errors stay the interpreter's. {@op_set_field, table_reg, name, value_reg, name_hint} -> table_val = :erlang.element(table_reg + 1, regs) value = :erlang.element(value_reg + 1, regs) - state = Executor.dispatcher_set_field(table_val, name, value, state, proto, name_hint) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + + case table_val do + {:tref, id} -> + table = :erlang.map_get(id, state.tables) + + state = + case :erlang.map_get(:metatable, table) do + nil -> + %{state | tables: :maps.put(id, Table.put(table, name, value), state.tables)} + + _ -> + Executor.dispatcher_set_field(table_val, name, value, sync(state, cs, cd), proto, name_hint) + end + + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) + + # Indexing a non-table always raises; the bridge owns the wording. + _ -> + Executor.dispatcher_set_field(table_val, name, value, sync(state, cs, cd), proto, name_hint) + end # `:set_list` with a positive integer count is the table-constructor # form. The `count == 0` sentinel was filtered upstream and never @@ -935,7 +961,7 @@ defmodule Lua.VM.Dispatcher do set_list_into_table(table, regs, start, count, offset, 0) end) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # `:set_list` multi-return tail (`{f(), 1}`): fold the static prefix # `init_count` with the trailing values count the last multi-return @@ -950,7 +976,7 @@ defmodule Lua.VM.Dispatcher do set_list_into_table(table, regs, start, total, offset, 0) end) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_length, dest, source} -> value = :erlang.element(source + 1, regs) @@ -965,22 +991,22 @@ defmodule Lua.VM.Dispatcher do # without __len is the border length of the data map. len = Table.length(table) regs = :erlang.setelement(dest + 1, regs, len) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> - {len, state} = Executor.dispatcher_length(value, state, proto) + {len, state} = Executor.dispatcher_length(value, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, len) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end v when is_binary(v) -> regs = :erlang.setelement(dest + 1, regs, byte_size(v)) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) _ -> - {len, state} = Executor.dispatcher_length(value, state, proto) + {len, state} = Executor.dispatcher_length(value, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, len) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end # ── numeric_for ───────────────────────────────────────────────── @@ -997,7 +1023,7 @@ defmodule Lua.VM.Dispatcher do :erlang.element(base + 1, regs), :erlang.element(base + 2, regs), :erlang.element(base + 3, regs), - state + sync(state, cs, cd) ) regs = :erlang.setelement(base + 1, regs, counter) @@ -1009,12 +1035,26 @@ defmodule Lua.VM.Dispatcher do if should_continue do regs = :erlang.setelement(loop_var + 1, regs, counter) - state = Executor.dispatcher_close_open_upvalues_at_or_above(state, loop_var) + ou = close_upv(ou, loop_var) marker = {:cps_for, base, loop_var, body_bc, code, pc + 1} loop_exit = {:loop_exit, code, pc + 1} - dispatch(body_bc, 1, regs, upvalues, proto, state, [marker, loop_exit | cont], frames, instruction_count) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [marker, loop_exit | cont], + frames, + instruction_count, + cs, + cd, + ou + ) else - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end # ── while_loop / repeat_loop / generic_for ───────────────────── @@ -1027,12 +1067,12 @@ defmodule Lua.VM.Dispatcher do {@op_while_loop, test_reg, cond_bc, body_bc} -> cps = {:cps_while_test, test_reg, cond_bc, body_bc, code, pc + 1} loop_exit = {:loop_exit, code, pc + 1} - dispatch(cond_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | cont], frames, instruction_count) + dispatch(cond_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | cont], frames, instruction_count, cs, cd, ou) {@op_repeat_loop, test_reg, body_bc, cond_bc} -> cps = {:cps_repeat_body, test_reg, body_bc, cond_bc, code, pc + 1} loop_exit = {:loop_exit, code, pc + 1} - dispatch(body_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | cont], frames, instruction_count) + dispatch(body_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | cont], frames, instruction_count, cs, cd, ou) {@op_generic_for, base, var_regs, body_bc, line} -> # Iterator call follows the same shape as the executor: @@ -1045,7 +1085,7 @@ defmodule Lua.VM.Dispatcher do invariant_state = :erlang.element(base + 2, regs) control = :erlang.element(base + 3, regs) - state = %{state | instruction_count: instruction_count} + state = %{state | call_stack: cs, call_depth: cd, instruction_count: instruction_count} {results, state} = Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state, line) @@ -1054,19 +1094,33 @@ defmodule Lua.VM.Dispatcher do case results do [nil | _] -> - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) [] -> - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) [first | _] -> regs = :erlang.setelement(base + 3, regs, first) 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) + ou = close_upv(ou, first_var_reg) 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, instruction_count) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [marker, loop_exit | cont], + frames, + instruction_count, + cs, + cd, + ou + ) end # ── break ───────────────────────────────────────────────────── @@ -1077,7 +1131,7 @@ defmodule Lua.VM.Dispatcher do {@op_break} -> {exit_code, exit_pc, rest_cont} = find_loop_exit(cont) - dispatch(exit_code, exit_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(exit_code, exit_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) # ── label / goto ────────────────────────────────────────────── # @@ -1089,16 +1143,16 @@ defmodule Lua.VM.Dispatcher do # enclosing `code` recorded on the unwound markers. {@op_label, _name, _level} -> - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_goto, 0, target_pc, level} -> - state = Executor.dispatcher_close_open_upvalues_at_or_above(state, level) - dispatch(code, target_pc, regs, upvalues, proto, state, cont, frames, instruction_count) + ou = close_upv(ou, level) + dispatch(code, target_pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_goto, depth, target_pc, level} -> - state = Executor.dispatcher_close_open_upvalues_at_or_above(state, level) + ou = close_upv(ou, level) {dest_code, rest_cont} = unwind_goto(cont, depth) - dispatch(dest_code, target_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(dest_code, target_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) # ── Closure construction ────────────────────────────────────── # @@ -1114,7 +1168,7 @@ defmodule Lua.VM.Dispatcher do {@op_closure, dest, proto_index} -> nested_proto = Enum.at(proto.prototypes, proto_index) - {cells, state} = build_upvalues(nested_proto.upvalue_descriptors, regs, upvalues, state, []) + {cells, state, ou} = build_upvalues(nested_proto.upvalue_descriptors, regs, upvalues, state, ou, []) upvalues_tuple = List.to_tuple(:lists.reverse(cells)) closure = @@ -1124,7 +1178,7 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(dest + 1, regs, closure) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Upvalue access ──────────────────────────────────────────── # @@ -1139,21 +1193,21 @@ defmodule Lua.VM.Dispatcher do cell_ref = :erlang.element(index + 1, upvalues) value = :erlang.element(source + 1, regs) state = %{state | upvalue_cells: Map.put(state.upvalue_cells, cell_ref, value)} - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_get_open_upvalue, dest, reg} -> value = - case Map.get(state.open_upvalues, reg) do + case :maps.get(reg, ou, nil) do nil -> :erlang.element(reg + 1, regs) - cell_ref -> Map.get(state.upvalue_cells, cell_ref) + cell_ref -> :maps.get(cell_ref, state.upvalue_cells, nil) end regs = :erlang.setelement(dest + 1, regs, value) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_set_open_upvalue, reg, source} -> state = - case Map.get(state.open_upvalues, reg) do + case :maps.get(reg, ou, nil) do nil -> state @@ -1162,11 +1216,11 @@ defmodule Lua.VM.Dispatcher do %{state | upvalue_cells: Map.put(state.upvalue_cells, cell_ref, value)} end - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_close_upvalues, threshold} -> - state = Executor.dispatcher_close_open_upvalues_at_or_above(state, threshold) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + ou = close_upv(ou, threshold) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Vararg ──────────────────────────────────────────────────── # @@ -1181,25 +1235,25 @@ defmodule Lua.VM.Dispatcher do varargs = proto.varargs {regs, n} = write_varargs(regs, base, varargs, 0) state = %{state | multi_return_count: n} - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) {@op_vararg, base, count} -> regs = write_varargs_n(regs, base, proto.varargs, count) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Multi-return returns ────────────────────────────────────── {@op_return_proto_varargs} -> - return_multi(proto.varargs, state, frames, instruction_count) + return_multi(proto.varargs, state, frames, instruction_count, cs, cd, ou) {@op_return_collect, base, fixed} -> total = fixed + state.multi_return_count results = collect_args(regs, base, total) - return_multi(results, state, frames, instruction_count) + return_multi(results, state, frames, instruction_count, cs, cd, ou) {@op_return_multi, base, count} -> results = collect_args(regs, base, count) - return_multi(results, state, frames, instruction_count) + return_multi(results, state, frames, instruction_count, cs, cd, ou) # ── Multi-return calls ──────────────────────────────────────── # @@ -1227,7 +1281,12 @@ defmodule Lua.VM.Dispatcher do case func_value do {:compiled_closure, callee_proto, callee_upvalues} -> callee_regs = init_callee_regs(callee_proto, regs, base + 1, total_args) - callee_proto = setup_vararg_proto(callee_proto, regs, base + 1, total_args) + + callee_proto = + if callee_proto.is_vararg, + do: setup_vararg_proto(callee_proto, regs, base + 1, total_args), + else: callee_proto + # Reuse the fast-path frame shapes when result_count is 0 # (discard) or 1 (single integer base). Only the genuine # multi-return shapes (-1, -2, n > 1) need the tagged @@ -1239,17 +1298,10 @@ defmodule Lua.VM.Dispatcher do _ -> {:multi, base, result_count} end - frame = {code, pc + 1, regs, upvalues, proto, cont, dest, state.open_upvalues} - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) - - state = %{ - state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, - open_upvalues: %{} - } + frame = {code, pc + 1, regs, upvalues, proto, cont, dest, ou} + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) dispatch( callee_proto.bytecode, @@ -1260,25 +1312,27 @@ defmodule Lua.VM.Dispatcher do state, [], [frame | frames], - instruction_count + instruction_count, + [call_info | cs], + cd + 1, + %{} ) {:lua_closure, _, _} = closure -> args = collect_args(regs, base + 1, total_args) - call_info = Executor.dispatcher_call_info(proto, name_hint, 0) - instruction_count = State.tick!(state, instruction_count) - State.check_call_depth!(state) + call_info = {proto.source, 0, name_hint} + instruction_count = tick(state, instruction_count, cs, cd) + ckdepth(state, cs, cd) state = %{ state - | call_stack: [call_info | state.call_stack], - call_depth: state.call_depth + 1, + | call_stack: [call_info | cs], + call_depth: cd + 1, instruction_count: instruction_count } {results, state} = Executor.call_function(closure, args, state) instruction_count = state.instruction_count - state = %{state | call_stack: tl(state.call_stack), call_depth: state.call_depth - 1} apply_multi_call_result( result_count, @@ -1292,13 +1346,16 @@ defmodule Lua.VM.Dispatcher do state, cont, frames, - instruction_count + instruction_count, + cs, + cd, + ou ) _ -> args = collect_args(regs, base + 1, total_args) - state = %{state | instruction_count: instruction_count} + state = %{state | call_stack: cs, call_depth: cd, instruction_count: instruction_count} {results, state} = Executor.dispatcher_call_function(func_value, args, state, proto, name_hint, line) @@ -1317,7 +1374,10 @@ defmodule Lua.VM.Dispatcher do state, cont, frames, - instruction_count + instruction_count, + cs, + cd, + ou ) end @@ -1331,10 +1391,10 @@ defmodule Lua.VM.Dispatcher do {@op_self, base, obj_reg, method_name, name_hint} -> obj = :erlang.element(obj_reg + 1, regs) - {func, state} = Executor.dispatcher_index_method_target(obj, method_name, state, proto, name_hint) + {func, state} = Executor.dispatcher_index_method_target(obj, method_name, sync(state, cs, cd), proto, name_hint) regs = :erlang.setelement(base + 2, regs, obj) regs = :erlang.setelement(base + 1, regs, func) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) # ── Concatenation ───────────────────────────────────────────── # @@ -1352,19 +1412,30 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(dest + 1, regs, left <> right) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) else - {result, state} = Executor.dispatcher_concat(left, right, state, proto) + {result, state} = Executor.dispatcher_concat(left, right, sync(state, cs, cd), proto) regs = :erlang.setelement(dest + 1, regs, result) - dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end end end # ── End-of-body handling ──────────────────────────────────────────────── - defp finish_body(regs, upvalues, proto, state, [{next_code, next_pc} | rest_cont], frames, instruction_count) do - dispatch(next_code, next_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + defp finish_body( + regs, + upvalues, + proto, + state, + [{next_code, next_pc} | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) do + dispatch(next_code, next_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) end # `:numeric_for` body ran to completion. Increment the counter, re-test, @@ -1381,7 +1452,10 @@ defmodule Lua.VM.Dispatcher do state, [{:cps_for, base, loop_var, body_bc, outer_code, outer_pc} = marker, {:loop_exit, _, _} = loop_exit | rest_cont], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do counter = :erlang.element(base + 1, regs) step = :erlang.element(base + 3, regs) @@ -1391,12 +1465,26 @@ defmodule Lua.VM.Dispatcher do should_continue = if step > 0, do: new_counter <= limit, else: new_counter >= limit if should_continue do - instruction_count = State.tick!(state, instruction_count) + instruction_count = tick(state, instruction_count, cs, cd) regs = :erlang.setelement(loop_var + 1, regs, new_counter) - state = Executor.dispatcher_close_open_upvalues_at_or_above(state, loop_var) - dispatch(body_bc, 1, regs, upvalues, proto, state, [marker, loop_exit | rest_cont], frames, instruction_count) + ou = close_upv(ou, loop_var) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [marker, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) else - dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) end end @@ -1412,15 +1500,32 @@ defmodule Lua.VM.Dispatcher do {:loop_exit, _, _} = loop_exit | rest_cont ], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do case :erlang.element(test_reg + 1, regs) do v when v === nil or v === false -> - dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) _ -> cps = {:cps_while_body, test_reg, cond_bc, body_bc, outer_code, outer_pc} - dispatch(body_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | rest_cont], frames, instruction_count) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [cps, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) end end @@ -1436,11 +1541,28 @@ defmodule Lua.VM.Dispatcher do {:loop_exit, _, _} = loop_exit | rest_cont ], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do - instruction_count = State.tick!(state, instruction_count) + instruction_count = tick(state, instruction_count, cs, cd) cps = {:cps_while_test, test_reg, cond_bc, body_bc, outer_code, outer_pc} - dispatch(cond_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | rest_cont], frames, instruction_count) + + dispatch( + cond_bc, + 1, + regs, + upvalues, + proto, + state, + [cps, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) end # `:repeat_loop`: body just finished. Run the condition next. @@ -1454,10 +1576,27 @@ defmodule Lua.VM.Dispatcher do {:loop_exit, _, _} = loop_exit | rest_cont ], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do cps = {:cps_repeat_cond, test_reg, body_bc, cond_bc, outer_code, outer_pc} - dispatch(cond_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | rest_cont], frames, instruction_count) + + dispatch( + cond_bc, + 1, + regs, + upvalues, + proto, + state, + [cps, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) end # `:repeat_loop`: condition just finished. test_reg truthy = exit (Lua's @@ -1472,16 +1611,33 @@ defmodule Lua.VM.Dispatcher do {:loop_exit, _, _} = loop_exit | rest_cont ], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do case :erlang.element(test_reg + 1, regs) do v when v === nil or v === false -> - instruction_count = State.tick!(state, instruction_count) + instruction_count = tick(state, instruction_count, cs, cd) cps = {:cps_repeat_body, test_reg, body_bc, cond_bc, outer_code, outer_pc} - dispatch(body_bc, 1, regs, upvalues, proto, state, [cps, loop_exit | rest_cont], frames, instruction_count) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [cps, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) _ -> - dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) end end @@ -1496,13 +1652,16 @@ defmodule Lua.VM.Dispatcher do {:loop_exit, _, _} = loop_exit | rest_cont ], frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do iter_func = :erlang.element(base + 1, regs) invariant_state = :erlang.element(base + 2, regs) control = :erlang.element(base + 3, regs) - state = %{state | instruction_count: instruction_count} + state = %{state | call_stack: cs, call_depth: cd, instruction_count: instruction_count} {results, state} = Executor.dispatcher_call_value(iter_func, [invariant_state, control], proto, state, line) @@ -1511,18 +1670,32 @@ defmodule Lua.VM.Dispatcher do case results do [nil | _] -> - dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) [] -> - dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count) + dispatch(outer_code, outer_pc, regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) [first | _] -> - instruction_count = State.tick!(state, instruction_count) + instruction_count = tick(state, instruction_count, cs, cd) regs = :erlang.setelement(base + 3, regs, first) 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) - dispatch(body_bc, 1, regs, upvalues, proto, state, [marker, loop_exit | rest_cont], frames, instruction_count) + ou = close_upv(ou, first_var_reg) + + dispatch( + body_bc, + 1, + regs, + upvalues, + proto, + state, + [marker, loop_exit | rest_cont], + frames, + instruction_count, + cs, + cd, + ou + ) end end @@ -1530,8 +1703,8 @@ defmodule Lua.VM.Dispatcher do # body ran past its last instruction with the loop_exit still on top). # Drop the loop_exit and let the next iteration of finish_body see the # cont below it. - defp finish_body(regs, upvalues, proto, state, [{:loop_exit, _, _} | rest_cont], frames, instruction_count) do - finish_body(regs, upvalues, proto, state, rest_cont, frames, instruction_count) + defp finish_body(regs, upvalues, proto, state, [{:loop_exit, _, _} | rest_cont], frames, instruction_count, cs, cd, ou) do + finish_body(regs, upvalues, proto, state, rest_cont, frames, instruction_count, cs, cd, ou) end # Body exhausted with no continuation: prototype ran off the end. Lua @@ -1539,8 +1712,8 @@ defmodule Lua.VM.Dispatcher do # values when control falls off the end, not a single `nil` — the # caller's `result_count` decides how that's projected (nil for a # single-value site, empty slot for a multi-return one). - defp finish_body(_regs, _upvalues, _proto, state, [], frames, instruction_count) do - return_multi([], state, frames, instruction_count) + defp finish_body(_regs, _upvalues, _proto, state, [], frames, instruction_count, cs, cd, ou) do + return_multi([], state, frames, instruction_count, cs, cd, ou) end # ── Return propagation through frames ─────────────────────────────────── @@ -1559,29 +1732,33 @@ defmodule Lua.VM.Dispatcher do # {:multi, B, -2} → expand all into regs[B..], set multi_return_count. # {:multi, B, n>1} → write n results into regs[B..], pad nil. - defp return_one(value, state, [], instruction_count) do - # Top of this dispatcher sub-evaluation: stamp the tally back into the - # state so a caller in the other engine can resume the same budget. - {[value], %{state | instruction_count: instruction_count}} + defp return_one(value, state, [], instruction_count, cs, cd, _ou) do + # Top of this dispatcher sub-evaluation: stamp the loop-carried control + # parameters back into the state so a caller in the other engine + # resumes the same budget and the same call stack. A bridge out of the + # loop may have left a deeper stack stamped in the struct; `cs`/`cd` + # have unwound back to their entry values, so this restores them. + {[value], %{state | instruction_count: instruction_count, call_stack: cs, call_depth: cd}} end - defp return_one(value, state, [frame | rest_frames], instruction_count) do - {code, pc, regs, upvalues, proto, cont, dest, saved_open} = frame + defp return_one(value, state, [frame | rest_frames], instruction_count, cs, cd, _ou) do + {code, pc, regs, upvalues, proto, cont, dest, ou} = frame # Every dispatcher frame corresponds to a Lua-level call that pushed a # call_stack entry. Pop it on the way out — the interpreter's # `do_frame_return/6` does the same at executor.ex:1767. - state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack), call_depth: state.call_depth - 1} + cs = tl(cs) + cd = cd - 1 case dest do :discard -> - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) n when is_integer(n) -> regs = :erlang.setelement(n + 1, regs, value) - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) {:multi, _, -1} -> - return_one(value, state, rest_frames, instruction_count) + return_one(value, state, rest_frames, instruction_count, cs, cd, ou) {:multi, base, -2} -> # The expansion dest may sit past the statically reserved register @@ -1591,31 +1768,32 @@ defmodule Lua.VM.Dispatcher do regs = grow_regs(regs, base + 1) regs = :erlang.setelement(base + 1, regs, value) state = %{state | multi_return_count: 1} - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) {:multi, base, n} when is_integer(n) and n > 1 -> regs = grow_regs(regs, base + n) regs = :erlang.setelement(base + 1, regs, value) regs = pad_nils(regs, base + 1, n - 1) - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) end end # List-return path for `:return_multi`, `:return_collect`, # `:return_proto_varargs`, and the non-compiled-callee branch of - # `:call_multi`. Mirrors `return_one/3`'s frame-variant handling. + # `:call_multi`. Mirrors `return_one/7`'s frame-variant handling. - defp return_multi(results, state, [], instruction_count) do - {results, %{state | instruction_count: instruction_count}} + defp return_multi(results, state, [], instruction_count, cs, cd, _ou) do + {results, %{state | instruction_count: instruction_count, call_stack: cs, call_depth: cd}} end - defp return_multi(results, state, [frame | rest_frames], instruction_count) do - {code, pc, regs, upvalues, proto, cont, dest, saved_open} = frame - state = %{state | open_upvalues: saved_open, call_stack: tl(state.call_stack), call_depth: state.call_depth - 1} + defp return_multi(results, state, [frame | rest_frames], instruction_count, cs, cd, _ou) do + {code, pc, regs, upvalues, proto, cont, dest, ou} = frame + cs = tl(cs) + cd = cd - 1 case dest do :discard -> - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) n when is_integer(n) -> v = @@ -1625,24 +1803,68 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(n + 1, regs, v) - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) {:multi, _, -1} -> - return_multi(results, state, rest_frames, instruction_count) + return_multi(results, state, rest_frames, instruction_count, cs, cd, ou) {:multi, base, -2} -> regs = write_results(regs, base, results) state = %{state | multi_return_count: length(results)} - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) {:multi, base, n} when is_integer(n) and n > 1 -> regs = write_results_n(regs, base, results, n) - dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, rest_frames, instruction_count, cs, cd, ou) end end # ── Helpers ───────────────────────────────────────────────────────────── + # Writes the loop-carried control fields back into the struct. Called + # wherever execution leaves the dispatch loop — bridges into `Executor`, + # native callbacks, the loop's own raise sites — so anything that reads + # `state.call_stack` / `state.call_depth` out there sees the live values. + # + # The first clause makes a repeat crossing free: a loop body that bridges + # every iteration without calling anything (a `t[k] = v` write, an + # `__index` read) leaves the struct already carrying this frame's stack, + # so only the first crossing rebuilds it. + @compile {:inline, sync: 3} + defp sync(%{call_stack: call_stack, call_depth: call_depth} = state, call_stack, call_depth), do: state + + defp sync(state, call_stack, call_depth) do + %{state | call_stack: call_stack, call_depth: call_depth} + end + + # Hot-path guards. Each of these fires on every call or loop iteration + # only to discover it has nothing to do under the default configuration. + # Inlining the guard here keeps the no-op case to a single function-head + # match instead of a cross-module call into `Executor`/`State`; the + # non-trivial case still routes to the canonical implementation (paying + # one `sync/3` on the way) so the behaviour and its error shapes live in + # one place. + @compile {:inline, close_upv: 2, tick: 4, ckdepth: 3} + + defp close_upv(open_upvalues, _threshold) when map_size(open_upvalues) == 0, do: open_upvalues + + defp close_upv(open_upvalues, threshold) do + :maps.filter(fn reg, _cell -> reg < threshold end, open_upvalues) + end + + defp tick(%{max_instructions: :infinity}, instruction_count, _call_stack, _call_depth), do: instruction_count + + defp tick(state, instruction_count, call_stack, call_depth) do + State.tick!(sync(state, call_stack, call_depth), instruction_count) + end + + defp ckdepth(%{max_call_depth: :infinity}, _call_stack, _call_depth), do: :ok + defp ckdepth(%{max_call_depth: max}, _call_stack, call_depth) when call_depth < max, do: :ok + + defp ckdepth(state, call_stack, call_depth) do + State.check_call_depth!(sync(state, call_stack, call_depth)) + end + defp clear_nils(regs, _dest, 0), do: regs defp clear_nils(regs, dest, n) do @@ -1735,47 +1957,42 @@ defmodule Lua.VM.Dispatcher do # Vararg setup at the call boundary. Mirrors the executor's per-call # behaviour: when calling a vararg function, regs[param_count..total_args) # become the varargs list carried on `%{proto | varargs: ...}`. + # + # Only reached when the callee is actually vararg — call sites test + # `callee_proto.is_vararg` inline so a non-vararg call pays one field + # read rather than a call into a function that rebuilds nothing. The + # vararg case still copies the whole `%Prototype{}`. defp setup_vararg_proto(callee_proto, src_regs, src_off, total_args) do - if callee_proto.is_vararg do - param_count = callee_proto.param_count - vararg_count = max(total_args - param_count, 0) - varargs = collect_args(src_regs, src_off + param_count, vararg_count) - %{callee_proto | varargs: varargs} - else - callee_proto - end + param_count = callee_proto.param_count + vararg_count = max(total_args - param_count, 0) + varargs = collect_args(src_regs, src_off + param_count, vararg_count) + %{callee_proto | varargs: varargs} end # Closure upvalue capture. `:parent_local` allocates (or reuses) an # open-upvalue cell so multiple closures over the same register share # mutation. `:parent_upvalue` forwards our own cell ref to the child. - defp build_upvalues([], _regs, _upvalues, state, acc), do: {acc, state} + defp build_upvalues([], _regs, _upvalues, state, open_upvalues, acc), do: {acc, state, open_upvalues} - defp build_upvalues([{:parent_local, reg, _name} | rest], regs, upvalues, state, acc) do - {cell_ref, state} = - case Map.get(state.open_upvalues, reg) do + defp build_upvalues([{:parent_local, reg, _name} | rest], regs, upvalues, state, open_upvalues, acc) do + {cell_ref, state, open_upvalues} = + case :maps.get(reg, open_upvalues, nil) do nil -> new_ref = make_ref() value = :erlang.element(reg + 1, regs) - - new_state = %{ - state - | upvalue_cells: Map.put(state.upvalue_cells, new_ref, value), - open_upvalues: Map.put(state.open_upvalues, reg, new_ref) - } - - {new_ref, new_state} + state = %{state | upvalue_cells: Map.put(state.upvalue_cells, new_ref, value)} + {new_ref, state, :maps.put(reg, new_ref, open_upvalues)} existing_cell -> - {existing_cell, state} + {existing_cell, state, open_upvalues} end - build_upvalues(rest, regs, upvalues, state, [cell_ref | acc]) + build_upvalues(rest, regs, upvalues, state, open_upvalues, [cell_ref | acc]) end - defp build_upvalues([{:parent_upvalue, index, _name} | rest], regs, upvalues, state, acc) do + defp build_upvalues([{:parent_upvalue, index, _name} | rest], regs, upvalues, state, open_upvalues, acc) do cell_ref = :erlang.element(index + 1, upvalues) - build_upvalues(rest, regs, upvalues, state, [cell_ref | acc]) + build_upvalues(rest, regs, upvalues, state, open_upvalues, [cell_ref | acc]) end # `:vararg` count=0 form: write every vararg into regs[base..] @@ -1891,12 +2108,31 @@ defmodule Lua.VM.Dispatcher do state, cont, frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do - dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end - defp apply_multi_call_result(1, base, results, code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) do + defp apply_multi_call_result( + 1, + base, + results, + code, + pc, + regs, + upvalues, + proto, + state, + cont, + frames, + instruction_count, + cs, + cd, + ou + ) do first = case results do [v | _] -> v @@ -1904,7 +2140,7 @@ defmodule Lua.VM.Dispatcher do end regs = :erlang.setelement(base + 1, regs, first) - dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end defp apply_multi_call_result( @@ -1919,21 +2155,56 @@ defmodule Lua.VM.Dispatcher do state, _cont, frames, - instruction_count + instruction_count, + cs, + cd, + ou ) do - return_multi(results, state, frames, instruction_count) + return_multi(results, state, frames, instruction_count, cs, cd, ou) end - defp apply_multi_call_result(-2, base, results, code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) do + defp apply_multi_call_result( + -2, + base, + results, + code, + pc, + regs, + upvalues, + proto, + state, + cont, + frames, + instruction_count, + cs, + cd, + ou + ) do regs = write_results(regs, base, results) state = %{state | multi_return_count: length(results)} - dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end - defp apply_multi_call_result(n, base, results, code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) + defp apply_multi_call_result( + n, + base, + results, + code, + pc, + regs, + upvalues, + proto, + state, + cont, + frames, + instruction_count, + cs, + cd, + ou + ) when is_integer(n) and n > 1 do regs = write_results_n(regs, base, results, n) - dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count) + dispatch(code, pc, regs, upvalues, proto, state, cont, frames, instruction_count, cs, cd, ou) end # Lazy regs-tuple growth. Used at the points where multi-return diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index 71536bc..4aa879f 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -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 @@ -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 @@ -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 @@ -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() @@ -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 @@ -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) @@ -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 @@ -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) @@ -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) diff --git a/lib/lua/vm/state.ex b/lib/lua/vm/state.ex index 248437f..5a70773 100644 --- a/lib/lua/vm/state.ex +++ b/lib/lua/vm/state.ex @@ -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 diff --git a/lib/lua/vm/table.ex b/lib/lua/vm/table.ex index 5116c5a..d644f70 100644 --- a/lib/lua/vm/table.ex +++ b/lib/lua/vm/table.ex @@ -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 @@ -198,6 +202,27 @@ 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. @@ -205,7 +230,7 @@ defmodule Lua.VM.Table do 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)) diff --git a/test/lua/call_function_error_value_test.exs b/test/lua/call_function_error_value_test.exs index 2a8271a..33a584b 100644 --- a/test/lua/call_function_error_value_test.exs +++ b/test/lua/call_function_error_value_test.exs @@ -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 diff --git a/test/lua/vm/dispatcher_test.exs b/test/lua/vm/dispatcher_test.exs index 79507ed..038b2c1 100644 --- a/test/lua/vm/dispatcher_test.exs +++ b/test/lua/vm/dispatcher_test.exs @@ -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!(""" diff --git a/test/lua/vm/require_open_upvalue_test.exs b/test/lua/vm/require_open_upvalue_test.exs index f92ef30..b328be6 100644 --- a/test/lua/vm/require_open_upvalue_test.exs +++ b/test/lua/vm/require_open_upvalue_test.exs @@ -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