Skip to content

Commit 9225cb4

Browse files
Make zend_register_*_constant() functions return pointers, use them (#19029)
Have each of the specialized methods for registering a constant return a pointer to the registered constant the same way that the generic `zend_register_constant()` function does, and use those in the generated arginfo files to avoid needing to search for a constant that was just registered in order to add attributes to it.
1 parent b0aaa31 commit 9225cb4

16 files changed

+89
-103
lines changed

UPGRADING.INTERNALS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ PHP 8.5 INTERNALS UPGRADE NOTES
5050
did not match its actual behavior.
5151
. zend_register_constant() now returns a pointer to the added constant
5252
on success and NULL on failure instead of SUCCESS/FAILURE.
53+
The specialized registration methods that previously had void returns
54+
also return pointers to the added constants:
55+
* zend_register_bool_constant()
56+
* zend_register_null_constant()
57+
* zend_register_long_constant()
58+
* zend_register_double_constant()
59+
* zend_register_string_constant()
60+
* zend_register_stringl_constant()
5361

5462
========================
5563
2. Build system changes

Zend/zend_constants.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,62 +136,62 @@ void zend_register_standard_constants(void)
136136
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
137137
}
138138

139-
ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
139+
ZEND_API zend_constant *zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
140140
{
141141
zend_constant c;
142142

143143
ZVAL_NULL(&c.value);
144144
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
145145
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
146-
zend_register_constant(&c);
146+
return zend_register_constant(&c);
147147
}
148148

149-
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
149+
ZEND_API zend_constant *zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
150150
{
151151
zend_constant c;
152152

153153
ZVAL_BOOL(&c.value, bval);
154154
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
155155
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
156-
zend_register_constant(&c);
156+
return zend_register_constant(&c);
157157
}
158158

159-
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
159+
ZEND_API zend_constant *zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
160160
{
161161
zend_constant c;
162162

163163
ZVAL_LONG(&c.value, lval);
164164
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
165165
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
166-
zend_register_constant(&c);
166+
return zend_register_constant(&c);
167167
}
168168

169169

170-
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
170+
ZEND_API zend_constant *zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
171171
{
172172
zend_constant c;
173173

174174
ZVAL_DOUBLE(&c.value, dval);
175175
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
176176
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
177-
zend_register_constant(&c);
177+
return zend_register_constant(&c);
178178
}
179179

180180

181-
ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number)
181+
ZEND_API zend_constant *zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number)
182182
{
183183
zend_constant c;
184184

185185
ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
186186
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
187187
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
188-
zend_register_constant(&c);
188+
return zend_register_constant(&c);
189189
}
190190

191191

192-
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
192+
ZEND_API zend_constant *zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
193193
{
194-
zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
194+
return zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
195195
}
196196

197197
static zend_constant *zend_get_halt_offset_constant(const char *name, size_t name_len)

Zend/zend_constants.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name);
9191
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len);
9292
ZEND_API zval *zend_get_constant_ex(zend_string *name, zend_class_entry *scope, uint32_t flags);
9393
ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, zend_class_entry *scope, uint32_t flags);
94-
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number);
95-
ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number);
96-
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number);
97-
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
98-
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
99-
ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number);
94+
ZEND_API zend_constant *zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number);
95+
ZEND_API zend_constant *zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number);
96+
ZEND_API zend_constant *zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number);
97+
ZEND_API zend_constant *zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
98+
ZEND_API zend_constant *zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
99+
ZEND_API zend_constant *zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number);
100100
ZEND_API zend_constant *zend_register_constant(zend_constant *c);
101101
void zend_constant_add_attributes(zend_constant *c, HashTable *attributes);
102102
#ifdef ZTS

Zend/zend_constants_arginfo.h

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/gen_stub.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,24 +2781,36 @@ private function getGlobalConstDeclaration(EvaluatedValue $value): string
27812781
if ($this->isDeprecated) {
27822782
$flags .= " | CONST_DEPRECATED";
27832783
}
2784+
2785+
$code = "\t";
2786+
if ($this->attributes !== []) {
2787+
if ($this->phpVersionIdMinimumCompatibility === null || $this->phpVersionIdMinimumCompatibility >= PHP_85_VERSION_ID) {
2788+
// Registration only returns the constant since PHP 8.5, it's
2789+
// not worth the bloat to add two different registration blocks
2790+
// with conditions for the PHP version
2791+
$constVarName = 'const_' . $constName;
2792+
$code .= "zend_constant *$constVarName = ";
2793+
}
2794+
}
2795+
27842796
if ($value->type->isNull()) {
2785-
return "\tREGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
2797+
return $code . "REGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
27862798
}
27872799

27882800
if ($value->type->isBool()) {
2789-
return "\tREGISTER_BOOL_CONSTANT(\"$constName\", " . ($cExpr ?: ($constValue ? "true" : "false")) . ", $flags);\n";
2801+
return $code . "REGISTER_BOOL_CONSTANT(\"$constName\", " . ($cExpr ?: ($constValue ? "true" : "false")) . ", $flags);\n";
27902802
}
27912803

27922804
if ($value->type->isInt()) {
2793-
return "\tREGISTER_LONG_CONSTANT(\"$constName\", " . ($cExpr ?: (int) $constValue) . ", $flags);\n";
2805+
return $code . "REGISTER_LONG_CONSTANT(\"$constName\", " . ($cExpr ?: (int) $constValue) . ", $flags);\n";
27942806
}
27952807

27962808
if ($value->type->isFloat()) {
2797-
return "\tREGISTER_DOUBLE_CONSTANT(\"$constName\", " . ($cExpr ?: (float) $constValue) . ", $flags);\n";
2809+
return $code . "REGISTER_DOUBLE_CONSTANT(\"$constName\", " . ($cExpr ?: (float) $constValue) . ", $flags);\n";
27982810
}
27992811

28002812
if ($value->type->isString()) {
2801-
return "\tREGISTER_STRING_CONSTANT(\"$constName\", " . ($cExpr ?: '"' . addslashes($constValue) . '"') . ", $flags);\n";
2813+
return $code . "REGISTER_STRING_CONSTANT(\"$constName\", " . ($cExpr ?: '"' . addslashes($constValue) . '"') . ", $flags);\n";
28022814
}
28032815

28042816
throw new Exception("Unimplemented constant type");
@@ -5307,12 +5319,11 @@ function generateGlobalConstantAttributeInitialization(
53075319
$isConditional = false;
53085320
if ($phpVersionIdMinimumCompatibility !== null && $phpVersionIdMinimumCompatibility < PHP_85_VERSION_ID) {
53095321
$isConditional = true;
5310-
$phpVersionIdMinimumCompatibility = PHP_85_VERSION_ID;
53115322
}
53125323
$code = generateCodeWithConditions(
53135324
$constInfos,
53145325
"",
5315-
static function (ConstInfo $constInfo) use ($allConstInfos, $phpVersionIdMinimumCompatibility) {
5326+
static function (ConstInfo $constInfo) use ($allConstInfos, $isConditional) {
53165327
$code = "";
53175328

53185329
if ($constInfo->attributes === []) {
@@ -5321,13 +5332,18 @@ static function (ConstInfo $constInfo) use ($allConstInfos, $phpVersionIdMinimum
53215332
$constName = str_replace('\\', '\\\\', $constInfo->name->__toString());
53225333
$constVarName = 'const_' . $constName;
53235334

5324-
$code .= "\tzend_constant *$constVarName = zend_hash_str_find_ptr(EG(zend_constants), \"" . $constName . "\", sizeof(\"" . $constName . "\") - 1);\n";
5335+
// The entire attribute block will be conditional if PHP < 8.5 is
5336+
// supported, but also if PHP < 8.5 is supported we need to search
5337+
// for the constant; see GH-19029
5338+
if ($isConditional) {
5339+
$code .= "\tzend_constant *$constVarName = zend_hash_str_find_ptr(EG(zend_constants), \"" . $constName . "\", sizeof(\"" . $constName . "\") - 1);\n";
5340+
}
53255341
foreach ($constInfo->attributes as $key => $attribute) {
53265342
$code .= $attribute->generateCode(
53275343
"zend_add_global_constant_attribute($constVarName",
53285344
$constVarName . "_$key",
53295345
$allConstInfos,
5330-
$phpVersionIdMinimumCompatibility
5346+
PHP_85_VERSION_ID
53315347
);
53325348
}
53335349

ext/curl/curl_arginfo.h

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/date/php_date_arginfo.h

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/dom/php_dom_arginfo.h

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)