lua-users home
lua-l archive

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


2017-11-02 9:41 GMT+02:00 Marc Balmer <marc@msys.ch>:
> Is it possible to create a luaL_Buffer over several calls to Lua functions?  E.g. like in the following pseudo code:
>
> d = foo.new() -- Does a luaL_buffinit() in the C code
> d:func_a() -- Adds some values to the buffer
>
> -- do a lot of other stuff
>
> d:func_b() -- Ass some more values to the buffer
>
> -- do more stuff
>
> d:flush() --  Does something with the buffer
>
> Or must all buffer operations be finished when I return from C code?

The C code must know that Lua depends on the buffer remaining intact.
The standard way of doing that is to create a full userdata whose
storage block is the new luaL_Buffer, with your functions as methods.

The userdata belongs to Lua. As long as there still is a reference to
that object alive somewhere, it should not be collected. You might
need a __gc metamethod which frees the structure to make sure it is
eventually cleaned away.