Quellcode durchsuchen

Merge pull request #241 from nuskey8/fix/meta-return

Fix return value is cleared
Akito Inoue vor 2 Wochen
Ursprung
Commit
c611c729df
2 geänderte Dateien mit 71 neuen und 2 gelöschten Zeilen
  1. 6 2
      src/Lua/Runtime/LuaVirtualMachine.cs
  2. 65 0
      tests/Lua.Tests/LuaObjectTests.cs

+ 6 - 2
src/Lua/Runtime/LuaVirtualMachine.cs

@@ -1266,6 +1266,7 @@ public static partial class LuaVirtualMachine
             if (!task.IsCompleted)
             {
                 context.PostOperation = PostOperationType.Call;
+                context.CurrentReturnFrameBase = returnBase;
                 context.Task = task;
                 return false;
             }
@@ -2002,6 +2003,7 @@ public static partial class LuaVirtualMachine
             if (!task.IsCompleted)
             {
                 context.PostOperation = PostOperationType.SetResult;
+                context.CurrentReturnFrameBase = newFrame.ReturnBase;
                 context.Task = task;
                 return false;
             }
@@ -2010,7 +2012,7 @@ public static partial class LuaVirtualMachine
                 = task.GetAwaiter().GetResult() != 0
                     ? stack.FastGet(newFrame.ReturnBase)
                     : default;
-            stack.PopUntil(newBase - variableArgumentCount);
+            stack.PopUntil(newFrame.ReturnBase + 1);
             context.State.PopCallStackFrame();
             return true;
         }
@@ -2129,6 +2131,7 @@ public static partial class LuaVirtualMachine
             if (!task.IsCompleted)
             {
                 context.PostOperation = PostOperationType.SetResult;
+                context.CurrentReturnFrameBase = newFrame.ReturnBase;
                 context.Task = task;
                 return false;
             }
@@ -2137,7 +2140,7 @@ public static partial class LuaVirtualMachine
                 ? stack.Get(newFrame.ReturnBase)
                 : default;
             stack.Get(context.Instruction.A + context.FrameBase) = result;
-            stack.PopUntil(newBase - variableArgumentCount);
+            stack.PopUntil(newFrame.ReturnBase + 1);
             context.State.PopCallStackFrame();
             return true;
         }
@@ -2271,6 +2274,7 @@ public static partial class LuaVirtualMachine
             if (!task.IsCompleted)
             {
                 context.PostOperation = PostOperationType.Compare;
+                context.CurrentReturnFrameBase = newFrame.ReturnBase;
                 context.Task = task;
                 return false;
             }

+ 65 - 0
tests/Lua.Tests/LuaObjectTests.cs

@@ -1,7 +1,48 @@
 using Lua.Standard;
 
 namespace Lua.Tests;
+[LuaObject]
+public partial class LuaTestObj {
+    int x;
+    int y;
+
+    [LuaMember("x")]
+    public int X {
+        get => x;
+        set => x = value;
+    }
 
+    [LuaMember("y")]
+    public int Y {
+        get => y;
+        set => y = value;
+    }
+
+    [LuaMember("create")]
+    public static LuaTestObj Create(int x, int y) {
+        return new LuaTestObj() {
+            x = x,
+            y = y
+        };
+    }
+
+    [LuaMetamethod(LuaObjectMetamethod.Add)]
+    public static LuaTestObj Add(LuaTestObj a, LuaTestObj b) {
+        return new LuaTestObj() {
+            x = a.x + b.x,
+            y = a.y + b.y
+        };
+    }
+    
+    [LuaMetamethod(LuaObjectMetamethod.Sub)]
+    public static async Task<LuaTestObj> Sub(LuaTestObj a, LuaTestObj b) {
+        await Task.Delay(1);
+        return new LuaTestObj() {
+            x = a.x - b.x,
+            y = a.y - b.y
+        };
+    }
+}
 [LuaObject]
 public partial class TestUserData
 {
@@ -160,4 +201,28 @@ public class LuaObjectTests
         Assert.That(results, Has.Length.EqualTo(1));
         Assert.That(results[0], Is.EqualTo(new LuaValue("Called!")));
     }
+    
+    [Test]
+    public async Task Test_ArithMetamethod()
+    {
+        var userData = new LuaTestObj();
+
+        var state = LuaState.Create();
+        state.OpenBasicLibrary();
+        state.Environment["TestObj"]=userData;
+        var results = await state.DoStringAsync("""
+                                                local a = TestObj.create(1, 2)
+                                                local b = TestObj.create(3, 4)
+                                                return a + b, a - b
+                                                """);
+        Assert.That(results, Has.Length.EqualTo(2));
+        Assert.That(results[0].Read<object>(), Is.TypeOf<LuaTestObj>());
+        var objAdd = results[0].Read<LuaTestObj>();
+        Assert.That(objAdd.X, Is.EqualTo(4));
+        Assert.That(objAdd.Y, Is.EqualTo(6));
+        Assert.That(results[1].Read<object>(), Is.TypeOf<LuaTestObj>());
+        var objSub = results[1].Read<LuaTestObj>();
+        Assert.That(objSub.X, Is.EqualTo(-2));
+        Assert.That(objSub.Y, Is.EqualTo(-2));
+    }
 }