[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [Lua5.4] lua_toclose will close the wrong value
- From: Sean Conner <sean@...>
- Date: Tue, 21 May 2019 15:40:31 -0400
It was thus said that the Great Roberto Ierusalimschy once stated:
> > The manual states for lua_toclose():
> >
> > [...] AN INDEX MARKED AS TO-BE-CLOSED SHOULD NOT BE REMOVED FROM
> > THE STACK BY ANY OTHER FUNCTION IN THE API EXCEPT LUA_SETTOP OR
> > LUA_POP.
> >
> > [...]
> >
> > [...] It might be that moving the item to be
> > cleaned is disallowed as well and the manual doesn't mention it.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> It seems it does.
No, it wasn't removed, it was *moved*. As I stated in the email
(switching the comments to use a Forth-like stack notation):
// before -- after | stack
lua_pushinteger(L,1); // -- i | i
lua_newtable(L); // -- t | t i
luaL_newlib(L,mt); // -- t | t2 t i
lua_setmetatable(L,-2); // t2 x -- x | t i
lua_toclose(L,-1); // -- | t i
lua_remove(L,-2); // ? -- ? | t
When lua_tclose() was called, the stack looked like:
-1 table marked as to-close
-2 integer
The lua_remove(L,-2) is removing the integer, not the variable marked as
"to-close". It's *moved*, not *removed*, unless the act of "moving" is the
same as "remove, then insert".
-spc