Browse Source

Merge pull request #62 from AnnulusGames/fix-luaobject-bugs

Fix LuaObject bugs
Annulus Games 11 months ago
parent
commit
2e529ec802
2 changed files with 40 additions and 2 deletions
  1. 28 2
      src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs
  2. 12 0
      src/Lua/LuaValue.cs

+ 28 - 2
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,7 +362,32 @@ partial class LuaObjectGenerator
 
 
             foreach (var parameter in methodMetadata.Symbol.Parameters)
             foreach (var parameter in methodMetadata.Symbol.Parameters)
             {
             {
-                builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});");
+                var isParameterLuaValue = SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue);
+
+                if (parameter.HasExplicitDefaultValue)
+                {
+                    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
+                {
+                    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++;
             }
             }
 
 
@@ -398,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

+ 12 - 0
src/Lua/LuaValue.cs

@@ -46,6 +46,18 @@ public readonly struct LuaValue : IEquatable<LuaValue>
                     result = Unsafe.As<double, T>(ref v);
                     result = Unsafe.As<double, T>(ref v);
                     return true;
                     return true;
                 }
                 }
+                else if (t == typeof(int))
+                {
+                    var v = (int)value;
+                    result = Unsafe.As<int, T>(ref v);
+                    return true;
+                }
+                else if (t == typeof(long))
+                {
+                    var v = (long)value;
+                    result = Unsafe.As<long, T>(ref v);
+                    return true;
+                }
                 else if (t == typeof(object))
                 else if (t == typeof(object))
                 {
                 {
                     result = (T)(object)value;
                     result = (T)(object)value;