Skip to content

Commit 91ef412

Browse files
committed
Refactor zend_object_handlers API to pass zend_object* and zend_string* insted of zval(s).
1 parent 0476d55 commit 91ef412

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2686
-1614
lines changed

Zend/zend_API.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,22 @@ ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest
504504
convert_to_string(arg);
505505
*dest = Z_STR_P(arg);
506506
} else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
507+
zend_object *zobj = Z_OBJ_P(arg);
508+
507509
if (Z_OBJ_HANDLER_P(arg, cast_object)) {
508510
zval obj;
509-
if (Z_OBJ_HANDLER_P(arg, cast_object)(arg, &obj, IS_STRING) == SUCCESS) {
510-
zval_ptr_dtor(arg);
511+
if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
512+
OBJ_RELEASE(zobj);
511513
ZVAL_COPY_VALUE(arg, &obj);
512514
*dest = Z_STR_P(arg);
513515
return 1;
514516
}
515-
} else if (Z_OBJ_HANDLER_P(arg, get)) {
517+
} else if (zobj->handlers->get) {
516518
zval rv;
517-
zval *z = Z_OBJ_HANDLER_P(arg, get)(arg, &rv);
519+
zval *z = zobj->handlers->get(zobj, &rv);
518520

519521
if (Z_TYPE_P(z) != IS_OBJECT) {
520-
zval_ptr_dtor(arg);
522+
OBJ_RELEASE(zobj);
521523
if (Z_TYPE_P(z) == IS_STRING) {
522524
ZVAL_COPY_VALUE(arg, z);
523525
} else {
@@ -1101,18 +1103,16 @@ ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this
11011103
* because it may call __set from the uninitialized object otherwise. */
11021104
ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
11031105
{
1104-
const zend_object_handlers *obj_ht = Z_OBJ_HT_P(obj);
1106+
zend_object *zobj = Z_OBJ_P(obj);
1107+
zend_object_write_property_t write_property = zobj->handlers->write_property;
11051108
zend_class_entry *old_scope = EG(fake_scope);
11061109
zend_string *key;
11071110
zval *value;
11081111

11091112
EG(fake_scope) = Z_OBJCE_P(obj);
11101113
ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
11111114
if (key) {
1112-
zval member;
1113-
1114-
ZVAL_STR(&member, key);
1115-
obj_ht->write_property(obj, &member, value, NULL);
1115+
write_property(zobj, key, value, NULL);
11161116
}
11171117
} ZEND_HASH_FOREACH_END();
11181118
EG(fake_scope) = old_scope;
@@ -1728,11 +1728,11 @@ ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len,
17281728

17291729
ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
17301730
{
1731-
zval z_key;
1731+
zend_string *str;
17321732

1733-
ZVAL_STRINGL(&z_key, key, key_len);
1734-
Z_OBJ_HANDLER_P(arg, write_property)(arg, &z_key, value, NULL);
1735-
zval_ptr_dtor(&z_key);
1733+
str = zend_string_init(key, key_len, 0);
1734+
Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
1735+
zend_string_release_ex(str, 0);
17361736
return SUCCESS;
17371737
}
17381738
/* }}} */
@@ -3143,16 +3143,18 @@ ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *obj
31433143
zend_class_entry *calling_scope;
31443144
zend_function *fptr;
31453145
zend_object *object;
3146-
if (Z_OBJ_HANDLER_P(callable, get_closure)
3147-
&& Z_OBJ_HANDLER_P(callable, get_closure)(callable, &calling_scope, &fptr, &object) == SUCCESS) {
3148-
zend_class_entry *ce = Z_OBJCE_P(callable);
3146+
zend_object *zobj = Z_OBJ_P(callable);
3147+
3148+
if (zobj->handlers->get_closure
3149+
&& zobj->handlers->get_closure(zobj, &calling_scope, &fptr, &object) == SUCCESS) {
3150+
zend_class_entry *ce = zobj->ce;
31493151
zend_string *callable_name = zend_string_alloc(
31503152
ZSTR_LEN(ce->name) + sizeof("::__invoke") - 1, 0);
31513153
memcpy(ZSTR_VAL(callable_name), ZSTR_VAL(ce->name), ZSTR_LEN(ce->name));
31523154
memcpy(ZSTR_VAL(callable_name) + ZSTR_LEN(ce->name), "::__invoke", sizeof("::__invoke"));
31533155
return callable_name;
31543156
}
3155-
return zval_get_string(callable);
3157+
return zval_get_string_func(callable);
31563158
}
31573159
case IS_REFERENCE:
31583160
callable = Z_REFVAL_P(callable);
@@ -3277,7 +3279,7 @@ static zend_always_inline zend_bool zend_is_callable_impl(zval *callable, zend_o
32773279
}
32783280
return 0;
32793281
case IS_OBJECT:
3280-
if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(callable, &fcc->calling_scope, &fcc->function_handler, &fcc->object) == SUCCESS) {
3282+
if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object) == SUCCESS) {
32813283
fcc->called_scope = fcc->calling_scope;
32823284
return 1;
32833285
}
@@ -3944,28 +3946,26 @@ ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char
39443946

39453947
ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
39463948
{
3947-
zval property;
39483949
zend_class_entry *old_scope = EG(fake_scope);
39493950

39503951
EG(fake_scope) = scope;
39513952

3952-
ZVAL_STR(&property, name);
3953-
Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3953+
Z_OBJ_HT_P(object)->write_property(Z_OBJ_P(object), name, value, NULL);
39543954

39553955
EG(fake_scope) = old_scope;
39563956
}
39573957
/* }}} */
39583958

39593959
ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
39603960
{
3961-
zval property;
3961+
zend_string *property;
39623962
zend_class_entry *old_scope = EG(fake_scope);
39633963

39643964
EG(fake_scope) = scope;
39653965

3966-
ZVAL_STRINGL(&property, name, name_length);
3967-
Z_OBJ_HT_P(object)->write_property(object, &property, value, NULL);
3968-
zval_ptr_dtor(&property);
3966+
property = zend_string_init(name, name_length, 0);
3967+
Z_OBJ_HT_P(object)->write_property(Z_OBJ_P(object), property, value, NULL);
3968+
zend_string_release_ex(property, 0);
39693969

39703970
EG(fake_scope) = old_scope;
39713971
}
@@ -3982,14 +3982,14 @@ ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, c
39823982

39833983
ZEND_API void zend_unset_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
39843984
{
3985-
zval property;
3985+
zend_string *property;
39863986
zend_class_entry *old_scope = EG(fake_scope);
39873987

39883988
EG(fake_scope) = scope;
39893989

3990-
ZVAL_STRINGL(&property, name, name_length);
3991-
Z_OBJ_HT_P(object)->unset_property(object, &property, 0);
3992-
zval_ptr_dtor(&property);
3990+
property = zend_string_init(name, name_length, 0);
3991+
Z_OBJ_HT_P(object)->unset_property(Z_OBJ_P(object), property, 0);
3992+
zend_string_release_ex(property, 0);
39933993

39943994
EG(fake_scope) = old_scope;
39953995
}
@@ -4148,13 +4148,12 @@ ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const
41484148

41494149
ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zend_bool silent, zval *rv) /* {{{ */
41504150
{
4151-
zval property, *value;
4151+
zval *value;
41524152
zend_class_entry *old_scope = EG(fake_scope);
41534153

41544154
EG(fake_scope) = scope;
41554155

4156-
ZVAL_STR(&property, name);
4157-
value = Z_OBJ_HT_P(object)->read_property(object, &property, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
4156+
value = Z_OBJ_HT_P(object)->read_property(Z_OBJ_P(object), name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
41584157

41594158
EG(fake_scope) = old_scope;
41604159
return value;

Zend/zend_API.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ END_EXTERN_C()
632632
#define RETURN_FALSE { RETVAL_FALSE; return; }
633633
#define RETURN_TRUE { RETVAL_TRUE; return; }
634634

635-
#define HASH_OF(p) (Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties((p)) : NULL)))
635+
#define HASH_OF(p) (Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties(Z_OBJ_P(p)) : NULL)))
636636
#define ZVAL_IS_NULL(z) (Z_TYPE_P(z) == IS_NULL)
637637

638638
/* For compatibility */
@@ -1501,15 +1501,16 @@ static zend_always_inline int zend_parse_arg_array_ht(zval *arg, HashTable **des
15011501
if (EXPECTED(Z_TYPE_P(arg) == IS_ARRAY)) {
15021502
*dest = Z_ARRVAL_P(arg);
15031503
} else if (or_object && EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
1504+
zend_object *zobj = Z_OBJ_P(arg);
15041505
if (separate
1505-
&& Z_OBJ_P(arg)->properties
1506-
&& UNEXPECTED(GC_REFCOUNT(Z_OBJ_P(arg)->properties) > 1)) {
1507-
if (EXPECTED(!(GC_FLAGS(Z_OBJ_P(arg)->properties) & IS_ARRAY_IMMUTABLE))) {
1508-
GC_DELREF(Z_OBJ_P(arg)->properties);
1506+
&& zobj->properties
1507+
&& UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
1508+
if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
1509+
GC_DELREF(zobj->properties);
15091510
}
1510-
Z_OBJ_P(arg)->properties = zend_array_dup(Z_OBJ_P(arg)->properties);
1511+
zobj->properties = zend_array_dup(zobj->properties);
15111512
}
1512-
*dest = Z_OBJ_HT_P(arg)->get_properties(arg);
1513+
*dest = zobj->handlers->get_properties(zobj);
15131514
} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
15141515
*dest = NULL;
15151516
} else {

Zend/zend_builtin_functions.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -811,11 +811,11 @@ ZEND_FUNCTION(define)
811811
if (Z_TYPE(val_free) == IS_UNDEF) {
812812
if (Z_OBJ_HT_P(val)->get) {
813813
zval rv;
814-
val = Z_OBJ_HT_P(val)->get(val, &rv);
814+
val = Z_OBJ_HT_P(val)->get(Z_OBJ_P(val), &rv);
815815
ZVAL_COPY_VALUE(&val_free, val);
816816
goto repeat;
817817
} else if (Z_OBJ_HT_P(val)->cast_object) {
818-
if (Z_OBJ_HT_P(val)->cast_object(val, &val_free, IS_STRING) == SUCCESS) {
818+
if (Z_OBJ_HT_P(val)->cast_object(Z_OBJ_P(val), &val_free, IS_STRING) == SUCCESS) {
819819
val = &val_free;
820820
break;
821821
}
@@ -1097,13 +1097,12 @@ ZEND_FUNCTION(get_object_vars)
10971097
Z_PARAM_OBJECT(obj)
10981098
ZEND_PARSE_PARAMETERS_END();
10991099

1100-
properties = Z_OBJ_HT_P(obj)->get_properties(obj);
1100+
zobj = Z_OBJ_P(obj);
1101+
properties = zobj->handlers->get_properties(zobj);
11011102
if (properties == NULL) {
11021103
RETURN_FALSE;
11031104
}
11041105

1105-
zobj = Z_OBJ_P(obj);
1106-
11071106
if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
11081107
/* fast copy */
11091108
if (EXPECTED(zobj->handlers == &std_object_handlers)) {
@@ -1292,7 +1291,6 @@ ZEND_FUNCTION(property_exists)
12921291
zend_string *property;
12931292
zend_class_entry *ce;
12941293
zend_property_info *property_info;
1295-
zval property_z;
12961294

12971295
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
12981296
return;
@@ -1321,10 +1319,8 @@ ZEND_FUNCTION(property_exists)
13211319
RETURN_TRUE;
13221320
}
13231321

1324-
ZVAL_STR(&property_z, property);
1325-
13261322
if (Z_TYPE_P(object) == IS_OBJECT &&
1327-
Z_OBJ_HANDLER_P(object, has_property)(object, &property_z, 2, NULL)) {
1323+
Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, 2, NULL)) {
13281324
RETURN_TRUE;
13291325
}
13301326
RETURN_FALSE;

Zend/zend_closures.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -395,28 +395,28 @@ static zend_function *zend_closure_get_method(zend_object **object, zend_string
395395
}
396396
/* }}} */
397397

398-
static zval *zend_closure_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
398+
static zval *zend_closure_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
399399
{
400400
ZEND_CLOSURE_PROPERTY_ERROR();
401401
return &EG(uninitialized_zval);
402402
}
403403
/* }}} */
404404

405-
static zval *zend_closure_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
405+
static zval *zend_closure_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot) /* {{{ */
406406
{
407407
ZEND_CLOSURE_PROPERTY_ERROR();
408408
return value;
409409
}
410410
/* }}} */
411411

412-
static zval *zend_closure_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
412+
static zval *zend_closure_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) /* {{{ */
413413
{
414414
ZEND_CLOSURE_PROPERTY_ERROR();
415415
return NULL;
416416
}
417417
/* }}} */
418418

419-
static int zend_closure_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
419+
static int zend_closure_has_property(zend_object *object, zend_string *member, int has_set_exists, void **cache_slot) /* {{{ */
420420
{
421421
if (has_set_exists != ZEND_PROPERTY_EXISTS) {
422422
ZEND_CLOSURE_PROPERTY_ERROR();
@@ -425,7 +425,7 @@ static int zend_closure_has_property(zval *object, zval *member, int has_set_exi
425425
}
426426
/* }}} */
427427

428-
static void zend_closure_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
428+
static void zend_closure_unset_property(zend_object *object, zend_string *member, void **cache_slot) /* {{{ */
429429
{
430430
ZEND_CLOSURE_PROPERTY_ERROR();
431431
}
@@ -461,9 +461,9 @@ static zend_object *zend_closure_new(zend_class_entry *class_type) /* {{{ */
461461
}
462462
/* }}} */
463463

464-
static zend_object *zend_closure_clone(zval *zobject) /* {{{ */
464+
static zend_object *zend_closure_clone(zend_object *zobject) /* {{{ */
465465
{
466-
zend_closure *closure = (zend_closure *)Z_OBJ_P(zobject);
466+
zend_closure *closure = (zend_closure *)zobject;
467467
zval result;
468468

469469
zend_create_closure(&result, &closure->func,
@@ -472,9 +472,9 @@ static zend_object *zend_closure_clone(zval *zobject) /* {{{ */
472472
}
473473
/* }}} */
474474

475-
int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
475+
int zend_closure_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
476476
{
477-
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
477+
zend_closure *closure = (zend_closure *)obj;
478478
*fptr_ptr = &closure->func;
479479
*ce_ptr = closure->called_scope;
480480

@@ -488,9 +488,9 @@ int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function
488488
}
489489
/* }}} */
490490

491-
static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp) /* {{{ */
491+
static HashTable *zend_closure_get_debug_info(zend_object *object, int *is_temp) /* {{{ */
492492
{
493-
zend_closure *closure = (zend_closure *)Z_OBJ_P(object);
493+
zend_closure *closure = (zend_closure *)object;
494494
zval val;
495495
struct _zend_arg_info *arg_info = closure->func.common.arg_info;
496496
HashTable *debug_info;
@@ -553,9 +553,9 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp) /* {{{
553553
}
554554
/* }}} */
555555

556-
static HashTable *zend_closure_get_gc(zval *obj, zval **table, int *n) /* {{{ */
556+
static HashTable *zend_closure_get_gc(zend_object *obj, zval **table, int *n) /* {{{ */
557557
{
558-
zend_closure *closure = (zend_closure *)Z_OBJ_P(obj);
558+
zend_closure *closure = (zend_closure *)obj;
559559

560560
*table = Z_TYPE(closure->this_ptr) != IS_NULL ? &closure->this_ptr : NULL;
561561
*n = Z_TYPE(closure->this_ptr) != IS_NULL ? 1 : 0;

Zend/zend_exceptions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ ZEND_API ZEND_COLD void zend_exception_error(zend_object *ex, int severity) /* {
982982
zend_string *str, *file = NULL;
983983
zend_long line = 0;
984984

985-
zend_call_method_with_0_params(&exception, ce_exception, &ex->ce->__tostring, "__tostring", &tmp);
985+
zend_call_method_with_0_params(Z_OBJ(exception), ce_exception, &ex->ce->__tostring, "__tostring", &tmp);
986986
if (!EG(exception)) {
987987
if (Z_TYPE(tmp) != IS_STRING) {
988988
zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));

0 commit comments

Comments
 (0)