@@ -3069,19 +3069,26 @@ class StringBuilder {
3069
3069
* @param string $varName
3070
3070
* @param string $strContent
3071
3071
* @param ?int $minPHPCompatibility
3072
+ * @param bool $interned
3072
3073
* @return string[]
3073
3074
*/
3074
3075
public static function getString (
3075
3076
string $ varName ,
3076
3077
string $ content ,
3077
- ?int $ minPHPCompatibility
3078
+ ?int $ minPHPCompatibility ,
3079
+ bool $ interned = false
3078
3080
): array {
3079
3081
// Generally strings will not be known
3082
+ $ initFn = $ interned ? 'zend_string_init_interned ' : 'zend_string_init ' ;
3080
3083
$ result = [
3081
- "\tzend_string * $ varName = zend_string_init ( \"$ content \", sizeof( \"$ content \") - 1, 1); \n" ,
3084
+ "\tzend_string * $ varName = $ initFn ( \"$ content \", sizeof( \"$ content \") - 1, 1); \n" ,
3082
3085
$ varName ,
3083
3086
"\tzend_string_release( $ varName); \n"
3084
3087
];
3088
+ // For attribute values that are not freed
3089
+ if ($ varName === '' ) {
3090
+ $ result [0 ] = "$ initFn( \"$ content \", sizeof( \"$ content \") - 1, 1); \n" ;
3091
+ }
3085
3092
// If not set, use the current latest version
3086
3093
$ allVersions = ALL_PHP_VERSION_IDS ;
3087
3094
$ minPhp = $ minPHPCompatibility ?? end ($ allVersions );
@@ -3338,40 +3345,36 @@ public function __construct(string $class, array $args) {
3338
3345
3339
3346
/** @param array<string, ConstInfo> $allConstInfos */
3340
3347
public function generateCode (string $ invocation , string $ nameSuffix , array $ allConstInfos , ?int $ phpVersionIdMinimumCompatibility ): string {
3341
- $ php82MinimumCompatibility = $ phpVersionIdMinimumCompatibility === null || $ phpVersionIdMinimumCompatibility >= PHP_82_VERSION_ID ;
3342
- $ php84MinimumCompatibility = $ phpVersionIdMinimumCompatibility === null || $ phpVersionIdMinimumCompatibility >= PHP_84_VERSION_ID ;
3343
- /* see ZEND_KNOWN_STRINGS in Zend/strings.h */
3344
- $ knowns = [
3345
- "message " => "ZEND_STR_MESSAGE " ,
3346
- ];
3347
- if ($ php82MinimumCompatibility ) {
3348
- $ knowns ["SensitiveParameter " ] = "ZEND_STR_SENSITIVEPARAMETER " ;
3349
- }
3350
- if ($ php84MinimumCompatibility ) {
3351
- $ knowns ["Deprecated " ] = "ZEND_STR_DEPRECATED_CAPITALIZED " ;
3352
- $ knowns ["since " ] = "ZEND_STR_SINCE " ;
3353
- }
3354
-
3355
- $ code = "\n" ;
3356
3348
$ escapedAttributeName = strtr ($ this ->class , '\\' , '_ ' );
3357
- if (isset ($ knowns [$ escapedAttributeName ])) {
3358
- $ code .= "\t" . ($ this ->args ? "zend_attribute *attribute_ {$ escapedAttributeName }_ $ nameSuffix = " : "" ) . "$ invocation, ZSTR_KNOWN( {$ knowns [$ escapedAttributeName ]}), " . count ($ this ->args ) . "); \n" ;
3359
- } else {
3360
- $ code .= "\tzend_string *attribute_name_ {$ escapedAttributeName }_ $ nameSuffix = zend_string_init_interned( \"" . addcslashes ($ this ->class , "\\" ) . "\", sizeof( \"" . addcslashes ($ this ->class , "\\" ) . "\") - 1, 1); \n" ;
3361
- $ code .= "\t" . ($ this ->args ? "zend_attribute *attribute_ {$ escapedAttributeName }_ $ nameSuffix = " : "" ) . "$ invocation, attribute_name_ {$ escapedAttributeName }_ $ nameSuffix, " . count ($ this ->args ) . "); \n" ;
3362
- $ code .= "\tzend_string_release(attribute_name_ {$ escapedAttributeName }_ $ nameSuffix); \n" ;
3363
- }
3349
+ [$ stringInit , $ nameCode , $ stringRelease ] = StringBuilder::getString (
3350
+ "attribute_name_ {$ escapedAttributeName }_ $ nameSuffix " ,
3351
+ addcslashes ($ this ->class , "\\" ),
3352
+ $ phpVersionIdMinimumCompatibility ,
3353
+ true
3354
+ );
3355
+ $ code = "\n" ;
3356
+ $ code .= $ stringInit ;
3357
+ $ code .= "\t" . ($ this ->args ? "zend_attribute *attribute_ {$ escapedAttributeName }_ $ nameSuffix = " : "" ) . "$ invocation, $ nameCode, " . count ($ this ->args ) . "); \n" ;
3358
+ $ code .= $ stringRelease ;
3359
+
3364
3360
foreach ($ this ->args as $ i => $ arg ) {
3365
3361
$ value = EvaluatedValue::createFromExpression ($ arg ->value , null , null , $ allConstInfos );
3366
3362
$ zvalName = "attribute_ {$ escapedAttributeName }_ {$ nameSuffix }_arg $ i " ;
3367
3363
$ code .= $ value ->initializeZval ($ zvalName );
3368
3364
$ code .= "\tZVAL_COPY_VALUE(&attribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].value, & $ zvalName); \n" ;
3369
3365
if ($ arg ->name ) {
3370
- if (isset ($ knowns [$ arg ->name ->name ])) {
3371
- $ code .= "\tattribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].name = ZSTR_KNOWN( {$ knowns [$ arg ->name ->name ]}); \n" ;
3366
+ [$ stringInit , $ nameCode , $ stringRelease ] = StringBuilder::getString (
3367
+ "" ,
3368
+ $ arg ->name ->name ,
3369
+ $ phpVersionIdMinimumCompatibility ,
3370
+ true
3371
+ );
3372
+ if ($ stringInit === '' ) {
3373
+ $ nameCode .= "; \n" ;
3372
3374
} else {
3373
- $ code .= "\t attribute_ { $ escapedAttributeName } _ { $ nameSuffix } ->args[ $ i ].name = zend_string_init_interned( \"{ $ arg -> name -> name }\" , sizeof( \"{ $ arg -> name -> name }\" ) - 1, 1); \n" ;
3375
+ $ nameCode = $ stringInit ;
3374
3376
}
3377
+ $ code .= "\tattribute_ {$ escapedAttributeName }_ {$ nameSuffix }->args[ $ i].name = $ nameCode " ;
3375
3378
}
3376
3379
}
3377
3380
return $ code ;
0 commit comments