Skip to content

Commit c461b60

Browse files
authored
refactor: change zend_is_true to return bool (#14301)
Previously this returned `int`. Many functions actually take advantage of the fact this returns exactly 0 or 1. For instance, `main/streams/xp_socket.c` does: sockopts |= STREAM_SOCKOP_IPV6_V6ONLY_ENABLED * zend_is_true(tmpzval); And `Zend/zend_compile.c` does: child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))]; I changed a few places trivially from `int` to `bool`, but there are still many places such as the object handlers which return `int` that should eventually be `bool`.
1 parent 2ab114e commit c461b60

20 files changed

+57
-49
lines changed

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
8686
for internal functions. If you need a cache slot for both internal and user
8787
functions, you may obtain a slot for each through the corresponding function.
8888

89+
* zend_is_true now returns bool rather than int. Note that on PHP 8 this has
90+
always returned 0 or 1, so conversion should be trivial.
8991

9092
========================
9193
2. Build system changes

Zend/Optimizer/pass1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
309309
case ZEND_JMPZ:
310310
case ZEND_JMPNZ:
311311
if (opline->op1_type == IS_CONST) {
312-
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
312+
bool should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
313313

314314
if (opline->opcode == ZEND_JMPZ) {
315315
should_jmp = !should_jmp;

Zend/zend_object_handlers.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,12 @@ ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *
10571057
}
10581058
/* }}} */
10591059

1060+
// todo: make zend_std_has_dimension return bool as well
10601061
ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
10611062
{
10621063
zend_class_entry *ce = object->ce;
10631064
zval retval, tmp_offset;
1064-
int result;
1065+
bool result;
10651066

10661067
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
10671068
if (EXPECTED(funcs)) {
@@ -1081,6 +1082,7 @@ ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check
10811082
zend_bad_array_access(ce);
10821083
return 0;
10831084
}
1085+
10841086
return result;
10851087
}
10861088
/* }}} */
@@ -1810,9 +1812,10 @@ ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
18101812
return ZEND_UNCOMPARABLE;
18111813
}
18121814

1815+
// todo: make zend_std_has_property return bool as well
18131816
ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
18141817
{
1815-
int result;
1818+
bool result;
18161819
zval *value = NULL;
18171820
uintptr_t property_offset;
18181821
const zend_property_info *prop_info = NULL;
@@ -1826,7 +1829,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18261829
}
18271830
if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) {
18281831
/* Skip __isset() for uninitialized typed properties */
1829-
result = 0;
1832+
result = false;
18301833
goto exit;
18311834
}
18321835
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
@@ -1862,17 +1865,17 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18621865
result = (Z_TYPE_P(value) != IS_NULL);
18631866
} else {
18641867
ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
1865-
result = 1;
1868+
result = true;
18661869
}
18671870
goto exit;
18681871
}
18691872
}
18701873
} else if (UNEXPECTED(EG(exception))) {
1871-
result = 0;
1874+
result = false;
18721875
goto exit;
18731876
}
18741877

1875-
result = 0;
1878+
result = false;
18761879
if ((has_set_exists != ZEND_PROPERTY_EXISTS) && zobj->ce->__isset) {
18771880
uint32_t *guard = zend_get_property_guard(zobj, name);
18781881

@@ -1893,7 +1896,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18931896
result = i_zend_is_true(&rv);
18941897
zval_ptr_dtor(&rv);
18951898
} else {
1896-
result = 0;
1899+
result = false;
18971900
}
18981901
}
18991902
(*guard) &= ~IN_ISSET;

Zend/zend_operators.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,9 +2818,9 @@ ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
28182818
}
28192819
/* }}} */
28202820

2821-
ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2821+
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
28222822
{
2823-
return (int) i_zend_is_true(op);
2823+
return i_zend_is_true(op);
28242824
}
28252825
/* }}} */
28262826

Zend/zend_operators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ static zend_always_inline bool try_convert_to_string(zval *op) {
372372
#define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }
373373

374374

375-
ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op);
375+
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op);
376376
ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op);
377377

378378
#define zval_is_true(op) \

Zend/zend_vm_def.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7313,8 +7313,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
73137313
{
73147314
USE_OPLINE
73157315
zval *value;
7316-
/* Should be bool result? as below got: result = (opline->extended_value & ZEND_ISEMPTY) */
7317-
int result;
7316+
bool result;
73187317
zval *varname;
73197318
zend_string *name, *tmp_name;
73207319
HashTable *target_symbol_table;
@@ -7351,7 +7350,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
73517350
}
73527351
}
73537352

7354-
ZEND_VM_SMART_BRANCH(result, 1);
7353+
ZEND_VM_SMART_BRANCH(result, true);
73557354
}
73567355

73577356
/* No specialization for op_types (CONST|TMPVAR|CV, UNUSED|CLASS_FETCH|CONST|VAR) */

Zend/zend_vm_execute.h

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_weakrefs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
405405
zend_hash_index_add_new(&wm->ht, obj_key, value);
406406
}
407407

408+
// todo: make zend_weakmap_has_dimension return bool as well
408409
/* int return and check_empty due to Object Handler API */
409410
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
410411
{

ext/dom/php_dom.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,13 @@ zval *dom_write_property(zend_object *object, zend_string *name, zval *value, vo
404404
return zend_std_write_property(object, name, value, cache_slot);
405405
}
406406

407+
// todo: make dom_property_exists return bool as well
407408
/* {{{ dom_property_exists */
408409
static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
409410
{
410411
dom_object *obj = php_dom_obj_from_obj(object);
411412
dom_prop_handler *hnd = NULL;
412-
int retval = 0;
413+
bool retval = false;
413414

414415
if (obj->prop_handler != NULL) {
415416
hnd = zend_hash_find_ptr(obj->prop_handler, name);
@@ -418,7 +419,7 @@ static int dom_property_exists(zend_object *object, zend_string *name, int check
418419
zval tmp;
419420

420421
if (check_empty == 2) {
421-
retval = 1;
422+
retval = true;
422423
} else if (hnd->read_func(obj, &tmp) == SUCCESS) {
423424
if (check_empty == 1) {
424425
retval = zend_is_true(&tmp);
@@ -1441,7 +1442,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
14411442
dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);
14421443

14431444
php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks);
1444-
intern->register_node_ns = 1;
1445+
intern->register_node_ns = true;
14451446

14461447
intern->dom.prop_handler = &dom_xpath_prop_handlers;
14471448

ext/dom/php_dom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern zend_module_entry dom_module_entry;
6666

6767
typedef struct _dom_xpath_object {
6868
php_dom_xpath_callbacks xpath_callbacks;
69-
int register_node_ns;
69+
bool register_node_ns;
7070
dom_object dom;
7171
} dom_xpath_object;
7272

0 commit comments

Comments
 (0)