lua-users home
lua-l archive

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


I want to export some data into the script space, and prevent the user from
modifying it from within their script.

For example I exported the following simple test data
( C code not included )

Account.version = 1.0;

The following C code snippet makes the above readonly -

int readonly(lua_State* L)
{
   luaL_error(L,"error: read only table");
   return 0;
}

/* __newindex = readonly */
lua_getglobal(L,"Account");

luaL_newmetatable(L,"mt_Account");
lua_pushliteral(L,"__newindex");
lua_pushcfunction(L,readonly);
lua_rawset(L,-3);
lua_setmetatable(L,-2);

However, I find I can still modify the value of Account.version. I only get the error message if I try and add new fields to the table. From the description in the book my function should
be called whenever lua tries to assign something to the table.

Any reason why this should be so.


Mark