从纯Lua创建新的空userdata

我想我在Lua中看到了一个可以返回新用户数据的本地函数.它存在吗?是否可以从普通的Lua脚本创建自定义用户数据?

解决方法

你可能会想到newproxy

来自:http://lua-users.org/wiki/HiddenFeatures

newproxy is an unsupported and undocumented function in the Lua base
library. From Lua code,the setMetatable function may only be used
on objects of table type. The newproxy function circumvents that
limitation by creating a zero-size userdata and setting either a new,
empty Metatable on it or using the Metatable of another newproxy
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的主要推动力消失了.

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...