Converting LuaJIT FFI structs to tables
snippet in lua

Converting LuaJIT FFI structs to tables

user8998

local ffi = require "ffi"
local reflect = require "reflect"

ffi.cdef[[typedef struct test{int x, y;}test;]]
local cd = ffi.new('test', 1, 2)

function totab(struct) 
  local t = {}
  for refct in reflect.typeof(struct):members() do
    t[refct.name] = struct[refct.name]
  end
  return t
end

local ret = totab(cd)
assert(ret.x == 1 and ret.y == 2)