3 ** Copyright (C) 2005-2012 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 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
)
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
);
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
))
363 copyTV(L
, L
->top
-1, tv
);
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
)) {
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);
388 lj_err_caller(L
, LJ_ERR_FFI_WRCONST
);
389 return 0; /* unreachable */
392 LJLIB_CF(ffi_clib___gc
)
395 if (o
< L
->top
&& tvisudata(o
) && udataV(o
)->udtype
== UDTYPE_FFI_CLIB
)
396 lj_clib_unload((CLibrary
*)uddata(udataV(o
)));
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
);
418 lj_gc_anybarriert(L
, t
);
421 cts
->cb
.cbid
[slot
] = 0;
422 cts
->cb
.topid
= slot
< cts
->cb
.topid
? slot
: cts
->cb
.topid
;
427 lj_err_caller(L
, LJ_ERR_FFI_BADCBACK
);
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
452 GCstr
*s
= lj_lib_checkstr(L
, 1);
456 cp
.cts
= ctype_cts(L
);
457 cp
.srcname
= 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. */
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
);
473 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
474 TValue
*o
= L
->base
+1;
476 if ((info
& CTF_VLA
)) {
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
);
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. */
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
);
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
);
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);
543 GCcdata
*cd
= cdataV(o
);
544 CTypeID id2
= cd
->ctypeid
== CTID_CTYPEID
? *(CTypeID
*)cdataptr(cd
) :
546 CType
*ct1
= lj_ctype_rawref(cts
, id1
);
547 CType
*ct2
= lj_ctype_rawref(cts
, id2
);
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
)) {
561 setboolV(L
->top
-1, b
);
562 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
568 CTState
*cts
= ctype_cts(L
);
569 CTypeID id
= ffi_checkctype(L
, cts
, NULL
);
571 if (LJ_UNLIKELY(tviscdata(L
->base
) && cdataisv(cdataV(L
->base
)))) {
572 sz
= cdatavlen(cdataV(L
->base
));
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));
578 sz
= ctype_hassize(ct
->info
) ? ct
->size
: CTSIZE_INVALID
;
579 if (LJ_UNLIKELY(sz
== CTSIZE_INVALID
)) {
584 setintV(L
->top
-1, (int32_t)sz
);
588 LJLIB_CF(ffi_alignof
)
590 CTState
*cts
= ctype_cts(L
);
591 CTypeID id
= ffi_checkctype(L
, cts
, NULL
);
593 CTInfo info
= lj_ctype_info(cts
, id
, &sz
);
594 setintV(L
->top
-1, 1 << ctype_align(info
));
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
);
605 if (ctype_isstruct(ct
->info
) && ct
->size
!= CTSIZE_INVALID
) {
606 CType
*fct
= lj_ctype_getfield(cts
, ct
, name
, &ofs
);
608 setintV(L
->top
-1, ofs
);
609 if (ctype_isfield(fct
->info
)) {
611 } else if (ctype_isbitfield(fct
->info
)) {
612 setintV(L
->top
++, ctype_bitpos(fct
->info
));
613 setintV(L
->top
++, ctype_bitbsz(fct
->info
));
621 LJLIB_CF(ffi_errno
) LJLIB_REC(.)
624 if (L
->top
> L
->base
)
625 errno
= ffi_checkint(L
, 1);
626 setintV(L
->top
++, err
);
630 LJLIB_CF(ffi_string
) LJLIB_REC(.)
632 CTState
*cts
= ctype_cts(L
);
633 TValue
*o
= lj_lib_checkany(L
, 1);
637 len
= (size_t)ffi_checkint(L
, 2);
638 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_P_CVOID
), (uint8_t *)&p
, o
,
641 lj_cconv_ct_tv(cts
, ctype_get(cts
, CTID_P_CCHAR
), (uint8_t *)&p
, o
,
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
));
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;
657 if (tvisstr(o
) && o
+1 >= L
->top
)
658 len
= strV(o
)->len
+1; /* Copy Lua string including trailing '\0'. */
660 len
= (CTSize
)ffi_checkint(L
, 3);
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);
670 if (L
->base
+2 < L
->top
&& !tvisnil(L
->base
+2)) fill
= ffi_checkint(L
, 3);
671 memset(dp
, fill
, len
);
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);
684 case H_(849858eb
,ad35fd06
): b
= 1; break; /* 64bit */
686 case H_(662d3c79
,d0e22477
): b
= 1; break; /* 32bit */
689 case H_(e33ee463
,e33ee463
): b
= 1; break; /* fpu */
692 case H_(61211a23
,c2e8c81c
): b
= 1; break; /* softfp */
694 case H_(539417a8
,8ce0812f
): b
= 1; break; /* hardfp */
697 case H_(2182df8f
,f2ed1152
): b
= 1; break; /* eabi */
700 case H_(4ab624a8
,4ab624a8
): b
= 1; break; /* win */
702 case H_(3af93066
,1f001464
): b
= 1; break; /* le/be */
706 setboolV(L
->top
-1, b
);
707 setboolV(&G(L
)->tmptv2
, b
); /* Remember for trace recorder. */
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. */
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
);
729 lj_err_caller(L
, LJ_ERR_PROTMT
);
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
);
739 LJLIB_PUSH(top
-7) LJLIB_SET(!) /* Store reference to finalizer table. */
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
);
755 cd
->marked
|= LJ_GC_CDATA_FIN
;
757 cd
->marked
&= ~LJ_GC_CDATA_FIN
;
759 L
->top
= L
->base
+1; /* Pass through the cdata object. */
763 LJLIB_PUSH(top
-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
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
);
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
));
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));
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
);