rzel / kahlua

Automatically exported from code.google.com/p/kahlua
0 stars 0 forks source link

weak tables can't work #20

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
ever since r225, and most likely never ever.

see example in PIL http://www.lua.org/pil/17.html :
a = {}
b = {}
setmetatable(a, b)
b.__mode = "k"         -- now `a' has weak keys
key = {}               -- creates first key
a[key] = 1
key = {}               -- creates second key
a[key] = 2
collectgarbage()       -- forces a garbage collection cycle
for k, v in pairs(a) do print(v) end
      --> 2

since weak mode is evaluated at setmetatable(), the table never learns
about changed __mode.
metatable probably needs to be a smart table object which filters this
parameter and notifies its parent?

Original issue reported on code.google.com by matej...@gmail.com on 9 Jan 2010 at 7:50

GoogleCodeExporter commented 8 years ago
This is not a bug, it's the same behaviour as defined in the Lua reference:

http://www.lua.org/manual/5.1/manual.html#2.10.2

"After you use a table as a metatable, you should not change the value of its 
__mode
field. Otherwise, the weak behavior of the tables controlled by this metatable 
is
undefined."

Original comment by kristofer.karlsson@gmail.com on 9 Jan 2010 at 9:15