Skip to content

Commit 6256e24

Browse files
committed
Refactored attributes to avoid ZEND API changes.
1 parent 8697ccf commit 6256e24

File tree

9 files changed

+167
-137
lines changed

9 files changed

+167
-137
lines changed

Zend/zend_API.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,7 +3515,7 @@ static zend_always_inline zend_bool is_persistent_class(zend_class_entry *ce) {
35153515
&& ce->info.internal.module->type == MODULE_PERSISTENT;
35163516
}
35173517

3518-
ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, HashTable *attributes, zend_type type) /* {{{ */
3518+
ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type) /* {{{ */
35193519
{
35203520
zend_property_info *property_info, *property_info_ptr;
35213521

@@ -3608,7 +3608,7 @@ ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name
36083608
property_info->name = zend_new_interned_string(property_info->name);
36093609
property_info->flags = access_type;
36103610
property_info->doc_comment = doc_comment;
3611-
property_info->attributes = attributes;
3611+
property_info->attributes = NULL;
36123612
property_info->ce = ce;
36133613
property_info->type = type;
36143614

@@ -3745,16 +3745,16 @@ ZEND_API int zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, ze
37453745
}
37463746
/* }}} */
37473747

3748-
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, HashTable *attributes) /* {{{ */
3748+
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
37493749
{
3750-
return zend_declare_typed_property(ce, name, property, access_type, doc_comment, attributes, (zend_type) ZEND_TYPE_INIT_NONE(0));
3750+
return zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
37513751
}
37523752
/* }}} */
37533753

37543754
ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
37553755
{
37563756
zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
3757-
int ret = zend_declare_property_ex(ce, key, property, access_type, NULL, NULL);
3757+
int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
37583758
zend_string_release(key);
37593759
return ret;
37603760
}
@@ -3814,7 +3814,7 @@ ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *nam
38143814
}
38153815
/* }}} */
38163816

3817-
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment, HashTable *attributes) /* {{{ */
3817+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
38183818
{
38193819
zend_class_constant *c;
38203820

@@ -3841,7 +3841,7 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
38413841
ZVAL_COPY_VALUE(&c->value, value);
38423842
Z_ACCESS_FLAGS(c->value) = access_type;
38433843
c->doc_comment = doc_comment;
3844-
c->attributes = attributes;
3844+
c->attributes = NULL;
38453845
c->ce = ce;
38463846
if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
38473847
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
@@ -3867,7 +3867,7 @@ ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name,
38673867
} else {
38683868
key = zend_string_init(name, name_length, 0);
38693869
}
3870-
ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL, NULL);
3870+
ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
38713871
zend_string_release(key);
38723872
return ret;
38733873
}

Zend/zend_API.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_nam
351351
ZEND_API const char *zend_get_module_version(const char *module_name);
352352
ZEND_API int zend_get_module_started(const char *module_name);
353353

354-
ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, HashTable *attributes, zend_type type);
354+
ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type);
355355

356-
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, HashTable *attributes);
356+
ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment);
357357
ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type);
358358
ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type);
359359
ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type);
@@ -362,7 +362,7 @@ ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name
362362
ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type);
363363
ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type);
364364

365-
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment, HashTable *attributes);
365+
ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment);
366366
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value);
367367
ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length);
368368
ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value);

Zend/zend_attributes.c

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@ ZEND_API zend_attributes_internal_validator zend_attribute_get_validator(zend_st
1818
return zend_hash_find_ptr(&internal_validators, lcname);
1919
}
2020

21-
ZEND_API void zend_attribute_free(zend_attribute *attr, int persistent)
22-
{
23-
uint32_t i;
24-
25-
zend_string_release(attr->name);
26-
zend_string_release(attr->lcname);
27-
28-
for (i = 0; i < attr->argc; i++) {
29-
zval_ptr_dtor(&attr->argv[i]);
30-
}
31-
32-
pefree(attr, persistent);
33-
}
34-
3521
static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
3622
{
3723
if (attributes) {
@@ -84,9 +70,52 @@ ZEND_API zend_attribute *zend_get_parameter_attribute_str(HashTable *attributes,
8470
return get_attribute_str(attributes, str, len, offset + 1);
8571
}
8672

87-
static void attribute_ptr_dtor(zval *v)
73+
static zend_always_inline void free_attribute(zend_attribute *attr, int persistent)
8874
{
89-
zend_attribute_free((zend_attribute *) Z_PTR_P(v), 1);
75+
uint32_t i;
76+
77+
zend_string_release(attr->name);
78+
zend_string_release(attr->lcname);
79+
80+
for (i = 0; i < attr->argc; i++) {
81+
zval_ptr_dtor(&attr->argv[i]);
82+
}
83+
84+
pefree(attr, persistent);
85+
}
86+
87+
static void attr_free(zval *v)
88+
{
89+
free_attribute((zend_attribute *) Z_PTR_P(v), 0);
90+
}
91+
92+
static void attr_pfree(zval *v)
93+
{
94+
free_attribute((zend_attribute *) Z_PTR_P(v), 1);
95+
}
96+
97+
ZEND_API zend_attribute *zend_add_attribute(HashTable **attributes, zend_bool persistent, uint32_t offset, zend_string *name, uint32_t argc)
98+
{
99+
if (*attributes == NULL) {
100+
*attributes = pemalloc(sizeof(HashTable), persistent);
101+
zend_hash_init(*attributes, 8, NULL, persistent ? attr_pfree : attr_free, persistent);
102+
}
103+
104+
zend_attribute *attr = pemalloc(ZEND_ATTRIBUTE_SIZE(argc), persistent);
105+
106+
if (persistent == (zend_bool) (GC_FLAGS(name) & IS_STR_PERSISTENT)) {
107+
attr->name = zend_string_copy(name);
108+
} else {
109+
attr->name = zend_string_dup(name, persistent);
110+
}
111+
112+
attr->lcname = zend_string_tolower_ex(attr->name, persistent);
113+
attr->offset = offset;
114+
attr->argc = argc;
115+
116+
zend_hash_next_index_insert_ptr(*attributes, attr);
117+
118+
return attr;
90119
}
91120

92121
ZEND_API void zend_compiler_attribute_register(zend_class_entry *ce, zend_attributes_internal_validator validator)
@@ -100,19 +129,7 @@ ZEND_API void zend_compiler_attribute_register(zend_class_entry *ce, zend_attrib
100129
zend_hash_update_ptr(&internal_validators, lcname, validator);
101130
zend_string_release(lcname);
102131

103-
if (ce->attributes == NULL) {
104-
ce->attributes = pemalloc(sizeof(HashTable), 1);
105-
zend_hash_init(ce->attributes, 8, NULL, attribute_ptr_dtor, 1);
106-
}
107-
108-
zend_attribute *attr = pemalloc(ZEND_ATTRIBUTE_SIZE(0), 1);
109-
110-
attr->name = zend_string_copy(zend_ce_php_attribute->name);
111-
attr->lcname = zend_string_tolower_ex(attr->name, 1);
112-
attr->offset = 0;
113-
attr->argc = 0;
114-
115-
zend_hash_next_index_insert_ptr(ce->attributes, attr);
132+
zend_add_class_attribute(ce, zend_ce_php_attribute->name, 0);
116133
}
117134

118135
void zend_register_attribute_ce(void)

Zend/zend_attributes.h

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ typedef struct _zend_attribute {
2626

2727
typedef void (*zend_attributes_internal_validator)(zend_attribute *attr, int target);
2828

29-
ZEND_API void zend_attribute_free(zend_attribute *attr, int persistent);
30-
3129
ZEND_API zend_attribute *zend_get_attribute(HashTable *attributes, zend_string *lcname);
3230
ZEND_API zend_attribute *zend_get_attribute_str(HashTable *attributes, const char *str, size_t len);
3331

@@ -37,8 +35,59 @@ ZEND_API zend_attribute *zend_get_parameter_attribute_str(HashTable *attributes,
3735
ZEND_API void zend_compiler_attribute_register(zend_class_entry *ce, zend_attributes_internal_validator validator);
3836
ZEND_API zend_attributes_internal_validator zend_attribute_get_validator(zend_string *lcname);
3937

40-
void zend_register_attribute_ce(void);
38+
ZEND_API zend_attribute *zend_add_attribute(HashTable **attributes, zend_bool persistent, uint32_t offset, zend_string *name, uint32_t argc);
4139

4240
END_EXTERN_C()
4341

42+
static zend_always_inline zend_attribute *zend_add_class_attribute(zend_class_entry *ce, zend_string *name, uint32_t argc)
43+
{
44+
return zend_add_attribute(&ce->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
45+
}
46+
47+
static zend_always_inline zend_attribute *zend_add_function_attribute(zend_function *func, zend_string *name, uint32_t argc)
48+
{
49+
return zend_add_attribute(&func->common.attributes, func->common.type != ZEND_USER_FUNCTION, 0, name, argc);
50+
}
51+
52+
static zend_always_inline zend_attribute *zend_add_parameter_attribute(zend_function *func, uint32_t offset, zend_string *name, uint32_t argc)
53+
{
54+
return zend_add_attribute(&func->common.attributes, func->common.type != ZEND_USER_FUNCTION, offset + 1, name, argc);
55+
}
56+
57+
static zend_always_inline zend_attribute *zend_add_property_attribute(zend_class_entry *ce, zend_property_info *info, zend_string *name, uint32_t argc)
58+
{
59+
return zend_add_attribute(&info->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
60+
}
61+
62+
static zend_always_inline zend_attribute *zend_add_property_attribute_str(zend_class_entry *ce, const char *key, size_t len, zend_string *name, uint32_t argc)
63+
{
64+
zend_property_info *info = (zend_property_info *) zend_hash_str_find_ptr(&ce->properties_info, key, len);
65+
66+
if (info == NULL) {
67+
zend_error_noreturn(E_ERROR, "Property '%.*s' not found in class '%s'", (int) len, key, ZSTR_VAL(ce->name));
68+
return NULL;
69+
}
70+
71+
return zend_add_attribute(&info->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
72+
}
73+
74+
static zend_always_inline zend_attribute *zend_add_class_constant_attribute(zend_class_entry *ce, zend_class_constant *c, zend_string *name, uint32_t argc)
75+
{
76+
return zend_add_attribute(&c->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
77+
}
78+
79+
static zend_always_inline zend_attribute *zend_add_class_constant_attribute_str(zend_class_entry *ce, const char *key, size_t len, zend_string *name, uint32_t argc)
80+
{
81+
zend_class_constant *c = (zend_class_constant *) zend_hash_str_find_ptr(&ce->constants_table, key, len);
82+
83+
if (c == NULL) {
84+
zend_error_noreturn(E_ERROR, "Class constant '%.*s' not found in class '%s'", (int) len, key, ZSTR_VAL(ce->name));
85+
return NULL;
86+
}
87+
88+
return zend_add_attribute(&c->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
89+
}
90+
91+
void zend_register_attribute_ce(void);
92+
4493
#endif

0 commit comments

Comments
 (0)