Skip to content

Make zend_register_*_constant() functions return pointers, use them #19029

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ PHP 8.5 INTERNALS UPGRADE NOTES
did not match its actual behavior.
. zend_register_constant() now returns a pointer to the added constant
on success and NULL on failure instead of SUCCESS/FAILURE.
The same is true for the specialized registration methods
* zend_register_bool_constant()
* zend_register_null_constant()
* zend_register_long_constant()
* zend_register_double_constant()
* zend_register_string_constant()
* zend_register_stringl_constant()

========================
2. Build system changes
Expand Down
24 changes: 12 additions & 12 deletions Zend/zend_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,62 +136,62 @@ void zend_register_standard_constants(void)
null_const = zend_hash_str_find_ptr(EG(zend_constants), "NULL", sizeof("NULL")-1);
}

ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
ZEND_API zend_constant *zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
{
zend_constant c;

ZVAL_NULL(&c.value);
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
zend_register_constant(&c);
return zend_register_constant(&c);
}

ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
ZEND_API zend_constant *zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
{
zend_constant c;

ZVAL_BOOL(&c.value, bval);
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
zend_register_constant(&c);
return zend_register_constant(&c);
}

ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
ZEND_API zend_constant *zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
{
zend_constant c;

ZVAL_LONG(&c.value, lval);
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
zend_register_constant(&c);
return zend_register_constant(&c);
}


ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
ZEND_API zend_constant *zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
{
zend_constant c;

ZVAL_DOUBLE(&c.value, dval);
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
zend_register_constant(&c);
return zend_register_constant(&c);
}


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)
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)
{
zend_constant c;

ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
zend_register_constant(&c);
return zend_register_constant(&c);
}


ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
ZEND_API zend_constant *zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number)
{
zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
return zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number);
}

static zend_constant *zend_get_halt_offset_constant(const char *name, size_t name_len)
Expand Down
12 changes: 6 additions & 6 deletions Zend/zend_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name);
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len);
ZEND_API zval *zend_get_constant_ex(zend_string *name, zend_class_entry *scope, uint32_t flags);
ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name, zend_class_entry *scope, uint32_t flags);
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number);
ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number);
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number);
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
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);
ZEND_API zend_constant *zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number);
ZEND_API zend_constant *zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number);
ZEND_API zend_constant *zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number);
ZEND_API zend_constant *zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
ZEND_API zend_constant *zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
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);
ZEND_API zend_constant *zend_register_constant(zend_constant *c);
void zend_constant_add_attributes(zend_constant *c, HashTable *attributes);
#ifdef ZTS
Expand Down
3 changes: 1 addition & 2 deletions Zend/zend_constants_arginfo.h

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

34 changes: 25 additions & 9 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2781,24 +2781,36 @@ private function getGlobalConstDeclaration(EvaluatedValue $value): string
if ($this->isDeprecated) {
$flags .= " | CONST_DEPRECATED";
}

$code = "\t";
if ($this->attributes !== []) {
if ($this->phpVersionIdMinimumCompatibility === null || $this->phpVersionIdMinimumCompatibility >= PHP_85_VERSION_ID) {
// Registration only returns the constant since PHP 8.5, it's
// not worth the bloat to add two different registration blocks
// with conditions for the PHP version
$constVarName = 'const_' . $constName;
$code .= "zend_constant *$constVarName = ";
}
}

if ($value->type->isNull()) {
return "\tREGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
return $code . "REGISTER_NULL_CONSTANT(\"$constName\", $flags);\n";
}

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

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

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

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

throw new Exception("Unimplemented constant type");
Expand Down Expand Up @@ -5307,12 +5319,11 @@ function generateGlobalConstantAttributeInitialization(
$isConditional = false;
if ($phpVersionIdMinimumCompatibility !== null && $phpVersionIdMinimumCompatibility < PHP_85_VERSION_ID) {
$isConditional = true;
$phpVersionIdMinimumCompatibility = PHP_85_VERSION_ID;
}
$code = generateCodeWithConditions(
$constInfos,
"",
static function (ConstInfo $constInfo) use ($allConstInfos, $phpVersionIdMinimumCompatibility) {
static function (ConstInfo $constInfo) use ($allConstInfos, $isConditional) {
$code = "";

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

$code .= "\tzend_constant *$constVarName = zend_hash_str_find_ptr(EG(zend_constants), \"" . $constName . "\", sizeof(\"" . $constName . "\") - 1);\n";
// The entire attribute block will be conditional if PHP < 8.5 is
// supported, but also if PHP < 8.5 is supported we need to search
// for the constant; see GH-19029
if ($isConditional) {
$code .= "\tzend_constant *$constVarName = zend_hash_str_find_ptr(EG(zend_constants), \"" . $constName . "\", sizeof(\"" . $constName . "\") - 1);\n";
}
foreach ($constInfo->attributes as $key => $attribute) {
$code .= $attribute->generateCode(
"zend_add_global_constant_attribute($constVarName",
$constVarName . "_$key",
$allConstInfos,
$phpVersionIdMinimumCompatibility
PHP_85_VERSION_ID
);
}

Expand Down
3 changes: 1 addition & 2 deletions ext/curl/curl_arginfo.h

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

9 changes: 3 additions & 6 deletions ext/date/php_date_arginfo.h

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

3 changes: 1 addition & 2 deletions ext/dom/php_dom_arginfo.h

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

6 changes: 2 additions & 4 deletions ext/enchant/enchant_arginfo.h

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

Loading