1 Dec 1997 11:06
Re: access to lua arrays from within C code
Unknown <unknown <at> unknown.invalid>
1997-12-01 10:06:31 GMT
1997-12-01 10:06:31 GMT
>I have a C function that expects char *argv[] as an argument.
>The natural representation for argv in Lua would be as a table
>with elements 1, 2, 3, etc.
>
>What is the recommended method of getting access to the array elements
>from within my C code?
The easiest solution is something like:
/* CAUTION: untested code ahead */
lua_Object t=lua_getparam(1);
for (i=0; i<N; i++)
{
lua_Object v;
lua_beginblock();
lua_pushobject(t);
lua_pushnumber(i);
v=lua_gettable()
if (v==LUA_NOOBJECT || lua_isnil(v)) break;
argv[i]=lua_getnumber(v);
lua_endblock();
}
argc=i;
myfunction(argc,argv);
Another solution is to use "call" in lua:
(Continue reading)
However, I'm not looking for easy answers here.
Making all variables global by default was a decision taken in the very early
days of the design.
The rationale is that Lua is designed to be used as a configuration language,
where end users will hardly ever write their own functions.
Now imagine the mess of having to write
global fgcolor="red"
global bgcolor="white"
global tolerance=0.001
This seems quite verbose and useless, from the point of view of the end user.
So the decision of having globals by default makes sense to us
and
we don't feel we need to change it.
Moreover, we cannot change it without breaking *lots* of Lua code.
>While explaining Lua to some folks for whom I wrote
>a little preprocessor using Lua, I realized that explaining
RSS Feed