From fa2572da6c45a6dc18cec8cf9ecc80869b6ac04e Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Tue, 29 May 2012 12:08:21 +0200 Subject: [PATCH] FFI: Convert io.* file handle to FILE * pointer (but as a void *). --- doc/ext_ffi_semantics.html | 22 ++++++++++++---------- src/lj_cconv.c | 5 ++++- src/lj_crecord.c | 9 ++++++++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/doc/ext_ffi_semantics.html b/doc/ext_ffi_semantics.html index 304befa7..72b46597 100644 --- a/doc/ext_ffi_semantics.html +++ b/doc/ext_ffi_semantics.html @@ -289,22 +289,24 @@ arguments to C calls: nilNULL(void *) -userdatauserdata payload →(void *) - lightuserdatalightuserdata address →(void *) - -stringmatch against enum constantenum -stringcopy string data + zero-byteint8_t[], uint8_t[] +userdatauserdata payload →(void *) -stringstring data →const char[] +io.* fileget FILE * handle →(void *) -functioncreate callback →C function type - -tabletable initializerArray +stringmatch against enum constantenum + +stringcopy string data + zero-byteint8_t[], uint8_t[] -tabletable initializerstruct/union +stringstring data →const char[] +functioncreate callback →C function type + +tabletable initializerArray + +tabletable initializerstruct/union + cdatacdata payload →C type

diff --git a/src/lj_cconv.c b/src/lj_cconv.c index 240a8d10..b09c0abe 100644 --- a/src/lj_cconv.c +++ b/src/lj_cconv.c @@ -603,7 +603,10 @@ void lj_cconv_ct_tv(CTState *cts, CType *d, tmpptr = (void *)0; flags |= CCF_FROMTV; } else if (tvisudata(o)) { - tmpptr = uddata(udataV(o)); + GCudata *ud = udataV(o); + tmpptr = uddata(ud); + if (ud->udtype == UDTYPE_IO_FILE) + tmpptr = *(void **)tmpptr; } else if (tvislightud(o)) { tmpptr = lightudV(o); } else if (tvisfunc(o)) { diff --git a/src/lj_crecord.c b/src/lj_crecord.c index 74d62d6c..268800e4 100644 --- a/src/lj_crecord.c +++ b/src/lj_crecord.c @@ -385,7 +385,14 @@ static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval) } else if (tref_isnil(sp)) { sp = lj_ir_kptr(J, NULL); } else if (tref_isudata(sp)) { - sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCudata))); + GCudata *ud = udataV(sval); + if (ud->udtype == UDTYPE_IO_FILE) { + TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE); + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); + sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE); + } else { + sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCudata))); + } } else if (tref_isstr(sp)) { if (ctype_isenum(d->info)) { /* Match string against enum constant. */ GCstr *str = strV(sval);