[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: access to lua arrays from within C code
- From: lhf (Luiz Henrique de Figueiredo)
- Date: Mon, 1 Dec 1997 08:06:31 -0200
>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:
function myfunction(t)
call(actual_myfunction,t)
end
where the C function "myfunction" is registered in Lua as "actual_myfunction".
With this code in place, you can access the array elements in C as if they
were arguments:
for (i=0; i<N; i++)
{
lua_Object v;
v=lua_param(i)
if (v==LUA_NOOBJECT || lua_isnil(v)) break;
argv[i]=lua_getnumber(v);
}
Hope this helps.
--lhf