Skip to content

Commit

Permalink
fix #474
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Feb 19, 2024
1 parent 9844824 commit b03671b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 40 deletions.
28 changes: 20 additions & 8 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ end
local function multiKey(t, f, ...)
local n, i, k = select("#", ...), 1
while true do
local arg = assert(select(i, ...))
local arg = select(i, ...)
if not arg then error(i .. " is nil") end
if f then
k = f(arg)
else
Expand Down Expand Up @@ -1399,23 +1400,34 @@ local Tuple = defCls("System.Tuple", {
local tupleMetaTable = setmetatable({ __index = Object, __call = tupleCreate }, Object)
setmetatable(Tuple, tupleMetaTable)

local ValueTuple = defStc("System.ValueTuple", {
local ValueTuple = {
Deconstruct = tupleDeconstruct,
ToString = tupleToString,
__eq = tupleEquals,
Equals = tupleEquals,
EqualsObj = tupleEqualsObj,
CompareTo = tupleCompareTo,
CompareToObj = tupleCompareToObj,
getLength = tupleLength,
get = tupleGet,
default = function ()
throw(System.NotSupportedException("not support default(T) when T is ValueTuple"))
default = function (T)
local genericT = T.__genericT__
local t, n = {}, #genericT
for i = 1, n do
t[i] = genericT[i]:default()
end
t.n = n
return setmetatable(t, T)
end
})
}

local valueTupleMetaTable = setmetatable({ __index = ValueType, __call = tupleCreate }, ValueType)
setmetatable(ValueTuple, valueTupleMetaTable)
local ValueTupleFn = defStc("System.ValueTuple", function (...)
return {
__eq = tupleEquals,
__genericT__ = { ... },
}
end, ValueTuple, '')
ValueTuple.__call = tupleCreate
System.ValueTuple = ValueTupleFn

local function recordEquals(t, other)
if getmetatable(t) == getmetatable(other) then
Expand Down
3 changes: 2 additions & 1 deletion CSharp.lua/CoreSystem.Lua/CoreSystem/Math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ local function scaleB(a, b)
end

local function sinCos(a)
return System.ValueTuple(math.sin(a), math.cos(a))
local Double = System.Double
return System.ValueTuple(Double, Double)(math.sin(a), math.cos(a))
end

local function bigMul(a, b)
Expand Down
5 changes: 0 additions & 5 deletions CSharp.lua/LuaAst/LuaIdentifierNameSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ public class LuaIdentifierNameSyntax : LuaExpressionSyntax {
public static readonly LuaIdentifierNameSyntax Each = "System.each";
public static readonly LuaIdentifierNameSyntax Object = "System.Object";
public static readonly LuaIdentifierNameSyntax Array = "System.Array";
public static readonly LuaIdentifierNameSyntax MultiArray = "System.MultiArray";
public static readonly LuaIdentifierNameSyntax EmptyArray = "System.Array.Empty";
public static readonly LuaIdentifierNameSyntax Apply = "System.apply";
public static readonly LuaIdentifierNameSyntax StaticCtor = "static";
public static readonly LuaIdentifierNameSyntax Init = "internal";
public static readonly LuaIdentifierNameSyntax Ctor = "__ctor__";
public static readonly LuaIdentifierNameSyntax Inherits = "base";
public static readonly LuaIdentifierNameSyntax Default = "default";
public static readonly LuaIdentifierNameSyntax SystemDefault = "System.default";
public static readonly LuaIdentifierNameSyntax Property = "System.property";
public static readonly LuaIdentifierNameSyntax Event = "System.event";
Expand Down Expand Up @@ -88,7 +86,6 @@ public class LuaIdentifierNameSyntax : LuaExpressionSyntax {
public static readonly LuaIdentifierNameSyntax NullableClone = "System.Nullable.clone";
public static readonly LuaIdentifierNameSyntax CopyThis = "__copy__";
public static readonly LuaIdentifierNameSyntax RecordMembers = "__members__";
public static readonly LuaIdentifierNameSyntax ValueType = "System.ValueType";
public static readonly LuaIdentifierNameSyntax DateTime = "System.DateTime";
public static readonly LuaIdentifierNameSyntax TimeSpan = "System.TimeSpan";
public static readonly LuaIdentifierNameSyntax AnonymousType = "System.AnonymousType";
Expand All @@ -99,11 +96,9 @@ public class LuaIdentifierNameSyntax : LuaExpressionSyntax {
public static readonly LuaIdentifierNameSyntax Base = "base";
public static readonly LuaIdentifierNameSyntax SystemBase = "System.base";
public static readonly LuaIdentifierNameSyntax Tuple = "System.Tuple";
public static readonly LuaIdentifierNameSyntax ValueTuple = "System.ValueTuple";
public static readonly LuaIdentifierNameSyntax RecordType = "System.RecordType";
public static readonly LuaIdentifierNameSyntax RecordValueType = "System.RecordValueType";
public static readonly LuaIdentifierNameSyntax Deconstruct = "Deconstruct";
public static readonly LuaIdentifierNameSyntax KeyValuePair = "System.KeyValuePair";
public static readonly LuaIdentifierNameSyntax NullableType = "System.Nullable";
public static readonly LuaIdentifierNameSyntax Range = "System.Range";
public static readonly LuaIdentifierNameSyntax Index = "System.Index";
Expand Down
6 changes: 1 addition & 5 deletions CSharp.lua/LuaSyntaxGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,11 +2023,7 @@ internal LuaExpressionSyntax GetTypeName(ISymbol symbol, LuaSyntaxNodeTransform
if (namedTypeSymbol.IsAnonymousType) {
return LuaIdentifierNameSyntax.AnonymousType;
}

if (namedTypeSymbol.IsTupleType) {
return LuaIdentifierNameSyntax.ValueTuple;
}


if (namedTypeSymbol.IsSystemTuple()) {
return LuaIdentifierNameSyntax.Tuple;
}
Expand Down
2 changes: 1 addition & 1 deletion CSharp.lua/LuaSyntaxNodeTransform.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ private LuaNumericalForStatementSyntax GetNumericalForStatement(ForStatementSynt

private LuaExpressionSyntax GetValueTupleDefaultExpression(ITypeSymbol typeSymbol) {
var elementTypes = typeSymbol.GetTupleElementTypes();
return BuildValueTupleCreateExpression(elementTypes.Select(GetDefaultValueExpression));
return BuildValueTupleCreateExpression(typeSymbol, elementTypes.Select(GetDefaultValueExpression));
}

private LuaExpressionSyntax GetDefaultValueExpression(ITypeSymbol typeSymbol) {
Expand Down
20 changes: 8 additions & 12 deletions CSharp.lua/LuaSyntaxNodeTransform.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ private LuaSyntaxNode VisitObjectCreationExpression(BaseObjectCreationExpression
Contract.Assert(node.ArgumentList!.Arguments.Count == 1);
var argument = node.ArgumentList.Arguments.First();
return argument.Expression.Accept(this);
} else if (symbol.ContainingType.IsTupleType) {
var arguments = node.ArgumentList!.Arguments;
if (arguments.Count == 0) {
creationExpression = GetValueTupleDefaultExpression(symbol.ContainingType);
} else {
var expressions = arguments.Select(i => i.Expression.AcceptExpression(this));
creationExpression = BuildValueTupleCreateExpression(expressions);
}
} else {
creationExpression = GetObjectCreationExpression(symbol, node);
}
Expand Down Expand Up @@ -1516,11 +1508,13 @@ public override LuaSyntaxNode VisitRefExpression(RefExpressionSyntax node) {
}

public override LuaSyntaxNode VisitTupleType(TupleTypeSyntax node) {
return LuaIdentifierNameSyntax.ValueTuple;
var typeSymbol = semanticModel_.GetTypeInfo(node).Type;
return GetTypeName(typeSymbol);
}

private LuaExpressionSyntax BuildValueTupleCreateExpression(IEnumerable<LuaExpressionSyntax> expressions) {
return LuaIdentifierNameSyntax.ValueTuple.Invocation(expressions);
private LuaExpressionSyntax BuildValueTupleCreateExpression(ITypeSymbol typeSymbol, IEnumerable<LuaExpressionSyntax> expressions) {
var typeName = GetTypeName(typeSymbol);
return typeName.Invocation(expressions);
}

public override LuaSyntaxNode VisitTupleExpression(TupleExpressionSyntax node) {
Expand Down Expand Up @@ -1558,7 +1552,9 @@ public override LuaSyntaxNode VisitTupleExpression(TupleExpressionSyntax node) {
return new LuaLocalTupleVariableExpression(expressions.Cast<LuaIdentifierNameSyntax>());
}
}
return BuildValueTupleCreateExpression(expressions);

var typeSymbol = semanticModel_.GetTypeInfo(node).Type;
return BuildValueTupleCreateExpression(typeSymbol, expressions);
}

public override LuaSyntaxNode VisitParenthesizedVariableDesignation(ParenthesizedVariableDesignationSyntax node) {
Expand Down
24 changes: 16 additions & 8 deletions CSharp.lua/System.xml
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,22 @@ limitations under the License.
</property>
</class>
<class name="ValueTuple">
<method name="Create" ArgCount="2" Template="System.ValueTuple({0}, {1})" />
<method name="Create" ArgCount="3" Template="System.ValueTuple({0}, {1}, {2})" />
<method name="Create" ArgCount="4" Template="System.ValueTuple({0}, {1}, {2}, {3})" />
<method name="Create" ArgCount="5" Template="System.ValueTuple({0}, {1}, {2}, {3}, {4})" />
<method name="Create" ArgCount="6" Template="System.ValueTuple({0}, {1}, {2}, {3}, {4}, {5})" />
<method name="Create" ArgCount="7" Template="System.ValueTuple({0}, {1}, {2}, {3}, {4}, {5}, {6})" />
<method name="Create" ArgCount="8" Template="System.ValueTuple({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})" />
</class>
<method name="Create" ArgCount="2" Template="System.ValueTuple({`0}, {`1})({0}, {1})" />
<method name="Create" ArgCount="3" Template="System.ValueTuple({`0}, {`1}, {`2})({0}, {1}, {2})" />
<method name="Create" ArgCount="4" Template="System.ValueTuple({`0}, {`1}, {`2}, {`3})({0}, {1}, {2}, {3})" />
<method name="Create" ArgCount="5" Template="System.ValueTuple({`0}, {`1}, {`2}, {`3}, {`4})({0}, {1}, {2}, {3}, {4})" />
<method name="Create" ArgCount="6" Template="System.ValueTuple({`0}, {`1}, {`2}, {`3}, {`4}, {`5})({0}, {1}, {2}, {3}, {4}, {5})" />
<method name="Create" ArgCount="7" Template="System.ValueTuple({`0}, {`1}, {`2}, {`3}, {`4}, {`5}, {`6})({0}, {1}, {2}, {3}, {4}, {5}, {6})" />
<method name="Create" ArgCount="8" Template="System.ValueTuple({`0}, {`1}, {`2}, {`3}, {`4}, {`5}, {`6}, {`7})({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7})" />
</class>
<class name="ValueTuple`1" Name="System.ValueTuple" />
<class name="ValueTuple`2" Name="System.ValueTuple" />
<class name="ValueTuple`3" Name="System.ValueTuple" />
<class name="ValueTuple`4" Name="System.ValueTuple" />
<class name="ValueTuple`5" Name="System.ValueTuple" />
<class name="ValueTuple`6" Name="System.ValueTuple" />
<class name="ValueTuple`7" Name="System.ValueTuple" />
<class name="ValueTuple`8" Name="System.ValueTuple" />
<class name="Span`1" Name="System.Span">
<method name=".ctor" Template="{0}" />
<method name="op_Implicit" ArgCount="1" Name="ctorArray" />
Expand Down
6 changes: 6 additions & 0 deletions test/self-compiling/launcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ local codeAnalysisTypes = {
"Microsoft.CodeAnalysis.IPropertySymbol",
"Microsoft.CodeAnalysis.Optional_1",
"Microsoft.CodeAnalysis.SyntaxList_1",
"Microsoft.CodeAnalysis.SyntaxNode",
"Microsoft.CodeAnalysis.DocumentationProvider",

"Microsoft.CodeAnalysis.CSharp.CSharpCompilation",
"Microsoft.CodeAnalysis.CSharp.CSharpCommandLineArguments",
"Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker",
"Microsoft.CodeAnalysis.CSharp.Syntax.BlockSyntax",
"Microsoft.CodeAnalysis.CSharp.Syntax.BaseTypeSyntax",
"Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax",
"Microsoft.CodeAnalysis.CSharp.Syntax.NameColonSyntax",
"Microsoft.CodeAnalysis.CSharp.Syntax.ParameterSyntax",
"Microsoft.CodeAnalysis.CSharp.Syntax.AttributeListSyntax",
}
Expand Down

0 comments on commit b03671b

Please sign in to comment.