[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Testing 5.1, ouch
- From: Chris <coderight@...>
- Date: Sat, 25 Feb 2006 07:25:42 -0500
On 2/25/06, Luiz Henrique de Figueiredo <firstname.lastname@example.org> 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);
lua_getfield(L, LUA_REGISTRYINDEX, tname);
if (!lua_getmetatable(L, ud))
else if (!lua_rawequal(L, -1, -2))
p = NULL;