3 ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
25 #include "lj_cparse.h"
28 #include "lj_carith.h"
30 #include "lj_ccallback.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
)
43 lj_err_argtype(L
, 1, "C type");
45 if (tvisstr(o
)) { /* Parse an abstract C type declaration. */
51 cp
.srcname
= strdata(s
);
54 cp
.mode
= CPARSE_MODE_ABSTRACT
|CPARSE_MODE_NOIMPLICIT
;
55 errcode
= lj_cparse(&cp
);
56 if (errcode
) lj_err_throw(L
, errcode
); /* Propagate errors. */
60 if (!tviscdata(o
)) goto err_argtype
;
61 if (param
&& param
< L
->top
) lj_err_arg(L
, 1, LJ_ERR_FFI_NUMPARAM
);
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
);
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;
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
));
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;
95 lj_err_arg(L
, narg
, LJ_ERR_NOVAL
);
96 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_INT32
), (uint8_t *)&i
, o
,
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
;
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));
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
);
125 if (mm
== MM_index
) {
126 cTValue
*o
= lj_meta_tget(L
, tv
, base
+1);
128 if (tvisnil(o
)) goto err_index
;
129 copyTV(L
, L
->top
-1, o
);
133 TValue
*o
= lj_meta_tset(L
, tv
, base
+1);
135 copyTV(L
, o
, base
+2);
141 return lj_meta_tailcall(L
, tv
);
144 LJLIB_CF(ffi_meta___index
) LJLIB_REC(cdata_index
0)
146 CTState
*cts
= ctype_cts(L
);
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
);
155 return ffi_index_meta(L
, cts
, ct
, MM_index
);
156 if (lj_cdata_get(cts
, ct
, L
->top
-1, p
))
161 LJLIB_CF(ffi_meta___newindex
) LJLIB_REC(cdata_index
1)
163 CTState
*cts
= ctype_cts(L
);
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
);
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
);
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
)
193 LJLIB_CF(ffi_meta___len
) LJLIB_REC(cdata_arith MM_len
)
198 LJLIB_CF(ffi_meta___lt
) LJLIB_REC(cdata_arith MM_lt
)
203 LJLIB_CF(ffi_meta___le
) LJLIB_REC(cdata_arith MM_le
)
208 LJLIB_CF(ffi_meta___concat
) LJLIB_REC(cdata_arith MM_concat
)
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
;
224 if (cd
->ctypeid
== CTID_CTYPEID
) {
225 id
= *(CTypeID
*)cdataptr(cd
);
228 int ret
= lj_ccall_func(L
, cd
);
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
);
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
)
248 LJLIB_CF(ffi_meta___sub
) LJLIB_REC(cdata_arith MM_sub
)
253 LJLIB_CF(ffi_meta___mul
) LJLIB_REC(cdata_arith MM_mul
)
258 LJLIB_CF(ffi_meta___div
) LJLIB_REC(cdata_arith MM_div
)
263 LJLIB_CF(ffi_meta___mod
) LJLIB_REC(cdata_arith MM_mod
)
268 LJLIB_CF(ffi_meta___pow
) LJLIB_REC(cdata_arith MM_pow
)
273 LJLIB_CF(ffi_meta___unm
) LJLIB_REC(cdata_arith MM_unm
)
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
) {
289 CTState
*cts
= ctype_cts(L
);
290 CType
*ct
= ctype_raw(cts
, id
);
291 if (ctype_isref(ct
->info
)) {
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
));
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
)));
302 } else if (ctype_isfunc(ct
->info
)) {
304 } else if (ctype_isenum(ct
->info
)) {
305 msg
= "cdata<%s>: %d";
306 p
= (void *)(uintptr_t)*(uint32_t **)p
;
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
);
316 return lj_meta_tailcall(L
, tv
);
320 lj_str_pushf(L
, msg
, strdata(lj_ctype_repr(L
, id
, NULL
)), p
);
326 static int ffi_pairs(lua_State
*L
, MMS mm
)
328 CTState
*cts
= ctype_cts(L
);
329 CTypeID id
= ffi_checkcdata(L
, 1)->ctypeid
;
330 CType
*ct
= ctype_raw(cts
, id
);
332 if (ctype_isptr(ct
->info
)) id
= ctype_cid(ct
->info
);
333 tv
= lj_ctype_meta(cts
, id
, mm
);
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
);
340 LJLIB_CF(ffi_meta___pairs
)
342 return ffi_pairs(L
, MM_pairs
);
345 LJLIB_CF(ffi_meta___ipairs
)
347 return ffi_pairs(L
, MM_ipairs
);
350 LJLIB_PUSH("ffi") LJLIB_SET(__metatable
)
352 #include "lj_libdef.h"
354 /* -- C library metamethods ----------------------------------------------- */
356 #define LJLIB_MODULE_ffi_clib
358 /* Index C library by a name. */
359 static TValue
*ffi_clib_index(lua_State
*L
)
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));
371 LJLIB_CF(ffi_clib___index
) LJLIB_REC(clib_index
1)
373 TValue
*tv
= ffi_clib_index(L
);
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
))
387 copyTV(L
, L
->top
-1, tv
);
391 LJLIB_CF(ffi_clib___newindex
) LJLIB_REC(clib_index
0)
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
)) {
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
;
406 if (!((d
->info
|qual
) & CTF_CONST
)) {
407 lj_cconv_ct_tv(cts
, d
, *(void **)cdataptr(cd
), o
, 0);
412 lj_err_caller(L
, LJ_ERR_FFI_WRCONST
);
413 return 0; /* unreachable */
416 LJLIB_CF(ffi_clib___gc
)
419 if (o
< L
->top
&& tvisudata(o
) && udataV(o
)->udtype
== UDTYPE_FFI_CLIB
)
420 lj_clib_unload((CLibrary
*)uddata(udataV(o
)));
424 #include "lj_libdef.h"
426 /* -- Callback function metamethods --------------------------------------- */
428 #define LJLIB_MODULE_ffi_callback
430 static int ffi_callback_set(lua_State
*L
, GCfunc
*fn
)
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
);
442 lj_gc_anybarriert(L
, t
);
445 cts
->cb
.cbid
[slot
] = 0;
446 cts
->cb
.topid
= slot
< cts
->cb
.topid
? slot
: cts
->cb
.topid
;
451 lj_err_caller(L
, LJ_ERR_FFI_BADCBACK
);
455 LJLIB_CF(ffi_callback_free
)
457 return ffi_callback_set(L
, NULL
);
460 LJLIB_CF(ffi_callback_set
)
462 GCfunc
*fn
= lj_lib_checkfunc(L
, 2);
463 return ffi_callback_set(L
, fn
);
466 LJLIB_PUSH(top
-1) LJLIB_SET(__index
)
468 #include "lj_libdef.h"
470 /* -- FFI library functions ----------------------------------------------- */
472 #define LJLIB_MODULE_ffi
476 GCstr
*s
= lj_lib_checkstr(L
, 1);
480 cp
.cts
= ctype_cts(L
);
481 cp
.srcname
= 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. */
491 LJLIB_CF(ffi_new
) LJLIB_REC(.)
493 CTState
*cts
= ctype_cts(L
);
494 CTypeID id
= ffi_checkctype(L
, cts
, NULL
);
495 CType
*ct
= ctype_raw(cts
, id
);
497 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
498 TValue
*o
= L
->base
+1;
500 if ((info
& CTF_VLA
)) {
502 sz
= lj_ctype_vlsize(cts
, ct
, (CTSize
)ffi_checkint(L
, 2));
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
);
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
;
526 L
->top
= o
; /* Only return the cdata itself. */
531 LJLIB_CF(ffi_cast
) LJLIB_REC(ffi_new
)
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
);
549 LJLIB_CF(ffi_typeof
) LJLIB_REC(.)
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
);
560 LJLIB_CF(ffi_istype
) LJLIB_REC(.)
562 CTState
*cts
= ctype_cts(L
);
563 CTypeID id1
= ffi_checkctype(L
, cts
, NULL
);
564 TValue
*o
= lj_lib_checkany(L
, 2);
567 GCcdata
*cd
= cdataV(o
);
568 CTypeID id2
= cd
->ctypeid
== CTID_CTYPEID
? *(CTypeID
*)cdataptr(cd
) :
570 CType
*ct1
= lj_ctype_rawref(cts
, id1
);
571 CType
*ct2
= lj_ctype_rawref(cts
, id2
);
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
)) {
585 setboolV(L
->top
-1, b
);
586 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
590 LJLIB_CF(ffi_sizeof
) LJLIB_REC(ffi_xof FF_ffi_sizeof
)
592 CTState
*cts
= ctype_cts(L
);
593 CTypeID id
= ffi_checkctype(L
, cts
, NULL
);
595 if (LJ_UNLIKELY(tviscdata(L
->base
) && cdataisv(cdataV(L
->base
)))) {
596 sz
= cdatavlen(cdataV(L
->base
));
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));
602 sz
= ctype_hassize(ct
->info
) ? ct
->size
: CTSIZE_INVALID
;
603 if (LJ_UNLIKELY(sz
== CTSIZE_INVALID
)) {
608 setintV(L
->top
-1, (int32_t)sz
);
612 LJLIB_CF(ffi_alignof
) LJLIB_REC(ffi_xof FF_ffi_alignof
)
614 CTState
*cts
= ctype_cts(L
);
615 CTypeID id
= ffi_checkctype(L
, cts
, NULL
);
617 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
618 setintV(L
->top
-1, 1 << ctype_align(info
));
622 LJLIB_CF(ffi_offsetof
) LJLIB_REC(ffi_xof FF_ffi_offsetof
)
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
);
629 if (ctype_isstruct(ct
->info
) && ct
->size
!= CTSIZE_INVALID
) {
630 CType
*fct
= lj_ctype_getfield(cts
, ct
, name
, &ofs
);
632 setintV(L
->top
-1, ofs
);
633 if (ctype_isfield(fct
->info
)) {
635 } else if (ctype_isbitfield(fct
->info
)) {
636 setintV(L
->top
++, ctype_bitpos(fct
->info
));
637 setintV(L
->top
++, ctype_bitbsz(fct
->info
));
645 LJLIB_CF(ffi_errno
) LJLIB_REC(.)
648 if (L
->top
> L
->base
)
649 errno
= ffi_checkint(L
, 1);
650 setintV(L
->top
++, err
);
654 LJLIB_CF(ffi_string
) LJLIB_REC(.)
656 CTState
*cts
= ctype_cts(L
);
657 TValue
*o
= lj_lib_checkany(L
, 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
,
665 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_P_CCHAR
), (uint8_t *)&p
, o
,
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
));
675 LJLIB_CF(ffi_copy
) LJLIB_REC(.)
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;
681 if (tvisstr(o
) && o
+1 >= L
->top
)
682 len
= strV(o
)->len
+1; /* Copy Lua string including trailing '\0'. */
684 len
= (CTSize
)ffi_checkint(L
, 3);
689 LJLIB_CF(ffi_fill
) LJLIB_REC(.)
691 void *dp
= ffi_checkptr(L
, 1, CTID_P_VOID
);
692 CTSize len
= (CTSize
)ffi_checkint(L
, 2);
694 if (L
->base
+2 < L
->top
&& !tvisnil(L
->base
+2)) fill
= ffi_checkint(L
, 3);
695 memset(dp
, fill
, len
);
699 #define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be)
701 /* Test ABI string. */
702 LJLIB_CF(ffi_abi
) LJLIB_REC(.)
704 GCstr
*s
= lj_lib_checkstr(L
, 1);
708 case H_(849858eb
,ad35fd06
): b
= 1; break; /* 64bit */
710 case H_(662d3c79
,d0e22477
): b
= 1; break; /* 32bit */
713 case H_(e33ee463
,e33ee463
): b
= 1; break; /* fpu */
716 case H_(61211a23
,c2e8c81c
): b
= 1; break; /* softfp */
718 case H_(539417a8
,8ce0812f
): b
= 1; break; /* hardfp */
721 case H_(2182df8f
,f2ed1152
): b
= 1; break; /* eabi */
724 case H_(4ab624a8
,4ab624a8
): b
= 1; break; /* win */
726 case H_(3af93066
,1f001464
): b
= 1; break; /* le/be */
730 setboolV(L
->top
-1, b
);
731 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
737 LJLIB_PUSH(top
-8) LJLIB_SET(!) /* Store reference to miscmap table. */
739 LJLIB_CF(ffi_metatype
)
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. */
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
);
753 lj_err_caller(L
, LJ_ERR_PROTMT
);
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
);
763 LJLIB_PUSH(top
-7) LJLIB_SET(!) /* Store reference to finalizer table. */
765 LJLIB_CF(ffi_gc
) LJLIB_REC(.)
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
);
779 cd
->marked
|= LJ_GC_CDATA_FIN
;
781 cd
->marked
&= ~LJ_GC_CDATA_FIN
;
783 L
->top
= L
->base
+1; /* Pass through the cdata object. */
787 LJLIB_PUSH(top
-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
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
);
797 LJLIB_PUSH(top
-4) LJLIB_SET(C
)
798 LJLIB_PUSH(top
-3) LJLIB_SET(os
)
799 LJLIB_PUSH(top
-2) LJLIB_SET(arch
)
801 #include "lj_libdef.h"
803 /* ------------------------------------------------------------------------ */
805 /* Create special weak-keyed finalizer table. */
806 static GCtab
*ffi_finalizer(lua_State
*L
)
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
));
818 /* Register FFI module as loaded. */
819 static void ffi_register_module(lua_State
*L
)
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
);
829 LUALIB_API
int luaopen_ffi(lua_State
*L
)
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));
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
);