aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2011-01-12 21:24:15 +0100
committerMike Pall <mike>2011-01-12 21:33:58 +0100
commita5e48285e39af2e794288091fafc02e113e1fa82 (patch)
treef1089c402a2d93fdc3dc5a58559333cfe18fb7ad /src
parent2363399b10cf79196df4dde1554027db2b50faf9 (diff)
downloadluajit-a5e48285e39af2e794288091fafc02e113e1fa82.tar.gz
luajit-a5e48285e39af2e794288091fafc02e113e1fa82.tar.bz2
luajit-a5e48285e39af2e794288091fafc02e113e1fa82.zip
FFI: Rearrange code in lib_ffi.c.
Diffstat (limited to 'src')
-rw-r--r--src/lib_ffi.c98
1 files changed, 50 insertions, 48 deletions
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 2b895ed5..b040a652 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -77,6 +77,8 @@ static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
77 return p; 77 return p;
78} 78}
79 79
80/* -- C data arithmetic --------------------------------------------------- */
81
80typedef struct FFIArith { 82typedef struct FFIArith {
81 uint8_t *p[2]; 83 uint8_t *p[2];
82 CType *ct[2]; 84 CType *ct[2];
@@ -111,54 +113,6 @@ static void ffi_checkarith(lua_State *L, CTState *cts, FFIArith *fa)
111 } 113 }
112} 114}
113 115
114/* -- C type metamethods -------------------------------------------------- */
115
116#define LJLIB_MODULE_ffi_meta
117
118LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
119{
120 CTState *cts = ctype_cts(L);
121 CTInfo qual = 0;
122 CType *ct;
123 uint8_t *p;
124 TValue *o = L->base;
125 if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */
126 lj_err_argt(L, 1, LUA_TCDATA);
127 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
128 if (lj_cdata_get(cts, ct, L->top-1, p))
129 lj_gc_check(L);
130 return 1;
131}
132
133LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
134{
135 CTState *cts = ctype_cts(L);
136 CTInfo qual = 0;
137 CType *ct;
138 uint8_t *p;
139 TValue *o = L->base;
140 if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */
141 lj_err_argt(L, 1, LUA_TCDATA);
142 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
143 lj_cdata_set(cts, ct, p, o+2, qual);
144 return 0;
145}
146
147/* Forward declaration. */
148static int lj_cf_ffi_new(lua_State *L);
149
150LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
151{
152 GCcdata *cd = ffi_checkcdata(L, 1);
153 int ret;
154 if (cd->typeid == CTID_CTYPEID)
155 return lj_cf_ffi_new(L);
156 if ((ret = lj_ccall_func(L, cd)) < 0)
157 lj_err_callerv(L, LJ_ERR_FFI_BADCALL,
158 strdata(lj_ctype_repr(L, cd->typeid, NULL)));
159 return ret;
160}
161
162/* Pointer arithmetic. */ 116/* Pointer arithmetic. */
163static int ffi_arith_ptr(lua_State *L, CTState *cts, FFIArith *fa, MMS mm) 117static int ffi_arith_ptr(lua_State *L, CTState *cts, FFIArith *fa, MMS mm)
164{ 118{
@@ -296,6 +250,54 @@ static int ffi_arith(lua_State *L)
296 return 1; 250 return 1;
297} 251}
298 252
253/* -- C type metamethods -------------------------------------------------- */
254
255#define LJLIB_MODULE_ffi_meta
256
257LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
258{
259 CTState *cts = ctype_cts(L);
260 CTInfo qual = 0;
261 CType *ct;
262 uint8_t *p;
263 TValue *o = L->base;
264 if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */
265 lj_err_argt(L, 1, LUA_TCDATA);
266 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
267 if (lj_cdata_get(cts, ct, L->top-1, p))
268 lj_gc_check(L);
269 return 1;
270}
271
272LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
273{
274 CTState *cts = ctype_cts(L);
275 CTInfo qual = 0;
276 CType *ct;
277 uint8_t *p;
278 TValue *o = L->base;
279 if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */
280 lj_err_argt(L, 1, LUA_TCDATA);
281 ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual);
282 lj_cdata_set(cts, ct, p, o+2, qual);
283 return 0;
284}
285
286/* Forward declaration. */
287static int lj_cf_ffi_new(lua_State *L);
288
289LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call)
290{
291 GCcdata *cd = ffi_checkcdata(L, 1);
292 int ret;
293 if (cd->typeid == CTID_CTYPEID)
294 return lj_cf_ffi_new(L);
295 if ((ret = lj_ccall_func(L, cd)) < 0)
296 lj_err_callerv(L, LJ_ERR_FFI_BADCALL,
297 strdata(lj_ctype_repr(L, cd->typeid, NULL)));
298 return ret;
299}
300
299LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add) 301LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add)
300{ 302{
301 return ffi_arith(L); 303 return ffi_arith(L);