lua-users home
lua-l archive

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

On Sun, Jun 10, 2012 at 12:28 AM, Oliver Schneider
<> wrote:
> Hi,
> at the moment I am attempting to implement a lua_Reader and I'm
> wondering whether best practices beyond the rather terse documentation
> in the manual exist?
> Basically what I'm stumbling over is the fact that the callback can be
> called x many times, so I need to keep state. I presume that's what the
> second parameter (void* data) is good for?
> My callback offers the complete buffer in the first read, simply because
> it's already in memory and I'm merely telling Lua where to find it. So I
> figure the first time around I return the pointer and the size of the
> buffer and the second time around I return NULL and the corresponding size.
> But it seems to me that it's not wise to keep the state in a static
> variable, so I was thinking of tying it to my lua_State as C user data.
> Is that the way to go or are there more appropriate methods?
> Thanks,
> // Oliver
> PS: if you have any sources for more information or even sample
> implementation you can point to, I'd appreciate if you could include those.

The auxiliary library function luaL_loadbuffer covers exactly this
use-case. Either call it directly, or consult the Lua source code to
see how it implements a reader.