> luaL_Buffer B{};
> lua_dump(L, buf_writer, &B, U->stripFunctions);
> // pushes dumped string on 'L', (and removes the light userdata when we
> are Lua 5.4)
> luaL_pushresult(&B);
> First, something I noticed with Lua 5.4. For all Lua 5 versions,
> documentation says that lua_dump isn't supposed to push anything on the
> stack (it reads [-0,+0,...]). However, with Lua 5.4.8, a light userdata is
> pushed on the stack. This is the placeholder pushed by luaL_buffinit, which
> will be later removed by my subsequent call to luaL_pushresult. So I
> suppose that the documentation for Lua 5.4 should be fixed accordingly.
I'm afraid you are mixing two different things. The documentation
[-0, +0,...] is for the caller of the API function, not for occasional
functions being called by it. The documentation before 5.5 was mute
about this other side---what was on the stack when lua_dump calls the
writer. The documentation of 5.5 mentions that in the Incompatibilities
Section:
The function lua_dump changed the way it keeps the stack through the
calls to the writer function. (That was not specified in previous
versions.)
> With Lua 5.5, the documentation says: "After calling luaL_pushresult
> <
https://www.lua.org/work/doc/manual.html#luaL_pushresult>, the stack is
> back to its level when the buffer was initialized, plus the final string on
> its top."
> However, my observation is that the closure that was on top of the stack
> when I called lua_dump, is removed by luaL_pushresult and replaced with the
> bytecode string. Is the error in the documentation or the implementation?
The documentation for auxiliar buffers is clear that you can only call
buffer functions when the stack is in the same level it was when you
initialized it:
You can use the stack between successive calls to buffer operations
as long as that use is balanced; that is, when you call a buffer
operation, the stack is at the same level it was immediately after
the previous buffer operation. (The only exception to this rule is
luaL_addvalue.)
Are you sure you are keeping that true through all the life of the buffer,
in particular when you call luaL_pushresult?
The documentation of lua_dump now is clear that the stack level around
the call to lua_dump and inside the calls to the writer function may not
be the same.
-- Roberto