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