如何使用LuaJIT FFI创建指向现有数据的指针? [英] How can I create a pointer to existing data using the LuaJIT FFI?

查看:312
本文介绍了如何使用LuaJIT FFI创建指向现有数据的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有使用LuaJIT FFI创建指针的示例,但是其中大多数没有指向现有数据.这样的一个例子在这里: 如何通过指向LuaJIT ffi的指针用作out参数?

I know there are examples of creating pointers using the LuaJIT FFI, but most of these aren't pointed to existing data. One such example of this is here: How to pass a pointer to LuaJIT ffi to be used as out argument?

我无法成功完成的一件事是创建一个指向现有值的指针.为了有一个我所知道的指针类型,我必须知道我想在将来的某个时刻有一个指向它的指针,例如:

One thing that I haven't been able to successfully do is create a pointer to an existing value. In order to have a pointer type as far as I know, I have to know that I want to have a pointer point to it at some point in the future, as in:

local vao = ffi.new("GLuint[1]")
gl.GenVertexArrays(1, vao)
gl.BindVertexArray(vao[0])

在这里,我知道glGenVertexArrays需要一个指向vao的指针,因此我将其指定为GLuint [1].在C语言中,我将执行以下操作:

Here, I know that glGenVertexArrays needs a pointer to vao, so I specify it as a GLuint[1]. In C, I would be doing something like the following:

GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

在这里,我不知道是否需要指向vao的指针,因此我可以正常指定它.

Here, I have no idea that I'll be needing a pointer to vao, so I can just specify it normally.

换句话说,有没有办法获取现有值的地址或创建指向现有值的指针?在创建值之前,我是否必须预见将要使用的值?

In other words, is there a way to get the address of, or create a pointer to, an existing value? Do I have to foresee what I'll be doing with the value before I create it?

谢谢!

推荐答案

无法在FFI中获取指向cdata对象的指针.

There's no way to get a pointer to a cdata object in the FFI.

我记得在LuaJIT邮件列表中读到,尽管我在档案中找不到确切的消息,但这样做是有意进行某些优化工作的.

I remember reading in the LuaJIT mailing list that this was done intentionally for some optimizations to work, though I can't find the exact message in the archive.

到目前为止,我不需要获取cdata对象的指针. LuaJIT通过引用引用cdata(类似于表),而type[1]技巧适用于out参数.

So far, I haven't had a need for getting the pointer of a cdata object; LuaJIT refers to cdata by reference (similar to tables) and the type[1] trick works for out parameters.

这篇关于如何使用LuaJIT FFI创建指向现有数据的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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