UserData.

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

UserData.

Andrew Paton
I'm having trouble assigning NULL values to userdata items... are you not
supposed to assign nulls to userdata types?  If so how would I store a
pointer into a lua variable from c?


Reply | Threaded
Open this post in threaded view
|

Re: UserData.

Roberto Ierusalimschy
> I'm having trouble assigning NULL values to userdata items...

This is a bug in Lua 4.0; it was already reported by ET:

> Message-ID: <[hidden email]>
> Date: Wed, 20 Dec 2000 18:04:14 +0100
> From: Edgar Toernig <[hidden email]>
> [...]
> PS: Btw, lua_newuserdata and co are IMHO slightly broken:
> 
> > print(dostring"")  -- gives lua_pusheruserdata(L, NULL)
> userdata(0): 0x80632b0
> > print(dostring"")
> userdata(0): 0x8063448 -- and another one?!?
> 
> Maybe this change
> 
> -  ts->u.d.value = (udata == NULL) ? uts+1 : udata;
> +  ts->u.d.value = s ? uts+1 : udata;
> 
> would make a little bit more sense.  (There are other
> problems too.  Still thinking about a solution.)

(The change is in lstring.c:122)

I think his fix will solve the problem, although I don't know what
would be the "other problems". (lua_newuserdata will return NULL if you
ask for zero bytes, but that is OK.)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

RE: UserData.

Andrew Paton
Thanks this is the exact line of code and how we wanted to change it but we
weren't sure about the effects.  For now this works.

-----Original Message-----
From:	[hidden email] [[hidden email]]
On Behalf Of Roberto Ierusalimschy
Sent:	Wednesday, January 31, 2001 6:42 AM
To:	Multiple recipients of list
Subject:	Re: UserData.

> I'm having trouble assigning NULL values to userdata items...

This is a bug in Lua 4.0; it was already reported by ET:

> Message-ID: <[hidden email]>
> Date: Wed, 20 Dec 2000 18:04:14 +0100
> From: Edgar Toernig <[hidden email]>
> [...]
> PS: Btw, lua_newuserdata and co are IMHO slightly broken:
>
> > print(dostring"")  -- gives lua_pusheruserdata(L, NULL)
> userdata(0): 0x80632b0
> > print(dostring"")
> userdata(0): 0x8063448 -- and another one?!?
>
> Maybe this change
>
> -  ts->u.d.value = (udata == NULL) ? uts+1 : udata;
> +  ts->u.d.value = s ? uts+1 : udata;
>
> would make a little bit more sense.  (There are other
> problems too.  Still thinking about a solution.)

(The change is in lstring.c:122)

I think his fix will solve the problem, although I don't know what
would be the "other problems". (lua_newuserdata will return NULL if you
ask for zero bytes, but that is OK.)

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: UserData.

Edgar Toernig
In reply to this post by Roberto Ierusalimschy
Hi,

Roberto Ierusalimschy wrote:
> 
> From: Edgar Toernig <[hidden email]>
> > Maybe this change
> >
> > -  ts->u.d.value = (udata == NULL) ? uts+1 : udata;
> > +  ts->u.d.value = s ? uts+1 : udata;
> >
> > would make a little bit more sense.  (There are other
> > problems too.  Still thinking about a solution.)
> 
> (The change is in lstring.c:122)
> 
> I think his fix will solve the problem, although I don't know what
> would be the "other problems".

For userdata?  Well, the LUA_ANYTAG stuff is IMHO bogus.  You get
some random objects back.  (I've simply removed it from Sol).  The
other thing is with lua_settag.  It's used to change the tag of
already existing userdata objects.  The only valid way to use this
is IMHO to invalidate a userdata object.  Any other usage may
generate duplicate objects with same udata/tag value and you no
longer know which one you get with lua_pushusertag.  So I made
an invalidate function (that's a little bit trivial at the moment)
and restricted the use of anonymous userdata objects (= userdata
with default tag) to valid pointers.

But the "other problems" were not only about userdata.  I was
twiddling with some other things at that time too.  Some user
function are pretty fragile.  Sometimes you may get unpredictable
results.  I.e. tinsert(x,foo()): if foo returns more than one value
you either get an error or a broken list.  Or dostring/dofile/call:
you don't know if the results come from the executed objects or from
the error handling.

Ciao, ET.