Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Jan 8, 2024
2 parents c3dca5c + bf5e56d commit 42fe1d7
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ local function get(t, index)
if v == nil then
throw(ArgumentOutOfRangeException("index"))
end
if v ~= null then
if v ~= null then
return v
end
return nil
Expand Down
54 changes: 31 additions & 23 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Dictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ local lengthFn = System.lengthFn
local versions = System.versions
local Array = System.Array
local toString = System.toString
local hasHash = System.hasHash

local checkIndexAndCount = System.checkIndexAndCount
local throwFailedVersion = System.throwFailedVersion
local ArgumentNullException = System.ArgumentNullException
local ArgumentException = System.ArgumentException
local KeyNotFoundException = System.KeyNotFoundException
local EqualityComparer = System.EqualityComparer
local NotSupportedException = System.NotSupportedException

local assert = assert
local pairs = pairs
local next = next
local select = select
Expand Down Expand Up @@ -247,12 +246,12 @@ end
local Dictionary = {
getIsFixedSize = falseFn,
getIsReadOnly = falseFn,
__ctor__ = function (this, ...)
__ctor__ = function (this, ...)
local n = select("#", ...)
if n == 0 then
elseif n == 1 then
local comparer = ...
if comparer == nil or type(comparer) == "number" then
if comparer == nil or type(comparer) == "number" then
else
local equals = comparer.EqualsOf
if equals == nil then
Expand All @@ -263,10 +262,10 @@ local Dictionary = {
end
else
local dictionary, comparer = ...
if comparer ~= nil then
if comparer ~= nil then
buildHasComparer(this, ...)
end
if type(dictionary) ~= "number" then
if type(dictionary) ~= "number" then
buildFromDictionary(this, dictionary)
end
end
Expand All @@ -275,7 +274,7 @@ local Dictionary = {
Add = function (this, ...)
local k, v
if select("#", ...) == 1 then
local pair = ...
local pair = ...
k, v = pair[1], pair[2]
else
k, v = ...
Expand All @@ -290,7 +289,7 @@ local Dictionary = {
end,
ContainsKey = function (this, key)
if key == nil then throw(ArgumentNullException("key")) end
return this[key] ~= nil
return this[key] ~= nil
end,
ContainsValue = function (this, value)
if value == nil then
Expand Down Expand Up @@ -329,11 +328,11 @@ local Dictionary = {
local count = getCount(this)
checkIndexAndCount(array, index, count)
if count > 0 then
local KeyValuePair = this.__genericT__
local T = this.__genericT__
index = index + 1
for k, v in pairs(this) do
if v == null then v = nil end
array[index] = setmetatable({ k, v }, KeyValuePair)
array[index] = setmetatable({ k, v }, T)
index = index + 1
end
end
Expand All @@ -358,7 +357,7 @@ local Dictionary = {
end,
TryAdd = function (this, key, value)
if key == nil then throw(ArgumentNullException("key")) end
local exists, currentValue = this:TryGetValue(key)
local exists = this:TryGetValue(key)
if exists then
return false
end
Expand Down Expand Up @@ -477,17 +476,17 @@ local ArrayDictionary = (function ()
local function buildFromDictionary(this, dictionary)
if dictionary == nil then throw(ArgumentNullException("dictionary")) end
local count = 1
local KeyValuePair = this.__genericT__
local T = this.__genericT__
for _, pair in each(dictionary) do
local k, v = pair[1], pair[2]
if type(k) == "table" and k.class == 'S' then
k = k:__clone__()
end
this[count] = setmetatable({ k, v }, KeyValuePair)
this[count] = setmetatable({ k, v }, T)
count = count + 1
end
end
end

local function add(this, key, value, set)
if key == nil then throw(ArgumentNullException("key")) end
local len = #this
Expand All @@ -508,7 +507,7 @@ local ArrayDictionary = (function ()
this[len + 1] = setmetatable({ key, value }, this.__genericT__)
versions[this] = (versions[this] or 0) + 1
end

local function remove(this, key)
if key == nil then throw(ArgumentNullException("key")) end
local len = #this
Expand All @@ -525,7 +524,7 @@ local ArrayDictionary = (function ()
end
return false
end

return {
getIsFixedSize = falseFn,
getIsReadOnly = falseFn,
Expand Down Expand Up @@ -605,7 +604,7 @@ local ArrayDictionary = (function ()
local comparer = EqualityComparer(this.__genericTValue__).getDefault()
if comparer:EqualsOf(t[2], pair[2]) then
return true
end
end
end
end
end
Expand Down Expand Up @@ -716,13 +715,22 @@ function System.isDictLike(t)
end

local DictionaryFn = define("System.Collections.Generic.Dictionary", function(TKey, TValue)
local array, len
if TKey.class == 'S' and type(TKey:default()) == "table" then
array = ArrayDictionary
else
local array
local c = TKey.class
if c == 'S'then
if type(TKey:default()) == "table" then
array = ArrayDictionary
end
elseif c == 'C' then
if hasHash(TKey) then
array = ArrayDictionary
end
end
local len
if not array then
len = getCount
end
return {
return {
base = { System.IDictionary_2(TKey, TValue), System.IDictionary, System.IReadOnlyDictionary_2(TKey, TValue) },
__genericT__ = KeyValuePairFn(TKey, TValue),
__genericTKey__ = TKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ local HashSet = {
elseif len == 1 then
local collection = ...
if collection == nil then return end
if collection.getEnumerator ~= nil then
if collection.GetEnumerator ~= nil then
build(this, collection, nil)
else
assert(true)
Expand Down
17 changes: 7 additions & 10 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Convert.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,11 @@ local function fromBase64Decode(s, offset, len, t, resultLength)
local coroutine
local c = s:get(i + offset)
i = i + 1
if c >= 65 then
if c <= 90 then
c = c - 65
elseif c <= 122 then
c = c - 71
end
elseif c >= 48 then
if c >= 65 and c <= 90 then
c = c - 65
elseif c >= 97 and c <= 122 then
c = c - 71
elseif c >= 48 and c <= 57 then
c = c + 4
else
if c == 43 then
Expand Down Expand Up @@ -545,7 +543,7 @@ local function fromBase64Decode(s, offset, len, t, resultLength)
throw(FormatException("Format_BadBase64CharArrayLength"))
end

if j < 2 then
if resultLength - j < 2 then
return - 1
end

Expand All @@ -555,7 +553,7 @@ local function fromBase64Decode(s, offset, len, t, resultLength)
codes = 0x000000ff
else
while i < len - 1 do
c = s:get(i + offset)
local c = s:get(i + offset)
if c ~= 32 and c ~= 10 and c ~= 13 and c ~= 9 then
break
end
Expand Down Expand Up @@ -596,7 +594,6 @@ local function fromBase64CharPtr(s, i, n)
n = n - 1
end
local resultLength = fromBase64ComputeResultLength(s, i, n)
print(resultLength .. " xxx")
local t = {}
fromBase64Decode(s, i, n, t, resultLength)
return System.arrayFromTable(t, Byte)
Expand Down
5 changes: 5 additions & 0 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ System = {
trueFn = trueFn,
identityFn = identityFn,
lengthFn = lengthFn,
nilFn = nilFn,
zeroFn = zeroFn,
oneFn = oneFn,
equals = equals,
Expand Down Expand Up @@ -1184,6 +1185,10 @@ Object = defCls("System.Object", {
})
setmetatable(Object, { __call = new })

System.hasHash = function (T)
return T.GetHashCode ~= identityFn
end

ValueType = defCls("System.ValueType", {
class = "S",
default = function(T)
Expand Down
7 changes: 4 additions & 3 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Reflection/Assembly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ local FieldInfo = define("System.Reflection.FieldInfo", {
base = { MemberInfo },
memberType = 4,
getFieldType = getFieldOrPropertyType,
GetRawConstantValue = System.nilFn,
GetValue = getOrSetField,
SetValue = function (this, obj, value)
getOrSetField(this, obj, true, value)
Expand Down Expand Up @@ -1114,16 +1115,16 @@ System.Delegate.CreateDelegate = function (delegateType, ...)
if throwOnBindFailure == false then
return nil
end
throw(MissingMethodException())
throw(MissingMethodException())
end
return method.f
end
method = typeof(target):GetMethod(method)
if method == nil then
if ignoreCase == false then
return nil
return nil
end
throw(MissingMethodException())
throw(MissingMethodException())
end
end
return System.fn(target, method.f)
Expand Down
39 changes: 38 additions & 1 deletion CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,50 @@ local function checkIndex(value, startIndex, count)
return startIndex, count, len
end

-- https://stackoverflow.com/questions/7983574/how-to-write-a-unicode-symbol-in-lua
local bytemarkers = { {0x7FF,192}, {0xFFFF,224}, {0x1FFFFF,240} }
local function utf8(decimal)
if decimal < 128 then return char(decimal) end
local charbytes = {}
for i = 1, #bytemarkers do
local vals = bytemarkers[i]
if decimal<=vals[1] then
for b = i + 1, 2, -1 do
local mod = decimal%64
decimal = (decimal-mod)/64
charbytes[b] = char(128+mod)
end
charbytes[1] = char(vals[2]+decimal)
break
end
end
return tconcat(charbytes)
end

local function ctor(String, value, startIndex, count)
if type(value) == "number" then
if startIndex <= 0 then throw(ArgumentOutOfRangeException("count")) end
return rep(char(value), startIndex)
end
startIndex, count = checkIndex(value, startIndex, count)
return char(unpack(value, startIndex + 1, startIndex + count))
local found
for i = startIndex + 1, startIndex + count do
local c = value[i]
if c >= 128 then
found = true
break
end
end
if not found then
return char(unpack(value, startIndex + 1, startIndex + count))
end
local t, index = {}, 1
for i = startIndex + 1, startIndex + count do
local c = value[i]
t[index] = utf8(c)
index = index + 1
end
return tconcat(t)
end

local function get(this, index)
Expand Down
2 changes: 1 addition & 1 deletion CSharp.lua/LuaAst/LuaSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static string GetCtorNameString(int ctorIndex) {
static LuaSyntaxNode() {
// lua reserved words
foreach (var field in typeof(Keyword).GetFields()) {
ReservedWords.Add(field.GetRawConstantValue().ToString());
ReservedWords.Add(field.GetRawConstantValue()?.ToString());
}
}

Expand Down
2 changes: 1 addition & 1 deletion CSharp.lua/LuaAst/LuaTypeDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ private void CheckStaticCtorFunction(LuaBlockSyntax body) {
staticCtor.AddParameter(LuaIdentifierNameSyntax.This);
staticCtor.Body.Statements.AddRange(statements);
AddStaticAssignmentNames(staticCtor.Body);
if (staticCtor_ is not null && staticCtor_.Document is not null) {
if (staticCtor_?.Document != null) {
body.AddStatement(staticCtor_.Document);
}
AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor);
Expand Down
8 changes: 0 additions & 8 deletions CSharp.lua/LuaSyntaxNodeTransform.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,14 +1108,6 @@ private LuaExpressionSyntax GetTypeNameOfMetadata(ISymbol symbol) {
}

private LuaExpressionSyntax BuildFieldOrPropertyMemberAccessExpression(LuaExpressionSyntax expression, LuaExpressionSyntax name, bool isStatic) {
/*
if (name is LuaInvocationExpressionSyntax luaInvocation && luaInvocation.Expression == LuaIdentifierNameSyntax.NullableClone) {
for (var i = 0; i < luaInvocation.Arguments.Count; i++) {
luaInvocation.Arguments[i] = BuildFieldOrPropertyMemberAccessExpression(expression, luaInvocation.Arguments[i], isStatic);
}
return name;
}*/

if (name is LuaPropertyTemplateExpressionSyntax propertyTemplate) {
var getExpression = propertyTemplate.GetTemplate != null
? (LuaCodeTemplateExpressionSyntax)BuildCodeTemplateExpression(propertyTemplate.GetTemplate, new LuaSymbolNameSyntax(expression))
Expand Down
Loading

0 comments on commit 42fe1d7

Please sign in to comment.