diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua index 8fc0541c..26805f59 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua @@ -239,35 +239,32 @@ local function fill(t, f, e, v) end end -local function buildArray(T, len, t) - if t == nil then - t = {} - if len > 0 then - local genericT = T.__genericT__ - local default = genericT:default() - if default == nil then - fill(t, 1, len, null) - elseif type(default) ~= "table" then - fill(t, 1, len, default) - else - for i = 1, len do - t[i] = genericT:default() - end +local function buildArray(ArrayT, n, t) + if type(n) == "table" then + t = n + elseif t ~= nil then + for i = 1, n do + if t[i] == nil then + t[i] = null end end else - if len > 0 then - local default = T.__genericT__:default() + t = {} + if n > 0 then + local T = ArrayT.__genericT__ + local default = T:default() if default == nil then - for i = 1, len do - if t[i] == nil then - t[i] = null - end + fill(t, 1, n, null) + elseif type(default) ~= "table" then + fill(t, 1, n, default) + else + for i = 1, n do + t[i] = T:default() end end end end - return setmetatable(t, T) + return setmetatable(t, ArrayT) end local function indexOf(t, v, startIndex, count) @@ -958,7 +955,7 @@ end Array = { version = 0, - new = buildArray, + __call = buildArray, set = set, get = get, setCapacity = function (t, len) @@ -1172,19 +1169,18 @@ Array = { if match(item) then break end - freeIndex = freeIndex + 1 + freeIndex = freeIndex + 1 end if freeIndex > size then return 0 end - local current = freeIndex + 1 - while current <= size do + while current <= size do while current <= size do local item = t[current] if item == null then item = nil end if not match(item) then break end - current = current + 1 + current = current + 1 end if current <= size then t[freeIndex] = t[current] @@ -1315,7 +1311,7 @@ Array = { end end, CreateInstance = function (elementType, length) - return buildArray(Array(elementType[1]), length) + return Array(elementType[1])(length) end, Empty = function (T) local t = emptys[T] @@ -1441,7 +1437,7 @@ Array = { Resize = function (t, newSize, T) if newSize < 0 then throw(ArgumentOutOfRangeException("newSize")) end if t == nil then - return buildArray(Array(T), newSize) + return Array(T)(newSize) end local len = #t if len > newSize then @@ -1585,34 +1581,6 @@ Array = { end } -function Array.__call(ArrayT, n, t) - if type(n) == "table" then - t = n - elseif t ~= nil then - for i = 1, n do - if t[i] == nil then - t[i] = null - end - end - else - t = {} - if n > 0 then - local T = ArrayT.__genericT__ - local default = T:default() - if default == nil then - fill(t, 1, n, null) - elseif type(default) ~= "table" then - fill(t, 1, n, default) - else - for i = 1, n do - t[i] = T:default() - end - end - end - end - return setmetatable(t, ArrayT) -end - function System.arrayFromList(t) return setmetatable(t, Array(t.__genericT__)) end diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Random.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Random.lua index f10015a3..242cc08e 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Random.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Random.lua @@ -9,7 +9,7 @@ System.define("System.Random", (function () local Sample, InternalSample, GenerateSeed, Next, GetSampleForLargeRange, NextDouble, NextBytes, internal, __ctor__, rnd internal = function (this) - this._seedArray = ArrayInt32:new(56) + this._seedArray = ArrayInt32(56) end __ctor__ = function (this, Seed) if not Seed then Seed = GenerateSeed() end diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/ReadOnlySpan.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/ReadOnlySpan.lua index 7c7512b2..733c702b 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/ReadOnlySpan.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/ReadOnlySpan.lua @@ -41,7 +41,7 @@ local ReadOnlySpan = { this._min = start this._max = start + length - 1 else - this._array = System.Array.new(this.__genericT__, 1) + this._array = System.Array(this.__genericT__)(1) this._array:set(0, input) this._min = 0 this._max = 0 diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua index e08e6cc3..3b3ddbd0 100644 --- a/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua @@ -22,7 +22,8 @@ local IndexOutOfRangeException = System.IndexOutOfRangeException local Span = { __ctor__ = function (this, input, ...) - if type(input) == "table" then + local t = type(input) + if t == "table" then local argsLen = select("#", ...) local maxLength = input:getLength() local start, length @@ -33,15 +34,19 @@ local Span = { end if start + length > maxLength then throw(ArgumentOutOfRangeException("length")) - end + end else start, length = 0, maxLength end this._array = input this._min = start this._max = start + length - 1 + elseif t == "number" then + this._array = System.Array(this.__genericT__)(input) + this._min = 0 + this._max = input else - this._array = System.Array.new(this.__genericT__, 1) + this._array = System.Array(this.__genericT__)(1) this._array:set(0, input) this._min = 0 this._max = 0 diff --git a/CSharp.lua/CoreSystem.Lua/Sample/test.lua b/CSharp.lua/CoreSystem.Lua/Sample/test.lua index c21dfb06..62ebef5d 100644 --- a/CSharp.lua/CoreSystem.Lua/Sample/test.lua +++ b/CSharp.lua/CoreSystem.Lua/Sample/test.lua @@ -51,27 +51,27 @@ local function printList(list) print(table.concat(t, " ")) end -local function testDateTimeAndTimeSpan() +local function testDateTimeAndTimeSpan() local date = System.DateTime.getNow() print(date:getTicks()) print(date:ToString(), date:getYear(), date:getMonth(), date:getDay(), date:getHour(), date:getMinute(), date:getSecond()) - + local ts = System.TimeSpan.FromSeconds(20) print(ts:ToString()) - + date = date + System.TimeSpan.FromDays(2) print(date:ToString()) - + date = date:AddMonths(2); print(date:ToString()) - - local baseTime = System.DateTime(1970, 1, 1) + + local baseTime = System.DateTime(1970, 1, 1) print(baseTime:ToString()) print(baseTime:AddMilliseconds(1458032204643):ToString()) end -local function testArray() - local arr = System.Array(System.Int32):new(10) +local function testArray() + local arr = System.Array(System.Int32)(10) print(arr:ToString(), #arr) printList(arr) arr:set(0, 2) diff --git a/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs b/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs index 212c8cac..fdae2b6f 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.Helper.cs @@ -459,15 +459,19 @@ private bool IsInternalMember(ISymbol symbol) { } private LuaExpressionSyntax BuildArray(IArrayTypeSymbol symbol, params LuaExpressionSyntax[] elements) { - var baseType = GetTypeName(symbol.ElementType); + return BuildArray(symbol.ElementType, elements); + } + + private LuaExpressionSyntax BuildArray(ITypeSymbol elementType, params LuaExpressionSyntax[] elements) { + var baseType = GetTypeName(elementType); var arrayType = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Array, baseType); - return BuildArray(symbol, arrayType, elements); + return BuildArray(elementType, arrayType, elements); } - private LuaExpressionSyntax BuildArray(IArrayTypeSymbol symbol, LuaExpressionSyntax arrayType, IList elements) { + private LuaExpressionSyntax BuildArray(ITypeSymbol elementType, LuaExpressionSyntax arrayType, IList elements) { var invocation = new LuaInvocationExpressionSyntax(arrayType); var table = new LuaTableExpression(elements); - bool isElementNotNull = (symbol.ElementType.IsValueType && !symbol.ElementType.IsNullableType()) + bool isElementNotNull = (elementType.IsValueType && !elementType.IsNullableType()) || elements.All(i => i is LuaLiteralExpressionSyntax && i != LuaIdentifierLiteralExpressionSyntax.Nil); if (isElementNotNull) { invocation.AddArgument(table); diff --git a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs index 7cc88acd..a8137fec 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs @@ -276,10 +276,14 @@ private void FillMultiArrayInitializer(InitializerExpressionSyntax initializer, } private LuaExpressionSyntax BuildArrayCreationExpression(IArrayTypeSymbol symbol, LuaArrayTypeAdapterExpressionSyntax arrayType, InitializerExpressionSyntax initializer) { + return BuildArrayCreationExpression(symbol.ElementType, arrayType, initializer); + } + + private LuaExpressionSyntax BuildArrayCreationExpression(ITypeSymbol elementType, LuaArrayTypeAdapterExpressionSyntax arrayType, InitializerExpressionSyntax initializer) { if (initializer?.Expressions.Count > 0) { if (arrayType.IsSimpleArray) { var initializerExpressions = initializer.Expressions.Select(i => i.AcceptExpression(this)).ToList(); - return BuildArray(symbol, arrayType, initializerExpressions); + return BuildArray(elementType, arrayType, initializerExpressions); } var rank = new LuaTableExpression { IsSingleLine = true }; @@ -292,7 +296,7 @@ private LuaExpressionSyntax BuildArrayCreationExpression(IArrayTypeSymbol symbol if (arrayType.IsSimpleArray) { var size = arrayType.RankSpecifier.Sizes.FirstOrDefault() ?? LuaNumberLiteralExpressionSyntax.Zero; if (size is LuaNumberLiteralExpressionSyntax { Number: 0 }) { - return BuildArray(symbol, arrayType, Array.Empty()); + return BuildArray(elementType, arrayType, Array.Empty()); } return BuildArray(arrayType, size); } @@ -1279,23 +1283,9 @@ public override LuaSyntaxNode VisitSizeOfExpression(SizeOfExpressionSyntax node) } public override LuaSyntaxNode VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node) { - var typeInfo = semanticModel_.GetTypeInfo(node.Type); - var symbol = (INamedTypeSymbol)typeInfo.Type; - var elementTypeExp = GetTypeName(symbol.TypeArguments.Single()); + var symbol = semanticModel_.GetTypeInfo(node.Type).Type; var spanTypeExp = node.Type.Accept(this); - - var arrayInvocationExp = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Array, elementTypeExp); - LuaExpressionSyntax arrayExp = arrayInvocationExp; - var newArrayExp = GetGenericTypeImportName(arrayInvocationExp, out var argumentTypeNames); - if (!IsLocalVarExistsInCurMethod(newArrayExp)) { - if (AddGenericImport(arrayInvocationExp, newArrayExp, argumentTypeNames, isFromCode: false)) { - arrayExp = newArrayExp; - } - } - - var arraySizeExp = spanTypeExp.RankSpecifier.Sizes.Single(); - - return spanTypeExp.Invocation(arrayExp.Invocation(arraySizeExp)); + return BuildArrayCreationExpression(symbol, spanTypeExp, node.Initializer); } public override LuaSyntaxNode VisitUnsafeStatement(UnsafeStatementSyntax node) {