lua-users home
lua-l archive

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


Perhaps I didn't describe my problem well enough. Let me try again. 

I have various loosely defined "types" of tables. By a "type" I simply
mean that when a table is passed to certain functions, the function
processing the table expects certain keys to exist in the table. The
keys are well-known and fixed, i.e. field names. Now I have no control
(nor want to) over where the table is created, so I need to support
tables created in Lua. It just seems that if I know I'm looking for
fields "field1", "field2", etc. it's a waste to have to do a bunch of
string processing when I want to get to the well-known members in a
table passed into C. Does that make sense? Thanks.

-----Original Message-----
From: David Jones [mailto:drj@pobox.com] 
Sent: Thursday, February 21, 2002 3:23 AM
To: Multiple recipients of list
Subject: Re: Optimizing variable access from C 


In message
<BCF3A47F02AB8643BB391B0517EBD6440394ECE6@red-msg-08.redmond.corp.mi
crosoft.com>, "Curt Carpenter" writes:
> > How about doing it other way around? I.e. storing the variables in C
> and accessing them with set/get functions from Lua?
> 
> Then I have to have some sort of reverse string lookup table 
> (duplicating what Lua already does), which seems about as expensive 
> (and more complex).

Store your variable in C.  EG

float coolParm1;

For each different C type you use you'll need a new tag.

Create a lua userdata object with the appropriate tag that points to the
C
variable:

lua_pushusertag(L, &coolParm1, myFloatTag);

Bind this value to a lua global variable:

lua_setglobal(L, "coolParm1");

Define a getglobal and setglobal tag method for myFloatTag that
translates the representation in C into a representation in lua.  In the
case of float this is obvious.

Now you can access the C variable from lua transparently.

I'm sure that's what the original respondent had in mind.

Cheers,
 drj