Fix shrinking of direct mapped block in builtin allocator.
[luajit-2.0/celess22.git] / src / lib_ffi.c
blob24a6625c5b75bcb04f6cda02a69f60a895fa1526
1 /*
2 ** FFI library.
3 ** Copyright (C) 2005-2012 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_ccallback.h"
31 #include "lj_clib.h"
32 #include "lj_ff.h"
33 #include "lj_lib.h"
35 /* -- C type checks ------------------------------------------------------- */
37 /* Check first argument for a C type and returns its ID. */
38 static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param)
40 TValue *o = L->base;
41 if (!(o < L->top)) {
42 err_argtype:
43 lj_err_argtype(L, 1, "C type");
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;
67 /* Check argument for C data and return it. */
68 static GCcdata *ffi_checkcdata(lua_State *L, int narg)
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);
76 /* Convert argument to C pointer. */
77 static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
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;
88 /* Convert argument to int32_t. */
89 static int32_t ffi_checkint(lua_State *L, int narg)
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;
101 /* -- C type metamethods -------------------------------------------------- */
103 #define LJLIB_MODULE_ffi_meta
105 /* Handle ctype __index/__newindex metamethods. */
106 static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)
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);
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;
132 } else {
133 TValue *o = lj_meta_tset(L, tv, base+1);
134 if (o) {
135 copyTV(L, o, base+2);
136 return 0;
139 tv = L->top-1;
141 return lj_meta_tailcall(L, tv);
144 LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
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;
161 LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
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);
176 lj_cdata_set(cts, ct, p, o+2, qual);
177 return 0;
180 /* Common handler for cdata arithmetic. */
181 static int ffi_arith(lua_State *L)
183 MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
184 return lj_carith_op(L, mm);
187 /* The following functions must be in contiguous ORDER MM. */
188 LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)
190 return ffi_arith(L);
193 LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len)
195 return ffi_arith(L);
198 LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt)
200 return ffi_arith(L);
203 LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le)
205 return ffi_arith(L);
208 LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat)
210 return ffi_arith(L);
213 /* Forward declaration. */
214 static int lj_cf_ffi_new(lua_State *L);
216 LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
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;
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);
243 LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)
245 return ffi_arith(L);
248 LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub)
250 return ffi_arith(L);
253 LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul)
255 return ffi_arith(L);
258 LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div)
260 return ffi_arith(L);
263 LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod)
265 return ffi_arith(L);
268 LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow)
270 return ffi_arith(L);
273 LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)
275 return ffi_arith(L);
277 /* End of contiguous ORDER MM. */
279 LJLIB_CF(ffi_meta___tostring)
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);
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);
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);
320 lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
321 checkgc:
322 lj_gc_check(L);
323 return 1;
326 LJLIB_PUSH("ffi") LJLIB_SET(__metatable)
328 #include "lj_libdef.h"
330 /* -- C library metamethods ----------------------------------------------- */
332 #define LJLIB_MODULE_ffi_clib
334 /* Index C library by a name. */
335 static TValue *ffi_clib_index(lua_State *L)
337 TValue *o = L->base;
338 CLibrary *cl;
339 if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))
340 lj_err_argt(L, 1, LUA_TUSERDATA);
341 cl = (CLibrary *)uddata(udataV(o));
342 if (!(o+1 < L->top && tvisstr(o+1)))
343 lj_err_argt(L, 2, LUA_TSTRING);
344 return lj_clib_index(L, cl, strV(o+1));
347 LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1)
349 TValue *tv = ffi_clib_index(L);
350 if (tviscdata(tv)) {
351 CTState *cts = ctype_cts(L);
352 GCcdata *cd = cdataV(tv);
353 CType *s = ctype_get(cts, cd->ctypeid);
354 if (ctype_isextern(s->info)) {
355 CTypeID sid = ctype_cid(s->info);
356 void *sp = *(void **)cdataptr(cd);
357 CType *ct = ctype_raw(cts, sid);
358 if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))
359 lj_gc_check(L);
360 return 1;
363 copyTV(L, L->top-1, tv);
364 return 1;
367 LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0)
369 TValue *tv = ffi_clib_index(L);
370 TValue *o = L->base+2;
371 if (o < L->top && tviscdata(tv)) {
372 CTState *cts = ctype_cts(L);
373 GCcdata *cd = cdataV(tv);
374 CType *d = ctype_get(cts, cd->ctypeid);
375 if (ctype_isextern(d->info)) {
376 CTInfo qual = 0;
377 for (;;) { /* Skip attributes and collect qualifiers. */
378 d = ctype_child(cts, d);
379 if (!ctype_isattrib(d->info)) break;
380 if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
382 if (!((d->info|qual) & CTF_CONST)) {
383 lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);
384 return 0;
388 lj_err_caller(L, LJ_ERR_FFI_WRCONST);
389 return 0; /* unreachable */
392 LJLIB_CF(ffi_clib___gc)
394 TValue *o = L->base;
395 if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)
396 lj_clib_unload((CLibrary *)uddata(udataV(o)));
397 return 0;
400 #include "lj_libdef.h"
402 /* -- Callback function metamethods --------------------------------------- */
404 #define LJLIB_MODULE_ffi_callback
406 static int ffi_callback_set(lua_State *L, GCfunc *fn)
408 GCcdata *cd = ffi_checkcdata(L, 1);
409 CTState *cts = ctype_cts(L);
410 CType *ct = ctype_raw(cts, cd->ctypeid);
411 if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) {
412 MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd));
413 if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) {
414 GCtab *t = cts->miscmap;
415 TValue *tv = lj_tab_setint(L, t, (int32_t)slot);
416 if (fn) {
417 setfuncV(L, tv, fn);
418 lj_gc_anybarriert(L, t);
419 } else {
420 setnilV(tv);
421 cts->cb.cbid[slot] = 0;
422 cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid;
424 return 0;
427 lj_err_caller(L, LJ_ERR_FFI_BADCBACK);
428 return 0;
431 LJLIB_CF(ffi_callback_free)
433 return ffi_callback_set(L, NULL);
436 LJLIB_CF(ffi_callback_set)
438 GCfunc *fn = lj_lib_checkfunc(L, 2);
439 return ffi_callback_set(L, fn);
442 LJLIB_PUSH(top-1) LJLIB_SET(__index)
444 #include "lj_libdef.h"
446 /* -- FFI library functions ----------------------------------------------- */
448 #define LJLIB_MODULE_ffi
450 LJLIB_CF(ffi_cdef)
452 GCstr *s = lj_lib_checkstr(L, 1);
453 CPState cp;
454 int errcode;
455 cp.L = L;
456 cp.cts = ctype_cts(L);
457 cp.srcname = strdata(s);
458 cp.p = strdata(s);
459 cp.param = L->base+1;
460 cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;
461 errcode = lj_cparse(&cp);
462 if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
463 lj_gc_check(L);
464 return 0;
467 LJLIB_CF(ffi_new) LJLIB_REC(.)
469 CTState *cts = ctype_cts(L);
470 CTypeID id = ffi_checkctype(L, cts, NULL);
471 CType *ct = ctype_raw(cts, id);
472 CTSize sz;
473 CTInfo info = lj_ctype_info(cts, id, &sz);
474 TValue *o = L->base+1;
475 GCcdata *cd;
476 if ((info & CTF_VLA)) {
477 o++;
478 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
480 if (sz == CTSIZE_INVALID)
481 lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);
482 if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)
483 cd = lj_cdata_new(cts, id, sz);
484 else
485 cd = lj_cdata_newv(cts, id, sz, ctype_align(info));
486 setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */
487 lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
488 o, (MSize)(L->top - o)); /* Initialize cdata. */
489 if (ctype_isstruct(ct->info)) {
490 /* Handle ctype __gc metamethod. Use the fast lookup here. */
491 cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
492 if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
493 GCtab *t = cts->finalizer;
494 if (gcref(t->metatable)) {
495 /* Add to finalizer table, if still enabled. */
496 copyTV(L, lj_tab_set(L, t, o-1), tv);
497 lj_gc_anybarriert(L, t);
498 cd->marked |= LJ_GC_CDATA_FIN;
502 L->top = o; /* Only return the cdata itself. */
503 lj_gc_check(L);
504 return 1;
507 LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
509 CTState *cts = ctype_cts(L);
510 CTypeID id = ffi_checkctype(L, cts, NULL);
511 CType *d = ctype_raw(cts, id);
512 TValue *o = lj_lib_checkany(L, 2);
513 L->top = o+1; /* Make sure this is the last item on the stack. */
514 if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
515 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
516 if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) {
517 GCcdata *cd = lj_cdata_new(cts, id, d->size);
518 lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
519 setcdataV(L, o, cd);
520 lj_gc_check(L);
522 return 1;
525 LJLIB_CF(ffi_typeof) LJLIB_REC(.)
527 CTState *cts = ctype_cts(L);
528 CTypeID id = ffi_checkctype(L, cts, L->base+1);
529 GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
530 *(CTypeID *)cdataptr(cd) = id;
531 setcdataV(L, L->top-1, cd);
532 lj_gc_check(L);
533 return 1;
536 LJLIB_CF(ffi_istype) LJLIB_REC(.)
538 CTState *cts = ctype_cts(L);
539 CTypeID id1 = ffi_checkctype(L, cts, NULL);
540 TValue *o = lj_lib_checkany(L, 2);
541 int b = 0;
542 if (tviscdata(o)) {
543 GCcdata *cd = cdataV(o);
544 CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
545 cd->ctypeid;
546 CType *ct1 = lj_ctype_rawref(cts, id1);
547 CType *ct2 = lj_ctype_rawref(cts, id2);
548 if (ct1 == ct2) {
549 b = 1;
550 } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
551 ct1->size == ct2->size) {
552 if (ctype_ispointer(ct1->info))
553 b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
554 else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
555 b = (((ct1->info ^ ct2->info) & ~CTF_QUAL) == 0);
556 } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
557 ct1 == ctype_rawchild(cts, ct2)) {
558 b = 1;
561 setboolV(L->top-1, b);
562 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
563 return 1;
566 LJLIB_CF(ffi_sizeof)
568 CTState *cts = ctype_cts(L);
569 CTypeID id = ffi_checkctype(L, cts, NULL);
570 CTSize sz;
571 if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
572 sz = cdatavlen(cdataV(L->base));
573 } else {
574 CType *ct = lj_ctype_rawref(cts, id);
575 if (ctype_isvltype(ct->info))
576 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
577 else
578 sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
579 if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
580 setnilV(L->top-1);
581 return 1;
584 setintV(L->top-1, (int32_t)sz);
585 return 1;
588 LJLIB_CF(ffi_alignof)
590 CTState *cts = ctype_cts(L);
591 CTypeID id = ffi_checkctype(L, cts, NULL);
592 CTSize sz = 0;
593 CTInfo info = lj_ctype_info(cts, id, &sz);
594 setintV(L->top-1, 1 << ctype_align(info));
595 return 1;
598 LJLIB_CF(ffi_offsetof)
600 CTState *cts = ctype_cts(L);
601 CTypeID id = ffi_checkctype(L, cts, NULL);
602 GCstr *name = lj_lib_checkstr(L, 2);
603 CType *ct = lj_ctype_rawref(cts, id);
604 CTSize ofs;
605 if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
606 CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
607 if (fct) {
608 setintV(L->top-1, ofs);
609 if (ctype_isfield(fct->info)) {
610 return 1;
611 } else if (ctype_isbitfield(fct->info)) {
612 setintV(L->top++, ctype_bitpos(fct->info));
613 setintV(L->top++, ctype_bitbsz(fct->info));
614 return 3;
618 return 0;
621 LJLIB_CF(ffi_errno) LJLIB_REC(.)
623 int err = errno;
624 if (L->top > L->base)
625 errno = ffi_checkint(L, 1);
626 setintV(L->top++, err);
627 return 1;
630 LJLIB_CF(ffi_string) LJLIB_REC(.)
632 CTState *cts = ctype_cts(L);
633 TValue *o = lj_lib_checkany(L, 1);
634 const char *p;
635 size_t len;
636 if (o+1 < L->top) {
637 len = (size_t)ffi_checkint(L, 2);
638 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
639 CCF_ARG(1));
640 } else {
641 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
642 CCF_ARG(1));
643 len = strlen(p);
645 L->top = o+1; /* Make sure this is the last item on the stack. */
646 setstrV(L, o, lj_str_new(L, p, len));
647 lj_gc_check(L);
648 return 1;
651 LJLIB_CF(ffi_copy) LJLIB_REC(.)
653 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
654 void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
655 TValue *o = L->base+1;
656 CTSize len;
657 if (tvisstr(o) && o+1 >= L->top)
658 len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */
659 else
660 len = (CTSize)ffi_checkint(L, 3);
661 memcpy(dp, sp, len);
662 return 0;
665 LJLIB_CF(ffi_fill) LJLIB_REC(.)
667 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
668 CTSize len = (CTSize)ffi_checkint(L, 2);
669 int32_t fill = 0;
670 if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
671 memset(dp, fill, len);
672 return 0;
675 #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be)
677 /* Test ABI string. */
678 LJLIB_CF(ffi_abi) LJLIB_REC(.)
680 GCstr *s = lj_lib_checkstr(L, 1);
681 int b = 0;
682 switch (s->hash) {
683 #if LJ_64
684 case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */
685 #else
686 case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */
687 #endif
688 #if LJ_ARCH_HASFPU
689 case H_(e33ee463,e33ee463): b = 1; break; /* fpu */
690 #endif
691 #if LJ_ABI_SOFTFP
692 case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */
693 #else
694 case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */
695 #endif
696 #if LJ_ABI_EABI
697 case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */
698 #endif
699 #if LJ_ABI_WIN
700 case H_(4ab624a8,4ab624a8): b = 1; break; /* win */
701 #endif
702 case H_(3af93066,1f001464): b = 1; break; /* le/be */
703 default:
704 break;
706 setboolV(L->top-1, b);
707 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
708 return 1;
711 #undef H_
713 LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */
715 LJLIB_CF(ffi_metatype)
717 CTState *cts = ctype_cts(L);
718 CTypeID id = ffi_checkctype(L, cts, NULL);
719 GCtab *mt = lj_lib_checktab(L, 2);
720 GCtab *t = cts->miscmap;
721 CType *ct = ctype_get(cts, id); /* Only allow raw types. */
722 TValue *tv;
723 GCcdata *cd;
724 if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
725 ctype_isvector(ct->info)))
726 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
727 tv = lj_tab_setinth(L, t, -(int32_t)id);
728 if (!tvisnil(tv))
729 lj_err_caller(L, LJ_ERR_PROTMT);
730 settabV(L, tv, mt);
731 lj_gc_anybarriert(L, t);
732 cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
733 *(CTypeID *)cdataptr(cd) = id;
734 setcdataV(L, L->top-1, cd);
735 lj_gc_check(L);
736 return 1;
739 LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
741 LJLIB_CF(ffi_gc)
743 GCcdata *cd = ffi_checkcdata(L, 1);
744 TValue *fin = lj_lib_checkany(L, 2);
745 CTState *cts = ctype_cts(L);
746 GCtab *t = cts->finalizer;
747 CType *ct = ctype_raw(cts, cd->ctypeid);
748 if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
749 ctype_isrefarray(ct->info)))
750 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
751 if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */
752 copyTV(L, lj_tab_set(L, t, L->base), fin);
753 lj_gc_anybarriert(L, t);
754 if (!tvisnil(fin))
755 cd->marked |= LJ_GC_CDATA_FIN;
756 else
757 cd->marked &= ~LJ_GC_CDATA_FIN;
759 L->top = L->base+1; /* Pass through the cdata object. */
760 return 1;
763 LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
765 LJLIB_CF(ffi_load)
767 GCstr *name = lj_lib_checkstr(L, 1);
768 int global = (L->base+1 < L->top && tvistruecond(L->base+1));
769 lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
770 return 1;
773 LJLIB_PUSH(top-4) LJLIB_SET(C)
774 LJLIB_PUSH(top-3) LJLIB_SET(os)
775 LJLIB_PUSH(top-2) LJLIB_SET(arch)
777 #include "lj_libdef.h"
779 /* ------------------------------------------------------------------------ */
781 /* Create special weak-keyed finalizer table. */
782 static GCtab *ffi_finalizer(lua_State *L)
784 /* NOBARRIER: The table is new (marked white). */
785 GCtab *t = lj_tab_new(L, 0, 1);
786 settabV(L, L->top++, t);
787 setgcref(t->metatable, obj2gco(t));
788 setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
789 lj_str_newlit(L, "K"));
790 t->nomm = (uint8_t)(~(1u<<MM_mode));
791 return t;
794 /* Register FFI module as loaded. */
795 static void ffi_register_module(lua_State *L)
797 cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
798 if (tmp && tvistab(tmp)) {
799 GCtab *t = tabV(tmp);
800 copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
801 lj_gc_anybarriert(L, t);
805 LUALIB_API int luaopen_ffi(lua_State *L)
807 CTState *cts = lj_ctype_init(L);
808 settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
809 cts->finalizer = ffi_finalizer(L);
810 LJ_LIB_REG(L, NULL, ffi_meta);
811 /* NOBARRIER: basemt is a GC root. */
812 setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
813 LJ_LIB_REG(L, NULL, ffi_clib);
814 LJ_LIB_REG(L, NULL, ffi_callback);
815 /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */
816 settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));
817 L->top--;
818 lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */
819 lua_pushliteral(L, LJ_OS_NAME);
820 lua_pushliteral(L, LJ_ARCH_NAME);
821 LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */
822 ffi_register_module(L);
823 return 1;
826 #endif