You need to understand the difference between __index
and __newindex
, and their relationship with the current contents of the main table.
__newindex
is only called/accessed when all the following are true:
- When you are setting a value into the main table, via
tbl[index] = expr
(or equivalent syntax, liketbl.name = expr
). - When the key you are trying to set into the main table does not already exist in the main table.
The second one trips people up often. And that's your problem here, because __index
is only accessed when:
- When the key being read from the main table does not already exist in the main table.
So if you want to filter every read from and write to a table, then that table must always be empty. Therefore, those reads and writes need to go into some other table you create for each new object. So your new
function needs to create two tables: one that remains empty and one that has all the data in it.
Honestly, I wish Lua had a way to create just an empty piece of userdata that you could bind a user-defined metatable to, just to avoid these issues.