Skip to content

Commit c1345e8

Browse files
committed
Improve PFA func name
1 parent bbba27e commit c1345e8

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Partial application function name
3+
--FILE--
4+
<?php
5+
6+
function g($a) {}
7+
8+
function f() {
9+
var_dump((new ReflectionFunction(g(?)))->getName());
10+
}
11+
12+
f();
13+
14+
var_dump((new ReflectionFunction(g(?)))->getName());
15+
16+
?>
17+
--EXPECTF--
18+
string(19) "{closure:pfa:f():6}"
19+
string(94) "{closure:pfa:%sfunction_name.php:11}"

Zend/zend_partial.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,47 @@ static zend_ast *zp_param_attributes_to_ast(zend_function *function,
443443
return attributes_ast;
444444
}
445445

446+
static zend_string *zp_pfa_name(const zend_op_array *declaring_op_array,
447+
const zend_op *declaring_opline)
448+
{
449+
zend_string *filename = declaring_op_array->filename;
450+
uint32_t start_lineno = declaring_opline->lineno;
451+
452+
zend_string *class = zend_empty_string;
453+
zend_string *separator = zend_empty_string;
454+
zend_string *function = filename;
455+
const char *parens = "";
456+
457+
if (declaring_op_array->function_name) {
458+
if (declaring_op_array->fn_flags & ZEND_ACC_CLOSURE) {
459+
/* If the parent function is a closure, don't redundantly
460+
* add the classname and parentheses.
461+
*/
462+
function = declaring_op_array->function_name;
463+
} else {
464+
function = declaring_op_array->function_name;
465+
parens = "()";
466+
467+
if (declaring_op_array->scope && declaring_op_array->scope->name) {
468+
class = declaring_op_array->scope->name;
469+
separator = ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM);
470+
}
471+
}
472+
}
473+
474+
zend_string *name = zend_strpprintf_unchecked(
475+
0,
476+
"{closure:pfa:%S%S%S%s:%" PRIu32 "}",
477+
class,
478+
separator,
479+
function,
480+
parens,
481+
start_lineno
482+
);
483+
484+
return name;
485+
}
486+
446487
zend_ast *zp_compile_forwarding_call(
447488
zval *this_ptr, zend_function *function,
448489
uint32_t argc, zval *argv, zend_array *extra_named_params,
@@ -874,8 +915,10 @@ zend_op_array *zp_compile(zval *this_ptr, zend_function *function,
874915
}
875916
#endif
876917

918+
zend_string *pfa_name = zp_pfa_name(declaring_op_array, declaring_opline);
919+
877920
op_array = zend_accel_compile_pfa(closure_ast, declaring_op_array,
878-
declaring_opline, function);
921+
declaring_opline, function, pfa_name);
879922

880923
zend_ast_destroy(closure_ast);
881924

ext/opcache/ZendAccelerator.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,8 @@ zend_op_array *zend_accel_pfa_cache_get(const zend_op_array *declaring_op_array,
20712071
zend_op_array *zend_accel_compile_pfa(zend_ast *ast,
20722072
const zend_op_array *declaring_op_array,
20732073
const zend_op *declaring_opline,
2074-
const zend_function *called_function)
2074+
const zend_function *called_function,
2075+
zend_string *pfa_func_name)
20752076
{
20762077
// TODO: file cache support
20772078

@@ -2117,6 +2118,9 @@ zend_op_array *zend_accel_compile_pfa(zend_ast *ast,
21172118
return NULL;
21182119
}
21192120

2121+
zend_string_release(op_array->dynamic_func_defs[0]->function_name);
2122+
op_array->dynamic_func_defs[0]->function_name = pfa_func_name;
2123+
21202124
/* Cache op_array only if the declaring op_array and the called function
21212125
* are cached */
21222126
if (!ZCG(accelerator_enabled)

ext/opcache/ZendAccelerator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ zend_op_array *zend_accel_pfa_cache_get(const zend_op_array *declaring_op_array,
343343
zend_op_array *zend_accel_compile_pfa(zend_ast *ast,
344344
const zend_op_array *declaring_op_array,
345345
const zend_op *declaring_opline,
346-
const zend_function *called_function);
346+
const zend_function *called_function,
347+
zend_string *pfa_func_name);
347348

348349
END_EXTERN_C()
349350

0 commit comments

Comments
 (0)