lib_ffi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. ** FFI library.
  3. ** Copyright (C) 2005-2023 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. cd = lj_cdata_newx(cts, id, sz, info);
  454. setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */
  455. lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
  456. o, (MSize)(L->top - o)); /* Initialize cdata. */
  457. if (ctype_isstruct(ct->info)) {
  458. /* Handle ctype __gc metamethod. Use the fast lookup here. */
  459. cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
  460. if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
  461. GCtab *t = cts->finalizer;
  462. if (gcref(t->metatable)) {
  463. /* Add to finalizer table, if still enabled. */
  464. copyTV(L, lj_tab_set(L, t, o-1), tv);
  465. lj_gc_anybarriert(L, t);
  466. cd->marked |= LJ_GC_CDATA_FIN;
  467. }
  468. }
  469. }
  470. L->top = o; /* Only return the cdata itself. */
  471. lj_gc_check(L);
  472. return 1;
  473. }
  474. LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
  475. {
  476. CTState *cts = ctype_cts(L);
  477. CTypeID id = ffi_checkctype(L, cts, NULL);
  478. CType *d = ctype_raw(cts, id);
  479. TValue *o = lj_lib_checkany(L, 2);
  480. L->top = o+1; /* Make sure this is the last item on the stack. */
  481. if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
  482. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  483. if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) {
  484. GCcdata *cd = lj_cdata_new(cts, id, d->size);
  485. lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
  486. setcdataV(L, o, cd);
  487. lj_gc_check(L);
  488. }
  489. return 1;
  490. }
  491. LJLIB_CF(ffi_typeof) LJLIB_REC(.)
  492. {
  493. CTState *cts = ctype_cts(L);
  494. CTypeID id = ffi_checkctype(L, cts, L->base+1);
  495. GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  496. *(CTypeID *)cdataptr(cd) = id;
  497. setcdataV(L, L->top-1, cd);
  498. lj_gc_check(L);
  499. return 1;
  500. }
  501. /* Internal and unsupported API. */
  502. LJLIB_CF(ffi_typeinfo)
  503. {
  504. CTState *cts = ctype_cts(L);
  505. CTypeID id = (CTypeID)ffi_checkint(L, 1);
  506. if (id > 0 && id < cts->top) {
  507. CType *ct = ctype_get(cts, id);
  508. GCtab *t;
  509. lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */
  510. t = tabV(L->top-1);
  511. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "info")), (int32_t)ct->info);
  512. if (ct->size != CTSIZE_INVALID)
  513. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "size")), (int32_t)ct->size);
  514. if (ct->sib)
  515. setintV(lj_tab_setstr(L, t, lj_str_newlit(L, "sib")), (int32_t)ct->sib);
  516. if (gcref(ct->name)) {
  517. GCstr *s = gco2str(gcref(ct->name));
  518. if (isdead(G(L), obj2gco(s))) flipwhite(obj2gco(s));
  519. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "name")), s);
  520. }
  521. lj_gc_check(L);
  522. return 1;
  523. }
  524. return 0;
  525. }
  526. LJLIB_CF(ffi_istype) LJLIB_REC(.)
  527. {
  528. CTState *cts = ctype_cts(L);
  529. CTypeID id1 = ffi_checkctype(L, cts, NULL);
  530. TValue *o = lj_lib_checkany(L, 2);
  531. int b = 0;
  532. if (tviscdata(o)) {
  533. GCcdata *cd = cdataV(o);
  534. CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
  535. cd->ctypeid;
  536. CType *ct1 = lj_ctype_rawref(cts, id1);
  537. CType *ct2 = lj_ctype_rawref(cts, id2);
  538. if (ct1 == ct2) {
  539. b = 1;
  540. } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
  541. ct1->size == ct2->size) {
  542. if (ctype_ispointer(ct1->info))
  543. b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
  544. else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
  545. b = (((ct1->info ^ ct2->info) & ~(CTF_QUAL|CTF_LONG)) == 0);
  546. } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
  547. ct1 == ctype_rawchild(cts, ct2)) {
  548. b = 1;
  549. }
  550. }
  551. setboolV(L->top-1, b);
  552. setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
  553. return 1;
  554. }
  555. LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof)
  556. {
  557. CTState *cts = ctype_cts(L);
  558. CTypeID id = ffi_checkctype(L, cts, NULL);
  559. CTSize sz;
  560. if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
  561. sz = cdatavlen(cdataV(L->base));
  562. } else {
  563. CType *ct = lj_ctype_rawref(cts, id);
  564. if (ctype_isvltype(ct->info))
  565. sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
  566. else
  567. sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
  568. if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
  569. setnilV(L->top-1);
  570. return 1;
  571. }
  572. }
  573. setintV(L->top-1, (int32_t)sz);
  574. return 1;
  575. }
  576. LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof)
  577. {
  578. CTState *cts = ctype_cts(L);
  579. CTypeID id = ffi_checkctype(L, cts, NULL);
  580. CTSize sz = 0;
  581. CTInfo info = lj_ctype_info_raw(cts, id, &sz);
  582. setintV(L->top-1, 1 << ctype_align(info));
  583. return 1;
  584. }
  585. LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof)
  586. {
  587. CTState *cts = ctype_cts(L);
  588. CTypeID id = ffi_checkctype(L, cts, NULL);
  589. GCstr *name = lj_lib_checkstr(L, 2);
  590. CType *ct = lj_ctype_rawref(cts, id);
  591. CTSize ofs;
  592. if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
  593. CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
  594. if (fct) {
  595. setintV(L->top-1, ofs);
  596. if (ctype_isfield(fct->info)) {
  597. return 1;
  598. } else if (ctype_isbitfield(fct->info)) {
  599. setintV(L->top++, ctype_bitpos(fct->info));
  600. setintV(L->top++, ctype_bitbsz(fct->info));
  601. return 3;
  602. }
  603. }
  604. }
  605. return 0;
  606. }
  607. LJLIB_CF(ffi_errno) LJLIB_REC(.)
  608. {
  609. int err = errno;
  610. if (L->top > L->base)
  611. errno = ffi_checkint(L, 1);
  612. setintV(L->top++, err);
  613. return 1;
  614. }
  615. LJLIB_CF(ffi_string) LJLIB_REC(.)
  616. {
  617. CTState *cts = ctype_cts(L);
  618. TValue *o = lj_lib_checkany(L, 1);
  619. const char *p;
  620. size_t len;
  621. if (o+1 < L->top && !tvisnil(o+1)) {
  622. len = (size_t)ffi_checkint(L, 2);
  623. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
  624. CCF_ARG(1));
  625. } else {
  626. lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
  627. CCF_ARG(1));
  628. len = strlen(p);
  629. }
  630. L->top = o+1; /* Make sure this is the last item on the stack. */
  631. setstrV(L, o, lj_str_new(L, p, len));
  632. lj_gc_check(L);
  633. return 1;
  634. }
  635. LJLIB_CF(ffi_copy) LJLIB_REC(.)
  636. {
  637. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  638. void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
  639. TValue *o = L->base+1;
  640. CTSize len;
  641. if (tvisstr(o) && o+1 >= L->top)
  642. len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */
  643. else
  644. len = (CTSize)ffi_checkint(L, 3);
  645. memcpy(dp, sp, len);
  646. return 0;
  647. }
  648. LJLIB_CF(ffi_fill) LJLIB_REC(.)
  649. {
  650. void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
  651. CTSize len = (CTSize)ffi_checkint(L, 2);
  652. int32_t fill = 0;
  653. if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
  654. memset(dp, fill, len);
  655. return 0;
  656. }
  657. /* Test ABI string. */
  658. LJLIB_CF(ffi_abi) LJLIB_REC(.)
  659. {
  660. GCstr *s = lj_lib_checkstr(L, 1);
  661. int b = lj_cparse_case(s,
  662. #if LJ_64
  663. "\00564bit"
  664. #else
  665. "\00532bit"
  666. #endif
  667. #if LJ_ARCH_HASFPU
  668. "\003fpu"
  669. #endif
  670. #if LJ_ABI_SOFTFP
  671. "\006softfp"
  672. #else
  673. "\006hardfp"
  674. #endif
  675. #if LJ_ABI_EABI
  676. "\004eabi"
  677. #endif
  678. #if LJ_ABI_WIN
  679. "\003win"
  680. #endif
  681. #if LJ_ABI_PAUTH
  682. "\005pauth"
  683. #endif
  684. #if LJ_TARGET_UWP
  685. "\003uwp"
  686. #endif
  687. #if LJ_LE
  688. "\002le"
  689. #else
  690. "\002be"
  691. #endif
  692. #if LJ_GC64
  693. "\004gc64"
  694. #endif
  695. ) >= 0;
  696. setboolV(L->top-1, b);
  697. setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
  698. return 1;
  699. }
  700. LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */
  701. LJLIB_CF(ffi_metatype)
  702. {
  703. CTState *cts = ctype_cts(L);
  704. CTypeID id = ffi_checkctype(L, cts, NULL);
  705. GCtab *mt = lj_lib_checktab(L, 2);
  706. GCtab *t = cts->miscmap;
  707. CType *ct = ctype_raw(cts, id);
  708. TValue *tv;
  709. GCcdata *cd;
  710. if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
  711. ctype_isvector(ct->info)))
  712. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  713. tv = lj_tab_setinth(L, t, -(int32_t)ctype_typeid(cts, ct));
  714. if (!tvisnil(tv))
  715. lj_err_caller(L, LJ_ERR_PROTMT);
  716. settabV(L, tv, mt);
  717. lj_gc_anybarriert(L, t);
  718. cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
  719. *(CTypeID *)cdataptr(cd) = id;
  720. setcdataV(L, L->top-1, cd);
  721. lj_gc_check(L);
  722. return 1;
  723. }
  724. LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
  725. LJLIB_CF(ffi_gc) LJLIB_REC(.)
  726. {
  727. GCcdata *cd = ffi_checkcdata(L, 1);
  728. TValue *fin = lj_lib_checkany(L, 2);
  729. CTState *cts = ctype_cts(L);
  730. CType *ct = ctype_raw(cts, cd->ctypeid);
  731. if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
  732. ctype_isrefarray(ct->info)))
  733. lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
  734. lj_cdata_setfin(L, cd, gcval(fin), itype(fin));
  735. L->top = L->base+1; /* Pass through the cdata object. */
  736. return 1;
  737. }
  738. LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
  739. LJLIB_CF(ffi_load)
  740. {
  741. GCstr *name = lj_lib_checkstr(L, 1);
  742. int global = (L->base+1 < L->top && tvistruecond(L->base+1));
  743. lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
  744. return 1;
  745. }
  746. LJLIB_PUSH(top-4) LJLIB_SET(C)
  747. LJLIB_PUSH(top-3) LJLIB_SET(os)
  748. LJLIB_PUSH(top-2) LJLIB_SET(arch)
  749. #include "lj_libdef.h"
  750. /* ------------------------------------------------------------------------ */
  751. /* Create special weak-keyed finalizer table. */
  752. static GCtab *ffi_finalizer(lua_State *L)
  753. {
  754. /* NOBARRIER: The table is new (marked white). */
  755. GCtab *t = lj_tab_new(L, 0, 1);
  756. settabV(L, L->top++, t);
  757. setgcref(t->metatable, obj2gco(t));
  758. setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
  759. lj_str_newlit(L, "k"));
  760. t->nomm = (uint8_t)(~(1u<<MM_mode));
  761. return t;
  762. }
  763. /* Register FFI module as loaded. */
  764. static void ffi_register_module(lua_State *L)
  765. {
  766. cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
  767. if (tmp && tvistab(tmp)) {
  768. GCtab *t = tabV(tmp);
  769. copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
  770. lj_gc_anybarriert(L, t);
  771. }
  772. }
  773. LUALIB_API int luaopen_ffi(lua_State *L)
  774. {
  775. CTState *cts = lj_ctype_init(L);
  776. settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
  777. cts->finalizer = ffi_finalizer(L);
  778. LJ_LIB_REG(L, NULL, ffi_meta);
  779. /* NOBARRIER: basemt is a GC root. */
  780. setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
  781. LJ_LIB_REG(L, NULL, ffi_clib);
  782. LJ_LIB_REG(L, NULL, ffi_callback);
  783. /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */
  784. settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));
  785. L->top--;
  786. lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */
  787. lua_pushliteral(L, LJ_OS_NAME);
  788. lua_pushliteral(L, LJ_ARCH_NAME);
  789. LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */
  790. ffi_register_module(L);
  791. return 1;
  792. }
  793. #endif