lua-users home
lua-l archive

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

PIL 2, p. 171 points to the equivalence of

(1) table.insert(t,line)


(2) t[#t+1] = line.

But with metatables these two differ: you can block
access to a read-only table for (2), but not for (1).

I didn't find documentation of the metatable ignorance of table.insert/remove in the printed reference manual.

Mark Hamburg had noted this before - without getting responses on the list - in

"A further issue with either approach is that table.insert, etc. all use raw access and hence don't go through the write function. One probably wouldn't want them to in general, but they should perhaps look for metamethods on the table that provide appropriate replacements."

Question: Shouldn't table.insert/remove respect metatables (__index, __newindex) for reasons of symmetry and principle-of-least-surprise?

[I stumbled across this in the wake of the 'secure tables' discussion,
where a few loopholes to table security emerged -
get/setmetatable can be easily plugged by __metatable (Roberto),
rawset/get are a bit more difficult but workarounds are not impossible (Diego) and they could be removed from Lua for special purposes. The above is a new one, I believe. Of course, again one could remove this from a special-purpose Lua build.

As an aside, seems Mark Hamburg wanted something similar to secure tables _in the Lua core_ a while ago, seconding my initial intuitions: