Skip to content

Commit 247f08f

Browse files
committed
make List compatible CJson
1 parent f61cadc commit 247f08f

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

CSharp.lua/CoreSystem.Lua/CoreSystem/Array.lua

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ local null = {}
5555
local arrayEnumerator
5656
local arrayFromTable
5757

58+
local versions = setmetatable({}, { __mode = "k" })
59+
5860
local function throwFailedVersion()
5961
throw(InvalidOperationException("Collection was modified; enumeration operation may not execute."))
6062
end
@@ -87,9 +89,9 @@ local function unWrap(v)
8789
end
8890

8991
local function ipairs(t)
90-
local version = t.version
92+
local version = versions[t]
9193
return function (t, i)
92-
if version ~= t.version then
94+
if version ~= versions[t] then
9395
throwFailedVersion()
9496
end
9597
local v = t[i]
@@ -169,13 +171,13 @@ local function set(t, index, v)
169171
throw(ArgumentOutOfRangeException("index"))
170172
end
171173
t[index] = v == nil and null or v
172-
t.version = t.version + 1
174+
versions[t] = (versions[t] or 0) + 1
173175
end
174176

175177
local function add(t, v)
176178
local n = #t
177179
t[n + 1] = v == nil and null or v
178-
t.version = t.version + 1
180+
versions[t] = (versions[t] or 0) + 1
179181
return n
180182
end
181183

@@ -190,7 +192,7 @@ local function addRange(t, collection)
190192
count = count + 1
191193
end
192194
end
193-
t.version = t.version + 1
195+
versions[t] = (versions[t] or 0) + 1
194196
end
195197

196198
local function unset()
@@ -313,7 +315,7 @@ local function removeRange(t, index, count)
313315
tmove(t, index + count + 1, n, index + 1)
314316
end
315317
fill(t, n - count + 1, n, nil)
316-
t.version = t.version + 1
318+
versions[t] = (versions[t] or 0) + 1
317319
end
318320
end
319321

@@ -356,7 +358,7 @@ end
356358
local function sort(t, comparer)
357359
if #t > 1 then
358360
tsort(t, getComp(t, comparer))
359-
t.version = t.version + 1
361+
versions[t] = (versions[t] or 0) + 1
360362
end
361363
end
362364

@@ -373,7 +375,7 @@ end, {
373375
end,
374376
MoveNext = function (this)
375377
local t = this.list
376-
if this.version ~= t.version then
378+
if this.version ~= versions[t] then
377379
throwFailedVersion()
378380
end
379381
local index = this.index
@@ -394,7 +396,7 @@ end, {
394396

395397
arrayEnumerator = function (t, T)
396398
if not T then T = t.__genericT__ end
397-
return setmetatable({ list = t, index = 1, version = t.version, currnet = T:default() }, ArrayEnumerator(T))
399+
return setmetatable({ list = t, index = 1, version = versions[t], currnet = T:default() }, ArrayEnumerator(T))
398400
end
399401

400402
local ArrayReverseEnumerator = define("System.ArrayReverseEnumerator", function (T)
@@ -410,7 +412,7 @@ end, {
410412
end,
411413
MoveNext = function (this)
412414
local t = this.list
413-
if this.version ~= t.version then
415+
if this.version ~= versions[t] then
414416
throwFailedVersion()
415417
end
416418
local index = this.index
@@ -431,7 +433,7 @@ end, {
431433

432434
local function reverseEnumerator(t)
433435
local T = t.__genericT__
434-
return setmetatable({ list = t, index = #t, version = t.version, currnet = T:default() }, ArrayReverseEnumerator(T))
436+
return setmetatable({ list = t, index = #t, version = versions[t], currnet = T:default() }, ArrayReverseEnumerator(T))
435437
end
436438

437439
local function checkArrayIndex(index1, index2)
@@ -476,7 +478,7 @@ Array = {
476478
for i = 1, size do
477479
t[i] = nil
478480
end
479-
t.version = t.version + 1
481+
versions[t] = (versions[t] or 0) + 1
480482
end
481483
end,
482484
findAll = function (t, match)
@@ -494,7 +496,7 @@ Array = {
494496
throw(ArgumentOutOfRangeException("index"))
495497
end
496498
tinsert(t, index + 1, v == nil and null or v)
497-
t.version = t.version + 1
499+
versions[t] = (versions[t] or 0) + 1
498500
end,
499501
insertRange = function (t, index, collection)
500502
if collection == nil then throw(ArgumentNullException("collection")) end
@@ -521,7 +523,7 @@ Array = {
521523
tinsert(t, index, v == nil and null or v)
522524
end
523525
end
524-
t.version = t.version + 1
526+
versions[t] = (versions[t] or 0) + 1
525527
end,
526528
last = function (t)
527529
local n = #t
@@ -535,7 +537,7 @@ Array = {
535537
if #t == 0 then throw(InvalidOperationException()) end
536538
local v = t[1]
537539
tremove(t, 1)
538-
t.version = t.version + 1
540+
versions[t] = (versions[t] or 0) + 1
539541
if v ~= null then
540542
return v
541543
end
@@ -554,7 +556,7 @@ Array = {
554556
local index = indexOf(t, v)
555557
if index >= 0 then
556558
tremove(t, index + 1)
557-
t.version = t.version + 1
559+
versions[t] = (versions[t] or 0) + 1
558560
return true
559561
end
560562
return false
@@ -599,7 +601,7 @@ Array = {
599601
if v == nil then
600602
throw(ArgumentOutOfRangeException("index"))
601603
end
602-
t.version = t.version + 1
604+
versions[t] = (versions[t] or 0) + 1
603605
end,
604606
getRange = function (t, index, count)
605607
if count < 0 or index > #t - count then
@@ -609,9 +611,7 @@ Array = {
609611
tmove(t, index + 1, index + count, 1, list)
610612
return setmetatable(list, System.List(t.__genericT__))
611613
end,
612-
reverseEnumerator = function (t)
613-
return setmetatable({ list = t, index = #t, version = t.version }, ArrayReverseEnumerator)
614-
end,
614+
reverseEnumerator = reverseEnumerator,
615615
getCount = lengthFn,
616616
getSyncRoot = System.identityFn,
617617
getLongLength = lengthFn,
@@ -834,7 +834,7 @@ Array = {
834834
i = i + 1
835835
j = j - 1
836836
end
837-
t.version = t.version + 1
837+
versions[t] = (versions[t] or 0) + 1
838838
end,
839839
Sort = function (t, ...)
840840
if t == nil then throw(ArgumentNullException("array")) end
@@ -857,7 +857,7 @@ Array = {
857857
tsort(arr, comp)
858858
tmove(arr, 1, count, index + 1, t)
859859
end
860-
t.version = t.version + 1
860+
versions[t] = (versions[t] or 0) + 1
861861
end
862862
end
863863
end,

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/List.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ local falseFn = System.falseFn
1919
local lengthFn = System.lengthFn
2020
local Array = System.Array
2121

22-
local List = {
23-
version = 0,
22+
local List = {
2423
__ctor__ = Array.ctorList,
2524
getCapacity = lengthFn,
2625
getCount = lengthFn,

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Queue.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ local System = System
1818
local Array = System.Array
1919

2020
local Queue = {
21-
version = 0,
2221
__ctor__ = Array.ctorList,
2322
getCount = Array.getLength,
2423
Clear = Array.clear,

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Stack.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ local System = System
1818
local Array = System.Array
1919

2020
local Stack = {
21-
version = 0,
2221
__ctor__ = Array.ctorList,
2322
getCount = Array.getLength,
2423
Clear = Array.clear,

0 commit comments

Comments
 (0)