aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luajit-2.0/src/lib_ffi.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2014-01-13 21:08:31 +1000
committerDavid Walter Seikel2014-01-13 21:08:31 +1000
commit637177eb1397ef1800027bccd50dbdc1af29a15b (patch)
tree3670a48303d05fceb8bf3ec4ee2901b72fe62d4d /libraries/luajit-2.0/src/lib_ffi.c
parentUpdate Irrlicht to 1.8.1. Include actual change markers this time. lol (diff)
downloadSledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.zip
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.gz
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.bz2
SledjHamr-637177eb1397ef1800027bccd50dbdc1af29a15b.tar.xz
Remove LuaJIT source, we can use packaged LuaJIT 2.0 release now.
Also some cleanups related to the other library removals.
Diffstat (limited to '')
-rw-r--r--libraries/luajit-2.0/src/lib_ffi.c811
1 files changed, 0 insertions, 811 deletions
diff --git a/libraries/luajit-2.0/src/lib_ffi.c b/libraries/luajit-2.0/src/lib_ffi.c
deleted file mode 100644
index 0a8a728..0000000
--- a/libraries/luajit-2.0/src/lib_ffi.c
+++ /dev/null
@@ -1,811 +0,0 @@
1/*
2** FFI library.
3** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#define lib_ffi_c
7#define LUA_LIB
8
9#include <errno.h>
10
11#include "lua.h"
12#include "lauxlib.h"
13#include "lualib.h"
14
15#include "lj_obj.h"
16
17#if LJ_HASFFI
18
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"
34
35/* -- C type checks ------------------------------------------------------- */
36
37/* Check first argument for a C type and returns its ID. */
38static CTypeID ffi_checkctype(lua_State *L, CTState *cts)
39{
40 TValue *o = L->base;
41 if (!(o < L->top)) {
42 err_argtype:
43 lj_err_argtype(L, 1, "C type");
44 }
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.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
54 errcode = lj_cparse(&cp);
55 if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
56 return cp.val.id;
57 } else {
58 GCcdata *cd;
59 if (!tviscdata(o)) goto err_argtype;
60 cd = cdataV(o);
61 return cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->typeid;
62 }
63}
64
65/* Check argument for C data and return it. */
66static GCcdata *ffi_checkcdata(lua_State *L, int narg)
67{
68 TValue *o = L->base + narg-1;
69 if (!(o < L->top && tviscdata(o)))
70 lj_err_argt(L, narg, LUA_TCDATA);
71 return cdataV(o);
72}
73
74/* Convert argument to C pointer. */
75static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
76{
77 CTState *cts = ctype_cts(L);
78 TValue *o = L->base + narg-1;
79 void *p;
80 if (o >= L->top)
81 lj_err_arg(L, narg, LJ_ERR_NOVAL);
82 lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg));
83 return p;
84}
85
86/* Convert argument to int32_t. */
87static int32_t ffi_checkint(lua_State *L, int narg)
88{
89 CTState *cts = ctype_cts(L);
90 TValue *o = L->base + narg-1;
91 int32_t i;
92 if (o >= L->top)
93 lj_err_arg(L, narg, LJ_ERR_NOVAL);
94 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o,
95 CCF_ARG(narg));
96 return i;
97}
98
99/* -- C type metamethods -------------------------------------------------- */
100
101#define LJLIB_MODULE_ffi_meta
102
103/* Handle ctype __index/__newindex metamethods. */
104static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm)
105{
106 CTypeID id = ctype_typeid(cts, ct);
107 cTValue *tv = lj_ctype_meta(cts, id, mm);
108 TValue *base = L->base;
109 if (!tv) {
110 const char *s;
111 err_index:
112 s = strdata(lj_ctype_repr(L, id, NULL));
113 if (tvisstr(L->base+1))
114 lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1));
115 else
116 lj_err_callerv(L, LJ_ERR_FFI_BADIDX, s);
117 }
118 if (!tvisfunc(tv)) {
119 if (mm == MM_index) {
120 cTValue *o = lj_meta_tget(L, tv, base+1);
121 if (o) {
122 if (tvisnil(o)) goto err_index;
123 copyTV(L, L->top-1, o);
124 return 1;
125 }
126 } else {
127 TValue *o = lj_meta_tset(L, tv, base+1);
128 if (o) {
129 copyTV(L, o, base+2);
130 return 0;
131 }
132 }
133 tv = L->top-1;
134 }
135 return lj_meta_tailcall(L, tv);
136}
137
138LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
139{
140 CTState *cts = ctype_cts(L);
141 CTInfo qual = 0;
142 CType *ct;
143 uint8_t *p;
144 TValue *o = L->base;
145 if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */
146 lj_err_argt(L, 1, LUA_TCDATA);
147 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
148 if ((qual & 1))
149 return ffi_index_meta(L, cts, ct, MM_index);
150 if (lj_cdata_get(cts, ct, L->top-1, p))
151 lj_gc_check(L);
152 return 1;
153}
154
155LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
156{
157 CTState *cts = ctype_cts(L);
158 CTInfo qual = 0;
159 CType *ct;
160 uint8_t *p;
161 TValue *o = L->base;
162 if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */
163 lj_err_argt(L, 1, LUA_TCDATA);
164 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
165 if ((qual & 1)) {
166 if ((qual & CTF_CONST))
167 lj_err_caller(L, LJ_ERR_FFI_WRCONST);
168 return ffi_index_meta(L, cts, ct, MM_newindex);
169 }
170 lj_cdata_set(cts, ct, p, o+2, qual);
171 return 0;
172}
173
174/* Common handler for cdata arithmetic. */
175static int ffi_arith(lua_State *L)
176{
177 MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq);
178 return lj_carith_op(L, mm);
179}
180
181/* The following functions must be in contiguous ORDER MM. */
182LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq)
183{
184 return ffi_arith(L);
185}
186
187LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len)
188{
189 return ffi_arith(L);
190}
191
192LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt)
193{
194 return ffi_arith(L);
195}
196
197LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le)
198{
199 return ffi_arith(L);
200}
201
202LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat)
203{
204 return ffi_arith(L);
205}
206
207/* Handle ctype __call metamethod. */
208static int ffi_call_meta(lua_State *L, CTypeID id)
209{
210 CTState *cts = ctype_cts(L);
211 CType *ct = ctype_raw(cts, id);
212 cTValue *tv;
213 if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
214 tv = lj_ctype_meta(cts, id, MM_call);
215 if (!tv)
216 lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL)));
217 return lj_meta_tailcall(L, tv);
218}
219
220/* Forward declaration. */
221static int lj_cf_ffi_new(lua_State *L);
222
223LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
224{
225 GCcdata *cd = ffi_checkcdata(L, 1);
226 int ret;
227 if (cd->typeid == CTID_CTYPEID)
228 return lj_cf_ffi_new(L);
229 if ((ret = lj_ccall_func(L, cd)) < 0)
230 return ffi_call_meta(L, cd->typeid);
231 return ret;
232}
233
234LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)
235{
236 return ffi_arith(L);
237}
238
239LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub)
240{
241 return ffi_arith(L);
242}
243
244LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul)
245{
246 return ffi_arith(L);
247}
248
249LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div)
250{
251 return ffi_arith(L);
252}
253
254LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod)
255{
256 return ffi_arith(L);
257}
258
259LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow)
260{
261 return ffi_arith(L);
262}
263
264LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm)
265{
266 return ffi_arith(L);
267}
268/* End of contiguous ORDER MM. */
269
270LJLIB_CF(ffi_meta___tostring)
271{
272 GCcdata *cd = ffi_checkcdata(L, 1);
273 const char *msg = "cdata<%s>: %p";
274 CTypeID id = cd->typeid;
275 void *p = cdataptr(cd);
276 if (id == CTID_CTYPEID) {
277 msg = "ctype<%s>";
278 id = *(CTypeID *)p;
279 } else {
280 CTState *cts = ctype_cts(L);
281 CType *ct = ctype_raw(cts, id);
282 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
283 if (ctype_iscomplex(ct->info)) {
284 setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size));
285 goto checkgc;
286 } else if (ct->size == 8 && ctype_isinteger(ct->info)) {
287 setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd),
288 (ct->info & CTF_UNSIGNED)));
289 goto checkgc;
290 } else if (ctype_isfunc(ct->info)) {
291 p = *(void **)p;
292 } else {
293 if (ctype_isptr(ct->info)) {
294 p = cdata_getptr(p, ct->size);
295 ct = ctype_rawchild(cts, ct);
296 }
297 if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) {
298 /* Handle ctype __tostring metamethod. */
299 cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring);
300 if (tv)
301 return lj_meta_tailcall(L, tv);
302 }
303 }
304 }
305 lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
306checkgc:
307 lj_gc_check(L);
308 return 1;
309}
310
311LJLIB_PUSH("ffi") LJLIB_SET(__metatable)
312
313#include "lj_libdef.h"
314
315/* -- C library metamethods ----------------------------------------------- */
316
317#define LJLIB_MODULE_ffi_clib
318
319/* Index C library by a name. */
320static TValue *ffi_clib_index(lua_State *L)
321{
322 TValue *o = L->base;
323 CLibrary *cl;
324 if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB))
325 lj_err_argt(L, 1, LUA_TUSERDATA);
326 cl = (CLibrary *)uddata(udataV(o));
327 if (!(o+1 < L->top && tvisstr(o+1)))
328 lj_err_argt(L, 2, LUA_TSTRING);
329 return lj_clib_index(L, cl, strV(o+1));
330}
331
332LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1)
333{
334 TValue *tv = ffi_clib_index(L);
335 if (tviscdata(tv)) {
336 CTState *cts = ctype_cts(L);
337 GCcdata *cd = cdataV(tv);
338 CType *s = ctype_get(cts, cd->typeid);
339 if (ctype_isextern(s->info)) {
340 CTypeID sid = ctype_cid(s->info);
341 void *sp = *(void **)cdataptr(cd);
342 CType *ct = ctype_raw(cts, sid);
343 if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
344 if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp))
345 lj_gc_check(L);
346 return 1;
347 }
348 }
349 copyTV(L, L->top-1, tv);
350 return 1;
351}
352
353LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0)
354{
355 TValue *tv = ffi_clib_index(L);
356 TValue *o = L->base+2;
357 if (o < L->top && tviscdata(tv)) {
358 CTState *cts = ctype_cts(L);
359 GCcdata *cd = cdataV(tv);
360 CType *d = ctype_get(cts, cd->typeid);
361 if (ctype_isextern(d->info)) {
362 CTInfo qual = 0;
363 for (;;) { /* Skip attributes and collect qualifiers. */
364 d = ctype_child(cts, d);
365 if (!ctype_isattrib(d->info)) break;
366 if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
367 }
368 if (!((d->info|qual) & CTF_CONST)) {
369 lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0);
370 return 0;
371 }
372 }
373 }
374 lj_err_caller(L, LJ_ERR_FFI_WRCONST);
375 return 0; /* unreachable */
376}
377
378LJLIB_CF(ffi_clib___gc)
379{
380 TValue *o = L->base;
381 if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)
382 lj_clib_unload((CLibrary *)uddata(udataV(o)));
383 return 0;
384}
385
386#include "lj_libdef.h"
387
388/* -- Callback function metamethods --------------------------------------- */
389
390#define LJLIB_MODULE_ffi_callback
391
392static int ffi_callback_set(lua_State *L, GCfunc *fn)
393{
394 GCcdata *cd = ffi_checkcdata(L, 1);
395 CTState *cts = ctype_cts(L);
396 CType *ct = ctype_raw(cts, cd->typeid);
397 if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) {
398 MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd));
399 if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) {
400 GCtab *t = cts->miscmap;
401 TValue *tv = lj_tab_setint(L, t, (int32_t)slot);
402 if (fn) {
403 setfuncV(L, tv, fn);
404 lj_gc_anybarriert(L, t);
405 } else {
406 setnilV(tv);
407 cts->cb.cbid[slot] = 0;
408 cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid;
409 }
410 return 0;
411 }
412 }
413 lj_err_caller(L, LJ_ERR_FFI_BADCBACK);
414 return 0;
415}
416
417LJLIB_CF(ffi_callback_free)
418{
419 return ffi_callback_set(L, NULL);
420}
421
422LJLIB_CF(ffi_callback_set)
423{
424 GCfunc *fn = lj_lib_checkfunc(L, 2);
425 return ffi_callback_set(L, fn);
426}
427
428LJLIB_PUSH(top-1) LJLIB_SET(__index)
429
430#include "lj_libdef.h"
431
432/* -- FFI library functions ----------------------------------------------- */
433
434#define LJLIB_MODULE_ffi
435
436LJLIB_CF(ffi_cdef)
437{
438 GCstr *s = lj_lib_checkstr(L, 1);
439 CPState cp;
440 int errcode;
441 cp.L = L;
442 cp.cts = ctype_cts(L);
443 cp.srcname = strdata(s);
444 cp.p = strdata(s);
445 cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT;
446 errcode = lj_cparse(&cp);
447 if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */
448 lj_gc_check(L);
449 return 0;
450}
451
452LJLIB_CF(ffi_new) LJLIB_REC(.)
453{
454 CTState *cts = ctype_cts(L);
455 CTypeID id = ffi_checkctype(L, cts);
456 CType *ct = ctype_raw(cts, id);
457 CTSize sz;
458 CTInfo info = lj_ctype_info(cts, id, &sz);
459 TValue *o = L->base+1;
460 GCcdata *cd;
461 if ((info & CTF_VLA)) {
462 o++;
463 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
464 }
465 if (sz == CTSIZE_INVALID)
466 lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE);
467 if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN)
468 cd = lj_cdata_new(cts, id, sz);
469 else
470 cd = lj_cdata_newv(cts, id, sz, ctype_align(info));
471 setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */
472 lj_cconv_ct_init(cts, ct, sz, cdataptr(cd),
473 o, (MSize)(L->top - o)); /* Initialize cdata. */
474 if (ctype_isstruct(ct->info)) {
475 /* Handle ctype __gc metamethod. Use the fast lookup here. */
476 cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id);
477 if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) {
478 GCtab *t = cts->finalizer;
479 if (gcref(t->metatable)) {
480 /* Add to finalizer table, if still enabled. */
481 copyTV(L, lj_tab_set(L, t, o-1), tv);
482 lj_gc_anybarriert(L, t);
483 cd->marked |= LJ_GC_CDATA_FIN;
484 }
485 }
486 }
487 L->top = o; /* Only return the cdata itself. */
488 lj_gc_check(L);
489 return 1;
490}
491
492LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new)
493{
494 CTState *cts = ctype_cts(L);
495 CTypeID id = ffi_checkctype(L, cts);
496 CType *d = ctype_raw(cts, id);
497 TValue *o = lj_lib_checkany(L, 2);
498 L->top = o+1; /* Make sure this is the last item on the stack. */
499 if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info)))
500 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
501 if (!(tviscdata(o) && cdataV(o)->typeid == id)) {
502 GCcdata *cd = lj_cdata_new(cts, id, d->size);
503 lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST);
504 setcdataV(L, o, cd);
505 lj_gc_check(L);
506 }
507 return 1;
508}
509
510LJLIB_CF(ffi_typeof)
511{
512 CTState *cts = ctype_cts(L);
513 CTypeID id = ffi_checkctype(L, cts);
514 GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
515 *(CTypeID *)cdataptr(cd) = id;
516 setcdataV(L, L->top-1, cd);
517 lj_gc_check(L);
518 return 1;
519}
520
521LJLIB_CF(ffi_istype) LJLIB_REC(ffi_istype)
522{
523 CTState *cts = ctype_cts(L);
524 CTypeID id1 = ffi_checkctype(L, cts);
525 TValue *o = lj_lib_checkany(L, 2);
526 int b = 0;
527 if (tviscdata(o)) {
528 GCcdata *cd = cdataV(o);
529 CTypeID id2 = cd->typeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) :
530 cd->typeid;
531 CType *ct1 = lj_ctype_rawref(cts, id1);
532 CType *ct2 = lj_ctype_rawref(cts, id2);
533 if (ct1 == ct2) {
534 b = 1;
535 } else if (ctype_type(ct1->info) == ctype_type(ct2->info) &&
536 ct1->size == ct2->size) {
537 if (ctype_ispointer(ct1->info))
538 b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL);
539 else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info))
540 b = (((ct1->info ^ ct2->info) & ~CTF_QUAL) == 0);
541 } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) &&
542 ct1 == ctype_rawchild(cts, ct2)) {
543 b = 1;
544 }
545 }
546 setboolV(L->top-1, b);
547 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
548 return 1;
549}
550
551LJLIB_CF(ffi_sizeof)
552{
553 CTState *cts = ctype_cts(L);
554 CTypeID id = ffi_checkctype(L, cts);
555 CTSize sz;
556 if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) {
557 sz = cdatavlen(cdataV(L->base));
558 } else {
559 CType *ct = lj_ctype_rawref(cts, id);
560 if (ctype_isvltype(ct->info))
561 sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2));
562 else
563 sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID;
564 if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) {
565 setnilV(L->top-1);
566 return 1;
567 }
568 }
569 setintV(L->top-1, (int32_t)sz);
570 return 1;
571}
572
573LJLIB_CF(ffi_alignof)
574{
575 CTState *cts = ctype_cts(L);
576 CTypeID id = ffi_checkctype(L, cts);
577 CTSize sz = 0;
578 CTInfo info = lj_ctype_info(cts, id, &sz);
579 setintV(L->top-1, 1 << ctype_align(info));
580 return 1;
581}
582
583LJLIB_CF(ffi_offsetof)
584{
585 CTState *cts = ctype_cts(L);
586 CTypeID id = ffi_checkctype(L, cts);
587 GCstr *name = lj_lib_checkstr(L, 2);
588 CType *ct = lj_ctype_rawref(cts, id);
589 CTSize ofs;
590 if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
591 CType *fct = lj_ctype_getfield(cts, ct, name, &ofs);
592 if (fct) {
593 setintV(L->top-1, ofs);
594 if (ctype_isfield(fct->info)) {
595 return 1;
596 } else if (ctype_isbitfield(fct->info)) {
597 setintV(L->top++, ctype_bitpos(fct->info));
598 setintV(L->top++, ctype_bitbsz(fct->info));
599 return 3;
600 }
601 }
602 }
603 return 0;
604}
605
606LJLIB_CF(ffi_errno) LJLIB_REC(.)
607{
608 int err = errno;
609 if (L->top > L->base)
610 errno = ffi_checkint(L, 1);
611 setintV(L->top++, err);
612 return 1;
613}
614
615LJLIB_CF(ffi_string) LJLIB_REC(.)
616{
617 CTState *cts = ctype_cts(L);
618 TValue *o = lj_lib_checkany(L, 1);
619 const char *p;
620 size_t len;
621 if (o+1 < L->top) {
622 len = (size_t)ffi_checkint(L, 2);
623 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o,
624 CCF_ARG(1));
625 } else {
626 lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o,
627 CCF_ARG(1));
628 len = strlen(p);
629 }
630 L->top = o+1; /* Make sure this is the last item on the stack. */
631 setstrV(L, o, lj_str_new(L, p, len));
632 lj_gc_check(L);
633 return 1;
634}
635
636LJLIB_CF(ffi_copy) LJLIB_REC(.)
637{
638 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
639 void *sp = ffi_checkptr(L, 2, CTID_P_CVOID);
640 TValue *o = L->base+1;
641 CTSize len;
642 if (tvisstr(o) && o+1 >= L->top)
643 len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */
644 else
645 len = (CTSize)ffi_checkint(L, 3);
646 memcpy(dp, sp, len);
647 return 0;
648}
649
650LJLIB_CF(ffi_fill) LJLIB_REC(.)
651{
652 void *dp = ffi_checkptr(L, 1, CTID_P_VOID);
653 CTSize len = (CTSize)ffi_checkint(L, 2);
654 int32_t fill = 0;
655 if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3);
656 memset(dp, fill, len);
657 return 0;
658}
659
660#define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be)
661
662/* Test ABI string. */
663LJLIB_CF(ffi_abi) LJLIB_REC(.)
664{
665 GCstr *s = lj_lib_checkstr(L, 1);
666 int b = 0;
667 switch (s->hash) {
668#if LJ_64
669 case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */
670#else
671 case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */
672#endif
673#if LJ_ARCH_HASFPU
674 case H_(e33ee463,e33ee463): b = 1; break; /* fpu */
675#endif
676#if LJ_ABI_SOFTFP
677 case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */
678#else
679 case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */
680#endif
681#if LJ_ABI_EABI
682 case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */
683#endif
684#if LJ_ABI_WIN
685 case H_(4ab624a8,4ab624a8): b = 1; break; /* win */
686#endif
687 case H_(3af93066,1f001464): b = 1; break; /* le/be */
688 default:
689 break;
690 }
691 setboolV(L->top-1, b);
692 setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */
693 return 1;
694}
695
696#undef H_
697
698LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */
699
700LJLIB_CF(ffi_metatype)
701{
702 CTState *cts = ctype_cts(L);
703 CTypeID id = ffi_checkctype(L, cts);
704 GCtab *mt = lj_lib_checktab(L, 2);
705 GCtab *t = cts->miscmap;
706 CType *ct = ctype_get(cts, id); /* Only allow raw types. */
707 TValue *tv;
708 GCcdata *cd;
709 if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) ||
710 ctype_isvector(ct->info)))
711 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
712 tv = lj_tab_setinth(L, t, -(int32_t)id);
713 if (!tvisnil(tv))
714 lj_err_caller(L, LJ_ERR_PROTMT);
715 settabV(L, tv, mt);
716 lj_gc_anybarriert(L, t);
717 cd = lj_cdata_new(cts, CTID_CTYPEID, 4);
718 *(CTypeID *)cdataptr(cd) = id;
719 setcdataV(L, L->top-1, cd);
720 lj_gc_check(L);
721 return 1;
722}
723
724LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */
725
726LJLIB_CF(ffi_gc)
727{
728 GCcdata *cd = ffi_checkcdata(L, 1);
729 TValue *fin = lj_lib_checkany(L, 2);
730 CTState *cts = ctype_cts(L);
731 GCtab *t = cts->finalizer;
732 CType *ct = ctype_raw(cts, cd->typeid);
733 if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) ||
734 ctype_isrefarray(ct->info)))
735 lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE);
736 if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */
737 copyTV(L, lj_tab_set(L, t, L->base), fin);
738 lj_gc_anybarriert(L, t);
739 if (!tvisnil(fin))
740 cd->marked |= LJ_GC_CDATA_FIN;
741 else
742 cd->marked &= ~LJ_GC_CDATA_FIN;
743 }
744 L->top = L->base+1; /* Pass through the cdata object. */
745 return 1;
746}
747
748LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */
749
750LJLIB_CF(ffi_load)
751{
752 GCstr *name = lj_lib_checkstr(L, 1);
753 int global = (L->base+1 < L->top && tvistruecond(L->base+1));
754 lj_clib_load(L, tabref(curr_func(L)->c.env), name, global);
755 return 1;
756}
757
758LJLIB_PUSH(top-4) LJLIB_SET(C)
759LJLIB_PUSH(top-3) LJLIB_SET(os)
760LJLIB_PUSH(top-2) LJLIB_SET(arch)
761
762#include "lj_libdef.h"
763
764/* ------------------------------------------------------------------------ */
765
766/* Create special weak-keyed finalizer table. */
767static GCtab *ffi_finalizer(lua_State *L)
768{
769 /* NOBARRIER: The table is new (marked white). */
770 GCtab *t = lj_tab_new(L, 0, 1);
771 settabV(L, L->top++, t);
772 setgcref(t->metatable, obj2gco(t));
773 setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")),
774 lj_str_newlit(L, "K"));
775 t->nomm = (uint8_t)(~(1u<<MM_mode));
776 return t;
777}
778
779/* Register FFI module as loaded. */
780static void ffi_register_module(lua_State *L)
781{
782 cTValue *tmp = lj_tab_getstr(tabV(registry(L)), lj_str_newlit(L, "_LOADED"));
783 if (tmp && tvistab(tmp)) {
784 GCtab *t = tabV(tmp);
785 copyTV(L, lj_tab_setstr(L, t, lj_str_newlit(L, LUA_FFILIBNAME)), L->top-1);
786 lj_gc_anybarriert(L, t);
787 }
788}
789
790LUALIB_API int luaopen_ffi(lua_State *L)
791{
792 CTState *cts = lj_ctype_init(L);
793 settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1)));
794 cts->finalizer = ffi_finalizer(L);
795 LJ_LIB_REG(L, NULL, ffi_meta);
796 /* NOBARRIER: basemt is a GC root. */
797 setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1)));
798 LJ_LIB_REG(L, NULL, ffi_clib);
799 LJ_LIB_REG(L, NULL, ffi_callback);
800 /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */
801 settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1));
802 L->top--;
803 lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */
804 lua_pushliteral(L, LJ_OS_NAME);
805 lua_pushliteral(L, LJ_ARCH_NAME);
806 LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */
807 ffi_register_module(L);
808 return 1;
809}
810
811#endif