I've been thinking about this for weeks now and I can't find a clean
way to do it. The problem is this:
Let's say we have a C struct like:
struct foo {
short s, *sv;
int i, *iv;
float f, *fv;
};
What I want is a good way of filling out this struct from within
Lua. I have thought of two approaches.
1) Filling out a normal lua table with the values (like: foo = { s =
2; sv = {1,2,3}; i = 3; ...}) then passing it to a C function to
move the data to the struct.
2) Creating a suitable lua object (table or userdata with newindex
metamethods) and assigning values to it via lua(like: foo =
struct(); foo.s = 2;foo.sv = {2,3,4};...)
Method 2 seems more natural and is more convenient for me, but the
biggest problem with both approaches is the difference between lua
and C types. Forcing all numbers in C to be of type lua_Number
solves the problem and although scalars can be easily cast, I still
need vectors of other types(like unsigned short) so this isn't an
option.
Now that I think about it actually method 2 is harder than I
thought. I was thinking of creating, via C, a lua table like:
foo = {
s = light userdata pointer to foo.s;
sv = light userdata pointer to foo.sv;
...
}
and then set a __newindex metamethod wich gets the pointer and
writes the data to it. But this way the metamethod won't even
trigger. Damn it! Things like that should be easy with extension
languages. Does anyone have any good ideas, I'm getting desperate.
Dimitris