Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Dec 28, 2020
1 parent 11fcc35 commit 69a6c4a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
4 changes: 2 additions & 2 deletions CSharp.lua/LuaSyntaxNodeTransform.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, Exp
return InternalBuildCodeTemplateExpression(codeTemplate, targetExpression, arguments.Select<LuaExpressionSyntax, Func<LuaExpressionSyntax>>(i => () => i), typeArguments);
}

private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, ExpressionSyntax targetExpression, IEnumerable<ExpressionSyntax> arguments, IList<ITypeSymbol> typeArguments) {
return InternalBuildCodeTemplateExpression(codeTemplate, targetExpression, arguments.Select<ExpressionSyntax, Func<LuaExpressionSyntax>>(i => () => VisitExpression(i)), typeArguments);
private LuaExpressionSyntax BuildCodeTemplateExpression(string codeTemplate, ExpressionSyntax targetExpression, IEnumerable<Func<LuaExpressionSyntax>> arguments, IList<ITypeSymbol> typeArguments) {
return InternalBuildCodeTemplateExpression(codeTemplate, targetExpression, arguments, typeArguments);
}

private void AddCodeTemplateExpression(LuaExpressionSyntax expression, string comma, LuaCodeTemplateExpressionSyntax codeTemplateExpression) {
Expand Down
2 changes: 1 addition & 1 deletion CSharp.lua/LuaSyntaxNodeTransform.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override LuaSyntaxNode VisitObjectCreationExpression(ObjectCreationExpres
if (symbol != null) {
string codeTemplate = XmlMetaProvider.GetMethodCodeTemplate(symbol);
if (codeTemplate != null) {
creationExpression = BuildCodeTemplateExpression(codeTemplate, null, node.ArgumentList.Arguments.Select(i => i.Expression), symbol.TypeArguments);
creationExpression = BuildCodeTemplateExpression(codeTemplate, null, FillCodeTemplateInvocationArguments(symbol, node.ArgumentList, null), symbol.TypeArguments);
} else if (node.Type.IsKind(SyntaxKind.NullableType)) {
Contract.Assert(node.ArgumentList.Arguments.Count == 1);
var argument = node.ArgumentList.Arguments.First();
Expand Down
43 changes: 32 additions & 11 deletions CSharp.lua/LuaSyntaxNodeTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2178,6 +2178,37 @@ private bool IsEnumToStringInvocationExpression(IMethodSymbol symbol, Invocation
return false;
}

private List<Func<LuaExpressionSyntax>> FillCodeTemplateInvocationArguments(IMethodSymbol symbol, ArgumentListSyntax argumentList, List<Func<LuaExpressionSyntax>> argumentExpressions) {
argumentExpressions ??= new();
foreach (var argument in argumentList.Arguments) {
if (argument.NameColon != null) {
string name = argument.NameColon.Name.Identifier.ValueText;
int index = symbol.Parameters.IndexOf(i => i.Name == name);
if (index == -1) {
throw new InvalidOperationException();
}
argumentExpressions.AddAt(index, () => VisitExpression(argument.Expression));
} else {
argumentExpressions.Add(() => VisitExpression(argument.Expression));
}
}

for (int i = 0; i < argumentExpressions.Count; ++i) {
if (argumentExpressions[i] == null) {
argumentExpressions[i] = () => GetDefaultParameterValue(symbol.Parameters[i], argumentList.Parent, true);
}
}

if (symbol.Parameters.Length > argumentList.Arguments.Count) {
argumentExpressions.AddRange(symbol.Parameters.Skip(argumentList.Arguments.Count).Where(i => !i.IsParams).Select(i => {
Func<LuaExpressionSyntax> func = () => GetDefaultParameterValue(i, argumentList.Parent, true);
return func;
}));
}

return argumentExpressions;
}

private LuaExpressionSyntax CheckCodeTemplateInvocationExpression(IMethodSymbol symbol, InvocationExpressionSyntax node) {
var kind = node.Expression.Kind();
if (kind == SyntaxKind.SimpleMemberAccessExpression || kind == SyntaxKind.MemberBindingExpression || kind == SyntaxKind.IdentifierName) {
Expand All @@ -2203,17 +2234,7 @@ private LuaExpressionSyntax CheckCodeTemplateInvocationExpression(IMethodSymbol
}
}

argumentExpressions.AddRange(node.ArgumentList.Arguments.Select(i => {
Func<LuaExpressionSyntax> func = () => VisitExpression(i.Expression);
return func;
}));
if (symbol.Parameters.Length > node.ArgumentList.Arguments.Count) {
argumentExpressions.AddRange(symbol.Parameters.Skip(node.ArgumentList.Arguments.Count).Where(i => !i.IsParams).Select(i => {
Func<LuaExpressionSyntax> func = () => GetDefaultParameterValue(i, node, true);
return func;
}));
}

FillCodeTemplateInvocationArguments(symbol, node.ArgumentList, argumentExpressions);
var invocationExpression = InternalBuildCodeTemplateExpression(
codeTemplate,
memberAccessExpression?.Expression,
Expand Down
2 changes: 1 addition & 1 deletion test/__bin/Lua/lua/run.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ end
local luaVersions = {
"Lua5.3",
"LuaJIT-2.0.2",
-- "MoonJIT-2.2.0"
--"MoonJIT-2.2.0"
}

local function compile(arg)
Expand Down

0 comments on commit 69a6c4a

Please sign in to comment.