lua-users home
lua-l archive

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


Hi list,

I would like to accomplish the following but I'm new to Lua and a bit lost:
I have a C library that I want to use from Lua. One of the key
functions in the library has (roughly) the following signature:

int execute_cmd(cmd_t mycmd, cb_t mycallback);

The idea is that you call that function and that you provide a
callback that the library will invoke a number of times when it has
some results as it executes the command. When there are no more
results the execute_cmd() function returns.
What I ideally would like to do in Lua is similar to the
coroutines-as-iterators example from PiL2 (page 80):

for d in execute_cmd{<cmdparameters>} do
 <process results>
end

In other words: I would like the clean and simple iterator-style Lua
code but make full use of the 'streaming' behavior of the C library.
This means that in the end I need to be able to transfer the data that
I receive in that callback to Lua somehow. However PiL2 is quite
clear: you can only yield from C when it is called from Lua and the C
function ends with 'return lua_yield(L, nres);' which ofcourse doesn't
match the interface of the library.

Is there some way to achieve this? I still have an alternative: just
let the user in Lua pass a callback that I pass along to the C
callback who will call it:

execute_cmd({<cmdparameters>}, function(d) <process results> end)

but somehow I find the iterator-style more pleasing to the eye :)

Thanks,

Dirk