Skip to content

Commit 2e529ec

Browse files
authored
Merge pull request #62 from AnnulusGames/fix-luaobject-bugs
Fix LuaObject bugs
2 parents ae1e3ec + 4c8f26a commit 2e529ec

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Lua.SourceGenerator/LuaObjectGenerator.Emit.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
34

45
namespace Lua.SourceGenerator;
56

@@ -361,7 +362,32 @@ static void EmitMethodFunction(string functionName, string chunkName, TypeMetada
361362

362363
foreach (var parameter in methodMetadata.Symbol.Parameters)
363364
{
364-
builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});");
365+
var isParameterLuaValue = SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue);
366+
367+
if (parameter.HasExplicitDefaultValue)
368+
{
369+
var syntax = (ParameterSyntax)parameter.DeclaringSyntaxReferences[0].GetSyntax();
370+
371+
if (isParameterLuaValue)
372+
{
373+
builder.AppendLine($"var arg{index} = context.HasArgument({index}) ? context.GetArgument({index}) : {syntax.Default!.Value.ToFullString()};");
374+
}
375+
else
376+
{
377+
builder.AppendLine($"var arg{index} = context.HasArgument({index}) ? context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index}) : {syntax.Default!.Value.ToFullString()};");
378+
}
379+
}
380+
else
381+
{
382+
if (isParameterLuaValue)
383+
{
384+
builder.AppendLine($"var arg{index} = context.GetArgument({index});");
385+
}
386+
else
387+
{
388+
builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});");
389+
}
390+
}
365391
index++;
366392
}
367393

@@ -398,7 +424,7 @@ static void EmitMethodFunction(string functionName, string chunkName, TypeMetada
398424
{
399425
builder.AppendLine("buffer.Span[0] = new global::Lua.LuaValue(result);");
400426
}
401-
427+
402428
builder.AppendLine($"return {(methodMetadata.IsAsync ? "1" : "new(1)")};");
403429
}
404430
else

src/Lua/LuaValue.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public bool TryRead<T>(out T result)
4646
result = Unsafe.As<double, T>(ref v);
4747
return true;
4848
}
49+
else if (t == typeof(int))
50+
{
51+
var v = (int)value;
52+
result = Unsafe.As<int, T>(ref v);
53+
return true;
54+
}
55+
else if (t == typeof(long))
56+
{
57+
var v = (long)value;
58+
result = Unsafe.As<long, T>(ref v);
59+
return true;
60+
}
4961
else if (t == typeof(object))
5062
{
5163
result = (T)(object)value;

0 commit comments

Comments
 (0)