Hi, 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. Regards, Yegappan -- -- 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/CAAW7x7m7gXuqOwgEgwkXwSpMtTrzXnbzqvshm%3Dhm34X4JjXiow%40mail.gmail.com.
