Passing C structure pointer to lua script

I would like to know if there is a way to pass the structure pointer to a lua script, and get to its members from lua without copying (for reading and writing).

So, for example, is it possible to overwrite a c-structure element directly through its pointer?

(I use luajit)

+6
source share
3 answers

Having seen how you noted this for luajit, you can combine light user data (as mentioned by others) with FFI for direct access to structure members, see the tutorial here: http://luajit.org/ext_ffi_tutorial.html

+5
source

In addition to Tim’s answer, you can also go to bright user data. You are not getting a copy of your data on the Lua stack, all you click on Lua is a pointer.

Lua does not understand what is in this pointer, regardless of whether it points to real memory or how to access any objects in this pointer, so you will have to handle all this yourself on C. I usually send a pointer to an element in list, so if there is a risk that the record has been removed from the list, I first iterate over the list to check the pointer (not a big deal if your lists are short). To access the elements inside a pointer in Lua, you need to write get / set functions in C that you can call from Lua.

To get started, here are the entries when clicking and retrieving lightuserdata:

+6
source

The way to do this is with custom lua data. Here are some examples: link , another link .

+1
source

Source: https://habr.com/ru/post/896581/


All Articles