加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

从纯Lua创建新的空userdata

发布时间:2020-12-15 00:26:57 所属栏目:大数据 来源:网络整理
导读:我想我在Lua中看到了一个可以返回新用户数据的本地函数.它存在吗?是否可以从普通的Lua脚本创建自定义用户数据? 解决方法 你可能会想到newproxy 来自:http://lua-users.org/wiki/HiddenFeatures newproxy is an unsupported and undocumented function in
我想我在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的主要推动力消失了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读