Code: Select all
local function serializeCData(cdata)
storeType(cdata) -- needs a stable type identifier
storeSize(cdata)
storeData(cdata)
end
local function deserializeCData(source)
local type = restoreType(source)
local size = restoreSize(source)
return ffi.cast(type .. '*', source:read(size))
end
Code: Select all
ffi.cdef [[
typedef struct {
int i;
float f;
} MyData;
]]
local MyData = ffi.typeof('MyData')
Code: Select all
local data = MyData(23, 42)
print(MyData) -- 'ctype<struct xyz>'
print(type(MyData)) -- 'cdata'
print(type(data)) -- 'cdata'
print(ffi.typeof(data)) -- 'ctype<struct xyz>'
print(data) -- 'cdata<struct xyz>: 0xbaadc0de'
'ctype<struct xyz>' can not be used, since it's not a stable identifier. It will change when the order of cdefs changes, or when a new cdef is inserted.