From 5fc5fdde8ab4a4b9f91c476e9bde050f89be2cc1 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:12:57 +0900 Subject: [PATCH 01/21] perf(debugger): install checkpoints only while controls are enabled A private execution copy used to carry its trap instructions permanently, so every line boundary, call, return and loop back-edge paid a second dispatch even with no breakpoint set. Measure: a loop-heavy chunk ran 1.4-1.6x slower with an attached but idle debugger. The copy now stores its trap positions and patches or restores them when controls change. A disabled copy is byte-for-byte identical to the prototype, so idle debugging adds no per-instruction work (measured 1.00-1.06x). Stops no longer rescan every prototype's line table: breakpoint membership is cached per copy and refreshed only when breakpoints or sources change. Per-stop cost with 1,001 executed prototypes drops from 0.48-0.65 ms to 0.035 ms. Also names the reserved trap opcodes, adds IsDebugTrap, bounds-checks the ForPrep header probe and keeps Instruction.ToString from indexing past OpNames for a trap value. --- src/Lua.Debugger/LuaDebugger.Code.cs | 90 ++++++++++++++----- src/Lua.Debugger/LuaDebugger.Sources.cs | 5 ++ src/Lua/Runtime/Instruction.cs | 5 +- src/Lua/Runtime/LuaDebugCode.cs | 87 ++++++++++++------ src/Lua/Runtime/LuaVirtualMachine.Debug.cs | 7 ++ src/Lua/Runtime/LuaVirtualMachine.cs | 5 +- tests/Lua.Debugger.Tests/DebuggerTrapTests.cs | 24 +++++ 7 files changed, 171 insertions(+), 52 deletions(-) create mode 100644 tests/Lua.Debugger.Tests/DebuggerTrapTests.cs diff --git a/src/Lua.Debugger/LuaDebugger.Code.cs b/src/Lua.Debugger/LuaDebugger.Code.cs index baf5a26d..60202b10 100644 --- a/src/Lua.Debugger/LuaDebugger.Code.cs +++ b/src/Lua.Debugger/LuaDebugger.Code.cs @@ -5,8 +5,10 @@ namespace Lua.Debugging; public sealed partial class LuaDebugger { + const int WeakReferenceCleanupInterval = 64; readonly ConditionalWeakTable executionCodes = new(); readonly List> codeReferences = new(); + int additionsSinceCleanup; LuaDebugCode ILuaDebugSession.GetExecutionCode(LuaClosure closure) { @@ -14,24 +16,43 @@ LuaDebugCode ILuaDebugSession.GetExecutionCode(LuaClosure closure) return existing; lock (gate) { - if (!registered.TryGetValue(closure.Proto, out var source)) - { - RegisterSource(closure); - source = registered.GetValue( - closure.Proto, - _ => throw new InvalidOperationException() - ); - } - var code = new ExecutionCode(identity, closure.Proto, source); - executionCodes.Add(closure.Proto, code); - if (codeReferences.Count == codeReferences.Capacity) - codeReferences.RemoveAll(static reference => !reference.TryGetTarget(out _)); - codeReferences.Add(new WeakReference(code)); - UpdateControls(code, IsAttached); - return code; + // GetValue instead of TryGetValue/Add: the execution thread and the protocol thread + // can both reach a prototype first, and Add would throw on the loser. + return executionCodes.GetValue( + closure.Proto, + prototype => + { + if (!registered.TryGetValue(prototype, out var source)) + { + RegisterSource(closure); + source = registered.GetValue( + prototype, + _ => throw new InvalidOperationException() + ); + } + var code = new ExecutionCode(identity, prototype, source) + { + HasBreakpointLine = HasBreakpointLine(prototype, source), + }; + Track(code); + UpdateControls(code, IsAttached); + return code; + } + ); + } + } + + void Track(ExecutionCode code) + { + if (++additionsSinceCleanup >= WeakReferenceCleanupInterval) + { + additionsSinceCleanup = 0; + codeReferences.RemoveAll(static reference => !reference.TryGetTarget(out _)); } + codeReferences.Add(new WeakReference(code)); } + /// Applies the current controls to every execution copy. Cheap: no prototype is scanned. void UpdateExecutionCodes() { var attached = IsAttached; @@ -46,20 +67,41 @@ void UpdateExecutionCodes() void UpdateControls(ExecutionCode code, bool attached) { - var enabled = pauseRequested || step != null; - if (!enabled && code.Source.Points.Count != 0) - foreach (var line in code.Prototype.LineInfo) - if (code.Source.Points.ContainsKey(line)) - { - enabled = true; - break; - } - code.Enabled = attached && enabled; + code.SetEnabled(attached && (pauseRequested || step != null || code.HasBreakpointLine)); + } + + /// + /// Recomputes which execution copies can hit a breakpoint. Only breakpoint or source changes + /// need this; stops and steps reuse the cached answer instead of rescanning every prototype. + /// + void RefreshBreakpointLines() + { + for (var i = codeReferences.Count - 1; i >= 0; i--) + { + if (!codeReferences[i].TryGetTarget(out var code)) + { + codeReferences.RemoveAt(i); + continue; + } + code.HasBreakpointLine = HasBreakpointLine(code.Prototype, code.Source); + } + } + + static bool HasBreakpointLine(Prototype prototype, SourceEntry source) + { + if (source.Points.Count == 0) + return false; + var lines = prototype.LineInfo; + for (var i = 0; i < lines.Length; i++) + if (source.Points.ContainsKey(lines[i])) + return true; + return false; } sealed class ExecutionCode(object identity, Prototype prototype, SourceEntry source) : LuaDebugCode(identity, prototype) { public readonly SourceEntry Source = source; + public bool HasBreakpointLine; } } diff --git a/src/Lua.Debugger/LuaDebugger.Sources.cs b/src/Lua.Debugger/LuaDebugger.Sources.cs index 59e1e5fd..be87a7bf 100644 --- a/src/Lua.Debugger/LuaDebugger.Sources.cs +++ b/src/Lua.Debugger/LuaDebugger.Sources.cs @@ -50,6 +50,7 @@ public LuaBreakpoint[] SetBreakpoints(string source, IReadOnlyList lines) if (request.Resolved.Verified) entry.Points[request.Resolved.Line] = request.Resolved; } + RefreshBreakpointLines(); UpdateExecutionCodes(); return requests.Select(x => x.Resolved).ToArray(); } @@ -60,6 +61,7 @@ public void ClearBreakpoints() lock (gate) { ClearBreakpointsCore(); + RefreshBreakpointLines(); UpdateExecutionCodes(); } } @@ -93,7 +95,10 @@ public LuaDebugSource RegisterSource(LuaClosure closure, string? content = null) if (content != null) entry.Source = entry.Source with { Content = content }; if (controlsChanged) + { + RefreshBreakpointLines(); UpdateExecutionCodes(); + } return entry.Source; } } diff --git a/src/Lua/Runtime/Instruction.cs b/src/Lua/Runtime/Instruction.cs index 566f9827..c3f5db1b 100644 --- a/src/Lua/Runtime/Instruction.cs +++ b/src/Lua/Runtime/Instruction.cs @@ -281,7 +281,10 @@ public static uint CreateAx(OpCode op, int a) public override string ToString() { var op = OpCode; - var s = OpNames[(byte)op]; + var index = (byte)op; + if ((uint)index >= (uint)OpNames.Length) + return $"TRAP {index}"; + var s = OpNames[index]; switch (OpMode(op)) { case IABC: diff --git a/src/Lua/Runtime/LuaDebugCode.cs b/src/Lua/Runtime/LuaDebugCode.cs index 761cd546..32cc8747 100644 --- a/src/Lua/Runtime/LuaDebugCode.cs +++ b/src/Lua/Runtime/LuaDebugCode.cs @@ -18,11 +18,18 @@ CancellationToken cancellationToken ); } +/// +/// A private instruction copy for one prototype, used to place debugger checkpoints. +/// Checkpoints are only patched into the copy while controls need them; a disabled copy is +/// byte-for-byte identical to 's code, so an attached but idle debugger +/// does not change the dispatch of ordinary instructions. +/// internal class LuaDebugCode { internal readonly object Identity; internal readonly Prototype Prototype; internal readonly Instruction[] Code; + readonly int[] traps; internal volatile bool Enabled; internal LuaDebugCode(object identity, Prototype prototype) @@ -30,21 +37,53 @@ internal LuaDebugCode(object identity, Prototype prototype) Identity = identity; Prototype = prototype; Code = prototype.Code.ToArray(); - var points = new bool[Code.Length]; + traps = FindTraps(prototype); + } + + internal void SetEnabled(bool enabled) + { + if (Enabled == enabled) + return; + Enabled = enabled; + if (enabled) + { + foreach (var pc in traps) + Code[pc] = (uint)LuaVirtualMachine.DebuggerBreakOpCode; + return; + } + + var source = Prototype.Code; + foreach (var pc in traps) + Code[pc] = source[pc]; + } + + internal void ResumeAt(int pc) + { + Code[pc] = (uint)LuaVirtualMachine.DebuggerResumeOpCode; + } + + internal Instruction RestoreAt(int pc) + { + // Re-arm this checkpoint for later visits, unless controls were released while stopped. + Code[pc] = Enabled ? (uint)LuaVirtualMachine.DebuggerBreakOpCode : Prototype.Code[pc]; + return Prototype.Code[pc]; + } + + static int[] FindTraps(Prototype prototype) + { + var code = prototype.Code; + var lines = prototype.LineInfo; + var points = new bool[code.Length]; void Mark(int pc) { if ((uint)pc < (uint)points.Length) points[pc] = true; } Mark(0); - for (var pc = 0; pc < Code.Length; pc++) + for (var pc = 0; pc < code.Length; pc++) { - var instruction = Code[pc]; - if ( - pc != 0 - && !prototype.LineInfo.IsEmpty - && prototype.LineInfo[pc] != prototype.LineInfo[pc - 1] - ) + var instruction = code[pc]; + if (pc != 0 && !lines.IsEmpty && lines[pc] != lines[pc - 1]) Mark(pc); switch (instruction.OpCode) { @@ -57,11 +96,9 @@ void Mark(int pc) var target = pc + 1 + instruction.SBx; if ( target <= pc - || Code[target].OpCode != OpCode.ForLoop - || ( - !prototype.LineInfo.IsEmpty - && prototype.LineInfo[target] != prototype.LineInfo[pc] - ) + || target >= code.Length + || code[target].OpCode != OpCode.ForLoop + || (!lines.IsEmpty && lines[target] != lines[pc]) ) Mark(target); break; @@ -85,19 +122,19 @@ void Mark(int pc) break; } } - for (var pc = 0; pc < Code.Length; pc++) - if (points[pc] && Code[pc].OpCode != OpCode.ExtraArg) - Code[pc] = (uint)LuaVirtualMachine.DebuggerBreakOpCode; - } - internal void ResumeAt(int pc) - { - Code[pc] = (uint)LuaVirtualMachine.DebuggerResumeOpCode; + var count = 0; + for (var pc = 0; pc < points.Length; pc++) + if (IsTrapPoint(points, code, pc)) + count++; + var traps = new int[count]; + var index = 0; + for (var pc = 0; pc < points.Length; pc++) + if (IsTrapPoint(points, code, pc)) + traps[index++] = pc; + return traps; } - internal Instruction RestoreAt(int pc) - { - Code[pc] = (uint)LuaVirtualMachine.DebuggerBreakOpCode; - return Prototype.Code[pc]; - } + static bool IsTrapPoint(bool[] points, ReadOnlySpan code, int pc) => + points[pc] && code[pc].OpCode != OpCode.ExtraArg; } diff --git a/src/Lua/Runtime/LuaVirtualMachine.Debug.cs b/src/Lua/Runtime/LuaVirtualMachine.Debug.cs index 0921ddfb..93876534 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.Debug.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.Debug.cs @@ -5,9 +5,16 @@ namespace Lua.Runtime; public static partial class LuaVirtualMachine { + // The opcode field is six bits wide (Instruction.SizeOp) and the published set currently ends + // at ExtraArg, so these two encodings are the only ones that cannot collide with a real + // instruction. DebuggerTrapTests fails if the enum ever grows into them. internal const OpCode DebuggerBreakOpCode = (OpCode)63; internal const OpCode DebuggerResumeOpCode = (OpCode)62; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static bool IsDebugTrap(OpCode opCode) => + opCode is DebuggerBreakOpCode or DebuggerResumeOpCode; + [MethodImpl(MethodImplOptions.NoInlining)] static bool ExecuteDebuggerCheckpoint( VirtualMachineExecutionContext context, diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index 22e3a384..35db3ee6 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -1072,6 +1072,7 @@ out doRestart // Track an untrapped initial header so its first backward jump is a line event. if ( context.DebugCode != null + && (uint)(context.Pc + 1) < (uint)context.DebugCode.Code.Length && Unsafe.Add(ref instructionsHead, context.Pc + 1).OpCode == OpCode.ForLoop ) @@ -1154,7 +1155,7 @@ static void VarArg(VirtualMachineExecutionContext context) continue; default: - if (opCode is DebuggerResumeOpCode or DebuggerBreakOpCode) + if (IsDebugTrap(opCode)) { if (context.DebugCode == null) { @@ -1165,7 +1166,7 @@ static void VarArg(VirtualMachineExecutionContext context) opCode == DebuggerResumeOpCode ? context.DebugCode.RestoreAt(context.Pc) : context.Prototype.Code[context.Pc]; - if (instruction.OpCode is DebuggerBreakOpCode or DebuggerResumeOpCode) + if (IsDebugTrap(instruction.OpCode)) { ThrowLuaNotImplementedException(context, instruction.OpCode); return true; diff --git a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs new file mode 100644 index 00000000..065aec6d --- /dev/null +++ b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs @@ -0,0 +1,24 @@ +using Lua.Runtime; +using NUnit.Framework; + +namespace Lua.Debugger.Tests; + +/// +/// The debugger trampoline occupies the two opcode encodings the six-bit instruction field +/// leaves unused. This guards the day someone grows the published set into them. +/// +public class DebuggerTrapTests +{ + [Test] + public void TrapEncodingsStayOutsideThePublishedInstructionSet() + { + var highest = Enum.GetValues().Select(static op => (byte)op).Max(); + Assert.That( + highest, + Is.LessThan(62), + "LuaVirtualMachine reserves opcode 62/63 for debugger traps; move the trampoline " + + "or widen Instruction.SizeOp before adding opcodes here." + ); + Assert.That(Instruction.SizeOp, Is.EqualTo(6)); + } +} From b7328995a8dca7b00cca4c94c8c49acdd9a5c2cd Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:13:02 +0900 Subject: [PATCH 02/21] fix(debugger): fail fast when inspecting inside the Stopped callback Stopped runs on the paused execution before its command pump starts, so blocking on GetVariablesAsync there waited on a pump that could never run: no error, no timeout, one hung host. Inspections issued from that callback now throw InvalidOperationException naming the fix, and the event documents the constraint. Also drops the unsynchronised 'step == null' fast path in ExecutionEnded; it read a field written under the session lock from another thread for no measurable gain and could miss a step that had to be cleared. --- src/Lua.Debugger/LuaDebugger.cs | 31 ++++++++++++++++++++--- tests/Lua.Debugger.Tests/DebuggerTests.cs | 31 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 7ecf5867..6deea189 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -21,6 +21,7 @@ public sealed partial class LuaDebugger : IDisposable, ILuaDebugSession bool resumeRequested; bool evaluating; bool disposed; + int stoppedThreadId = -1; int nextBreakpointId; int nextStopId; int nextFrameId; @@ -36,6 +37,10 @@ public LuaDebugger(LuaState state, LuaDebuggerOptions? options) } /// Raised on the execution thread once the stack is available for inspection. + /// + /// Inspections are serviced by the paused execution after this callback returns. Do not block + /// on them here; and friends throw instead of deadlocking. + /// public event Action? Stopped; /// Raised after a stop and its inspections unwind, including when disposed during a stop. @@ -59,7 +64,7 @@ public void Attach() { if (disposed) throw new ObjectDisposedException(nameof(LuaDebugger)); - if (!IsAttached && root.GlobalState.ActiveExecutions != 0) + if (!IsAttached && Volatile.Read(ref root.GlobalState.ActiveExecutions) != 0) throw new InvalidOperationException( "Install the debugger before starting Lua execution." ); @@ -191,8 +196,6 @@ CancellationToken cancellationToken void ILuaDebugSession.ExecutionEnded() { - if (step == null) - return; lock (gate) { if (step is not { } request || request.State.CallStackFrameCount != 0) @@ -220,7 +223,7 @@ async ValueTask SuspendAsync(PauseContext context, CancellationToken cancel using var cancellation = cancellationToken.Register(Wake); try { - Stopped?.Invoke(context.Stop); + RaiseStopped(context.Stop); while (true) { cancellationToken.ThrowIfCancellationRequested(); @@ -263,6 +266,19 @@ async ValueTask SuspendAsync(PauseContext context, CancellationToken cancel } } + void RaiseStopped(LuaDebugStop stop) + { + Volatile.Write(ref stoppedThreadId, Environment.CurrentManagedThreadId); + try + { + Stopped?.Invoke(stop); + } + finally + { + Volatile.Write(ref stoppedThreadId, -1); + } + } + void Wake() { lock (gate) @@ -291,6 +307,13 @@ CancellationToken cancellationToken { lock (gate) { + // The paused execution is still inside the Stopped callback, so the command pump has + // not started yet. Queueing there would wait forever; fail with the reason instead. + if (Environment.CurrentManagedThreadId == Volatile.Read(ref stoppedThreadId)) + throw new InvalidOperationException( + "Inspect after the Stopped handler returns. Inspections are serviced by the " + + "paused execution, which is still inside that callback." + ); RequireStop(stopId); var command = new Command(action, cancellationToken); commands.Enqueue(command); diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index f328caf4..280b121b 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -702,4 +702,35 @@ await state.DoStringAsync( Assert.That(await execution, Is.EqualTo(new LuaValue[] { true, 42 })); Assert.That(state.Environment["hooks"].Read(), Is.GreaterThan(0)); } + + [TestCase(LuaDebugPauseMode.Asynchronous)] + [TestCase(LuaDebugPauseMode.Synchronous)] + public async Task InspectingInsideTheStoppedHandlerFailsInsteadOfHanging(LuaDebugPauseMode mode) + { + using var state = LuaState.Create(); + using var debugger = new LuaDebugger(state, new LuaDebuggerOptions { PauseMode = mode }); + var closure = state.Load("local x = 1\nreturn x", "@stopped-handler.lua"); + debugger.Attach(); + debugger.RegisterSource(closure); + debugger.SetBreakpoints("stopped-handler.lua", [1]); + InvalidOperationException? failure = null; + var stops = 0; + debugger.Stopped += stop => + { + stops++; + failure = Assert.Throws(() => + { + _ = debugger.GetVariablesAsync(stop.Id, 1, LuaDebugScope.Locals); + }); + debugger.Continue(stop.Id); + }; + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + Assert.That( + await state.ExecuteAsync(closure, timeout.Token), + Is.EqualTo(new LuaValue[] { 1 }) + ); + Assert.That(stops, Is.EqualTo(1)); + Assert.That(failure, Is.Not.Null); + Assert.That(failure!.Message, Does.Contain("after the Stopped handler returns")); + } } From 87688d03c842b1142cc4c0c19f37fba0db2cfdce Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:13:08 +0900 Subject: [PATCH 03/21] fix: track active executions atomically and survive malformed DAP frames ActiveExecutions decides when a debugger may attach and when a pending step has to end, but it was incremented with ++/-- from host calls and coroutine resumes that can run on different threads. It is now a compare-exchange for the outermost entry plus Interlocked updates elsewhere. A malformed DAP frame raised InvalidDataException, which is not an IOException, so the listener's per-connection catch missed it: the accept loop unwound and the debugger was detached from the host. The client is now dropped and the listener keeps accepting, so an editor can reconnect. --- src/Lua.Debugger.Dap/LuaDapServer.cs | 5 +++++ src/Lua/Internal/CoroutineCore.cs | 2 +- src/Lua/LuaState.cs | 14 ++++++++------ tests/Lua.Debugger.Dap.Tests/DapTests.cs | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Lua.Debugger.Dap/LuaDapServer.cs b/src/Lua.Debugger.Dap/LuaDapServer.cs index b8fdf18e..21705848 100644 --- a/src/Lua.Debugger.Dap/LuaDapServer.cs +++ b/src/Lua.Debugger.Dap/LuaDapServer.cs @@ -64,6 +64,11 @@ public async Task RunAsync(CancellationToken cancellationToken = default) } catch (IOException) { } catch (SocketException) { } + catch (InvalidDataException) + { + // A malformed frame ends this client only; the listener stays available so the + // editor can reconnect and the debugger is not detached from the host. + } finally { Volatile.Write(ref session, null); diff --git a/src/Lua/Internal/CoroutineCore.cs b/src/Lua/Internal/CoroutineCore.cs index 98685e9e..a308f055 100644 --- a/src/Lua/Internal/CoroutineCore.cs +++ b/src/Lua/Internal/CoroutineCore.cs @@ -121,7 +121,7 @@ phase is Completed or Closed or Cancelling ); try { - Thread.GlobalState.ActiveExecutions++; + Interlocked.Increment(ref Thread.GlobalState.ActiveExecutions); entered = true; if (isFirstCall) Thread.Stack.EnsureCapacity(Thread.Stack.Count + argCount); diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index 13c525f4..dc927fe0 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -675,16 +675,18 @@ CancellationToken cancellationToken [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool EnterExecution() { - if (GlobalState.ActiveExecutions != 0) - return false; - GlobalState.ActiveExecutions++; - return true; + // Nested host re-entry (Lua calling C# that calls Lua again) must not count: only the + // outermost entry owns the 0 -> 1 transition that later reports ExecutionEnded. + // A compare-exchange keeps that decision atomic across coroutine resumes and host calls. + return Interlocked.CompareExchange(ref GlobalState.ActiveExecutions, 1, 0) == 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void ExitExecution(bool entered) { - if (entered && --GlobalState.ActiveExecutions == 0) + if (!entered) + return; + if (Interlocked.Decrement(ref GlobalState.ActiveExecutions) == 0) Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); } @@ -944,7 +946,7 @@ public void Dispose() CoreData.Release(); CoreData = null!; - if (GlobalState.ActiveExecutions == 0) + if (Volatile.Read(ref GlobalState.ActiveExecutions) == 0) Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); } } diff --git a/tests/Lua.Debugger.Dap.Tests/DapTests.cs b/tests/Lua.Debugger.Dap.Tests/DapTests.cs index 28efb32c..2a7d9bdd 100644 --- a/tests/Lua.Debugger.Dap.Tests/DapTests.cs +++ b/tests/Lua.Debugger.Dap.Tests/DapTests.cs @@ -325,6 +325,27 @@ await host.Execute(closure).WaitAsync(host.Token), ); } + [Test] + public async Task MalformedFrameDropsTheClientWithoutDetachingTheDebugger() + { + await using var host = await Host.CreateAsync(); + var closure = host.State.Load("return 42", "@" + host.Path); + await host.InitializeAsync(); + await host.RequestAsync("configurationDone"); + await host + .Client.GetStream() + .WriteAsync("Content-Length: 8\r\n\r\nnot-json"u8.ToArray(), host.Token); + host.Client.Dispose(); + await host.ReconnectAsync(); + await host.InitializeAsync(); + await host.RequestAsync("configurationDone"); + Assert.That(host.Server.Debugger.IsAttached, Is.True); + Assert.That( + await host.Execute(closure).WaitAsync(host.Token), + Is.EqualTo(new LuaValue[] { 42 }) + ); + } + [Test] public async Task CoordinatesAndUriPathsRespectClientInitialization() { From 27e6ad4f56f0baa3d7b03d2a8f3910795082a353 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:13:14 +0900 Subject: [PATCH 04/21] docs(bench): restore debugger benchmarks and refresh the performance notes The benchmark reorganization deleted the four debugger benchmarks while docs/debugger.md still pointed at them and at a cost model that no longer matched the code. Two focused workloads replace them: attached-versus-idle execution, and one stop/continue per iteration with 1 versus 2,001 executed prototypes. Both are wired into the existing BenchmarkSwitcher. The documentation now describes the checkpoint lifecycle that the code implements (patched only while controls need them), states the stop-handler inspection constraint, and records the malformed-frame behaviour of the listener. --- docs/debugger.md | 32 ++--- sandbox/Benchmark/Benchmark.csproj | 3 +- .../Benchmark/Workloads/DebuggerWorkloads.cs | 134 ++++++++++++++++++ 3 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 sandbox/Benchmark/Workloads/DebuggerWorkloads.cs diff --git a/docs/debugger.md b/docs/debugger.md index 4e28102e..0b9ad017 100644 --- a/docs/debugger.md +++ b/docs/debugger.md @@ -37,7 +37,7 @@ finally The server binds to loopback by default; another bind address is an explicit host choice. It serves one client at a time and supports reconnecting. It does not own the state, launch the host process, or terminate it on disconnect. `WaitForConfigurationAsync` waits for the first client's `configurationDone`, including installing an entry-stop request, before execution starts. Hosts that should run without an editor can omit that wait. -Disconnect and EOF clear session controls and release a stop. The server keeps its debugger installed for later clients; server disposal detaches it. Host functions used by an evaluation must honor their cancellation token so disconnect can cancel that evaluation. Pass the application's lifetime token to execution and the server when needed. A pause cannot interrupt arbitrary C# code or an outstanding host await. Normal Lua-state ownership rules apply: do not dispose the state or execute it concurrently while it is running or stopped. +Disconnect and EOF clear session controls and release a stop. A malformed frame drops only that client: the listener keeps accepting and the debugger stays attached, so the editor can reconnect. The server keeps its debugger installed for later clients; server disposal detaches it. Host functions used by an evaluation must honor their cancellation token so disconnect can cancel that evaluation. Pass the application's lifetime token to execution and the server when needed. A pause cannot interrupt arbitrary C# code or an outstanding host await. Normal Lua-state ownership rules apply: do not dispose the state or execute it concurrently while it is running or stopped. For another transport, use `LuaDapSession(debugger, inputStream, outputStream)` and await `RunAsync`. The caller owns the streams. Use one active DAP session per debugger. @@ -112,6 +112,8 @@ A dedicated VM checkpoint runs before a selected instruction executes. It does n An asynchronous stop awaits a signal; a synchronous stop waits on its debugger command queue. Both process inspection commands on the suspended execution, and the protocol reader continues receiving requests without reading live stack storage itself. Frame/variable handles expire on resume, cancellation, or disconnect; table references are cleared when the stop ends. +`Stopped` runs on the paused execution before its command pump starts, so it is the one place where inspections cannot be serviced yet: blocking on `GetVariablesAsync` (or any other inspection) there throws `InvalidOperationException` instead of waiting forever. Record the stop, return, and inspect afterwards; `Continue` may be called from the handler. + DAP inspections are serialized separately from execution controls. Continue and stepping can cancel an awaiting evaluation; execution resumes after that inspection unwinds. Requests retain the stop ID observed when received, so queued inspections cannot operate on a later stop. An application resume racing an editor resume preserves the newer stop and produces one response to the editor request. Failures from inspection cancellation callbacks after an acknowledged resume are reported through DAP stderr output. An editor connecting to an existing stop receives it after configuration completes. Disconnect releases the stop and clears session controls after pending requests finish. Replacing the debugger while execution is idle also covers coroutines that yielded under its predecessor. If posted work throws during a synchronous stop, pending inspections are cancelled and drained before the failure unwinds Lua execution. @@ -150,36 +152,32 @@ An ordinary Lua hook can be replaced by the script and adds callback/frame overh Other embedders informed the hosting API as well. [MoonSharp's server](https://github.com/moonsharp-devs/moonsharp/blob/master/src/MoonSharp.VsCodeDebugger/MoonSharpVsCodeDebugServer.cs) attaches application-owned scripts and accepts a source finder; its [AsyncDebugger](https://github.com/moonsharp-devs/moonsharp/blob/master/src/MoonSharp.VsCodeDebugger/DebuggerLogic/AsyncDebugger.cs) nevertheless blocks the script thread while waiting for debugger actions. [Jint's debug callbacks](https://github.com/sebastienros/jint/blob/main/Jint/Runtime/Debugger/DebugHandler.cs) run inside engine execution and resume it by returning. [ClearScript's document pause flag](https://github.com/ClearFoundry/ClearScript/blob/master/ClearScript/DocumentFlags.cs) separates a selected script's initial debugger wait from general engine setup. Lua-CSharp keeps application ownership and source mapping explicit, and offers both blocking and asynchronous stops because its host calls can already await. -The VM keeps one dispatch loop. Ordinary execution uses `Prototype.Code`; a debugger installs its own cached execution copy for each prototype. That copy contains fixed trap instructions at selected source-line and control-flow points. Breakpoint, pause, and step changes update control flags; the protocol thread never patches instructions. Inactive checkpoints immediately dispatch the original instruction. Loop targets provide a checkpoint even for a single-line infinite loop. Bytecode without source metadata still has control-flow checkpoints. +The VM keeps one dispatch loop. Ordinary execution uses `Prototype.Code`; a debugger installs its own cached execution copy for each prototype. While controls need it, that copy carries trap instructions at selected source-line and control-flow points. Breakpoint, pause, and step changes update control flags and patch or unpatch those points; the protocol thread never touches `Prototype.Code`. A copy with no active controls is byte-for-byte identical to the original, so idle debugging costs nothing per instruction. Loop targets provide a checkpoint even for a single-line infinite loop. Bytecode without source metadata still has control-flow checkpoints. Trap handling lives in the dispatch switch's existing fallback path. There is no second interpreter loop or generic specialization. Keeping traps outside the ordinary opcode cases also avoids expanding the normal dispatch table. A forward numeric-loop setup jump needs no separate header trap when it stays on the same source line. The setup records that untrapped header once, preserving the first backward jump's line event. Loop-body targets and source-line boundaries remain checkpoints. Call/return checkpoints remain available for pause requests around host awaits; unreachable fallthrough points after returns and tail calls are omitted. -An internal resume trap executes the stopped instruction once and restores its checkpoint. Only the VM owner writes that marker. It survives breakpoint edits during a stop and does not replay Lua hooks. Private execution copies keep shared prototypes, other Lua states, and bytecode dumps unchanged. Volatile control flags are checked only at traps, so ordinary instruction fetching needs no debugger flag or volatile read. Execution-copy metadata is weakly cached and released with its prototype. Attached execution pays for checkpoints, including when no breakpoint is hit; detached execution uses the ordinary instructions. +An internal resume trap executes the stopped instruction once and restores its checkpoint. Only the VM owner writes that marker. It survives breakpoint edits during a stop and does not replay Lua hooks. Private execution copies keep shared prototypes, other Lua states, and bytecode dumps unchanged. Volatile control flags are checked only at traps, so ordinary instruction fetching needs no debugger flag or volatile read. Execution-copy metadata is weakly cached and released with its prototype. Detached execution uses the ordinary instructions directly. -Execution copies carry an opaque session identity and their source metadata, without a reference back to the debugger. A yielded coroutine therefore does not retain a detached debugger or its application callbacks. The weak tracking list removes dead entries when it fills, before growing, as well as during control changes. Compiling temporary chunks therefore does not grow that list indefinitely while controls stay unchanged. The VM checks ownership at frame restart; within the instruction loop it reads the active debugger only at enabled checkpoints. Ownership requires no weak-reference lookup on the execution path. +Execution copies carry an opaque session identity and their source metadata, without a reference back to the debugger. A yielded coroutine therefore does not retain a detached debugger or its application callbacks. The weak tracking list prunes dead entries periodically, before it grows, and during control changes. Compiling temporary chunks therefore does not grow that list indefinitely while controls stay unchanged. The VM checks ownership at frame restart; within the instruction loop it reads the active debugger only at enabled checkpoints. Ownership requires no weak-reference lookup on the execution path. ## Performance -Debugging is opt-in. Detached execution uses the original bytecode. Starting a server installs checkpoints before accepting clients, so an installed debugger has an execution cost even while no editor is connected. The cost depends on the workload's line boundaries, calls, loop targets and enabled breakpoints. A breakpoint in an uncalled function does not activate matching in other functions from the same source. +Debugging is opt-in. Detached execution uses the original bytecode. Starting a server installs the debugger before accepting clients, but an installed debugger only pays for the checkpoints its controls need: with no breakpoints, no requested pause and no pending step, execution runs on an unpatched copy and matches detached speed within measurement noise (1.0–1.1x on loop-heavy workloads). Verified breakpoints patch only the prototypes whose lines they name, so a breakpoint in an uncalled function does not slow other functions from the same source. + +Stops and steps cost work proportional to the number of *executed* prototypes, not to their instruction count: each stop revisits the tracked execution copies and applies cached control state. Breakpoint membership is cached per prototype and refreshed only when breakpoints or loaded sources change. -First execution builds private instruction copies and source metadata. Reusing compiled chunks avoids that setup on later calls. Pause, stepping and inspection allocate only as needed; execution ownership is tracked at host-call and coroutine-resume boundaries. Control updates use the weak tracking list and add no per-instruction work. +First execution builds private instruction copies and source metadata. Reusing compiled chunks avoids that setup on later calls. Pause, stepping and inspection allocate only as needed; execution ownership is tracked at host-call and coroutine-resume boundaries. Run benchmarks in Release mode from this repository: ```sh -# Warmed execution, detached versus installed, across six interpreter workloads. -dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerBenchmark*' - -# An unhit breakpoint in another function versus a cold branch of the active function. -dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerBreakpointBenchmark*' - -# Pause/reset cost with 1 or 2,000 activated functions. -dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerControlBenchmark*' +# Warmed execution, detached versus attached-but-idle, across three interpreter workloads. +dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerIdleBenchmark*' -# First-use metadata cost; compilation is excluded. -dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerFirstUseBenchmark*' +# One stop/continue per loop iteration, with 1 versus 2,001 executed prototypes. +dotnet run -c Release --project sandbox/Benchmark -- --filter '*DebuggerStopBenchmark*' ``` -`DebuggerFirstUseBenchmark` varies function count, source preregistration and an unhit breakpoint. Keep first-use measurements separate from warmed execution and measure allocations as well as elapsed time. Small timing differences are sensitive to JIT profiles, code layout and system load; compare the same workloads and runtime settings when assessing a change. +`DebuggerIdleBenchmark` reports the per-instruction cost of an attached debugger; it should stay within noise of the detached case. `DebuggerStopBenchmark` reports the per-stop bookkeeping cost and must not grow with the instruction count of the stopped program. Keep first-use measurements separate from warmed execution and measure allocations as well as elapsed time. Small timing differences are sensitive to JIT profiles, code layout and system load; compare the same workloads and runtime settings when assessing a change. diff --git a/sandbox/Benchmark/Benchmark.csproj b/sandbox/Benchmark/Benchmark.csproj index 3445e82a..35310518 100644 --- a/sandbox/Benchmark/Benchmark.csproj +++ b/sandbox/Benchmark/Benchmark.csproj @@ -1,4 +1,4 @@ - + Exe net10.0 @@ -14,6 +14,7 @@ + diff --git a/sandbox/Benchmark/Workloads/DebuggerWorkloads.cs b/sandbox/Benchmark/Workloads/DebuggerWorkloads.cs new file mode 100644 index 00000000..d8036c90 --- /dev/null +++ b/sandbox/Benchmark/Workloads/DebuggerWorkloads.cs @@ -0,0 +1,134 @@ +using BenchmarkDotNet.Attributes; +using Lua; +using Lua.Debugging; +using Lua.Runtime; +using Lua.Standard; + +[BenchmarkCategory("Debugger")] +[MemoryDiagnoser] +public class DebuggerIdleBenchmark +{ + [Params("numeric-loop", "calls", "coroutines")] + public string Workload { get; set; } = "numeric-loop"; + + /// An attached debugger with no breakpoints and no pending step must cost nothing per instruction. + [Params(false, true)] + public bool Attached { get; set; } + + LuaState state = default!; + LuaClosure closure = default!; + LuaDebugger? debugger; + + [GlobalSetup] + public void Setup() + { + state = LuaState.Create(); + state.OpenStandardLibraries(); + closure = state.Load(Source(Workload), "=debugger-idle"); + if (Attached) + { + debugger = new LuaDebugger(state); + debugger.Attach(); + debugger.RegisterSource(closure); + } + Run(); + } + + [Benchmark] + public void Run() => + state.ExecuteAsync(closure, Memory.Empty).GetAwaiter().GetResult(); + + [GlobalCleanup] + public void Cleanup() + { + debugger?.Dispose(); + state.Dispose(); + } + + static string Source(string workload) => + workload switch + { + "numeric-loop" => "local x=0;for i=1,10000 do x=x+i end;assert(x==50005000)", + "calls" => + "local function f(x)return x+1 end;local x=0;for i=1,1000 do x=f(x)end;assert(x==1000)", + "coroutines" => + "local co=coroutine.create(function()for i=1,100 do coroutine.yield(i)end end);" + + "local x=0;for i=1,100 do local ok,v=coroutine.resume(co);assert(ok);x=x+v end;assert(x==5050)", + _ => throw new InvalidOperationException(), + }; +} + +/// +/// Cost of one stop/continue cycle while every executed prototype is tracked. A breakpoint line +/// inside a loop stops once per iteration, so the per-stop bookkeeping dominates the measurement. +/// +[BenchmarkCategory("Debugger")] +[MemoryDiagnoser] +public class DebuggerStopBenchmark +{ + const int Stops = 500; + + /// Number of additional functions executed before the loop, each with 40 source lines. + [Params(0, 2000)] + public int ActivatedFunctions { get; set; } + + LuaState state = default!; + LuaClosure closure = default!; + LuaDebugger debugger = default!; + int observedStops; + + [GlobalSetup] + public void Setup() + { + state = LuaState.Create(); + state.OpenStandardLibraries(); + debugger = new LuaDebugger(state); + debugger.Attach(); + var source = BuildSource(ActivatedFunctions); + closure = state.Load(source, "=debugger-stop"); + debugger.RegisterSource(closure); + debugger.SetBreakpoints("=debugger-stop", [source.Split('\n').Length - 3]); + debugger.Stopped += stop => + { + observedStops++; + debugger.Continue(stop.Id); + }; + Run(); + } + + [Benchmark] + public int Run() + { + observedStops = 0; + state.ExecuteAsync(closure, Memory.Empty).GetAwaiter().GetResult(); + return observedStops; + } + + [GlobalCleanup] + public void Cleanup() + { + debugger.Dispose(); + state.Dispose(); + } + + static string BuildSource(int functions) + { + var builder = new System.Text.StringBuilder(); + builder.Append("local fs = {}\n"); + for (var i = 0; i < functions; i++) + { + builder.Append("fs[").Append(i).Append("] = function(x)\n"); + for (var line = 0; line < 40; line++) + builder.Append(" x = x + ").Append(line).Append('\n'); + builder.Append(" return x\nend\n"); + } + for (var i = 0; i < functions; i++) + builder.Append("fs[").Append(i).Append("](1)\n"); + builder.Append("local n = 0\n"); + builder.Append("for i = 1, ").Append(Stops).Append(" do\n"); + builder.Append(" n = n + 1\n"); + builder.Append("end\n"); + builder.Append("return n\n"); + return builder.ToString(); + } +} From 9038bd810c604953b5d25f1a3f0fe2745c1b6652 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:36:42 +0900 Subject: [PATCH 05/21] fix(debugger): scope pause requests to the execution they were issued for A pause that arrived while Lua was inside a host call was never consumed if that execution then unwound (cancellation, host error), so the next, unrelated execution stopped at its first line. ExecutionEnded now drops the abandoned request, while a step that owns a still-suspended coroutine keeps its frames and its step. CurrentStop also reports no stop while it is resuming, matching the predicate that RequireStop and TryContinue already use, so a session cannot adopt a stop that is on its way out. --- src/Lua.Debugger/LuaDebugger.cs | 21 ++++++++--- tests/Lua.Debugger.Tests/DebuggerTests.cs | 46 +++++++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 6deea189..70402fc4 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -54,7 +54,9 @@ public LuaDebugStop? CurrentStop get { lock (gate) - return paused?.Stop; + // A stop that is already resuming is no longer adoptable; RequireStop and + // TryContinue use the same predicate. + return resumeRequested ? null : paused?.Stop; } } @@ -198,10 +200,19 @@ void ILuaDebugSession.ExecutionEnded() { lock (gate) { - if (step is not { } request || request.State.CallStackFrameCount != 0) - return; - step = null; - UpdateExecutionCodes(); + // A pause belongs to the execution it was requested against. If that execution unwound + // (cancellation, host error) before a checkpoint consumed it, keeping the request would + // stop the next, unrelated execution. A step that owns a still-suspended coroutine is + // kept: its Lua frames are live even though no execution is running. + var changed = pauseRequested; + pauseRequested = false; + if (step is { } request && request.State.CallStackFrameCount == 0) + { + step = null; + changed = true; + } + if (changed) + UpdateExecutionCodes(); } } diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index 280b121b..7e03441c 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -733,4 +733,50 @@ await state.ExecuteAsync(closure, timeout.Token), Assert.That(failure, Is.Not.Null); Assert.That(failure!.Message, Does.Contain("after the Stopped handler returns")); } + + [Test] + public async Task PauseRequestDoesNotOutliveItsExecution() + { + using var state = LuaState.Create(); + using var debugger = new LuaDebugger(state); + var closure = state.Load("wait()\nreturn 1", "@pause-scope.lua"); + debugger.Attach(); + debugger.RegisterSource(closure); + var entered = new TaskCompletionSource( + TaskCreationOptions.RunContinuationsAsynchronously + ); + state.Environment["wait"] = new LuaFunction( + async (context, token) => + { + entered.TrySetResult(true); + await Task.Delay(Timeout.Infinite, token); + return context.Return(); + } + ); + using var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + var execution = state.ExecuteAsync(closure, cancellation.Token).AsTask(); + await entered.Task.WaitAsync(cancellation.Token); + // The pause arrives while the execution is alive but outside any Lua checkpoint. + debugger.Pause(); + cancellation.Cancel(); + try + { + await execution; + Assert.Fail("Execution should be cancelled."); + } + catch (LuaCanceledException) { } + + var stops = 0; + debugger.Stopped += stop => + { + stops++; + debugger.Continue(stop.Id); + }; + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + Assert.That( + await state.ExecuteAsync(state.Load("return 2", "@pause-scope-2.lua"), timeout.Token), + Is.EqualTo(new LuaValue[] { 2 }) + ); + Assert.That(stops, Is.Zero); + } } From 09b5edd21088258a6beebb07dcec1159f2ad295b Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:36:52 +0900 Subject: [PATCH 06/21] perf(dap): page stack traces instead of materializing the whole stack stackTrace, scopes and evaluate(frameId: 0) each built every frame of the suspended stack, then sliced. Protocol clients page 20 frames at a time, so a deeply recursive script cost O(depth) work per page and O(depth^2) to walk a large stack; the editor could appear to hang. The debugger now exposes an internal paged read whose cost follows the page, and the DAP layer uses it for all three call sites. Public GetStackTraceAsync keeps its signature and returns the full stack as before. The paging test also pins totalFrames and the stability of frame ids across pages. The documentation records stack paging, the pause-request lifetime and the Locals naming that follows debug.getlocal. --- docs/debugger.md | 2 +- .../LuaDapSession.Requests.cs | 37 +++++--- src/Lua.Debugger/LuaDebugInfo.cs | 2 + src/Lua.Debugger/LuaDebugger.Inspection.cs | 92 ++++++++++++------- tests/Lua.Debugger.Dap.Tests/DapTests.cs | 71 ++++++++++++++ 5 files changed, 153 insertions(+), 51 deletions(-) diff --git a/docs/debugger.md b/docs/debugger.md index 0b9ad017..c0ee4577 100644 --- a/docs/debugger.md +++ b/docs/debugger.md @@ -132,7 +132,7 @@ The synchronous pump drains accepted callbacks even when a stop is resumed immed | Step over | At the next line in the original frame, or after it returns. A tail-call replacement is distinct. | | Step out | After the original frame returns. A coroutine yielding/completing can transfer the stop to its caller. | -Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, and raw table/userdata contents are inspected lazily with paging. Value display does not call user `__tostring`. +Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. A pause request belongs to the execution it was issued against: if that execution unwinds before a checkpoint consumes it, the request is dropped rather than stopping the next one. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, raw table/userdata contents and stack frames are inspected lazily with paging, and a stack page costs work proportional to the page rather than to the stack depth. The `Locals` scope keeps `debug.getlocal` numbering, so hidden loop control variables (such as `(for index)` or `(for control)`) appear alongside ordinary locals and varargs are listed as `...[n]`. Value display does not call user `__tostring`. Source paths use host path/case rules. Breakpoints resolve to the first executable line at or after the requested line. Unknown sources remain pending until registered/executed, with breakpoint-change events on resolution. There is no basename-only fallback. For an in-memory chunk, use an `=name` chunk name, register its text, and use the source reference supplied by the adapter. diff --git a/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs b/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs index 54e5cba6..87edbe82 100644 --- a/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs +++ b/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs @@ -176,22 +176,26 @@ await RespondAsync( return; case "stackTrace": CheckThread(args); - var frames = await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), token) - .ConfigureAwait(false); var start = Integer(args, "startFrame"); var levels = Integer(args, "levels"); if (start < 0 || levels < 0) throw new ArgumentOutOfRangeException(nameof(args)); - var selected = frames.Skip(start).Take(levels == 0 ? int.MaxValue : levels); + var stack = await debugger + .GetStackTraceAsync( + CurrentStop(expectedStop), + start, + levels == 0 ? int.MaxValue : levels, + token + ) + .ConfigureAwait(false); await RespondAsync( request, new JsonObject { - ["totalFrames"] = frames.Length, + ["totalFrames"] = stack.TotalFrames, ["stackFrames"] = new JsonArray( - selected - .Select(frame => + stack + .Frames.Select(frame => (JsonNode?) new JsonObject { @@ -216,10 +220,10 @@ await RespondAsync( return; case "scopes": var frameId = Integer(args, "frameId"); - var allFrames = await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), token) + var frame = await debugger + .GetStackTraceAsync(CurrentStop(expectedStop), frameId - 1, 1, token) .ConfigureAwait(false); - if (!allFrames.Any(frame => frame.Id == frameId)) + if (frame.Frames.Length == 0 || frame.Frames[0].Id != frameId) throw new ArgumentException("Unknown frame."); var scopes = new JsonArray(); foreach ( @@ -295,11 +299,14 @@ await RespondAsync( case "evaluate": var evaluationFrame = Integer(args, "frameId"); if (evaluationFrame == 0) - evaluationFrame = ( - await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), token) - .ConfigureAwait(false) - )[0].Id; + { + var top = await debugger + .GetStackTraceAsync(CurrentStop(expectedStop), 0, 1, token) + .ConfigureAwait(false); + if (top.Frames.Length == 0) + throw new InvalidOperationException("Lua execution is not stopped."); + evaluationFrame = top.Frames[0].Id; + } var evaluated = await debugger .EvaluateAsync( CurrentStop(expectedStop), diff --git a/src/Lua.Debugger/LuaDebugInfo.cs b/src/Lua.Debugger/LuaDebugInfo.cs index 5847af6a..fbeafa90 100644 --- a/src/Lua.Debugger/LuaDebugInfo.cs +++ b/src/Lua.Debugger/LuaDebugInfo.cs @@ -29,6 +29,8 @@ public sealed record LuaDebugStop( public sealed record LuaDebugStackFrame(int Id, string Name, LuaDebugSource? Source, int Line); +internal readonly record struct LuaDebugStackTrace(LuaDebugStackFrame[] Frames, int TotalFrames); + public sealed record LuaDebugVariable(string Name, LuaValue Value, string DisplayValue) { public bool IsReadOnly { get; init; } diff --git a/src/Lua.Debugger/LuaDebugger.Inspection.cs b/src/Lua.Debugger/LuaDebugger.Inspection.cs index 25ab0cdb..73c3fddd 100644 --- a/src/Lua.Debugger/LuaDebugger.Inspection.cs +++ b/src/Lua.Debugger/LuaDebugger.Inspection.cs @@ -11,44 +11,66 @@ public Task GetStackTraceAsync( InspectAsync( stopId, (context, _) => + new ValueTask( + ReadStackTrace(context, 0, int.MaxValue).Frames + ), + cancellationToken + ); + + /// + /// Reads one page of the suspended stack. The cost is proportional to the page, not to the + /// stack depth, which keeps protocol paging usable for deeply recursive scripts. + /// + internal Task GetStackTraceAsync( + int stopId, + int start, + int count, + CancellationToken cancellationToken = default + ) => + InspectAsync( + stopId, + (context, _) => + new ValueTask(ReadStackTrace(context, start, count)), + cancellationToken + ); + + LuaDebugStackTrace ReadStackTrace(PauseContext context, int start, int count) + { + var frames = context.State.GetCallStackFrames(); + var total = frames.Length; + if (start < 0 || start >= total || count <= 0) + return new LuaDebugStackTrace(Array.Empty(), total); + var end = (int)Math.Min(total, (long)start + count); + var result = new LuaDebugStackFrame[end - start]; + for (var position = start; position < end; position++) + { + // Frames are reported top first, while the call stack stores the oldest frame first. + var i = total - 1 - position; + var frame = frames[i]; + var pc = position == 0 ? context.Pc : frames[i + 1].CallerInstructionIndex; + LuaDebugSource? source = null; + var line = 0; + var name = frame.Function.Name; + if (frame.Function is LuaClosure closure) { - var frames = context.State.GetCallStackFrames(); - var result = new LuaDebugStackFrame[frames.Length]; - for (var i = frames.Length - 1; i >= 0; i--) + lock (gate) { - var frame = frames[i]; - var pc = - i == frames.Length - 1 ? context.Pc : frames[i + 1].CallerInstructionIndex; - LuaDebugSource? source = null; - var line = 0; - var name = frame.Function.Name; - if (frame.Function is LuaClosure closure) - { - lock (gate) - { - RegisterPrototype(closure.Proto); - source = registered - .GetValue(closure.Proto, _ => throw new InvalidOperationException()) - .Source; - } - if ((uint)pc < (uint)closure.Proto.LineInfo.Length) - line = closure.Proto.LineInfo[pc]; - name = - closure.Proto.LineDefined == 0 - ? "main chunk" - : $"function at {source.Name}:{closure.Proto.LineDefined}"; - } - result[frames.Length - 1 - i] = new LuaDebugStackFrame( - i + 1, - name, - source, - line - ); + RegisterPrototype(closure.Proto); + source = registered + .GetValue(closure.Proto, _ => throw new InvalidOperationException()) + .Source; } - return new ValueTask(result); - }, - cancellationToken - ); + if ((uint)pc < (uint)closure.Proto.LineInfo.Length) + line = closure.Proto.LineInfo[pc]; + name = + closure.Proto.LineDefined == 0 + ? "main chunk" + : $"function at {source.Name}:{closure.Proto.LineDefined}"; + } + result[position - start] = new LuaDebugStackFrame(i + 1, name, source, line); + } + return new LuaDebugStackTrace(result, total); + } public Task GetVariablesAsync( int stopId, diff --git a/tests/Lua.Debugger.Dap.Tests/DapTests.cs b/tests/Lua.Debugger.Dap.Tests/DapTests.cs index 2a7d9bdd..8069e1bb 100644 --- a/tests/Lua.Debugger.Dap.Tests/DapTests.cs +++ b/tests/Lua.Debugger.Dap.Tests/DapTests.cs @@ -346,6 +346,77 @@ await host.Execute(closure).WaitAsync(host.Token), ); } + [Test] + public async Task StackTracePagingReportsTheTotalAndStableFrameIds() + { + await using var host = await Host.CreateAsync(); + var closure = host.State.Load( + "local function inner()\n return 1\nend\n" + + "local function outer()\n local value = inner()\n return value\nend\n" + + "local result = outer()\nreturn result", + "@" + host.Path + ); + host.Server.Debugger.RegisterSource(closure); + await host.InitializeAsync(); + await host.RequestAsync( + "setBreakpoints", + new() + { + ["source"] = new JsonObject { ["path"] = host.Path }, + ["breakpoints"] = new JsonArray(new JsonObject { ["line"] = 2 }), + } + ); + await host.RequestAsync("configurationDone"); + var execution = host.Execute(closure); + await host.EventAsync("stopped"); + + var all = await host.RequestAsync("stackTrace", new() { ["threadId"] = 1 }); + var total = all["body"]!["totalFrames"]!.GetValue(); + Assert.That(total, Is.EqualTo(3)); + + var page = await host.RequestAsync( + "stackTrace", + new() + { + ["threadId"] = 1, + ["startFrame"] = 1, + ["levels"] = 2, + } + ); + Assert.That(page["body"]!["totalFrames"]!.GetValue(), Is.EqualTo(3)); + var frames = page["body"]!["stackFrames"]!.AsArray(); + Assert.That( + frames.Select(frame => frame!["id"]!.GetValue()), + Is.EqualTo(new[] { 2, 1 }) + ); + Assert.That(frames[0]!["line"]!.GetValue(), Is.EqualTo(5)); + + // The page must still resolve scopes for the frame it returned. + var scopes = await host.RequestAsync("scopes", new() { ["frameId"] = 2 }); + Assert.That(scopes["body"]!["scopes"]!.AsArray().Count, Is.EqualTo(3)); + + var beyond = await host.RequestAsync( + "stackTrace", + new() + { + ["threadId"] = 1, + ["startFrame"] = 3, + ["levels"] = 5, + } + ); + Assert.That(beyond["body"]!["totalFrames"]!.GetValue(), Is.EqualTo(3)); + Assert.That(beyond["body"]!["stackFrames"]!.AsArray().Count, Is.Zero); + var invalid = await host.RequestAsync( + "scopes", + new() { ["frameId"] = 9 }, + expectSuccess: false + ); + Assert.That(invalid["message"]!.GetValue(), Is.EqualTo("Unknown frame.")); + + await host.RequestAsync("continue", new() { ["threadId"] = 1 }); + await execution.WaitAsync(host.Token); + } + [Test] public async Task CoordinatesAndUriPathsRespectClientInitialization() { From 2ad7ae5a33378f48a92944ba57f41275ba0b5c0c Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:36:58 +0900 Subject: [PATCH 07/21] test(debugger): cover line events for scoping and control flow The line-event parity test compared debugger stops with Lua's own line hook for nine one-liners. Repeat-until with a local in the condition, goto labels, method calls, multi-line expressions, long comments and strings, nested closures and a loop break all place traps differently; they are now checked against the same oracle. Also pins the trap encoding: the reserved opcodes must stay outside the published instruction set, and a trap value must not overflow the instruction name table used by diagnostics. --- tests/Lua.Debugger.Tests/DebuggerTests.cs | 14 ++++++++++++++ tests/Lua.Debugger.Tests/DebuggerTrapTests.cs | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index 7e03441c..9216d5fb 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -41,6 +41,20 @@ public async Task NewlyRegisteredLinesUpdateExistingExecutionControls() "local function f(x) return x+1 end;local function g(x) return f(x) end;local n=0;for i=1,3 do n=g(n) end;return n" )] [TestCase("local n=0;for k,v in ipairs({1,2,3}) do n=n+v end;return n")] + // Scoping and control flow that commonly diverges from a line hook. + [TestCase("local n = 0\nrepeat\n local done = n >= 2\n n = n + 1\nuntil done\nreturn n")] + [TestCase("local n = 0\n::again::\nn = n + 1\nif n < 3 then goto again end\nreturn n")] + [TestCase( + "local t = {value = 1}\nfunction t:bump(x)\n self.value = self.value + x\n return self.value\nend\nreturn t:bump(2)" + )] + [TestCase("local x = 1\nlocal y =\n x\n + 2\nreturn y")] + [TestCase("--[[ a\nb ]]\nlocal s = [[line1\nline2]]\nreturn #s")] + [TestCase( + "local function make(a)\n return function(b)\n return a + b\n end\nend\nreturn make(1)(2)" + )] + [TestCase( + "local s = 0\nfor i = 1, 10 do\n if i == 3 then break end\n s = s + i\nend\nreturn s" + )] public async Task BreakpointLineEventsMatchLuaLineHooks(string source) { async Task Run(bool debug) diff --git a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs index 065aec6d..1ae821da 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs @@ -21,4 +21,13 @@ public void TrapEncodingsStayOutsideThePublishedInstructionSet() ); Assert.That(Instruction.SizeOp, Is.EqualTo(6)); } + + [Test] + public void TrapEncodingDoesNotOverflowTheInstructionNameTable() + { + // Diagnostics must not throw while a debugger checkpoint is on the stack. + var trap = new Instruction(63); + Assert.That(trap.ToString(), Is.EqualTo("TRAP 63")); + Assert.That(trap.OpCode, Is.EqualTo((OpCode)63)); + } } From afca1e4e6d1e0d535ff6d4f59d135a515a0223da Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 22:56:54 +0900 Subject: [PATCH 08/21] fix(stdlib): emit one set of parentheses around argument type errors BadArgument's (expected, actual) overloads pre-wrapped their text while the single-message overload wraps it again, so every type mismatch printed either "(string expected, got boolean))" or "((tonumber expected, got base out of range))". Lua 5.2 prints exactly one pair: "bad argument #1 to 'rep' (string expected, got boolean)". The four overloads now hand the wrapper unwrapped text, and ArgumentErrorFormatTests asserts the exact wording plus the absence of a doubled closing parenthesis for string, table, math and byte checks. --- src/Lua/Exceptions.cs | 9 +++--- tests/Lua.Tests/ArgumentErrorFormatTests.cs | 36 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tests/Lua.Tests/ArgumentErrorFormatTests.cs diff --git a/src/Lua/Exceptions.cs b/src/Lua/Exceptions.cs index 3c7a465f..0ebaa0cd 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -377,10 +377,11 @@ public static void BadArgument( LuaValueType actual ) { + // The single-message overload owns the surrounding parentheses. BadArgument( state, argumentId, - $"{LuaValue.ToString(expected)} expected, got {LuaValue.ToString(actual)})" + $"{LuaValue.ToString(expected)} expected, got {LuaValue.ToString(actual)}" ); } @@ -394,13 +395,13 @@ LuaValueType actual BadArgument( state, argumentId, - $"({string.Join(" or ", expected.Select(LuaValue.ToString))} expected, got {LuaValue.ToString(actual)})" + $"{string.Join(" or ", expected.Select(LuaValue.ToString))} expected, got {LuaValue.ToString(actual)}" ); } public static void BadArgument(LuaState state, int argumentId, string expected, string actual) { - BadArgument(state, argumentId, $"({expected} expected, got {actual})"); + BadArgument(state, argumentId, $"{expected} expected, got {actual}"); } public static void BadArgument(LuaState state, int argumentId, string[] expected, string actual) @@ -410,7 +411,7 @@ public static void BadArgument(LuaState state, int argumentId, string[] expected throw new ArgumentException("Expected array must not be empty", nameof(expected)); } - BadArgument(state, argumentId, $"({string.Join(" or ", expected)} expected, got {actual})"); + BadArgument(state, argumentId, $"{string.Join(" or ", expected)} expected, got {actual}"); } public static void BadArgument(LuaState state, int argumentId, string message) diff --git a/tests/Lua.Tests/ArgumentErrorFormatTests.cs b/tests/Lua.Tests/ArgumentErrorFormatTests.cs new file mode 100644 index 00000000..c2a8ade1 --- /dev/null +++ b/tests/Lua.Tests/ArgumentErrorFormatTests.cs @@ -0,0 +1,36 @@ +using Lua.Standard; + +namespace Lua.Tests; + +/// +/// Lua 5.2 renders argument checks as "bad argument #1 to 'rep' (string expected, got boolean)"; +/// the surrounding parentheses belong to the message wrapper and must appear exactly once. +/// +public class ArgumentErrorFormatTests +{ + [TestCase("return string.rep(true)", "bad argument #1 to 'rep' (string expected, got boolean)")] + [TestCase("return table.concat(1)", "bad argument #1 to 'concat' (table expected, got number)")] + [TestCase("return math.abs('x')", "bad argument #1 to 'abs' (number expected, got string)")] + [TestCase("return string.byte(false)", "bad argument #1 to 'byte' (string expected, got boolean)")] + public async Task TypeErrorsUseSingleParentheses(string source, string expected) + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync( + async () => await state.DoStringAsync(source, "=argument-format") + ); + Assert.That(exception!.Message, Does.Contain(expected)); + Assert.That(exception.Message, Does.Not.Contain("))")); + } + + [Test] + public async Task MissingArgumentKeepsLuaWording() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync( + async () => await state.DoStringAsync("return string.format('%d')") + ); + Assert.That(exception!.Message, Does.Contain("bad argument #2 to 'format' (no value)")); + } +} From 162068c0158ce962e4520d21fe485436ed6fb71c Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:10:18 +0900 Subject: [PATCH 09/21] fix(runtime): report ordering failures with Lua's compare wording Comparison failures named the internal operator, so `1 < 'a'` produced "attempt to lt a number value with a string value". luaG_ordererror instead describes the operands: "attempt to compare number with string", or "attempt to compare two table values" when both types match. The opcode path passes "compare" and table.sort's comparator passes "lt", so both spellings are routed through one helper. OperatorTests asserted the previous wording; they now expect Lua 5.2's, and ArgumentErrorFormatTests covers number/string, table/number, table/table and a mixed table.sort. --- src/Lua/Exceptions.cs | 24 ++++++++++++++ tests/Lua.Tests/ArgumentErrorFormatTests.cs | 35 ++++++++++++++++++--- tests/Lua.Tests/OperatorTests.cs | 15 ++------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/Lua/Exceptions.cs b/src/Lua/Exceptions.cs index 0ebaa0cd..1c211bbd 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -227,6 +227,12 @@ public Traceback? LuaTraceback public static void AttemptInvalidOperation(LuaState? state, string op, LuaValue a, LuaValue b) { + // Ordering follows luaG_ordererror: "compare X with Y", not the operator's own name. + if (op is "lt" or "le" or "compare") + { + ThrowComparisonFailed(state, a, b); + } + var typeA = a.TypeToString(); var typeB = b.TypeToString(); if (typeA == typeB) @@ -240,6 +246,18 @@ public static void AttemptInvalidOperation(LuaState? state, string op, LuaValue ); } + static void ThrowComparisonFailed(LuaState? state, LuaValue a, LuaValue b) + { + var typeA = LuaValue.ToString(a.Type); + var typeB = LuaValue.ToString(b.Type); + if (a.Type == b.Type) + { + throw new LuaRuntimeException(state, $"attempt to compare two {typeA} values"); + } + + throw new LuaRuntimeException(state, $"attempt to compare {typeA} with {typeB}"); + } + public static void AttemptInvalidOperation(LuaState? state, string op, LuaValue a) { throw new LuaRuntimeException(state, $"attempt to {op} a {a.TypeToString()} value"); @@ -263,6 +281,12 @@ int regB ? state.Stack[caller.Base + regB] : ((LuaClosure)caller.Function).Proto.Constants[regB - 256]; var function = caller.Function; + if (op is "compare") + { + // luaG_ordererror describes the values; it never names the operator or the locals. + ThrowComparisonFailed(state, luaValueA, luaValueB); + } + var tA = LuaDebug.GetName(((LuaClosure)function).Proto, lastPc, regA, out var nameA); var tB = LuaDebug.GetName(((LuaClosure)function).Proto, lastPc, regB, out var nameB); diff --git a/tests/Lua.Tests/ArgumentErrorFormatTests.cs b/tests/Lua.Tests/ArgumentErrorFormatTests.cs index c2a8ade1..5716fb84 100644 --- a/tests/Lua.Tests/ArgumentErrorFormatTests.cs +++ b/tests/Lua.Tests/ArgumentErrorFormatTests.cs @@ -11,13 +11,16 @@ public class ArgumentErrorFormatTests [TestCase("return string.rep(true)", "bad argument #1 to 'rep' (string expected, got boolean)")] [TestCase("return table.concat(1)", "bad argument #1 to 'concat' (table expected, got number)")] [TestCase("return math.abs('x')", "bad argument #1 to 'abs' (number expected, got string)")] - [TestCase("return string.byte(false)", "bad argument #1 to 'byte' (string expected, got boolean)")] + [TestCase( + "return string.byte(false)", + "bad argument #1 to 'byte' (string expected, got boolean)" + )] public async Task TypeErrorsUseSingleParentheses(string source, string expected) { using var state = LuaState.Create(); state.OpenStandardLibraries(); - var exception = Assert.ThrowsAsync( - async () => await state.DoStringAsync(source, "=argument-format") + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync(source, "=argument-format") ); Assert.That(exception!.Message, Does.Contain(expected)); Assert.That(exception.Message, Does.Not.Contain("))")); @@ -28,9 +31,31 @@ public async Task MissingArgumentKeepsLuaWording() { using var state = LuaState.Create(); state.OpenStandardLibraries(); - var exception = Assert.ThrowsAsync( - async () => await state.DoStringAsync("return string.format('%d')") + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync("return string.format('%d')") ); Assert.That(exception!.Message, Does.Contain("bad argument #2 to 'format' (no value)")); } + + /// + /// Ordering failures follow luaG_ordererror, which describes the values instead of naming the + /// internal operator ("lt"); table.sort reports the same message for mixed element types. + /// + [TestCase("return 1 < 'a'", "attempt to compare number with string")] + [TestCase("return {} < 1", "attempt to compare table with number")] + [TestCase("return {} < {}", "attempt to compare two table values")] + [TestCase( + "local t = {1, 'a', 2} return table.sort(t)", + "attempt to compare string with number" + )] + public async Task ComparisonErrorsDescribeTheValues(string source, string expected) + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync(source, "=comparison") + ); + Assert.That(exception!.Message, Does.Contain(expected)); + Assert.That(exception.Message, Does.Not.Contain("attempt to lt")); + } } diff --git a/tests/Lua.Tests/OperatorTests.cs b/tests/Lua.Tests/OperatorTests.cs index 15152f1e..29436b73 100644 --- a/tests/Lua.Tests/OperatorTests.cs +++ b/tests/Lua.Tests/OperatorTests.cs @@ -13,10 +13,7 @@ public void NilGreaterThan_ThrowsRuntimeException() await state.DoStringAsync("local test = nil > 5").AsTask() ); - Assert.That( - ex!.Message, - Does.Contain("attempt to compare a number value with a nil value") - ); + Assert.That(ex!.Message, Does.Contain("attempt to compare number with nil")); } [Test] @@ -28,10 +25,7 @@ public void NilGreaterThanOrEquals_ThrowsRuntimeException() await state.DoStringAsync("local test = nil >= 5").AsTask() ); - Assert.That( - ex!.Message, - Does.Contain("attempt to compare a number value with a nil value") - ); + Assert.That(ex!.Message, Does.Contain("attempt to compare number with nil")); } [Test] @@ -43,10 +37,7 @@ public void GreaterThanOrEqualsNil_ThrowsRuntimeException() await state.DoStringAsync("local test = 5 >= nil").AsTask() ); - Assert.That( - ex!.Message, - Does.Contain("attempt to compare a nil value with a number value") - ); + Assert.That(ex!.Message, Does.Contain("attempt to compare nil with number")); } [TestCase(1, 6)] From 335dc41457a7dd44d810fb3dacfaea41ddaa6f8b Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:18:50 +0900 Subject: [PATCH 10/21] fix(stdlib): raise invalid key to 'next' for keys outside the table `next(t, key)` returned nil both when iteration finished and when the key was never part of the table, so the second case silently ended a traversal instead of reporting it. Lua 5.2, and 5.3/5.4 which use the same findindex, raise "invalid key to 'next'" for that case only. The dictionary already models Lua's dead keys: clearing a field keeps its slot with a weak reference so the next() cursor stays valid, which is why clearing fields during traversal is allowed. The fix surfaces that existing distinction instead of testing membership, which is what broke nextvar.lua and the collected-key lifetime test: - LuaValueDictionary.TryGetNext returns Found/End/InvalidKey from FindIterationIndex. - LuaTable exposes TryGetNextStatus; the public TryGetNext keeps its bool contract. - next() maps InvalidKey to the Lua error. - Deleting the last live entry now keeps its dead slot, and the next insertion into an all-dead table compacts it, mirroring Lua's rehash so a stale cursor becomes invalid. Verified case by case against a native Lua 5.2 build (including single-key clear, clear-all-while-iterating, delete-then-insert, number/table keys, array holes and a replaced table); all twelve agree. NextIterationTests pins them, and the full suite (3518 tests, official Lua 5.2 suite included) still passes. --- src/Lua/Internal/LuaValueDictionary.cs | 48 +++++++--- src/Lua/LuaNextStatus.cs | 18 ++++ src/Lua/LuaTable.cs | 26 +++--- src/Lua/Standard/BasicLibrary.cs | 17 ++-- tests/Lua.Tests/NextIterationTests.cs | 119 +++++++++++++++++++++++++ 5 files changed, 196 insertions(+), 32 deletions(-) create mode 100644 src/Lua/LuaNextStatus.cs create mode 100644 tests/Lua.Tests/NextIterationTests.cs diff --git a/src/Lua/Internal/LuaValueDictionary.cs b/src/Lua/Internal/LuaValueDictionary.cs index 7c6db950..a473d557 100644 --- a/src/Lua/Internal/LuaValueDictionary.cs +++ b/src/Lua/Internal/LuaValueDictionary.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; @@ -196,6 +196,13 @@ void Insert(LuaValue key, LuaValue value) return; } + if (_count != 0 && _nilCount == _count) + { + // Every entry is a dead key left by clearing during traversal. Lua drops those when a + // new key forces a rehash, so a cursor pointing at one stops being valid after this. + Clear(); + } + if (_buckets == null) { Initialize(0); @@ -330,7 +337,17 @@ void Delete(LuaValue key) if (_nilCount + 1 == Count) { - Clear(); + // Keep the dead entry instead of dropping it: Lua allows clearing the key that `next` + // just returned, so that key must remain findable. The next insertion compacts or + // reuses this storage as usual. + ref var deleted = ref _entries![index]; + if (deleted.keyObject is { } lastObject) + { + deleted.keyObject = CreateDeletedKey(lastObject); + deleted.keyType = LuaValueType.Nil; + } + deleted.value = default; + _nilCount++; return; } @@ -428,25 +445,30 @@ public bool TryGetValue(LuaValue key, out LuaValue value) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool TryGetNext(LuaValue key, out KeyValuePair pair) + public LuaNextStatus TryGetNext(LuaValue key, out KeyValuePair pair) { var index = FindIterationIndex(key); - if (index >= 0) + if (index < 0) { - var entries = _entries!; - while (++index < _count) + // The key is not in the table at all. A key that was cleared during traversal keeps a + // dead entry (see Delete), so it is still found above and iteration continues. + pair = default; + return LuaNextStatus.InvalidKey; + } + + var entries = _entries!; + while (++index < _count) + { + ref var entry = ref entries[index]; + if (entry is { next: >= -1, value.Type: not LuaValueType.Nil }) { - ref var entry = ref entries[index]; - if (entry is { next: >= -1, value.Type: not LuaValueType.Nil }) - { - pair = new(new(entry.keyType, entry.keyNumber, entry.keyObject), entry.value); - return true; - } + pair = new(new(entry.keyType, entry.keyNumber, entry.keyObject), entry.value); + return LuaNextStatus.Found; } } pair = default; - return false; + return LuaNextStatus.End; } int FindIterationIndex(LuaValue key) diff --git a/src/Lua/LuaNextStatus.cs b/src/Lua/LuaNextStatus.cs new file mode 100644 index 00000000..01a7e85e --- /dev/null +++ b/src/Lua/LuaNextStatus.cs @@ -0,0 +1,18 @@ +namespace Lua; + +/// Outcome of advancing a raw next iteration. +internal enum LuaNextStatus +{ + /// Another key/value pair was found. + Found, + + /// The iteration reached the end of the table. + End, + + /// + /// The supplied key is not part of the table, so the iteration cannot continue. + /// Lua raises invalid key to 'next' for this case; a key cleared during traversal keeps a + /// dead entry and does not reach this state. + /// + InvalidKey, +} diff --git a/src/Lua/LuaTable.cs b/src/Lua/LuaTable.cs index dd6212ee..85bc541b 100644 --- a/src/Lua/LuaTable.cs +++ b/src/Lua/LuaTable.cs @@ -353,7 +353,14 @@ internal bool TryRemoveWithinArray(int index, int length, out LuaValue value) return true; } - public bool TryGetNext(LuaValue key, out KeyValuePair pair) + public bool TryGetNext(LuaValue key, out KeyValuePair pair) => + TryGetNextStatus(key, out pair) == LuaNextStatus.Found; + + /// + /// Iterates the table like next. The status separates "iteration finished" from "the key + /// is not in this table", which next reports as an error. + /// + internal LuaNextStatus TryGetNextStatus(LuaValue key, out KeyValuePair pair) { var index = -1; if (key.Type is LuaValueType.Nil) @@ -373,7 +380,7 @@ public bool TryGetNext(LuaValue key, out KeyValuePair pair) if (span[i].Type is not LuaValueType.Nil) { pair = new(index + i + 1, span[i]); - return true; + return LuaNextStatus.Found; } } @@ -382,20 +389,15 @@ public bool TryGetNext(LuaValue key, out KeyValuePair pair) if (kv.Value.Type is not LuaValueType.Nil) { pair = kv; - return true; + return LuaNextStatus.Found; } } - } - else - { - if (dictionary.TryGetNext(key, out pair)) - { - return true; - } + + pair = default; + return LuaNextStatus.End; } - pair = default; - return false; + return dictionary.TryGetNext(key, out pair); } public void Clear() diff --git a/src/Lua/Standard/BasicLibrary.cs b/src/Lua/Standard/BasicLibrary.cs index ece3fa77..08296b44 100644 --- a/src/Lua/Standard/BasicLibrary.cs +++ b/src/Lua/Standard/BasicLibrary.cs @@ -386,13 +386,16 @@ CancellationToken cancellationToken var arg0 = context.GetArgument(0); var arg1 = context.HasArgument(1) ? context.Arguments[1] : LuaValue.Nil; - if (arg0.TryGetNext(arg1, out var kv)) - { - return new(context.Return(kv.Key, kv.Value)); - } - else - { - return new(context.Return(LuaValue.Nil)); + switch (arg0.TryGetNextStatus(arg1, out var kv)) + { + case LuaNextStatus.Found: + return new(context.Return(kv.Key, kv.Value)); + case LuaNextStatus.InvalidKey: + // Lua 5.2 to 5.4 all reject a key that is not in the table, while a field cleared + // during the traversal remains a valid cursor. + throw new LuaRuntimeException(context.State, "invalid key to 'next'"); + default: + return new(context.Return(LuaValue.Nil)); } } diff --git a/tests/Lua.Tests/NextIterationTests.cs b/tests/Lua.Tests/NextIterationTests.cs new file mode 100644 index 00000000..2af167d6 --- /dev/null +++ b/tests/Lua.Tests/NextIterationTests.cs @@ -0,0 +1,119 @@ +using Lua.Standard; + +namespace Lua.Tests; + +/// +/// Lua 5.2 (and 5.3/5.4) reject a next cursor that is not part of the table, while a field +/// cleared during traversal stays a valid cursor. All expectations here were taken from a native +/// Lua 5.2 build. +/// +public class NextIterationTests +{ + static LuaState CreateState() + { + var state = LuaState.Create(); + state.OpenStandardLibraries(); + return state; + } + + [Test] + public async Task RejectsAKeyThatIsNotInTheTable() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local t = {a = 1} return pcall(next, t, 'missing')", + "=next" + ); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain("invalid key to 'next'")); + } + + [Test] + public async Task RejectsAnIntegerKeyAboveTheArrayPart() + { + using var state = CreateState(); + var results = await state.DoStringAsync("return pcall(next, {}, 1)", "=next"); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain("invalid key to 'next'")); + } + + [Test] + public async Task ClearedLastKeyStillEndsTheIteration() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local t = {a = 1}\n" + + "local k = next(t)\n" + + "t[k] = nil\n" + + "local ok, v = pcall(next, t, k)\n" + + "return ok, v == nil", + "=next" + ); + Assert.That(results[0].Read(), Is.True); + Assert.That(results[1].Read(), Is.True); + } + + [Test] + public async Task ClearedKeyContinuesToTheNextEntry() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local t = {a = 1, b = 2, c = 3}\n" + + "local k = next(t)\n" + + "t[k] = nil\n" + + "local ok, n = pcall(next, t, k)\n" + + "return ok, n ~= nil", + "=next" + ); + Assert.That(results[0].Read(), Is.True); + Assert.That(results[1].Read(), Is.True); + } + + [Test] + public async Task ClearingEveryKeyDuringPairsCompletes() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local t = {a = 1, b = 2, c = 3}\n" + + "local n = 0\n" + + "for k in pairs(t) do t[k] = nil n = n + 1 end\n" + + "return n, next(t) == nil", + "=next" + ); + Assert.That(results[0].Read(), Is.EqualTo(3)); + Assert.That(results[1].Read(), Is.True); + } + + [Test] + public async Task InsertingAfterClearingInvalidatesTheCursor() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local t = {a = 1}\n" + + "local k = next(t)\n" + + "t[k] = nil\n" + + "t.b = 2\n" + + "return pcall(next, t, k)", + "=next" + ); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain("invalid key to 'next'")); + } + + [Test] + public async Task NilKeyAndArrayHolesStayValid() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local ok = pcall(next, {}, nil)\n" + + "local t = {1, 2, 3}\n" + + "t[2] = nil\n" + + "local k, v = next(t, 1)\n" + + "return ok, k, v", + "=next" + ); + Assert.That(results[0].Read(), Is.True); + Assert.That(results[1].Read(), Is.EqualTo(3)); + Assert.That(results[2].Read(), Is.EqualTo(3)); + } +} From 784da468fcb043405da0b8684f7e0f69fc1fe5ab Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:51:05 +0900 Subject: [PATCH 11/21] fix(stdlib): match Lua's pattern wording and function names in errors A 60-case differential run against native Lua 5.2 (patterns, string.format, #, io, debug, load, os.date, table) left three real differences: - malformed patterns printed "(ends with %)" without the quotes Lua includes. - string.format('abc%') reported "invalid option '%' to 'format'". Lua reads the missing conversion and, when no value is left, reports the missing argument: "bad argument #2 to 'string.format' (no value)". Both places that raise this now go through BadArgument, so the argument number and the function name agree with the rest of the library. - os.date hard-coded "to 'date'" instead of routing through BadArgument. Fixing the last two exposed the naming rule behind them: when a library function is called without a usable call-site name (through pcall, or from a C# callback), luaL_argerror falls back to the function's global path by searching the globals, so it reports "string.format" or "os.date"; a field call still reports "format". GetCurrentFunctionName now implements that fallback, searching _G and its one-level tables while skipping underscore entries. The same corpus now differs only in debug.getinfo's short_src, which deliberately prints "[C#]" instead of "[C]". Six regression tests pin the new messages, and the suite (3524 tests, official Lua 5.2 suite included) passes. --- src/Lua/Exceptions.cs | 57 ++++++++++++++++-- src/Lua/Standard/Internal/DateTimeHelper.cs | 6 +- src/Lua/Standard/Internal/MatchState.cs | 2 +- src/Lua/Standard/StringLibrary.cs | 15 +++-- tests/Lua.Tests/ArgumentErrorFormatTests.cs | 65 +++++++++++++++++++++ 5 files changed, 132 insertions(+), 13 deletions(-) diff --git a/src/Lua/Exceptions.cs b/src/Lua/Exceptions.cs index 1c211bbd..435c5733 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -373,20 +373,65 @@ internal static (string NameWhat, string Name) GetCurrentFunctionName(LuaState s { var frames = state.GetCallStackFrames(); if (frames.Length < 2) - return ("function", current.Function.Name); + return GlobalOrFunctionName(state, current.Function); var caller = frames[^2]; callerPrototype = (caller.Function as LuaClosure)?.Proto; } if (callerPrototype is null) { - return ("function", current.Function.Name); + // The caller is a host function (pcall, a C# callback), so the call site cannot name + // this function; Lua then searches the globals for it. + return GlobalOrFunctionName(state, current.Function); } - return ( - LuaDebug.GetFuncName(callerPrototype, pc, out var name) ?? "", - name ?? current.Function.Name - ); + var nameWhat = LuaDebug.GetFuncName(callerPrototype, pc, out var name) ?? ""; + if ( + string.IsNullOrEmpty(name) + && FindGlobalFunctionName(state, current.Function) is { } global + ) + return (nameWhat, global); + + return (nameWhat, name ?? current.Function.Name); + } + + static (string NameWhat, string Name) GlobalOrFunctionName( + LuaState state, + LuaFunction function + ) => + FindGlobalFunctionName(state, function) is { } global + ? ("global", global) + : ("function", function.Name); + + /// + /// Mirrors Lua's pushglobalfuncname: when a library function is called without a usable call + /// site name, its registered path (for example string.format) is reported instead. + /// + static string? FindGlobalFunctionName(LuaState state, LuaFunction function) + { + foreach (var pair in state.GlobalState.Environment) + { + if (pair.Key.Type is not LuaValueType.String) + continue; + var key = pair.Key.Read(); + if (key.Length == 0 || key[0] == '_') + continue; + if (pair.Value.TryReadFunction(out var value) && ReferenceEquals(value, function)) + return key; + if (!pair.Value.TryRead(out var table)) + continue; + foreach (var inner in table) + { + if (inner.Key.Type is not LuaValueType.String) + continue; + if ( + inner.Value.TryReadFunction(out var nested) && ReferenceEquals(nested, function) + ) + return key + "." + inner.Key.Read(); + } + } + + return null; } public static void BadArgument(LuaState state, int argumentId) diff --git a/src/Lua/Standard/Internal/DateTimeHelper.cs b/src/Lua/Standard/Internal/DateTimeHelper.cs index 556f66da..75ce9064 100644 --- a/src/Lua/Standard/Internal/DateTimeHelper.cs +++ b/src/Lua/Standard/Internal/DateTimeHelper.cs @@ -195,9 +195,11 @@ TimeZoneInfo zone [MethodImpl(MethodImplOptions.NoInlining)] static void ThrowInvalidConversionSpecifier(LuaState state, ReadOnlySpan format) { - throw new LuaRuntimeException( + // Route through BadArgument so an indirect call reports "os.date" like Lua does. + LuaRuntimeException.BadArgument( state, - $"bad argument #1 to 'date' (invalid conversion specifier '{format.ToString()}')" + 1, + $"invalid conversion specifier '{format.ToString()}'" ); } diff --git a/src/Lua/Standard/Internal/MatchState.cs b/src/Lua/Standard/Internal/MatchState.cs index ff7da8a9..ad3161a1 100644 --- a/src/Lua/Standard/Internal/MatchState.cs +++ b/src/Lua/Standard/Internal/MatchState.cs @@ -430,7 +430,7 @@ int ClassEnd(ReadOnlySpan pattern, int pIdx) case LEsc: if (pIdx >= pattern.Length) { - throw new LuaRuntimeException(State, "malformed pattern (ends with %)"); + throw new LuaRuntimeException(State, "malformed pattern (ends with '%')"); } return pIdx + 1; diff --git a/src/Lua/Standard/StringLibrary.cs b/src/Lua/Standard/StringLibrary.cs index e2b9bfea..17428dc1 100644 --- a/src/Lua/Standard/StringLibrary.cs +++ b/src/Lua/Standard/StringLibrary.cs @@ -129,7 +129,17 @@ CancellationToken cancellationToken { i++; if (i == format.Length) + { + // Lua reads the missing conversion as '\0' and, when no argument is left, + // reports the missing value for that argument instead of an invalid option. + if (parameterIndex >= context.ArgumentCount) + LuaRuntimeException.BadArgument( + context.State, + parameterIndex + 1, + "no value" + ); throw new LuaRuntimeException(context.State, "invalid option '%' to 'format'"); + } // escape if (format[i] == '%') @@ -199,10 +209,7 @@ CancellationToken cancellationToken if (context.ArgumentCount <= parameterIndex) { - throw new LuaRuntimeException( - context.State, - $"bad argument #{parameterIndex + 1} to 'format' (no value)" - ); + LuaRuntimeException.BadArgument(context.State, parameterIndex + 1, "no value"); } var parameter = context.GetArgument(parameterIndex++); diff --git a/tests/Lua.Tests/ArgumentErrorFormatTests.cs b/tests/Lua.Tests/ArgumentErrorFormatTests.cs index 5716fb84..2ffca122 100644 --- a/tests/Lua.Tests/ArgumentErrorFormatTests.cs +++ b/tests/Lua.Tests/ArgumentErrorFormatTests.cs @@ -58,4 +58,69 @@ await state.DoStringAsync(source, "=comparison") Assert.That(exception!.Message, Does.Contain(expected)); Assert.That(exception.Message, Does.Not.Contain("attempt to lt")); } + + [Test] + public async Task PatternErrorsQuoteTheOffendingCharacter() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync("return ('x'):find('%')", "=pattern") + ); + Assert.That(exception!.Message, Does.Contain("malformed pattern (ends with '%')")); + } + + /// + /// A trailing '%' makes Lua look for the value of the missing conversion, so the error names + /// the absent argument rather than the (invisible) option character. + /// + [TestCase( + "return pcall(string.format, 'abc%')", + "bad argument #2 to 'string.format' (no value)" + )] + [TestCase( + "return pcall(function() return string.format('abc%') end)", + "bad argument #2 to 'format' (no value)" + )] + public async Task TrailingFormatConversionReportsTheMissingValue(string source, string expected) + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var results = await state.DoStringAsync(source, "=format"); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain(expected)); + } + + /// + /// Lua's luaL_argerror falls back to the function's global path when the call site cannot name + /// it (an indirect call through pcall), while a field call keeps the short name. + /// + [TestCase( + "return pcall(string.byte, false)", + "bad argument #1 to 'string.byte' (string expected, got boolean)" + )] + [TestCase( + "return pcall(os.date, '!%Q')", + "bad argument #1 to 'os.date' (invalid conversion specifier '%Q')" + )] + public async Task IndirectCallsUseTheGlobalFunctionName(string source, string expected) + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var results = await state.DoStringAsync(source, "=names"); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain(expected)); + } + + [Test] + public async Task DirectCallsKeepTheShortFunctionName() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync("return string.byte(false)", "=names") + ); + Assert.That(exception!.Message, Does.Contain("bad argument #1 to 'byte'")); + Assert.That(exception.Message, Does.Not.Contain("string.byte")); + } } From 5395c1fe6dce054cf852bda6c630f9eeea2e2c5d Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 00:32:57 +0900 Subject: [PATCH 12/21] fix(stdlib): stop wrapping complete argument messages as "expected, got" ThrowBadArgument forwarded its message as the *expected type*, so every caller rendered "(select expected, got index out of range)" or "(debug.getinfo expected, got invalid option)" instead of Lua's "(index out of range)" / "(invalid option)". Eleven call sites (select, tonumber base range, math.random interval, debug options and levels, upvalue indices) were affected. The helper now hands the message to BadArgument unchanged. debug.getinfo also reported the options argument as #1; Lua numbers it #2 since the level is #1. Verified with the Lua 5.2 oracle: debug.getinfo(1,'z') now matches exactly, and select/tonumber messages match apart from the known tail-call position prefix. Suite: 3524 tests pass. --- src/Lua/LuaFunctionExecutionContext.cs | 9 +++------ src/Lua/Standard/DebugLibrary.cs | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Lua/LuaFunctionExecutionContext.cs b/src/Lua/LuaFunctionExecutionContext.cs index 1eacf58e..4ad57260 100644 --- a/src/Lua/LuaFunctionExecutionContext.cs +++ b/src/Lua/LuaFunctionExecutionContext.cs @@ -230,12 +230,9 @@ public Span GetReturnBuffer(int count) internal void ThrowBadArgument(int index, string message) { - LuaRuntimeException.BadArgument( - State, - index, - State.GetCurrentFrame().Function.Name, - message - ); + // Callers pass a complete Lua message ("index out of range", "base out of range"); wrapping + // it as an "expected, got" pair produced "(select expected, got index out of range)". + LuaRuntimeException.BadArgument(State, index, message); } void ThrowIfArgumentNotExists(int index) diff --git a/src/Lua/Standard/DebugLibrary.cs b/src/Lua/Standard/DebugLibrary.cs index e1b92c06..a860a3bd 100644 --- a/src/Lua/Standard/DebugLibrary.cs +++ b/src/Lua/Standard/DebugLibrary.cs @@ -1,4 +1,4 @@ -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using Lua.Internal; using Lua.Internal.CompilerServices; using Lua.Runtime; @@ -595,7 +595,7 @@ out var isValid ); if (!isValid) { - context.ThrowBadArgument(argOffset + 1, "invalid option"); + context.ThrowBadArgument(argOffset + 2, "invalid option"); } LuaTable table = new(0, 1); From d07b57e3ff0923b8f8299f790a8cfe86a191ec6f Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 00:54:56 +0900 Subject: [PATCH 13/21] fix(runtime): report the calling line for tail calls into host functions Lua 5.2 reuses a call frame only for Lua closures: `return host()` is an ordinary call, so an error raised inside the host function still reports the line that made the call. This engine discards the caller's frame for native tail calls (so values held only by that caller stay collectable while the native call is suspended, a guarantee tail-call lifetime tests enforce), which left such errors without any position: `bad argument #1 to 'rep' (...)` instead of `chunk:2: bad argument ...`. The frame now records the discarded caller's prototype and call-site pc. Holding a prototype keeps no caller values alive, so the lifetime guarantee is unchanged. WriteLastLuaTrace consults that record when the innermost frame is such a tail call. Its loop previously required at least two frames, but an unwound trace commonly holds only the host frame, so the search now also inspects index 0. The differential corpus drops from 19 to 10 differences; the rest are platform NaN text, `[C#]` branding, a `table.remove` argument number, error-position prefixes Lua omits for `next`/`yield`, and one real gap left open: debug.getinfo still reports the grandparent for the level above a tail-called host function, because the frame itself is gone. 3527 tests pass, including the tail-call lifetime suite; the new tests fail against the pre-fix library. --- src/Lua/Runtime/CallStackFrame.cs | 10 +++ src/Lua/Runtime/LuaVirtualMachine.cs | 9 +++ src/Lua/Runtime/Tracebacks.cs | 53 ++++++++++----- tests/Lua.Tests/TailCallErrorPositionTests.cs | 66 +++++++++++++++++++ 4 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 tests/Lua.Tests/TailCallErrorPositionTests.cs diff --git a/src/Lua/Runtime/CallStackFrame.cs b/src/Lua/Runtime/CallStackFrame.cs index a7ae3ed7..cdf7effd 100644 --- a/src/Lua/Runtime/CallStackFrame.cs +++ b/src/Lua/Runtime/CallStackFrame.cs @@ -13,6 +13,16 @@ public record struct CallStackFrame internal CallStackFrameFlags Flags; public int Version; + /// + /// Prototype of the caller that was discarded by a tail call into a host function, and the + /// index of its call instruction. Lua keeps that caller for C functions; this engine releases + /// it, so the call site is recorded instead: a prototype does not keep the caller's values + /// alive, unlike the frame itself. + /// + internal Prototype? DiscardedCallerPrototype; + + internal int DiscardedCallerPc; + internal bool IsTailCall => (Flags & CallStackFrameFlags.TailCall) == CallStackFrameFlags.TailCall; } diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index 35db3ee6..ce2d0e3e 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -1778,6 +1778,15 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart) newFrame.CallerInstructionIndex = lastFrame.CallerInstructionIndex; newFrame.Flags |= lastFrame.Flags & CallStackFrameFlags.ReversedLe; + if (func is not LuaClosure) + { + // The caller frame is discarded below, so remember where it called from. Errors and + // tracebacks report that position; only the prototype is kept, not the caller, so its + // values stay collectable while a native callee is suspended. + newFrame.DiscardedCallerPrototype = context.Prototype; + newFrame.DiscardedCallerPc = context.Pc; + } + state.ReplaceCallStackFrame(newFrame); // Release the replaced closure before a hook or native tail callee can suspend. context.LuaClosure = null!; diff --git a/src/Lua/Runtime/Tracebacks.cs b/src/Lua/Runtime/Tracebacks.cs index 4a6bdfcd..ab13b4ca 100644 --- a/src/Lua/Runtime/Tracebacks.cs +++ b/src/Lua/Runtime/Tracebacks.cs @@ -24,34 +24,55 @@ internal static void WriteLastLuaTrace( return; } - var intFormatBuffer = (stackalloc char[15]); - var shortSourceBuffer = (stackalloc char[59]); - for (var index = stackFrames.Length - level; index >= 1; index--) + for (var index = stackFrames.Length - level; index >= 0; index--) { - var lastFunc = stackFrames[index - 1].Function; var frame = stackFrames[index]; - if (!frame.IsTailCall && lastFunc is LuaClosure closure) + if ( + index >= 1 + && !frame.IsTailCall + && stackFrames[index - 1].Function is LuaClosure closure + ) { var p = closure.Proto; if ((uint)frame.CallerInstructionIndex >= (uint)p.LineInfo.Length) return; - var len = LuaDebug.WriteShortSource(p.ChunkName, shortSourceBuffer); - list.AddRange(shortSourceBuffer[..len]); - list.AddRange(":"); - p.LineInfo[frame.CallerInstructionIndex] - .TryFormat( - intFormatBuffer, - out var charsWritten, - provider: CultureInfo.InvariantCulture - ); - list.AddRange(intFormatBuffer[..charsWritten]); + WritePosition(ref list, p, frame.CallerInstructionIndex); + return; + } - list.AddRange(": "); + // A tail call into a host function discards the caller's frame. Its call site is kept + // on the frame so the error still reports the line that made the call, even when the + // unwound trace holds only that frame. + if ( + index == stackFrames.Length - 1 + && frame.DiscardedCallerPrototype is { } callerPrototype + && (uint)frame.DiscardedCallerPc < (uint)callerPrototype.LineInfo.Length + ) + { + WritePosition(ref list, callerPrototype, frame.DiscardedCallerPc); return; } } } + static void WritePosition(ref PooledList list, Prototype prototype, int pc) + { + var intFormatBuffer = (stackalloc char[15]); + var shortSourceBuffer = (stackalloc char[59]); + var len = LuaDebug.WriteShortSource(prototype.ChunkName, shortSourceBuffer); + list.AddRange(shortSourceBuffer[..len]); + list.AddRange(":"); + prototype + .LineInfo[pc] + .TryFormat( + intFormatBuffer, + out var charsWritten, + provider: CultureInfo.InvariantCulture + ); + list.AddRange(intFormatBuffer[..charsWritten]); + list.AddRange(": "); + } + internal void WriteLastLuaTrace(ref PooledList list, int level = 1) { WriteLastLuaTrace(StackFrames, ref list, level); diff --git a/tests/Lua.Tests/TailCallErrorPositionTests.cs b/tests/Lua.Tests/TailCallErrorPositionTests.cs new file mode 100644 index 00000000..92f69316 --- /dev/null +++ b/tests/Lua.Tests/TailCallErrorPositionTests.cs @@ -0,0 +1,66 @@ +using Lua.Standard; + +namespace Lua.Tests; + +/// +/// Lua 5.2 tail-calls only Lua closures, so an error raised by a library function reached through +/// return f() still reports the calling line. This engine discards the caller's frame to let +/// its values be collected, but records the call site; errors and tracebacks must report it. +/// +public class TailCallErrorPositionTests +{ + [Test] + public async Task ErrorFromTailCalledLibraryFunctionReportsTheCallingLine() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var results = await state.DoStringAsync( + "local function call()\n" + + " return string.rep(true)\n" + + "end\n" + + "local ok, err = pcall(call)\n" + + "return ok, err", + "=tailcall" + ); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain(":2: bad argument #1 to 'rep'")); + } + + [Test] + public async Task NestedTailCallsReportTheInnermostCallingLine() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var results = await state.DoStringAsync( + "local function inner()\n" + + " return table.concat(1)\n" + + "end\n" + + "local function outer()\n" + + " return inner()\n" + + "end\n" + + "local ok, err = pcall(outer)\n" + + "return ok, err", + "=nested" + ); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain(":2: bad argument #1 to 'concat'")); + } + + [Test] + public async Task OrdinaryCallsKeepReportingTheirLine() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var results = await state.DoStringAsync( + "local function call()\n" + + " local result = string.rep(true)\n" + + " return result\n" + + "end\n" + + "local ok, err = pcall(call)\n" + + "return ok, err", + "=ordinary" + ); + Assert.That(results[0].Read(), Is.False); + Assert.That(results[1].ToString(), Does.Contain(":2: bad argument #1 to 'rep'")); + } +} From c0be89b7e4a7286fef1b5b01ce922b4e02dc2665 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:11:32 +0900 Subject: [PATCH 14/21] perf(runtime): keep call frames at 32 bytes and capture tail-call sites per exception The previous commit put the discarded tail-call site on CallStackFrame, which grew the frame from 32 to 48 bytes for information only error paths read. Measured on this machine (min of 3 runs, same probe binary against each build): workload 32-byte frame 48-byte frame lua calls 5M 231 ms 483 ms host calls 2M 104 ms 223 ms coroutines 20k 21.06 MB 25.94 MB allocated deep recursion 50k 10.01 MB 12.01 MB total (fresh state) pcall errors 100k 1639 ms 1634 ms So the extra fields roughly halved call throughput. The original pre-branch 32-byte frame measures 237 / 107 ms, confirming the frame size (an extra reference field to copy and a larger struct per push/pop), not the surrounding changes, is responsible. The frame is back to the fields execution needs: Base, ReturnBase, Function, VariableArgumentCount, CallerInstructionIndex, Flags (32 bytes, plain struct instead of a record struct). Both pieces of debug-only state moved out of it: - debugger frame ids live in a per-state int[] indexed by call depth, allocated on the first use and cleared whenever a depth gets a new frame, so ordinary runs never touch it (LuaDebugger reads the current frame's id through CurrentFrameVersion); - the discarded caller's position is captured by LuaRuntimeException when it is created, while the tail call is still current, and used only when the trace yields no position. That keeps the tail-call error positions from the previous commit and drops its storage cost to zero. Verified: 3527 tests, 84 debugger and 44 DAP tests pass, including the tail-call position tests and the tail-call lifetime suite (values discarded by a native tail call remain collectable). pcall throughput is unchanged, so the capture costs nothing measurable. --- src/Lua.Debugger/LuaDebugger.cs | 2 +- src/Lua/Exceptions.cs | 41 ++++++++++++++++++++- src/Lua/LuaState.cs | 55 ++++++++++++++++++++++++++-- src/Lua/Runtime/CallStackFrame.cs | 13 +------ src/Lua/Runtime/LuaVirtualMachine.cs | 9 ----- src/Lua/Runtime/Tracebacks.cs | 23 ++---------- 6 files changed, 96 insertions(+), 47 deletions(-) diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 70402fc4..4a1aa3dd 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -226,7 +226,7 @@ static bool ShouldStep(LuaState state, StepRequest request) return depth < request.Depth || request.Mode == LuaDebugStep.Over && depth == request.Depth - && state.GetCurrentFrame().Version == request.FrameId; + && state.CurrentFrameVersion == request.FrameId; } async ValueTask SuspendAsync(PauseContext context, CancellationToken cancellationToken) diff --git a/src/Lua/Exceptions.cs b/src/Lua/Exceptions.cs index 435c5733..0913f22c 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -199,6 +199,8 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) state.ExceptionTrace.Clear(); state.CurrentException = this; State = state; + if (level <= 1) + CaptureTailCallSite(state); } ErrorObject = errorObject; @@ -207,6 +209,36 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) int level = 1; + // Where the discarded caller of a native tail call made the call. Captured when the exception + // is created, while the tail call still sits on the stack; the frame itself is released so the + // caller's values stay collectable while the native callee is suspended. + Prototype? tailCallSitePrototype; + int tailCallSitePc; + + void CaptureTailCallSite(LuaState state) + { + var frames = state.GetCallStackFrames(); + if (frames.Length == 0) + return; + var top = frames[^1]; + if (!top.IsTailCall || top.Function is LuaClosure) + return; + if (state.LastCallerPrototype is not { } prototype) + return; + if ((uint)state.LastPc >= (uint)prototype.LineInfo.Length) + return; + tailCallSitePrototype = prototype; + tailCallSitePc = state.LastPc; + } + + bool TryWriteTailCallSite(ref PooledList list) + { + if (tailCallSitePrototype is not { } prototype) + return false; + Traceback.WritePosition(ref list, prototype, tailCallSitePc); + return true; + } + Traceback? luaTraceback; public Traceback? LuaTraceback @@ -515,14 +547,17 @@ double value } } - static string CreateMessage(Traceback traceback, LuaValue errorObject, int level) + string CreateMessage(Traceback traceback, LuaValue errorObject, int level) { var pooledList = new PooledList(64); pooledList.Clear(); try { pooledList.AddRange("Lua-CSharp: "); + var before = pooledList.Count; traceback.WriteLastLuaTrace(ref pooledList, level); + if (pooledList.Count == before) + TryWriteTailCallSite(ref pooledList); pooledList.AddRange($"{errorObject}"); return pooledList.AsSpan().ToString(); } @@ -599,6 +634,8 @@ internal string MinimalMessage(LuaPlatform platform) try { Traceback.WriteLastLuaTrace(callStack, ref pooledList); + if (pooledList.Count == 0) + TryWriteTailCallSite(ref pooledList); pooledList.AddRange(message); return pooledList.AsSpan().ToString(); } @@ -618,6 +655,8 @@ internal string MinimalMessage(LuaPlatform platform) try { luaTraceback.WriteLastLuaTrace(ref pooledList, level); + if (pooledList.Count == 0) + TryWriteTailCallSite(ref pooledList); pooledList.AddRange(message); return pooledList.AsSpan().ToString(); } diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index dc927fe0..24671d30 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -180,10 +180,18 @@ class ThreadCoreData : IPoolNode internal readonly LuaStack Stack = new(); internal FastStackCore CallStack; + // Debugger frame ids, indexed by call depth. Allocated on first use so ordinary execution + // pays nothing for them; a slot is cleared whenever a new frame occupies its depth. + internal int[]? FrameVersions; + void Clear() { Stack.Clear(); CallStack.Clear(); + if (FrameVersions != null) + { + Array.Clear(FrameVersions); + } } static LinkedPool pool; @@ -299,10 +307,40 @@ public ref readonly CallStackFrame GetCurrentFrame() internal int GetDebugFrameId(int id) { - ref var frame = ref CoreData!.CallStack.PeekRef(); - if (frame.Version == 0) - frame.Version = id; - return frame.Version; + var depth = CoreData!.CallStack.Count - 1; + var versions = GetOrCreateFrameVersions(depth); + if (versions[depth] == 0) + versions[depth] = id; + return versions[depth]; + } + + /// Debugger frame id of the innermost frame, or 0 when it has none yet. + internal int CurrentFrameVersion + { + get + { + var returned = CoreData; + if (returned?.FrameVersions is not { } versions) + return 0; + var depth = returned.CallStack.Count - 1; + return (uint)depth < (uint)versions.Length ? versions[depth] : 0; + } + } + + int[] GetOrCreateFrameVersions(int depth) + { + var versions = CoreData!.FrameVersions; + if (versions == null) + { + versions = CoreData.FrameVersions = new int[Math.Max(depth + 8, 16)]; + } + else if ((uint)depth >= (uint)versions.Length) + { + Array.Resize(ref versions, Math.Max(depth + 8, versions.Length * 2)); + CoreData.FrameVersions = versions; + } + + return versions; } public ReadOnlySpan GetStackValues() @@ -374,6 +412,10 @@ internal void PushCallStackFrame(in CallStackFrame frame) CurrentException?.BuildOrGet(); CurrentException = null; callStack.Push(frame); + if (CoreData.FrameVersions is { } versions && callStack.Count <= versions.Length) + { + versions[callStack.Count - 1] = 0; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -394,6 +436,11 @@ internal void ReplaceCallStackFrame(in CallStackFrame frame) CurrentException = null; } callStack.PeekRef() = frame; + if (CoreData.FrameVersions is { } versions && callStack.Count <= versions.Length) + { + // A tail call is a new frame even though it keeps the depth. + versions[callStack.Count - 1] = 0; + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Lua/Runtime/CallStackFrame.cs b/src/Lua/Runtime/CallStackFrame.cs index cdf7effd..911c593e 100644 --- a/src/Lua/Runtime/CallStackFrame.cs +++ b/src/Lua/Runtime/CallStackFrame.cs @@ -3,7 +3,7 @@ namespace Lua.Runtime; [StructLayout(LayoutKind.Auto)] -public record struct CallStackFrame +public struct CallStackFrame { public required int Base; public required int ReturnBase; @@ -11,17 +11,6 @@ public record struct CallStackFrame public required int VariableArgumentCount; public int CallerInstructionIndex; internal CallStackFrameFlags Flags; - public int Version; - - /// - /// Prototype of the caller that was discarded by a tail call into a host function, and the - /// index of its call instruction. Lua keeps that caller for C functions; this engine releases - /// it, so the call site is recorded instead: a prototype does not keep the caller's values - /// alive, unlike the frame itself. - /// - internal Prototype? DiscardedCallerPrototype; - - internal int DiscardedCallerPc; internal bool IsTailCall => (Flags & CallStackFrameFlags.TailCall) == CallStackFrameFlags.TailCall; diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index ce2d0e3e..35db3ee6 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -1778,15 +1778,6 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart) newFrame.CallerInstructionIndex = lastFrame.CallerInstructionIndex; newFrame.Flags |= lastFrame.Flags & CallStackFrameFlags.ReversedLe; - if (func is not LuaClosure) - { - // The caller frame is discarded below, so remember where it called from. Errors and - // tracebacks report that position; only the prototype is kept, not the caller, so its - // values stay collectable while a native callee is suspended. - newFrame.DiscardedCallerPrototype = context.Prototype; - newFrame.DiscardedCallerPc = context.Pc; - } - state.ReplaceCallStackFrame(newFrame); // Release the replaced closure before a hook or native tail callee can suspend. context.LuaClosure = null!; diff --git a/src/Lua/Runtime/Tracebacks.cs b/src/Lua/Runtime/Tracebacks.cs index ab13b4ca..f581fa71 100644 --- a/src/Lua/Runtime/Tracebacks.cs +++ b/src/Lua/Runtime/Tracebacks.cs @@ -24,14 +24,10 @@ internal static void WriteLastLuaTrace( return; } - for (var index = stackFrames.Length - level; index >= 0; index--) + for (var index = stackFrames.Length - level; index >= 1; index--) { var frame = stackFrames[index]; - if ( - index >= 1 - && !frame.IsTailCall - && stackFrames[index - 1].Function is LuaClosure closure - ) + if (!frame.IsTailCall && stackFrames[index - 1].Function is LuaClosure closure) { var p = closure.Proto; if ((uint)frame.CallerInstructionIndex >= (uint)p.LineInfo.Length) @@ -39,23 +35,10 @@ internal static void WriteLastLuaTrace( WritePosition(ref list, p, frame.CallerInstructionIndex); return; } - - // A tail call into a host function discards the caller's frame. Its call site is kept - // on the frame so the error still reports the line that made the call, even when the - // unwound trace holds only that frame. - if ( - index == stackFrames.Length - 1 - && frame.DiscardedCallerPrototype is { } callerPrototype - && (uint)frame.DiscardedCallerPc < (uint)callerPrototype.LineInfo.Length - ) - { - WritePosition(ref list, callerPrototype, frame.DiscardedCallerPc); - return; - } } } - static void WritePosition(ref PooledList list, Prototype prototype, int pc) + internal static void WritePosition(ref PooledList list, Prototype prototype, int pc) { var intFormatBuffer = (stackalloc char[15]); var shortSourceBuffer = (stackalloc char[59]); From 3982d2a41d3f612f29914399bf10d463e6a7966d Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:49:31 +0900 Subject: [PATCH 15/21] perf(debugger): stop paying per call and per stop for attached-but-idle debugging Three costs the BenchmarkDotNet runs exposed, measured with ShortRun on .NET 10: DebuggerIdleBenchmark (attached vs detached) calls 120.39 us / 64,080 B -> 89.59 us / 112 B (1.55x -> 1.06x) coroutines 43.98 us / 8,576 B -> 45.44 us / 8,584 B (1.01x, unchanged) numeric-loop 89.74 us / 32 B -> 93.50 us / 32 B (1.00x, unchanged) DebuggerStopBenchmark (500 stops per invocation) 0 functions 120.3 us -> 116.3 us 2,000 functions 8,325 us -> 1,157 us (16.7 us -> 2.3 us per stop) The 64 KB per operation on the calls workload was an allocation regression from the checkpoint redesign: every frame entry and return asked the session for its execution copy, so each call took two cache lookups. The context now keeps the copy it just replaced and reuses it when execution alternates between a caller and its callee, which is the common pattern, and only falls back to the session for a third prototype or a changed debugger identity. That removes both the lookups and the allocation (calls now allocate 112 B per operation rather than 64,080 B) and also fixes the pre-existing 1.6x cost: the pre-branch build showed the same slowdown with no allocation, so the lookups, not the copies, were the expense. Stops no longer un-patch and re-patch every prototype. A step stays armed while the program is stopped (Continue clears it when stepping really ends), a breakpoint or step stop leaves the traps as they are, and control updates only run when the enabled state actually changes rather than on every stop, continue and stop-end. The remaining difference between 116 us and 1,157 us is the benchmark workload itself creating 2,000 closures per invocation, not debugger bookkeeping. Verified: 3527 tests, 84 debugger and 44 DAP tests pass, including stepping, breakpoint editing during a stop, detach/reset and the lifetime suites. --- src/Lua.Debugger/LuaDebugger.cs | 22 ++++++++++++++--- src/Lua/Runtime/LuaVirtualMachine.cs | 37 +++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 4a1aa3dd..9266b49f 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -128,12 +128,16 @@ internal bool TryContinue(int stopId, LuaDebugStep mode) { if (paused is not { } context || context.Stop.Id != stopId || resumeRequested) return false; + var stepping = pauseRequested || step != null; step = mode == LuaDebugStep.Continue ? null : new StepRequest(mode, context.State, context.Depth, context.FrameId); resumeRequested = true; - UpdateExecutionCodes(); + // Re-patching every prototype is only needed when stepping starts or stops; a plain + // continue between two stops of the same kind leaves the enabled state unchanged. + if (stepping != (pauseRequested || step != null)) + UpdateExecutionCodes(); signal.TrySetResult(true); inspections = context.Inspections; } @@ -172,8 +176,11 @@ CancellationToken cancellationToken pauseRequested ? "pause" : breakpoint != null ? "breakpoint" : "step"; + var pauseConsumed = pauseRequested; pauseRequested = false; - step = null; + // `step` stays armed while the program is stopped. Clearing it here would un-patch and + // then re-patch every prototype's traps on each step, which costs O(prototypes) per + // step for no observable difference; Continue clears it when stepping really ends. var stop = new LuaDebugStop(++nextStopId, reason, entry.Source, line, breakpoint?.Id); paused = context = new PauseContext( state, @@ -184,7 +191,10 @@ CancellationToken cancellationToken ); resumeRequested = false; signal = NewSignal(); - UpdateExecutionCodes(); + // Only a consumed pause request changes which checkpoints are needed; breakpoint and + // step stops keep the current traps armed. + if (pauseConsumed) + UpdateExecutionCodes(); } return options.PauseMode == LuaDebugPauseMode.Synchronous ? SuspendSynchronously(context, cancellationToken) @@ -267,7 +277,11 @@ async ValueTask SuspendAsync(PauseContext context, CancellationToken cancel commands .Dequeue() .Fail(new InvalidOperationException("The debug stop has ended.")); - UpdateExecutionCodes(); + // TryContinue and Release already applied the control change they caused. Only a + // stop that ends without either (cancellation, state teardown) still needs the + // prototypes revisited, and that is the rare path. + if (!resumeRequested) + UpdateExecutionCodes(); stopEnded = StopEnded; if (disposed) StopEnded = null; diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index 35db3ee6..e9381207 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -64,6 +64,7 @@ void Init(LuaState state, in CallStackFrame frame, CancellationToken cancellatio public int LastHookPc; public int LastDebugPc = -1; public LuaDebugCode? DebugCode; + public LuaDebugCode? PreviousDebugCode; public bool IsTopLevel => BaseCallStackCount == State.CallStackFrameCount; @@ -367,6 +368,7 @@ or PostOperationType.DontPop State = null!; LuaClosure = null!; DebugCode = null; + PreviousDebugCode = null; Task = default; CancellationToken = default; pool.TryPush(this); @@ -433,12 +435,35 @@ static bool MoveNext(VirtualMachineExecutionContext context) // This is a label to restart the execution when new function is called or restarted Restart: var debugger = Volatile.Read(ref context.GlobalState.Debugger); - if ( - context.DebugCode == null - || context.DebugCode.Prototype != context.Prototype - || context.DebugCode.Identity != debugger?.Identity - ) - context.DebugCode = debugger?.GetExecutionCode(context.LuaClosure); + { + // Alternating between two prototypes (a caller and its callee) is the common case, + // so keep the previous execution copy and reuse it instead of asking the session + // on every entry and return. + var current = context.DebugCode; + var prototype = context.Prototype; + if ( + current == null + || current.Prototype != prototype + || current.Identity != debugger?.Identity + ) + { + var previous = context.PreviousDebugCode; + if ( + previous != null + && previous.Prototype == prototype + && previous.Identity == debugger?.Identity + ) + { + context.DebugCode = previous; + context.PreviousDebugCode = current; + } + else + { + context.PreviousDebugCode = current; + context.DebugCode = debugger?.GetExecutionCode(context.LuaClosure); + } + } + } ref var instructionsHead = ref context.DebugCode != null ? ref context.DebugCode.Code[0] : ref Unsafe.AsRef(in context.Prototype.Code[0]); From 8afb59dcc639ad3d6d36bf269cb6e47c1197870f Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 09:55:25 +0900 Subject: [PATCH 16/21] fix: build for netstandard2.1 and report absent arguments like Lua The solution build for every target framework caught a netstandard2.1-only error in the new frame-version array: Array.Clear(array) does not exist there. It now uses the (start, length) overload. Typed argument getters reported "value expected" when the argument was absent, which is luaL_checkany's wording. Lua distinguishes an absent argument from an explicit nil and says "X expected, got no value", so select(), string.byte(), math.abs(), table.concat(), string.format() and any other typed check now match, while an explicit nil still reports "expected, got nil" and the untyped getter keeps "value expected". select also read its selector with the untyped getter, so pcall(select) reported "value expected" instead of the integer check's message; it now peeks and lets the number check produce the Lua message. The 5.2 differential corpus is down to the deliberate "[C#]" short source name and a name Lua itself picks non-deterministically for a function reached through pcall. All 3527 tests pass, and Lua.slnx builds with no warnings on netstandard2.1, net6.0, net8.0 and net10.0. --- src/Lua/LuaFunctionExecutionContext.cs | 19 +++++++++++++++++-- src/Lua/LuaState.cs | 2 +- src/Lua/Standard/BasicLibrary.cs | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Lua/LuaFunctionExecutionContext.cs b/src/Lua/LuaFunctionExecutionContext.cs index 4ad57260..7bce62ed 100644 --- a/src/Lua/LuaFunctionExecutionContext.cs +++ b/src/Lua/LuaFunctionExecutionContext.cs @@ -63,15 +63,30 @@ internal LuaValue GetArgumentOrDefault(int index, LuaValue defaultValue = defaul [MethodImpl(MethodImplOptions.AggressiveInlining)] public T GetArgument(int index) { - ThrowIfArgumentNotExists(index); + if (ArgumentCount <= index) + ThrowMissingArgument(index); return ReadArgument(index, in Arguments[index]); } + [MethodImpl(MethodImplOptions.NoInlining)] + void ThrowMissingArgument(int index) + { + // Lua reports an absent argument as "got no value", unlike an explicit nil, which keeps the + // "expected, got nil" wording. + var type = typeof(T); + var name = LuaValue.TryGetLuaValueType(type, out var valueType) + ? LuaValue.ToString(valueType) + : type.Name; + LuaRuntimeException.BadArgument(State, index + 1, $"{name} expected, got no value"); + } + // Lua library strings accept numbers; typed C# arguments remain strict. [MethodImpl(MethodImplOptions.AggressiveInlining)] internal string GetStringArgument(int index) { - var argument = GetArgument(index); + if (ArgumentCount <= index) + ThrowMissingArgument(index); + var argument = Arguments[index]; if (argument.TryReadString(out var text)) return text; if (argument.Type == LuaValueType.Number) diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index 24671d30..06deebd1 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -190,7 +190,7 @@ void Clear() CallStack.Clear(); if (FrameVersions != null) { - Array.Clear(FrameVersions); + Array.Clear(FrameVersions, 0, FrameVersions.Length); } } diff --git a/src/Lua/Standard/BasicLibrary.cs b/src/Lua/Standard/BasicLibrary.cs index 08296b44..5e25674c 100644 --- a/src/Lua/Standard/BasicLibrary.cs +++ b/src/Lua/Standard/BasicLibrary.cs @@ -617,7 +617,7 @@ public ValueTask Select( CancellationToken cancellationToken ) { - var selector = context.GetArgument(0); + var selector = context.ArgumentCount > 0 ? context.Arguments[0] : LuaValue.Nil; if (selector.Type == LuaValueType.String) { var text = selector.UnsafeReadString(); From 441740a612db992eedc0a280949b4626027a78ea Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:13:15 +0900 Subject: [PATCH 17/21] fix(debug): report the caller a native tail call discarded A tail call into a host function replaces the caller's frame to keep that caller's values collectable, so `debug.getinfo` counted one frame too few: `getinfo(1)` inside such a call reported the grandparent (or nil), and `getlocal`/`setlocal` read the grandparent's registers. Lua 5.2 keeps the frame for C callees, so this was a silent wrong answer rather than an error. The stack a script observes is now one logical frame deeper than the physical stack while the top frame is a tail-called host function, using the prototype and call site the state records at the tail call (LastCallerPrototype/LastPc, already maintained for error positions): - `getinfo` level 1 is reconstructed from that record, so `what`, `source`, `short_src`, `linedefined`, `lastlinedefined`, `currentline`, `nparams`, `isvararg`, `nups`, `istailcall` and `name`/`namewhat` report the discarded caller; higher levels shift by one and again reach the real frames. Only `f` is nil: the closure was released with the frame. - `getlocal`/`setlocal` keep correct level numbering and report no locals for that frame, because its registers were reused; previously they silently returned the grandparent's locals. - LuaDebug gained a prototype-based info path for the closure-less frame. Verified against native Lua 5.2: `getinfo(1,'Slu')` now matches field for field (what/short_src/linedefined/currentline/nparams/isvararg) through a discarded caller, as does `getinfo(2)`. Four regression tests pin the levels, the fields and the local limitation; docs/stdlib/debug.md documents it. 3531 tests pass and the solution builds on all target frameworks. --- docs/stdlib/debug.md | 4 +- src/Lua/Internal/LuaDebug.cs | 57 +++++++------ src/Lua/Standard/DebugLibrary.cs | 88 ++++++++++++++++++-- tests/Lua.Tests/DebugDiscardedCallerTests.cs | 80 ++++++++++++++++++ 4 files changed, 195 insertions(+), 34 deletions(-) create mode 100644 tests/Lua.Tests/DebugDiscardedCallerTests.cs diff --git a/docs/stdlib/debug.md b/docs/stdlib/debug.md index 9e4521ea..f32ad51a 100644 --- a/docs/stdlib/debug.md +++ b/docs/stdlib/debug.md @@ -8,8 +8,8 @@ Install with `state.OpenDebugLibrary()`. Thread-aware functions accept an option | --- | --- | --- | | [`debug.debug()`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.debug) | Officially enters an interactive debug command loop. | **Unavailable:** not registered. Interactive console/debugger integration must be provided by the host. | | [`debug.gethook([thread])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.gethook) | Returns the installed hook, mask, and count. After removing the hook: nil, empty mask, and zero count. | A host-installed hook is returned as its managed `LuaFunction`; C Lua reports a hook installed through its C API as the string `"external hook"`. The host hook's execution policy can differ from a Lua-installed hook; see below. | -| [`debug.getinfo([thread,] f [, what])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getinfo) | Returns function/frame metadata selected by `n`, `S`, `l`, `u`, `t`, `f`, and `L`, or nil for a missing stack level. Numeric-string levels are accepted and fractions truncate toward zero, as in Lua 5.2. | C# functions report `what="C#"` and `source="=[C#]"` instead of C-function labels. `u` exposes managed closure capture counts. | -| [`debug.getlocal([thread,] f, index)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getlocal) | Inspects a frame local/temporary/vararg, or a Lua function's parameter name; returns nil when no such local exists. | Managed frames expose their Lua-facing argument/temporary slots, not arbitrary C# local variables. The implementation operates on Lua-CSharp stack metadata. | +| [`debug.getinfo([thread,] f [, what])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getinfo) | Returns function/frame metadata selected by `n`, `S`, `l`, `u`, `t`, `f`, and `L`, or nil for a missing stack level. Numeric-string levels are accepted and fractions truncate toward zero, as in Lua 5.2. | C# functions report `what="C#"` and `source="=[C#]"` instead of C-function labels. `u` exposes managed closure capture counts. A level naming the caller that a native tail call discarded (`return hostFunction()`) is reconstructed from the recorded call site, so `S`, `l`, `u`, `t` and `n` report that caller; only `f` is nil, because its closure is released with the frame. | +| [`debug.getlocal([thread,] f, index)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getlocal) | Inspects a frame local/temporary/vararg, or a Lua function's parameter name; returns nil when no such local exists. | Managed frames expose their Lua-facing argument/temporary slots, not arbitrary C# local variables. The implementation operates on Lua-CSharp stack metadata. The caller discarded by a native tail call reports no locals (nil): its registers were reused, even though its level still exists for `getinfo`. | | [`debug.getmetatable(value)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getmetatable) | Returns the actual metatable, bypassing `__metatable` protection. | May return a host-frozen generated metatable. Reading the actual table does not make its contents writable. | | [`debug.getregistry()`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getregistry) | Returns the state's registry table. | Registry contents are managed runtime state; C API integer-slot conventions and native pointers are not a portable contract for Lua-CSharp. | | [`debug.getupvalue(f, index)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getupvalue) | Returns an upvalue name/value, or no values for an invalid slot. | Supports Lua closures and `CSharpClosure.UpValues`. Managed capture names and stripped Lua upvalue names are empty strings, as with C-function captures and stripped Lua names in C Lua 5.2.4. | diff --git a/src/Lua/Internal/LuaDebug.cs b/src/Lua/Internal/LuaDebug.cs index 976e6284..212f53c3 100644 --- a/src/Lua/Internal/LuaDebug.cs +++ b/src/Lua/Internal/LuaDebug.cs @@ -128,8 +128,10 @@ public static LuaDebug Create( LuaGlobalState globalState, CallStackFrame? prevFrame, CallStackFrame? frame, - LuaFunction function, + LuaFunction? function, + Prototype? prototype, int pc, + int callerPc, ReadOnlySpan what, out bool isValid ) @@ -139,7 +141,7 @@ out bool isValid buffer = new(globalState); } - isValid = buffer.GetInfo(prevFrame, frame, function, pc, what); + isValid = buffer.GetInfo(prevFrame, frame, function, prototype, pc, callerPc, what); return new(buffer, buffer.version); } @@ -214,12 +216,15 @@ internal void Return(uint version) internal bool GetInfo( CallStackFrame? prevFrame, CallStackFrame? frame, - LuaFunction function, + LuaFunction? function, + Prototype? prototype, int pc, + int callerPc, ReadOnlySpan what ) { var closure = function as LuaClosure; + var proto = closure?.Proto ?? prototype; var status = 1; foreach (var c in what) { @@ -227,36 +232,35 @@ ReadOnlySpan what { case 'S': { - GetFuncInfo(function); + if (function is not null) + GetFuncInfo(function); + else + GetFuncInfo(proto!); break; } case 'l': { CurrentLine = - frame.HasValue && closure is not null - ? ( - (uint)pc < (uint)closure.Proto.LineInfo.Length - ? closure.Proto.LineInfo[pc] - : 0 - ) + proto is not null && (frame.HasValue || function is null) + ? ((uint)pc < (uint)proto.LineInfo.Length ? proto.LineInfo[pc] : 0) : -1; break; } case 'u': { UpValueCount = - closure is not null ? closure.UpValues.Length + proto is not null ? proto.UpValues.Length : function is CSharpClosure csClosure ? csClosure.UpValues.Length : 0; - if (closure is null) + if (proto is null) { IsVarArg = true; ParameterCount = 0; } else { - IsVarArg = closure.Proto.HasVariableArguments; - ParameterCount = closure.Proto.ParameterCount; + IsVarArg = proto.HasVariableArguments; + ParameterCount = proto.ParameterCount; } break; @@ -276,7 +280,7 @@ closure is not null ? closure.UpValues.Length { NameWhat = GetFuncName( prevFrameClosure.Proto, - frame?.CallerInstructionIndex ?? 0, + frame?.CallerInstructionIndex ?? callerPc, out Name ); } @@ -292,7 +296,7 @@ out Name } else if (NameWhat != null && Name is "?") { - Name = function.Name; + Name = function?.Name; } break; @@ -317,16 +321,21 @@ void GetFuncInfo(LuaFunction f) LineDefined = -1; LastLineDefined = -1; What = "C#"; + ShortSourceLength = WriteShortSource(Source, ShortSource); + return; } - else - { - var p = cl.Proto; - Source = p.ChunkName; - LineDefined = p.LineDefined; - LastLineDefined = p.LastLineDefined; - What = LineDefined == 0 ? "main" : "Lua"; - } + GetFuncInfo(cl.Proto); + } + + // Used for a caller frame that a tail call into a host function discarded: its prototype is + // still known, but its closure is gone. + void GetFuncInfo(Prototype p) + { + Source = p.ChunkName; + LineDefined = p.LineDefined; + LastLineDefined = p.LastLineDefined; + What = LineDefined == 0 ? "main" : "Lua"; ShortSourceLength = WriteShortSource(Source, ShortSource); } } diff --git a/src/Lua/Standard/DebugLibrary.cs b/src/Lua/Standard/DebugLibrary.cs index a860a3bd..20c51f0d 100644 --- a/src/Lua/Standard/DebugLibrary.cs +++ b/src/Lua/Standard/DebugLibrary.cs @@ -54,6 +54,30 @@ static LuaState GetLuaThread(in LuaFunctionExecutionContext context, out int arg return context.State; } + /// + /// A tail call into a host function replaces the caller's frame, yet that caller is still part of + /// the stack Lua code can observe. The state keeps its prototype and call site, which is enough + /// to answer level queries for it while the host function runs. + /// + readonly record struct DiscardedCaller(Prototype Prototype, int LinePc, int CallerPc); + + static bool TryGetDiscardedCaller(LuaState state, out DiscardedCaller caller) + { + caller = default; + var frames = state.GetCallStackFrames(); + if (frames.Length == 0) + return false; + var top = frames[^1]; + if (!top.IsTailCall || top.Function is LuaClosure) + return false; + if (state.LastCallerPrototype is not { } prototype) + return false; + if ((uint)state.LastPc >= (uint)prototype.LineInfo.Length) + return false; + caller = new(prototype, state.LastPc, top.CallerInstructionIndex); + return true; + } + static ref LuaValue FindLocal(LuaState state, int level, int index, out string? name) { if (index == 0) @@ -169,6 +193,16 @@ static LuaValue GetParam(LuaFunction function, int index) var level = context.GetIntegerArgument(argOffset); + var discarded = TryGetDiscardedCaller(state, out _); + if (discarded && level == 1) + { + // The discarded caller's registers are gone, so it has no readable locals. + return new(context.Return(LuaValue.Nil)); + } + + if (discarded && level > 1) + level--; + if (level < 0 || level >= state.GetCallStackFrames().Length) { context.ThrowBadArgument(1, "level out of range"); @@ -194,6 +228,16 @@ CancellationToken cancellationToken var index = context.GetIntegerArgument(argOffset + 1); var level = context.GetIntegerArgument(argOffset); + var discarded = TryGetDiscardedCaller(state, out _); + if (discarded && level == 1) + { + // The discarded caller's registers are gone, so it has no writable locals. + return new(context.Return(LuaValue.Nil)); + } + + if (discarded && level > 1) + level--; + if (level < 0 || level >= state.GetCallStackFrames().Length) { context.ThrowBadArgument(1, "level out of range"); @@ -561,27 +605,50 @@ CancellationToken cancellationToken var pc = 0; var arg1 = context.GetArgument(argOffset); - if (!arg1.TryReadFunction(out var functionToInspect)) + LuaFunction? functionToInspect = null; + Prototype? syntheticPrototype = null; + var callerPc = -1; + + if (!arg1.TryReadFunction(out var inspected)) { if (!arg1.TryReadDouble(out _)) context.ThrowBadArgument(argOffset, "function or level expected"); var level = context.GetIntegerArgument(argOffset) + 1; var callStack = state.GetCallStackFrames(); + var discarded = TryGetDiscardedCaller(state, out var caller); + var logicalLength = callStack.Length + (discarded ? 1 : 0); - if (level <= 0 || level > callStack.Length) + if (level <= 0 || level > logicalLength) { return new(context.Return(LuaValue.Nil)); } - currentFrame = callStack[^level]; - previousFrame = level + 1 <= callStack.Length ? callStack[^(level + 1)] : null; - if (level != 1) + if (discarded && level == 2) { - pc = callStack[^(level - 1)].CallerInstructionIndex; + // Level 1 (here 2) is the caller the tail call discarded, reported from its + // recorded call site instead of skipping to the frame below it. + previousFrame = callStack.Length >= 2 ? callStack[^2] : null; + pc = caller.LinePc; + callerPc = caller.CallerPc; + syntheticPrototype = caller.Prototype; } + else + { + var index = discarded && level > 2 ? level - 1 : level; + currentFrame = callStack[^index]; + previousFrame = index + 1 <= callStack.Length ? callStack[^(index + 1)] : null; + if (index != 1) + { + pc = callStack[^(index - 1)].CallerInstructionIndex; + } - functionToInspect = currentFrame.Value.Function; + functionToInspect = currentFrame.Value.Function; + } + } + else + { + functionToInspect = inspected; } using var debug = LuaDebug.Create( @@ -589,7 +656,9 @@ CancellationToken cancellationToken previousFrame, currentFrame, functionToInspect, + syntheticPrototype, pc, + callerPc, what, out var isValid ); @@ -634,7 +703,10 @@ out var isValid if (what.Contains('f')) { - table["func"] = functionToInspect; + // A discarded caller's closure is released, so only its prototype-based fields remain. + table["func"] = functionToInspect is { } inspectedFunction + ? inspectedFunction + : LuaValue.Nil; } if (what.Contains('L')) diff --git a/tests/Lua.Tests/DebugDiscardedCallerTests.cs b/tests/Lua.Tests/DebugDiscardedCallerTests.cs new file mode 100644 index 00000000..7854d3b8 --- /dev/null +++ b/tests/Lua.Tests/DebugDiscardedCallerTests.cs @@ -0,0 +1,80 @@ +using Lua.Standard; + +namespace Lua.Tests; + +/// +/// A tail call into a host function replaces the caller's frame, so the caller exists only as a +/// recorded call site. Lua 5.2 keeps the frame and reports it from debug.getinfo; these tests +/// pin the same answers for the levels and fields that can be reconstructed, and the documented +/// limit: the discarded frame's registers are gone, so its locals cannot be read. +/// +public class DebugDiscardedCallerTests +{ + static LuaState CreateState() + { + var state = LuaState.Create(); + state.OpenStandardLibraries(); + return state; + } + + [Test] + public async Task GetInfoReportsTheCallerDiscardedByANativeTailCall() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local function f(a)\n" + + " return debug.getinfo(1, 'Slu')\n" + + "end\n" + + "local info = f(1)\n" + + "return info.what, info.linedefined, info.currentline, info.nparams, info.isvararg", + "=discarded" + ); + Assert.That(results[0].Read(), Is.EqualTo("Lua")); + Assert.That(results[1].Read(), Is.EqualTo(1)); + Assert.That(results[2].Read(), Is.EqualTo(2)); + Assert.That(results[3].Read(), Is.EqualTo(1)); + Assert.That(results[4].Read(), Is.False); + } + + [Test] + public async Task GetInfoLevelTwoSkipsTheDiscardedCaller() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local function f()\n" + + " return debug.getinfo(2, 'S')\n" + + "end\n" + + "local info = f()\n" + + "return info.what, info.linedefined", + "=discarded" + ); + Assert.That(results[0].Read(), Is.EqualTo("main")); + Assert.That(results[1].Read(), Is.EqualTo(0)); + } + + [Test] + public async Task GetLocalReadsTheFrameBelowTheDiscardedCaller() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local function f()\n" + + " return debug.getlocal(2, 1)\n" + + "end\n" + + "local name = f()\n" + + "return name", + "=discarded" + ); + Assert.That(results[0].Read(), Is.EqualTo("f")); + } + + [Test] + public async Task GetLocalCannotReadTheDiscardedCallersRegisters() + { + using var state = CreateState(); + var results = await state.DoStringAsync( + "local function f(a)\n" + " return debug.getlocal(1, 1)\n" + "end\n" + "return f(7)", + "=discarded" + ); + Assert.That(results[0].Type, Is.EqualTo(LuaValueType.Nil)); + } +} From 7cfd836d51515ae2bc8bec470e87eadd66cf4419 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:46:44 +0900 Subject: [PATCH 18/21] Fix debugger scope lookup and native tail-call reporting --- docs/stdlib/debug.md | 2 +- .../LuaDapSession.Requests.cs | 23 +--- src/Lua.Debugger/LuaDebugger.Code.cs | 25 +--- src/Lua.Debugger/LuaDebugger.Inspection.cs | 2 + src/Lua.Debugger/LuaDebugger.Sources.cs | 11 +- src/Lua.Debugger/LuaDebugger.cs | 8 +- src/Lua/Exceptions.cs | 115 +++++------------ src/Lua/Internal/LuaDebug.cs | 87 +++++-------- src/Lua/InternalVisibleTo.cs | 1 + src/Lua/LuaState.cs | 14 +++ src/Lua/Runtime/LuaVirtualMachine.cs | 10 +- src/Lua/Runtime/Tracebacks.cs | 5 - src/Lua/Standard/DebugLibrary.cs | 118 +++++++----------- tests/Lua.Debugger.Dap.Tests/DapTests.cs | 16 +-- tests/Lua.Debugger.Tests/DebuggerTrapTests.cs | 19 +++ tests/Lua.Tests/DebugDiscardedCallerTests.cs | 43 +++++-- tests/Lua.Tests/TailCallErrorPositionTests.cs | 85 ++++++++++--- 17 files changed, 286 insertions(+), 298 deletions(-) diff --git a/docs/stdlib/debug.md b/docs/stdlib/debug.md index f32ad51a..bb7e9837 100644 --- a/docs/stdlib/debug.md +++ b/docs/stdlib/debug.md @@ -8,7 +8,7 @@ Install with `state.OpenDebugLibrary()`. Thread-aware functions accept an option | --- | --- | --- | | [`debug.debug()`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.debug) | Officially enters an interactive debug command loop. | **Unavailable:** not registered. Interactive console/debugger integration must be provided by the host. | | [`debug.gethook([thread])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.gethook) | Returns the installed hook, mask, and count. After removing the hook: nil, empty mask, and zero count. | A host-installed hook is returned as its managed `LuaFunction`; C Lua reports a hook installed through its C API as the string `"external hook"`. The host hook's execution policy can differ from a Lua-installed hook; see below. | -| [`debug.getinfo([thread,] f [, what])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getinfo) | Returns function/frame metadata selected by `n`, `S`, `l`, `u`, `t`, `f`, and `L`, or nil for a missing stack level. Numeric-string levels are accepted and fractions truncate toward zero, as in Lua 5.2. | C# functions report `what="C#"` and `source="=[C#]"` instead of C-function labels. `u` exposes managed closure capture counts. A level naming the caller that a native tail call discarded (`return hostFunction()`) is reconstructed from the recorded call site, so `S`, `l`, `u`, `t` and `n` report that caller; only `f` is nil, because its closure is released with the frame. | +| [`debug.getinfo([thread,] f [, what])`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getinfo) | Returns function/frame metadata selected by `n`, `S`, `l`, `u`, `t`, `f`, and `L`, or nil for a missing stack level. Numeric-string levels are accepted and fractions truncate toward zero, as in Lua 5.2. | C# functions report `what="C#"` and `source="=[C#]"` instead of C-function labels. `u` exposes managed closure capture counts. For a Lua caller discarded by `return hostFunction()`, the recorded call site and prototype provide `n`, `S`, `l`, `u`, `t`, and `L`. The `func` field is nil because the caller's closure was released. | | [`debug.getlocal([thread,] f, index)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getlocal) | Inspects a frame local/temporary/vararg, or a Lua function's parameter name; returns nil when no such local exists. | Managed frames expose their Lua-facing argument/temporary slots, not arbitrary C# local variables. The implementation operates on Lua-CSharp stack metadata. The caller discarded by a native tail call reports no locals (nil): its registers were reused, even though its level still exists for `getinfo`. | | [`debug.getmetatable(value)`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getmetatable) | Returns the actual metatable, bypassing `__metatable` protection. | May return a host-frozen generated metatable. Reading the actual table does not make its contents writable. | | [`debug.getregistry()`](https://www.lua.org/manual/5.2/manual.html#pdf-debug.getregistry) | Returns the state's registry table. | Registry contents are managed runtime state; C API integer-slot conventions and native pointers are not a portable contract for Lua-CSharp. | diff --git a/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs b/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs index 87edbe82..3f450cd1 100644 --- a/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs +++ b/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs @@ -220,10 +220,8 @@ await RespondAsync( return; case "scopes": var frameId = Integer(args, "frameId"); - var frame = await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), frameId - 1, 1, token) - .ConfigureAwait(false); - if (frame.Frames.Length == 0 || frame.Frames[0].Id != frameId) + var frameCount = debugger.GetFrameCount(CurrentStop(expectedStop)); + if (frameId < 1 || frameId > frameCount) throw new ArgumentException("Unknown frame."); var scopes = new JsonArray(); foreach ( @@ -297,23 +295,12 @@ await RespondAsync( .ConfigureAwait(false); return; case "evaluate": + var evaluationStop = CurrentStop(expectedStop); var evaluationFrame = Integer(args, "frameId"); if (evaluationFrame == 0) - { - var top = await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), 0, 1, token) - .ConfigureAwait(false); - if (top.Frames.Length == 0) - throw new InvalidOperationException("Lua execution is not stopped."); - evaluationFrame = top.Frames[0].Id; - } + evaluationFrame = debugger.GetFrameCount(evaluationStop); var evaluated = await debugger - .EvaluateAsync( - CurrentStop(expectedStop), - evaluationFrame, - Text(args, "expression"), - token - ) + .EvaluateAsync(evaluationStop, evaluationFrame, Text(args, "expression"), token) .ConfigureAwait(false); await RespondAsync( request, diff --git a/src/Lua.Debugger/LuaDebugger.Code.cs b/src/Lua.Debugger/LuaDebugger.Code.cs index 60202b10..af35f0c4 100644 --- a/src/Lua.Debugger/LuaDebugger.Code.cs +++ b/src/Lua.Debugger/LuaDebugger.Code.cs @@ -52,14 +52,18 @@ void Track(ExecutionCode code) codeReferences.Add(new WeakReference(code)); } - /// Applies the current controls to every execution copy. Cheap: no prototype is scanned. - void UpdateExecutionCodes() + // Only breakpoint and source changes need to scan prototype lines. + void UpdateExecutionCodes(bool breakpointsChanged = false) { var attached = IsAttached; for (var i = codeReferences.Count - 1; i >= 0; i--) { if (codeReferences[i].TryGetTarget(out var code)) + { + if (breakpointsChanged) + code.HasBreakpointLine = HasBreakpointLine(code.Prototype, code.Source); UpdateControls(code, attached); + } else codeReferences.RemoveAt(i); } @@ -70,23 +74,6 @@ void UpdateControls(ExecutionCode code, bool attached) code.SetEnabled(attached && (pauseRequested || step != null || code.HasBreakpointLine)); } - /// - /// Recomputes which execution copies can hit a breakpoint. Only breakpoint or source changes - /// need this; stops and steps reuse the cached answer instead of rescanning every prototype. - /// - void RefreshBreakpointLines() - { - for (var i = codeReferences.Count - 1; i >= 0; i--) - { - if (!codeReferences[i].TryGetTarget(out var code)) - { - codeReferences.RemoveAt(i); - continue; - } - code.HasBreakpointLine = HasBreakpointLine(code.Prototype, code.Source); - } - } - static bool HasBreakpointLine(Prototype prototype, SourceEntry source) { if (source.Points.Count == 0) diff --git a/src/Lua.Debugger/LuaDebugger.Inspection.cs b/src/Lua.Debugger/LuaDebugger.Inspection.cs index 73c3fddd..7cfd88e2 100644 --- a/src/Lua.Debugger/LuaDebugger.Inspection.cs +++ b/src/Lua.Debugger/LuaDebugger.Inspection.cs @@ -336,6 +336,7 @@ CancellationToken cancellationToken ); var previousPc = context.State.LastPc; var previousPrototype = context.State.LastCallerPrototype; + var previousNativeTailCaller = context.State.LastNativeTailCaller; evaluating = true; context.State.NonYieldableCallCount++; try @@ -348,6 +349,7 @@ CancellationToken cancellationToken context.State.NonYieldableCallCount--; context.State.LastPc = previousPc; context.State.LastCallerPrototype = previousPrototype; + context.State.LastNativeTailCaller = previousNativeTailCaller; evaluating = false; } } diff --git a/src/Lua.Debugger/LuaDebugger.Sources.cs b/src/Lua.Debugger/LuaDebugger.Sources.cs index be87a7bf..23c80628 100644 --- a/src/Lua.Debugger/LuaDebugger.Sources.cs +++ b/src/Lua.Debugger/LuaDebugger.Sources.cs @@ -50,8 +50,7 @@ public LuaBreakpoint[] SetBreakpoints(string source, IReadOnlyList lines) if (request.Resolved.Verified) entry.Points[request.Resolved.Line] = request.Resolved; } - RefreshBreakpointLines(); - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); return requests.Select(x => x.Resolved).ToArray(); } } @@ -61,8 +60,7 @@ public void ClearBreakpoints() lock (gate) { ClearBreakpointsCore(); - RefreshBreakpointLines(); - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); } } @@ -95,10 +93,7 @@ public LuaDebugSource RegisterSource(LuaClosure closure, string? content = null) if (content != null) entry.Source = entry.Source with { Content = content }; if (controlsChanged) - { - RefreshBreakpointLines(); - UpdateExecutionCodes(); - } + UpdateExecutionCodes(breakpointsChanged: true); return entry.Source; } } diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 9266b49f..ba9c9a6c 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -95,7 +95,7 @@ void Release(bool detach) step = null; pauseRequested = false; resumeRequested = true; - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); signal.TrySetResult(true); inspections = paused?.Inspections; } @@ -324,6 +324,12 @@ PauseContext RequireStop(int id) => ? context : throw new InvalidOperationException("The debug stop is no longer active."); + internal int GetFrameCount(int stopId) + { + lock (gate) + return RequireStop(stopId).Depth; + } + Task InspectAsync( int stopId, Func> action, diff --git a/src/Lua/Exceptions.cs b/src/Lua/Exceptions.cs index 0913f22c..9459f0c4 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -199,7 +199,7 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) state.ExceptionTrace.Clear(); state.CurrentException = this; State = state; - if (level <= 1) + if (level == 1) CaptureTailCallSite(state); } @@ -207,7 +207,7 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) this.level = level; } - int level = 1; + readonly int level = 1; // Where the discarded caller of a native tail call made the call. Captured when the exception // is created, while the tail call still sits on the stack; the frame itself is released so the @@ -217,26 +217,14 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) void CaptureTailCallSite(LuaState state) { - var frames = state.GetCallStackFrames(); - if (frames.Length == 0) - return; - var top = frames[^1]; - if (!top.IsTailCall || top.Function is LuaClosure) - return; - if (state.LastCallerPrototype is not { } prototype) - return; - if ((uint)state.LastPc >= (uint)prototype.LineInfo.Length) + var caller = state.GetNativeTailCaller(); + if ( + caller.Prototype is not { } prototype + || (uint)caller.Pc >= (uint)prototype.LineInfo.Length + ) return; tailCallSitePrototype = prototype; - tailCallSitePc = state.LastPc; - } - - bool TryWriteTailCallSite(ref PooledList list) - { - if (tailCallSitePrototype is not { } prototype) - return false; - Traceback.WritePosition(ref list, prototype, tailCallSitePc); - return true; + tailCallSitePc = caller.Pc; } Traceback? luaTraceback; @@ -398,8 +386,10 @@ internal static (string NameWhat, string Name) GetCurrentFunctionName(LuaState s Prototype? callerPrototype; if (current.IsTailCall) { - pc = state.LastPc; - callerPrototype = state.LastCallerPrototype; + (callerPrototype, pc) = + current.Function is LuaClosure + ? (state.LastCallerPrototype, state.LastPc) + : (state.LastNativeTailCaller.Prototype, state.LastNativeTailCaller.Pc); } else { @@ -547,18 +537,18 @@ double value } } - string CreateMessage(Traceback traceback, LuaValue errorObject, int level) + string FormatMessage(ReadOnlySpan frames, string message, string prefix = "") { var pooledList = new PooledList(64); pooledList.Clear(); try { - pooledList.AddRange("Lua-CSharp: "); - var before = pooledList.Count; - traceback.WriteLastLuaTrace(ref pooledList, level); - if (pooledList.Count == before) - TryWriteTailCallSite(ref pooledList); - pooledList.AddRange($"{errorObject}"); + pooledList.AddRange(prefix); + if (tailCallSitePrototype is { } prototype) + Traceback.WritePosition(ref pooledList, prototype, tailCallSitePc); + else + Traceback.WriteLastLuaTrace(frames, ref pooledList, level); + pooledList.AddRange(message); return pooledList.AsSpan().ToString(); } finally @@ -614,57 +604,20 @@ internal string MinimalMessage(LuaPlatform platform) return message; } - if (luaTraceback == null) + ReadOnlySpan frames = default; + if (luaTraceback is { } traceback) { - if (State != null) - { - ReadOnlySpan callStack = State.ExceptionTrace.AsSpan(); - if (callStack.IsEmpty) - callStack = State.GetCallStackFrames(); - level = Math.Min(level, callStack.Length + 1); - callStack = callStack[..^(level - 1)]; - if (callStack.IsEmpty) - { - return ErrorObject.ToString(); - } - - { - var pooledList = new PooledList(64); - pooledList.Clear(); - try - { - Traceback.WriteLastLuaTrace(callStack, ref pooledList); - if (pooledList.Count == 0) - TryWriteTailCallSite(ref pooledList); - pooledList.AddRange(message); - return pooledList.AsSpan().ToString(); - } - finally - { - pooledList.Dispose(); - } - } - } - - return message; + frames = traceback.StackFrames; } - + else if (State is { } state) { - var pooledList = new PooledList(64); - pooledList.Clear(); - try - { - luaTraceback.WriteLastLuaTrace(ref pooledList, level); - if (pooledList.Count == 0) - TryWriteTailCallSite(ref pooledList); - pooledList.AddRange(message); - return pooledList.AsSpan().ToString(); - } - finally - { - pooledList.Dispose(); - } + frames = state.ExceptionTrace.AsSpan(); + if (frames.IsEmpty) + frames = state.GetCallStackFrames(); } + return frames.IsEmpty && tailCallSitePrototype == null + ? message + : FormatMessage(frames, message); } public override string Message @@ -676,12 +629,10 @@ public override string Message return InnerException.Message; } - if (LuaTraceback == null) - { - return ErrorObject.ToString(); - } - - return CreateMessage(LuaTraceback, ErrorObject, level); + var traceback = LuaTraceback; + return traceback == null + ? ErrorObject.ToString() + : FormatMessage(traceback.StackFrames, ErrorObject.ToString(), "Lua-CSharp: "); } } diff --git a/src/Lua/Internal/LuaDebug.cs b/src/Lua/Internal/LuaDebug.cs index 212f53c3..d8bfae51 100644 --- a/src/Lua/Internal/LuaDebug.cs +++ b/src/Lua/Internal/LuaDebug.cs @@ -126,12 +126,11 @@ public ReadOnlySpan ShortSource public static LuaDebug Create( LuaGlobalState globalState, - CallStackFrame? prevFrame, - CallStackFrame? frame, LuaFunction? function, Prototype? prototype, - int pc, - int callerPc, + int? pc, + bool isTailCall, + (Prototype? Prototype, int Pc) caller, ReadOnlySpan what, out bool isValid ) @@ -141,7 +140,7 @@ out bool isValid buffer = new(globalState); } - isValid = buffer.GetInfo(prevFrame, frame, function, prototype, pc, callerPc, what); + isValid = buffer.GetInfo(function, prototype, pc, isTailCall, caller, what); return new(buffer, buffer.version); } @@ -214,17 +213,14 @@ internal void Return(uint version) } internal bool GetInfo( - CallStackFrame? prevFrame, - CallStackFrame? frame, LuaFunction? function, Prototype? prototype, - int pc, - int callerPc, + int? pc, + bool isTailCall, + (Prototype? Prototype, int Pc) caller, ReadOnlySpan what ) { - var closure = function as LuaClosure; - var proto = closure?.Proto ?? prototype; var status = 1; foreach (var c in what) { @@ -232,69 +228,57 @@ ReadOnlySpan what { case 'S': { - if (function is not null) - GetFuncInfo(function); - else - GetFuncInfo(proto!); + GetFuncInfo(prototype); break; } case 'l': { CurrentLine = - proto is not null && (frame.HasValue || function is null) - ? ((uint)pc < (uint)proto.LineInfo.Length ? proto.LineInfo[pc] : 0) + prototype is not null && pc is { } instructionIndex + ? ( + (uint)instructionIndex < (uint)prototype.LineInfo.Length + ? prototype.LineInfo[instructionIndex] + : 0 + ) : -1; break; } case 'u': { UpValueCount = - proto is not null ? proto.UpValues.Length + prototype is not null ? prototype.UpValues.Length : function is CSharpClosure csClosure ? csClosure.UpValues.Length : 0; - if (proto is null) + if (prototype is null) { IsVarArg = true; ParameterCount = 0; } else { - IsVarArg = proto.HasVariableArguments; - ParameterCount = proto.ParameterCount; + IsVarArg = prototype.HasVariableArguments; + ParameterCount = prototype.ParameterCount; } break; } case 't': { - IsTailCall = - frame.HasValue - && (frame.Value.Flags | CallStackFrameFlags.TailCall) - == frame.Value.Flags; + IsTailCall = isTailCall; break; } case 'n': { - /* calling function is a known Lua function? */ - if (prevFrame is { Function: LuaClosure prevFrameClosure }) - { - NameWhat = GetFuncName( - prevFrameClosure.Proto, - frame?.CallerInstructionIndex ?? callerPc, - out Name - ); - } - else - { - NameWhat = null; - } + NameWhat = caller.Prototype is { } callerPrototype + ? GetFuncName(callerPrototype, caller.Pc, out Name) + : null; if (NameWhat is null) { NameWhat = ""; /* not found */ Name = null; } - else if (NameWhat != null && Name is "?") + else if (Name is "?") { Name = function?.Name; } @@ -313,29 +297,22 @@ out Name return status == 1; } - void GetFuncInfo(LuaFunction f) + void GetFuncInfo(Prototype? prototype) { - if (f is not LuaClosure cl) + if (prototype is null) { Source = "=[C#]"; LineDefined = -1; LastLineDefined = -1; What = "C#"; - ShortSourceLength = WriteShortSource(Source, ShortSource); - return; } - - GetFuncInfo(cl.Proto); - } - - // Used for a caller frame that a tail call into a host function discarded: its prototype is - // still known, but its closure is gone. - void GetFuncInfo(Prototype p) - { - Source = p.ChunkName; - LineDefined = p.LineDefined; - LastLineDefined = p.LastLineDefined; - What = LineDefined == 0 ? "main" : "Lua"; + else + { + Source = prototype.ChunkName; + LineDefined = prototype.LineDefined; + LastLineDefined = prototype.LastLineDefined; + What = LineDefined == 0 ? "main" : "Lua"; + } ShortSourceLength = WriteShortSource(Source, ShortSource); } } diff --git a/src/Lua/InternalVisibleTo.cs b/src/Lua/InternalVisibleTo.cs index 1fe6b9e2..18bf9cd0 100644 --- a/src/Lua/InternalVisibleTo.cs +++ b/src/Lua/InternalVisibleTo.cs @@ -2,3 +2,4 @@ [assembly: InternalsVisibleTo("Lua.Tests")] [assembly: InternalsVisibleTo("Lua.Debugger")] +[assembly: InternalsVisibleTo("Lua.Debugger.Tests")] diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index 06deebd1..886f54f6 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -233,8 +233,20 @@ public void Release() internal int ErrorHandlerIndex; internal int NonYieldableCallCount; internal readonly ReversedStack ExceptionTrace = new(); + internal Prototype? LastCallerPrototype; + // Only native tail calls need to reconstruct a discarded caller. Line hooks can change LastPc. + internal (Prototype? Prototype, int Pc, bool IsTailCall) LastNativeTailCaller; + + internal (Prototype? Prototype, int Pc, bool IsTailCall) GetNativeTailCaller() + { + var frames = GetCallStackFrames(); + return !frames.IsEmpty && frames[^1].IsTailCall && frames[^1].Function is not LuaClosure + ? LastNativeTailCaller + : default; + } + internal ref FastListCore OpenUpValues => ref openUpValues; public bool IsRunning => CallStackFrameCount != 0; public bool IsCoroutine => coroutine != null; @@ -677,6 +689,7 @@ CancellationToken cancellationToken var state = this; var callStackTop = state.CallStackFrameCount; var previousCallerPrototype = state.LastCallerPrototype; + var previousNativeTailCaller = state.LastNativeTailCaller; var previousPc = state.LastPc; var entered = EnterExecution(); try @@ -714,6 +727,7 @@ CancellationToken cancellationToken { PopCallStackFrameUntil(callStackTop); LastCallerPrototype = previousCallerPrototype; + LastNativeTailCaller = previousNativeTailCaller; LastPc = previousPc; ExitExecution(entered); } diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index e9381207..6032a5bd 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -1807,6 +1807,14 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart) // Release the replaced closure before a hook or native tail callee can suspend. context.LuaClosure = null!; + var isLuaClosure = func is LuaClosure; + if (!isLuaClosure) + state.LastNativeTailCaller = ( + state.LastCallerPrototype, + context.Pc, + lastFrame.IsTailCall + ); + if (state.CallOrReturnHookMask.Value != 0 && !context.State.IsInHook) { context.PostOperation = PostOperationType.TailCall; @@ -1815,7 +1823,7 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart) return false; } - if (func is LuaClosure) + if (isLuaClosure) { context.Push(newFrame); doRestart = true; diff --git a/src/Lua/Runtime/Tracebacks.cs b/src/Lua/Runtime/Tracebacks.cs index f581fa71..25db7bc8 100644 --- a/src/Lua/Runtime/Tracebacks.cs +++ b/src/Lua/Runtime/Tracebacks.cs @@ -56,11 +56,6 @@ internal static void WritePosition(ref PooledList list, Prototype prototyp list.AddRange(": "); } - internal void WriteLastLuaTrace(ref PooledList list, int level = 1) - { - WriteLastLuaTrace(StackFrames, ref list, level); - } - public int LastLine { get diff --git a/src/Lua/Standard/DebugLibrary.cs b/src/Lua/Standard/DebugLibrary.cs index 20c51f0d..c17bfab1 100644 --- a/src/Lua/Standard/DebugLibrary.cs +++ b/src/Lua/Standard/DebugLibrary.cs @@ -54,30 +54,6 @@ static LuaState GetLuaThread(in LuaFunctionExecutionContext context, out int arg return context.State; } - /// - /// A tail call into a host function replaces the caller's frame, yet that caller is still part of - /// the stack Lua code can observe. The state keeps its prototype and call site, which is enough - /// to answer level queries for it while the host function runs. - /// - readonly record struct DiscardedCaller(Prototype Prototype, int LinePc, int CallerPc); - - static bool TryGetDiscardedCaller(LuaState state, out DiscardedCaller caller) - { - caller = default; - var frames = state.GetCallStackFrames(); - if (frames.Length == 0) - return false; - var top = frames[^1]; - if (!top.IsTailCall || top.Function is LuaClosure) - return false; - if (state.LastCallerPrototype is not { } prototype) - return false; - if ((uint)state.LastPc >= (uint)prototype.LineInfo.Length) - return false; - caller = new(prototype, state.LastPc, top.CallerInstructionIndex); - return true; - } - static ref LuaValue FindLocal(LuaState state, int level, int index, out string? name) { if (index == 0) @@ -193,7 +169,7 @@ static LuaValue GetParam(LuaFunction function, int index) var level = context.GetIntegerArgument(argOffset); - var discarded = TryGetDiscardedCaller(state, out _); + var discarded = state.GetNativeTailCaller().Prototype != null; if (discarded && level == 1) { // The discarded caller's registers are gone, so it has no readable locals. @@ -228,7 +204,7 @@ CancellationToken cancellationToken var index = context.GetIntegerArgument(argOffset + 1); var level = context.GetIntegerArgument(argOffset); - var discarded = TryGetDiscardedCaller(state, out _); + var discarded = state.GetNativeTailCaller().Prototype != null; if (discarded && level == 1) { // The discarded caller's registers are gone, so it has no writable locals. @@ -600,65 +576,69 @@ CancellationToken cancellationToken { var state = GetLuaThread(context, out var argOffset); var what = context.GetArgumentOrDefault(argOffset + 1, "flnStu"); - CallStackFrame? previousFrame = null; - CallStackFrame? currentFrame = null; - var pc = 0; var arg1 = context.GetArgument(argOffset); - LuaFunction? functionToInspect = null; - Prototype? syntheticPrototype = null; - var callerPc = -1; + arg1.TryReadFunction(out var functionToInspect); + Prototype? prototype = null; + int? pc = null; + var isTailCall = false; + (Prototype? Prototype, int Pc) caller = default; - if (!arg1.TryReadFunction(out var inspected)) + if (functionToInspect == null) { if (!arg1.TryReadDouble(out _)) context.ThrowBadArgument(argOffset, "function or level expected"); - var level = context.GetIntegerArgument(argOffset) + 1; + var level = context.GetIntegerArgument(argOffset); var callStack = state.GetCallStackFrames(); - var discarded = TryGetDiscardedCaller(state, out var caller); - var logicalLength = callStack.Length + (discarded ? 1 : 0); - - if (level <= 0 || level > logicalLength) + var tailCaller = state.GetNativeTailCaller(); + var discarded = tailCaller.Prototype != null; + if ((uint)level >= (uint)(callStack.Length + (discarded ? 1 : 0))) { return new(context.Return(LuaValue.Nil)); } - if (discarded && level == 2) + // Level 1 uses the native frame's slot to describe the discarded Lua caller. + var synthetic = discarded && level == 1; + var index = callStack.Length - 1 - level; + if (discarded && level > 0) + index++; + ref readonly var frame = ref callStack[index]; + if (synthetic) { - // Level 1 (here 2) is the caller the tail call discarded, reported from its - // recorded call site instead of skipping to the frame below it. - previousFrame = callStack.Length >= 2 ? callStack[^2] : null; - pc = caller.LinePc; - callerPc = caller.CallerPc; - syntheticPrototype = caller.Prototype; + prototype = tailCaller.Prototype; + pc = tailCaller.Pc; + isTailCall = tailCaller.IsTailCall; } else { - var index = discarded && level > 2 ? level - 1 : level; - currentFrame = callStack[^index]; - previousFrame = index + 1 <= callStack.Length ? callStack[^(index + 1)] : null; - if (index != 1) - { - pc = callStack[^(index - 1)].CallerInstructionIndex; - } + functionToInspect = frame.Function; + pc = index + 1 < callStack.Length ? callStack[index + 1].CallerInstructionIndex : 0; + isTailCall = frame.IsTailCall; + } - functionToInspect = currentFrame.Value.Function; + if (discarded && level == 0) + { + caller = (tailCaller.Prototype, tailCaller.Pc); + isTailCall = false; + } + else if (index > 0 && !(synthetic && isTailCall)) + { + caller = ( + (callStack[index - 1].Function as LuaClosure)?.Proto, + frame.CallerInstructionIndex + ); } - } - else - { - functionToInspect = inspected; } + prototype ??= (functionToInspect as LuaClosure)?.Proto; using var debug = LuaDebug.Create( context.GlobalState, - previousFrame, - currentFrame, functionToInspect, - syntheticPrototype, + prototype, pc, - callerPc, + isTailCall, + caller, what, out var isValid ); @@ -709,18 +689,12 @@ out var isValid : LuaValue.Nil; } - if (what.Contains('L')) + if (what.Contains('L') && prototype is not null) { - if (functionToInspect is LuaClosure closure) - { - LuaTable activeLines = new(0, 8); - foreach (var line in closure.Proto.LineInfo) - { - activeLines[line] = true; - } - - table["activelines"] = activeLines; - } + LuaTable activeLines = new(0, 8); + foreach (var line in prototype.LineInfo) + activeLines[line] = true; + table["activelines"] = activeLines; } return new(context.Return(table)); diff --git a/tests/Lua.Debugger.Dap.Tests/DapTests.cs b/tests/Lua.Debugger.Dap.Tests/DapTests.cs index 8069e1bb..35d0facf 100644 --- a/tests/Lua.Debugger.Dap.Tests/DapTests.cs +++ b/tests/Lua.Debugger.Dap.Tests/DapTests.cs @@ -238,10 +238,7 @@ await host.RequestAsync( children["body"]!["variables"]![0]!["value"]!.GetValue(), Is.EqualTo("\"é🐇\"") ); - var evaluated = await host.RequestAsync( - "evaluate", - new() { ["frameId"] = frameId, ["expression"] = "x+4" } - ); + var evaluated = await host.RequestAsync("evaluate", new() { ["expression"] = "x+4" }); Assert.That(evaluated["body"]!["result"]!.GetValue(), Is.EqualTo("5")); await host.RequestAsync( "setVariable", @@ -391,9 +388,14 @@ await host.RequestAsync( ); Assert.That(frames[0]!["line"]!.GetValue(), Is.EqualTo(5)); - // The page must still resolve scopes for the frame it returned. - var scopes = await host.RequestAsync("scopes", new() { ["frameId"] = 2 }); - Assert.That(scopes["body"]!["scopes"]!.AsArray().Count, Is.EqualTo(3)); + foreach (var frame in all["body"]!["stackFrames"]!.AsArray()) + { + var scopes = await host.RequestAsync( + "scopes", + new() { ["frameId"] = frame!["id"]!.GetValue() } + ); + Assert.That(scopes["body"]!["scopes"]!.AsArray().Count, Is.EqualTo(3)); + } var beyond = await host.RequestAsync( "stackTrace", diff --git a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs index 1ae821da..7b3cdd88 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs @@ -1,3 +1,4 @@ +using Lua.Debugging; using Lua.Runtime; using NUnit.Framework; @@ -30,4 +31,22 @@ public void TrapEncodingDoesNotOverflowTheInstructionNameTable() Assert.That(trap.ToString(), Is.EqualTo("TRAP 63")); Assert.That(trap.OpCode, Is.EqualTo((OpCode)63)); } + + [Test] + public void ResetRestoresOriginalInstructionsWhileStillAttached() + { + using var state = LuaState.Create(); + using var debugger = new LuaDebugger(state); + debugger.Attach(); + var closure = state.Load("local value = 42\nreturn value", "=reset"); + debugger.RegisterSource(closure); + debugger.SetBreakpoints("=reset", [2]); + + var code = ((ILuaDebugSession)debugger).GetExecutionCode(closure); + Assert.That(code.Enabled, Is.True); + + debugger.Reset(); + Assert.That(debugger.IsAttached, Is.True); + Assert.That(code.Code, Is.EqualTo(closure.Proto.Code.ToArray())); + } } diff --git a/tests/Lua.Tests/DebugDiscardedCallerTests.cs b/tests/Lua.Tests/DebugDiscardedCallerTests.cs index 7854d3b8..c0a3a2d0 100644 --- a/tests/Lua.Tests/DebugDiscardedCallerTests.cs +++ b/tests/Lua.Tests/DebugDiscardedCallerTests.cs @@ -17,23 +17,42 @@ static LuaState CreateState() return state; } - [Test] - public async Task GetInfoReportsTheCallerDiscardedByANativeTailCall() + [TestCase(false)] + [TestCase(true)] + public async Task GetInfoReportsTheCallerDiscardedByANativeTailCall(bool tailCall) { using var state = CreateState(); + var invoke = tailCall + ? "local function outer() return f(1) end\nlocal info = outer()" + : "local info = f(1)"; var results = await state.DoStringAsync( - "local function f(a)\n" - + " return debug.getinfo(1, 'Slu')\n" - + "end\n" - + "local info = f(1)\n" - + "return info.what, info.linedefined, info.currentline, info.nparams, info.isvararg", + $""" + local function f(a) + return debug.getinfo(1, 'SluLntf') + end + {invoke} + return info.what, info.linedefined, info.currentline, info.nparams, info.isvararg, + info.istailcall, info.name, info.activelines[2], info.func + """, "=discarded" ); - Assert.That(results[0].Read(), Is.EqualTo("Lua")); - Assert.That(results[1].Read(), Is.EqualTo(1)); - Assert.That(results[2].Read(), Is.EqualTo(2)); - Assert.That(results[3].Read(), Is.EqualTo(1)); - Assert.That(results[4].Read(), Is.False); + Assert.That( + results, + Is.EqualTo( + new LuaValue[] + { + "Lua", + 1, + 2, + 1, + false, + tailCall, + tailCall ? LuaValue.Nil : "f", + true, + LuaValue.Nil, + } + ) + ); } [Test] diff --git a/tests/Lua.Tests/TailCallErrorPositionTests.cs b/tests/Lua.Tests/TailCallErrorPositionTests.cs index 92f69316..3abcbcb8 100644 --- a/tests/Lua.Tests/TailCallErrorPositionTests.cs +++ b/tests/Lua.Tests/TailCallErrorPositionTests.cs @@ -9,23 +9,6 @@ namespace Lua.Tests; /// public class TailCallErrorPositionTests { - [Test] - public async Task ErrorFromTailCalledLibraryFunctionReportsTheCallingLine() - { - using var state = LuaState.Create(); - state.OpenStandardLibraries(); - var results = await state.DoStringAsync( - "local function call()\n" - + " return string.rep(true)\n" - + "end\n" - + "local ok, err = pcall(call)\n" - + "return ok, err", - "=tailcall" - ); - Assert.That(results[0].Read(), Is.False); - Assert.That(results[1].ToString(), Does.Contain(":2: bad argument #1 to 'rep'")); - } - [Test] public async Task NestedTailCallsReportTheInnermostCallingLine() { @@ -46,6 +29,74 @@ public async Task NestedTailCallsReportTheInnermostCallingLine() Assert.That(results[1].ToString(), Does.Contain(":2: bad argument #1 to 'concat'")); } + [TestCase(false)] + [TestCase(true)] + public async Task TailCallSiteTakesPrecedenceOverOlderLuaFrames(bool protectedCall) + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var closure = state.Load( + """ + local function inner() + return string.rep(true) + end + local function middle() + return (inner()) + end + return (middle()) + """, + "=nested" + ); + + string message; + if (protectedCall) + { + state.Environment["call"] = closure; + var results = await state.DoStringAsync("return pcall(call)"); + Assert.That(results[0].Read(), Is.False); + message = results[1].Read(); + } + else + { + var exception = Assert.ThrowsAsync(async () => + await state.ExecuteAsync(closure) + ); + message = exception!.Message; + } + Assert.That(message, Does.Contain("nested:2: bad argument #1 to 'rep'")); + } + + [Test] + public void TailCalledErrorAtLevelZeroOmitsTheSourcePosition() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync("return error('message', 0)", "=level-zero") + ); + Assert.That(exception!.Message, Is.EqualTo("Lua-CSharp: message")); + } + + [Test] + public void TailCallSiteSurvivesChangesToLineHookState() + { + using var state = LuaState.Create(); + var hook = new LuaFunction((context, _) => new(context.Return())); + state.SetHook(hook, "c"); + state.Environment["fail"] = new LuaFunction( + (context, _) => + { + context.State.SetHook(hook, "l"); + throw new LuaRuntimeException(context.State, "message"); + } + ); + + var exception = Assert.ThrowsAsync(async () => + await state.DoStringAsync("local value = 42\nreturn fail()", "=tail-hook") + ); + Assert.That(exception!.Message, Is.EqualTo("Lua-CSharp: tail-hook:2: message")); + } + [Test] public async Task OrdinaryCallsKeepReportingTheirLine() { From 2439e3bd58d3c92b0d4b04c267e55efb9637cfd1 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:12:19 +0900 Subject: [PATCH 19/21] Fix debugger lifecycle and empty-table iteration --- src/Lua.Debugger/LuaDebugger.Code.cs | 10 +++--- src/Lua.Debugger/LuaDebugger.Inspection.cs | 2 -- src/Lua.Debugger/LuaDebugger.cs | 12 +++++++ src/Lua/Internal/LuaValueDictionary.cs | 40 +++++++--------------- src/Lua/LuaState.cs | 4 +-- src/Lua/LuaTable.cs | 14 ++++---- src/Lua/Runtime/LuaDebugCode.cs | 2 ++ src/Lua/Runtime/LuaVirtualMachine.cs | 26 +++++++++++--- tests/Lua.Debugger.Tests/DebuggerTests.cs | 20 +++++++++++ tests/Lua.Tests/NextIterationTests.cs | 16 +++++---- tests/Lua.Tests/TracebackTests.cs | 31 +++++++++++------ 11 files changed, 110 insertions(+), 67 deletions(-) diff --git a/src/Lua.Debugger/LuaDebugger.Code.cs b/src/Lua.Debugger/LuaDebugger.Code.cs index af35f0c4..a3eb9c5a 100644 --- a/src/Lua.Debugger/LuaDebugger.Code.cs +++ b/src/Lua.Debugger/LuaDebugger.Code.cs @@ -5,10 +5,10 @@ namespace Lua.Debugging; public sealed partial class LuaDebugger { - const int WeakReferenceCleanupInterval = 64; + const int MinimumCleanupInterval = 64; readonly ConditionalWeakTable executionCodes = new(); readonly List> codeReferences = new(); - int additionsSinceCleanup; + int additionsUntilCleanup = MinimumCleanupInterval; LuaDebugCode ILuaDebugSession.GetExecutionCode(LuaClosure closure) { @@ -44,10 +44,11 @@ LuaDebugCode ILuaDebugSession.GetExecutionCode(LuaClosure closure) void Track(ExecutionCode code) { - if (++additionsSinceCleanup >= WeakReferenceCleanupInterval) + if (--additionsUntilCleanup == 0) { - additionsSinceCleanup = 0; codeReferences.RemoveAll(static reference => !reference.TryGetTarget(out _)); + // Amortize each full scan over the surviving entries. + additionsUntilCleanup = Math.Max(MinimumCleanupInterval, codeReferences.Count); } codeReferences.Add(new WeakReference(code)); } @@ -67,6 +68,7 @@ void UpdateExecutionCodes(bool breakpointsChanged = false) else codeReferences.RemoveAt(i); } + additionsUntilCleanup = Math.Max(MinimumCleanupInterval, codeReferences.Count); } void UpdateControls(ExecutionCode code, bool attached) diff --git a/src/Lua.Debugger/LuaDebugger.Inspection.cs b/src/Lua.Debugger/LuaDebugger.Inspection.cs index 7cfd88e2..73c3fddd 100644 --- a/src/Lua.Debugger/LuaDebugger.Inspection.cs +++ b/src/Lua.Debugger/LuaDebugger.Inspection.cs @@ -336,7 +336,6 @@ CancellationToken cancellationToken ); var previousPc = context.State.LastPc; var previousPrototype = context.State.LastCallerPrototype; - var previousNativeTailCaller = context.State.LastNativeTailCaller; evaluating = true; context.State.NonYieldableCallCount++; try @@ -349,7 +348,6 @@ CancellationToken cancellationToken context.State.NonYieldableCallCount--; context.State.LastPc = previousPc; context.State.LastCallerPrototype = previousPrototype; - context.State.LastNativeTailCaller = previousNativeTailCaller; evaluating = false; } } diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index ba9c9a6c..f273a273 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -226,6 +226,18 @@ void ILuaDebugSession.ExecutionEnded() } } + void ILuaDebugSession.ThreadDisposed(LuaState state) + { + lock (gate) + { + if (step?.State != state) + return; + step = null; + if (!pauseRequested) + UpdateExecutionCodes(); + } + } + static bool ShouldStep(LuaState state, StepRequest request) { if (request.Mode == LuaDebugStep.Into) diff --git a/src/Lua/Internal/LuaValueDictionary.cs b/src/Lua/Internal/LuaValueDictionary.cs index a473d557..ca1f138b 100644 --- a/src/Lua/Internal/LuaValueDictionary.cs +++ b/src/Lua/Internal/LuaValueDictionary.cs @@ -40,7 +40,7 @@ public LuaValueDictionary(int capacity) public int Count => _count - _freeCount; - public int NilCount => _nilCount; + public int LiveCount => Count - _nilCount; public LuaValue this[LuaValue key] { @@ -335,22 +335,6 @@ void Delete(LuaValue key) if (Unsafe.IsNullRef(ref value) || value.Type == LuaValueType.Nil) return; - if (_nilCount + 1 == Count) - { - // Keep the dead entry instead of dropping it: Lua allows clearing the key that `next` - // just returned, so that key must remain findable. The next insertion compacts or - // reuses this storage as usual. - ref var deleted = ref _entries![index]; - if (deleted.keyObject is { } lastObject) - { - deleted.keyObject = CreateDeletedKey(lastObject); - deleted.keyType = LuaValueType.Nil; - } - deleted.value = default; - _nilCount++; - return; - } - ref var entry = ref _entries![index]; if (entry.keyObject is { } keyObject) { @@ -456,15 +440,20 @@ public LuaNextStatus TryGetNext(LuaValue key, out KeyValuePair= -1, value.Type: not LuaValueType.Nil }) { pair = new(new(entry.keyType, entry.keyNumber, entry.keyObject), entry.value); return LuaNextStatus.Found; } + + // Reuse the enumerator's scan when there are holes after the cursor. + index++; + return MoveNext(this, _version, ref index, out pair) + ? LuaNextStatus.Found + : LuaNextStatus.End; } pair = default; @@ -561,20 +550,15 @@ out KeyValuePair current ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion(); } - SearchNext: // Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends. // dictionary.count+1 could be negative if dictionary.count is int.MaxValue - while ((uint)index < (uint)dictionary._count) + var end = dictionary.LiveCount == 0 ? 0 : dictionary._count; + while ((uint)index < (uint)end) { ref var entry = ref dictionary._entries![index++]; - if (entry.next >= -1) + if (entry is { next: >= -1, value.Type: not LuaValueType.Nil }) { - if (entry.value.Type is LuaValueType.Nil) - { - goto SearchNext; - } - current = new(new(entry.keyType, entry.keyNumber, entry.keyObject), entry.value); return true; } diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index 886f54f6..d13db025 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -689,7 +689,6 @@ CancellationToken cancellationToken var state = this; var callStackTop = state.CallStackFrameCount; var previousCallerPrototype = state.LastCallerPrototype; - var previousNativeTailCaller = state.LastNativeTailCaller; var previousPc = state.LastPc; var entered = EnterExecution(); try @@ -727,7 +726,6 @@ CancellationToken cancellationToken { PopCallStackFrameUntil(callStackTop); LastCallerPrototype = previousCallerPrototype; - LastNativeTailCaller = previousNativeTailCaller; LastPc = previousPc; ExitExecution(entered); } @@ -1008,6 +1006,6 @@ public void Dispose() CoreData.Release(); CoreData = null!; if (Volatile.Read(ref GlobalState.ActiveExecutions) == 0) - Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); + Volatile.Read(ref GlobalState.Debugger)?.ThreadDisposed(this); } } diff --git a/src/Lua/LuaTable.cs b/src/Lua/LuaTable.cs index 85bc541b..c51c47ff 100644 --- a/src/Lua/LuaTable.cs +++ b/src/Lua/LuaTable.cs @@ -108,7 +108,7 @@ public LuaValue this[LuaValue key] } } - public int HashMapCount => dictionary.Count - dictionary.NilCount; + public int HashMapCount => dictionary.LiveCount; public int ArrayLength { @@ -384,13 +384,11 @@ internal LuaNextStatus TryGetNextStatus(LuaValue key, out KeyValuePair indexList = new(dictionary.Count); + using PooledList<(int, LuaValue)> indexList = new(dictionary.LiveCount); // Move some of the elements of the hash part to a newly allocated array foreach (var kv in dictionary) diff --git a/src/Lua/Runtime/LuaDebugCode.cs b/src/Lua/Runtime/LuaDebugCode.cs index 32cc8747..3cf56c85 100644 --- a/src/Lua/Runtime/LuaDebugCode.cs +++ b/src/Lua/Runtime/LuaDebugCode.cs @@ -8,6 +8,8 @@ internal interface ILuaDebugSession void ExecutionEnded(); + void ThreadDisposed(LuaState state); + ValueTask CheckpointAsync( LuaState state, LuaClosure closure, diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index 6032a5bd..a67d9f97 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -66,6 +66,9 @@ void Init(LuaState state, in CallStackFrame frame, CancellationToken cancellatio public LuaDebugCode? DebugCode; public LuaDebugCode? PreviousDebugCode; + (Prototype? Prototype, int Pc, bool IsTailCall) previousNativeTailCaller; + bool hasNativeTailCaller; + public bool IsTopLevel => BaseCallStackCount == State.CallStackFrameCount; public int BaseCallStackCount; @@ -219,6 +222,17 @@ public void PopOnTopCallStackFrames() State.PopCallStackFrameUntil(BaseCallStackCount); } + public void RecordNativeTailCaller(bool callerWasTailCalled) + { + // A reentrant VM context must give the outer native callee its call site back. + if (!hasNativeTailCaller) + { + previousNativeTailCaller = State.LastNativeTailCaller; + hasNativeTailCaller = true; + } + State.LastNativeTailCaller = (State.LastCallerPrototype, Pc, callerWasTailCalled); + } + bool ExecutePostOperation( int varArgs, PostOperationType postOperation, @@ -364,6 +378,12 @@ or PostOperationType.DontPop } finally { + if (hasNativeTailCaller) + { + State.LastNativeTailCaller = previousNativeTailCaller; + previousNativeTailCaller = default; + hasNativeTailCaller = false; + } Stack = null!; State = null!; LuaClosure = null!; @@ -1809,11 +1829,7 @@ static bool TailCall(VirtualMachineExecutionContext context, out bool doRestart) var isLuaClosure = func is LuaClosure; if (!isLuaClosure) - state.LastNativeTailCaller = ( - state.LastCallerPrototype, - context.Pc, - lastFrame.IsTailCall - ); + context.RecordNativeTailCaller(lastFrame.IsTailCall); if (state.CallOrReturnHookMask.Value != 0 && !context.State.IsInHook) { diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index 9216d5fb..f3799e00 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -748,6 +748,26 @@ await state.ExecuteAsync(closure, timeout.Token), Assert.That(failure!.Message, Does.Contain("after the Stopped handler returns")); } + [Test] + public async Task DisposingAnIdleSiblingPreservesTheNextPause() + { + using var state = LuaState.Create(); + using var debugger = new LuaDebugger(state); + debugger.Attach(); + var stops = new List(); + debugger.Stopped += stop => + { + stops.Add(stop.Reason); + debugger.Continue(stop.Id); + }; + + using (var sibling = state.CreateThread()) + debugger.Pause(); + await state.DoStringAsync("return 42"); + + Assert.That(stops, Is.EqualTo(new[] { "pause" })); + } + [Test] public async Task PauseRequestDoesNotOutliveItsExecution() { diff --git a/tests/Lua.Tests/NextIterationTests.cs b/tests/Lua.Tests/NextIterationTests.cs index 2af167d6..6d60d608 100644 --- a/tests/Lua.Tests/NextIterationTests.cs +++ b/tests/Lua.Tests/NextIterationTests.cs @@ -42,15 +42,17 @@ public async Task ClearedLastKeyStillEndsTheIteration() { using var state = CreateState(); var results = await state.DoStringAsync( - "local t = {a = 1}\n" - + "local k = next(t)\n" - + "t[k] = nil\n" - + "local ok, v = pcall(next, t, k)\n" - + "return ok, v == nil", + """ + local t = {1, a = 1} + t.a = nil + local ok, v = pcall(next, t, 'a') + local arrayEnd = next(t, 1) == nil + t[1] = nil + return ok, v == nil, arrayEnd, next(t) == nil, not pcall(next, t, 'missing') + """, "=next" ); - Assert.That(results[0].Read(), Is.True); - Assert.That(results[1].Read(), Is.True); + Assert.That(results, Is.EqualTo(new LuaValue[] { true, true, true, true, true })); } [Test] diff --git a/tests/Lua.Tests/TracebackTests.cs b/tests/Lua.Tests/TracebackTests.cs index b3057191..6bc85b04 100644 --- a/tests/Lua.Tests/TracebackTests.cs +++ b/tests/Lua.Tests/TracebackTests.cs @@ -60,27 +60,38 @@ await state.DoStringAsync( Assert.That(trace.StackFrames.ToArray(), Is.EqualTo(frames)); } - [Test] - public async Task NestedExecutionRestoresTailCallerForArgumentErrors() + [TestCase(false)] + [TestCase(true)] + public async Task NestedExecutionRestoresTailCallerForArgumentErrors(bool useCallAsync) { using var state = LuaState.Create(); - state.Environment["inner"] = new LuaFunction((context, _) => new(context.Return())); + state.Environment["inner"] = new LuaFunction( + async (context, _) => + { + if (useCallAsync) + await Task.Yield(); + return context.Return(); + } + ); + var inner = state.Load("return inner()", "=inner-call"); state.Environment["outer"] = new LuaFunction( async (context, token) => { - await context.State.DoStringAsync( - "local function f() return inner() end; return f()", - "inner-call", - token - ); + if (useCallAsync) + await context.State.CallAsync(inner, [], token); + else + await context.State.ExecuteAsync(inner, token); LuaRuntimeException.BadArgument(context.State, 1, "bad value"); return context.Return(); } ); var exception = Assert.ThrowsAsync(async () => - await state.DoStringAsync("return outer()", "outer-call") + await state.DoStringAsync("local x = 1\nreturn outer()", "=outer-call") )!; Assert.That(exception.InnerException, Is.Null); - Assert.That(exception.ErrorObject.Read(), Does.Contain("to 'outer'")); + Assert.That( + exception.Message, + Does.StartWith("Lua-CSharp: outer-call:2: bad argument #1 to 'outer'") + ); } } From bcac366dfcb7161d82e22a06962a8ef1d12acd48 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:32:19 +0900 Subject: [PATCH 20/21] Keep debugger resume state in the VM --- docs/debugger.md | 4 +- src/Lua.Debugger/LuaDebugger.Code.cs | 41 +++++------- src/Lua.Debugger/LuaDebugger.Sources.cs | 38 +++++------ src/Lua.Debugger/LuaDebugger.cs | 63 +++++++++++-------- src/Lua/LuaState.cs | 4 +- src/Lua/Runtime/LuaDebugCode.cs | 21 ++----- src/Lua/Runtime/LuaVirtualMachine.Debug.cs | 26 +++----- src/Lua/Runtime/LuaVirtualMachine.cs | 47 ++++++-------- tests/Lua.Debugger.Tests/DebuggerTests.cs | 16 +++-- tests/Lua.Debugger.Tests/DebuggerTrapTests.cs | 14 ++--- 10 files changed, 124 insertions(+), 150 deletions(-) diff --git a/docs/debugger.md b/docs/debugger.md index c0ee4577..aa5ae847 100644 --- a/docs/debugger.md +++ b/docs/debugger.md @@ -132,7 +132,7 @@ The synchronous pump drains accepted callbacks even when a stop is resumed immed | Step over | At the next line in the original frame, or after it returns. A tail-call replacement is distinct. | | Step out | After the original frame returns. A coroutine yielding/completing can transfer the stop to its caller. | -Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. A pause request belongs to the execution it was issued against: if that execution unwinds before a checkpoint consumes it, the request is dropped rather than stopping the next one. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, raw table/userdata contents and stack frames are inspected lazily with paging, and a stack page costs work proportional to the page rather than to the stack depth. The `Locals` scope keeps `debug.getlocal` numbering, so hidden loop control variables (such as `(for index)` or `(for control)`) appear alongside ordinary locals and varargs are listed as `...[n]`. Value display does not call user `__tostring`. +Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. A pause requested during execution expires if that execution unwinds before reaching a checkpoint. A pause requested while idle waits for the next checkpoint. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, raw table/userdata contents and stack frames are inspected lazily with paging, and a stack page costs work proportional to the page rather than to the stack depth. The `Locals` scope keeps `debug.getlocal` numbering, so hidden loop control variables (such as `(for index)` or `(for control)`) appear alongside ordinary locals and varargs are listed as `...[n]`. Value display does not call user `__tostring`. Source paths use host path/case rules. Breakpoints resolve to the first executable line at or after the requested line. Unknown sources remain pending until registered/executed, with breakpoint-change events on resolution. There is no basename-only fallback. For an in-memory chunk, use an `=name` chunk name, register its text, and use the source reference supplied by the adapter. @@ -158,7 +158,7 @@ Trap handling lives in the dispatch switch's existing fallback path. There is no A forward numeric-loop setup jump needs no separate header trap when it stays on the same source line. The setup records that untrapped header once, preserving the first backward jump's line event. Loop-body targets and source-line boundaries remain checkpoints. Call/return checkpoints remain available for pause requests around host awaits; unreachable fallthrough points after returns and tail calls are omitted. -An internal resume trap executes the stopped instruction once and restores its checkpoint. Only the VM owner writes that marker. It survives breakpoint edits during a stop and does not replay Lua hooks. Private execution copies keep shared prototypes, other Lua states, and bytecode dumps unchanged. Volatile control flags are checked only at traps, so ordinary instruction fetching needs no debugger flag or volatile read. Execution-copy metadata is weakly cached and released with its prototype. Detached execution uses the ordinary instructions directly. +The VM resumes with the original instruction it saved before stopping. Breakpoint edits can patch future fetches while the saved instruction executes once. Lua hooks already run for that instruction stay counted once. Private execution copies keep shared prototypes, other Lua states, and bytecode dumps unchanged. Volatile control flags are checked only at traps, so ordinary instruction fetching needs no debugger flag or volatile read. Execution-copy metadata is weakly cached and released with its prototype. Detached execution uses the ordinary instructions directly. Execution copies carry an opaque session identity and their source metadata, without a reference back to the debugger. A yielded coroutine therefore does not retain a detached debugger or its application callbacks. The weak tracking list prunes dead entries periodically, before it grows, and during control changes. Compiling temporary chunks therefore does not grow that list indefinitely while controls stay unchanged. The VM checks ownership at frame restart; within the instruction loop it reads the active debugger only at enabled checkpoints. Ownership requires no weak-reference lookup on the execution path. diff --git a/src/Lua.Debugger/LuaDebugger.Code.cs b/src/Lua.Debugger/LuaDebugger.Code.cs index a3eb9c5a..b3350f29 100644 --- a/src/Lua.Debugger/LuaDebugger.Code.cs +++ b/src/Lua.Debugger/LuaDebugger.Code.cs @@ -10,38 +10,31 @@ public sealed partial class LuaDebugger readonly List> codeReferences = new(); int additionsUntilCleanup = MinimumCleanupInterval; - LuaDebugCode ILuaDebugSession.GetExecutionCode(LuaClosure closure) + LuaDebugCode ILuaDebugSession.GetExecutionCode(Prototype prototype) { - if (executionCodes.TryGetValue(closure.Proto, out var existing)) + if (executionCodes.TryGetValue(prototype, out var existing)) return existing; lock (gate) { // GetValue instead of TryGetValue/Add: the execution thread and the protocol thread // can both reach a prototype first, and Add would throw on the loser. - return executionCodes.GetValue( - closure.Proto, - prototype => - { - if (!registered.TryGetValue(prototype, out var source)) - { - RegisterSource(closure); - source = registered.GetValue( - prototype, - _ => throw new InvalidOperationException() - ); - } - var code = new ExecutionCode(identity, prototype, source) - { - HasBreakpointLine = HasBreakpointLine(prototype, source), - }; - Track(code); - UpdateControls(code, IsAttached); - return code; - } - ); + return executionCodes.GetValue(prototype, CreateExecutionCode); } } + ExecutionCode CreateExecutionCode(Prototype prototype) + { + if (!registered.TryGetValue(prototype, out var source)) + source = RegisterSourceCore(prototype); + var code = new ExecutionCode(identity, prototype, source) + { + HasBreakpointLine = HasBreakpointLine(prototype, source), + }; + Track(code); + UpdateControls(code, IsAttached); + return code; + } + void Track(ExecutionCode code) { if (--additionsUntilCleanup == 0) @@ -73,7 +66,7 @@ void UpdateExecutionCodes(bool breakpointsChanged = false) void UpdateControls(ExecutionCode code, bool attached) { - code.SetEnabled(attached && (pauseRequested || step != null || code.HasBreakpointLine)); + code.SetEnabled(attached && (PauseRequested || step != null || code.HasBreakpointLine)); } static bool HasBreakpointLine(Prototype prototype, SourceEntry source) diff --git a/src/Lua.Debugger/LuaDebugger.Sources.cs b/src/Lua.Debugger/LuaDebugger.Sources.cs index 23c80628..69108de9 100644 --- a/src/Lua.Debugger/LuaDebugger.Sources.cs +++ b/src/Lua.Debugger/LuaDebugger.Sources.cs @@ -75,27 +75,27 @@ void ClearBreakpointsCore() public LuaDebugSource RegisterSource(LuaClosure closure, string? content = null) { lock (gate) + return RegisterSourceCore(closure.Proto, content).Source; + } + + SourceEntry RegisterSourceCore(Prototype rootPrototype, string? content = null) + { + var work = new Stack(); + var controlsChanged = false; + work.Push(rootPrototype); + while (work.Count != 0) { - var work = new Stack(); - var controlsChanged = false; - work.Push(closure.Proto); - while (work.Count != 0) - { - var prototype = work.Pop(); - controlsChanged |= RegisterPrototype(prototype); - foreach (var child in prototype.ChildPrototypes) - work.Push(child); - } - var entry = registered.GetValue( - closure.Proto, - _ => throw new InvalidOperationException() - ); - if (content != null) - entry.Source = entry.Source with { Content = content }; - if (controlsChanged) - UpdateExecutionCodes(breakpointsChanged: true); - return entry.Source; + var prototype = work.Pop(); + controlsChanged |= RegisterPrototype(prototype); + foreach (var child in prototype.ChildPrototypes) + work.Push(child); } + var entry = registered.GetValue(rootPrototype, _ => throw new InvalidOperationException()); + if (content != null) + entry.Source = entry.Source with { Content = content }; + if (controlsChanged) + UpdateExecutionCodes(breakpointsChanged: true); + return entry; } public LuaDebugSource[] GetSources() diff --git a/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index f273a273..5cd3713b 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -16,8 +16,8 @@ public sealed partial class LuaDebugger : IDisposable, ILuaDebugSession readonly Queue commands = new(); TaskCompletionSource signal = NewSignal(); PauseContext? paused; - StepRequest? step; - bool pauseRequested; + volatile StepRequest? step; + volatile PauseRequest pause; bool resumeRequested; bool evaluating; bool disposed; @@ -26,6 +26,8 @@ public sealed partial class LuaDebugger : IDisposable, ILuaDebugSession int nextStopId; int nextFrameId; + bool PauseRequested => pause != PauseRequest.None; + public LuaDebugger(LuaState state) : this(state, null) { } @@ -93,7 +95,7 @@ void Release(bool detach) Interlocked.CompareExchange(ref root.GlobalState.Debugger, null, this); ClearBreakpointsCore(); step = null; - pauseRequested = false; + pause = PauseRequest.None; resumeRequested = true; UpdateExecutionCodes(breakpointsChanged: true); signal.TrySetResult(true); @@ -109,7 +111,12 @@ public void Pause() { if (!IsAttached) throw new InvalidOperationException("The debugger is not attached."); - pauseRequested = true; + // Publish before sampling the count. If we see an active execution, its quiescence + // callback must see the request and take the gate before deciding whether to clear it. + pause = PauseRequest.ActiveExecution; + Interlocked.MemoryBarrier(); + if (Volatile.Read(ref root.GlobalState.ActiveExecutions) == 0) + pause = PauseRequest.NextCheckpoint; UpdateExecutionCodes(); } } @@ -128,7 +135,7 @@ internal bool TryContinue(int stopId, LuaDebugStep mode) { if (paused is not { } context || context.Stop.Id != stopId || resumeRequested) return false; - var stepping = pauseRequested || step != null; + var controlsEnabled = PauseRequested || step != null; step = mode == LuaDebugStep.Continue ? null @@ -136,7 +143,7 @@ internal bool TryContinue(int stopId, LuaDebugStep mode) resumeRequested = true; // Re-patching every prototype is only needed when stepping starts or stops; a plain // continue between two stops of the same kind leaves the enabled state unchanged. - if (stepping != (pauseRequested || step != null)) + if (controlsEnabled != (PauseRequested || step != null)) UpdateExecutionCodes(); signal.TrySetResult(true); inspections = context.Inspections; @@ -148,10 +155,10 @@ internal bool TryContinue(int stopId, LuaDebugStep mode) [MethodImpl(MethodImplOptions.NoInlining)] ValueTask ILuaDebugSession.CheckpointAsync( LuaState state, - LuaClosure closure, + LuaDebugCode code, int pc, int line, - bool lineEntry, + bool isLineEntry, CancellationToken cancellationToken ) { @@ -162,22 +169,19 @@ CancellationToken cancellationToken { if (paused != null || !IsAttached) return new(0); - var entry = registered.GetValue( - closure.Proto, - _ => throw new InvalidOperationException() - ); + var entry = ((ExecutionCode)code).Source; LuaBreakpoint? breakpoint = null; - if (lineEntry) + if (isLineEntry) entry.Points.TryGetValue(line, out breakpoint); - var stopForStep = lineEntry && step is { } request && ShouldStep(state, request); - if (!pauseRequested && !stopForStep && breakpoint == null) + var stopForStep = isLineEntry && step is { } request && ShouldStep(state, request); + var pauseConsumed = PauseRequested; + if (!pauseConsumed && !stopForStep && breakpoint == null) return new(0); var reason = - pauseRequested ? "pause" + pauseConsumed ? "pause" : breakpoint != null ? "breakpoint" : "step"; - var pauseConsumed = pauseRequested; - pauseRequested = false; + pause = PauseRequest.None; // `step` stays armed while the program is stopped. Clearing it here would un-patch and // then re-patch every prototype's traps on each step, which costs O(prototypes) per // step for no observable difference; Continue clears it when stepping really ends. @@ -206,16 +210,16 @@ ValueTask SuspendSynchronously( CancellationToken cancellationToken ) => new(SynchronousDebugPause.Run(token => SuspendAsync(context, token), cancellationToken)); - void ILuaDebugSession.ExecutionEnded() + void ILuaDebugSession.ExecutionQuiesced() { + if (pause != PauseRequest.ActiveExecution && step == null) + return; lock (gate) { - // A pause belongs to the execution it was requested against. If that execution unwound - // (cancellation, host error) before a checkpoint consumed it, keeping the request would - // stop the next, unrelated execution. A step that owns a still-suspended coroutine is - // kept: its Lua frames are live even though no execution is running. - var changed = pauseRequested; - pauseRequested = false; + var changed = pause == PauseRequest.ActiveExecution; + if (changed) + pause = PauseRequest.None; + // A yielded coroutine can still own a step when the global state becomes idle. if (step is { } request && request.State.CallStackFrameCount == 0) { step = null; @@ -233,7 +237,7 @@ void ILuaDebugSession.ThreadDisposed(LuaState state) if (step?.State != state) return; step = null; - if (!pauseRequested) + if (!PauseRequested) UpdateExecutionCodes(); } } @@ -394,6 +398,13 @@ public void Dispose() static TaskCompletionSource NewSignal() => new(TaskCreationOptions.RunContinuationsAsynchronously); + enum PauseRequest + { + None, + NextCheckpoint, + ActiveExecution, + } + sealed record StepRequest(LuaDebugStep Mode, LuaState State, int Depth, int FrameId); sealed record PauseContext(LuaState State, int Pc, int Depth, int FrameId, LuaDebugStop Stop) diff --git a/src/Lua/LuaState.cs b/src/Lua/LuaState.cs index d13db025..a8f4b0a5 100644 --- a/src/Lua/LuaState.cs +++ b/src/Lua/LuaState.cs @@ -735,7 +735,7 @@ CancellationToken cancellationToken internal bool EnterExecution() { // Nested host re-entry (Lua calling C# that calls Lua again) must not count: only the - // outermost entry owns the 0 -> 1 transition that later reports ExecutionEnded. + // outermost entry owns the 0 -> 1 transition that later reports global quiescence. // A compare-exchange keeps that decision atomic across coroutine resumes and host calls. return Interlocked.CompareExchange(ref GlobalState.ActiveExecutions, 1, 0) == 0; } @@ -746,7 +746,7 @@ internal void ExitExecution(bool entered) if (!entered) return; if (Interlocked.Decrement(ref GlobalState.ActiveExecutions) == 0) - Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); + Volatile.Read(ref GlobalState.Debugger)?.ExecutionQuiesced(); } [AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))] diff --git a/src/Lua/Runtime/LuaDebugCode.cs b/src/Lua/Runtime/LuaDebugCode.cs index 3cf56c85..e800980d 100644 --- a/src/Lua/Runtime/LuaDebugCode.cs +++ b/src/Lua/Runtime/LuaDebugCode.cs @@ -4,18 +4,19 @@ internal interface ILuaDebugSession { object Identity { get; } - LuaDebugCode GetExecutionCode(LuaClosure closure); + LuaDebugCode GetExecutionCode(Prototype prototype); - void ExecutionEnded(); + // The global state's active execution count just reached zero. + void ExecutionQuiesced(); void ThreadDisposed(LuaState state); ValueTask CheckpointAsync( LuaState state, - LuaClosure closure, + LuaDebugCode code, int pc, int line, - bool lineEntry, + bool isLineEntry, CancellationToken cancellationToken ); } @@ -59,18 +60,6 @@ internal void SetEnabled(bool enabled) Code[pc] = source[pc]; } - internal void ResumeAt(int pc) - { - Code[pc] = (uint)LuaVirtualMachine.DebuggerResumeOpCode; - } - - internal Instruction RestoreAt(int pc) - { - // Re-arm this checkpoint for later visits, unless controls were released while stopped. - Code[pc] = Enabled ? (uint)LuaVirtualMachine.DebuggerBreakOpCode : Prototype.Code[pc]; - return Prototype.Code[pc]; - } - static int[] FindTraps(Prototype prototype) { var code = prototype.Code; diff --git a/src/Lua/Runtime/LuaVirtualMachine.Debug.cs b/src/Lua/Runtime/LuaVirtualMachine.Debug.cs index 93876534..27f6389a 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.Debug.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.Debug.cs @@ -5,29 +5,19 @@ namespace Lua.Runtime; public static partial class LuaVirtualMachine { - // The opcode field is six bits wide (Instruction.SizeOp) and the published set currently ends - // at ExtraArg, so these two encodings are the only ones that cannot collide with a real - // instruction. DebuggerTrapTests fails if the enum ever grows into them. + // Reserve the highest encoding in the six-bit opcode field for debugger checkpoints. internal const OpCode DebuggerBreakOpCode = (OpCode)63; - internal const OpCode DebuggerResumeOpCode = (OpCode)62; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static bool IsDebugTrap(OpCode opCode) => - opCode is DebuggerBreakOpCode or DebuggerResumeOpCode; [MethodImpl(MethodImplOptions.NoInlining)] - static bool ExecuteDebuggerCheckpoint( - VirtualMachineExecutionContext context, - ILuaDebugSession? debugger - ) + static bool ExecuteDebuggerCheckpoint(VirtualMachineExecutionContext context, LuaDebugCode code) { - if (debugger == null) + var debugger = Volatile.Read(ref context.GlobalState.Debugger); + if (debugger == null || code.Identity != debugger.Identity) return false; - var prototype = context.Prototype; var pc = context.Pc; - var lines = prototype.LineInfo; + var lines = code.Prototype.LineInfo; var line = lines.IsEmpty ? 0 : lines[pc]; - var lineEntry = + var isLineEntry = pc == 0 || context.LastDebugPc < 0 || pc <= context.LastDebugPc @@ -35,10 +25,10 @@ static bool ExecuteDebuggerCheckpoint( context.LastDebugPc = pc; var task = debugger.CheckpointAsync( context.State, - context.LuaClosure, + code, pc, line, - lineEntry, + isLineEntry, context.CancellationToken ); if (task.IsCompleted) diff --git a/src/Lua/Runtime/LuaVirtualMachine.cs b/src/Lua/Runtime/LuaVirtualMachine.cs index a67d9f97..60d50478 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -304,17 +304,12 @@ public async ValueTask ExecuteClosureAsyncImpl() Task = default; if (PostOperation == PostOperationType.Debugger) { - PostOperation = PostOperationType.None; - // Re-fetch the stopped instruction without counting or firing its Lua hook twice. + // Dispatch the saved instruction without counting or firing its Lua hook twice. if (State.HookCount != 0) State.HookCount++; LastHookPc = Pc + 1; toCatchFlag = false; ThrowIfCancellationRequested(); - if ( - DebugCode!.Identity == Volatile.Read(ref GlobalState.Debugger)?.Identity - ) - DebugCode.ResumeAt(Pc + 1); continue; } ref readonly var frame = ref State.GetCurrentFrame(); @@ -480,7 +475,7 @@ static bool MoveNext(VirtualMachineExecutionContext context) else { context.PreviousDebugCode = current; - context.DebugCode = debugger?.GetExecutionCode(context.LuaClosure); + context.DebugCode = debugger?.GetExecutionCode(prototype); } } } @@ -514,9 +509,20 @@ static bool MoveNext(VirtualMachineExecutionContext context) } Loop: - while (true) + Instruction instruction; + if (context.PostOperation == PostOperationType.Debugger) + { + context.PostOperation = PostOperationType.None; + context.Pc++; + instruction = context.Instruction; + } + else + { + instruction = Unsafe.Add(ref instructionsHead, ++context.Pc); + } + + for (; ; instruction = Unsafe.Add(ref instructionsHead, ++context.Pc)) { - var instruction = Unsafe.Add(ref instructionsHead, ++context.Pc); context.Instruction = instruction; if (--hookCount == 0 || (lineHookFlag && context.Pc != context.LastHookPc)) { @@ -1200,31 +1206,16 @@ static void VarArg(VirtualMachineExecutionContext context) continue; default: - if (IsDebugTrap(opCode)) + if (opCode == DebuggerBreakOpCode && context.DebugCode is { } code) { - if (context.DebugCode == null) - { - ThrowLuaNotImplementedException(context, opCode); - return true; - } - instruction = - opCode == DebuggerResumeOpCode - ? context.DebugCode.RestoreAt(context.Pc) - : context.Prototype.Code[context.Pc]; - if (IsDebugTrap(instruction.OpCode)) + instruction = code.Prototype.Code[context.Pc]; + if (instruction.OpCode == DebuggerBreakOpCode) { ThrowLuaNotImplementedException(context, instruction.OpCode); return true; } context.Instruction = instruction; - if ( - opCode == DebuggerBreakOpCode - && context.DebugCode.Enabled - && ExecuteDebuggerCheckpoint( - context, - Volatile.Read(ref context.GlobalState.Debugger) - ) - ) + if (code.Enabled && ExecuteDebuggerCheckpoint(context, code)) return true; goto Dispatch; } diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index f3799e00..777cee45 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -282,8 +282,9 @@ await debugger.SetVariableAsync( Assert.That(await execution, Is.EqualTo(new LuaValue[] { LuaValue.Nil, 41 })); } - [Test] - public async Task RemovingALuaHookDuringAStopDoesNotReplayIt() + [TestCase(false)] + [TestCase(true)] + public async Task ResumingDoesNotReplayTheStoppedInstructionHook(bool armBreakpoint) { var (state, debugger, stops) = Create(); using var ownedState = state; @@ -302,9 +303,11 @@ public async Task RemovingALuaHookDuringAStopDoesNotReplayIt() 1 ); debugger.Pause(); - var execution = state.DoStringAsync("return 42", cancellationToken: timeout.Token).AsTask(); + var execution = state.DoStringAsync("return 42", "=resume-hook", timeout.Token).AsTask(); var stop = await stops.Reader.ReadAsync(timeout.Token); state.SetHook(null, ""); + if (armBreakpoint) + Assert.That(debugger.SetBreakpoints("=resume-hook", [1])[0].Verified, Is.True); var stoppedHooks = hooks; debugger.Continue(stop.Id); Assert.That(await execution, Is.EqualTo(new LuaValue[] { 42 })); @@ -748,8 +751,9 @@ await state.ExecuteAsync(closure, timeout.Token), Assert.That(failure!.Message, Does.Contain("after the Stopped handler returns")); } - [Test] - public async Task DisposingAnIdleSiblingPreservesTheNextPause() + [TestCase(false)] + [TestCase(true)] + public async Task IdlePauseSurvivesLifecycleNotifications(bool lateQuiescence) { using var state = LuaState.Create(); using var debugger = new LuaDebugger(state); @@ -763,6 +767,8 @@ public async Task DisposingAnIdleSiblingPreservesTheNextPause() using (var sibling = state.CreateThread()) debugger.Pause(); + if (lateQuiescence) + ((ILuaDebugSession)debugger).ExecutionQuiesced(); await state.DoStringAsync("return 42"); Assert.That(stops, Is.EqualTo(new[] { "pause" })); diff --git a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs index 7b3cdd88..669bc5d4 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs @@ -5,21 +5,15 @@ namespace Lua.Debugger.Tests; /// -/// The debugger trampoline occupies the two opcode encodings the six-bit instruction field -/// leaves unused. This guards the day someone grows the published set into them. +/// The debugger checkpoint reserves the highest encoding in the six-bit opcode field. /// public class DebuggerTrapTests { [Test] - public void TrapEncodingsStayOutsideThePublishedInstructionSet() + public void TrapEncodingStaysOutsideThePublishedInstructionSet() { var highest = Enum.GetValues().Select(static op => (byte)op).Max(); - Assert.That( - highest, - Is.LessThan(62), - "LuaVirtualMachine reserves opcode 62/63 for debugger traps; move the trampoline " - + "or widen Instruction.SizeOp before adding opcodes here." - ); + Assert.That(highest, Is.LessThan((byte)LuaVirtualMachine.DebuggerBreakOpCode)); Assert.That(Instruction.SizeOp, Is.EqualTo(6)); } @@ -42,7 +36,7 @@ public void ResetRestoresOriginalInstructionsWhileStillAttached() debugger.RegisterSource(closure); debugger.SetBreakpoints("=reset", [2]); - var code = ((ILuaDebugSession)debugger).GetExecutionCode(closure); + var code = ((ILuaDebugSession)debugger).GetExecutionCode(closure.Proto); Assert.That(code.Enabled, Is.True); debugger.Reset(); From 29174eb787a878e4479c398df17acd8ea3c203f1 Mon Sep 17 00:00:00 2001 From: akeit0 <90429982+Akeit0@users.noreply.github.com> Date: Wed, 11 Nov 2026 11:38:18 +0900 Subject: [PATCH 21/21] Refresh breakpoint traps after stack source discovery --- docs/debugger.md | 4 +-- src/Lua.Debugger/LuaDebugger.Inspection.cs | 26 ++++++++------- src/Lua.Debugger/LuaDebugger.Sources.cs | 29 +++++++++-------- tests/Lua.Debugger.Tests/DebuggerTests.cs | 38 ++++++++++++++++++++++ 4 files changed, 70 insertions(+), 27 deletions(-) diff --git a/docs/debugger.md b/docs/debugger.md index aa5ae847..9380cae0 100644 --- a/docs/debugger.md +++ b/docs/debugger.md @@ -132,7 +132,7 @@ The synchronous pump drains accepted callbacks even when a stop is resumed immed | Step over | At the next line in the original frame, or after it returns. A tail-call replacement is distinct. | | Step out | After the original frame returns. A coroutine yielding/completing can transfer the stop to its caller. | -Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. A pause requested during execution expires if that execution unwinds before reaching a checkpoint. A pause requested while idle waits for the next checkpoint. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, raw table/userdata contents and stack frames are inspected lazily with paging, and a stack page costs work proportional to the page rather than to the stack depth. The `Locals` scope keeps `debug.getlocal` numbering, so hidden loop control variables (such as `(for index)` or `(for control)`) appear alongside ordinary locals and varargs are listed as `...[n]`. Value display does not call user `__tostring`. +Frame identity supplements depth to handle tail calls. A pending step ends when its owning Lua stack has unwound and execution returns to the host; breakpoints remain installed for later invocations. A yielded coroutine retains its step across direct C# resumes because its Lua frames are still live. A pause requested during execution expires if that execution unwinds before reaching a checkpoint. A pause requested while idle waits for the next checkpoint. Hooks already executed for a stopped instruction are not executed again on resume. Locals, upvalues, varargs, globals, raw table/userdata contents and stack frames are inspected lazily. Stack paging materializes only the requested frames. The `Locals` scope keeps `debug.getlocal` numbering, so hidden loop control variables (such as `(for index)` or `(for control)`) appear alongside ordinary locals and varargs are listed as `...[n]`. Value display does not call user `__tostring`. Source paths use host path/case rules. Breakpoints resolve to the first executable line at or after the requested line. Unknown sources remain pending until registered/executed, with breakpoint-change events on resolution. There is no basename-only fallback. For an in-memory chunk, use an `=name` chunk name, register its text, and use the source reference supplied by the adapter. @@ -166,7 +166,7 @@ Execution copies carry an opaque session identity and their source metadata, wit Debugging is opt-in. Detached execution uses the original bytecode. Starting a server installs the debugger before accepting clients, but an installed debugger only pays for the checkpoints its controls need: with no breakpoints, no requested pause and no pending step, execution runs on an unpatched copy and matches detached speed within measurement noise (1.0–1.1x on loop-heavy workloads). Verified breakpoints patch only the prototypes whose lines they name, so a breakpoint in an uncalled function does not slow other functions from the same source. -Stops and steps cost work proportional to the number of *executed* prototypes, not to their instruction count: each stop revisits the tracked execution copies and applies cached control state. Breakpoint membership is cached per prototype and refreshed only when breakpoints or loaded sources change. +Stops and resumes reuse existing traps while the controls still need the same checkpoints. Control changes can revisit tracked execution copies. Breakpoint membership is cached per prototype and refreshed when breakpoints or registered source lines change. First execution builds private instruction copies and source metadata. Reusing compiled chunks avoids that setup on later calls. Pause, stepping and inspection allocate only as needed; execution ownership is tracked at host-call and coroutine-resume boundaries. diff --git a/src/Lua.Debugger/LuaDebugger.Inspection.cs b/src/Lua.Debugger/LuaDebugger.Inspection.cs index 73c3fddd..4794d8a9 100644 --- a/src/Lua.Debugger/LuaDebugger.Inspection.cs +++ b/src/Lua.Debugger/LuaDebugger.Inspection.cs @@ -18,8 +18,8 @@ public Task GetStackTraceAsync( ); /// - /// Reads one page of the suspended stack. The cost is proportional to the page, not to the - /// stack depth, which keeps protocol paging usable for deeply recursive scripts. + /// Materializes the requested frames. Discovering executable source lines can also update + /// breakpoints. /// internal Task GetStackTraceAsync( int stopId, @@ -42,6 +42,7 @@ LuaDebugStackTrace ReadStackTrace(PauseContext context, int start, int count) return new LuaDebugStackTrace(Array.Empty(), total); var end = (int)Math.Min(total, (long)start + count); var result = new LuaDebugStackFrame[end - start]; + var controlsChanged = false; for (var position = start; position < end; position++) { // Frames are reported top first, while the call stack stores the oldest frame first. @@ -53,22 +54,23 @@ LuaDebugStackTrace ReadStackTrace(PauseContext context, int start, int count) var name = frame.Function.Name; if (frame.Function is LuaClosure closure) { + var prototype = closure.Proto; lock (gate) - { - RegisterPrototype(closure.Proto); - source = registered - .GetValue(closure.Proto, _ => throw new InvalidOperationException()) - .Source; - } - if ((uint)pc < (uint)closure.Proto.LineInfo.Length) - line = closure.Proto.LineInfo[pc]; + source = RegisterPrototype(prototype, ref controlsChanged).Source; + if ((uint)pc < (uint)prototype.LineInfo.Length) + line = prototype.LineInfo[pc]; name = - closure.Proto.LineDefined == 0 + prototype.LineDefined == 0 ? "main chunk" - : $"function at {source.Name}:{closure.Proto.LineDefined}"; + : $"function at {source.Name}:{prototype.LineDefined}"; } result[position - start] = new LuaDebugStackFrame(i + 1, name, source, line); } + if (controlsChanged) + { + lock (gate) + UpdateExecutionCodes(breakpointsChanged: true); + } return new LuaDebugStackTrace(result, total); } diff --git a/src/Lua.Debugger/LuaDebugger.Sources.cs b/src/Lua.Debugger/LuaDebugger.Sources.cs index 69108de9..e5b7d40c 100644 --- a/src/Lua.Debugger/LuaDebugger.Sources.cs +++ b/src/Lua.Debugger/LuaDebugger.Sources.cs @@ -80,17 +80,21 @@ public LuaDebugSource RegisterSource(LuaClosure closure, string? content = null) SourceEntry RegisterSourceCore(Prototype rootPrototype, string? content = null) { - var work = new Stack(); var controlsChanged = false; - work.Push(rootPrototype); - while (work.Count != 0) + var entry = RegisterPrototype(rootPrototype, ref controlsChanged); + var children = rootPrototype.ChildPrototypes; + if (!children.IsEmpty) { - var prototype = work.Pop(); - controlsChanged |= RegisterPrototype(prototype); - foreach (var child in prototype.ChildPrototypes) + var work = new Stack(children.Length); + foreach (var child in children) work.Push(child); + while (work.TryPop(out var prototype)) + { + RegisterPrototype(prototype, ref controlsChanged); + foreach (var child in prototype.ChildPrototypes) + work.Push(child); + } } - var entry = registered.GetValue(rootPrototype, _ => throw new InvalidOperationException()); if (content != null) entry.Source = entry.Source with { Content = content }; if (controlsChanged) @@ -136,16 +140,16 @@ public async ValueTask ReadSourceAsync( return root.Platform.DecodeText(await input.ReadAllAsync(cancellationToken)); } - bool RegisterPrototype(Prototype prototype) + SourceEntry RegisterPrototype(Prototype prototype, ref bool controlsChanged) { - if (registered.TryGetValue(prototype, out _)) - return false; + if (registered.TryGetValue(prototype, out var entry)) + return entry; var name = root.Platform.DecodeText(prototype.ChunkName); var path = options.SourcePathResolver?.Invoke(name); if (path == null && name.StartsWith('@')) path = name[1..]; var key = path != null ? SourceKey(path) : "=" + name.TrimStart('='); - if (!sources.TryGetValue(key, out var entry)) + if (!sources.TryGetValue(key, out entry)) { path = path != null ? key : null; entry = new SourceEntry( @@ -163,7 +167,6 @@ bool RegisterPrototype(Prototype prototype) if (line > 0) entry.Lines.Add(line); registered.Add(prototype, entry); - var controlsChanged = false; if (breakpoints.TryGetValue(key, out var requests)) { entry.Points.Clear(); @@ -180,7 +183,7 @@ bool RegisterPrototype(Prototype prototype) } } } - return controlsChanged; + return entry; } static void Resolve(RequestedBreakpoint request, SourceEntry? entry) diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index 777cee45..5556d8be 100644 --- a/tests/Lua.Debugger.Tests/DebuggerTests.cs +++ b/tests/Lua.Debugger.Tests/DebuggerTests.cs @@ -30,6 +30,44 @@ public async Task NewlyRegisteredLinesUpdateExistingExecutionControls() Assert.That(stops, Is.EqualTo(new[] { 6, 4 })); } + [Test] + public async Task StackInspectionRefreshesTrapsWhenABreakpointMoves() + { + using var state = LuaState.Create(); + state.OpenStandardLibraries(); + var inner = state.Load("coroutine.yield()\n\n\n\n\nreturn 42", "=shared"); + state.Environment["inner"] = inner; + using var thread = state.CreateCoroutine( + state.Load("local n = 1\nlocal value = inner()\nreturn value", "=shared") + ); + using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); + var output = new LuaStack(); + await thread.ResumeAsync(output, timeout.Token); + + using var debugger = new LuaDebugger(state); + debugger.Attach(); + var code = ((ILuaDebugSession)debugger).GetExecutionCode(inner.Proto); + Assert.That(debugger.SetBreakpoints("=shared", [2])[0].Line, Is.EqualTo(6)); + var changes = new List(); + debugger.BreakpointChanged += breakpoint => changes.Add(breakpoint.Line); + var stops = Channel.CreateUnbounded(); + debugger.Stopped += stop => stops.Writer.TryWrite(stop); + var execution = thread.ResumeAsync(output, timeout.Token).AsTask(); + var stop = await stops.Reader.ReadAsync(timeout.Token); + try + { + Assert.That(code.Enabled, Is.True); + await debugger.GetStackTraceAsync(stop.Id, timeout.Token); + Assert.That(changes, Is.EqualTo(new[] { 2 })); + Assert.That(code.Enabled, Is.False); + } + finally + { + debugger.Continue(stop.Id); + await execution.WaitAsync(timeout.Token); + } + } + [TestCase("local n=0;for i=1,3 do n=n+i end;return n")] [TestCase("for i=1,3 do end; return 42")] [TestCase("for i=1,0 do error('unreachable') end; return 42")]