src/lib_ffi.c - luajit-2.0-src

Functions defined

Macros defined

Source code

  1. /*
  2. ** FFI library.
  3. ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
  4. */

  5. #define lib_ffi_c
  6. #define LUA_LIB

  7. #include <errno.h>

  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lualib.h"

  11. #include "lj_obj.h"

  12. #if LJ_HASFFI

  13. #include "lj_gc.h"
  14. #include "lj_err.h"
  15. #include "lj_str.h"
  16. #include "lj_tab.h"
  17. #include "lj_meta.h"
  18. #include "lj_ctype.h"
  19. #include "lj_cparse.h"
  20. #include "lj_cdata.h"
  21. #include "lj_cconv.h"
  22. #include "lj_carith.h"
  23. #include "lj_ccall.h"
  24. #include "lj_ccallback.h"
  25. #include "lj_clib.h"
  26. #include "lj_strfmt.h"
  27. #include "lj_ff.h"
  28. #include "lj_lib.h"

  29. /* -- C type checks ------------------------------------------------------- */

  30. /* Check first argument for a C type and returns its ID. */
  31. static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param)
  32. {
  33.   TValue *o = L->base;
  34.   if (!(o < L->top)) {
  35.   err_argtype:
  36.     lj_err_argtype(L, 1, "C type");
  37.   }
  38.   if (tvisstr(o)) {  /* Parse an abstract C type declaration. */
  39.     GCstr *s = strV(o);
  40.     CPState cp;
  41.     int errcode;
  42.     cp.L = L;
  43.     cp.cts = cts;
  44.     cp.srcname = strdata(s);
  45.     cp.p = strdata(s);
  46.     cp.param = param;
  47.     cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
  48.     errcode = lj_cparse(&cp);
  49.     if (errcode) lj_err_throw(L, errcode);  /* Propagate errors. */
  50.     return cp.val.id;
  51.   } else {
  52.     GCcdata *cd;
  53.     if (!tviscdata(o)) goto err_argtype;
  54.     if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM);
  55.     cd = cdataV(o);
  56.     return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid;
  57.   }
  58. }

  59. /* Check argument for C data and return it. */
  60. static GCcdata *ffi_checkcdata(lua_State *L, int narg)
  61. {
  62.   TValue *o = L->base + narg-1;
  63.   if (!(o < L->top && tviscdata(o)))
  64.     lj_err_argt(L, narg, LUA_TCDATA);
  65.   return cdataV(o);
  66. }

  67. /* Convert argument to C pointer. */
  68. static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
  69. {
  70.   CTState *cts = ctype_cts(L);
  71.   TValue *o = L->base + narg-1;
  72.   void *p;
  73.   if (o >= L->top)
  74.     lj_err_arg(L, narg, LJ_ERR_NOVAL);
  75.   lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg));
  76.   return p;
  77. }

  78. /* Convert argument to int32_t. */
  79. static int32_t ffi_checkint(lua_State *L, int narg)
  80. {
  81.   CTState *cts = ctype_cts(L);
  82.   TValue *o = L->base + narg-1;
  83.   int32_t i;
  84.   if (o >= L->top)
  85.     lj_err_arg(L, narg, LJ_ERR_NOVAL);
  86.   lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o,
  87.                  CCF_ARG(narg));
  88.   return i;
  89. }

  90. /* -- C type metamethods -------------------------------------------------- */

  91. #define LJLIB_MODULE_ffi_meta

  92. /* Handle ctype __index/__newindex metamethods. */
  93. static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)
  94. {
  95.   CTypeID id = ctype_typeid(cts, ct);
  96.   cTValue *tv = lj_ctype_meta(cts, id, mm);
  97.   TValue *base = L->base;
  98.   if (!tv) {
  99.     const char *s;
  100.   err_index:
  101.     s = strdata(lj_ctype_repr(L, id, NULL));
  102.     if (tvisstr(L->base+1)) {
  103.       lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1));
  104.     } else {
  105.       const char *key = tviscdata(L->base+1) ?
  106.         strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) :
  107.         lj_typename(L->base+1);
  108.       lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key);
  109.     }
  110.   }
  111.   if (!tvisfunc(tv)) {
  112.     if (mm == MM_index) {
  113.       cTValue *o = lj_meta_tget(L, tv, base+1);
  114.       if (o) {
  115.         if (tvisnil(o)) goto err_index;
  116.         copyTV(L, L->top-1, o);
  117.         return 1;
  118.       }
  119.     } else {
  120.       TValue *o = lj_meta_tset(L, tv, base+1);
  121.       if (o) {
  122.         copyTV(L, o, base+2);
  123.         return 0;
  124.       }
  125.     }
  126.     copyTV(L, base, L->top);
  127.     tv = L->top-1-LJ_FR2;
  128.   }
  129.   return lj_meta_tailcall(L, tv);
  130. }

  131. LJLIB_CF(ffi_meta___index)        LJLIB_REC(cdata_index 0)
  132. {
  133.   CTState *cts = ctype_cts(L);
  134.   CTInfo qual = 0;
  135.   CType *ct;
  136.   uint8_t *p;
  137.   TValue *o = L->base;
  138.   if (!(o+1 < L->top && tviscdata(o)))  /* Also checks for presence of key. */
  139.     lj_err_argt(L, 1, LUA_TCDATA);
  140.   ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
  141.   if ((qual & 1))
  142.     return ffi_index_meta(L, cts, ct, MM_index);
  143.   if (lj_cdata_get(cts, ct, L->top-1, p))
  144.     lj_gc_check(L);
  145.   return 1;
  146. }

  147. LJLIB_CF(ffi_meta___newindex)        LJLIB_REC(cdata_index 1)
  148. {
  149.   CTState *cts = ctype_cts(L);
  150.   CTInfo qual = 0;
  151.   CType *ct;
  152.   uint8_t *p;
  153.   TValue *o = L->base;
  154.   if (!(o+2 < L->top && tviscdata(o)))  /* Also checks for key and value. */
  155.     lj_err_argt(L, 1, LUA_TCDATA);
  156.   ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
  157.   if ((qual & 1)) {
  158.     if ((qual & CTF_CONST))
  159.       lj_err_caller(L, LJ_ERR_FFI_WRCONST);
  160.     return ffi_index_meta(L, cts, ct, MM_newindex);
  161.   }
  162.   lj_cdata_set(cts, ct, p, o+2, qual);
  163.   return 0;
  164. }

  165. /* Common handler for cdata arithmetic. */
  166. static int ffi_arith(lua_State *L)
  167. {
  168.   MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
  169.   return lj_carith_op(L, mm);
  170. }

  171. /* The following functions must be in contiguous ORDER MM. */
  172. LJLIB_CF(ffi_meta___eq)                LJLIB_REC(cdata_arith MM_eq)
  173. {
  174.   return ffi_arith(L);
  175. }

  176. LJLIB_CF(ffi_meta___len)        LJLIB_REC(cdata_arith MM_len)
  177. {
  178.   return ffi_arith(L);
  179. }

  180. LJLIB_CF(ffi_meta___lt)                LJLIB_REC(cdata_arith MM_lt)
  181. {
  182.   return ffi_arith(L);
  183. }

  184. LJLIB_CF(ffi_meta___le)                LJLIB_REC(cdata_arith MM_le)
  185. {
  186.   return ffi_arith(L);
  187. }

  188. LJLIB_CF(ffi_meta___concat)        LJLIB_REC(cdata_arith MM_concat)
  189. {
  190.   return ffi_arith(L);
  191. }

  192. /* Forward declaration. */
  193. static int lj_cf_ffi_new(lua_State *L);

  194. LJLIB_CF(ffi_meta___call)        LJLIB_REC(cdata_call)
  195. {
  196.   CTState *cts = ctype_cts(L);
  197.   GCcdata *cd = ffi_checkcdata(L, 1);
  198.   CTypeID id = cd->ctypeid;
  199.   CType *ct;
  200.   cTValue *tv;
  201.   MMS mm = MM_call;
  202.   if (cd->ctypeid == CTID_CTYPEID) {
  203.     id = *(CTypeID *)cdataptr(cd);
  204.     mm = MM_new;
  205.   } else {
  206.     int ret = lj_ccall_func(L, cd);
  207.     if (ret >= 0)
  208.       return ret;
  209.   }
  210.   /* Handle ctype __call/__new metamethod. */
  211.   ct = ctype_raw(cts, id);
  212.   if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  213.   tv = lj_ctype_meta(cts, id, mm);
  214.   if (tv)
  215.     return lj_meta_tailcall(L, tv);
  216.   else if (mm == MM_call)
  217.     lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL)));
  218.   return lj_cf_ffi_new(L);
  219. }

  220. LJLIB_CF(ffi_meta___add)        LJLIB_REC(cdata_arith MM_add)
  221. {
  222.   return ffi_arith(L);
  223. }

  224. LJLIB_CF(ffi_meta___sub)        LJLIB_REC(cdata_arith MM_sub)
  225. {
  226.   return ffi_arith(L);
  227. }

  228. LJLIB_CF(ffi_meta___mul)        LJLIB_REC(cdata_arith MM_mul)
  229. {
  230.   return ffi_arith(L);
  231. }

  232. LJLIB_CF(ffi_meta___div)        LJLIB_REC(cdata_arith MM_div)
  233. {
  234.   return ffi_arith(L);
  235. }

  236. LJLIB_CF(ffi_meta___mod)        LJLIB_REC(cdata_arith MM_mod)
  237. {
  238.   return ffi_arith(L);
  239. }

  240. LJLIB_CF(ffi_meta___pow)        LJLIB_REC(cdata_arith MM_pow)
  241. {
  242.   return ffi_arith(L);
  243. }

  244. LJLIB_CF(ffi_meta___unm)        LJLIB_REC(cdata_arith MM_unm)
  245. {
  246.   return ffi_arith(L);
  247. }
  248. /* End of contiguous ORDER MM. */

  249. LJLIB_CF(ffi_meta___tostring)
  250. {
  251.   GCcdata *cd = ffi_checkcdata(L, 1);
  252.   const char *msg = "cdata<%s>: %p";
  253.   CTypeID id = cd->ctypeid;
  254.   void *p = cdataptr(cd);
  255.   if (id == CTID_CTYPEID) {
  256.     msg = "ctype<%s>";
  257.     id = *(CTypeID *)p;
  258.   } else {
  259.     CTState *cts = ctype_cts(L);
  260.     CType *ct = ctype_raw(cts, id);
  261.     if (ctype_isref(ct->info)) {
  262.       p = *(void **)p;
  263.       ct = ctype_rawchild(cts, ct);
  264.     }
  265.     if (ctype_iscomplex(ct->info)) {
  266.       setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size));
  267.       goto checkgc;
  268.     } else if (ct->size == 8 && ctype_isinteger(ct->info)) {
  269.       setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),
  270.                                                (ct->info & CTF_UNSIGNED)));
  271.       goto checkgc;
  272.     } else if (ctype_isfunc(ct->info)) {
  273.       p = *(void **)p;
  274.     } else if (ctype_isenum(ct->info)) {
  275.       msg = "cdata<%s>: %d";
  276.       p = (void *)(uintptr_t)*(uint32_t **)p;
  277.     } else {
  278.       if (ctype_isptr(ct->info)) {
  279.         p = cdata_getptr(p, ct->size);
  280.         ct = ctype_rawchild(cts, ct);
  281.       }
  282.       if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) {
  283.         /* Handle ctype __tostring metamethod. */
  284.         cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring);
  285.         if (tv)
  286.           return lj_meta_tailcall(L, tv);
  287.       }
  288.     }
  289.   }
  290.   lj_strfmt_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
  291. checkgc:
  292.   lj_gc_check(L);
  293.   return 1;
  294. }

  295. static int ffi_pairs(lua_State *L, MMS mm)
  296. {
  297.   CTState *cts = ctype_cts(L);
  298.   CTypeID id = ffi_checkcdata(L, 1)->ctypeid;
  299.   CType *ct = ctype_raw(cts, id);
  300.   cTValue *tv;
  301.   if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  302.   tv = lj_ctype_meta(cts, id, mm);
  303.   if (!tv)
  304.     lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)),
  305.                    strdata(mmname_str(G(L), mm)));
  306.   return lj_meta_tailcall(L, tv);
  307. }

  308. LJLIB_CF(ffi_meta___pairs)
  309. {
  310.   return ffi_pairs(L, MM_pairs);
  311. }

  312. LJLIB_CF(ffi_meta___ipairs)
  313. {
  314.   return ffi_pairs(L, MM_ipairs);
  315. }

  316. LJLIB_PUSH("ffi") LJLIB_SET(__metatable)

  317. #include "lj_libdef.h"

  318. /* -- C library metamethods ----------------------------------------------- */

  319. #define LJLIB_MODULE_ffi_clib

  320. /* Index C library by a name. */
  321. static TValue *ffi_clib_index(lua_State *L)
  322. {
  323.   TValue *o = L->base;
  324.   CLibrary *cl;
  325.   if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))
  326.     lj_err_argt(L, 1, LUA_TUSERDATA);
  327.   cl = (CLibrary *)uddata(udataV(o));
  328.   if (!(o+1 < L->top && tvisstr(o+1)))
  329.     lj_err_argt(L, 2, LUA_TSTRING);
  330.   return lj_clib_index(L, cl, strV(o+1));
  331. }

  332. LJLIB_CF(ffi_clib___index)        LJLIB_REC(clib_index 1)
  333. {
  334.   TValue *tv = ffi_clib_index(L);
  335.   if (tviscdata(tv)) {
  336.     CTState *cts = ctype_cts(L);
  337.     GCcdata *cd = cdataV(tv);
  338.     CType *s = ctype_get(cts, cd->ctypeid);
  339.     if (ctype_isextern(s->info)) {
  340.       CTypeID sid = ctype_cid(s->info);
  341.       void *sp = *(void **)cdataptr(cd);
  342.       CType *ct = ctype_raw(cts, sid);
  343.       if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))
  344.         lj_gc_check(L);
  345.       return 1;
  346.     }
  347.   }
  348.   copyTV(L, L->top-1, tv);
  349.   return 1;
  350. }

  351. LJLIB_CF(ffi_clib___newindex)        LJLIB_REC(clib_index 0)
  352. {
  353.   TValue *tv = ffi_clib_index(L);
  354.   TValue *o = L->base+2;
  355.   if (o < L->top && tviscdata(tv)) {
  356.     CTState *cts = ctype_cts(L);
  357.     GCcdata *cd = cdataV(tv);
  358.     CType *d = ctype_get(cts, cd->ctypeid);
  359.     if (ctype_isextern(d->info)) {
  360.       CTInfo qual = 0;
  361.       for (;;) {  /* Skip attributes and collect qualifiers. */
  362.         d = ctype_child(cts, d);
  363.         if (!ctype_isattrib(d->info)) break;
  364.         if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
  365.       }
  366.       if (!((d->info|qual) & CTF_CONST)) {
  367.         lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);
  368.         return 0;
  369.       }
  370.     }
  371.   }
  372.   lj_err_caller(L, LJ_ERR_FFI_WRCONST);
  373.   return 0/* unreachable */
  374. }

  375. LJLIB_CF(ffi_clib___gc)
  376. {
  377.   TValue *o = L->base;
  378.   if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)
  379.     lj_clib_unload((CLibrary *)uddata(udataV(o)));
  380.   return 0;
  381. }

  382. #include "lj_libdef.h"

  383. /* -- Callback function metamethods --------------------------------------- */

  384. #define LJLIB_MODULE_ffi_callback

  385. static int ffi_callback_set(lua_State *L, GCfunc *fn)
  386. {
  387.   GCcdata *cd = ffi_checkcdata(L, 1);
  388.   CTState *cts = ctype_cts(L);
  389.   CType *ct = ctype_raw(cts, cd->ctypeid);
  390.   if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) {
  391.     MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd));
  392.     if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) {
  393.       GCtab *t = cts->miscmap;
  394.       TValue *tv = lj_tab_setint(L, t, (int32_t)slot);
  395.       if (fn) {
  396.         setfuncV(L, tv, fn);
  397.         lj_gc_anybarriert(L, t);
  398.       } else {
  399.         setnilV(tv);
  400.         cts->cb.cbid[slot] = 0;
  401.         cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid;
  402.       }
  403.       return 0;
  404.     }
  405.   }
  406.   lj_err_caller(L, LJ_ERR_FFI_BADCBACK);
  407.   return 0;
  408. }

  409. LJLIB_CF(ffi_callback_free)
  410. {
  411.   return ffi_callback_set(L, NULL);
  412. }

  413. LJLIB_CF(ffi_callback_set)
  414. {
  415.   GCfunc *fn = lj_lib_checkfunc(L, 2);
  416.   return ffi_callback_set(L, fn);
  417. }

  418. LJLIB_PUSH(top-1) LJLIB_SET(__index)

  419. #include "lj_libdef.h"

  420. /* -- FFI library functions ----------------------------------------------- */

  421. #define LJLIB_MODULE_ffi

  422. LJLIB_CF(ffi_cdef)
  423. {
  424.   GCstr *s = lj_lib_checkstr(L, 1);
  425.   CPState cp;
  426.   int errcode;
  427.   cp.L = L;
  428.   cp.cts = ctype_cts(L);
  429.   cp.srcname = strdata(s);
  430.   cp.p = strdata(s);
  431.   cp.param = L->base+1;
  432.   cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;
  433.   errcode = lj_cparse(&cp);
  434.   if (errcode) lj_err_throw(L, errcode);  /* Propagate errors. */
  435.   lj_gc_check(L);
  436.   return 0;
  437. }

  438. LJLIB_CF(ffi_new)        LJLIB_REC(.)
  439. {
  440.   CTState *cts = ctype_cts(L);
  441.   CTypeID id = ffi_checkctype(L, cts, NULL);
  442.   CType *ct = ctype_raw(cts, id);
  443.   CTSize sz;
  444.   CTInfo info = lj_ctype_info(cts, id, &sz);
  445.   TValue *o = L->base+1;
  446.   GCcdata *cd;
  447.   if ((info & CTF_VLA)) {
  448.     o++;
  449.     sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
  450.   }
  451.   if (sz == CTSIZE_INVALID)
  452.     lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);
  453.   if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)
  454.     cd = lj_cdata_new(cts, id, sz);
  455.   else
  456.     cd = lj_cdata_newv(L, id, sz, ctype_align(info));
  457.   setcdataV(L, o-1, cd);  /* Anchor the uninitialized cdata. */
  458.   lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
  459.                    o, (MSize)(L->top - o));  /* Initialize cdata. */
  460.   if (ctype_isstruct(ct->info)) {
  461.     /* Handle ctype __gc metamethod. Use the fast lookup here. */
  462.     cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
  463.     if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
  464.       GCtab *t = cts->finalizer;
  465.       if (gcref(t->metatable)) {
  466.         /* Add to finalizer table, if still enabled. */
  467.         copyTV(L, lj_tab_set(L, t, o-1), tv);
  468.         lj_gc_anybarriert(L, t);
  469.         cd->marked |= LJ_GC_CDATA_FIN;
  470.       }
  471.     }
  472.   }
  473.   L->top = o;  /* Only return the cdata itself. */
  474.   lj_gc_check(L);
  475.   return 1;
  476. }

  477. LJLIB_CF(ffi_cast)        LJLIB_REC(ffi_new)
  478. {
  479.   CTState *cts = ctype_cts(L);
  480.   CTypeID id = ffi_checkctype(L, cts, NULL);
  481.   CType *d = ctype_raw(cts, id);
  482.   TValue *o = lj_lib_checkany(L, 2);
  483.   L->top = o+1/* Make sure this is the last item on the stack. */
  484.   if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
  485.     lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  486.   if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) {
  487.     GCcdata *cd = lj_cdata_new(cts, id, d->size);
  488.     lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
  489.     setcdataV(L, o, cd);
  490.     lj_gc_check(L);
  491.   }
  492.   return 1;
  493. }

  494. LJLIB_CF(ffi_typeof)        LJLIB_REC(.)
  495. {
  496.   CTState *cts = ctype_cts(L);
  497.   CTypeID id = ffi_checkctype(L, cts, L->base+1);
  498.   GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  499.   *(CTypeID *)cdataptr(cd) = id;
  500.   setcdataV(L, L->top-1, cd);
  501.   lj_gc_check(L);
  502.   return 1;
  503. }

  504. /* Internal and unsupported API. */
  505. LJLIB_CF(ffi_typeinfo)
  506. {
  507.   CTState *cts = ctype_cts(L);
  508.   CTypeID id = (CTypeID)ffi_checkint(L, 1);
  509.   if (id > 0 && id < cts->top) {
  510.     CType *ct = ctype_get(cts, id);
  511.     GCtab *t;
  512.     lua_createtable(L, 0, 4);  /* Increment hash size if fields are added. */
  513.     t = tabV(L->top-1);
  514.     setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "info")), (int32_t)ct->info);
  515.     if (ct->size != CTSIZE_INVALID)
  516.       setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "size")), (int32_t)ct->size);
  517.     if (ct->sib)
  518.       setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);
  519.     if (gcref(ct->name)) {
  520.       GCstr *s = gco2str(gcref(ct->name));
  521.       setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);
  522.     }
  523.     lj_gc_check(L);
  524.     return 1;
  525.   }
  526.   return 0;
  527. }

  528. LJLIB_CF(ffi_istype)        LJLIB_REC(.)
  529. {
  530.   CTState *cts = ctype_cts(L);
  531.   CTypeID id1 = ffi_checkctype(L, cts, NULL);
  532.   TValue *o = lj_lib_checkany(L, 2);
  533.   int b = 0;
  534.   if (tviscdata(o)) {
  535.     GCcdata *cd = cdataV(o);
  536.     CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
  537.                                                 cd->ctypeid;
  538.     CType *ct1 = lj_ctype_rawref(cts, id1);
  539.     CType *ct2 = lj_ctype_rawref(cts, id2);
  540.     if (ct1 == ct2) {
  541.       b = 1;
  542.     } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
  543.                ct1->size == ct2->size) {
  544.       if (ctype_ispointer(ct1->info))
  545.         b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
  546.       else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
  547.         b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0);
  548.     } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
  549.                ct1 == ctype_rawchild(cts, ct2)) {
  550.       b = 1;
  551.     }
  552.   }
  553.   setboolV(L->top-1, b);
  554.   setboolV(&G(L)->tmptv2, b);  /* Remember for trace recorder. */
  555.   return 1;
  556. }

  557. LJLIB_CF(ffi_sizeof)        LJLIB_REC(ffi_xof FF_ffi_sizeof)
  558. {
  559.   CTState *cts = ctype_cts(L);
  560.   CTypeID id = ffi_checkctype(L, cts, NULL);
  561.   CTSize sz;
  562.   if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
  563.     sz = cdatavlen(cdataV(L->base));
  564.   } else {
  565.     CType *ct = lj_ctype_rawref(cts, id);
  566.     if (ctype_isvltype(ct->info))
  567.       sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
  568.     else
  569.       sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
  570.     if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
  571.       setnilV(L->top-1);
  572.       return 1;
  573.     }
  574.   }
  575.   setintV(L->top-1, (int32_t)sz);
  576.   return 1;
  577. }

  578. LJLIB_CF(ffi_alignof)        LJLIB_REC(ffi_xof FF_ffi_alignof)
  579. {
  580.   CTState *cts = ctype_cts(L);
  581.   CTypeID id = ffi_checkctype(L, cts, NULL);
  582.   CTSize sz = 0;
  583.   CTInfo info = lj_ctype_info(cts, id, &sz);
  584.   setintV(L->top-1, 1 << ctype_align(info));
  585.   return 1;
  586. }

  587. LJLIB_CF(ffi_offsetof)        LJLIB_REC(ffi_xof FF_ffi_offsetof)
  588. {
  589.   CTState *cts = ctype_cts(L);
  590.   CTypeID id = ffi_checkctype(L, cts, NULL);
  591.   GCstr *name = lj_lib_checkstr(L, 2);
  592.   CType *ct = lj_ctype_rawref(cts, id);
  593.   CTSize ofs;
  594.   if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
  595.     CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
  596.     if (fct) {
  597.       setintV(L->top-1, ofs);
  598.       if (ctype_isfield(fct->info)) {
  599.         return 1;
  600.       } else if (ctype_isbitfield(fct->info)) {
  601.         setintV(L->top++, ctype_bitpos(fct->info));
  602.         setintV(L->top++, ctype_bitbsz(fct->info));
  603.         return 3;
  604.       }
  605.     }
  606.   }
  607.   return 0;
  608. }

  609. LJLIB_CF(ffi_errno)        LJLIB_REC(.)
  610. {
  611.   int err = errno;
  612.   if (L->top > L->base)
  613.     errno = ffi_checkint(L, 1);
  614.   setintV(L->top++, err);
  615.   return 1;
  616. }

  617. LJLIB_CF(ffi_string)        LJLIB_REC(.)
  618. {
  619.   CTState *cts = ctype_cts(L);
  620.   TValue *o = lj_lib_checkany(L, 1);
  621.   const char *p;
  622.   size_t len;
  623.   if (o+1 < L->top && !tvisnil(o+1)) {
  624.     len = (size_t)ffi_checkint(L, 2);
  625.     lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
  626.                    CCF_ARG(1));
  627.   } else {
  628.     lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
  629.                    CCF_ARG(1));
  630.     len = strlen(p);
  631.   }
  632.   L->top = o+1/* Make sure this is the last item on the stack. */
  633.   setstrV(L, o, lj_str_new(L, p, len));
  634.   lj_gc_check(L);
  635.   return 1;
  636. }

  637. LJLIB_CF(ffi_copy)        LJLIB_REC(.)
  638. {
  639.   void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  640.   void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
  641.   TValue *o = L->base+1;
  642.   CTSize len;
  643.   if (tvisstr(o) && o+1 >= L->top)
  644.     len = strV(o)->len+1/* Copy Lua string including trailing '\0'. */
  645.   else
  646.     len = (CTSize)ffi_checkint(L, 3);
  647.   memcpy(dp, sp, len);
  648.   return 0;
  649. }

  650. LJLIB_CF(ffi_fill)        LJLIB_REC(.)
  651. {
  652.   void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  653.   CTSize len = (CTSize)ffi_checkint(L, 2);
  654.   int32_t fill = 0;
  655.   if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
  656.   memset(dp, fill, len);
  657.   return 0;
  658. }

  659. #define H_(le, be)        LJ_ENDIAN_SELECT(0x##le, 0x##be)

  660. /* Test ABI string. */
  661. LJLIB_CF(ffi_abi)        LJLIB_REC(.)
  662. {
  663.   GCstr *s = lj_lib_checkstr(L, 1);
  664.   int b = 0;
  665.   switch (s->hash) {
  666. #if LJ_64
  667.   case H_(849858eb,ad35fd06): b = 1; break/* 64bit */
  668. #else
  669.   case H_(662d3c79,d0e22477): b = 1; break/* 32bit */
  670. #endif
  671. #if LJ_ARCH_HASFPU
  672.   case H_(e33ee463,e33ee463): b = 1; break/* fpu */
  673. #endif
  674. #if LJ_ABI_SOFTFP
  675.   case H_(61211a23,c2e8c81c): b = 1; break/* softfp */
  676. #else
  677.   case H_(539417a8,8ce0812f): b = 1; break/* hardfp */
  678. #endif
  679. #if LJ_ABI_EABI
  680.   case H_(2182df8f,f2ed1152): b = 1; break/* eabi */
  681. #endif
  682. #if LJ_ABI_WIN
  683.   case H_(4ab624a8,4ab624a8): b = 1; break/* win */
  684. #endif
  685.   case H_(3af93066,1f001464): b = 1; break/* le/be */
  686. #if LJ_GC64
  687.   case H_(9e89d2c9,13c83c92): b = 1; break/* gc64 */
  688. #endif
  689.   default:
  690.     break;
  691.   }
  692.   setboolV(L->top-1, b);
  693.   setboolV(&G(L)->tmptv2, b);  /* Remember for trace recorder. */
  694.   return 1;
  695. }

  696. #undef H_

  697. LJLIB_PUSH(top-8) LJLIB_SET(!)  /* Store reference to miscmap table. */

  698. LJLIB_CF(ffi_metatype)
  699. {
  700.   CTState *cts = ctype_cts(L);
  701.   CTypeID id = ffi_checkctype(L, cts, NULL);
  702.   GCtab *mt = lj_lib_checktab(L, 2);
  703.   GCtab *t = cts->miscmap;
  704.   CType *ct = ctype_get(cts, id);  /* Only allow raw types. */
  705.   TValue *tv;
  706.   GCcdata *cd;
  707.   if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
  708.         ctype_isvector(ct->info)))
  709.     lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  710.   tv = lj_tab_setinth(L, t, -(int32_t)id);
  711.   if (!tvisnil(tv))
  712.     lj_err_caller(L, LJ_ERR_PROTMT);
  713.   settabV(L, tv, mt);
  714.   lj_gc_anybarriert(L, t);
  715.   cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  716.   *(CTypeID *)cdataptr(cd) = id;
  717.   setcdataV(L, L->top-1, cd);
  718.   lj_gc_check(L);
  719.   return 1;
  720. }

  721. LJLIB_PUSH(top-7) LJLIB_SET(!)  /* Store reference to finalizer table. */

  722. LJLIB_CF(ffi_gc)        LJLIB_REC(.)
  723. {
  724.   GCcdata *cd = ffi_checkcdata(L, 1);
  725.   TValue *fin = lj_lib_checkany(L, 2);
  726.   CTState *cts = ctype_cts(L);
  727.   CType *ct = ctype_raw(cts, cd->ctypeid);
  728.   if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
  729.         ctype_isrefarray(ct->info)))
  730.     lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  731.   lj_cdata_setfin(L, cd, gcval(fin), itype(fin));
  732.   L->top = L->base+1/* Pass through the cdata object. */
  733.   return 1;
  734. }

  735. LJLIB_PUSH(top-5) LJLIB_SET(!)  /* Store clib metatable in func environment. */

  736. LJLIB_CF(ffi_load)
  737. {
  738.   GCstr *name = lj_lib_checkstr(L, 1);
  739.   int global = (L->base+1 < L->top && tvistruecond(L->base+1));
  740.   lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
  741.   return 1;
  742. }

  743. LJLIB_PUSH(top-4) LJLIB_SET(C)
  744. LJLIB_PUSH(top-3) LJLIB_SET(os)
  745. LJLIB_PUSH(top-2) LJLIB_SET(arch)

  746. #include "lj_libdef.h"

  747. /* ------------------------------------------------------------------------ */

  748. /* Create special weak-keyed finalizer table. */
  749. static GCtab *ffi_finalizer(lua_State *L)
  750. {
  751.   /* NOBARRIER: The table is new (marked white). */
  752.   GCtab *t = lj_tab_new(L, 0, 1);
  753.   settabV(L, L->top++, t);
  754.   setgcref(t->metatable, obj2gco(t));
  755.   setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
  756.           lj_str_newlit(L, "K"));
  757.   t->nomm = (uint8_t)(~(1u<<MM_mode));
  758.   return t;
  759. }

  760. /* Register FFI module as loaded. */
  761. static void ffi_register_module(lua_State *L)
  762. {
  763.   cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
  764.   if (tmp && tvistab(tmp)) {
  765.     GCtab *t = tabV(tmp);
  766.     copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
  767.     lj_gc_anybarriert(L, t);
  768.   }
  769. }

  770. LUALIB_API int luaopen_ffi(lua_State *L)
  771. {
  772.   CTState *cts = lj_ctype_init(L);
  773.   settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
  774.   cts->finalizer = ffi_finalizer(L);
  775.   LJ_LIB_REG(L, NULL, ffi_meta);
  776.   /* NOBARRIER: basemt is a GC root. */
  777.   setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
  778.   LJ_LIB_REG(L, NULL, ffi_clib);
  779.   LJ_LIB_REG(L, NULL, ffi_callback);
  780.   /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */
  781.   settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));
  782.   L->top--;
  783.   lj_clib_default(L, tabV(L->top-1));  /* Create ffi.C default namespace. */
  784.   lua_pushliteral(L, LJ_OS_NAME);
  785.   lua_pushliteral(L, LJ_ARCH_NAME);
  786.   LJ_LIB_REG(L, NULL, ffi);  /* Note: no global "ffi" created! */
  787.   ffi_register_module(L);
  788.   return 1;
  789. }

  790. #endif