Skip to content

Commit 50c2e67

Browse files
committed
Use zend_std_build_properties() to access zend_object.properties
The zend_object.properties HashTable needs to be built just in time by calling rebuild_object_properties() on the object before accessing it. Normally this is done automatically in zend_std_get_properties(), but we do it manually in a few places. In this change I introduce an inline variant of zend_std_build_properties(), and refactor these places to use it instead of calling rebuild_object_properties() manually. rebuild_object_properties() is made static to enforce usage of zend_std_get_properties() or zend_std_build_properties_ex().
1 parent 5471f3d commit 50c2e67

13 files changed

+112
-248
lines changed

Zend/zend_API.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "zend_inheritance.h"
3232
#include "zend_ini.h"
3333
#include "zend_enum.h"
34+
#include "zend_object_handlers.h"
3435
#include "zend_observer.h"
3536

3637
#include <stdarg.h>
@@ -1764,10 +1765,7 @@ ZEND_API void object_properties_load(zend_object *object, HashTable *properties)
17641765
ZSTR_VAL(object->ce->name), property_info != ZEND_WRONG_PROPERTY_INFO ? zend_get_unmangled_property_name(key): "");
17651766
}
17661767

1767-
if (!object->properties) {
1768-
rebuild_object_properties(object);
1769-
}
1770-
prop = zend_hash_update(object->properties, key, prop);
1768+
prop = zend_hash_update(zend_std_get_properties_ex(object), key, prop);
17711769
zval_add_ref(prop);
17721770
}
17731771
} else {
@@ -1779,10 +1777,7 @@ ZEND_API void object_properties_load(zend_object *object, HashTable *properties)
17791777
ZSTR_VAL(object->ce->name), h);
17801778
}
17811779

1782-
if (!object->properties) {
1783-
rebuild_object_properties(object);
1784-
}
1785-
prop = zend_hash_index_update(object->properties, h, prop);
1780+
prop = zend_hash_index_update(zend_std_get_properties_ex(object), h, prop);
17861781
zval_add_ref(prop);
17871782
}
17881783
} ZEND_HASH_FOREACH_END();

Zend/zend_object_handlers.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
called, we cal __call handler.
6363
*/
6464

65-
ZEND_API void rebuild_object_properties(zend_object *zobj) /* {{{ */
65+
void rebuild_object_properties(zend_object *zobj) /* {{{ */
6666
{
6767
if (!zobj->properties) {
6868
zend_property_info *prop_info;
@@ -129,10 +129,7 @@ ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj) /*
129129

130130
ZEND_API HashTable *zend_std_get_properties(zend_object *zobj) /* {{{ */
131131
{
132-
if (!zobj->properties) {
133-
rebuild_object_properties(zobj);
134-
}
135-
return zobj->properties;
132+
return zend_std_get_properties_ex(zobj);
136133
}
137134
/* }}} */
138135

Zend/zend_object_handlers.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,14 @@ ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *
267267
ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj);
268268
ZEND_API int zend_std_compare_objects(zval *o1, zval *o2);
269269
ZEND_API zend_result zend_std_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only);
270-
ZEND_API void rebuild_object_properties(zend_object *zobj);
270+
271+
static inline HashTable *zend_std_get_properties_ex(zend_object *object)
272+
{
273+
if (!object->properties) {
274+
return zend_std_get_properties(object);
275+
}
276+
return object->properties;
277+
}
271278

272279
ZEND_API HashTable *zend_std_build_object_properties_array(zend_object *zobj);
273280

Zend/zend_vm_def.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,9 +2495,6 @@ ZEND_VM_C_LABEL(fast_assign_obj):
24952495
}
24962496

24972497
if (!zobj->ce->__set && (zobj->ce->ce_flags & ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES)) {
2498-
if (EXPECTED(zobj->properties == NULL)) {
2499-
rebuild_object_properties(zobj);
2500-
}
25012498
if (OP_DATA_TYPE == IS_CONST) {
25022499
if (UNEXPECTED(Z_OPT_REFCOUNTED_P(value))) {
25032500
Z_ADDREF_P(value);
@@ -2521,8 +2518,8 @@ ZEND_VM_C_LABEL(fast_assign_obj):
25212518
} else if (OP_DATA_TYPE == IS_CV) {
25222519
Z_TRY_ADDREF_P(value);
25232520
}
2524-
}
2525-
zend_hash_add_new(zobj->properties, name, value);
2521+
}
2522+
zend_hash_add_new(zend_std_get_properties_ex(zobj), name, value);
25262523
if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
25272524
ZVAL_COPY(EX_VAR(opline->result.var), value);
25282525
}

0 commit comments

Comments
 (0)