luaL_Buffer use question

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

luaL_Buffer use question

Gunnar Zötl
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

Reply | Threaded
Open this post in threaded view
|

Re: luaL_Buffer use question

Roberto Ierusalimschy
> 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.

It is safe to "abandon" a buffer (that is, to finish a function
that is using a buffer without pushing its contents).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: luaL_Buffer use question

Gunnar Zötl
Am 2017-03-14 15:25, schrieb Roberto Ierusalimschy:

> It is safe to "abandon" a buffer (that is, to finish a function
> that is using a buffer without pushing its contents).

Excellent, thanks!

Gunnar

Reply | Threaded
Open this post in threaded view
|

Re: luaL_Buffer use question

Duncan Cross
In reply to this post by Gunnar Zötl
On Tue, Mar 14, 2017 at 1:07 PM,  <[hidden email]> 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.

-Duncan