|
1 | 1 | using Microsoft.CodeAnalysis; |
2 | 2 | using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
3 | 4 |
|
4 | 5 | namespace Lua.SourceGenerator; |
5 | 6 |
|
@@ -361,13 +362,31 @@ static void EmitMethodFunction(string functionName, string chunkName, TypeMetada |
361 | 362 |
|
362 | 363 | foreach (var parameter in methodMetadata.Symbol.Parameters) |
363 | 364 | { |
364 | | - if (SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue)) |
| 365 | + var isParameterLuaValue = SymbolEqualityComparer.Default.Equals(parameter.Type, references.LuaValue); |
| 366 | + |
| 367 | + if (parameter.HasExplicitDefaultValue) |
365 | 368 | { |
366 | | - builder.AppendLine($"var arg{index} = context.GetArgument({index});"); |
| 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 | + } |
367 | 379 | } |
368 | 380 | else |
369 | 381 | { |
370 | | - builder.AppendLine($"var arg{index} = context.GetArgument<{parameter.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}>({index});"); |
| 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 | + } |
371 | 390 | } |
372 | 391 | index++; |
373 | 392 | } |
@@ -405,7 +424,7 @@ static void EmitMethodFunction(string functionName, string chunkName, TypeMetada |
405 | 424 | { |
406 | 425 | builder.AppendLine("buffer.Span[0] = new global::Lua.LuaValue(result);"); |
407 | 426 | } |
408 | | - |
| 427 | + |
409 | 428 | builder.AppendLine($"return {(methodMetadata.IsAsync ? "1" : "new(1)")};"); |
410 | 429 | } |
411 | 430 | else |
|
0 commit comments