Browse Source

Fix: correct async return statement in LuaObjectGenerator generated code

Akeit0 7 months ago
parent
commit
930b5d90dc

+ 1 - 1
src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

@@ -451,7 +451,7 @@ partial class LuaObjectGenerator
                 }
                 }
                 else
                 else
                 {
                 {
-                    builder.AppendLine(methodMetadata.IsAsync ? "context.Return(new global::Lua.LuaValue(result))));" : "new global::System.Threading.Tasks.ValueTask<int>(context.Return(new global::Lua.LuaValue(result)));");
+                    builder.AppendLine(methodMetadata.IsAsync ? "context.Return(new global::Lua.LuaValue(result));" : "new global::System.Threading.Tasks.ValueTask<int>(context.Return(new global::Lua.LuaValue(result)));");
                 }
                 }
             }
             }
             else
             else

+ 21 - 1
tests/Lua.Tests/LuaObjectTests.cs

@@ -6,7 +6,7 @@ namespace Lua.Tests;
 public partial class TestUserData
 public partial class TestUserData
 {
 {
     [LuaMember]
     [LuaMember]
-    public double Property { get; set; }
+    public int Property { get; set; }
 
 
     [LuaMember("p2")]
     [LuaMember("p2")]
     public string PropertyWithName { get; set; } = "";
     public string PropertyWithName { get; set; } = "";
@@ -36,6 +36,13 @@ public partial class TestUserData
         return Property;
         return Property;
     }
     }
 
 
+    [LuaMember]
+    public async Task<double> InstanceMethodWithReturnValueAsync()
+    {
+        await Task.Delay(1);
+        return Property;
+    }
+
     [LuaMetamethod(LuaObjectMetamethod.Call)]
     [LuaMetamethod(LuaObjectMetamethod.Call)]
     public string Call()
     public string Call()
     {
     {
@@ -120,6 +127,19 @@ public class LuaObjectTests
         Assert.That(results, Has.Length.EqualTo(1));
         Assert.That(results, Has.Length.EqualTo(1));
         Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
         Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
     }
     }
+    
+    [Test]
+    public async Task Test_InstanceMethodWithReturnValueAsync()
+    {
+        var userData = new TestUserData() { Property = 1 };
+
+        var state = LuaState.Create();
+        state.Environment["test"] = userData;
+        var results = await state.DoStringAsync("return test:InstanceMethodWithReturnValueAsync()");
+
+        Assert.That(results, Has.Length.EqualTo(1));
+        Assert.That(results[0], Is.EqualTo(new LuaValue(1)));
+    }
 
 
     [Test]
     [Test]
     public async Task Test_CallMetamethod()
     public async Task Test_CallMetamethod()