Skip to content

Commit 68c7f5f

Browse files
authored
Merge pull request #45 from AnnulusGames/fix-metamethod-invalid-result
Fix: Incorrect values ​​may be returned if metamethod return value does not exist
2 parents bfd61e5 + d17ed67 commit 68c7f5f

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

src/Lua/Runtime/LuaVirtualMachine.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ internal async static ValueTask<int> ExecuteClosureAsync(LuaState state, CallSta
145145
stack.Push(vb);
146146
stack.Push(vc);
147147

148-
await func.InvokeAsync(new()
148+
var resultCount = await func.InvokeAsync(new()
149149
{
150150
State = state,
151151
Thread = thread,
@@ -156,7 +156,7 @@ await func.InvokeAsync(new()
156156
RootChunkName = rootChunk.Name,
157157
}, resultBuffer.AsMemory(), cancellationToken);
158158

159-
stack.UnsafeGet(RA) = resultBuffer[0];
159+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
160160
}
161161
else
162162
{
@@ -187,7 +187,7 @@ await func.InvokeAsync(new()
187187
stack.Push(vb);
188188
stack.Push(vc);
189189

190-
await func.InvokeAsync(new()
190+
var resultCount = await func.InvokeAsync(new()
191191
{
192192
State = state,
193193
Thread = thread,
@@ -198,7 +198,7 @@ await func.InvokeAsync(new()
198198
RootChunkName = rootChunk.Name,
199199
}, resultBuffer.AsMemory(), cancellationToken);
200200

201-
stack.UnsafeGet(RA) = resultBuffer[0];
201+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
202202
}
203203
else
204204
{
@@ -229,7 +229,7 @@ await func.InvokeAsync(new()
229229
stack.Push(vb);
230230
stack.Push(vc);
231231

232-
await func.InvokeAsync(new()
232+
var resultCount = await func.InvokeAsync(new()
233233
{
234234
State = state,
235235
Thread = thread,
@@ -240,7 +240,7 @@ await func.InvokeAsync(new()
240240
RootChunkName = rootChunk.Name,
241241
}, resultBuffer.AsMemory(), cancellationToken);
242242

243-
stack.UnsafeGet(RA) = resultBuffer[0];
243+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
244244
}
245245
else
246246
{
@@ -271,7 +271,7 @@ await func.InvokeAsync(new()
271271
stack.Push(vb);
272272
stack.Push(vc);
273273

274-
await func.InvokeAsync(new()
274+
var resultCount = await func.InvokeAsync(new()
275275
{
276276
State = state,
277277
Thread = thread,
@@ -282,7 +282,7 @@ await func.InvokeAsync(new()
282282
RootChunkName = rootChunk.Name,
283283
}, resultBuffer.AsMemory(), cancellationToken);
284284

285-
stack.UnsafeGet(RA) = resultBuffer[0];
285+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
286286
}
287287
else
288288
{
@@ -318,7 +318,7 @@ await func.InvokeAsync(new()
318318
stack.Push(vb);
319319
stack.Push(vc);
320320

321-
await func.InvokeAsync(new()
321+
var resultCount = await func.InvokeAsync(new()
322322
{
323323
State = state,
324324
Thread = thread,
@@ -329,7 +329,7 @@ await func.InvokeAsync(new()
329329
RootChunkName = rootChunk.Name,
330330
}, resultBuffer.AsMemory(), cancellationToken);
331331

332-
stack.UnsafeGet(RA) = resultBuffer[0];
332+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
333333
}
334334
else
335335
{
@@ -360,7 +360,7 @@ await func.InvokeAsync(new()
360360
stack.Push(vb);
361361
stack.Push(vc);
362362

363-
await func.InvokeAsync(new()
363+
var resultCount = await func.InvokeAsync(new()
364364
{
365365
State = state,
366366
Thread = thread,
@@ -371,7 +371,7 @@ await func.InvokeAsync(new()
371371
RootChunkName = rootChunk.Name,
372372
}, resultBuffer.AsMemory(), cancellationToken);
373373

374-
stack.UnsafeGet(RA) = resultBuffer[0];
374+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
375375
}
376376
else
377377
{
@@ -400,7 +400,7 @@ await func.InvokeAsync(new()
400400

401401
stack.Push(vb);
402402

403-
await func.InvokeAsync(new()
403+
var resultCount = await func.InvokeAsync(new()
404404
{
405405
State = state,
406406
Thread = thread,
@@ -411,7 +411,7 @@ await func.InvokeAsync(new()
411411
RootChunkName = rootChunk.Name,
412412
}, resultBuffer.AsMemory(), cancellationToken);
413413

414-
stack.UnsafeGet(RA) = resultBuffer[0];
414+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
415415
}
416416
else
417417
{
@@ -447,7 +447,7 @@ await func.InvokeAsync(new()
447447

448448
stack.Push(vb);
449449

450-
await func.InvokeAsync(new()
450+
var resultCount = await func.InvokeAsync(new()
451451
{
452452
State = state,
453453
Thread = thread,
@@ -458,7 +458,7 @@ await func.InvokeAsync(new()
458458
RootChunkName = rootChunk.Name,
459459
}, resultBuffer.AsMemory(), cancellationToken);
460460

461-
stack.UnsafeGet(RA) = resultBuffer[0];
461+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
462462
}
463463
else if (vb.TryRead<LuaTable>(out var table))
464464
{
@@ -507,7 +507,7 @@ await func.InvokeAsync(new()
507507
stack.Push(vb);
508508
stack.Push(vc);
509509

510-
await func.InvokeAsync(new()
510+
var resultCount = await func.InvokeAsync(new()
511511
{
512512
State = state,
513513
Thread = thread,
@@ -518,7 +518,7 @@ await func.InvokeAsync(new()
518518
RootChunkName = rootChunk.Name,
519519
}, resultBuffer.AsMemory(), cancellationToken);
520520

521-
stack.UnsafeGet(RA) = resultBuffer[0];
521+
stack.UnsafeGet(RA) = resultCount == 0 ? LuaValue.Nil : resultBuffer[0];
522522
}
523523
else
524524
{
@@ -551,7 +551,7 @@ await func.InvokeAsync(new()
551551
stack.Push(vb);
552552
stack.Push(vc);
553553

554-
await func.InvokeAsync(new()
554+
var resultCount = await func.InvokeAsync(new()
555555
{
556556
State = state,
557557
Thread = thread,
@@ -562,7 +562,7 @@ await func.InvokeAsync(new()
562562
RootChunkName = rootChunk.Name,
563563
}, resultBuffer.AsMemory(), cancellationToken);
564564

565-
compareResult = resultBuffer[0].ToBoolean();
565+
compareResult = resultCount != 0 && resultBuffer[0].ToBoolean();
566566
}
567567

568568
if (compareResult != (instruction.A == 1))
@@ -595,7 +595,7 @@ await func.InvokeAsync(new()
595595
stack.Push(vb);
596596
stack.Push(vc);
597597

598-
await func.InvokeAsync(new()
598+
var resultCount = await func.InvokeAsync(new()
599599
{
600600
State = state,
601601
Thread = thread,
@@ -606,7 +606,7 @@ await func.InvokeAsync(new()
606606
RootChunkName = rootChunk.Name,
607607
}, resultBuffer.AsMemory(), cancellationToken);
608608

609-
compareResult = resultBuffer[0].ToBoolean();
609+
compareResult = resultCount != 0 && resultBuffer[0].ToBoolean();
610610
}
611611
else
612612
{
@@ -643,7 +643,7 @@ await func.InvokeAsync(new()
643643
stack.Push(vb);
644644
stack.Push(vc);
645645

646-
await func.InvokeAsync(new()
646+
var resultCount = await func.InvokeAsync(new()
647647
{
648648
State = state,
649649
Thread = thread,
@@ -654,7 +654,7 @@ await func.InvokeAsync(new()
654654
RootChunkName = rootChunk.Name,
655655
}, resultBuffer.AsMemory(), cancellationToken);
656656

657-
compareResult = resultBuffer[0].ToBoolean();
657+
compareResult = resultCount != 0 && resultBuffer[0].ToBoolean();
658658
}
659659
else
660660
{

0 commit comments

Comments
 (0)