From 6bed45db0bdb92c2ffd45348d629f5ea13621aef Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Wed, 27 Dec 2023 14:30:10 -0500 Subject: [PATCH 1/2] Add Span and minimal MemoryExtensions # Conflicts: # CSharp.lua/CoreSystem.Lua/All.lua # CSharp.lua/System.xml --- CSharp.lua/CoreSystem.Lua/All.lua | 2 + .../CoreSystem/MemoryExtensions.lua | 29 +++++ CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua | 102 ++++++++++++++++++ CSharp.lua/System.xml | 7 ++ 4 files changed, 140 insertions(+) create mode 100644 CSharp.lua/CoreSystem.Lua/CoreSystem/MemoryExtensions.lua create mode 100644 CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua diff --git a/CSharp.lua/CoreSystem.Lua/All.lua b/CSharp.lua/CoreSystem.Lua/All.lua index 8b719023..a86240c4 100644 --- a/CSharp.lua/CoreSystem.Lua/All.lua +++ b/CSharp.lua/CoreSystem.Lua/All.lua @@ -33,6 +33,8 @@ return function(dir, conf) load("DateTime") load("Collections.EqualityComparer") load("Array") + load("Span") + load("MemoryExtensions") load("Type") load("Collections.List") load("Collections.Dictionary") diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/MemoryExtensions.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/MemoryExtensions.lua new file mode 100644 index 00000000..897b2127 --- /dev/null +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/MemoryExtensions.lua @@ -0,0 +1,29 @@ +--[[ +Copyright 2017 YANG Huan (sy.yanghuan@gmail.com). + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--]] + +local System = System +local Span = System.Span + +System.MemoryExtensions = { + AsSpan = function (array) + local SpanT = Span(array.__genericT__) + return SpanT(array) + end, + AsBoundedSpan = function (array, start, length) + local SpanT = Span(array.__genericT__) + return SpanT(array, start, length) + end +} diff --git a/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua b/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua new file mode 100644 index 00000000..e08e6cc3 --- /dev/null +++ b/CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua @@ -0,0 +1,102 @@ +--[[ +Copyright 2017 YANG Huan (sy.yanghuan@gmail.com). + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--]] + +local System = System + +local throw = System.throw +local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException +local IndexOutOfRangeException = System.IndexOutOfRangeException + +local Span = { + __ctor__ = function (this, input, ...) + if type(input) == "table" then + local argsLen = select("#", ...) + local maxLength = input:getLength() + local start, length + if argsLen == 2 then + start, length = ... + if start >= maxLength then + throw(ArgumentOutOfRangeException("start")) + end + if start + length > maxLength then + throw(ArgumentOutOfRangeException("length")) + end + else + start, length = 0, maxLength + end + this._array = input + this._min = start + this._max = start + length - 1 + else + this._array = System.Array.new(this.__genericT__, 1) + this._array:set(0, input) + this._min = 0 + this._max = 0 + end + end, + get = function (this, index) + local i = this._min + index + if i > this._max then + throw(IndexOutOfRangeException("index")) + end + return this._array:get(i) + end, + set = function (this, index, value) + local i = this._min + index + if i > this._max then + throw(IndexOutOfRangeException("index")) + end + return this._array:set(i, value) + end, + getIsEmpty = function (this) + return this:getLength() <= 0 + end, + getLength = function (this) + return this._max - this._min + 1 + end, + Slice = function (this, start, ...) + local newMin = this._min + start + if newMin > this._max then + throw(ArgumentOutOfRangeException("start")) + end + + local newMax + local argsLen = select("#", ...) + if argsLen == 1 then + length = ... + newMax = newMin + length - 1 + if newMax > this._max then + throw(ArgumentOutOfRangeException("length")) + end + else + newMax = this._max + end + local ctor = System.Span(this.__genericT__) + return ctor(this._array, newMin, newMax - newMin + 1) + end, + ctorArray = function (array) + local ctor = System.Span(array.__genericT__) + return ctor(array) + end +} + +local SpanFn = System.defStc("System.Span", function (T) + return { + __genericT__ = T + } +end, Span, 1) + +System.Span = SpanFn diff --git a/CSharp.lua/System.xml b/CSharp.lua/System.xml index 028f1f6a..9592b166 100644 --- a/CSharp.lua/System.xml +++ b/CSharp.lua/System.xml @@ -345,6 +345,13 @@ limitations under the License. + + + + + + + From e85ba4946d76dc45f480fdb65fcd5f33ae316a22 Mon Sep 17 00:00:00 2001 From: Joel Verhagen Date: Wed, 27 Dec 2023 16:16:58 -0500 Subject: [PATCH 2/2] Treat stackalloc as normal heap alloc --- CSharp.lua/LuaSyntaxNodeTransform.Object.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs index 2dcd24e0..75e8d152 100644 --- a/CSharp.lua/LuaSyntaxNodeTransform.Object.cs +++ b/CSharp.lua/LuaSyntaxNodeTransform.Object.cs @@ -1273,10 +1273,23 @@ public override LuaSyntaxNode VisitSizeOfExpression(SizeOfExpressionSyntax node) } public override LuaSyntaxNode VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node) { - var arrayType = node.Type.Accept(this); - var symbol = (IArrayTypeSymbol)semanticModel_.GetTypeInfo(node.Type).Type; - var array = BuildArrayCreationExpression(symbol, arrayType, node.Initializer); - return new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.StackAlloc, array); + var typeInfo = semanticModel_.GetTypeInfo(node.Type); + var symbol = (INamedTypeSymbol)typeInfo.Type; + var elementTypeExp = GetTypeName(symbol.TypeArguments.Single()); + 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)); } public override LuaSyntaxNode VisitUnsafeStatement(UnsafeStatementSyntax node) {