Pointer – How to create a pointer to existing data using Luajit FFI?

I know there are some examples of using LuaJIT FFI to create pointers, but most of them do not point to existing data. One example is:
How to pass a pointer to LuaJIT ffi to be used as out argument?

One thing I cannot do successfully is to create a pointer to an existing value. As far as I know, in order to get a pointer type, I have to know that I I want to point to it at some point in the future, such as:

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

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

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

< p>Here, I don’t know that I need a pointer to vao, so I can specify it normally.

In other words, is there a way to get the address of an existing value or create a pointer? Before creating it, do I have to foresee what I will do with this value?

Thank you!

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

< p>I remember reading in the LuaJIT mailing list that this was done deliberately for some optimization work, although I could not find the exact information in the archive.

So far, I have not needed to obtain A pointer to a cdata object; LuaJIT refers to cdata by reference (similar to a table), and the type[1] trick applies to out parameters.

I know there are some that use LuaJIT FFI to create pointers Example, but most of them do not point to existing data. One example is:
How to pass a pointer to LuaJIT ffi to be used as out argument?

I can’t succeed One thing to do is to create a pointer to an existing value. As far as I know, in order to get a pointer type, I must know that I want to point to it at some point in the future, such as:

< /p>

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

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

< pre>GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

Here, I don’t know I need a pointer to vao, so I can be normal Specify it.

In other words, is there a way to get the address of an existing value or create a pointer? Before creating it, do I have to foresee what I will do with this value?

Thank you!

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

I remember reading this on the LuaJIT mailing list It was done deliberately for some optimization work, although I could not find the exact information in the archive.

So far, I have not needed to obtain the pointer of the cdata object; LuaJIT refers to the cdata by reference (similar (In the table), the type[1] technique is applicable to out parameters.

Leave a Comment

Your email address will not be published.