Recursive read-only tables

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Recursive read-only tables

Greg Falcon
I am working on a multi-user Lua project, and needed a way to provide
access control to tables, so they can be provided on a read-only basis
to users without write privileges.  The simple implementation, using
__index to point to the protected table, leaves much to be desired in
terms of iteration and nested data.

I've posted my work on the wiki at
http://lua-users.org/wiki/RecursiveReadOnlyTables .  I'd really
appreciate any feedback, improvements, or criticisms you guys can come
up with.  And if this code proves useful to others, so much the
better.

The short version is that each weak table gets its own metatable.  The
__index and __pairs methods are both generated as closures over the
protected table.  __index and __pairs both wrap any read/write tables
escaping it as read-only as well.

Greg F