Skip to content

Commit 64ca266

Browse files
committed
Return thread from LuaReference.IsAlive
1 parent 8e75d74 commit 64ca266

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

src/Laylua.Tests/Tests/Marshaler/LuaMarshalerDelegateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,6 @@ private void ParamsObjectMethod(params object[] args)
235235

236236
private static bool IsReferenceAlive(LuaReference? reference)
237237
{
238-
return (bool) typeof(LuaReference).GetMethod("IsAlive", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, [reference])!;
238+
return (bool) typeof(LuaReference).GetMethod("IsAlive", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, [reference, null])!;
239239
}
240240
}

src/Laylua/Library/Entities/Reference/LuaReference.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ internal LuaThread Thread
2323
ThrowIfInvalid();
2424
return ThreadCore;
2525
}
26-
set
27-
{
28-
ThreadCore = value is Lua lua
29-
? lua.MainThread
30-
: value.CloneReference();
31-
}
26+
set => ThreadCore = value is Lua lua
27+
? lua.MainThread
28+
: value.CloneReference();
3229
}
3330

3431
internal int Reference
@@ -53,12 +50,12 @@ private protected LuaReference()
5350

5451
~LuaReference()
5552
{
56-
if (!IsAlive(this))
53+
if (!IsAlive(this, out var thread))
5754
return;
5855

5956
if (!LuaRegistry.IsPersistentReference(_reference))
6057
{
61-
Lua.FromThread(Thread).PushLeakedReference(_reference);
58+
Lua.FromThread(thread).PushLeakedReference(_reference);
6259
}
6360

6461
if (Thread!.Marshaler.ReturnReference(this))
@@ -137,19 +134,19 @@ public bool Equals(LuaReference? other)
137134
if (other == null)
138135
return false;
139136

140-
if (!IsAlive(this) || !IsAlive(other))
137+
if (!IsAlive(this, out var thread) || !IsAlive(other, out var otherThread))
141138
return false;
142139

143140
if (_reference == other._reference)
144141
return true;
145142

146-
Thread!.Stack.EnsureFreeCapacity(2);
143+
thread.Stack.EnsureFreeCapacity(2);
147144

148-
using (Thread.Stack.SnapshotCount())
145+
using (thread.Stack.SnapshotCount())
149146
{
150-
Thread.Stack.Push(this);
151-
Thread.Stack.Push(other);
152-
var L = Thread.State.L;
147+
thread.Stack.Push(this);
148+
thread.Stack.Push(other);
149+
var L = thread.State.L;
153150
if (lua_rawequal(L, -2, -1))
154151
return true;
155152
}
@@ -178,15 +175,15 @@ public sealed override int GetHashCode()
178175
/// </returns>
179176
public override string ToString()
180177
{
181-
if (!IsAlive(this))
178+
if (!IsAlive(this, out var thread))
182179
return $"<disposed {GetType().ToTypeString()}>";
183180

184-
Thread.Stack.EnsureFreeCapacity(2);
181+
thread.Stack.EnsureFreeCapacity(2);
185182

186-
using (Thread.Stack.SnapshotCount())
183+
using (thread.Stack.SnapshotCount())
187184
{
188-
Thread.Stack.Push(this);
189-
var L = Thread.State.L;
185+
thread.Stack.Push(this);
186+
var L = thread.State.L;
190187
return luaL_tostring(L, -1).ToString() ?? "<invalid>";
191188
}
192189
}
@@ -200,10 +197,10 @@ public virtual void Dispose()
200197
if (LuaRegistry.IsPersistentReference(_reference))
201198
return;
202199

203-
if (!IsAlive(this))
200+
if (!IsAlive(this, out var thread))
204201
return;
205202

206-
var L = Thread!.State.L;
203+
var L = thread.State.L;
207204
if (L == null)
208205
return;
209206

@@ -215,7 +212,7 @@ public virtual void Dispose()
215212

216213
if (this is not LuaThread)
217214
{
218-
Thread.Dispose();
215+
thread.Dispose();
219216
}
220217

221218
IsDisposed = true;
@@ -252,9 +249,9 @@ internal static void ValidateOwnership(LuaThread thread, LuaReference reference)
252249
}
253250
}
254251

255-
internal static bool IsAlive(LuaReference reference)
252+
internal static bool IsAlive(LuaReference reference, [MaybeNullWhen(false)] out LuaThread thread)
256253
{
257-
var lua = reference.ThreadCore;
258-
return lua != null && !lua.IsDisposed && !reference.IsDisposed && !lua.MainThread.IsDisposed;
254+
thread = reference.ThreadCore;
255+
return thread != null && !thread.IsDisposed && !reference.IsDisposed && !thread.MainThread.IsDisposed;
259256
}
260257
}

0 commit comments

Comments
 (0)