Skip to content

Let closure created from magic method accept named parameters #11364

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 1 commit into from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Jun 3, 2023

Closes GH-11348.

Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

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

I don't know this code that well but this looks correct to me, thank you!

@@ -841,7 +852,7 @@ void zend_closure_from_frame(zval *return_value, zend_execute_data *call) { /* {

memset(&trampoline, 0, sizeof(zend_internal_function));
trampoline.type = ZEND_INTERNAL_FUNCTION;
trampoline.fn_flags = mptr->common.fn_flags & ZEND_ACC_STATIC;
trampoline.fn_flags = mptr->common.fn_flags & (ZEND_ACC_STATIC | ZEND_ACC_VARIADIC);
Copy link
Member

Choose a reason for hiding this comment

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

I just noticed something else, above there is a check for the __invoke magic method that compares a C string, but we have a known interned string for this ZEND_STR_MAGIC_INVOKE, wondering if it makes more sense to use this with zend_string_equals() as it will use a pointer comparison first.

But that is an aside.

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably, feel free to open a PR and check if there's more such cases ;)

Comment on lines +297 to +308
if (EX_CALL_INFO() & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
zend_string *name;
zval *named_param_zval;
array_init_size(&fci.params[1], ZEND_NUM_ARGS() + zend_hash_num_elements(EX(extra_named_params)));
/* Avoid conversion from packed to mixed later. */
zend_hash_real_init_mixed(Z_ARRVAL(fci.params[1]));
zend_copy_parameters_array(ZEND_NUM_ARGS(), &fci.params[1]);
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EX(extra_named_params), name, named_param_zval) {
Z_TRY_ADDREF_P(named_param_zval);
zend_hash_add_new(Z_ARRVAL(fci.params[1]), name, named_param_zval);
} ZEND_HASH_FOREACH_END();
} else if (ZEND_NUM_ARGS()) {
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering why we couldn't use the *named_params field of the FCI struct, before realising that we need to merge all the parameters into a single array to pass as an argument to the magic method.

Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

This looks right.

@nielsdos nielsdos closed this in 61e1f8a Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Closure created from magic method does not accept named arguments
4 participants