[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 22:34:37 -0400
It was thus said that the Great Sean Conner once stated:
> 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*.
I've played around with the code a bit more. This code:
static int close(lua_State *L)
{
(void)L;
printf("CLOSING!\n");
return 0;
}
static const luaL_Reg mt[] =
{
{ "__close" , close } ,
{ NULL , NULL }
};
int luaopen_toclose(lua_State *L)
{
// slot 1 - string "toclose"
// slot 2 - string "./toclose"
lua_newtable(L); // slot three - table
luaL_newlib(L,mt);
lua_setmetatable(L,-2);
lua_toclose(L, -1);
lua_pushinteger(L,1); // slot four - integer
lua_insert(L,1); // stack now 1)int 2)str 3)str 4)table
return 1;
}
results in:
[spc]lucy:/tmp>~/apps/lua-5.4/lua
Lua 5.4.0 Copyright (C) 1994-2019 Lua.org, PUC-Rio
> require "toclose"
attempt to close non-closable variable '(C temporary)'
stack traceback:
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
I'm thinking the manual needs to state, "an index marked as 'to-be-closed'
should not be moved, nor removed by any other function in the API except
lua_settop() or lua_pop() ..."
-spc (Would have thought the value would be marked, not the slot ... )