Skip to content

Commit 5572da5

Browse files
authored
Merge pull request #18 from koolkode/API
Register Attributes Without Changes to ZEND APIs
2 parents 8697ccf + 9e15b99 commit 5572da5

File tree

9 files changed

+147
-142
lines changed

9 files changed

+147
-142
lines changed

Zend/zend_API.c

Lines changed: 12 additions & 13 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 zend_property_info *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,13 +3608,13 @@ 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

36153615
zend_hash_update_ptr(&ce->properties_info, name, property_info);
36163616

3617-
return SUCCESS;
3617+
return property_info;
36183618
}
36193619
/* }}} */
36203620

@@ -3745,16 +3745,17 @@ 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+
zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
3751+
return SUCCESS;
37513752
}
37523753
/* }}} */
37533754

37543755
ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
37553756
{
37563757
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);
3758+
int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
37583759
zend_string_release(key);
37593760
return ret;
37603761
}
@@ -3814,7 +3815,7 @@ ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *nam
38143815
}
38153816
/* }}} */
38163817

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) /* {{{ */
3818+
ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
38183819
{
38193820
zend_class_constant *c;
38203821

@@ -3841,7 +3842,7 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
38413842
ZVAL_COPY_VALUE(&c->value, value);
38423843
Z_ACCESS_FLAGS(c->value) = access_type;
38433844
c->doc_comment = doc_comment;
3844-
c->attributes = attributes;
3845+
c->attributes = NULL;
38453846
c->ce = ce;
38463847
if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
38473848
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
@@ -3852,24 +3853,22 @@ ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *n
38523853
"Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
38533854
}
38543855

3855-
return SUCCESS;
3856+
return c;
38563857
}
38573858
/* }}} */
38583859

38593860
ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
38603861
{
3861-
int ret;
3862-
38633862
zend_string *key;
38643863

38653864
if (ce->type == ZEND_INTERNAL_CLASS) {
38663865
key = zend_string_init_interned(name, name_length, 1);
38673866
} else {
38683867
key = zend_string_init(name, name_length, 0);
38693868
}
3870-
ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL, NULL);
3869+
zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
38713870
zend_string_release(key);
3872-
return ret;
3871+
return SUCCESS;
38733872
}
38743873
/* }}} */
38753874

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 zend_property_info *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 zend_class_constant *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 == ((GC_FLAGS(name) & IS_STR_PERSISTENT) != 0)) {
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: 28 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,35 @@ 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_class_constant_attribute(zend_class_entry *ce, zend_class_constant *c, zend_string *name, uint32_t argc)
63+
{
64+
return zend_add_attribute(&c->attributes, ce->type != ZEND_USER_CLASS, 0, name, argc);
65+
}
66+
67+
void zend_register_attribute_ce(void);
68+
4469
#endif

0 commit comments

Comments
 (0)