[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: preventing a library from being modified
- From: Coda Highland <chighland@...>
- Date: Thu, 23 Mar 2017 13:00:17 -0700
On Thu, Mar 23, 2017 at 12:58 PM, pierre <pierre.saunier@ppmodeler.com> wrote:
> Hi,
>
> I am trying to protect a library from being modified... but without success.
>
> Basically, I am creating a newindex function:
> static int luaA_library_newindex(lua_State *L)
> {
> lua_pushstring(L, "library: package cannot be modified");
> lua_error(L);
> return 0;
> }
>
> then, I am creating a metatable, assign it the the library (a table) and set
> its __newindex:
> int luaA_open_library(lua_State *L)
> {
> luaL_newmetatable(L, "library");
>
> lua_pushstring(L, "__index");
> lua_pushvalue(L, -2); // pushes the metatable
> lua_settable(L, -3); // metatable.__index = metatable
>
> lua_pushstring(L, "__newindex");
> lua_pushcfunction(L, luaA_library_newindex);
> lua_settable(L, -3); // metatable.__newindex = func
>
> luaL_openlib(L, "library", luaA_library_f, 0);
>
> luaL_getmetatable(L, "library");
> lua_setmetatable(L, -2); // library.metatable = metatable
> }
>
>
> the problem is, it is preventing to add new values to the library table but
> not to modify existing ones...
> How can I prevent to modify existing entries?
>
>
> Thanks,
> Craouette
Create a proxy table that passes through accesses to the library, so
that it doesn't itself contain any keys -- that way all writes trigger
newindex.
/s/ Adam