lua-users home
lua-l archive

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



On 2-Feb-07, at 4:18 PM, Sam Roberts wrote:

It looks like in lua 5.0.2, luaL_newmetatable put two entries in the
registry, one to map the tname to the metadata, and the other to map the
metadata to the tname. So this would be easier if you aren't using 5.1.

Well, that's true enough but lauxlib is not really part of core Lua.

Even if you can't control the creation of metatables, you can certainly get at them with luaL_newmetatable, so as long as you have a list of the types you're interested in (which seems a reasonable assumption), you can do something like this:

static const char* typenames[] = {
  "Zara.Sound",
  "Zara.Sight",
  "Zara.Smell",
  NULL
};

static void identify (lua_State *L, const char* names[]) {
  int i;
  for (i = 0; names[i]; ++i) {
    if (luaL_newmetatable(L, names[i])) {
      luaL_error(L, "Couldn't find the metatable for '%s'", names[i]);
    }
    lua_pushstring(L, names[i]);
    lua_setfield(L, -2, "__typename");
    lua_pop(L, 1);
  }
}

I put a fully functional but useless example at http://primero.ricilake.net/lua/zara.c