lua-users home
lua-l archive

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


Vyacheslav Egorov wrote:
Shmuel Zeigerman wrote:
You probably forgot to assign the "__index" field
of the metatable to the metatable itself:

  luaL_newmetatable(L, "LuaBook.gc_event");
  lua_pushliteral(L, "__index");
  lua_pushvalue(L, -2);         /* push metatable */
  lua_rawset(L, -3);            /* metatable.__index = metatable */

That is necessary only if you plan to use metatable as a place where Lua will look _userdata_ fields. It is a common (yet newbie-confusing) practice to store methods in metatable, instead of special separate table.

Yes, you're right.
"__gc" doesn't have to be a field of the userdatum,
it has to be a field of its metatable.

Thanks for pointing that out.
--
Shmuel