flush
authorMichael Wallner <mike@php.net>
Mon, 8 Feb 2016 07:36:36 +0000 (08:36 +0100)
committerMichael Wallner <mike@php.net>
Mon, 8 Feb 2016 07:36:36 +0000 (08:36 +0100)
src/engine.c
src/libffi.c
src/parser.h
src/parser.re
src/parser_proc.h
src/parser_proc.y

index dc9d2746f235aff80c90a36a35eaf7319dbf0488..a86cd0b62460e598d782912cb59dd6368fcbd99d 100644 (file)
@@ -193,6 +193,8 @@ static inline ZEND_RESULT_CODE psi_parse_args(zend_execute_data *execute_data, i
                        Z_PARAM_PROLOGUE(0);
                } else if (PSI_T_MIXED == iarg->type->type) {
                        Z_PARAM_PROLOGUE(0);
+               } else if (PSI_T_CALLABLE == iarg->type->type) {
+                       Z_PARAM_FUNC_EX(iarg->val.zend.cb.fci, iarg->val.zend.cb.fcc, 1, 0);
                } else {
                        error_code = ZPP_ERROR_FAILURE;
                        break;
@@ -323,6 +325,11 @@ static inline impl_val *psi_let_val(token_t let_func, impl_arg *iarg, impl_val *
                        arg_val->ptr = obj->data;
                }
                break;
+       case PSI_T_CBVAL:
+               if (iarg->type->type == PSI_T_CALLABLE) {
+                       
+               }
+               break;
        EMPTY_SWITCH_DEFAULT_CASE();
        }
        return arg_val;
index be9edfd26ec1cb4a5df3a7210fc3a5b94f482907..428a7e647e8441a9a499728fcbb2f38bb349c9a8 100644 (file)
@@ -43,10 +43,25 @@ static void *psi_ffi_closure_alloc(size_t s, void **code)
        }
        return *code;
 #else
-       return NULL;
+# error "Neither ffi_closure_alloc() nor mmap() available"
 #endif
 }
 
+static ffi_status psi_ffi_prep_closure(ffi_closure **closure, void **code, ffi_cif *sig, void (*handler)(ffi_cif*,void*,void**,void*), void *data) {
+       *closure = psi_ffi_closure_alloc(sizeof(ffi_closure), code);
+       ZEND_ASSERT(*closure != NULL);
+
+#if PSI_HAVE_FFI_PREP_CLOSURE_LOC
+       return ffi_prep_closure_loc(*closure, sig, handler, data, *code);
+
+#elif PSI_HAVE_FFI_PREP_CLOSURE
+       return ffi_prep_closure(*code, sig, handler, data);
+#else
+# error "Neither ffi_prep_closure() nor ffi_prep_closure_loc() is available"
+#endif
+
+}
+
 static void psi_ffi_closure_free(void *c)
 {
 #ifdef PSI_HAVE_FFI_CLOSURE_ALLOC
@@ -59,9 +74,59 @@ static void psi_ffi_closure_free(void *c)
 static void psi_ffi_handler(ffi_cif *signature, void *_result, void **_args, void *_data);
 static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg);
 
+typedef struct PSI_LibffiContext {
+       ffi_cif signature;
+       ffi_type *params[2];
+} PSI_LibffiContext;
+
+typedef struct PSI_LibffiCall {
+       void *code;
+       ffi_closure *closure;
+       ffi_cif signature;
+       void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
+} PSI_LibffiCall;
+
 static inline ffi_abi psi_ffi_abi(const char *convention) {
        return FFI_DEFAULT_ABI;
 }
+
+static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
+       int rc;
+       size_t i, c = decl->args ? decl->args->count : 0;
+       PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
+
+       for (i = 0; i < c; ++i) {
+               call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
+       }
+       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),
+                       c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
+       ZEND_ASSERT(FFI_OK == rc);
+
+       return call;
+}
+
+static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
+       PSI_LibffiContext *context = C->context;
+       ffi_status rc;
+
+       rc = psi_ffi_prep_closure(&call->closure, &call->code, &context->signature, psi_ffi_handler, impl);
+       ZEND_ASSERT(FFI_OK == rc);
+}
+
+static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
+       if (call->closure) {
+               psi_ffi_closure_free(call->closure);
+       }
+       free(call);
+}
+
 static inline ffi_type *psi_ffi_token_type(token_t t) {
        switch (t) {
        default:
@@ -194,6 +259,20 @@ static inline ffi_type *psi_ffi_decl_type(decl_type *type) {
        decl_type *real = real_decl_type(type);
 
        switch (real->type) {
+       case PSI_T_FUNCTION:
+               if (!real->func->call.sym) {
+                       PSI_LibffiCall *call = PSI_LibffiCallAlloc(&PSI_G(context), real->func);
+                       ffi_status rc;
+
+                       rc = psi_ffi_prep_closure(&real->func->call.closure.data, &real->func->call.sym,
+                                       &call->signature, psi_ffi_handler, NULL);
+                       if (FFI_OK == rc) {
+                               real->func->call.info = call;
+                               real->func->call.closure.dtor = psi_ffi_closure_free;
+                       }
+               }
+               return &ffi_type_pointer;
+
        case PSI_T_STRUCT:
                if (!real->strct->engine.type) {
                        ffi_type *strct = calloc(1, sizeof(ffi_type));
@@ -223,69 +302,6 @@ static inline ffi_type *psi_ffi_decl_arg_type(decl_arg *darg) {
        }
 }
 
-typedef struct PSI_LibffiContext {
-       ffi_cif signature;
-       ffi_type *params[2];
-} PSI_LibffiContext;
-
-typedef struct PSI_LibffiCall {
-       void *code;
-       ffi_closure *closure;
-       ffi_cif signature;
-       void *params[1]; /* [type1, type2, NULL, arg1, arg2] ... */
-} PSI_LibffiCall;
-
-static inline PSI_LibffiCall *PSI_LibffiCallAlloc(PSI_Context *C, decl *decl) {
-       int rc;
-       size_t i, c = decl->args ? decl->args->count : 0;
-       PSI_LibffiCall *call = calloc(1, sizeof(*call) + 2 * c * sizeof(void *));
-
-       for (i = 0; i < c; ++i) {
-               call->params[i] = psi_ffi_decl_arg_type(decl->args->args[i]);
-       }
-       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),
-                       c, psi_ffi_decl_arg_type(decl->func), (ffi_type **) call->params);
-       ZEND_ASSERT(FFI_OK == rc);
-
-       return call;
-}
-
-static inline void PSI_LibffiCallInitClosure(PSI_Context *C, PSI_LibffiCall *call, impl *impl) {
-       PSI_LibffiContext *context = C->context;
-       int rc;
-
-       call->closure = psi_ffi_closure_alloc(sizeof(ffi_closure), &call->code);
-       ZEND_ASSERT(call->closure != NULL);
-
-#if PSI_HAVE_FFI_PREP_CLOSURE_LOC
-       rc = ffi_prep_closure_loc(
-                       call->closure,
-                       &context->signature,
-                       psi_ffi_handler,
-                       impl,
-                       call->code);
-
-#elif PSI_HAVE_FFI_PREP_CLOSURE
-       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
-       ZEND_ASSERT(FFI_OK == rc);
-}
-
-static inline void PSI_LibffiCallFree(PSI_LibffiCall *call) {
-       if (call->closure) {
-               psi_ffi_closure_free(call->closure);
-       }
-       free(call);
-}
 
 static inline PSI_LibffiContext *PSI_LibffiContextInit(PSI_LibffiContext *L) {
        ffi_status rc;
index 94860c5248d0e0fd9e6076b4ae90e5e4ce5a2fe5..350213c940d88a133c007b91886bd4a25539cf90 100644 (file)
@@ -7,6 +7,7 @@
 #include <string.h>
 
 #include <Zend/zend_types.h>
+#include <Zend/zend_API.h> /* fcall */
 
 #include "parser_proc.h"
 
@@ -51,6 +52,10 @@ typedef union impl_val {
                zend_bool bval;
                zend_long lval;
                zend_string *str;
+               struct {
+                       zend_fcall_info *fci;
+                       zend_fcall_info_cache *fcc;
+               } cb;
        } zend;
        void *ptr;
        uint8_t _dbg[sizeof(void *)];
@@ -290,6 +295,10 @@ typedef struct decl_callinfo {
        size_t argc;
        void **args;
        void **rval;
+       struct {
+               void *data;
+               void (*dtor)(void *data);
+       } closure;
 } decl_callinfo;
 
 typedef struct decl {
index 7e445fd8a0a99c1fc9296803fe920e792cdce7a0..bb6ccab9225579572ee59defec89fbdd8ef74a6f 100644 (file)
@@ -234,6 +234,7 @@ token_t PSI_ParserScan(PSI_Parser *P)
                'BOOLVAL' {RETURN(PSI_T_BOOLVAL);}
                'ARRVAL' {RETURN(PSI_T_ARRVAL);}
                'OBJVAL' {RETURN(PSI_T_OBJVAL);}
+               'CBVAL' {RETURN(PSI_T_CBVAL);}
                'CALLOC' {RETURN(PSI_T_CALLOC);}
                'TO_OBJECT' {RETURN(PSI_T_TO_OBJECT);}
                'TO_ARRAY' {RETURN(PSI_T_TO_ARRAY);}
index 8471c14bdb651379da6cbe4e11da526ab8d189a5..04ebb7e1c7fb3a6649f809abb3968f8090e17403 100644 (file)
 #define PSI_T_TRUE                            54
 #define PSI_T_FALSE                           55
 #define PSI_T_DOLLAR_NAME                     56
-#define PSI_T_OBJVAL                          57
-#define PSI_T_ARRVAL                          58
-#define PSI_T_PATHVAL                         59
-#define PSI_T_STRLEN                          60
-#define PSI_T_STRVAL                          61
-#define PSI_T_FLOATVAL                        62
-#define PSI_T_INTVAL                          63
-#define PSI_T_BOOLVAL                         64
-#define PSI_T_TO_OBJECT                       65
-#define PSI_T_TO_ARRAY                        66
-#define PSI_T_TO_STRING                       67
-#define PSI_T_TO_INT                          68
-#define PSI_T_TO_FLOAT                        69
-#define PSI_T_TO_BOOL                         70
-#define PSI_T_MIXED                           71
-#define PSI_T_ARRAY                           72
-#define PSI_T_OBJECT                          73
-#define PSI_T_CALLABLE                        74
-#define PSI_T_AMPERSAND                       75
+#define PSI_T_CBVAL                           57
+#define PSI_T_OBJVAL                          58
+#define PSI_T_ARRVAL                          59
+#define PSI_T_PATHVAL                         60
+#define PSI_T_STRLEN                          61
+#define PSI_T_STRVAL                          62
+#define PSI_T_FLOATVAL                        63
+#define PSI_T_INTVAL                          64
+#define PSI_T_BOOLVAL                         65
+#define PSI_T_TO_OBJECT                       66
+#define PSI_T_TO_ARRAY                        67
+#define PSI_T_TO_STRING                       68
+#define PSI_T_TO_INT                          69
+#define PSI_T_TO_FLOAT                        70
+#define PSI_T_TO_BOOL                         71
+#define PSI_T_MIXED                           72
+#define PSI_T_ARRAY                           73
+#define PSI_T_OBJECT                          74
+#define PSI_T_CALLABLE                        75
+#define PSI_T_AMPERSAND                       76
index b4bf9600f9d2e2b675439d50dd0c1d9fa0dbe2f6..87f5eae89835b67773ae584161b3c30326a64edc 100644 (file)
@@ -720,7 +720,7 @@ let_val(val) ::= let_func(func). {
 let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
        alloc = init_let_calloc(nmemb, size);
 }
-%token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
+%token_class let_func_token CBVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
 %type let_func {let_func*}
 %destructor let_func {free_let_func($$);}
 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {