File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -127,3 +127,48 @@ func test_setgetmetatable() -> bool:
127127 assert (table .get_metatable () == null )
128128 assert (table .get_metatable () == lua_state .globals .getmetatable .invoke (table ))
129129 return true
130+
131+
132+ func test_to_dictionary_pairs () -> bool :
133+ var table = lua_state .do_string ("""
134+ local function iterate_index_then_self(t)
135+ -- iteration over `t` metatable's __index table, if there's any
136+ local mt = getmetatable(t)
137+ if mt and type(mt.__index) == "table" then
138+ for k, v in pairs(mt.__index) do
139+ coroutine.yield(k, v)
140+ end
141+ end
142+
143+ -- now iterate over `t` normally
144+ for k, v in next, t, nil do
145+ coroutine.yield(k, v)
146+ end
147+ end
148+
149+ local t = {
150+ key1 = 1,
151+ key2 = 2,
152+ }
153+ setmetatable(t, {
154+ -- Here's an example "__index" table
155+ __index = {
156+ meta_key1 = 1,
157+ meta_key2 = 2,
158+ },
159+ -- And here's the "__pairs" metamethod
160+ __pairs = function(t)
161+ return coroutine.wrap(iterate_index_then_self), t, nil
162+ end,
163+ })
164+ return t
165+ """ )
166+
167+ assert (table .to_dictionary () == {
168+ key1 = 1 ,
169+ key2 = 2 ,
170+ meta_key1 = 1 ,
171+ meta_key2 = 2 ,
172+ })
173+
174+ return true
You can’t perform that action at this time.
0 commit comments