lua-users home
lua-l archive

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


Dear Asko
I am not very engaged with lua at this time, but it seems to me we already
discussed the point ?
The main usafulness I found for userdata ( may be other usages - this is my
own point of view ) is
to make some C variables ´visible´ and ´modifiable´ from lua scripts. Which
in fact is what luasocket does. You accomplish these tasks thru the
methamethods ( you mention these later ). So, your
userdata may be a kind of ´class´ and every instance an ´object´.
But the whole thing is a lot more clear and straigthforward in lua 5.0 than
4.0
Consider
a = myluavisibleclass( )
-- this is like a contructor, and it could receive arguments ...
-- suppose your C construct is ´polimorphic´ ( mine are )
b = myluavisibleclass( "abc" )
c = myluavisibleclass( 5 )
if  c < b -- will invoke the __lt methametod, which will be a c function in
your code
             -- your function will receive the two operands, and ´decide´
whether c < b
if c == b  -- the __eq methemethod, new in 5.0

c = b       -- there is NO methamethod to process assignment ... simply, c
will ´point´ to
               -- whatever b is
c:assign( b )  -- you can provide any method you like to your ´class´ this
is done thru
                    -- the __function metamethod, which in turns must search
for the appropiate
                    -- C function to execute or trigger an error
c > 4            -- not allowed: the lvm will raise an error because the
operands are not the same
                    -- type, even if one of them is not a native one

etc
The best thing is that some of these objects are locals, they will be GCed
..
Also, you can set some global ´variables´ in your C code, and manage to set
them as global variables
in lua
if  MyGlobVar < myluavisibleclass( "A" )
then
    MyGlobVar:assign( "A" )
end
Ok. The main usefulness would be to do things like you do with globals in
C/C++.
Else, you need
a) Lots of  lua functions to get ( and set ) some states which are
maintained as globlas in your host
program.
b) ´Push´ lua globals every time you are about to call a lua chunck. If the
lua chunk may alter some
of the variables, you must check every of them explicitly in your code.
In other words:
Lots of times, it´s more ´natural´ to get/set the value of a variable than
to call a function.
The question regarding assignment ´assimetry´ was a claim I posted some
months ago, and
nobody answered. It´s not very orthogonal to use relational operators
directly, but resort to a method
for assignment.
I ´ported´ luapi to 5.0 (alpha), and added lots of userdata support to it.
Also, I have working
examples of these ´classes´, both to ´bind´ a C global to a lua global, and
to support temporary
objects. Before I could complete the doc, lua 5.0 beta was announced, and I
waited to see what
changes were needed to luapi. And now, I am very busy. If you like, I could
send you the
alpha version ´as is´ ( no completed doc ), so you can see by yourself what
I mean.
Also, I need to take a hard option. The question is: I am ready to start a
very ambitious project,
which includes  lua as an important role player. Part of this role _require_
´userdata´ binding and
exchange. So I must to decide if
1) all operations on these objects will be done thru methods ( because of
simmetry ) like
    c:lt( a ), c:le( a ), c:eq( a ), c:assign( a ) etc
2) to hack the beta source code ( lvm.c ) to allow all operators, including
assignment, to check
for a metamethod. in one of the operands, and then passing the whole
expression to that metamethod.
The only error triggered would be ( for binary operators ) if the two
operands have metamethods,
and they are not the same ( ambiguity ).
Some people answered me regarding assignment ´overloading´, telling that
being assignment a so common operation ( probably the most frequent ),
checking for a  metamethod on every side of the
expresion would be too expensive. May be, but I would like simmetry over
performance. I am not
programming games.
Tell me if you would like to see the ´5.0´ version of luapi and the userdata
test code.
Regards


----- Original Message -----
From: "Asko Kauppi" <Asko.Kauppi@fi.flextronics.com>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Tuesday, February 18, 2003 7:34 AM
Subject: Need for help...


>
> Does anyone know what 'lua_newuserdata()' really is and why / where it
> should be used?
>
> I'm doing an (experimental) gluax compatibility cover over old-style C
API.
>
> The following extract is from 'luasocket.c':
>
>     /* declare new Lua tags for used userdata values */
>     p_tags tags = (p_tags) lua_newuserdata(L, sizeof(t_tags));
>     tags->client = lua_newtag(L);
>     tags->server = lua_newtag(L);
>     tags->table = lua_newtag(L);
>     tags->udp = lua_newtag(L);
>
> 'p_tags' is simply (same source file):
>
> typedef struct t_tags {
>     int client, server, table, udp;
> } t_tags;
> typedef t_tags *p_tags;
>
> Question is, what is really the importance / need for 'lua_newuserdata()'?
> It's not documented in 4.0 PDF and in my understanding, is just a fancy
> 'alloc'. I must be missing something...?
>
> Declaration in 'luaapi.c':
>
> LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
>   TString *ts = luaS_newudata(L, size, NULL);
>   tsvalue(L->top) = ts;
>   ttype(L->top) = LUA_TUSERDATA;
>   api_incr_top(L);
>   return ts->u.d.value;
> }
>
> -ak
>
> ###########################################
> This message has been scanned by F-Secure Anti-Virus for Microsoft
Exchange.
>