lua-users home
lua-l archive

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


On Wed 09 Jan, Peter Loveday wrote:

> Is there a way to associate a name with userdata in Lua 4.1?

 We are still thinking about the "official" way to do that (with some
support from the auxlib and baselib).

One proposal is to add a field "typename" in the mettable, and to add
an entry "typename -> mettable" into the registry. Then it is easy to
map from typenames to metatables in both directions. To create a new
userdata with a given typename, you retrive the metatable from the registry.
To test whether a given userdata has the correct typename, you can do:

  lua_geteventtable(L, udata);
  lua_getstr(L, LUA_REGISTRYINDEX, tname);
  res = lua_equal(L, -1, -2);
  lua_pop(L, 2);

(The iolib is using this scheme, but without support from auxlib.)

-- Roberto