lua-users home
lua-l archive

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


On Wed, Mar 23, 2011 at 9:54 PM, Alexander Gladysh <agladysh@gmail.com> wrote:
> On Thu, Mar 24, 2011 at 04:50, Luiz Henrique de Figueiredo
> <lhf@tecgraf.puc-rio.br> wrote:
>>> I want to bind to Lua a third-party argv function, taking a list of
>>> strings as input:
>
>>>     foo * bar(baz * pSelf, int argc, char ** argv, size_t * argvlen);
>
>>> The question: what is a best way write bindings for bar() in Lua C API?
>
>> I'd simply limit the size of argv (ie, argc) to say 100 or 1000 and
>> use a local char** array filled from the Lua arguments. Not the utmost
>> general but easy to write and get right and probably good enough.
>
> Good idea!
>
> Also, utmost generality makes no sense here — AFAIR, the number of
> arguments of a function in Lua is limited.
>
> So, are there some LUAI_MAXARGS constant I can use? (Couldn't find it
> from a first glance.)
One option that would be to convert the argv into a Lua array, then
you would be without limitations.

Ex:
foo * bar(baz * pSelf, int argc, char ** argv, size_t * argvlen)
{
  /* Assuming pSelf has the lua state */
  int i;
  lua_createtable(L, argc, 0);
  for (i = 0; i < argc; i++) {
    lua_pushlstring(L, argv[i], argvlen[i]);
    lua_rawseti(L, -2, i + 1);
  }
  lua_call(L, 1, 0);
  /* Figure out what foo is to return */
}


-- 
Thomas Harning Jr.