lua-users home
lua-l archive

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


Dive Florida wrote:
> Could you please tell me how to write a non-throwing function
> luaL_isudata? I'm thinking about using:
> 
> void luaL_checktype (lua_State *L, int narg, int t);

Here is an implementation based on luaL_checkudata:

LUALIB_API int luaL_isudata (lua_State *L, int ud, const char *tname) {
  if (lua_isuserdata(L, ud)) {  /* value is a userdata? */
    if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */
      int equal;
      lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct
metatable */
      equal = lua_rawequal(L, -1, -2);  /* does it have the correct mt?
*/
      lua_pop(L, 2);  /* remove both metatables */
      return equal;
    }
  }
  return 0; /* else false */
}

Also please send plain text messages to the mailing list, some email
software (including mine) have trouble dealing with HTML ones. See last
paragraph of http://www.lua.org/lua-l.html.