lua-users home
lua-l archive

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


It's easy enough to create a table with non-iterable keys.
Simply put the non-iterable keys in another table, and make that the metatable.
Something like this:


static const luaL_reg banklib[] = {
   {"CurrenAccount", curAcc},
   {"CurrentPassword", curPwd},
   {NULL, NULL}
};

static const luaL_reg prot_banklib[] = {
   {"GetAdminPassword", adminPwd},
   {NULL, NULL};
};

luaL_openlib(L, "bank", banklib, 0);

luaL_openlib(L, NULL, prot_banklib, 0);

lua_pushliteral(L, "__index");
lua_pushvalue(L, -2);
lua_settable(L, -3);

lua_pushliteral(L, "__metatable");
lua_pushboolean(L, 0);
lua_settable(L, -3);

lua_setmetatable(L, -2);

-----

But what does that buy you, really? In the end, they can still call the function. They just need to know it's name.

So you could make the name itself unavailable, by using a table as a key instead of a string. Then only functions which you give the key to would be able to use it. But I think a simpler approach is to reverse the above; the administrative bank library has the protected interfaces, with the regular bank library as its __index meta. Then you give the admin table to functions you want to have access to the admin library, and the regular one to everyone else.

On 30-May-05, at 2:23 AM, Stukov wrote:

Hello Edwin,

EEM> Mayber you can check out Programming in Lua
EEM> and read the part on object orientation:

EEM> http://www.lua.org/pil/16.html

It's a case if class privacy.
I need 'interface' privacy.

Example:

somecode.c ----------

  lua_State* L;

  static const luaL_reg banklib[] = {
         {"CurrenAccount", curAcc},
         {"CurrentPassword", curPwd},
         {"GetAdminPassword", adminPwd},
         {NULL, NULL}
  };

  luaL_openlib(L, "bank", banklib, 0);

----------------

script.lua      ------------

    for line in pairs(bank) do
      print(line)
    end

------------



output:
       CurrenAccount
       CurrentPassword
       GetAdminPassword

But I want to hide 'GetAdminPassword' from ENUMERATING.

Of course, if user knows that 'bank' table has method 'GetAdminPassword', he
can call it.

--
Best regards,
Stukov
mailto:Stukov@gala.net