Skip to content

Commit 90e16f6

Browse files
Fixed list and dict bugs
1 parent 48fe6fd commit 90e16f6

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

pythonlua/luainit.lua

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ local str = tostring
1414
local int = tonumber
1515

1616
local function len(t)
17+
if type(t._data) == "table" then
18+
return #t._data
19+
end
20+
1721
return #t
1822
end
1923

@@ -43,18 +47,20 @@ local function range(from, to, step)
4347
end
4448

4549
local function list(t)
50+
local result = {}
51+
4652
local methods = {}
4753

4854
methods.append = function(value)
49-
table.insert(t, value)
55+
table.insert(result._data, value)
5056
end
5157

5258
local iterator_index = nil
5359

54-
setmetatable(t, {
60+
setmetatable(result, {
5561
__index = function(self, index)
5662
if type(index) == "number" and index < 0 then
57-
return rawget(t, #t + index + 1)
63+
return rawget(result._data, #result._data + index + 1)
5864
end
5965

6066
return methods[index]
@@ -65,22 +71,29 @@ local function list(t)
6571
end
6672

6773
local v = nil
68-
iterator_index, v = next(t, iterator_index)
74+
iterator_index, v = next(result._data, iterator_index)
6975

7076
return v
7177
end,
7278
})
7379

74-
t.is_list = true
80+
result.is_list = true
81+
82+
result._data = {}
83+
for _, v in ipairs(t) do
84+
table.insert(result._data, v)
85+
end
7586

76-
return t
87+
return result
7788
end
7889

7990
function dict(t)
91+
local result = {}
92+
8093
local methods = {}
8194

8295
methods.items = function()
83-
return pairs(t)
96+
return pairs(result._data)
8497
end
8598

8699
local key_index = nil
@@ -92,15 +105,20 @@ function dict(t)
92105
key_index = nil
93106
end
94107

95-
key_index, _ = next(t, key_index)
108+
key_index, _ = next(result._data, key_index)
96109

97110
return key_index
98111
end,
99112
})
100113

101-
t.is_dict = true
114+
result.is_dict = true
115+
116+
result._data = {}
117+
for k, v in pairs(t) do
118+
result._data[k] = v
119+
end
102120

103-
return t
121+
return result
104122
end
105123

106124
function enumerate(t, start)

0 commit comments

Comments
 (0)