lua-users home
lua-l archive

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


On 2/25/06, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
I never felt the need for testing two types in the same code.

There are however many situations where you might need this.   It's not even about testing two userdata types.  You could be testing against a normal type or a userdata type and want to return a different error.  Or countless other situations.

> 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.  :-(

Yes, this is exactly the problem, the userdata types are not easy to check and the old function allowed more flexibility.
 

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

Yes, this is what I did.  I added a "luaL_toudata" based on the 5.1 checkudata:

LUALIB_API void* luaL_toudata (lua_State* L, int ud, const char* tname)
{
   void* p = lua_touserdata(L, ud);
   if (p)
   {
      lua_getfield(L, LUA_REGISTRYINDEX, tname);
      if (!lua_getmetatable(L, ud))
      {
         lua_pop(L, 1);
         return NULL;
      }
      else if (!lua_rawequal(L, -1, -2))
         p = NULL;

      lua_pop(L, 2);
   }

   return p;
}

--
// Chris