Browse Source

Add: support for default values ​​for parameters

AnnulusGames 11 months ago
parent
commit
4c8f26a697
1 changed files with 23 additions and 4 deletions
  1. 23 4
      src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

+ 23 - 4
src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

@@ -1,5 +1,6 @@
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.CSharp;
 using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
 
 
 namespace Lua.SourceGenerator;
 namespace Lua.SourceGenerator;
 
 
@@ -361,13 +362,31 @@ partial class LuaObjectGenerator
 
 
             foreach (var parameter in methodMetadata.Symbol.Parameters)
             foreach (var parameter in methodMetadata.Symbol.Parameters)
             {
             {
-                if (SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue))
+                var isParameterLuaValue = SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue);
+
+                if (parameter.HasExplicitDefaultValue)
                 {
                 {
-                    builder.AppendLine($"var arg{index} = context.GetArgument({index});");
+                    var syntax = (ParameterSyntax)parameter.DeclaringSyntaxReferences[0].GetSyntax();
+
+                    if (isParameterLuaValue)
+                    {
+                        builder.AppendLine($"var arg{index} = context.HasArgument({index}) ? context.GetArgument({index}) : {syntax.Default!.Value.ToFullString()};");
+                    }
+                    else
+                    {
+                        builder.AppendLine($"var arg{index} = context.HasArgument({index}) ?  context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index}) : {syntax.Default!.Value.ToFullString()};");
+                    }
                 }
                 }
                 else
                 else
                 {
                 {
-                    builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});");
+                    if (isParameterLuaValue)
+                    {
+                        builder.AppendLine($"var arg{index} = context.GetArgument({index});");
+                    }
+                    else
+                    {
+                        builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});");
+                    }
                 }
                 }
                 index++;
                 index++;
             }
             }
@@ -405,7 +424,7 @@ partial class LuaObjectGenerator
                 {
                 {
                     builder.AppendLine("buffer.Span[0] = new global::Lua.LuaValue(result);");
                     builder.AppendLine("buffer.Span[0] = new global::Lua.LuaValue(result);");
                 }
                 }
-                
+
                 builder.AppendLine($"return {(methodMetadata.IsAsync ? "1" : "new(1)")};");
                 builder.AppendLine($"return {(methodMetadata.IsAsync ? "1" : "new(1)")};");
             }
             }
             else
             else