Skip to content

Commit f21c4fb

Browse files
committed
1 parent cce0efd commit f21c4fb

10 files changed

+514
-1
lines changed

Zend/tests/composed_callable_001.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Composed callable using direct object methods.
3+
--FILE--
4+
<?php
5+
6+
$cb = new \ComposedCallable(strtolower(...));
7+
var_dump($cb("Hello"));
8+
$cb->prepend(fn($x) => "$x World");
9+
var_dump($cb("Hello"));
10+
$cb->append('strrev');
11+
var_dump($cb("Hello"));
12+
$cb->insert(ucfirst(...), 2);
13+
var_dump($cb("Hello"));
14+
--EXPECT--
15+
string(5) "hello"
16+
string(11) "hello world"
17+
string(11) "dlrow olleh"
18+
string(11) "dlrow olleH"

Zend/tests/composed_callable_002.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Composed callable using composition.
3+
--FILE--
4+
<?php
5+
6+
$cb1 = new \ComposedCallable([strtolower(...)]);
7+
$cb2 = $cb1 + strrev(...);
8+
var_dump($cb1("Hello World"));
9+
var_dump($cb2("Hello World"));
10+
--EXPECT--
11+
string(11) "hello world"
12+
string(11) "dlrow olleh"

Zend/tests/composed_callable_003.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--TEST--
2+
Composed callable using composition and auto-promotion
3+
--FILE--
4+
<?php
5+
6+
$cb = strtolower(...) + str_rot13(...) + (fn($x) => "<$x>");
7+
var_dump($cb("Hello World"));
8+
--EXPECT--
9+
string(13) "<uryyb jbeyq>"

Zend/zend_closures.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "zend.h"
2222
#include "zend_API.h"
2323
#include "zend_closures.h"
24+
#include "zend_composed_callable.h"
2425
#include "zend_exceptions.h"
2526
#include "zend_interfaces.h"
2627
#include "zend_objects.h"
@@ -705,6 +706,14 @@ ZEND_COLD ZEND_METHOD(Closure, __construct)
705706
}
706707
/* }}} */
707708

709+
static zend_result zend_closure_do_operation(uint8_t opcode, zval *result, zval *op1, zval *op2) {
710+
if (opcode != ZEND_ADD) {
711+
return FAILURE;
712+
}
713+
714+
return zend_composed_callable_new_from_pair(result, op1, op2);
715+
}
716+
708717
void zend_register_closure_ce(void) /* {{{ */
709718
{
710719
zend_ce_closure = register_class_Closure();
@@ -720,6 +729,7 @@ void zend_register_closure_ce(void) /* {{{ */
720729
closure_handlers.get_debug_info = zend_closure_get_debug_info;
721730
closure_handlers.get_closure = zend_closure_get_closure;
722731
closure_handlers.get_gc = zend_closure_get_gc;
732+
closure_handlers.do_operation = zend_closure_do_operation;
723733
}
724734
/* }}} */
725735

0 commit comments

Comments
 (0)