[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: __call and userdata
- From: Graham Wakefield <lists@...>
- Date: Thu, 15 Mar 2007 18:37:10 -0700
Hi all,
I'm having trouble using __call in a metatable for userdata; is this
supported?
Here's a rough sketch:
print(Noise)
table.foreach(Noise, print)
noise = Noise()
output:
table: 0x7162b8
__gc function: 0x716318
__call function: 0xffd00
__index table: 0x7162b8
new function: 0x7162e0
error: attempt to call global 'Noise' (a table value)
The C++ code called to load the Noise metatable:
int Noise :: init_metatable(lua_State * L)
{
luaL_newmetatable(L, "Noise"); // create a new class metatable
// mt.__index = mt
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);
// install constructor:
lua_pushstring(L, "new");
lua_pushcfunction(L, Noise::__new);
lua_settable(L, -3);
lua_pushstring(L, "__call");
lua_pushcfunction(L, Noise::__new);
lua_settable(L, -3);
// install default destructor:
lua_pushstring(L, "__gc");
lua_pushcfunction(L, Noise::__gc);
lua_settable(L, -3);
// make metatable available globally:
lua_pushvalue(L, -1);
lua_setglobal(L, "Noise");
return 1; // leaves the metatable on the stack
}