Skip to content

gen_stub: Intern the parameter name string for named arguments in internal attributes #14595

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

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ public function generateCode(string $invocation, string $nameSuffix, array $allC
$code .= $value->initializeZval($zvalName);
$code .= "\tZVAL_COPY_VALUE(&attribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].value, &$zvalName);\n";
if ($arg->name) {
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = zend_string_init(\"{$arg->name->name}\", sizeof(\"{$arg->name->name}\") - 1, 1);\n";
$code .= "\tattribute_{$escapedAttributeName}_{$nameSuffix}->args[$i].name = zend_string_init_interned(\"{$arg->name->name}\", sizeof(\"{$arg->name->name}\") - 1, 1);\n";
}
}
return $code;
Expand Down
22 changes: 22 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ static zend_class_entry *zend_test_attribute;
static zend_class_entry *zend_test_repeatable_attribute;
static zend_class_entry *zend_test_parameter_attribute;
static zend_class_entry *zend_test_property_attribute;
static zend_class_entry *zend_test_attribute_with_arguments;
static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_class_with_property_attribute;
Expand Down Expand Up @@ -575,6 +576,11 @@ static ZEND_FUNCTION(zend_test_parameter_with_attribute)
RETURN_LONG(1);
}

static ZEND_FUNCTION(zend_test_attribute_with_named_argument)
{
ZEND_PARSE_PARAMETERS_NONE();
}

#ifdef ZEND_CHECK_STACK_LIMIT
static ZEND_FUNCTION(zend_test_zend_call_stack_get)
{
Expand Down Expand Up @@ -989,6 +995,19 @@ static ZEND_METHOD(ZendTestPropertyAttribute, __construct)
ZVAL_STR_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), parameter);
}

static ZEND_METHOD(ZendTestAttributeWithArguments, __construct)
{
zval *arg;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(arg)
ZEND_PARSE_PARAMETERS_END();

zend_string *property_name = zend_string_init("arg", strlen("arg"), 0);
zend_update_property_ex(zend_test_attribute_with_arguments, Z_OBJ_P(ZEND_THIS), property_name, arg);
zend_string_release(property_name);
}

static ZEND_METHOD(ZendTestClassWithMethodWithParameterAttribute, no_override)
{
zend_string *parameter;
Expand Down Expand Up @@ -1217,6 +1236,9 @@ PHP_MINIT_FUNCTION(zend_test)
zend_test_property_attribute = register_class_ZendTestPropertyAttribute();
zend_mark_internal_attribute(zend_test_property_attribute);

zend_test_attribute_with_arguments = register_class_ZendTestAttributeWithArguments();
zend_mark_internal_attribute(zend_test_attribute_with_arguments);

zend_test_class_with_method_with_parameter_attribute = register_class_ZendTestClassWithMethodWithParameterAttribute();
zend_test_child_class_with_method_with_parameter_attribute = register_class_ZendTestChildClassWithMethodWithParameterAttribute(zend_test_class_with_method_with_parameter_attribute);

Expand Down
10 changes: 10 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ public function testMethod(): bool {}
final class ZendTestAttribute {
}

#[Attribute(Attribute::TARGET_ALL)]
final class ZendTestAttributeWithArguments {
public readonly mixed $arg;

public function __construct(mixed $arg) {}
}

#[Attribute(Attribute::TARGET_ALL|Attribute::IS_REPEATABLE)]
final class ZendTestRepeatableAttribute {
}
Expand Down Expand Up @@ -243,6 +250,9 @@ function zend_test_parameter_with_attribute(
string $parameter
): int {}

#[ZendTestAttributeWithArguments(arg: "foo")]
function zend_test_attribute_with_named_argument(): void {}

function zend_get_current_func_name(): string {}

function zend_call_method(object|string $obj_or_class, string $method, mixed $arg1 = UNKNOWN, mixed $arg2 = UNKNOWN): mixed {}
Expand Down
53 changes: 52 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions ext/zend_test/tests/attribute-named-parameter.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Verify that attributes for internal functions correctly support named arguments.
--EXTENSIONS--
zend_test
--FILE--
<?php

$reflection = new ReflectionFunction("zend_test_attribute_with_named_argument");
$attribute = $reflection->getAttributes()[0];
var_dump($attribute->getArguments());
var_dump($attribute->newInstance());

?>
--EXPECTF--
array(1) {
["arg"]=>
string(3) "foo"
}
object(ZendTestAttributeWithArguments)#3 (1) {
["arg"]=>
string(3) "foo"
}
Loading