Problem w/lua_pushuserdata

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

Problem w/lua_pushuserdata

Matthias Gall
Hi @ll,

I am using lua_pushuserdata to exchange pointers between C-program and LUA-script, and I just found something weird: When I push
NULL from the C-function like this:
    CObject* O = NULL; // CObject is a class declared in my program
    // doing something here, O might remain NULL or become something else
    lua_pushuserdata (m_pLuaState, O);
    return 1;
the result of the calling LUA function - which looks similar to this: -
    result_ptr = myfunction ()
get's anything, but never "nil" when it should.

I worked around that by pushing "0" as a number, but I am still wondering why it does not work with NULL as userdata.
lua_pushuserdata _should_ work with a NULL pointer for obvious reasons. Any ideas?

Matt

--
Matthias Gall, lead programmer at sechsta sinn
http://www.sechsta-sinn.de
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Problem w/lua_pushuserdata

Edgar Toernig
Matthias Gall wrote:
> 
> When I push NULL from the C-function like this:
>[...] lua_pushuserdata (m_pLuaState, O); [...]
> get's anything, but never "nil" when it should.

Who said that a pushuserdata(NULL) gives a nil?  You're right that it is
broken in Lua 4.0 - you get a userdata object with some kind of random
pointer but expecting nil...

This patch will fix the bug:

--- src/lstring.c       Sat Apr  7 03:22:54 2001
+++ src/lstring.c       Sat Oct 30 18:49:00 2001
@@ -119,7 +119,7 @@
   ts->nexthash = NULL;
   ts->len = s;
   ts->u.d.tag = 0;
-  ts->u.d.value = (udata == NULL) ? uts+1 : udata;
+  ts->u.d.value = s ? uts+1 : udata;
   L->nblocks += sizestring(s);
  /* insert it on table */
   newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size-1));

> lua_pushuserdata _should_ work with a NULL pointer for obvious reasons.
> Any ideas?

It's simply a bug ;-)  But if you expect a nil you have to change your
code too.

Ciao, ET.

PS: Be careful when using userdata objects with the default tag...


Reply | Threaded
Open this post in threaded view
|

Re: Problem w/lua_pushuserdata

Matthias Gall
> It's simply a bug ;-)  But if you expect a nil you have to change your
> code too.

I guess I know what you mean, because I don't find a way to check for a NULL in my LUA script now.
Neither
    if function_result == nil then ...
nor
    if function_result == 0 then ...
works. But, function_result is userdata, and it has the value "0", as my debug output says ...

Could you help me again please?

> PS: Be careful when using userdata objects with the default tag...

Why?

Thanks in advance,
Matt


-- 
Matthias Gall, lead programmer at sechsta sinn
http://www.sechsta-sinn.de
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Problem w/lua_pushuserdata

Edgar Toernig
Matthias Gall wrote:
> 
> I guess I know what you mean, because I don't find a way to check for a
> NULL in my LUA script now.
>[...] 
> Could you help me again please?

Just return a nil instead of a userdata(NULL).  I.e.:

  if (x)
    lua_pushuserdata(x);
  else
    lua_pusnhil();

> > PS: Be careful when using userdata objects with the default tag...
> 
> Why?

Because there's no way to check whether the userdata objects you get are
really from you or which type of pointer they hold.

Ciao, ET.