X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Flibffi.c;h=f776fb7670455cf50a2758ac3fc7f039d5926837;hp=d5158271b65879a31afccd66dbbbef6f922f5730;hb=574519ef5e3ab76f00253fad870afce7135638d1;hpb=61918592ab618c073b9846783ce79fed9f26c5f7 diff --git a/src/libffi.c b/src/libffi.c index d515827..f776fb7 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -93,6 +93,20 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_double; } } +static inline ffi_type *psi_ffi_impl_type(token_t impl_type) { + switch (impl_type) { + case PSI_T_BOOL: + return &ffi_type_sint8; + case PSI_T_INT: + return &ffi_type_sint64; + case PSI_T_STRING: + return &ffi_type_pointer; + case PSI_T_FLOAT: + case PSI_T_DOUBLE: + return &ffi_type_double; + EMPTY_SWITCH_DEFAULT_CASE(); + } +} static inline ffi_type *psi_ffi_decl_type(decl_type *type) { return psi_ffi_token_type(real_decl_type(type)->type); } @@ -127,6 +141,8 @@ static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) { call->params[c] = NULL; decl->call.info = call; + decl->call.rval = decl->func->ptr; + decl->call.argc = c; decl->call.args = (void **) &call->params[c+1]; rc = ffi_prep_cif(&call->signature, psi_ffi_abi(decl->abi->convention), @@ -152,7 +168,7 @@ static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *cal call->code); #elif PSI_HAVE_FFI_PREP_CLOSURE - rc = ffi_prep_closure(data->code, &context->signature, psi_ffi_handler, data); + rc = ffi_prep_closure(call->code, &context->signature, psi_ffi_handler, impl); #else # error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() available" #endif @@ -250,10 +266,33 @@ static zend_function_entry *psi_ffi_compile(PSI_Context *C) return zfe; } -static void psi_ffi_call(PSI_Context *C, impl_val *ret_val, decl *decl) { - PSI_LibffiCall *call = decl->call.info; +static void psi_ffi_call(PSI_Context *C, decl_callinfo *decl_call, impl_vararg *va) { + PSI_LibffiCall *call = decl_call->info; + + if (va) { + ffi_status rc; + ffi_cif signature; + size_t i, nfixedargs = decl_call->argc, ntotalargs = nfixedargs + va->args->count; + void **params = calloc(2 * ntotalargs + 2, sizeof(void *)); + + for (i = 0; i < nfixedargs; ++i) { + params[i] = call->params[i]; + params[i + ntotalargs + 1] = call->params[i + nfixedargs + 1]; + } + for (i = 0; i < va->args->count; ++i) { + params[nfixedargs + i] = psi_ffi_impl_type(va->types[i]); + params[nfixedargs + i + ntotalargs + 1] = &va->values[i]; + } - ffi_call(&call->signature, FFI_FN(decl->call.sym), ret_val, decl->call.args); + rc = ffi_prep_cif_var(&signature, call->signature.abi, + nfixedargs, ntotalargs, + call->signature.rtype, (ffi_type **) params); + ZEND_ASSERT(FFI_OK == rc); + ffi_call(&signature, FFI_FN(decl_call->sym), decl_call->rval, ¶ms[ntotalargs + 1]); + free(params); + } else { + ffi_call(&call->signature, FFI_FN(decl_call->sym), decl_call->rval, decl_call->args); + } } static PSI_ContextOps ops = {