lua-users home
lua-l archive

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

On Tue, Mar 14, 2017 at 1:07 PM,  <> wrote:
> Hi there,
> I am currently investigating the use of a luaL_Buffer for storing data I
> read from some place until all is read an I am ready to return it as a
> string. Whilst basic usage is quite straightforward, I don't quite see what
> I am supposed to do when, during the buildup of a string with a luaL_Buffer,
> an error occurs and I am not interested in it's contents anymore and instead
> want to return something else (like nil + an error message). Can I safely
> abandon the buffer, or do I need to finalize it with luaL_pushresult() in
> any case and just pop the result off the stack? If the latter, can I somehow
> avoid the new string getting interned (for speed reasons)? This needs to
> work with lua 5.1-5.3, in case there are any differences in this regard.
> Gunnar

I believe that you can just abandon it. Under the hood, there are two
things that a luaL_Buffer uses to concatenate strings: a fixed-size
char array in the luaL_Buffer structure itself (an inline array, so it
does not need to be deallocated separately from the structure itself)
and the stack of the Lua state passed to luaL_buffinit() -- so if you
abandon the buffer, there could well be some extra string value(s)
left at the top of the stack. If all you're doing is pushing nil and
an error message to return, it shouldn't cause a problem.