lua-users home
lua-l archive

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


> I'm writing a binding to libedit, and there's a function el_set with
> the following signature:
> 
> int el_set(EditLine *el, int op, ...)
> 
> When op is EL_BIND, the varargs accepted are:
> 
> const char *, ..., NULL
> 
> I'm wondering what the best practice for binding this function to Lua
> would be.  I figure I have two choices:
> 
> 1) Write function calls for a reasonable number of arguments.  Ex.
> 
> if(lua_gettop(L) == 1) {
>   el_set(el, EL_BIND, something, luaL_checkstring(L, 1), NULL);
> } else if(lua_gettop(L) == 2) {
>   el_set(el, EL_BIND, something, luaL_checkstring(L, 1), luaL_checkstring(L, 2), NULL);
> } /* etc. */
> 
> 2) Use libffi to make the call to el_set.
> 
> What would be the recommended way to do this?

Probably EL_BIND stops at the first NULL argument, no matter
how many actual arguments it has. (That is the only possibility
using vararg.h.) So, you may write simply this:

  el_set(el, EL_BIND, something,
    lua_tostring(L, 1),
    lua_tostring(L, 2),
    lua_tostring(L, 3),
    ...
    lua_tostring(L, 20),  /* accept up to 20 strings */
    NULL);

lua_tostring will return NULL at the first absent parameter and
stop the list for el_set.

(You probably will want to write a loop first to check that
all arguments are really strings and also raise an error
if there are more than 20 arguments.)

-- Roberto