Skip to content

Commit 70abc14

Browse files
committed
Pass LuaDebug by reference
1 parent b728ee4 commit 70abc14

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

src/Laylua.Tests/Tests/Moon/LuaStateTests.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void CancellationTokenLuaHook_CancelsExecutionWithError()
2525
// Act & Assert
2626
Assert.That(() => Lua.Execute("while true do end"), Throws.TypeOf<LuaException>()
2727
.With.InnerException.TypeOf<OperationCanceledException>()
28-
.And.InnerException.Property(nameof(OperationCanceledException.CancellationToken)).EqualTo(cts.Token));
28+
.And.InnerException.Property(nameof(OperationCanceledException.CancellationToken)).EqualTo(cts.Token));
2929
}
3030

3131
[Test]
@@ -37,7 +37,7 @@ public void MaxInstructionCountLuaHook_CancelsExecutionWithError()
3737
// Act & Assert
3838
Assert.That(() => Lua.Execute("while true do end"), Throws.TypeOf<LuaException>()
3939
.With.InnerException.TypeOf<MaxInstructionCountReachedException>()
40-
.And.InnerException.Property(nameof(MaxInstructionCountReachedException.InstructionCount)).EqualTo(100));
40+
.And.InnerException.Property(nameof(MaxInstructionCountReachedException.InstructionCount)).EqualTo(100));
4141
}
4242

4343
private sealed class InstructionCountLuaHook(int instructionCount) : LuaHook
@@ -48,7 +48,7 @@ private sealed class InstructionCountLuaHook(int instructionCount) : LuaHook
4848

4949
public int TimesCalled { get; private set; }
5050

51-
protected override void Execute(LuaThread lua, LuaDebug debug)
51+
protected override void Execute(LuaThread lua, ref LuaDebug debug)
5252
{
5353
TimesCalled++;
5454
}
@@ -75,6 +75,42 @@ public void CombinedCountLuaHooks_CallsBothAsExpected()
7575
Assert.That(fiveInstructionsHook.TimesCalled, Is.EqualTo(2));
7676
}
7777

78+
private sealed class FunctionNameGrabberLuaHook : LuaHook
79+
{
80+
protected override LuaEventMask EventMask => LuaEventMask.Call;
81+
82+
protected override int InstructionCount => 0;
83+
84+
public List<string> FunctionNames { get; } = [];
85+
86+
protected override void Execute(LuaThread lua, ref LuaDebug debug)
87+
{
88+
var functionName = debug.FunctionName.ToString();
89+
if (!string.IsNullOrWhiteSpace(functionName))
90+
{
91+
FunctionNames.Add(functionName);
92+
}
93+
}
94+
}
95+
96+
[Test]
97+
public void FunctionNameGrabberLuaHook_GrabsFunctionNames()
98+
{
99+
// Arrange
100+
var hook = new FunctionNameGrabberLuaHook();
101+
Lua.State.Hook = hook;
102+
103+
Lua.SetGlobal("func1", static () => { });
104+
Lua.SetGlobal("func2", static () => { });
105+
Lua.Execute("function func3() end");
106+
107+
// Act
108+
Lua.Execute("func1() func2() func3()");
109+
110+
// Assert
111+
Assert.That(hook.FunctionNames, Is.EqualTo(new[] { "func1", "func2", "func3" }));
112+
}
113+
78114
[Test]
79115
public void GC_IsRunning_ReturnsTrue()
80116
{

src/Laylua/Moon/Hook/Default/CancellationTokenLuaHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public CancellationTokenLuaHook(CancellationToken cancellationToken)
2727
}
2828

2929
/// <inheritdoc/>
30-
protected internal override void Execute(LuaThread lua, LuaDebug debug)
30+
protected internal override void Execute(LuaThread lua, ref LuaDebug debug)
3131
{
3232
if (!_cancellationToken.IsCancellationRequested)
3333
{

src/Laylua/Moon/Hook/Default/CombinedLuaHook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public CombinedLuaHook(params Span<LuaHook> hooks)
4646
}
4747

4848
/// <inheritdoc/>
49-
protected internal override void Execute(LuaThread lua, LuaDebug debug)
49+
protected internal override void Execute(LuaThread lua, ref LuaDebug debug)
5050
{
5151
for (var hookIndex = 0; hookIndex < _hooks.Length; hookIndex++)
5252
{
@@ -65,7 +65,7 @@ protected internal override void Execute(LuaThread lua, LuaDebug debug)
6565
_instructionCounts[hookIndex] = 0;
6666
}
6767

68-
hook.Execute(lua, debug);
68+
hook.Execute(lua, ref debug);
6969
}
7070
}
7171

src/Laylua/Moon/Hook/Default/MaxInstructionCountLuaHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public MaxInstructionCountLuaHook(int maxInstructionCount)
2727
}
2828

2929
/// <inheritdoc/>
30-
protected internal override void Execute(LuaThread lua, LuaDebug debug)
30+
protected internal override void Execute(LuaThread lua, ref LuaDebug debug)
3131
{
3232
lua_getinfo(lua.State.L, "Sn", debug.ActivationRecord);
3333

src/Laylua/Moon/Hook/LuaHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public abstract class LuaHook
2828
/// </remarks>
2929
/// <param name="lua"> The Lua state. </param>
3030
/// <param name="debug"> The debug information. </param>
31-
protected internal abstract void Execute(LuaThread lua, LuaDebug debug);
31+
protected internal abstract void Execute(LuaThread lua, ref LuaDebug debug);
3232

3333
/// <summary>
3434
/// Combines multiple hooks into one.

src/Laylua/Moon/LuaState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ internal LuaHookFunction WrapHookFunction(LuaHook hook)
191191
using (var thread = LuaThread.FromExtraSpace(state))
192192
{
193193
var debug = new LuaDebug(thread, ar);
194-
hook.Execute(thread, debug);
194+
hook.Execute(thread, ref debug);
195195
}
196196
};
197197

0 commit comments

Comments
 (0)