lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

While playing around I found a weird behavior with metatables. I did not find notes about it in the manual so I guess this is a bug.


meta={}
setmetatable(meta, meta)
meta.__newindex=function(t, key, value) print("set", t, key, value) end
proxy = setmetatable({}, meta)


Then if you type:
	proxy.test = 42
it does not call the metamethod __newindex().


I tested it against official Lua 5.1.4 release and against 5.1.4 + all official patches, it has the same behavior.

Interestingly
	1) if you omit the second line: setmetatable(meta, meta), it works as expected (__newindex() is indeed called):
		meta={}
		meta.__newindex=function(t, key, value) print("set", t, key, value) end
		proxy = setmetatable({}, meta)

		proxy.test = 42
	--> 	set     table: 0x23ba130        test    42


	2) if you first call meta.test=42 and then proxy.test=42 it also work as expected. (it needs warm up:) )
		meta={}
		setmetatable(meta, meta)
		meta.__newindex=function(t, key, value) print("set", t, key, value) end
		proxy = setmetatable({}, meta)

		meta.test=42
	-->	set     table: 0x1aaa550        test    42
		
		proxy.test=42
	-->	set     table: 0x1aab3d0        test    42

C.