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.
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.