Просмотр исходного кода

add: add support for init/set/get only properties for SourceGenerator

akeit0 2 недель назад
Родитель
Сommit
46460ba9bd

+ 6 - 0
src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

@@ -283,6 +283,12 @@ partial class LuaObjectGenerator
             {
                 foreach (var propertyMetadata in typeMetadata.Properties)
                 {
+                    if (propertyMetadata.IsWriteOnly)
+                    {
+                        builder.AppendLine(@$"""{propertyMetadata.LuaMemberName}"" => throw new global::Lua.LuaRuntimeException(context.State, $""'{{key}}' cannot be read.""),");
+                        continue;
+                    }
+
                     var conversionPrefix = GetLuaValuePrefix(propertyMetadata.Type, references, compilation);
                     if (propertyMetadata.IsStatic)
                     {

+ 3 - 1
src/Lua.SourceGenerator/PropertyMetadata.cs

@@ -9,6 +9,7 @@ public class PropertyMetadata
     public string TypeFullName { get; }
     public bool IsStatic { get; }
     public bool IsReadOnly { get; }
+    public bool IsWriteOnly { get; }
     public string LuaMemberName { get; }
 
     public PropertyMetadata(ISymbol symbol, SymbolReferences references)
@@ -27,7 +28,8 @@ public class PropertyMetadata
         {
             Type = property.Type;
             TypeFullName = property.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
-            IsReadOnly = property.SetMethod == null;
+            IsReadOnly = property.IsReadOnly || property.SetMethod == null || property.SetMethod.IsInitOnly;
+            IsWriteOnly = property.IsWriteOnly;
         }
         else
         {

+ 0 - 5
src/Lua.SourceGenerator/TypeMetadata.cs

@@ -37,11 +37,6 @@ class TypeMetadata
 
                 if (x is IPropertySymbol p)
                 {
-                    if (p.GetMethod == null || p.SetMethod == null)
-                    {
-                        return false;
-                    }
-
                     if (p.IsIndexer)
                     {
                         return false;

+ 27 - 27
tests/Lua.Tests/LuaObjectTests.cs

@@ -1,59 +1,59 @@
 using Lua.Standard;
 
 namespace Lua.Tests;
+
 [LuaObject]
-public partial class LuaTestObj {
+public partial class LuaTestObj
+{
     int x;
     int y;
 
     [LuaMember("x")]
-    public int X {
+    public int X
+    {
         get => x;
         set => x = value;
     }
 
     [LuaMember("y")]
-    public int 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
-        };
+    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
-        };
+    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) {
+    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
-        };
+        return new LuaTestObj() { x = a.x - b.x, y = a.y - b.y };
     }
 }
+
 [LuaObject]
 public partial class TestUserData
 {
-    [LuaMember]
-    public int Property { get; set; }
+    [LuaMember] public int Property { get; init; }
 
-    [LuaMember]
-    public LuaValue LuaValueProperty { get; set; }
+    [LuaMember] public int ReadOnlyProperty { get; }
 
-    [LuaMember("p2")]
-    public string PropertyWithName { get; set; } = "";
+    [LuaMember] public int SetOnlyProperty { set { } }
+
+    [LuaMember] public LuaValue LuaValueProperty { get; set; }
+
+    [LuaMember("p2")] public string PropertyWithName { get; set; } = "";
 
     [LuaMember]
     public static void MethodVoid()
@@ -201,7 +201,7 @@ 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()
     {
@@ -209,7 +209,7 @@ public class LuaObjectTests
 
         var state = LuaState.Create();
         state.OpenBasicLibrary();
-        state.Environment["TestObj"]=userData;
+        state.Environment["TestObj"] = userData;
         var results = await state.DoStringAsync("""
                                                 local a = TestObj.create(1, 2)
                                                 local b = TestObj.create(3, 4)