I have this C struct:
typedef struct { double x, y; } point_t;
and I need to send a point_t
variable to Lua and change its values and then return it to C.
The first problem that I have is to cast it in Lua. How can it be done ?
main.c
point_t ponto;
ponto.x = 0; ponto.y = 0;
lua_getglobal(L, "jit"); //Get variavel with function
lua_pushlightuserdata(L, &ponto);
lua_pcall(L, 1, 1, 0);
point_t *pontop = (point_t*)lua_touserdata(L, -1);
test.lua
jit = function(num)
local ffi = require("ffi")
ffi.cdef[[
typedef struct { double x, y; } point_t;
]]
local point
local mt = {}
point = ffi.metatype("point_t", mt)
local a = point(3, 4)
print(a.x, a.y)
b = ffi.cast("point_t*",num)
b.x = 10
b.y = 20
return b
end
The local variable a
is created with point_t
type, how can I cast b
to use it like a
?