beta-0.89.2
[luatex.git] / source / libs / luajit / LuaJIT-src / src / lj_cdata.c
blob5cd2c1140efd61fc0a749a25e308d7aaea27b91d
1 /*
2 ** C data management.
3 ** Copyright (C) 2005-2015 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include "lj_obj.h"
8 #if LJ_HASFFI
10 #include "lj_gc.h"
11 #include "lj_err.h"
12 #include "lj_tab.h"
13 #include "lj_ctype.h"
14 #include "lj_cconv.h"
15 #include "lj_cdata.h"
17 /* -- C data allocation --------------------------------------------------- */
19 /* Allocate a new C data object holding a reference to another object. */
20 GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id)
22 CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR);
23 GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR);
24 *(const void **)cdataptr(cd) = p;
25 return cd;
28 /* Allocate variable-sized or specially aligned C data object. */
29 GCcdata *lj_cdata_newv(lua_State *L, CTypeID id, CTSize sz, CTSize align)
31 global_State *g;
32 MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) +
33 (align > CT_MEMALIGN ? (1u<<align) - (1u<<CT_MEMALIGN) : 0);
34 char *p = lj_mem_newt(L, extra + sz, char);
35 uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata);
36 uintptr_t almask = (1u << align) - 1u;
37 GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata));
38 lua_assert((char *)cd - p < 65536);
39 cdatav(cd)->offset = (uint16_t)((char *)cd - p);
40 cdatav(cd)->extra = extra;
41 cdatav(cd)->len = sz;
42 g = G(L);
43 setgcrefr(cd->nextgc, g->gc.root);
44 setgcref(g->gc.root, obj2gco(cd));
45 newwhite(g, obj2gco(cd));
46 cd->marked |= 0x80;
47 cd->gct = ~LJ_TCDATA;
48 cd->ctypeid = id;
49 return cd;
52 /* Free a C data object. */
53 void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd)
55 if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) {
56 GCobj *root;
57 makewhite(g, obj2gco(cd));
58 markfinalized(obj2gco(cd));
59 if ((root = gcref(g->gc.mmudata)) != NULL) {
60 setgcrefr(cd->nextgc, root->gch.nextgc);
61 setgcref(root->gch.nextgc, obj2gco(cd));
62 setgcref(g->gc.mmudata, obj2gco(cd));
63 } else {
64 setgcref(cd->nextgc, obj2gco(cd));
65 setgcref(g->gc.mmudata, obj2gco(cd));
67 } else if (LJ_LIKELY(!cdataisv(cd))) {
68 CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid);
69 CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR;
70 lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) ||
71 ctype_isextern(ct->info));
72 lj_mem_free(g, cd, sizeof(GCcdata) + sz);
73 } else {
74 lj_mem_free(g, memcdatav(cd), sizecdatav(cd));
78 void lj_cdata_setfin(lua_State *L, GCcdata *cd, GCobj *obj, uint32_t it)
80 GCtab *t = ctype_ctsG(G(L))->finalizer;
81 if (gcref(t->metatable)) {
82 /* Add cdata to finalizer table, if still enabled. */
83 TValue *tv, tmp;
84 setcdataV(L, &tmp, cd);
85 lj_gc_anybarriert(L, t);
86 tv = lj_tab_set(L, t, &tmp);
87 setgcV(L, tv, obj, it);
88 if (!tvisnil(tv))
89 cd->marked |= LJ_GC_CDATA_FIN;
90 else
91 cd->marked &= ~LJ_GC_CDATA_FIN;
95 /* -- C data indexing ----------------------------------------------------- */
97 /* Index C data by a TValue. Return CType and pointer. */
98 CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
99 CTInfo *qual)
101 uint8_t *p = (uint8_t *)cdataptr(cd);
102 CType *ct = ctype_get(cts, cd->ctypeid);
103 ptrdiff_t idx;
105 /* Resolve reference for cdata object. */
106 if (ctype_isref(ct->info)) {
107 lua_assert(ct->size == CTSIZE_PTR);
108 p = *(uint8_t **)p;
109 ct = ctype_child(cts, ct);
112 collect_attrib:
113 /* Skip attributes and collect qualifiers. */
114 while (ctype_isattrib(ct->info)) {
115 if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size;
116 ct = ctype_child(cts, ct);
118 lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */
120 if (tvisint(key)) {
121 idx = (ptrdiff_t)intV(key);
122 goto integer_key;
123 } else if (tvisnum(key)) { /* Numeric key. */
124 #ifdef _MSC_VER
125 /* Workaround for MSVC bug. */
126 volatile
127 #endif
128 lua_Number n = numV(key);
129 idx = LJ_64 ? (ptrdiff_t)n : (ptrdiff_t)lj_num2int(n);
130 integer_key:
131 if (ctype_ispointer(ct->info)) {
132 CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
133 if (sz == CTSIZE_INVALID)
134 lj_err_caller(cts->L, LJ_ERR_FFI_INVSIZE);
135 if (ctype_isptr(ct->info)) {
136 p = (uint8_t *)cdata_getptr(p, ct->size);
137 } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) {
138 if ((ct->info & CTF_COMPLEX)) idx &= 1;
139 *qual |= CTF_CONST; /* Valarray elements are constant. */
141 *pp = p + idx*(int32_t)sz;
142 return ct;
144 } else if (tviscdata(key)) { /* Integer cdata key. */
145 GCcdata *cdk = cdataV(key);
146 CType *ctk = ctype_raw(cts, cdk->ctypeid);
147 if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);
148 if (ctype_isinteger(ctk->info)) {
149 lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk,
150 (uint8_t *)&idx, cdataptr(cdk), 0);
151 goto integer_key;
153 } else if (tvisstr(key)) { /* String key. */
154 GCstr *name = strV(key);
155 if (ctype_isstruct(ct->info)) {
156 CTSize ofs;
157 CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual);
158 if (fct) {
159 *pp = p + ofs;
160 return fct;
162 } else if (ctype_iscomplex(ct->info)) {
163 if (name->len == 2) {
164 *qual |= CTF_CONST; /* Complex fields are constant. */
165 if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') {
166 *pp = p;
167 return ct;
168 } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') {
169 *pp = p + (ct->size >> 1);
170 return ct;
173 } else if (cd->ctypeid == CTID_CTYPEID) {
174 /* Allow indexing a (pointer to) struct constructor to get constants. */
175 CType *sct = ctype_raw(cts, *(CTypeID *)p);
176 if (ctype_isptr(sct->info))
177 sct = ctype_rawchild(cts, sct);
178 if (ctype_isstruct(sct->info)) {
179 CTSize ofs;
180 CType *fct = lj_ctype_getfield(cts, sct, name, &ofs);
181 if (fct && ctype_isconstval(fct->info))
182 return fct;
184 ct = sct; /* Allow resolving metamethods for constructors, too. */
187 if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
188 if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) {
189 p = (uint8_t *)cdata_getptr(p, ct->size);
190 ct = ctype_child(cts, ct);
191 goto collect_attrib;
194 *qual |= 1; /* Lookup failed. */
195 return ct; /* But return the resolved raw type. */
198 /* -- C data getters ------------------------------------------------------ */
200 /* Get constant value and convert to TValue. */
201 static void cdata_getconst(CTState *cts, TValue *o, CType *ct)
203 CType *ctt = ctype_child(cts, ct);
204 lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4);
205 /* Constants are already zero-extended/sign-extended to 32 bits. */
206 if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0)
207 setnumV(o, (lua_Number)(uint32_t)ct->size);
208 else
209 setintV(o, (int32_t)ct->size);
212 /* Get C data value and convert to TValue. */
213 int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp)
215 CTypeID sid;
217 if (ctype_isconstval(s->info)) {
218 cdata_getconst(cts, o, s);
219 return 0; /* No GC step needed. */
220 } else if (ctype_isbitfield(s->info)) {
221 return lj_cconv_tv_bf(cts, s, o, sp);
224 /* Get child type of pointer/array/field. */
225 lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info));
226 sid = ctype_cid(s->info);
227 s = ctype_get(cts, sid);
229 /* Resolve reference for field. */
230 if (ctype_isref(s->info)) {
231 lua_assert(s->size == CTSIZE_PTR);
232 sp = *(uint8_t **)sp;
233 sid = ctype_cid(s->info);
234 s = ctype_get(cts, sid);
237 /* Skip attributes. */
238 while (ctype_isattrib(s->info))
239 s = ctype_child(cts, s);
241 return lj_cconv_tv_ct(cts, s, sid, o, sp);
244 /* -- C data setters ------------------------------------------------------ */
246 /* Convert TValue and set C data value. */
247 void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual)
249 if (ctype_isconstval(d->info)) {
250 goto err_const;
251 } else if (ctype_isbitfield(d->info)) {
252 if (((d->info|qual) & CTF_CONST)) goto err_const;
253 lj_cconv_bf_tv(cts, d, dp, o);
254 return;
257 /* Get child type of pointer/array/field. */
258 lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info));
259 d = ctype_child(cts, d);
261 /* Resolve reference for field. */
262 if (ctype_isref(d->info)) {
263 lua_assert(d->size == CTSIZE_PTR);
264 dp = *(uint8_t **)dp;
265 d = ctype_child(cts, d);
268 /* Skip attributes and collect qualifiers. */
269 for (;;) {
270 if (ctype_isattrib(d->info)) {
271 if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size;
272 } else {
273 break;
275 d = ctype_child(cts, d);
278 lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info));
280 if (((d->info|qual) & CTF_CONST)) {
281 err_const:
282 lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST);
285 lj_cconv_ct_tv(cts, d, dp, o, 0);
288 #endif