diff --git a/docs/debugger.md b/docs/debugger.md index 4e28102e..9380cae0 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. @@ -130,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 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. @@ -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. +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 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 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. 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/docs/stdlib/debug.md b/docs/stdlib/debug.md index 9e4521ea..bb7e9837 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. 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. | | [`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/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(); + } +} 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.Debugger.Dap/LuaDapSession.Requests.cs b/src/Lua.Debugger.Dap/LuaDapSession.Requests.cs index 54e5cba6..3f450cd1 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,8 @@ await RespondAsync( return; case "scopes": var frameId = Integer(args, "frameId"); - var allFrames = await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), token) - .ConfigureAwait(false); - if (!allFrames.Any(frame => frame.Id == frameId)) + var frameCount = debugger.GetFrameCount(CurrentStop(expectedStop)); + if (frameId < 1 || frameId > frameCount) throw new ArgumentException("Unknown frame."); var scopes = new JsonArray(); foreach ( @@ -293,20 +295,12 @@ await RespondAsync( .ConfigureAwait(false); return; case "evaluate": + var evaluationStop = CurrentStop(expectedStop); var evaluationFrame = Integer(args, "frameId"); if (evaluationFrame == 0) - evaluationFrame = ( - await debugger - .GetStackTraceAsync(CurrentStop(expectedStop), token) - .ConfigureAwait(false) - )[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/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.Code.cs b/src/Lua.Debugger/LuaDebugger.Code.cs index baf5a26d..b3350f29 100644 --- a/src/Lua.Debugger/LuaDebugger.Code.cs +++ b/src/Lua.Debugger/LuaDebugger.Code.cs @@ -5,61 +5,85 @@ namespace Lua.Debugging; public sealed partial class LuaDebugger { + const int MinimumCleanupInterval = 64; readonly ConditionalWeakTable executionCodes = new(); 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) { - 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(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) + { + 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)); } - 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); } + additionsUntilCleanup = Math.Max(MinimumCleanupInterval, codeReferences.Count); } 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)); + } + + 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.Inspection.cs b/src/Lua.Debugger/LuaDebugger.Inspection.cs index 25ab0cdb..4794d8a9 100644 --- a/src/Lua.Debugger/LuaDebugger.Inspection.cs +++ b/src/Lua.Debugger/LuaDebugger.Inspection.cs @@ -11,45 +11,69 @@ public Task GetStackTraceAsync( InspectAsync( stopId, (context, _) => - { - var frames = context.State.GetCallStackFrames(); - var result = new LuaDebugStackFrame[frames.Length]; - for (var i = frames.Length - 1; i >= 0; i--) - { - 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 - ); - } - return new ValueTask(result); - }, + new ValueTask( + ReadStackTrace(context, 0, int.MaxValue).Frames + ), cancellationToken ); + /// + /// Materializes the requested frames. Discovering executable source lines can also update + /// breakpoints. + /// + 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]; + var controlsChanged = false; + 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 prototype = closure.Proto; + lock (gate) + source = RegisterPrototype(prototype, ref controlsChanged).Source; + if ((uint)pc < (uint)prototype.LineInfo.Length) + line = prototype.LineInfo[pc]; + name = + prototype.LineDefined == 0 + ? "main chunk" + : $"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); + } + public Task GetVariablesAsync( int stopId, int frameId, diff --git a/src/Lua.Debugger/LuaDebugger.Sources.cs b/src/Lua.Debugger/LuaDebugger.Sources.cs index 59e1e5fd..e5b7d40c 100644 --- a/src/Lua.Debugger/LuaDebugger.Sources.cs +++ b/src/Lua.Debugger/LuaDebugger.Sources.cs @@ -50,7 +50,7 @@ public LuaBreakpoint[] SetBreakpoints(string source, IReadOnlyList lines) if (request.Resolved.Verified) entry.Points[request.Resolved.Line] = request.Resolved; } - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); return requests.Select(x => x.Resolved).ToArray(); } } @@ -60,7 +60,7 @@ public void ClearBreakpoints() lock (gate) { ClearBreakpointsCore(); - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); } } @@ -75,27 +75,31 @@ 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 controlsChanged = false; + var entry = RegisterPrototype(rootPrototype, ref controlsChanged); + var children = rootPrototype.ChildPrototypes; + if (!children.IsEmpty) { - var work = new Stack(); - var controlsChanged = false; - work.Push(closure.Proto); - while (work.Count != 0) + var work = new Stack(children.Length); + foreach (var child in children) + work.Push(child); + while (work.TryPop(out var prototype)) { - var prototype = work.Pop(); - controlsChanged |= RegisterPrototype(prototype); + RegisterPrototype(prototype, ref controlsChanged); 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(); - return entry.Source; } + if (content != null) + entry.Source = entry.Source with { Content = content }; + if (controlsChanged) + UpdateExecutionCodes(breakpointsChanged: true); + return entry; } public LuaDebugSource[] GetSources() @@ -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/src/Lua.Debugger/LuaDebugger.cs b/src/Lua.Debugger/LuaDebugger.cs index 7ecf5867..5cd3713b 100644 --- a/src/Lua.Debugger/LuaDebugger.cs +++ b/src/Lua.Debugger/LuaDebugger.cs @@ -16,15 +16,18 @@ 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; + int stoppedThreadId = -1; int nextBreakpointId; int nextStopId; int nextFrameId; + bool PauseRequested => pause != PauseRequest.None; + public LuaDebugger(LuaState state) : this(state, null) { } @@ -36,6 +39,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. @@ -49,7 +56,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; } } @@ -59,7 +68,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." ); @@ -86,9 +95,9 @@ void Release(bool detach) Interlocked.CompareExchange(ref root.GlobalState.Debugger, null, this); ClearBreakpointsCore(); step = null; - pauseRequested = false; + pause = PauseRequest.None; resumeRequested = true; - UpdateExecutionCodes(); + UpdateExecutionCodes(breakpointsChanged: true); signal.TrySetResult(true); inspections = paused?.Inspections; } @@ -102,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(); } } @@ -121,12 +135,16 @@ internal bool TryContinue(int stopId, LuaDebugStep mode) { if (paused is not { } context || context.Stop.Id != stopId || resumeRequested) return false; + var controlsEnabled = 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 (controlsEnabled != (PauseRequested || step != null)) + UpdateExecutionCodes(); signal.TrySetResult(true); inspections = context.Inspections; } @@ -137,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 ) { @@ -151,22 +169,22 @@ 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"; - pauseRequested = false; - step = null; + 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. var stop = new LuaDebugStop(++nextStopId, reason, entry.Source, line, breakpoint?.Id); paused = context = new PauseContext( state, @@ -177,7 +195,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) @@ -189,16 +210,35 @@ ValueTask SuspendSynchronously( CancellationToken cancellationToken ) => new(SynchronousDebugPause.Run(token => SuspendAsync(context, token), cancellationToken)); - void ILuaDebugSession.ExecutionEnded() + void ILuaDebugSession.ExecutionQuiesced() { - if (step == null) + if (pause != PauseRequest.ActiveExecution && step == null) return; lock (gate) { - if (step is not { } request || request.State.CallStackFrameCount != 0) + 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; + changed = true; + } + if (changed) + UpdateExecutionCodes(); + } + } + + void ILuaDebugSession.ThreadDisposed(LuaState state) + { + lock (gate) + { + if (step?.State != state) return; step = null; - UpdateExecutionCodes(); + if (!PauseRequested) + UpdateExecutionCodes(); } } @@ -212,7 +252,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) @@ -220,7 +260,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(); @@ -253,7 +293,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; @@ -263,6 +307,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) @@ -283,6 +340,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, @@ -291,6 +354,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); @@ -328,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/Exceptions.cs b/src/Lua/Exceptions.cs index 3c7a465f..9459f0c4 100644 --- a/src/Lua/Exceptions.cs +++ b/src/Lua/Exceptions.cs @@ -199,13 +199,33 @@ public LuaRuntimeException(LuaState? state, LuaValue errorObject, int level = 1) state.ExceptionTrace.Clear(); state.CurrentException = this; State = state; + if (level == 1) + CaptureTailCallSite(state); } ErrorObject = errorObject; 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 + // caller's values stay collectable while the native callee is suspended. + Prototype? tailCallSitePrototype; + int tailCallSitePc; + + void CaptureTailCallSite(LuaState state) + { + var caller = state.GetNativeTailCaller(); + if ( + caller.Prototype is not { } prototype + || (uint)caller.Pc >= (uint)prototype.LineInfo.Length + ) + return; + tailCallSitePrototype = prototype; + tailCallSitePc = caller.Pc; + } Traceback? luaTraceback; @@ -227,6 +247,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 +266,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 +301,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); @@ -342,27 +386,74 @@ 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 { 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) @@ -377,10 +468,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 +486,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 +502,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) @@ -445,15 +537,18 @@ double value } } - static 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: "); - traceback.WriteLastLuaTrace(ref pooledList, level); - 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 @@ -509,53 +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); - 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); - 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 @@ -567,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/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/Internal/LuaDebug.cs b/src/Lua/Internal/LuaDebug.cs index 976e6284..d8bfae51 100644 --- a/src/Lua/Internal/LuaDebug.cs +++ b/src/Lua/Internal/LuaDebug.cs @@ -126,10 +126,11 @@ public ReadOnlySpan ShortSource public static LuaDebug Create( LuaGlobalState globalState, - CallStackFrame? prevFrame, - CallStackFrame? frame, - LuaFunction function, - int pc, + LuaFunction? function, + Prototype? prototype, + int? pc, + bool isTailCall, + (Prototype? Prototype, int Pc) caller, ReadOnlySpan what, out bool isValid ) @@ -139,7 +140,7 @@ out bool isValid buffer = new(globalState); } - isValid = buffer.GetInfo(prevFrame, frame, function, pc, what); + isValid = buffer.GetInfo(function, prototype, pc, isTailCall, caller, what); return new(buffer, buffer.version); } @@ -212,14 +213,14 @@ internal void Return(uint version) } internal bool GetInfo( - CallStackFrame? prevFrame, - CallStackFrame? frame, - LuaFunction function, - int pc, + LuaFunction? function, + Prototype? prototype, + int? pc, + bool isTailCall, + (Prototype? Prototype, int Pc) caller, ReadOnlySpan what ) { - var closure = function as LuaClosure; var status = 1; foreach (var c in what) { @@ -227,16 +228,16 @@ ReadOnlySpan what { case 'S': { - GetFuncInfo(function); + GetFuncInfo(prototype); break; } case 'l': { CurrentLine = - frame.HasValue && closure is not null + prototype is not null && pc is { } instructionIndex ? ( - (uint)pc < (uint)closure.Proto.LineInfo.Length - ? closure.Proto.LineInfo[pc] + (uint)instructionIndex < (uint)prototype.LineInfo.Length + ? prototype.LineInfo[instructionIndex] : 0 ) : -1; @@ -245,54 +246,41 @@ ReadOnlySpan what case 'u': { UpValueCount = - closure is not null ? closure.UpValues.Length + prototype is not null ? prototype.UpValues.Length : function is CSharpClosure csClosure ? csClosure.UpValues.Length : 0; - if (closure is null) + if (prototype is null) { IsVarArg = true; ParameterCount = 0; } else { - IsVarArg = closure.Proto.HasVariableArguments; - ParameterCount = closure.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 ?? 0, - 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; + Name = function?.Name; } break; @@ -309,9 +297,9 @@ 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; @@ -320,13 +308,11 @@ void GetFuncInfo(LuaFunction f) } else { - var p = cl.Proto; - Source = p.ChunkName; - LineDefined = p.LineDefined; - LastLineDefined = p.LastLineDefined; + Source = prototype.ChunkName; + LineDefined = prototype.LineDefined; + LastLineDefined = prototype.LastLineDefined; What = LineDefined == 0 ? "main" : "Lua"; } - ShortSourceLength = WriteShortSource(Source, ShortSource); } } diff --git a/src/Lua/Internal/LuaValueDictionary.cs b/src/Lua/Internal/LuaValueDictionary.cs index 7c6db950..ca1f138b 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; @@ -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] { @@ -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); @@ -328,12 +335,6 @@ void Delete(LuaValue key) if (Unsafe.IsNullRef(ref value) || value.Type == LuaValueType.Nil) return; - if (_nilCount + 1 == Count) - { - Clear(); - return; - } - ref var entry = ref _entries![index]; if (entry.keyObject is { } keyObject) { @@ -428,25 +429,35 @@ 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) + { + // 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; + } + + if (++index < _count) { - 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; } + + // 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; - return false; + return LuaNextStatus.End; } int FindIterationIndex(LuaValue key) @@ -539,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/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/LuaFunctionExecutionContext.cs b/src/Lua/LuaFunctionExecutionContext.cs index 1eacf58e..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) @@ -230,12 +245,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/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/LuaState.cs b/src/Lua/LuaState.cs index 13c525f4..a8f4b0a5 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, 0, FrameVersions.Length); + } } static LinkedPool pool; @@ -225,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; @@ -299,10 +319,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 +424,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 +448,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)] @@ -675,17 +734,19 @@ 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 global quiescence. + // 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) - Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); + if (!entered) + return; + if (Interlocked.Decrement(ref GlobalState.ActiveExecutions) == 0) + Volatile.Read(ref GlobalState.Debugger)?.ExecutionQuiesced(); } [AsyncMethodBuilder(typeof(LightAsyncValueTaskMethodBuilder<>))] @@ -944,7 +1005,7 @@ public void Dispose() CoreData.Release(); CoreData = null!; - if (GlobalState.ActiveExecutions == 0) - Volatile.Read(ref GlobalState.Debugger)?.ExecutionEnded(); + if (Volatile.Read(ref GlobalState.ActiveExecutions) == 0) + Volatile.Read(ref GlobalState.Debugger)?.ThreadDisposed(this); } } diff --git a/src/Lua/LuaTable.cs b/src/Lua/LuaTable.cs index dd6212ee..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 { @@ -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,29 +380,22 @@ 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; } } - foreach (var kv in dictionary) - { - if (kv.Value.Type is not LuaValueType.Nil) - { - pair = kv; - return true; - } - } - } - else - { - if (dictionary.TryGetNext(key, out pair)) + var hash = dictionary.GetEnumerator(); + if (hash.MoveNext()) { - return true; + pair = hash.Current; + return LuaNextStatus.Found; } + + pair = default; + return LuaNextStatus.End; } - pair = default; - return false; + return dictionary.TryGetNext(key, out pair); } public void Clear() @@ -459,7 +459,7 @@ void EnsureArrayCapacity(int newCapacity) hasWritableArrayReferences = false; } - using PooledList<(int, LuaValue)> 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/CallStackFrame.cs b/src/Lua/Runtime/CallStackFrame.cs index a7ae3ed7..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,7 +11,6 @@ public record struct CallStackFrame public required int VariableArgumentCount; public int CallerInstructionIndex; internal CallStackFrameFlags Flags; - public int Version; internal bool IsTailCall => (Flags & CallStackFrameFlags.TailCall) == CallStackFrameFlags.TailCall; 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..e800980d 100644 --- a/src/Lua/Runtime/LuaDebugCode.cs +++ b/src/Lua/Runtime/LuaDebugCode.cs @@ -4,25 +4,35 @@ 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 ); } +/// +/// 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 +40,41 @@ 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]; + } + + 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 +87,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 +113,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..27f6389a 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.Debug.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.Debug.cs @@ -5,22 +5,19 @@ namespace Lua.Runtime; public static partial class LuaVirtualMachine { + // 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.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 @@ -28,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 22e3a384..60d50478 100644 --- a/src/Lua/Runtime/LuaVirtualMachine.cs +++ b/src/Lua/Runtime/LuaVirtualMachine.cs @@ -64,6 +64,10 @@ void Init(LuaState state, in CallStackFrame frame, CancellationToken cancellatio public int LastHookPc; public int LastDebugPc = -1; public LuaDebugCode? DebugCode; + public LuaDebugCode? PreviousDebugCode; + + (Prototype? Prototype, int Pc, bool IsTailCall) previousNativeTailCaller; + bool hasNativeTailCaller; public bool IsTopLevel => BaseCallStackCount == State.CallStackFrameCount; @@ -218,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, @@ -289,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(); @@ -363,10 +373,17 @@ or PostOperationType.DontPop } finally { + if (hasNativeTailCaller) + { + State.LastNativeTailCaller = previousNativeTailCaller; + previousNativeTailCaller = default; + hasNativeTailCaller = false; + } Stack = null!; State = null!; LuaClosure = null!; DebugCode = null; + PreviousDebugCode = null; Task = default; CancellationToken = default; pool.TryPush(this); @@ -433,12 +450,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(prototype); + } + } + } ref var instructionsHead = ref context.DebugCode != null ? ref context.DebugCode.Code[0] : ref Unsafe.AsRef(in context.Prototype.Code[0]); @@ -469,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)) { @@ -1072,6 +1123,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,31 +1206,16 @@ static void VarArg(VirtualMachineExecutionContext context) continue; default: - if (opCode is DebuggerResumeOpCode or DebuggerBreakOpCode) + 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 (instruction.OpCode is DebuggerBreakOpCode or DebuggerResumeOpCode) + 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; } @@ -1781,6 +1818,10 @@ 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) + context.RecordNativeTailCaller(lastFrame.IsTailCall); + if (state.CallOrReturnHookMask.Value != 0 && !context.State.IsInHook) { context.PostOperation = PostOperationType.TailCall; @@ -1789,7 +1830,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 4a6bdfcd..25db7bc8 100644 --- a/src/Lua/Runtime/Tracebacks.cs +++ b/src/Lua/Runtime/Tracebacks.cs @@ -24,37 +24,36 @@ internal static void WriteLastLuaTrace( return; } - var intFormatBuffer = (stackalloc char[15]); - var shortSourceBuffer = (stackalloc char[59]); for (var index = stackFrames.Length - level; index >= 1; index--) { - var lastFunc = stackFrames[index - 1].Function; var frame = stackFrames[index]; - if (!frame.IsTailCall && lastFunc 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) 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]); - - list.AddRange(": "); + WritePosition(ref list, p, frame.CallerInstructionIndex); return; } } } - internal void WriteLastLuaTrace(ref PooledList list, int level = 1) + internal static void WritePosition(ref PooledList list, Prototype prototype, int pc) { - WriteLastLuaTrace(StackFrames, ref list, level); + 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(": "); } public int LastLine diff --git a/src/Lua/Standard/BasicLibrary.cs b/src/Lua/Standard/BasicLibrary.cs index ece3fa77..5e25674c 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)) + switch (arg0.TryGetNextStatus(arg1, out var kv)) { - return new(context.Return(kv.Key, kv.Value)); - } - else - { - return new(context.Return(LuaValue.Nil)); + 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)); } } @@ -614,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(); diff --git a/src/Lua/Standard/DebugLibrary.cs b/src/Lua/Standard/DebugLibrary.cs index e1b92c06..c17bfab1 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; @@ -169,6 +169,16 @@ static LuaValue GetParam(LuaFunction function, int index) var level = context.GetIntegerArgument(argOffset); + var discarded = state.GetNativeTailCaller().Prototype != null; + 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 +204,16 @@ CancellationToken cancellationToken var index = context.GetIntegerArgument(argOffset + 1); var level = context.GetIntegerArgument(argOffset); + var discarded = state.GetNativeTailCaller().Prototype != null; + 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"); @@ -556,46 +576,75 @@ 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); - if (!arg1.TryReadFunction(out var functionToInspect)) + arg1.TryReadFunction(out var functionToInspect); + Prototype? prototype = null; + int? pc = null; + var isTailCall = false; + (Prototype? Prototype, int Pc) caller = default; + + 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(); - - if (level <= 0 || level > callStack.Length) + var tailCaller = state.GetNativeTailCaller(); + var discarded = tailCaller.Prototype != null; + if ((uint)level >= (uint)(callStack.Length + (discarded ? 1 : 0))) { return new(context.Return(LuaValue.Nil)); } - currentFrame = callStack[^level]; - previousFrame = level + 1 <= callStack.Length ? callStack[^(level + 1)] : null; - if (level != 1) + // 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) { - pc = callStack[^(level - 1)].CallerInstructionIndex; + prototype = tailCaller.Prototype; + pc = tailCaller.Pc; + isTailCall = tailCaller.IsTailCall; + } + else + { + 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 + ); + } } + prototype ??= (functionToInspect as LuaClosure)?.Proto; using var debug = LuaDebug.Create( context.GlobalState, - previousFrame, - currentFrame, functionToInspect, + prototype, pc, + isTailCall, + caller, what, out var isValid ); if (!isValid) { - context.ThrowBadArgument(argOffset + 1, "invalid option"); + context.ThrowBadArgument(argOffset + 2, "invalid option"); } LuaTable table = new(0, 1); @@ -634,21 +683,18 @@ 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')) + 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/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.Debugger.Dap.Tests/DapTests.cs b/tests/Lua.Debugger.Dap.Tests/DapTests.cs index 28efb32c..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", @@ -325,6 +322,103 @@ 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 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)); + + 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", + 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() { diff --git a/tests/Lua.Debugger.Tests/DebuggerTests.cs b/tests/Lua.Debugger.Tests/DebuggerTests.cs index f328caf4..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")] @@ -41,6 +79,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) @@ -268,8 +320,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; @@ -288,9 +341,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 })); @@ -702,4 +757,104 @@ 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")); + } + + [TestCase(false)] + [TestCase(true)] + public async Task IdlePauseSurvivesLifecycleNotifications(bool lateQuiescence) + { + 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(); + if (lateQuiescence) + ((ILuaDebugSession)debugger).ExecutionQuiesced(); + await state.DoStringAsync("return 42"); + + Assert.That(stops, Is.EqualTo(new[] { "pause" })); + } + + [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); + } } diff --git a/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs new file mode 100644 index 00000000..669bc5d4 --- /dev/null +++ b/tests/Lua.Debugger.Tests/DebuggerTrapTests.cs @@ -0,0 +1,46 @@ +using Lua.Debugging; +using Lua.Runtime; +using NUnit.Framework; + +namespace Lua.Debugger.Tests; + +/// +/// The debugger checkpoint reserves the highest encoding in the six-bit opcode field. +/// +public class DebuggerTrapTests +{ + [Test] + public void TrapEncodingStaysOutsideThePublishedInstructionSet() + { + var highest = Enum.GetValues().Select(static op => (byte)op).Max(); + Assert.That(highest, Is.LessThan((byte)LuaVirtualMachine.DebuggerBreakOpCode)); + 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)); + } + + [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.Proto); + 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/ArgumentErrorFormatTests.cs b/tests/Lua.Tests/ArgumentErrorFormatTests.cs new file mode 100644 index 00000000..2ffca122 --- /dev/null +++ b/tests/Lua.Tests/ArgumentErrorFormatTests.cs @@ -0,0 +1,126 @@ +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)")); + } + + /// + /// 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")); + } + + [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")); + } +} diff --git a/tests/Lua.Tests/DebugDiscardedCallerTests.cs b/tests/Lua.Tests/DebugDiscardedCallerTests.cs new file mode 100644 index 00000000..c0a3a2d0 --- /dev/null +++ b/tests/Lua.Tests/DebugDiscardedCallerTests.cs @@ -0,0 +1,99 @@ +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; + } + + [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) + 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, + Is.EqualTo( + new LuaValue[] + { + "Lua", + 1, + 2, + 1, + false, + tailCall, + tailCall ? LuaValue.Nil : "f", + true, + LuaValue.Nil, + } + ) + ); + } + + [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)); + } +} diff --git a/tests/Lua.Tests/NextIterationTests.cs b/tests/Lua.Tests/NextIterationTests.cs new file mode 100644 index 00000000..6d60d608 --- /dev/null +++ b/tests/Lua.Tests/NextIterationTests.cs @@ -0,0 +1,121 @@ +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 = {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, Is.EqualTo(new LuaValue[] { true, true, true, true, 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)); + } +} 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)] diff --git a/tests/Lua.Tests/TailCallErrorPositionTests.cs b/tests/Lua.Tests/TailCallErrorPositionTests.cs new file mode 100644 index 00000000..3abcbcb8 --- /dev/null +++ b/tests/Lua.Tests/TailCallErrorPositionTests.cs @@ -0,0 +1,117 @@ +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 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'")); + } + + [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() + { + 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'")); + } +} 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'") + ); } }