解决方法
你可能会想到newproxy
来自:http://lua-users.org/wiki/HiddenFeatures
newproxy
is an unsupported and undocumented function in the Lua base
library. From Lua code,thesetMetatable
function may only be used
on objects of table type. Thenewproxy
function circumvents that
limitation by creating a zero-size userdata and setting either a new,
empty Metatable on it or using the Metatable of anothernewproxy
instance. We are then free to modify the Metatable from Lua. This is
the only way to create a proxy object from Lua which honors certain
Metamethods,such as__len
.
它对于__gc元方法也很有用,因为当newproxy实例变为空闲时,它会获得回调.
此功能在Lua 5.1中出现,但在5.2中已删除.在Lua 5.2中,__ gc元方法可以设置在零大小的表上,因此newproxy的主要推动力消失了.