[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: deny table modification
- From: "Thijs Schreijer" <thijs@...>
- Date: Fri, 21 Dec 2012 19:19:44 +0100
> >> Hello,
> >>
> >> I use from the C API a set of tables for storing function pointers in
> it.
> >> On the Lua side, I can call the function from the table, but I would
> >> like to deny any modification on the table from the Lua script.
> >
> > To prevent modifications on the table (that is the table CONTENTS),
> > you can use a proxy table, with some metamethods
> >
> >>
> >> I use a luaL_newlib and a lua_setglobal for setting my function
> >> table, but the script can set the global variable to nil or something
> >> else. Can I deny this, so that the table can not be overwritten?
> >
> > To prevent modifying the global that HOLDS the table itself, you could
> > use a similar approach on the global table (_G, _ENV, and the likes).
> > But that would have a penalty on all global lookups instead of just your
> table.
>
> For my understanding, after reading the Lua documentation: The global
> table is a "lookup table" for data in e special memory block? Will this
> memory block be prevented for write access or can each Lua script modify
> items of this table?
>
Your're looking for answers way to deep. Lua only has tables, _G and _ENV
are just like regular tables. A metatable is also just a table. So a
"lookup-table" refers to a specific use of just another table, not to some
'type' of table.
There are no special memory blocks. There are global variables and local
variables (local variables may also be accessed as up-values). When a
LuaState is first created, then creating the first thread (co-routine)
creates the global environment.
Check this post, as it might be helpful to understand how this memory stuff
works;
http://lua-users.org/lists/lua-l/2012-04/msg00880.html
putting it on the wiki is still on my to do list
Thijs