Skip to content

Commit 43e9fd5

Browse files
committed
zend_API: Add Z_PARAM_ENUM_NAME()
It is becoming increasingly common to have enum parameters on internal functions. Manually turning the singleton enum object into the case name is verbose, especially for optional parameters. Add a new `Z_PARAM_ENUM_NAME()` helper to directly retrieve the case name.
1 parent fb27990 commit 43e9fd5

7 files changed

Lines changed: 34 additions & 34 deletions

File tree

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ PHP 8.6 INTERNALS UPGRADE NOTES
5454
ZEND_ACC_USER_ARG_INFO flag was set.
5555
. Added zend_ast_call_get_args() to fetch the argument node from any call
5656
node.
57+
. Added Z_PARAM_ENUM_NAME().
5758

5859
========================
5960
2. Build system changes

Zend/zend_API.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,13 @@ ZEND_API ZEND_COLD void zend_class_redeclaration_error_ex(int type, zend_string
20092009
#define Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(dest_obj, _ce, dest_long, is_null) \
20102010
Z_PARAM_OBJ_OF_CLASS_OR_LONG_EX(dest_obj, _ce, dest_long, is_null, 1)
20112011

2012+
#define Z_PARAM_ENUM_NAME(dest, _ce) \
2013+
{ \
2014+
zend_object *_tmp = NULL; \
2015+
Z_PARAM_OBJ_OF_CLASS(_tmp, _ce); \
2016+
dest = Z_STR_P(zend_enum_fetch_case_name(_tmp)); \
2017+
}
2018+
20122019
/* old "p" */
20132020
#define Z_PARAM_PATH_EX(dest, dest_len, check_null, deref) \
20142021
Z_PARAM_PROLOGUE(deref, 0); \

ext/dom/element.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,14 +1680,14 @@ PHP_METHOD(DOMElement, insertAdjacentElement)
16801680

16811681
PHP_METHOD(Dom_Element, insertAdjacentElement)
16821682
{
1683-
zval *element_zval, *where_zv;
1683+
zval *element_zval;
1684+
const zend_string *where;
16841685

16851686
ZEND_PARSE_PARAMETERS_START(2, 2)
1686-
Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1687+
Z_PARAM_ENUM_NAME(where, dom_adjacent_position_class_entry)
16871688
Z_PARAM_OBJECT_OF_CLASS(element_zval, dom_modern_element_class_entry)
16881689
ZEND_PARSE_PARAMETERS_END();
16891690

1690-
const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
16911691
dom_element_insert_adjacent_element(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, element_zval);
16921692
}
16931693
/* }}} end DOMElement::insertAdjacentElement */
@@ -1728,23 +1728,22 @@ PHP_METHOD(DOMElement, insertAdjacentText)
17281728

17291729
PHP_METHOD(Dom_Element, insertAdjacentText)
17301730
{
1731-
zval *where_zv;
1731+
const zend_string *where;
17321732
zend_string *data;
17331733

17341734
ZEND_PARSE_PARAMETERS_START(2, 2)
1735-
Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1735+
Z_PARAM_ENUM_NAME(where, dom_adjacent_position_class_entry)
17361736
Z_PARAM_STR(data)
17371737
ZEND_PARSE_PARAMETERS_END();
17381738

1739-
const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
17401739
dom_element_insert_adjacent_text(INTERNAL_FUNCTION_PARAM_PASSTHRU, where, data);
17411740
}
17421741
/* }}} end DOMElement::insertAdjacentText */
17431742

17441743
/* https://html.spec.whatwg.org/#dom-element-insertadjacenthtml */
17451744
PHP_METHOD(Dom_Element, insertAdjacentHTML)
17461745
{
1747-
zval *where_zv;
1746+
const zend_string *where;
17481747
zend_string *string;
17491748

17501749
dom_object *this_intern;
@@ -1754,14 +1753,12 @@ PHP_METHOD(Dom_Element, insertAdjacentHTML)
17541753
bool created_context = false;
17551754

17561755
ZEND_PARSE_PARAMETERS_START(2, 2)
1757-
Z_PARAM_OBJECT_OF_CLASS(where_zv, dom_adjacent_position_class_entry)
1756+
Z_PARAM_ENUM_NAME(where, dom_adjacent_position_class_entry)
17581757
Z_PARAM_STR(string)
17591758
ZEND_PARSE_PARAMETERS_END();
17601759

17611760
DOM_GET_THIS_OBJ(thisp, id, xmlNodePtr, this_intern);
17621761

1763-
const zend_string *where = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(where_zv)));
1764-
17651762
/* 1. We don't do injection sinks. */
17661763

17671764
/* 2. Let context be NULL */

ext/pcntl/pcntl.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,10 +1823,9 @@ PHP_FUNCTION(pcntl_getcpu)
18231823
#endif
18241824

18251825
#if defined(HAVE_PTHREAD_SET_QOS_CLASS_SELF_NP)
1826-
static qos_class_t qos_zval_to_lval(const zval *qos_obj)
1826+
static qos_class_t qos_zval_to_lval(const zend_string *entry)
18271827
{
18281828
qos_class_t qos_class = QOS_CLASS_DEFAULT;
1829-
zend_string *entry = Z_STR_P(zend_enum_fetch_case_name(Z_OBJ_P(qos_obj)));
18301829

18311830
if (zend_string_equals_literal(entry, "UserInteractive")) {
18321831
qos_class = QOS_CLASS_USER_INTERACTIVE;
@@ -1886,13 +1885,13 @@ PHP_FUNCTION(pcntl_getqos_class)
18861885

18871886
PHP_FUNCTION(pcntl_setqos_class)
18881887
{
1889-
zval *qos_obj;
1888+
zend_string *qos;
18901889

18911890
ZEND_PARSE_PARAMETERS_START(1, 1)
1892-
Z_PARAM_OBJECT_OF_CLASS(qos_obj, QosClass_ce)
1891+
Z_PARAM_ENUM_NAME(qos, QosClass_ce)
18931892
ZEND_PARSE_PARAMETERS_END();
18941893

1895-
qos_class_t qos_class = qos_zval_to_lval(qos_obj);
1894+
qos_class_t qos_class = qos_zval_to_lval(qos);
18961895

18971896
if (UNEXPECTED(pthread_set_qos_class_self_np((qos_class_t)qos_class, 0) != 0))
18981897
{

ext/random/randomizer.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ PHP_METHOD(Random_Randomizer, getFloat)
131131
{
132132
php_random_randomizer *randomizer = Z_RANDOM_RANDOMIZER_P(ZEND_THIS);
133133
double min, max;
134-
zend_object *bounds = NULL;
134+
zend_string *bounds = NULL;
135135
int bounds_type = 'C' + sizeof("ClosedOpen") - 1;
136136

137137
ZEND_PARSE_PARAMETERS_START(2, 3)
138138
Z_PARAM_DOUBLE(min)
139139
Z_PARAM_DOUBLE(max)
140140
Z_PARAM_OPTIONAL
141-
Z_PARAM_OBJ_OF_CLASS(bounds, random_ce_Random_IntervalBoundary);
141+
Z_PARAM_ENUM_NAME(bounds, random_ce_Random_IntervalBoundary);
142142
ZEND_PARSE_PARAMETERS_END();
143143

144144
if (!zend_finite(min)) {
@@ -152,10 +152,7 @@ PHP_METHOD(Random_Randomizer, getFloat)
152152
}
153153

154154
if (bounds) {
155-
zval *case_name = zend_enum_fetch_case_name(bounds);
156-
zend_string *bounds_name = Z_STR_P(case_name);
157-
158-
bounds_type = ZSTR_VAL(bounds_name)[0] + ZSTR_LEN(bounds_name);
155+
bounds_type = ZSTR_VAL(bounds)[0] + ZSTR_LEN(bounds);
159156
}
160157

161158
switch (bounds_type) {

ext/reflection/php_reflection.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6547,16 +6547,16 @@ ZEND_METHOD(ReflectionProperty, hasHook)
65476547

65486548
reflection_object *intern;
65496549
property_reference *ref;
6550-
zend_object *type;
6550+
const zend_string *type;
65516551

65526552
ZEND_PARSE_PARAMETERS_START(1, 1)
6553-
Z_PARAM_OBJ_OF_CLASS(type, reflection_property_hook_type_ptr)
6553+
Z_PARAM_ENUM_NAME(type, reflection_property_hook_type_ptr)
65546554
ZEND_PARSE_PARAMETERS_END();
65556555

65566556
GET_REFLECTION_OBJECT_PTR(ref);
65576557

65586558
zend_property_hook_kind kind;
6559-
if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) {
6559+
if (zend_string_equals_literal(type, "Get")) {
65606560
kind = ZEND_PROPERTY_HOOK_GET;
65616561
} else {
65626562
kind = ZEND_PROPERTY_HOOK_SET;
@@ -6569,10 +6569,10 @@ ZEND_METHOD(ReflectionProperty, getHook)
65696569
{
65706570
reflection_object *intern;
65716571
property_reference *ref;
6572-
zend_object *type;
6572+
const zend_string *type;
65736573

65746574
ZEND_PARSE_PARAMETERS_START(1, 1)
6575-
Z_PARAM_OBJ_OF_CLASS(type, reflection_property_hook_type_ptr)
6575+
Z_PARAM_ENUM_NAME(type, reflection_property_hook_type_ptr)
65766576
ZEND_PARSE_PARAMETERS_END();
65776577

65786578
GET_REFLECTION_OBJECT_PTR(ref);
@@ -6583,7 +6583,7 @@ ZEND_METHOD(ReflectionProperty, getHook)
65836583
}
65846584

65856585
zend_function *hook;
6586-
if (zend_string_equals_literal(Z_STR_P(zend_enum_fetch_case_name(type)), "Get")) {
6586+
if (zend_string_equals_literal(type, "Get")) {
65876587
hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_GET];
65886588
} else {
65896589
hook = ref->prop->hooks[ZEND_PROPERTY_HOOK_SET];

ext/uri/php_uri.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ static void throw_cannot_recompose_uri_to_string(php_uri_object *object)
678678
zend_throw_exception_ex(php_uri_ce_error, 0, "Cannot recompose %s to a string", ZSTR_VAL(object->std.ce->name));
679679
}
680680

681-
static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, php_uri_object *that_object, zend_object *comparison_mode)
681+
static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, php_uri_object *that_object, const zend_string *comparison_mode)
682682
{
683683
php_uri_object *this_object = Z_URI_OBJECT_P(ZEND_THIS);
684684
ZEND_ASSERT(this_object->uri != NULL);
@@ -693,8 +693,7 @@ static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, php_uri_object *that_object
693693

694694
bool exclude_fragment = true;
695695
if (comparison_mode) {
696-
zval *case_name = zend_enum_fetch_case_name(comparison_mode);
697-
exclude_fragment = zend_string_equals_literal(Z_STR_P(case_name), "ExcludeFragment");
696+
exclude_fragment = zend_string_equals_literal(comparison_mode, "ExcludeFragment");
698697
}
699698

700699
zend_string *this_str = this_object->parser->to_string(
@@ -721,12 +720,12 @@ static void uri_equals(INTERNAL_FUNCTION_PARAMETERS, php_uri_object *that_object
721720
PHP_METHOD(Uri_Rfc3986_Uri, equals)
722721
{
723722
zend_object *that_object;
724-
zend_object *comparison_mode = NULL;
723+
const zend_string *comparison_mode = NULL;
725724

726725
ZEND_PARSE_PARAMETERS_START(1, 2)
727726
Z_PARAM_OBJ_OF_CLASS(that_object, php_uri_ce_rfc3986_uri)
728727
Z_PARAM_OPTIONAL
729-
Z_PARAM_OBJ_OF_CLASS(comparison_mode, php_uri_ce_comparison_mode)
728+
Z_PARAM_ENUM_NAME(comparison_mode, php_uri_ce_comparison_mode)
730729
ZEND_PARSE_PARAMETERS_END();
731730

732731
uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_uri_object_from_obj(that_object), comparison_mode);
@@ -917,12 +916,12 @@ PHP_METHOD(Uri_WhatWg_Url, getFragment)
917916
PHP_METHOD(Uri_WhatWg_Url, equals)
918917
{
919918
zend_object *that_object;
920-
zend_object *comparison_mode = NULL;
919+
const zend_string *comparison_mode = NULL;
921920

922921
ZEND_PARSE_PARAMETERS_START(1, 2)
923922
Z_PARAM_OBJ_OF_CLASS(that_object, php_uri_ce_whatwg_url)
924923
Z_PARAM_OPTIONAL
925-
Z_PARAM_OBJ_OF_CLASS(comparison_mode, php_uri_ce_comparison_mode)
924+
Z_PARAM_ENUM_NAME(comparison_mode, php_uri_ce_comparison_mode)
926925
ZEND_PARSE_PARAMETERS_END();
927926

928927
uri_equals(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_uri_object_from_obj(that_object), comparison_mode);

0 commit comments

Comments
 (0)