luaL_checkudata with negative index

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

luaL_checkudata with negative index

Jerome Vuarand
Hi list,

In lua 5.1 luaL_checkudata no longer support negative indices because
it pushes something on the stack before evaluating its argument. I
noticed that because lua-tcc use it that way and it was not working
with 5.1. Here is a unified patch to bring back old behaviour of
luaL_checkudata (also in attached file) :

###
--- lauxlib.c    Mon Jan 16 12:42:22 2006
+++ lauxlib.c    Sun Apr  9 06:51:00 2006
@@ -123,8 +123,10 @@

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

It would be nice to put it back into lua source code.

Doub.
Reply | Threaded
Open this post in threaded view
|

Re: luaL_checkudata with negative index

Javier Guerra Giraldez
On Sunday 09 April 2006 3:04 am, Jérôme VUARAND wrote:
> Hi list,
>
> In lua 5.1 luaL_checkudata no longer support negative indices because
> it pushes something on the stack before evaluating its argument. I
> noticed that because lua-tcc use it that way and it was not working
> with 5.1. Here is a unified patch to bring back old behaviour of
> luaL_checkudata (also in attached file) :

i've already patched lua-tcc to not use negative indices.  it's in the CVS
version.

BTW, was there any good reason to remove that feature of luaL_checkudata() ?

--
Javier

attachment0 (207 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: luaL_checkudata with negative index

Mildred Ki'Lya

On Sun, 9 Apr 2006 06:46:32 -0500 Javier Guerra wrote:
> BTW, was there any good reason to remove that feature of
> luaL_checkudata() ?

Isn't it a little confusing for the user to get an error mesage like
"parameter #-1 not valid, userdata expected, got ..." ?


--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: luaL_checkudata with negative index

Jerome Vuarand
2006/4/9, Mildred <[hidden email]>:
> Isn't it a little confusing for the user to get an error mesage like
> "parameter #-1 not valid, userdata expected, got ..." ?

It is. It took me a while to understand why it was not working. But
instead of cutting the feature a little adjustment of the message
would do the trick. Maybe an alternative version of checkudata with
another argument being the message to display in case of error would
be even better. Maybe we can just see this patch as a compability fix
with Lua 5.0.2 for people using checkudata that way. It doesn't change
the new way lauxlib manages metatable, it only reorders some
instructions.

Doub.