Skip to content

Commit

Permalink
Merge pull request #452 from joelverhagen/stackalloc
Browse files Browse the repository at this point in the history
Add Span, minimal MemoryExtensions, and stackalloc handling
  • Loading branch information
yanghuan authored Dec 28, 2023
2 parents c7e9003 + 7d56455 commit 8d37d41
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CSharp.lua/CoreSystem.Lua/All.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ return function(dir, conf)
load("DateTime")
load("Collections.EqualityComparer")
load("Array")
load("Span")
load("MemoryExtensions")
load("ReadOnlySpan")
load("Type")
load("Collections.List")
Expand Down
29 changes: 29 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/MemoryExtensions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--[[
Copyright 2017 YANG Huan ([email protected]).
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
}
102 changes: 102 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Span.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
--[[
Copyright 2017 YANG Huan ([email protected]).
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
21 changes: 17 additions & 4 deletions CSharp.lua/LuaSyntaxNodeTransform.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,10 +1273,23 @@ public override LuaSyntaxNode VisitSizeOfExpression(SizeOfExpressionSyntax node)
}

public override LuaSyntaxNode VisitStackAllocArrayCreationExpression(StackAllocArrayCreationExpressionSyntax node) {
var arrayType = node.Type.Accept<LuaArrayTypeAdapterExpressionSyntax>(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<LuaArrayTypeAdapterExpressionSyntax>(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) {
Expand Down
7 changes: 7 additions & 0 deletions CSharp.lua/System.xml
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,13 @@ limitations under the License.
<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>
<class name="Span`1" Name="System.Span">
<method name=".ctor" Template="{0}" />
</class>
<class name="MemoryExtensions" Name="System.MemoryExtensions">
<method name="AsSpan" ArgCount="1" Template="System.MemoryExtensions.AsSpan({0})" />
<method name="AsSpan" ArgCount="3" Template="System.MemoryExtensions.AsBoundedSpan({0}, {1}, {2})" />
</class>
<class name="ReadOnlySpan`1" Name="System.ReadOnlySpan">
<method name=".ctor" Template="{0}" />
<method name="op_Implicit" ArgCount="1" Name="ctorArray" />
Expand Down

0 comments on commit 8d37d41

Please sign in to comment.