lua-users home
lua-l archive

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


On Mar 22, 2010, at 4:44 AM, Ignacio Burgueño wrote:

 

   // hide metatable from Lua getmetatable()
   lua_pushvalue(L, methods);
   set(L, metatable, "__metatable");<--- metatable is index of a new metatable


The code above is what is preventing you from adding metamethods in Lua. Lunar lets you add plain methods in Lua, but not metamethods. That code is returning the methods table when you call getmetatable.

This is also why

getmetatable(bot:getLoc()).__mul(bot:getLoc(),2)

works. Thanks to the __metatable entry it is essentially the same as:

bot:getLoc().__mul(bot:getLoc(),2)

Which is then essentially the same as:

bot:getLoc():__mul(2)

But the operator* code does not call the Lua getmetatable -- specifically because it doesn't want to get fouled by metatable hiding -- and hence it doesn't look in the methods table.

What is probably needed is for the methods table created by Lunar to have a __newindex metamethod in its metatable which would recognize certain keys (e.g., "__mul") and store a copy of the value into the metatable as well as the methods table.

Mark

P.S. Why does Lua have this __metatable entry? Presumably so that one can protect metatables from access by untrusted code. This also points out why the circular via __index trick for metatables is a bad idea if you care about information hiding since the metatable for the object is then available as obj.__index.