Skip to content

Partial function application #5547

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

Closed
wants to merge 5 commits into from
Closed
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
26 changes: 26 additions & 0 deletions Zend/tests/partial_function/inline.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Inlined partials.
--FILE--
<?php

function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}

$arr = [1, 2, 3];

function mul(float $x, float $y) {
return $x * $y;
}

$result = array_map(mul(2, ?), $arr);

print_r($result);

--EXPECTF--
Array
(
[0] => 2
[1] => 4
[2] => 6
)
25 changes: 25 additions & 0 deletions Zend/tests/partial_function/methods.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Methods.
--FILE--
<?php

class Action {
function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}
}

$a = new Action();

$p1 = $a->volume(3, 5, ?);
$p2 = $a->volume(3, ?, 9);
$p3 = $a->volume(?, 5, 9);

print $p1(9);
print $p2(5);
print $p3(3);

--EXPECTF--
135
135
135
21 changes: 21 additions & 0 deletions Zend/tests/partial_function/multi-parameter-full.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Multi-parameter function to single-parameter function.
--FILE--
<?php

function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}

$p1 = volume(3, 5, ?);
$p2 = volume(3, ?, 9);
$p3 = volume(?, 5, 9);

print $p1(9);
print $p2(5);
print $p3(3);

--EXPECTF--
135
135
135
21 changes: 21 additions & 0 deletions Zend/tests/partial_function/multi-parameter.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Multi-parameter function to multi-parameter function.
--FILE--
<?php

function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}

$p1 = volume(3, ?, ?);
$p2 = volume(?, ?, 9);
$p3 = volume(?, 5, ?);

print $p1(5, 9);
print $p2(3, 5);
print $p3(3, 9);

--EXPECTF--
135
135
135
16 changes: 16 additions & 0 deletions Zend/tests/partial_function/nested.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Nested partials.
--FILE--
<?php

function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}

$p1 = volume(3, ?, ?);
$p2 = $p1(?, 9);

print $p1(5);

--EXPECTF--
135
15 changes: 15 additions & 0 deletions Zend/tests/partial_function/single-paramater.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Single parameter function.
--FILE--
<?php

function test1(int $a) {
return $a + 1;
}

$p = test1(?);

print p(3);

--EXPECTF--
4
24 changes: 24 additions & 0 deletions Zend/tests/partial_function/type-check.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Ensure reflected types inherit.
--FILE--
<?php

function volume(int $x, int $y, int $z): int {
return $x * $y * $z;
}

$p1 = volume(3, 5, ?);

$r = new ReflectionFunction($p1);

print $r->getNumberOfParameters();

$params = $r->getParameters();
print $params[0]->getType();

print $r->getReturnType();

--EXPECTF--
1
int
int
1 change: 1 addition & 0 deletions Zend/zend_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ enum _zend_ast_kind {
ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
ZEND_AST_TYPE,
ZEND_AST_CONSTANT_CLASS,
ZEND_AST_PARTIAL_PLACEHOLDER,

/* 1 child node */
ZEND_AST_VAR = 1 << ZEND_AST_NUM_CHILDREN_SHIFT,
Expand Down
9 changes: 9 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4019,6 +4019,15 @@ void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{{ */

znode name_node;

// If this is a partial function call, convert it to a special partial opcode.
zend_ast_list *arglist = zend_ast_get_list(args_ast);
for (int i; i < arglist->children; ++i) {
if (arglist->child[i]->kind == ZEND_AST_PARTIAL_PLACEHOLDER) {
zend_emit_op(NULL, ZEND_DO_PARTIAL_FCALL, NULL, NULL);
return;
}
}

if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
zend_compile_expr(&name_node, name_ast);
zend_compile_dynamic_call(result, &name_node, args_ast);
Expand Down
1 change: 1 addition & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ non_empty_argument_list:
argument:
expr { $$ = $1; }
| T_ELLIPSIS expr { $$ = zend_ast_create(ZEND_AST_UNPACK, $2); }
| '?' { $$ = zend_ast_create(ZEND_AST_PARTIAL_PLACEHOLDER); }
;

global_var_list:
Expand Down
31 changes: 31 additions & 0 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -4005,6 +4005,37 @@ ZEND_VM_C_LABEL(fcall_by_name_end):
ZEND_VM_CONTINUE();
}

ZEND_VM_HANDLER(195, ZEND_DO_PARTIAL_FCALL, ANY, ANY, SPEC(RETVAL))
{
// Unclear if this is needed.
USE_OPLINE

// Extract type information from the function.
USE_OPLINE
zend_execute_data *call = EX(call);
zend_function *fbc = call->func;
zval *ret;

SAVE_OPLINE();
EX(call) = call->prev_execute_data;

// Build a list of opcodes for "call the function"
// with some literal values and some parameters.


// Create a closure out of the information gathered above.
zend_create_closure(EX_VAR(opline->result.var), func, EX(func)->op_array.scope, called_scope, object);

ZEND_VM_NEXT_OPCODE();

/*
USE_OPLINE

ZEND_VM_SET_OPCODE(opline + 1);
ZEND_VM_CONTINUE();
*/
}

ZEND_VM_HOT_HANDLER(60, ZEND_DO_FCALL, ANY, ANY, SPEC(RETVAL))
{
USE_OPLINE
Expand Down
Loading