Hi Bram, On Fri, Jul 30, 2021 at 2:54 AM Bram Moolenaar <[email protected]> wrote: > > > > On Wed, Jul 28, 2021 at 9:01 PM Martin Tournoij <[email protected]> > > wrote: > > > > > Hm, the Lua manual > > > <https://www.lua.org/manual/5.4/manual.html#luaL_Buffer> doesn't mention > > > this at all; luaL_buffinit() does use LUAL_BUFFERSIZE initially, but > > > luaL_addlstring() calls prepbuffsize() which grows it as-needed. > > > > > > I mean, if it works then it works so it's fine, but I think it was somehow > > > using the API wrong, unless I completely misunderstood things. > > > > > > > > > The string returned by lua_tolstring() is on the Lua stack > > and using the Lua buffer also modifies the stack. I think using the > > Lua buffer while holding a pointer to the value returned by > > lua_tolstring() leads to this problem. > > > > The Lua reference manual has the following: > > > > ============================================== > > Because Lua has garbage collection, there is no guarantee that the > > pointer returned by lua_tolstring will be valid after the corresponding > > Lua value is removed from the stack. > > > > During its normal operation, a string buffer uses a variable number of > > stack slots. So, while using a buffer, you cannot assume that you know > > where the top of the stack is. 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. > > ============================================== > > > > I tried using vim_strsave() to create a copy of the string returned > > by lua_tolstring(). This fixes the issue. But this string needs to be > > allocated and freed once per loop iteration. It is more efficient > > to use the grow array to store the string. > > So, another way would have been to put the pop outside of the loop, > clean up afterwards. >
The pop is used to remove the value returned by Lua 'tostring' for each argument. So the pop has to be within the loop. If it is moved outside the loop, then we need to pop multiple values. Also, based on the documentation, it looks like Lua expects the stack to be the same between successive Lua buffer operations. This means that we cannot store and use the string returned by tostring in the stack as the Lua buffer operations are performed a few times within the loop. Regards, Yegappan > > I prefer using the growarray anyway, having to carefully read Lua > documentation to understand how this works has its disadvantages, and > apparently leads to mistakes. Well, using a growarray also has this > catch, that a pointer into the data becomes invalid when the data is > reallocated. > -- -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php --- You received this message because you are subscribed to the Google Groups "vim_dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/vim_dev/CAAW7x7kYvmwj0Ae4HDmrthakfybRwOs5nRkbnmWOgPzXkqCufw%40mail.gmail.com.
