[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: C type conversion
- From: Asko Kauppi <askok@...>
- Date: Sat, 16 Dec 2006 00:54:34 +0200
If I understood your question right, I would normally either use a
table or a userdata with 'getindex' method, so that the struct fields
can be read individually from Lua, without converting the whole
structure (into a table).
My usage varies, about 50-50 between these approaches. Often, both
will do the job.
You might ask things s.a.
- how many such objects do I have
- what is the lifespan, are they tossed around (good for userdata) or
created ad hoc by Lua code as well (good for tables)
- are all fields needed (tables) or just some (userdata)
This should lead you on the right tracks, I hope.
Also, if you want to limit access (i.e. not allow any additional
fields to be carried) then userdata. If you _wish_ additional fields
to be carried, then table.
-asko
Chris kirjoitti 15.12.2006 kello 23.05:
Is it generally better to perform type conversion before calling into
Lua or after?
For example, lets say I have some custom C structure and I want to get
it into Lua. I'm already in the C code and I'm going to be calling a
function in Lua. Is it better to convert the type into a Lua type
and pass that into the lua_call() or would it be better to pass a raw
pointer lightuserdata into Lua and then from within Lua call a C
function to convert the type?
Converting after calling into Lua seems to give a bit more safety
because you're moving your call into Lua as soon as possible.
However, the type needs to be converted from a C function anyway so it
seems a little cleaner to convert it before you call into Lua. Either
method will work but I'm wondering if anyone has had experiences where
one method was better for some reason or another.
CR