FFI: Extend metamethod tutorial.
[luajit-2.0.git] / src / lib_ffi.c
blob82942e44c3616af5c668170a1a45b12775c93715
1 /*
2 ** FFI library.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lib_ffi_c
7 #define LUA_LIB
9 #include <errno.h>
11 #include "lua.h"
12 #include "lauxlib.h"
13 #include "lualib.h"
15 #include "lj_obj.h"
17 #if LJ_HASFFI
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_clib.h"
31 #include "lj_ff.h"
32 #include "lj_lib.h"
34 /* -- C type checks ------------------------------------------------------- */
36 /* Check first argument for a C type and returns its ID. */
37 static CTypeID ffi_checkctype(lua_State *L, CTState *cts)
39 TValue *o = L->base;
40 if (!(o < L->top)) {
41 err_argtype:
42 lj_err_argtype(L, 1, "C type");
44 if (tvisstr(o)) { /* Parse an abstract C type declaration. */
45 GCstr *s = strV(o);
46 CPState cp;
47 int errcode;
48 cp.L = L;
49 cp.cts = cts;
50 cp.srcname = strdata(s);
51 cp.p = strdata(s);
52 cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
53 errcode = lj_cparse(&cp);
54 if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
55 return cp.val.id;
56 } else {
57 GCcdata *cd;
58 if (!tviscdata(o)) goto err_argtype;
59 cd = cdataV(o);
60 return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid;
64 /* Check argument for C data and return it. */
65 static GCcdata *ffi_checkcdata(lua_State *L, int narg)
67 TValue *o = L->base + narg-1;
68 if (!(o < L->top && tviscdata(o)))
69 lj_err_argt(L, narg, LUA_TCDATA);
70 return cdataV(o);
73 /* Convert argument to C pointer. */
74 static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
76 CTState *cts = ctype_cts(L);
77 TValue *o = L->base + narg-1;
78 void *p;
79 if (o >= L->top)
80 lj_err_arg(L, narg, LJ_ERR_NOVAL);
81 lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg));
82 return p;
85 /* Convert argument to int32_t. */
86 static int32_t ffi_checkint(lua_State *L, int narg)
88 CTState *cts = ctype_cts(L);
89 TValue *o = L->base + narg-1;
90 int32_t i;
91 if (o >= L->top)
92 lj_err_arg(L, narg, LJ_ERR_NOVAL);
93 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o,
94 CCF_ARG(narg));
95 return i;
98 /* -- C type metamethods -------------------------------------------------- */
100 #define LJLIB_MODULE_ffi_meta
102 /* Handle ctype __index/__newindex metamethods. */
103 static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)
105 CTypeID id = ctype_typeid(cts, ct);
106 cTValue *tv = lj_ctype_meta(cts, id, mm);
107 TValue *base = L->base;
108 if (!tv) {
109 const char *s;
110 err_index:
111 s = strdata(lj_ctype_repr(L, id, NULL));
112 if (tvisstr(L->base+1))
113 lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1));
114 else
115 lj_err_callerv(L, LJ_ERR_FFI_BADIDX, s);
117 if (!tvisfunc(tv)) {
118 if (mm == MM_index) {
119 cTValue *o = lj_meta_tget(L, tv, base+1);
120 if (o) {
121 if (tvisnil(o)) goto err_index;
122 copyTV(L, L->top-1, o);
123 return 1;
125 } else {
126 TValue *o = lj_meta_tset(L, tv, base+1);
127 if (o) {
128 copyTV(L, o, base+2);
129 return 0;
132 tv = L->top-1;
134 return lj_meta_tailcall(L, tv);
137 LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
139 CTState *cts = ctype_cts(L);
140 CTInfo qual = 0;
141 CType *ct;
142 uint8_t *p;
143 TValue *o = L->base;
144 if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */
145 lj_err_argt(L, 1, LUA_TCDATA);
146 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
147 if ((qual & 1))
148 return ffi_index_meta(L, cts, ct, MM_index);
149 if (lj_cdata_get(cts, ct, L->top-1, p))
150 lj_gc_check(L);
151 return 1;
154 LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
156 CTState *cts = ctype_cts(L);
157 CTInfo qual = 0;
158 CType *ct;
159 uint8_t *p;
160 TValue *o = L->base;
161 if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */
162 lj_err_argt(L, 1, LUA_TCDATA);
163 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
164 if ((qual & 1)) {
165 if ((qual & CTF_CONST))
166 lj_err_caller(L, LJ_ERR_FFI_WRCONST);
167 return ffi_index_meta(L, cts, ct, MM_newindex);
169 lj_cdata_set(cts, ct, p, o+2, qual);
170 return 0;
173 /* Common handler for cdata arithmetic. */
174 static int ffi_arith(lua_State *L)
176 MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
177 return lj_carith_op(L, mm);
180 /* The following functions must be in contiguous ORDER MM. */
181 LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)
183 return ffi_arith(L);
186 LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len)
188 return ffi_arith(L);
191 LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt)
193 return ffi_arith(L);
196 LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le)
198 return ffi_arith(L);
201 LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat)
203 return ffi_arith(L);
206 /* Handle ctype __call metamethod. */
207 static int ffi_call_meta(lua_State *L, CTypeID id)
209 CTState *cts = ctype_cts(L);
210 cTValue *tv = lj_ctype_meta(cts, id, MM_call);
211 if (!tv)
212 lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL)));
213 return lj_meta_tailcall(L, tv);
216 /* Forward declaration. */
217 static int lj_cf_ffi_new(lua_State *L);
219 LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
221 GCcdata *cd = ffi_checkcdata(L, 1);
222 int ret;
223 if (cd->typeid == CTID_CTYPEID)
224 return lj_cf_ffi_new(L);
225 if ((ret = lj_ccall_func(L, cd)) < 0)
226 return ffi_call_meta(L, cd->typeid);
227 return ret;
230 LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)
232 return ffi_arith(L);
235 LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub)
237 return ffi_arith(L);
240 LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul)
242 return ffi_arith(L);
245 LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div)
247 return ffi_arith(L);
250 LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod)
252 return ffi_arith(L);
255 LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow)
257 return ffi_arith(L);
260 LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)
262 return ffi_arith(L);
264 /* End of contiguous ORDER MM. */
266 LJLIB_CF(ffi_meta___tostring)
268 GCcdata *cd = ffi_checkcdata(L, 1);
269 const char *msg = "cdata<%s>: %p";
270 CTypeID id = cd->typeid;
271 void *p = cdataptr(cd);
272 if (id == CTID_CTYPEID) {
273 msg = "ctype<%s>";
274 id = *(CTypeID *)p;
275 } else {
276 CTState *cts = ctype_cts(L);
277 CType *ct = ctype_raw(cts, id);
278 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
279 if (ctype_iscomplex(ct->info)) {
280 setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size));
281 goto checkgc;
282 } else if (ct->size == 8 && ctype_isinteger(ct->info)) {
283 setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),
284 (ct->info & CTF_UNSIGNED)));
285 goto checkgc;
286 } else if (ctype_isfunc(ct->info)) {
287 p = *(void **)p;
288 } else {
289 if (ctype_isptr(ct->info)) {
290 p = cdata_getptr(p, ct->size);
291 ct = ctype_rawchild(cts, ct);
293 if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) {
294 /* Handle ctype __tostring metamethod. */
295 cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring);
296 if (tv)
297 return lj_meta_tailcall(L, tv);
301 lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
302 checkgc:
303 lj_gc_check(L);
304 return 1;
307 LJLIB_PUSH("ffi") LJLIB_SET(__metatable)
309 #include "lj_libdef.h"
311 /* -- C library metamethods ----------------------------------------------- */
313 #define LJLIB_MODULE_ffi_clib
315 /* Index C library by a name. */
316 static TValue *ffi_clib_index(lua_State *L)
318 TValue *o = L->base;
319 CLibrary *cl;
320 if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))
321 lj_err_argt(L, 1, LUA_TUSERDATA);
322 cl = (CLibrary *)uddata(udataV(o));
323 if (!(o+1 < L->top && tvisstr(o+1)))
324 lj_err_argt(L, 2, LUA_TSTRING);
325 return lj_clib_index(L, cl, strV(o+1));
328 LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1)
330 TValue *tv = ffi_clib_index(L);
331 if (tviscdata(tv)) {
332 CTState *cts = ctype_cts(L);
333 GCcdata *cd = cdataV(tv);
334 CType *s = ctype_get(cts, cd->typeid);
335 if (ctype_isextern(s->info)) {
336 CTypeID sid = ctype_cid(s->info);
337 void *sp = *(void **)cdataptr(cd);
338 CType *ct = ctype_raw(cts, sid);
339 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
340 if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))
341 lj_gc_check(L);
342 return 1;
345 copyTV(L, L->top-1, tv);
346 return 1;
349 LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0)
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->typeid);
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;
364 if (!((d->info|qual) & CTF_CONST)) {
365 lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);
366 return 0;
370 lj_err_caller(L, LJ_ERR_FFI_WRCONST);
371 return 0; /* unreachable */
374 LJLIB_CF(ffi_clib___gc)
376 TValue *o = L->base;
377 if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)
378 lj_clib_unload((CLibrary *)uddata(udataV(o)));
379 return 0;
382 #include "lj_libdef.h"
384 /* -- FFI library functions ----------------------------------------------- */
386 #define LJLIB_MODULE_ffi
388 LJLIB_CF(ffi_cdef)
390 GCstr *s = lj_lib_checkstr(L, 1);
391 CPState cp;
392 int errcode;
393 cp.L = L;
394 cp.cts = ctype_cts(L);
395 cp.srcname = strdata(s);
396 cp.p = strdata(s);
397 cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;
398 errcode = lj_cparse(&cp);
399 if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
400 lj_gc_check(L);
401 return 0;
404 LJLIB_CF(ffi_new) LJLIB_REC(.)
406 CTState *cts = ctype_cts(L);
407 CTypeID id = ffi_checkctype(L, cts);
408 CType *ct = ctype_raw(cts, id);
409 CTSize sz;
410 CTInfo info = lj_ctype_info(cts, id, &sz);
411 TValue *o = L->base+1;
412 GCcdata *cd;
413 if ((info & CTF_VLA)) {
414 o++;
415 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
417 if (sz == CTSIZE_INVALID)
418 lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);
419 if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)
420 cd = lj_cdata_new(cts, id, sz);
421 else
422 cd = lj_cdata_newv(cts, id, sz, ctype_align(info));
423 setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */
424 lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
425 o, (MSize)(L->top - o)); /* Initialize cdata. */
426 if (ctype_isstruct(ct->info)) {
427 /* Handle ctype __gc metamethod. Use the fast lookup here. */
428 cTValue *tv = lj_tab_getint(cts->metatype, (int32_t)id);
429 if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
430 GCtab *t = cts->finalizer;
431 if (gcref(t->metatable)) {
432 /* Add to finalizer table, if still enabled. */
433 copyTV(L, lj_tab_set(L, t, o-1), tv);
434 lj_gc_anybarriert(L, t);
435 cd->marked |= LJ_GC_CDATA_FIN;
439 L->top = o; /* Only return the cdata itself. */
440 lj_gc_check(L);
441 return 1;
444 LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
446 CTState *cts = ctype_cts(L);
447 CTypeID id = ffi_checkctype(L, cts);
448 CType *d = ctype_raw(cts, id);
449 TValue *o = lj_lib_checkany(L, 2);
450 L->top = o+1; /* Make sure this is the last item on the stack. */
451 if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
452 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
453 if (!(tviscdata(o) && cdataV(o)->typeid == id)) {
454 GCcdata *cd = lj_cdata_new(cts, id, d->size);
455 lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
456 setcdataV(L, o, cd);
457 lj_gc_check(L);
459 return 1;
462 LJLIB_CF(ffi_typeof)
464 CTState *cts = ctype_cts(L);
465 CTypeID id = ffi_checkctype(L, cts);
466 GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
467 *(CTypeID *)cdataptr(cd) = id;
468 setcdataV(L, L->top-1, cd);
469 lj_gc_check(L);
470 return 1;
473 LJLIB_CF(ffi_istype) LJLIB_REC(ffi_istype)
475 CTState *cts = ctype_cts(L);
476 CTypeID id1 = ffi_checkctype(L, cts);
477 TValue *o = lj_lib_checkany(L, 2);
478 int b = 0;
479 if (tviscdata(o)) {
480 GCcdata *cd = cdataV(o);
481 CTypeID id2 = cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
482 cd->typeid;
483 CType *ct1 = lj_ctype_rawref(cts, id1);
484 CType *ct2 = lj_ctype_rawref(cts, id2);
485 if (ct1 == ct2) {
486 b = 1;
487 } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
488 ct1->size == ct2->size) {
489 if (ctype_ispointer(ct1->info))
490 b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
491 else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
492 b = (((ct1->info ^ ct2->info) & ~CTF_QUAL) == 0);
493 } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
494 ct1 == ctype_rawchild(cts, ct2)) {
495 b = 1;
498 setboolV(L->top-1, b);
499 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
500 return 1;
503 LJLIB_CF(ffi_sizeof)
505 CTState *cts = ctype_cts(L);
506 CTypeID id = ffi_checkctype(L, cts);
507 CTSize sz;
508 if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
509 sz = cdatavlen(cdataV(L->base));
510 } else {
511 CType *ct = lj_ctype_rawref(cts, id);
512 if (ctype_isvltype(ct->info))
513 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
514 else
515 sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
516 if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
517 setnilV(L->top-1);
518 return 1;
521 setintV(L->top-1, (int32_t)sz);
522 return 1;
525 LJLIB_CF(ffi_alignof)
527 CTState *cts = ctype_cts(L);
528 CTypeID id = ffi_checkctype(L, cts);
529 CTSize sz = 0;
530 CTInfo info = lj_ctype_info(cts, id, &sz);
531 setintV(L->top-1, 1 << ctype_align(info));
532 return 1;
535 LJLIB_CF(ffi_offsetof)
537 CTState *cts = ctype_cts(L);
538 CTypeID id = ffi_checkctype(L, cts);
539 GCstr *name = lj_lib_checkstr(L, 2);
540 CType *ct = lj_ctype_rawref(cts, id);
541 CTSize ofs;
542 if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
543 CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
544 if (fct) {
545 setintV(L->top-1, ofs);
546 if (ctype_isfield(fct->info)) {
547 return 1;
548 } else if (ctype_isbitfield(fct->info)) {
549 setintV(L->top++, ctype_bitpos(fct->info));
550 setintV(L->top++, ctype_bitbsz(fct->info));
551 return 3;
555 return 0;
558 LJLIB_CF(ffi_errno)
560 int err = errno;
561 if (L->top > L->base)
562 errno = ffi_checkint(L, 1);
563 setintV(L->top++, err);
564 return 1;
567 LJLIB_CF(ffi_string) LJLIB_REC(.)
569 CTState *cts = ctype_cts(L);
570 TValue *o = lj_lib_checkany(L, 1);
571 const char *p;
572 size_t len;
573 if (o+1 < L->top) {
574 len = (size_t)ffi_checkint(L, 2);
575 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
576 CCF_ARG(1));
577 } else {
578 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
579 CCF_ARG(1));
580 len = strlen(p);
582 L->top = o+1; /* Make sure this is the last item on the stack. */
583 setstrV(L, o, lj_str_new(L, p, len));
584 lj_gc_check(L);
585 return 1;
588 LJLIB_CF(ffi_copy) LJLIB_REC(.)
590 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
591 void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
592 TValue *o = L->base+1;
593 CTSize len;
594 if (tvisstr(o) && o+1 >= L->top)
595 len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */
596 else
597 len = (CTSize)ffi_checkint(L, 3);
598 memcpy(dp, sp, len);
599 return 0;
602 LJLIB_CF(ffi_fill) LJLIB_REC(.)
604 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
605 CTSize len = (CTSize)ffi_checkint(L, 2);
606 int32_t fill = 0;
607 if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
608 memset(dp, fill, len);
609 return 0;
612 #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be)
614 /* Test ABI string. */
615 LJLIB_CF(ffi_abi) LJLIB_REC(.)
617 GCstr *s = lj_lib_checkstr(L, 1);
618 int b = 0;
619 switch (s->hash) {
620 #if LJ_64
621 case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */
622 #else
623 case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */
624 #endif
625 #if LJ_ARCH_HASFPU
626 case H_(e33ee463,e33ee463): b = 1; break; /* fpu */
627 #endif
628 #if LJ_ABI_SOFTFP
629 case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */
630 #else
631 case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */
632 #endif
633 #if LJ_ABI_EABI
634 case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */
635 #endif
636 #if LJ_ABI_WIN
637 case H_(4ab624a8,4ab624a8): b = 1; break; /* win */
638 #endif
639 case H_(3af93066,1f001464): b = 1; break; /* le/be */
640 default:
641 break;
643 setboolV(L->top-1, b);
644 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
645 return 1;
648 #undef H_
650 LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to metatype table. */
652 LJLIB_CF(ffi_metatype)
654 CTState *cts = ctype_cts(L);
655 CTypeID id = ffi_checkctype(L, cts);
656 GCtab *mt = lj_lib_checktab(L, 2);
657 GCtab *t = cts->metatype;
658 CType *ct = ctype_get(cts, id); /* Only allow raw types. */
659 TValue *tv;
660 GCcdata *cd;
661 if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
662 ctype_isvector(ct->info)))
663 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
664 tv = lj_tab_setint(L, t, (int32_t)id);
665 if (!tvisnil(tv))
666 lj_err_caller(L, LJ_ERR_PROTMT);
667 settabV(L, tv, mt);
668 lj_gc_anybarriert(L, t);
669 cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
670 *(CTypeID *)cdataptr(cd) = id;
671 setcdataV(L, L->top-1, cd);
672 lj_gc_check(L);
673 return 1;
676 LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
678 LJLIB_CF(ffi_gc)
680 GCcdata *cd = ffi_checkcdata(L, 1);
681 TValue *fin = lj_lib_checkany(L, 2);
682 CTState *cts = ctype_cts(L);
683 GCtab *t = cts->finalizer;
684 CType *ct = ctype_raw(cts, cd->typeid);
685 if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
686 ctype_isrefarray(ct->info)))
687 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
688 if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */
689 copyTV(L, lj_tab_set(L, t, L->base), fin);
690 lj_gc_anybarriert(L, t);
691 if (!tvisnil(fin))
692 cd->marked |= LJ_GC_CDATA_FIN;
693 else
694 cd->marked &= ~LJ_GC_CDATA_FIN;
696 L->top = L->base+1; /* Pass through the cdata object. */
697 return 1;
700 LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
702 LJLIB_CF(ffi_load)
704 GCstr *name = lj_lib_checkstr(L, 1);
705 int global = (L->base+1 < L->top && tvistruecond(L->base+1));
706 lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
707 return 1;
710 LJLIB_PUSH(top-4) LJLIB_SET(C)
711 LJLIB_PUSH(top-3) LJLIB_SET(os)
712 LJLIB_PUSH(top-2) LJLIB_SET(arch)
714 #include "lj_libdef.h"
716 /* ------------------------------------------------------------------------ */
718 /* Create special weak-keyed finalizer table. */
719 static GCtab *ffi_finalizer(lua_State *L)
721 /* NOBARRIER: The table is new (marked white). */
722 GCtab *t = lj_tab_new(L, 0, 1);
723 settabV(L, L->top++, t);
724 setgcref(t->metatable, obj2gco(t));
725 setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
726 lj_str_newlit(L, "K"));
727 t->nomm = (uint8_t)(~(1u<<MM_mode));
728 return t;
731 /* Register FFI module as loaded. */
732 static void ffi_register_module(lua_State *L)
734 cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
735 if (tmp && tvistab(tmp)) {
736 GCtab *t = tabV(tmp);
737 copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
738 lj_gc_anybarriert(L, t);
742 LUALIB_API int luaopen_ffi(lua_State *L)
744 CTState *cts = lj_ctype_init(L);
745 settabV(L, L->top++, (cts->metatype = lj_tab_new(L, 0, 0)));
746 cts->finalizer = ffi_finalizer(L);
747 LJ_LIB_REG(L, NULL, ffi_meta);
748 /* NOBARRIER: basemt is a GC root. */
749 setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
750 LJ_LIB_REG(L, NULL, ffi_clib);
751 lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */
752 lua_pushliteral(L, LJ_OS_NAME);
753 lua_pushliteral(L, LJ_ARCH_NAME);
754 LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */
755 ffi_register_module(L);
756 return 1;
759 #endif