X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Flibffi.c;h=8cf29ddbd7dc445c29b302e6fbc1c48cab1d47b2;hb=de01badbc6098fe8a2f68afef5bc984b9ed371a5;hp=2dbe0bcbd3dab226c47e0204006920df8e171935;hpb=5359ad5c181e5772f350fe1cba060cbed3a05b91;p=m6w6%2Fext-psi diff --git a/src/libffi.c b/src/libffi.c index 2dbe0bc..8cf29dd 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H # include "config.h" +#else +# include "php_config.h" #endif #include "php.h" @@ -7,7 +9,6 @@ #ifdef HAVE_LIBFFI #include "php_psi.h" -#include "ffi.h" #include "engine.h" #undef PACKAGE @@ -328,6 +329,37 @@ static void psi_ffi_init(struct psi_context *C) C->context = psi_ffi_context_init(NULL); } +static inline void psi_ffi_destroy_callbacks(struct psi_context *C, let_val *let_val) { + let_callback *cb; + let_func *fn = NULL; + + switch (let_val->kind) { + case PSI_LET_CALLBACK: + cb = let_val->data.callback; + + if (cb->decl && cb->decl->call.info) { + psi_ffi_call_free(cb->decl->call.info); + } + fn = cb->func; + /* no break */ + case PSI_LET_FUNC: + if (!fn) { + fn = let_val->data.func; + } + + if (fn->inner) { + size_t i; + + for (i = 0; i < fn->inner->count; ++i) { + psi_ffi_destroy_callbacks(C, fn->inner->vals[i]); + } + } + break; + default: + break; + } +} + static void psi_ffi_dtor(struct psi_context *C) { if (C->decls) { @@ -364,6 +396,41 @@ static void psi_ffi_dtor(struct psi_context *C) psi_ffi_context_free((void *) &C->context); } +static inline void psi_ffi_compile_callbacks(struct psi_context *C, let_val *let_val) { + struct psi_ffi_call *call; + let_callback *cb; + let_func *fn = NULL; + + switch (let_val->kind) { + case PSI_LET_CALLBACK: + cb = let_val->data.callback; + if ((call = psi_ffi_call_alloc(C, cb->decl))) { + if (FFI_OK != psi_ffi_call_init_callback_closure(C, call, cb)) { + psi_ffi_call_free(call); + break; + } + + cb->decl->call.sym = call->code; + } + fn = cb->func; + /* no break */ + case PSI_LET_FUNC: + if (!fn) { + fn = let_val->data.func; + } + if (fn->inner) { + size_t i; + + for (i = 0; i < fn->inner->count; ++i) { + psi_ffi_compile_callbacks(C, fn->inner->vals[i]); + } + } + break; + default: + break; + } +} + static zend_function_entry *psi_ffi_compile(struct psi_context *C) { size_t c, i, j = 0; @@ -382,12 +449,12 @@ static zend_function_entry *psi_ffi_compile(struct psi_context *C) if (!impl->decl) { continue; } - - if ((call = psi_ffi_call_alloc(C, impl->decl))) { - if (FFI_OK != psi_ffi_call_init_closure(C, call, impl)) { - psi_ffi_call_free(call); - continue; - } + if (!(call = psi_ffi_call_alloc(C, impl->decl))) { + continue; + } + if (FFI_OK != psi_ffi_call_init_closure(C, call, impl)) { + psi_ffi_call_free(call); + continue; } zf->fname = impl->func->name + (impl->func->name[0] == '\\'); @@ -397,20 +464,7 @@ static zend_function_entry *psi_ffi_compile(struct psi_context *C) ++j; for (c = 0; c < impl->stmts->let.count; ++c) { - let_stmt *let = impl->stmts->let.list[c]; - - if (let->val && let->val->kind == PSI_LET_CALLBACK) { - let_callback *cb = let->val->data.callback; - - if ((call = psi_ffi_call_alloc(C, cb->decl))) { - if (FFI_OK != psi_ffi_call_init_callback_closure(C, call, cb)) { - psi_ffi_call_free(call); - continue; - } - - cb->decl->call.sym = call->code; - } - } + psi_ffi_compile_callbacks(C, impl->stmts->let.list[c]->val); } }