lua-users home
lua-l archive

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


On Mon, Mar 22, 2010 at 4:27 AM, Christopher Eykamp <chris@eykamp.com> wrote:

At the risk of embarrassing myself, here's how I *think* lunar works when creating a 'class' called Point.  It first creates a metatable called Point, and adds some standard methods to it (__index, __tostring, __gc, new, etc.)  It then stores this table in the registry for later use.  It is this table to which I added the __mul method.  When lunar creates a Point object (using its C++ push method), it creates a userdata representing the object, then assigns it the metatable created earlier.

That is correct.
 
 As for __index, here is the most salient bit of lunar code, from the Register function, where all this is set up:

 

   // 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. An example:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = {}
> setmetatable(a, {__metatable="hi there"})
> print(a)
table: 00717E20
> print(getmetatable(a))
hi there

So you´ll need to comment those two lines or devise some way to inject arbitrarily methods in the metatable before "sealing" it.