com php-src: Implemented Closure::apply: Zend/tests/closure_a pply.phpt Zend/zend_closures.c Zend/zend_execute_ API.c
| From: | Tjerk Anne Meesters | Date: | Tue, 29 Jul 2014 01:36:17 +0000 |
| Subject: | com php-src: Implemented Closure::apply: Zend/tests/closure_a pply.phpt Zend/zend_closures.c Zend/zend_execute_ API.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-81164@lists.php.net to get a copy of this message | ||
Commit: 167128d854076d5ca1192725daeb12fe92d76436
Author: Andrea Faulds <ajf@ajf.me> Tue, 29 Jul 2014 02:36:17 +0100
Parents: f281a315f7acaefba0dc1699bf15c009b6d8a0ae
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=167128d854076d5ca1192725daeb12fe92d76436
Log:
Implemented Closure::apply
Changed paths:
A Zend/tests/closure_apply.phpt
M Zend/zend_closures.c
M Zend/zend_execute_API.c
Diff:
diff --git a/Zend/tests/closure_apply.phpt b/Zend/tests/closure_apply.phpt
new file mode 100644
index 0000000..9599031
--- /dev/null
+++ b/Zend/tests/closure_apply.phpt
@@ -0,0 +1,48 @@
+--TEST--
+Closure::apply
+--FILE--
+<?php
+
+class Foo {
+ public $x = 0;
+ function bar() {
+ return function () {
+ return $this->x;
+ };
+ }
+}
+
+$foo = new Foo;
+$qux = $foo->bar();
+
+$foobar = new Foo;
+$foobar->x = 3;
+
+var_dump($qux());
+var_dump($qux->apply($foo));
+var_dump($qux->apply($foobar));
+
+
+$bar = function () {
+ return $this->x;
+};
+
+$elePHPant = new StdClass;
+$elePHPant->x = 7;
+
+var_dump($bar->apply($elePHPant));
+
+
+$beta = function ($z) {
+ return $this->x * $z;
+};
+
+var_dump($beta->apply($elePHPant, 3));
+
+?>
+--EXPECT--
+int(0)
+int(0)
+int(3)
+int(7)
+int(21)
\ No newline at end of file
diff --git a/Zend/zend_closures.c b/Zend/zend_closures.c
index bd2ede3..c2d9de4 100644
--- a/Zend/zend_closures.c
+++ b/Zend/zend_closures.c
@@ -70,6 +70,61 @@ ZEND_METHOD(Closure, __invoke) /* {{{ */
}
/* }}} */
+/* {{{ proto mixed Closure::apply(object $to [, mixed $parameter] [, mixed $...] )
+ Call closure, binding to a given object */
+ZEND_METHOD(Closure, apply) /* {{{ */
+{
+ zval *zclosure, *newthis, *closure_result_ptr = NULL;
+ zend_closure *closure;
+ zend_fcall_info fci;
+ zend_fcall_info_cache fci_cache;
+
+ if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oo*",
&zclosure, zend_ce_closure, &newthis, &fci.params, &fci.param_count) == FAILURE) {
+ RETURN_NULL();
+ }
+
+ closure = (zend_closure *)zend_object_store_get_object(zclosure TSRMLS_CC);
+
+ if (closure->func.common.fn_flags & ZEND_ACC_STATIC) {
+ zend_error(E_WARNING, "Cannot bind an instance to a static closure");
+ RETURN_NULL();
+ }
+
+ if (closure->func.type == ZEND_INTERNAL_FUNCTION) {
+ /* verify that we aren't binding internal function to a wrong object */
+ if((closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0 &&
+ !instanceof_function(Z_OBJCE_P(newthis), closure->func.common.scope TSRMLS_CC)) {
+ zend_error(E_WARNING, "Cannot bind function %s::%s to object of class %s",
closure->func.common.scope->name, closure->func.common.function_name,
Z_OBJCE_P(newthis)->name);
+ RETURN_NULL();
+ }
+ }
+
+ fci.size = sizeof(fci);
+ fci.function_table = CG(function_table);
+ fci.object_ptr = newthis;
+ fci.function_name = zclosure;
+ fci.retval_ptr_ptr = &closure_result_ptr;
+ fci.no_separation = 1;
+ fci.symbol_table = NULL;
+
+ fci_cache.initialized = 1;
+ fci_cache.function_handler = &closure->func;
+ fci_cache.object_ptr = newthis;
+ fci_cache.calling_scope = fci_cache..called_scope = Z_OBJCE_P(newthis);
+
+ if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == FAILURE) {
+ RETVAL_FALSE;
+ } else if (closure_result_ptr) {
+ zval_ptr_dtor(&return_value);
+ *return_value_ptr = closure_result_ptr;
+ }
+
+ if (fci.param_count) {
+ efree(fci.params);
+ }
+}
+/* }}} */
+
/* {{{ proto Closure Closure::bind(Closure $old, object $to [, mixed $scope = "static" ]
)
Create a closure from another one and bind to another object and scope */
ZEND_METHOD(Closure, bind)
@@ -399,10 +454,16 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_bind, 0, 0, 2)
ZEND_ARG_INFO(0, newscope)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_INFO_EX(arginfo_closure_apply, 0, 0, 1)
+ ZEND_ARG_INFO(0, newthis)
+ ZEND_ARG_VARIADIC_INFO(0, parameters)
+ZEND_END_ARG_INFO()
+
static const zend_function_entry closure_functions[] = {
ZEND_ME(Closure, __construct, NULL, ZEND_ACC_PRIVATE)
ZEND_ME(Closure, bind, arginfo_closure_bind, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_MALIAS(Closure, bindTo, bind, arginfo_closure_bindto, ZEND_ACC_PUBLIC)
+ ZEND_ME(Closure, apply, arginfo_closure_apply, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
diff --git a/Zend/zend_execute_API.c b/Zend/zend_execute_API.c
index 8c2deb7...212af7b 100644
--- a/Zend/zend_execute_API.c
+++ b/Zend/zend_execute_API.c
@@ -653,7 +653,7 @@ int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TS
zend_error(E_ERROR, "Corrupted fcall_info provided to zend_call_function()");
break;
}
-
+
/* Initialize execute_data */
if (EG(current_execute_data)) {
execute_data = *EG(current_execute_data);