lua-users home
lua-l archive

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


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