@@ -111,3 +111,64 @@ func test_dont_unpack_other_state() -> bool:
111111 var t_type = other_state .do_string ("return type(t)" )
112112 assert (t_type == "userdata" , "Table should remain as Variant when passed to another LuaState" )
113113 return true
114+
115+
116+ func test_setgetmetatable () -> bool :
117+ var table = lua_state .create_table ()
118+ assert (table .get_metatable () == null )
119+ assert (table .get_metatable () == lua_state .globals .getmetatable .invoke (table ))
120+
121+ var metatable = lua_state .create_table ()
122+ table .set_metatable (metatable )
123+ assert (table .get_metatable () == metatable )
124+ assert (table .get_metatable () == lua_state .globals .getmetatable .invoke (table ))
125+
126+ table .set_metatable (null )
127+ assert (table .get_metatable () == null )
128+ assert (table .get_metatable () == lua_state .globals .getmetatable .invoke (table ))
129+ 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
0 commit comments