Lua 中的 userdata 和 lightuserdata 是什么? [英] What is userdata and lightuserdata in Lua?

查看:57
本文介绍了Lua 中的 userdata 和 lightuserdata 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Lua 中的 userdata 和 lightuserdata 是什么?
  • 我在哪里需要它?

一段时间以来,我一直在努力解决这个问题,但似乎找不到任何我真正理解的教程/解释.

I have been trying to wrap my head around it for some time now, but I can't seem to find any tutorials/explanations that I really understand.

为什么需要它们,为什么不能直接将 C 函数绑定到 Lua 元表?

Why do you need them, why can't you just directly bind C functions to Lua metatables?

推荐答案

用户数据是任意大小和内容的垃圾收集值.您从 C API 创建一个,使用 lua_newuserdata(),它创建并将其压入堆栈,并为您提供一个指向其内容的指针,以根据您在 C 中认为合适的方式进行初始化.

A userdata is a garbage-collected value of an arbitrary size and content. You create one from the C API, with lua_newuserdata(), which creates and pushes it on the stack and gives you a pointer to its content to initialize as you see fit from C.

它与调用 malloc() 非常相似.与 malloc() 的一个关键区别在于,您永远不需要调用 free(),而只需让对它的最后一个引用消失,垃圾收集器就会回收它最终存储.

It is very comparable to calling malloc(). A key distinction from malloc() is that you never need to call free(), rather you just allow the last reference to it to evaporate and the garbage collector will reclaim its storage eventually.

它们最适用于保存对 C 有用但必须从 Lua 管理的数据.它们支持单独的元表,这是允许将 C 或 C++ 对象绑定到 Lua 的关键特性.您只需使用 C 语言编写的访问、修改和/或使用用户数据内容的方法填充其元表,结果是一个可从 Lua 访问的对象.一个很好的例子是 io,它将 C FILE * 指针存储在用户数据中,并提供实现熟悉的readwrite 和类似方法的绑定.通过实现 __gc 元方法,io 库确保其 file 对象之一关闭关联的 FILE *何时收集.

They are most useful for holding data that is useful from C, but which must be managed from Lua. They support individual metatables, which are the key feature that allows binding C or C++ objects to Lua. You simply populate its metatable with methods written in C that access, modify, and/or use the content of the userdata, and the result is an object that is accessible from Lua. A good example of this is the io library, which stores C FILE * pointers in userdata, and provides bindings that implement the familiar read, write and similar methods. By implementing an __gc metamethod, the io library makes sure that one of its file objects closes the associated FILE * when it is collected.

轻量级用户数据是你在 Lua 中将指向某事物的指针表示为值的方式.您可以通过调用 lua_pushlightuserdata() 创建一个与作为其值的指针.它们由 Lua 管理,就像数字一样.当您需要以可以在 Lua 中传递名称的方式命名 C 对象时,它们很有用,但该对象的生命周期不受 Lua 管理.就像数字具有相同的值时相等一样,light userdata 持有相同的指针时比较相等.就像数字一样,只要它们在堆栈上或存储在变量中,它们就存在,并且它们没有单独的元表,也不会被垃圾收集.

A light userdata is how you represent a pointer to something as a value in Lua. You create one by calling lua_pushlightuserdata() with the pointer that is its value. They are managed by Lua much the same way that a number is. They are useful when you need to name a C object in a way that the name can be passed around within Lua, but the object's lifetime is not managed by Lua. Like numbers are equal when they have the same value, light userdata compare equal when they hold the same pointer. Like numbers, they exist as long as they are on the stack or stored in a variable, and they do not have individual metatables and they are not garbage collected.

这篇关于Lua 中的 userdata 和 lightuserdata 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆