lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


If I use the code
int iType= lua_getglobal( L, "_G");
at the beginning of this function, then iType will result in
LUA_TTABLE as expected, so I assume somehow "_G" is present already
when my Lua is running? (I invoke this function at the point of my
program, where the libraries have been loaded before, also my strbuf
library described in my other post today - and this all works fine...
just Lua does not run into this lock_new_index function).

On Thu, Oct 14, 2021 at 4:31 PM nobody <nobody+lua-list@afra-berlin.de> wrote:
>
> Have you checked what you're actually doing, on the Lua-level?
>
> > void GLOBAL_lock (lua_State *L) {
> >  luaL_newmetatable( L, "_G"));
> >  lua_pushvalue(L, -1); /* duplicate the metatable */
> >  lua_setfield(L, -2, "__index");
> >  lua_pushcfunction( L, lock_new_index);
> >  lua_setfield( L, -2, "__newindex");
> >  luaL_setmetatable( L, "_G");
> > }
>
> That's effectively a
>
> local temp = {} ; debug.getregistry()._G = temp
> temp.__index = temp
> temp.__newindex = lock_new_index
> debug.setmetatable( temp, debug.getregistry()._G )
>
> …and then you're not saving temp anywhere where you can experiment with it from Lua. You probably wanted to push the global environment at some point?
>
> -- nobody
>