#include "zend_objects_API.h"
#include "zend_globals.h"
+#define ZEND_INVOKE_FUNC_NAME "__invoke"
+
typedef struct _zend_closure {
zend_object std;
zend_function func;
zval *this_ptr;
+ zend_function *invoke;
} zend_closure;
static zend_class_entry *zend_ce_closure;
RETURN_FALSE;
}
- if (!return_value_ptr) {
- return_value_ptr = &closure_result_ptr;
- }
-
- if (call_user_function_ex(CG(function_table), NULL, this_ptr, return_value_ptr, ZEND_NUM_ARGS(), arguments, 1, NULL TSRMLS_CC) == FAILURE) {
+ if (call_user_function_ex(CG(function_table), NULL, this_ptr, &closure_result_ptr, ZEND_NUM_ARGS(), arguments, 1, NULL TSRMLS_CC) == FAILURE) {
efree(arguments);
RETURN_FALSE;
}
efree(arguments);
if (closure_result_ptr) {
- RETVAL_ZVAL(closure_result_ptr, 1, 1);
+ if (Z_ISREF_P(closure_result_ptr) && return_value_ptr) {
+ if (return_value) {
+ zval_ptr_dtor(&return_value);
+ }
+ *return_value_ptr = closure_result_ptr;
+ } else {
+ RETVAL_ZVAL(closure_result_ptr, 1, 1);
+ }
}
}
/* }}} */
-const static zend_function_entry closure_functions[] = { /* {{{ */
- ZEND_ME(Closure, __invoke, NULL, 0)
- {NULL, NULL, NULL}
-};
-/* }}} */
-
static zend_function *zend_closure_get_constructor(zval *object TSRMLS_DC) /* {{{ */
{
zend_error(E_ERROR, "Instantiation of 'Closure' is not allowed");
}
/* }}} */
+static zend_function *zend_closure_get_method(zval **object_ptr, zstr method_name, int method_len TSRMLS_DC) /* {{{ */
+{
+ unsigned int lc_name_len;
+ zstr lc_name;
+ zend_uchar type = UG(unicode)?IS_UNICODE:IS_STRING;
+
+ /* Create a zend_copy_str_tolower(dest, src, src_length); */
+ lc_name = zend_u_str_case_fold(type, method_name, method_len, 1, &lc_name_len);
+ if ((lc_name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1) &&
+ (ZEND_U_EQUAL(type, lc_name, lc_name_len, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1))) {
+ zend_closure *closure = (zend_closure *)zend_object_store_get_object(*object_ptr TSRMLS_CC);
+
+ if (!closure->invoke) {
+ closure->invoke = (zend_function*)emalloc(sizeof(zend_function));
+ closure->invoke->common = closure->func.common;
+ closure->invoke->type = ZEND_INTERNAL_FUNCTION;
+ closure->invoke->internal_function.handler = ZEND_MN(Closure___invoke);
+ closure->invoke->internal_function.module = 0;
+ closure->invoke->internal_function.scope = zend_ce_closure;
+ if (UG(unicode)) {
+ closure->invoke->internal_function.function_name.u = USTR_MAKE(ZEND_INVOKE_FUNC_NAME);
+ } else {
+ closure->invoke->internal_function.function_name.s = ZEND_INVOKE_FUNC_NAME;
+ }
+ }
+ efree(lc_name.v);
+ return (zend_function *)closure->invoke;
+ }
+ efree(lc_name.v);
+ return NULL;
+}
+/* }}} */
+
static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */
{
zend_closure *closure = (zend_closure *)object;
zval_ptr_dtor(&closure->this_ptr);
}
+ if (closure->invoke) {
+ if (UG(unicode)) {
+ efree(closure->invoke->internal_function.function_name.u);
+ }
+ efree(closure->invoke);
+ }
+
efree(closure);
}
/* }}} */
{
zend_class_entry ce;
- INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
+ INIT_CLASS_ENTRY(ce, "Closure", NULL);
zend_ce_closure = zend_register_internal_class(&ce TSRMLS_CC);
zend_ce_closure->ce_flags |= ZEND_ACC_FINAL_CLASS;
zend_ce_closure->create_object = zend_closure_new;
memcpy(&closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
closure_handlers.get_constructor = zend_closure_get_constructor;
+ closure_handlers.get_method = zend_closure_get_method;
closure_handlers.compare_objects = zend_closure_compare_objects;
closure_handlers.cast_object = zend_closure_cast_object_tostring;
closure_handlers.clone_obj = NULL;
TSRMLS_FETCH();
if (Z_TYPE_PP(p) & (IS_LEXICAL_VAR|IS_LEXICAL_REF)) {
- TSRMLS_FETCH();
is_ref = Z_TYPE_PP(p) & IS_LEXICAL_REF;
if (!EG(active_symbol_table)) {
zend_uchar utype = UG(unicode)?IS_UNICODE:IS_STRING;
if (utype == IS_UNICODE) {
- key.u = USTR_MAKE("__invoke");
+ key.u = USTR_MAKE(ZEND_INVOKE_FUNC_NAME);
} else {
- key.s = "__invoke";
+ key.s = ZEND_INVOKE_FUNC_NAME;
}
if (Z_TYPE_P(obj) == IS_OBJECT) {
efree(key.u);
}
return SUCCESS;
- } else if (zend_u_hash_find(&ce->function_table, utype, key, sizeof("__invoke"), (void**)fptr_ptr) == SUCCESS) {
+ } else if (zend_u_hash_find(&ce->function_table, utype, key, sizeof(ZEND_INVOKE_FUNC_NAME), (void**)fptr_ptr) == SUCCESS) {
*ce_ptr = ce;
if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
if (zobj_ptr) {