Skip to content

Commit

Permalink
fix #477
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghuan committed Feb 1, 2024
1 parent 0c3f01e commit 7191013
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bld/
.vs/

# Visual Studio Code options directory
.vscode/
#.vscode/

# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
Expand Down
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lua",
"request": "launch",
"tag": "independent_file",
"name": "LuaPanda-lua5.3",
"description": "独立文件调试模式,使用前请参考文档",
"luaPath": "${workspaceFolder}/test/__bin/lua5.3/lua.exe",
"packagePath": [],
"luaFileExtension": "",
"connectionPort": 8820,
"stopOnEntry": true,
"useCHook": true
}
]
}
25 changes: 8 additions & 17 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Dictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ local ArrayDictionaryCollection = define("System.Collections.Generic.ArrayDictio
Contains = function (this, v)
if this.kind then
return this.dict:ContainsValue(v)
end
end
return this.dict:ContainsKey(v)
end,
GetEnumerator = function (this)
Expand Down Expand Up @@ -534,7 +534,7 @@ local ArrayDictionary = (function ()
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 Down Expand Up @@ -697,8 +697,8 @@ local ArrayDictionary = (function ()
}
end)()

ArrayDictionaryFn = define("System.Collections.Generic.ArrayDictionary", function(TKey, TValue)
return {
ArrayDictionaryFn = define("System.Collections.Generic.ArrayDictionary", function(TKey, TValue)
return {
base = { System.IDictionary_2(TKey, TValue), System.IDictionary, System.IReadOnlyDictionary_2(TKey, TValue) },
__genericT__ = KeyValuePairFn(TKey, TValue),
__genericTKey__ = TKey,
Expand All @@ -715,19 +715,10 @@ function System.isDictLike(t)
end

local DictionaryFn = define("System.Collections.Generic.Dictionary", function(TKey, TValue)
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
local array, len
if hasHash(TKey) then
array = ArrayDictionary
else
len = getCount
end
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EqualityComparer = define("System.EqualityComparer", function (T)
equals = Equals
else
equals = function (x, y)
return x:Equals(y)
return x:Equals(y)
end
end
else
Expand Down
16 changes: 8 additions & 8 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Linq.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ local function selectMany(source, collectionSelector, resultSelector, T)
if source == nil then throw(ArgumentNullException("source")) end
if collectionSelector == nil then throw(ArgumentNullException("collectionSelector")) end
if resultSelector == nil then throw(ArgumentNullException("resultSelector")) end
return createEnumerable(T, function()
return createEnumerable(T, function()
local element, midEn
local index = -1
return createEnumerator(T, source, function(en)
return createEnumerator(T, source, function(en)
while true do
if midEn and midEn:MoveNext() then
return true, resultSelector(element, midEn:getCurrent())
Expand All @@ -159,7 +159,7 @@ local function selectMany(source, collectionSelector, resultSelector, T)
throw(NullReferenceException())
end
element = current
end
end
end
end)
end)
Expand Down Expand Up @@ -223,7 +223,7 @@ function Enumerable.Skip(source, count)
while count > 0 and en:MoveNext() do count = count - 1 end
if count <= 0 then
if en:MoveNext() then
return true, en:getCurrent()
return true, en:getCurrent()
end
end
return false
Expand All @@ -246,8 +246,8 @@ function Enumerable.SkipWhile(source, predicate)
if not predicate(current, index) then
isSkipEnd = true
return true, current
end
else
end
else
return false
end
end
Expand All @@ -261,7 +261,7 @@ end

local IGrouping = System.defInf("System.Linq.IGrouping_2", function (TKey, TElement)
return {
base = { IEnumerable_1(TElement) }
base = { IEnumerable_1(TElement) }
}
end)

Expand Down Expand Up @@ -293,7 +293,7 @@ local Lookup = {
end,
get = function (this, key)
local grouping = getGrouping(this, key)
if grouping ~= nil then return grouping end
if grouping ~= nil then return grouping end
return Empty(this.__genericTElement__)
end,
GetCount = function (this)
Expand Down
49 changes: 41 additions & 8 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1178,10 +1178,39 @@ else
end
end

local function hash(v)
if v == nil then return 0 end
local t = type(v)
if t == "number" then
if v % 1 == 0 and v >= -2147483648 and v <= 2147483647 then
return v
end
local s = tostring(v)
return s:GetHashCode()
elseif t == "string" then
local c = 0
for i = 1, #v do
local b = v:byte(i)
c = 31 * c + b
c = System.toInt32(c)
end
return c
elseif t == "boolean" then
return v and 1 or 0
elseif t == "function" then
return addr(v, 11)
end
return addr(v)
end

System.hasHash = function (t)
return t.GetHashCode ~= hash
end

System.equalsObj = equalsObj
System.compareObj = compareObj
System.hash = hash
System.toString = toString
System.addr = addr

Object = defCls("System.Object", {
__call = new,
Expand All @@ -1190,17 +1219,13 @@ Object = defCls("System.Object", {
class = "C",
EqualsObj = equals,
ReferenceEquals = rawequal,
GetHashCode = addr,
GetHashCode = hash,
EqualsStatic = equalsObj,
GetType = false,
ToString = function(this) return this.__name__ end
})
setmetatable(Object, { __call = new })

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

ValueType = defCls("System.ValueType", {
class = "S",
default = function(T)
Expand Down Expand Up @@ -1245,7 +1270,12 @@ ValueType = defCls("System.ValueType", {
return true
end,
GetHashCode = function (this)
throw(System.NotSupportedException(this.__name__ .. " User-defined struct not support GetHashCode"), 1)
local c = 17
for _, v in pairs(this) do
c = c * 31 + hash(v)
c = System.toInt32(c)
end
return c
end
})

Expand Down Expand Up @@ -1478,7 +1508,10 @@ local Nullable = {
if this == nil then
return 0
end
return this:GetHashCode()
if type(this) == "table" then
return this:GetHashCode()
end
return hash(this)
end,
clone = function (t)
if type(t) == "table" then
Expand Down
3 changes: 0 additions & 3 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Delegate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ Delegate = System.define("System.Delegate", {
DynamicInvoke = function (this, ...)
return this(...)
end,
GetHashCode = function (this)
return System.addr(this, 11)
end,
GetType = function ()
return System.typeof(Delegate)
end,
Expand Down
38 changes: 6 additions & 32 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/Number.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local throw = System.throw
local define = System.defStc
local equals = System.equals
local zeroFn = System.zeroFn
local identityFn = System.identityFn
local hash = System.hash
local debugsetmetatable = System.debugsetmetatable

local IComparable = System.IComparable
Expand Down Expand Up @@ -103,7 +103,7 @@ local Int = define("System.Int", {
CompareTo = compareInt,
Equals = equals,
ToString = toString,
GetHashCode = identityFn,
GetHashCode = hash,
CompareToObj = function (this, v)
if v == nil then return 1 end
if type(v) ~= "number" then
Expand Down Expand Up @@ -268,14 +268,6 @@ local function equalsObj(this, v)
return equalsDouble(this, v)
end

local function getHashCode(v)
if v % 1 == 0 and v >= -2147483648 and v <= 2147483647 then
return v
end
local s = tostring(v)
return s:GetHashCode()
end

local Number = define("System.Number", {
base = inherits,
default = zeroFn,
Expand All @@ -287,7 +279,7 @@ local Number = define("System.Number", {
NegativeInfinity = negInf,
PositiveInfinity = posInf,
EqualsObj = equalsObj,
GetHashCode = getHashCode,
GetHashCode = hash,
CompareToObj = function (this, v)
if v == nil then return 1 end
if type(v) ~= "number" then
Expand Down Expand Up @@ -383,28 +375,10 @@ if not debugsetmetatable then

function System.ObjectGetHashCode(this)
if this == nil then throw(NullReferenceException()) end
local t = type(this)
if t == "number" then
return getHashCode(this)
elseif t == "boolean" then
return this and 1 or 0
elseif t == "function" then
return System.addr(this, 11)
end
return this:GetHashCode()
end

System.Nullable.GetHashCode = function (this)
if this == nil then
return 0
end
local t = type(this)
if t == "number" then
return getHashCode(this)
elseif t == "boolean" then
return this and 1 or 0
if type(this) == "table" then
return this:GetHashCode()
end
return this:GetHashCode()
return hash(this)
end

function System.ObjectToString(this)
Expand Down
10 changes: 0 additions & 10 deletions CSharp.lua/CoreSystem.Lua/CoreSystem/String.lua
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,6 @@ local CharEnumerator = System.define("System.CharEnumerator", {
end
})

local function getHashCode(this)
local c = 0
for i = 1, #this do
local b = byte(this, i)
c = System.xor(c, System.band(b, 0x7FFFFFFF))
end
return c
end

local function getEnumerator(this)
return setmetatable({ s = this, index = 1 }, CharEnumerator)
end
Expand Down Expand Up @@ -760,7 +751,6 @@ string.Contains = contains
string.CopyTo = copyTo
string.EndsWith = endsWith
string.EqualsObj = equalsObj
string.GetHashCode = getHashCode
string.GetEnumerator = getEnumerator
string.GetTypeCode = getTypeCode
string.IndexOf = indexOf
Expand Down
7 changes: 3 additions & 4 deletions CSharp.lua/CoreSystem.Lua/Sample/test.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require("strict")
local _, socket = pcall(require, "socket")

local now = 0
Expand Down Expand Up @@ -421,6 +420,6 @@ test(testIO, "IO")
--test(testAsyncForeach, "testAsyncForeach")




local dt = System.DateTime()
local o = System.Nullable.clone(dt)
print(System.Nullable.GetHashCode(dt), o:GetHashCode())
Binary file added test/__bin/lua5.3/socket.dll
Binary file not shown.

0 comments on commit 7191013

Please sign in to comment.