diff --git a/CSharp.lua/LuaSyntaxNodeTransform.cs b/CSharp.lua/LuaSyntaxNodeTransform.cs index 384ac4b5..c21e1648 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.cs @@ -1735,6 +1735,10 @@ private LuaExpressionSyntax BuildBinaryInvokeAssignmentExpression(ExpressionSynt } private LuaExpressionSyntax BuildLuaSimpleAssignmentExpression(LuaExpressionSyntax left, LuaExpressionSyntax right) { + if (left is LuaPropertyTemplateExpressionSyntax propertyTemplate) { + return BuildCodeTemplateExpression(propertyTemplate.SetTemplate, propertyTemplate.GetArguments(right), propertyTemplate.Name); + } + if (left is LuaPropertyAdapterExpressionSyntax propertyAdapter) { propertyAdapter.IsGetOrAdd = false; propertyAdapter.ArgumentList.AddArgument(right); @@ -2726,6 +2730,9 @@ private LuaExpressionSyntax InternalVisitMemberAccessExpression(ISymbol symbol, if (node.Expression.IsKind(SyntaxKind.ThisExpression)) { var nameExpression = node.Name.AcceptExpression(this); + if (nameExpression is LuaPropertyTemplateExpressionSyntax thisTemplate) { + return BuildFieldOrPropertyMemberAccessExpression(LuaIdentifierNameSyntax.This, thisTemplate, false); + } if (symbol.Kind == SymbolKind.Method) { if (IsDelegateExpression((IMethodSymbol)symbol, node, nameExpression, LuaIdentifierNameSyntax.This, out var delegateExpression)) { return delegateExpression; @@ -2750,6 +2757,8 @@ private LuaExpressionSyntax InternalVisitMemberAccessExpression(ISymbol symbol, } case LuaMemberAccessExpressionSyntax memberAccess when memberAccess.Expression == LuaIdentifierNameSyntax.This: return nameExpression; + case LuaPropertyTemplateExpressionSyntax baseTemplate: + return BuildFieldOrPropertyMemberAccessExpression(LuaIdentifierNameSyntax.This, baseTemplate, false); default: return baseExpression.MemberAccess(nameExpression); } @@ -2977,6 +2986,19 @@ private bool IsEventAddOrRemoveIdentifierName(IdentifierNameSyntax node) { return false; } + // The name after the dot of a member access or of a ?. binding, whose receiver the caller fills in. + private static bool IsMemberNameOfAccess(IdentifierNameSyntax node) { + return node.Parent is MemberAccessExpressionSyntax access && access.Name == node + || node.Parent is MemberBindingExpressionSyntax + || node.Parent is QualifiedNameSyntax; + } + + // The receiver of a property template written by its bare name: the instance for an instance property; a static one has + // none, and its type stands for {this}. + private LuaExpressionSyntax GetPropertyTemplateReceiver(IPropertySymbol symbol) { + return symbol.IsStatic ? GetTypeName(symbol.ContainingType) : LuaIdentifierNameSyntax.This; + } + private bool IsPropertyField(IdentifierNameSyntax node, IPropertySymbol propertySymbol) { return IsPropertyField(propertySymbol) || (IsInternalNode(node) && generator_.IsAutoProperty(propertySymbol)); } @@ -3425,6 +3447,10 @@ LuaIdentifierNameSyntax GetSampleName(ISymbol nodeSymbol) { case SymbolKind.Property: { var propertyField = (IPropertySymbol)symbol; identifier = VisitPropertyOrEventIdentifierName(node, propertyField, true, out bool isField); + if (identifier is LuaPropertyTemplateExpressionSyntax propertyTemplate && !IsMemberNameOfAccess(node)) { + // Written with no receiver, the template's {this} is the instance itself. + identifier = BuildFieldOrPropertyMemberAccessExpression(GetPropertyTemplateReceiver(propertyField), propertyTemplate, propertyField.IsStatic); + } if (isField) { CheckValueTypeClone(propertyField.Type, node, ref identifier, true); } diff --git a/test/BridgeNetTests/Batch1/src/Templates/PropertyTemplateTests.cs b/test/BridgeNetTests/Batch1/src/Templates/PropertyTemplateTests.cs index 8160d2a7..d1725d28 100644 --- a/test/BridgeNetTests/Batch1/src/Templates/PropertyTemplateTests.cs +++ b/test/BridgeNetTests/Batch1/src/Templates/PropertyTemplateTests.cs @@ -204,9 +204,83 @@ public static void PropertyTemplateNullConditionalOperatorWorks() //Assert.Null(prop); } + [Test] + public static void PropertyTemplateWithoutReceiverTest() + { + var test = new TestType(); + test.WriteBare(3); + Assert.AreEqual(3, test.FullProp); + Assert.AreEqual(3, test.ReadBare()); + test.WriteThis(4); + Assert.AreEqual(4, test.ReadThis()); + Assert.AreEqual(5, TestType.ReadStatic()); + } + + [Test] + public static void PropertyTemplateThroughBaseTest() + { + var test = new DerivedType(); + test.WriteBase(5); + Assert.AreEqual(5, test.ReadBase()); + Assert.AreEqual(5, test.ReadInherited()); + } + + [Test] + public static void SetOnlyPropertyTemplateInInitializerTest() + { + var test = new TestType + { + SetProp = 6 + }; + Assert.AreEqual(6, test.GetProp); + } + + private class DerivedType : TestType + { + public int ReadBase() + { + return base.FullProp; + } + + public int ReadInherited() + { + return FullProp; + } + + public void WriteBase(int value) + { + base.FullProp = value; + } + } + #pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it private class TestType { + public int ReadBare() + { + return FullProp; + } + + public int ReadThis() + { + return this.FullProp; + } + + public void WriteBare(int value) + { + FullProp = value; + } + + public void WriteThis(int value) + { + this.FullProp = value; + } + + public static int ReadStatic() + { + return StaticGetOnlyProp; + } + /// @CSharpLua.Get = "a" /// @CSharpLua.Set = "a = {0}" public static extern int StaticProp { get; set; } @@ -220,6 +294,8 @@ private class TestType public extern int SetProp { get; set; } /// @CSharpLua.Get = "4" public extern int GetOnlyProp { get; } + /// @CSharpLua.Get = "5" + public static extern int StaticGetOnlyProp { get; } /// @CSharpLua.Get = "{this}.CustomProp3" /// @CSharpLua.Set = "{this}.CustomProp3 = {0}"