Skip to content

POC https://wiki.php.net/rfc/function-composition #18800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Zend/tests/composed_callable_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Composed callable using direct object methods.
--FILE--
<?php

$cb = new \ComposedCallable(strtolower(...));
var_dump($cb("Hello"));
$cb->prepend(fn($x) => "$x World");
var_dump($cb("Hello"));
$cb->append('strrev');
var_dump($cb("Hello"));
$cb->insert(ucfirst(...), 2);
var_dump($cb("Hello"));
--EXPECT--
string(5) "hello"
string(11) "hello world"
string(11) "dlrow olleh"
string(11) "dlrow olleH"
12 changes: 12 additions & 0 deletions Zend/tests/composed_callable_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Composed callable using composition.
--FILE--
<?php

$cb1 = new \ComposedCallable([strtolower(...)]);
$cb2 = $cb1 + strrev(...);
var_dump($cb1("Hello World"));
var_dump($cb2("Hello World"));
--EXPECT--
string(11) "hello world"
string(11) "dlrow olleh"
9 changes: 9 additions & 0 deletions Zend/tests/composed_callable_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--TEST--
Composed callable using composition and auto-promotion
--FILE--
<?php

$cb = strtolower(...) + str_rot13(...) + (fn($x) => "<$x>");
var_dump($cb("Hello World"));
--EXPECT--
string(13) "<uryyb jbeyq>"
10 changes: 10 additions & 0 deletions Zend/zend_closures.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "zend.h"
#include "zend_API.h"
#include "zend_closures.h"
#include "zend_composed_callable.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "zend_objects.h"
Expand Down Expand Up @@ -705,6 +706,14 @@ ZEND_COLD ZEND_METHOD(Closure, __construct)
}
/* }}} */

static zend_result zend_closure_do_operation(uint8_t opcode, zval *result, zval *op1, zval *op2) {
if (opcode != ZEND_ADD) {
return FAILURE;
}

return zend_composed_callable_new_from_pair(result, op1, op2);
}

void zend_register_closure_ce(void) /* {{{ */
{
zend_ce_closure = register_class_Closure();
Expand All @@ -720,6 +729,7 @@ void zend_register_closure_ce(void) /* {{{ */
closure_handlers.get_debug_info = zend_closure_get_debug_info;
closure_handlers.get_closure = zend_closure_get_closure;
closure_handlers.get_gc = zend_closure_get_gc;
closure_handlers.do_operation = zend_closure_do_operation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! That's not at all how I would have thought to do it.

If I follow, this is implemented as an operator override on closures, rather than special casing the operator itself, yes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. When the engine encounts a binary op (like addition) involving one of more objects, it checks them for do_operation overloads (with priority to the lhs) and asks the object to perform the addition itself. The first one which reports SUCCESS and populates result wins. So if you added (gmp) + (closure), then GMP would try to do the addition, have no idea what to make of a closure, and fail, and the closure would do the same (not knowing what to do with a gmp), and you'd wind up with an error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, an explicit engine level overload of the binary-op helper would allow us to cover situations like this one.

e.g.

diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c
index 140734d1b96..b9f9629c616 100644
--- a/Zend/zend_operators.c
+++ b/Zend/zend_operators.c
@@ -1128,6 +1128,11 @@ static zend_never_inline zend_result ZEND_FASTCALL add_function_slow(zval *resul
 
        ZEND_TRY_BINARY_OBJECT_OPERATION(ZEND_ADD);
 
+       if (((Z_TYPE_P(op1) == IS_OBJECT) || (Z_TYPE_P(op2) == IS_OBJECT)) &&
+         (zend_composed_callable_new_from_pair(result, op1, op2) == SUCCESS)) {
+               return SUCCESS;
+       }
+
        zval op1_copy, op2_copy;
        if (UNEXPECTED(zendi_try_convert_scalar_to_number(op1, &op1_copy) == FAILURE)
                        || UNEXPECTED(zendi_try_convert_scalar_to_number(op2, &op2_copy) == FAILURE)) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heck, could even cover the $cb = 'strtolower' + 'strrev'; kind of cases that way, if you wanted to go for maximum magic (I don't).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. When the engine encounts a binary op (like addition) involving one of more objects, it checks them for do_operation overloads (with priority to the lhs) and asks the object to perform the addition itself. The first one which reports SUCCESS and populates result wins. So if you added (gmp) + (closure), then GMP would try to do the addition, have no idea what to make of a closure, and fail, and the closure would do the same (not knowing what to do with a gmp), and you'd wind up with an error.

Our operator overloading mechanism is far from robust, and GMP used to throw its own exception and not delegate properly to the fallback.
Moreover, behaviour with other values might still be somewhat broken, this really needs tests with every possible scalar and null as the LHS and RHS of +

}
/* }}} */

Expand Down
Loading
Loading