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

查看:946
本文介绍了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?

推荐答案

userdata是任意大小和内容的垃圾回收值.您可以使用 lua_newuserdata() 从C API创建一个并将其压入堆栈,并为您提供一个指向其内容的指针,以根据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管理时,它们很有用.就像数字具有相同的值时它们相等一样,轻量级的用户数据在拥有相同的指针时也具有相等的效果.像数字一样,它们只要存在于堆栈中或存储在变量中就存在,并且它们没有单独的元表,也不会被垃圾收集.

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天全站免登陆