[PATCH] FFI: Add ffi.typeinfo().

  • From: Peter Colberg <peter@xxxxxxxxxxx>
  • Date: Wed, 8 Oct 2014 00:02:47 -0400

---
 src/lib_ffi.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 989fddd..cb56686 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -644,6 +644,30 @@ LJLIB_CF(ffi_offsetof)     LJLIB_REC(ffi_xof 
FF_ffi_offsetof)
   return 0;
 }
 
+LJLIB_CF(ffi_typeinfo)
+{
+  CTState *cts = ctype_cts(L);
+  CTypeID id = (CTypeID)ffi_checkint(L, 1);
+  GCtab *t;
+  if (id > 0 && id < cts->top) {
+    CType *ct = ctype_get(cts, id);
+    lua_createtable(L, 0, 4);  /* Increment hash size if fields are added. */
+    t = tabV(L->top-1);
+    setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "info")), (int32_t)ct->info);
+    if (ct->size != CTSIZE_INVALID)
+      setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "size")), 
(int32_t)ct->size);
+    if (ct->sib)
+      setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);
+    if (gcref(ct->name)) {
+      GCstr *s = gco2str(gcref(ct->name));
+      setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);
+    }
+    lj_gc_check(L);
+    return 1;
+  }
+  return 0;
+}
+
 LJLIB_CF(ffi_errno)    LJLIB_REC(.)
 {
   int err = errno;
-- 
2.1.1


--YiEDa0DAkWCtVeE4
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="test.lua"

local ffi = require("ffi")
assert(ffi.typeinfo(0) == nil)
assert(ffi.typeinfo(-1) == nil)

ffi.cdef[[
struct foo { double x, y; struct { double z; }; double w; };
]]
local cid = tonumber(ffi.typeof("struct foo"))
while cid do
  local info = ffi.typeinfo(cid)
  print(cid, info.info, info.size, info.name)
  cid = info.sib
end

--YiEDa0DAkWCtVeE4--

Other related posts:

  • » [PATCH] FFI: Add ffi.typeinfo(). - Peter Colberg