3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
25 #include "lj_cparse.h"
28 #include "lj_carith.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
)
42 lj_err_argtype(L
, 1, "C type");
44 if (tvisstr(o
)) { /* Parse an abstract C type declaration. */
50 cp
.srcname
= 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. */
58 if (!tviscdata(o
)) goto err_argtype
;
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
);
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;
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
));
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;
92 lj_err_arg(L
, narg
, LJ_ERR_NOVAL
);
93 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_INT32
), (uint8_t *)&i
, o
,
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
;
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));
115 lj_err_callerv(L
, LJ_ERR_FFI_BADIDX
, s
);
118 if (mm
== MM_index
) {
119 cTValue
*o
= lj_meta_tget(L
, tv
, base
+1);
121 if (tvisnil(o
)) goto err_index
;
122 copyTV(L
, L
->top
-1, o
);
126 TValue
*o
= lj_meta_tset(L
, tv
, base
+1);
128 copyTV(L
, o
, base
+2);
134 return lj_meta_tailcall(L
, tv
);
137 LJLIB_CF(ffi_meta___index
) LJLIB_REC(cdata_index
0)
139 CTState
*cts
= ctype_cts(L
);
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
);
148 return ffi_index_meta(L
, cts
, ct
, MM_index
);
149 if (lj_cdata_get(cts
, ct
, L
->top
-1, p
))
154 LJLIB_CF(ffi_meta___newindex
) LJLIB_REC(cdata_index
1)
156 CTState
*cts
= ctype_cts(L
);
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
);
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
);
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
)
186 LJLIB_CF(ffi_meta___len
) LJLIB_REC(cdata_arith MM_len
)
191 LJLIB_CF(ffi_meta___lt
) LJLIB_REC(cdata_arith MM_lt
)
196 LJLIB_CF(ffi_meta___le
) LJLIB_REC(cdata_arith MM_le
)
201 LJLIB_CF(ffi_meta___concat
) LJLIB_REC(cdata_arith MM_concat
)
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
);
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);
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);
230 LJLIB_CF(ffi_meta___add
) LJLIB_REC(cdata_arith MM_add
)
235 LJLIB_CF(ffi_meta___sub
) LJLIB_REC(cdata_arith MM_sub
)
240 LJLIB_CF(ffi_meta___mul
) LJLIB_REC(cdata_arith MM_mul
)
245 LJLIB_CF(ffi_meta___div
) LJLIB_REC(cdata_arith MM_div
)
250 LJLIB_CF(ffi_meta___mod
) LJLIB_REC(cdata_arith MM_mod
)
255 LJLIB_CF(ffi_meta___pow
) LJLIB_REC(cdata_arith MM_pow
)
260 LJLIB_CF(ffi_meta___unm
) LJLIB_REC(cdata_arith MM_unm
)
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
) {
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
));
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
)));
286 } else if (ctype_isfunc(ct
->info
)) {
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
);
297 return lj_meta_tailcall(L
, tv
);
301 lj_str_pushf(L
, msg
, strdata(lj_ctype_repr(L
, id
, NULL
)), p
);
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
)
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
);
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
))
345 copyTV(L
, L
->top
-1, tv
);
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
)) {
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);
370 lj_err_caller(L
, LJ_ERR_FFI_WRCONST
);
371 return 0; /* unreachable */
374 LJLIB_CF(ffi_clib___gc
)
377 if (o
< L
->top
&& tvisudata(o
) && udataV(o
)->udtype
== UDTYPE_FFI_CLIB
)
378 lj_clib_unload((CLibrary
*)uddata(udataV(o
)));
382 #include "lj_libdef.h"
384 /* -- FFI library functions ----------------------------------------------- */
386 #define LJLIB_MODULE_ffi
390 GCstr
*s
= lj_lib_checkstr(L
, 1);
394 cp
.cts
= ctype_cts(L
);
395 cp
.srcname
= 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. */
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
);
410 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
411 TValue
*o
= L
->base
+1;
413 if ((info
& CTF_VLA
)) {
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
);
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. */
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
);
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
);
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);
480 GCcdata
*cd
= cdataV(o
);
481 CTypeID id2
= cd
->typeid == CTID_CTYPEID
? *(CTypeID
*)cdataptr(cd
) :
483 CType
*ct1
= lj_ctype_rawref(cts
, id1
);
484 CType
*ct2
= lj_ctype_rawref(cts
, id2
);
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
)) {
498 setboolV(L
->top
-1, b
);
499 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
505 CTState
*cts
= ctype_cts(L
);
506 CTypeID id
= ffi_checkctype(L
, cts
);
508 if (LJ_UNLIKELY(tviscdata(L
->base
) && cdataisv(cdataV(L
->base
)))) {
509 sz
= cdatavlen(cdataV(L
->base
));
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));
515 sz
= ctype_hassize(ct
->info
) ? ct
->size
: CTSIZE_INVALID
;
516 if (LJ_UNLIKELY(sz
== CTSIZE_INVALID
)) {
521 setintV(L
->top
-1, (int32_t)sz
);
525 LJLIB_CF(ffi_alignof
)
527 CTState
*cts
= ctype_cts(L
);
528 CTypeID id
= ffi_checkctype(L
, cts
);
530 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
531 setintV(L
->top
-1, 1 << ctype_align(info
));
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
);
542 if (ctype_isstruct(ct
->info
) && ct
->size
!= CTSIZE_INVALID
) {
543 CType
*fct
= lj_ctype_getfield(cts
, ct
, name
, &ofs
);
545 setintV(L
->top
-1, ofs
);
546 if (ctype_isfield(fct
->info
)) {
548 } else if (ctype_isbitfield(fct
->info
)) {
549 setintV(L
->top
++, ctype_bitpos(fct
->info
));
550 setintV(L
->top
++, ctype_bitbsz(fct
->info
));
561 if (L
->top
> L
->base
)
562 errno
= ffi_checkint(L
, 1);
563 setintV(L
->top
++, err
);
567 LJLIB_CF(ffi_string
) LJLIB_REC(.)
569 CTState
*cts
= ctype_cts(L
);
570 TValue
*o
= lj_lib_checkany(L
, 1);
574 len
= (size_t)ffi_checkint(L
, 2);
575 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_P_CVOID
), (uint8_t *)&p
, o
,
578 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_P_CCHAR
), (uint8_t *)&p
, o
,
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
));
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;
594 if (tvisstr(o
) && o
+1 >= L
->top
)
595 len
= strV(o
)->len
+1; /* Copy Lua string including trailing '\0'. */
597 len
= (CTSize
)ffi_checkint(L
, 3);
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);
607 if (L
->base
+2 < L
->top
&& !tvisnil(L
->base
+2)) fill
= ffi_checkint(L
, 3);
608 memset(dp
, fill
, len
);
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);
621 case H_(849858eb
,ad35fd06
): b
= 1; break; /* 64bit */
623 case H_(662d3c79
,d0e22477
): b
= 1; break; /* 32bit */
626 case H_(e33ee463
,e33ee463
): b
= 1; break; /* fpu */
629 case H_(61211a23
,c2e8c81c
): b
= 1; break; /* softfp */
631 case H_(539417a8
,8ce0812f
): b
= 1; break; /* hardfp */
634 case H_(2182df8f
,f2ed1152
): b
= 1; break; /* eabi */
637 case H_(4ab624a8
,4ab624a8
): b
= 1; break; /* win */
639 case H_(3af93066
,1f001464
): b
= 1; break; /* le/be */
643 setboolV(L
->top
-1, b
);
644 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
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. */
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
);
666 lj_err_caller(L
, LJ_ERR_PROTMT
);
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
);
676 LJLIB_PUSH(top
-7) LJLIB_SET(!) /* Store reference to finalizer table. */
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
);
692 cd
->marked
|= LJ_GC_CDATA_FIN
;
694 cd
->marked
&= ~LJ_GC_CDATA_FIN
;
696 L
->top
= L
->base
+1; /* Pass through the cdata object. */
700 LJLIB_PUSH(top
-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
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
);
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
));
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
);