lib_ffi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. ** FFI library.
  3. ** Copyright (C) 2005-2014 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_ff.h"
  27. #include "lj_lib.h"
  28. /* -- C type checks ------------------------------------------------------- */
  29. /* Check first argument for a C type and returns its ID. */
  30. static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param)
  31. {
  32. TValue *o = L->base;
  33. if (!(o < L->top)) {
  34. err_argtype:
  35. lj_err_argtype(L, 1, "C type");
  36. }
  37. if (tvisstr(o)) { /* Parse an abstract C type declaration. */
  38. GCstr *s = strV(o);
  39. CPState cp;
  40. int errcode;
  41. cp.L = L;
  42. cp.cts = cts;
  43. cp.srcname = strdata(s);
  44. cp.p = strdata(s);
  45. cp.param = param;
  46. cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
  47. errcode = lj_cparse(&cp);
  48. if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
  49. return cp.val.id;
  50. } else {
  51. GCcdata *cd;
  52. if (!tviscdata(o)) goto err_argtype;
  53. if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM);
  54. cd = cdataV(o);
  55. return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid;
  56. }
  57. }
  58. /* Check argument for C data and return it. */
  59. static GCcdata *ffi_checkcdata(lua_State *L, int narg)
  60. {
  61. TValue *o = L->base + narg-1;
  62. if (!(o < L->top && tviscdata(o)))
  63. lj_err_argt(L, narg, LUA_TCDATA);
  64. return cdataV(o);
  65. }
  66. /* Convert argument to C pointer. */
  67. static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
  68. {
  69. CTState *cts = ctype_cts(L);
  70. TValue *o = L->base + narg-1;
  71. void *p;
  72. if (o >= L->top)
  73. lj_err_arg(L, narg, LJ_ERR_NOVAL);
  74. lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg));
  75. return p;
  76. }
  77. /* Convert argument to int32_t. */
  78. static int32_t ffi_checkint(lua_State *L, int narg)
  79. {
  80. CTState *cts = ctype_cts(L);
  81. TValue *o = L->base + narg-1;
  82. int32_t i;
  83. if (o >= L->top)
  84. lj_err_arg(L, narg, LJ_ERR_NOVAL);
  85. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o,
  86. CCF_ARG(narg));
  87. return i;
  88. }
  89. /* -- C type metamethods -------------------------------------------------- */
  90. #define LJLIB_MODULE_ffi_meta
  91. /* Handle ctype __index/__newindex metamethods. */
  92. static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)
  93. {
  94. CTypeID id = ctype_typeid(cts, ct);
  95. cTValue *tv = lj_ctype_meta(cts, id, mm);
  96. TValue *base = L->base;
  97. if (!tv) {
  98. const char *s;
  99. err_index:
  100. s = strdata(lj_ctype_repr(L, id, NULL));
  101. if (tvisstr(L->base+1)) {
  102. lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1));
  103. } else {
  104. const char *key = tviscdata(L->base+1) ?
  105. strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) :
  106. lj_typename(L->base+1);
  107. lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key);
  108. }
  109. }
  110. if (!tvisfunc(tv)) {
  111. if (mm == MM_index) {
  112. cTValue *o = lj_meta_tget(L, tv, base+1);
  113. if (o) {
  114. if (tvisnil(o)) goto err_index;
  115. copyTV(L, L->top-1, o);
  116. return 1;
  117. }
  118. } else {
  119. TValue *o = lj_meta_tset(L, tv, base+1);
  120. if (o) {
  121. copyTV(L, o, base+2);
  122. return 0;
  123. }
  124. }
  125. tv = L->top-1;
  126. }
  127. return lj_meta_tailcall(L, tv);
  128. }
  129. LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
  130. {
  131. CTState *cts = ctype_cts(L);
  132. CTInfo qual = 0;
  133. CType *ct;
  134. uint8_t *p;
  135. TValue *o = L->base;
  136. if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */
  137. lj_err_argt(L, 1, LUA_TCDATA);
  138. ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
  139. if ((qual & 1))
  140. return ffi_index_meta(L, cts, ct, MM_index);
  141. if (lj_cdata_get(cts, ct, L->top-1, p))
  142. lj_gc_check(L);
  143. return 1;
  144. }
  145. LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
  146. {
  147. CTState *cts = ctype_cts(L);
  148. CTInfo qual = 0;
  149. CType *ct;
  150. uint8_t *p;
  151. TValue *o = L->base;
  152. if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */
  153. lj_err_argt(L, 1, LUA_TCDATA);
  154. ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
  155. if ((qual & 1)) {
  156. if ((qual & CTF_CONST))
  157. lj_err_caller(L, LJ_ERR_FFI_WRCONST);
  158. return ffi_index_meta(L, cts, ct, MM_newindex);
  159. }
  160. lj_cdata_set(cts, ct, p, o+2, qual);
  161. return 0;
  162. }
  163. /* Common handler for cdata arithmetic. */
  164. static int ffi_arith(lua_State *L)
  165. {
  166. MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
  167. return lj_carith_op(L, mm);
  168. }
  169. /* The following functions must be in contiguous ORDER MM. */
  170. LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)
  171. {
  172. return ffi_arith(L);
  173. }
  174. LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len)
  175. {
  176. return ffi_arith(L);
  177. }
  178. LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt)
  179. {
  180. return ffi_arith(L);
  181. }
  182. LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le)
  183. {
  184. return ffi_arith(L);
  185. }
  186. LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat)
  187. {
  188. return ffi_arith(L);
  189. }
  190. /* Forward declaration. */
  191. static int lj_cf_ffi_new(lua_State *L);
  192. LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
  193. {
  194. CTState *cts = ctype_cts(L);
  195. GCcdata *cd = ffi_checkcdata(L, 1);
  196. CTypeID id = cd->ctypeid;
  197. CType *ct;
  198. cTValue *tv;
  199. MMS mm = MM_call;
  200. if (cd->ctypeid == CTID_CTYPEID) {
  201. id = *(CTypeID *)cdataptr(cd);
  202. mm = MM_new;
  203. } else {
  204. int ret = lj_ccall_func(L, cd);
  205. if (ret >= 0)
  206. return ret;
  207. }
  208. /* Handle ctype __call/__new metamethod. */
  209. ct = ctype_raw(cts, id);
  210. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  211. tv = lj_ctype_meta(cts, id, mm);
  212. if (tv)
  213. return lj_meta_tailcall(L, tv);
  214. else if (mm == MM_call)
  215. lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL)));
  216. return lj_cf_ffi_new(L);
  217. }
  218. LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)
  219. {
  220. return ffi_arith(L);
  221. }
  222. LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub)
  223. {
  224. return ffi_arith(L);
  225. }
  226. LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul)
  227. {
  228. return ffi_arith(L);
  229. }
  230. LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div)
  231. {
  232. return ffi_arith(L);
  233. }
  234. LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod)
  235. {
  236. return ffi_arith(L);
  237. }
  238. LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow)
  239. {
  240. return ffi_arith(L);
  241. }
  242. LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)
  243. {
  244. return ffi_arith(L);
  245. }
  246. /* End of contiguous ORDER MM. */
  247. LJLIB_CF(ffi_meta___tostring)
  248. {
  249. GCcdata *cd = ffi_checkcdata(L, 1);
  250. const char *msg = "cdata<%s>: %p";
  251. CTypeID id = cd->ctypeid;
  252. void *p = cdataptr(cd);
  253. if (id == CTID_CTYPEID) {
  254. msg = "ctype<%s>";
  255. id = *(CTypeID *)p;
  256. } else {
  257. CTState *cts = ctype_cts(L);
  258. CType *ct = ctype_raw(cts, id);
  259. if (ctype_isref(ct->info)) {
  260. p = *(void **)p;
  261. ct = ctype_rawchild(cts, ct);
  262. }
  263. if (ctype_iscomplex(ct->info)) {
  264. setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size));
  265. goto checkgc;
  266. } else if (ct->size == 8 && ctype_isinteger(ct->info)) {
  267. setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),
  268. (ct->info & CTF_UNSIGNED)));
  269. goto checkgc;
  270. } else if (ctype_isfunc(ct->info)) {
  271. p = *(void **)p;
  272. } else if (ctype_isenum(ct->info)) {
  273. msg = "cdata<%s>: %d";
  274. p = (void *)(uintptr_t)*(uint32_t **)p;
  275. } else {
  276. if (ctype_isptr(ct->info)) {
  277. p = cdata_getptr(p, ct->size);
  278. ct = ctype_rawchild(cts, ct);
  279. }
  280. if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) {
  281. /* Handle ctype __tostring metamethod. */
  282. cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring);
  283. if (tv)
  284. return lj_meta_tailcall(L, tv);
  285. }
  286. }
  287. }
  288. lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
  289. checkgc:
  290. lj_gc_check(L);
  291. return 1;
  292. }
  293. static int ffi_pairs(lua_State *L, MMS mm)
  294. {
  295. CTState *cts = ctype_cts(L);
  296. CTypeID id = ffi_checkcdata(L, 1)->ctypeid;
  297. CType *ct = ctype_raw(cts, id);
  298. cTValue *tv;
  299. if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
  300. tv = lj_ctype_meta(cts, id, mm);
  301. if (!tv)
  302. lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)),
  303. strdata(mmname_str(G(L), mm)));
  304. return lj_meta_tailcall(L, tv);
  305. }
  306. LJLIB_CF(ffi_meta___pairs)
  307. {
  308. return ffi_pairs(L, MM_pairs);
  309. }
  310. LJLIB_CF(ffi_meta___ipairs)
  311. {
  312. return ffi_pairs(L, MM_ipairs);
  313. }
  314. LJLIB_PUSH("ffi") LJLIB_SET(__metatable)
  315. #include "lj_libdef.h"
  316. /* -- C library metamethods ----------------------------------------------- */
  317. #define LJLIB_MODULE_ffi_clib
  318. /* Index C library by a name. */
  319. static TValue *ffi_clib_index(lua_State *L)
  320. {
  321. TValue *o = L->base;
  322. CLibrary *cl;
  323. if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))
  324. lj_err_argt(L, 1, LUA_TUSERDATA);
  325. cl = (CLibrary *)uddata(udataV(o));
  326. if (!(o+1 < L->top && tvisstr(o+1)))
  327. lj_err_argt(L, 2, LUA_TSTRING);
  328. return lj_clib_index(L, cl, strV(o+1));
  329. }
  330. LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1)
  331. {
  332. TValue *tv = ffi_clib_index(L);
  333. if (tviscdata(tv)) {
  334. CTState *cts = ctype_cts(L);
  335. GCcdata *cd = cdataV(tv);
  336. CType *s = ctype_get(cts, cd->ctypeid);
  337. if (ctype_isextern(s->info)) {
  338. CTypeID sid = ctype_cid(s->info);
  339. void *sp = *(void **)cdataptr(cd);
  340. CType *ct = ctype_raw(cts, sid);
  341. if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))
  342. lj_gc_check(L);
  343. return 1;
  344. }
  345. }
  346. copyTV(L, L->top-1, tv);
  347. return 1;
  348. }
  349. LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0)
  350. {
  351. TValue *tv = ffi_clib_index(L);
  352. TValue *o = L->base+2;
  353. if (o < L->top && tviscdata(tv)) {
  354. CTState *cts = ctype_cts(L);
  355. GCcdata *cd = cdataV(tv);
  356. CType *d = ctype_get(cts, cd->ctypeid);
  357. if (ctype_isextern(d->info)) {
  358. CTInfo qual = 0;
  359. for (;;) { /* Skip attributes and collect qualifiers. */
  360. d = ctype_child(cts, d);
  361. if (!ctype_isattrib(d->info)) break;
  362. if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
  363. }
  364. if (!((d->info|qual) & CTF_CONST)) {
  365. lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);
  366. return 0;
  367. }
  368. }
  369. }
  370. lj_err_caller(L, LJ_ERR_FFI_WRCONST);
  371. return 0; /* unreachable */
  372. }
  373. LJLIB_CF(ffi_clib___gc)
  374. {
  375. TValue *o = L->base;
  376. if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)
  377. lj_clib_unload((CLibrary *)uddata(udataV(o)));
  378. return 0;
  379. }
  380. #include "lj_libdef.h"
  381. /* -- Callback function metamethods --------------------------------------- */
  382. #define LJLIB_MODULE_ffi_callback
  383. static int ffi_callback_set(lua_State *L, GCfunc *fn)
  384. {
  385. GCcdata *cd = ffi_checkcdata(L, 1);
  386. CTState *cts = ctype_cts(L);
  387. CType *ct = ctype_raw(cts, cd->ctypeid);
  388. if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) {
  389. MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd));
  390. if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) {
  391. GCtab *t = cts->miscmap;
  392. TValue *tv = lj_tab_setint(L, t, (int32_t)slot);
  393. if (fn) {
  394. setfuncV(L, tv, fn);
  395. lj_gc_anybarriert(L, t);
  396. } else {
  397. setnilV(tv);
  398. cts->cb.cbid[slot] = 0;
  399. cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid;
  400. }
  401. return 0;
  402. }
  403. }
  404. lj_err_caller(L, LJ_ERR_FFI_BADCBACK);
  405. return 0;
  406. }
  407. LJLIB_CF(ffi_callback_free)
  408. {
  409. return ffi_callback_set(L, NULL);
  410. }
  411. LJLIB_CF(ffi_callback_set)
  412. {
  413. GCfunc *fn = lj_lib_checkfunc(L, 2);
  414. return ffi_callback_set(L, fn);
  415. }
  416. LJLIB_PUSH(top-1) LJLIB_SET(__index)
  417. #include "lj_libdef.h"
  418. /* -- FFI library functions ----------------------------------------------- */
  419. #define LJLIB_MODULE_ffi
  420. LJLIB_CF(ffi_cdef)
  421. {
  422. GCstr *s = lj_lib_checkstr(L, 1);
  423. CPState cp;
  424. int errcode;
  425. cp.L = L;
  426. cp.cts = ctype_cts(L);
  427. cp.srcname = strdata(s);
  428. cp.p = strdata(s);
  429. cp.param = L->base+1;
  430. cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;
  431. errcode = lj_cparse(&cp);
  432. if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
  433. lj_gc_check(L);
  434. return 0;
  435. }
  436. LJLIB_CF(ffi_new) LJLIB_REC(.)
  437. {
  438. CTState *cts = ctype_cts(L);
  439. CTypeID id = ffi_checkctype(L, cts, NULL);
  440. CType *ct = ctype_raw(cts, id);
  441. CTSize sz;
  442. CTInfo info = lj_ctype_info(cts, id, &sz);
  443. TValue *o = L->base+1;
  444. GCcdata *cd;
  445. if ((info & CTF_VLA)) {
  446. o++;
  447. sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
  448. }
  449. if (sz == CTSIZE_INVALID)
  450. lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);
  451. if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)
  452. cd = lj_cdata_new(cts, id, sz);
  453. else
  454. cd = lj_cdata_newv(cts, id, sz, ctype_align(info));
  455. setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */
  456. lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
  457. o, (MSize)(L->top - o)); /* Initialize cdata. */
  458. if (ctype_isstruct(ct->info)) {
  459. /* Handle ctype __gc metamethod. Use the fast lookup here. */
  460. cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
  461. if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
  462. GCtab *t = cts->finalizer;
  463. if (gcref(t->metatable)) {
  464. /* Add to finalizer table, if still enabled. */
  465. copyTV(L, lj_tab_set(L, t, o-1), tv);
  466. lj_gc_anybarriert(L, t);
  467. cd->marked |= LJ_GC_CDATA_FIN;
  468. }
  469. }
  470. }
  471. L->top = o; /* Only return the cdata itself. */
  472. lj_gc_check(L);
  473. return 1;
  474. }
  475. LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
  476. {
  477. CTState *cts = ctype_cts(L);
  478. CTypeID id = ffi_checkctype(L, cts, NULL);
  479. CType *d = ctype_raw(cts, id);
  480. TValue *o = lj_lib_checkany(L, 2);
  481. L->top = o+1; /* Make sure this is the last item on the stack. */
  482. if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
  483. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  484. if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) {
  485. GCcdata *cd = lj_cdata_new(cts, id, d->size);
  486. lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
  487. setcdataV(L, o, cd);
  488. lj_gc_check(L);
  489. }
  490. return 1;
  491. }
  492. LJLIB_CF(ffi_typeof) LJLIB_REC(.)
  493. {
  494. CTState *cts = ctype_cts(L);
  495. CTypeID id = ffi_checkctype(L, cts, L->base+1);
  496. GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  497. *(CTypeID *)cdataptr(cd) = id;
  498. setcdataV(L, L->top-1, cd);
  499. lj_gc_check(L);
  500. return 1;
  501. }
  502. LJLIB_CF(ffi_istype) LJLIB_REC(.)
  503. {
  504. CTState *cts = ctype_cts(L);
  505. CTypeID id1 = ffi_checkctype(L, cts, NULL);
  506. TValue *o = lj_lib_checkany(L, 2);
  507. int b = 0;
  508. if (tviscdata(o)) {
  509. GCcdata *cd = cdataV(o);
  510. CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
  511. cd->ctypeid;
  512. CType *ct1 = lj_ctype_rawref(cts, id1);
  513. CType *ct2 = lj_ctype_rawref(cts, id2);
  514. if (ct1 == ct2) {
  515. b = 1;
  516. } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
  517. ct1->size == ct2->size) {
  518. if (ctype_ispointer(ct1->info))
  519. b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
  520. else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
  521. b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0);
  522. } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
  523. ct1 == ctype_rawchild(cts, ct2)) {
  524. b = 1;
  525. }
  526. }
  527. setboolV(L->top-1, b);
  528. setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
  529. return 1;
  530. }
  531. LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof)
  532. {
  533. CTState *cts = ctype_cts(L);
  534. CTypeID id = ffi_checkctype(L, cts, NULL);
  535. CTSize sz;
  536. if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
  537. sz = cdatavlen(cdataV(L->base));
  538. } else {
  539. CType *ct = lj_ctype_rawref(cts, id);
  540. if (ctype_isvltype(ct->info))
  541. sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
  542. else
  543. sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
  544. if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
  545. setnilV(L->top-1);
  546. return 1;
  547. }
  548. }
  549. setintV(L->top-1, (int32_t)sz);
  550. return 1;
  551. }
  552. LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof)
  553. {
  554. CTState *cts = ctype_cts(L);
  555. CTypeID id = ffi_checkctype(L, cts, NULL);
  556. CTSize sz = 0;
  557. CTInfo info = lj_ctype_info(cts, id, &sz);
  558. setintV(L->top-1, 1 << ctype_align(info));
  559. return 1;
  560. }
  561. LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof)
  562. {
  563. CTState *cts = ctype_cts(L);
  564. CTypeID id = ffi_checkctype(L, cts, NULL);
  565. GCstr *name = lj_lib_checkstr(L, 2);
  566. CType *ct = lj_ctype_rawref(cts, id);
  567. CTSize ofs;
  568. if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
  569. CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
  570. if (fct) {
  571. setintV(L->top-1, ofs);
  572. if (ctype_isfield(fct->info)) {
  573. return 1;
  574. } else if (ctype_isbitfield(fct->info)) {
  575. setintV(L->top++, ctype_bitpos(fct->info));
  576. setintV(L->top++, ctype_bitbsz(fct->info));
  577. return 3;
  578. }
  579. }
  580. }
  581. return 0;
  582. }
  583. LJLIB_CF(ffi_errno) LJLIB_REC(.)
  584. {
  585. int err = errno;
  586. if (L->top > L->base)
  587. errno = ffi_checkint(L, 1);
  588. setintV(L->top++, err);
  589. return 1;
  590. }
  591. LJLIB_CF(ffi_string) LJLIB_REC(.)
  592. {
  593. CTState *cts = ctype_cts(L);
  594. TValue *o = lj_lib_checkany(L, 1);
  595. const char *p;
  596. size_t len;
  597. if (o+1 < L->top && !tvisnil(o+1)) {
  598. len = (size_t)ffi_checkint(L, 2);
  599. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
  600. CCF_ARG(1));
  601. } else {
  602. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
  603. CCF_ARG(1));
  604. len = strlen(p);
  605. }
  606. L->top = o+1; /* Make sure this is the last item on the stack. */
  607. setstrV(L, o, lj_str_new(L, p, len));
  608. lj_gc_check(L);
  609. return 1;
  610. }
  611. LJLIB_CF(ffi_copy) LJLIB_REC(.)
  612. {
  613. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  614. void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
  615. TValue *o = L->base+1;
  616. CTSize len;
  617. if (tvisstr(o) && o+1 >= L->top)
  618. len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */
  619. else
  620. len = (CTSize)ffi_checkint(L, 3);
  621. memcpy(dp, sp, len);
  622. return 0;
  623. }
  624. LJLIB_CF(ffi_fill) LJLIB_REC(.)
  625. {
  626. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  627. CTSize len = (CTSize)ffi_checkint(L, 2);
  628. int32_t fill = 0;
  629. if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
  630. memset(dp, fill, len);
  631. return 0;
  632. }
  633. #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be)
  634. /* Test ABI string. */
  635. LJLIB_CF(ffi_abi) LJLIB_REC(.)
  636. {
  637. GCstr *s = lj_lib_checkstr(L, 1);
  638. int b = 0;
  639. switch (s->hash) {
  640. #if LJ_64
  641. case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */
  642. #else
  643. case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */
  644. #endif
  645. #if LJ_ARCH_HASFPU
  646. case H_(e33ee463,e33ee463): b = 1; break; /* fpu */
  647. #endif
  648. #if LJ_ABI_SOFTFP
  649. case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */
  650. #else
  651. case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */
  652. #endif
  653. #if LJ_ABI_EABI
  654. case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */
  655. #endif
  656. #if LJ_ABI_WIN
  657. case H_(4ab624a8,4ab624a8): b = 1; break; /* win */
  658. #endif
  659. case H_(3af93066,1f001464): b = 1; break; /* le/be */
  660. default:
  661. break;
  662. }
  663. setboolV(L->top-1, b);
  664. setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
  665. return 1;
  666. }
  667. #undef H_
  668. LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */
  669. LJLIB_CF(ffi_metatype)
  670. {
  671. CTState *cts = ctype_cts(L);
  672. CTypeID id = ffi_checkctype(L, cts, NULL);
  673. GCtab *mt = lj_lib_checktab(L, 2);
  674. GCtab *t = cts->miscmap;
  675. CType *ct = ctype_get(cts, id); /* Only allow raw types. */
  676. TValue *tv;
  677. GCcdata *cd;
  678. if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
  679. ctype_isvector(ct->info)))
  680. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  681. tv = lj_tab_setinth(L, t, -(int32_t)id);
  682. if (!tvisnil(tv))
  683. lj_err_caller(L, LJ_ERR_PROTMT);
  684. settabV(L, tv, mt);
  685. lj_gc_anybarriert(L, t);
  686. cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  687. *(CTypeID *)cdataptr(cd) = id;
  688. setcdataV(L, L->top-1, cd);
  689. lj_gc_check(L);
  690. return 1;
  691. }
  692. LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
  693. LJLIB_CF(ffi_gc) LJLIB_REC(.)
  694. {
  695. GCcdata *cd = ffi_checkcdata(L, 1);
  696. TValue *fin = lj_lib_checkany(L, 2);
  697. CTState *cts = ctype_cts(L);
  698. GCtab *t = cts->finalizer;
  699. CType *ct = ctype_raw(cts, cd->ctypeid);
  700. if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
  701. ctype_isrefarray(ct->info)))
  702. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  703. if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */
  704. copyTV(L, lj_tab_set(L, t, L->base), fin);
  705. lj_gc_anybarriert(L, t);
  706. if (!tvisnil(fin))
  707. cd->marked |= LJ_GC_CDATA_FIN;
  708. else
  709. cd->marked &= ~LJ_GC_CDATA_FIN;
  710. }
  711. L->top = L->base+1; /* Pass through the cdata object. */
  712. return 1;
  713. }
  714. LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
  715. LJLIB_CF(ffi_load)
  716. {
  717. GCstr *name = lj_lib_checkstr(L, 1);
  718. int global = (L->base+1 < L->top && tvistruecond(L->base+1));
  719. lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
  720. return 1;
  721. }
  722. LJLIB_PUSH(top-4) LJLIB_SET(C)
  723. LJLIB_PUSH(top-3) LJLIB_SET(os)
  724. LJLIB_PUSH(top-2) LJLIB_SET(arch)
  725. #include "lj_libdef.h"
  726. /* ------------------------------------------------------------------------ */
  727. /* Create special weak-keyed finalizer table. */
  728. static GCtab *ffi_finalizer(lua_State *L)
  729. {
  730. /* NOBARRIER: The table is new (marked white). */
  731. GCtab *t = lj_tab_new(L, 0, 1);
  732. settabV(L, L->top++, t);
  733. setgcref(t->metatable, obj2gco(t));
  734. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
  735. lj_str_newlit(L, "K"));
  736. t->nomm = (uint8_t)(~(1u<<MM_mode));
  737. return t;
  738. }
  739. /* Register FFI module as loaded. */
  740. static void ffi_register_module(lua_State *L)
  741. {
  742. cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
  743. if (tmp && tvistab(tmp)) {
  744. GCtab *t = tabV(tmp);
  745. copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
  746. lj_gc_anybarriert(L, t);
  747. }
  748. }
  749. LUALIB_API int luaopen_ffi(lua_State *L)
  750. {
  751. CTState *cts = lj_ctype_init(L);
  752. settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
  753. cts->finalizer = ffi_finalizer(L);
  754. LJ_LIB_REG(L, NULL, ffi_meta);
  755. /* NOBARRIER: basemt is a GC root. */
  756. setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
  757. LJ_LIB_REG(L, NULL, ffi_clib);
  758. LJ_LIB_REG(L, NULL, ffi_callback);
  759. /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */
  760. settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));
  761. L->top--;
  762. lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */
  763. lua_pushliteral(L, LJ_OS_NAME);
  764. lua_pushliteral(L, LJ_ARCH_NAME);
  765. LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */
  766. ffi_register_module(L);
  767. return 1;
  768. }
  769. #endif