lua-users home
lua-l archive

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


> No, you are right but at this point:
> 
> int x_or_y() {
> if (lual_checkudata(L,i,"X")) {
>   /* ... */
> } else if (lual_checkudata(L,i,"Y")) {
>   /* ... */
> } else {
>   /* ... */
> }
> }
> cannot be done anymore.

I never felt the need for testing two types in the same code.

> This now has to be done in a *VERY* cumbersome way either declaring
> handlers for and doing pcalls for every check or adding a "__type"
> index to the metatable and test it. Neither one is a simple change,
> both are complex changes.  :-(

Or you can copy luaL_checkudata from lauxlib.c and adapt it to your needs.
I guess the code below would work for you. It's a one-line change:

LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {
  void *p = lua_touserdata(L, ud);
  lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */
  if (p == NULL || !lua_getmetatable(L, ud) || !lua_rawequal(L, -1, -2))
    p=NULL;
  lua_pop(L, 2);  /* remove both metatables */
  return p;
}

--lhf