-
Notifications
You must be signed in to change notification settings - Fork 8k
zend_builtin_functions.c: Various minor refactorings #20411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Girgias
wants to merge
7
commits into
php:master
Choose a base branch
from
Girgias:zend_built-in-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+48
−68
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e148a60
zend_builtin_functions.c: Use RETURN_THROW() for clarity
Girgias 5a748d4
zend_builtin_functions.c: use type uint32_t instead of int
Girgias ffe540b
zend_builtin_functions.c: use known string when possible
Girgias 788f26a
zend_builtin_functions.c: add const qualifiers
Girgias 35ad722
zend_builtin_functions.c: remove some variable shadowing
Girgias ac16f5c
zend_builtin_functions.c: refactor is_a_impl() to use early returns
Girgias c5b081f
zend_builtin_functions.c: use RET{URN|VAL}_BOOL() when possible
Girgias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,10 +82,10 @@ ZEND_FUNCTION(clone) | |
|
|
||
| /* clone() also exists as the ZEND_CLONE OPcode and both implementations must be kept in sync. */ | ||
|
|
||
| zend_class_entry *scope = zend_get_executed_scope(); | ||
| const zend_class_entry *scope = zend_get_executed_scope(); | ||
|
|
||
| zend_class_entry *ce = zobj->ce; | ||
| zend_function *clone = ce->clone; | ||
| const zend_class_entry *ce = zobj->ce; | ||
| const zend_function *clone = ce->clone; | ||
|
|
||
| if (UNEXPECTED(zobj->handlers->clone_obj == NULL)) { | ||
| zend_throw_error(NULL, "Trying to clone an uncloneable object of class %s", ZSTR_VAL(ce->name)); | ||
|
|
@@ -237,7 +237,7 @@ ZEND_FUNCTION(gc_status) | |
| /* {{{ Get the number of arguments that were passed to the function */ | ||
| ZEND_FUNCTION(func_num_args) | ||
| { | ||
| zend_execute_data *ex = EX(prev_execute_data); | ||
| const zend_execute_data *ex = EX(prev_execute_data); | ||
|
|
||
| ZEND_PARSE_PARAMETERS_NONE(); | ||
|
|
||
|
|
@@ -247,7 +247,7 @@ ZEND_FUNCTION(func_num_args) | |
| } | ||
|
|
||
| if (zend_forbid_dynamic_call() == FAILURE) { | ||
| RETURN_LONG(-1); | ||
| RETURN_THROWS(); | ||
| } | ||
|
|
||
| RETURN_LONG(ZEND_CALL_NUM_ARGS(ex)); | ||
|
|
@@ -501,7 +501,7 @@ ZEND_FUNCTION(error_reporting) | |
| } | ||
| /* }}} */ | ||
|
|
||
| static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */ | ||
| static bool validate_constant_array_argument(HashTable *ht, uint32_t argument_number) /* {{{ */ | ||
| { | ||
| bool ret = true; | ||
| zval *val; | ||
|
|
@@ -525,7 +525,7 @@ static bool validate_constant_array_argument(HashTable *ht, int argument_number) | |
| } | ||
| /* }}} */ | ||
|
|
||
| static void copy_constant_array(zval *dst, zval *src) /* {{{ */ | ||
| static void copy_constant_array(zval *dst, const zval *src) /* {{{ */ | ||
| { | ||
| zend_string *key; | ||
| zend_ulong idx; | ||
|
|
@@ -595,11 +595,7 @@ ZEND_FUNCTION(define) | |
| /* non persistent */ | ||
| ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT); | ||
| c.name = zend_string_copy(name); | ||
| if (zend_register_constant(&c) != NULL) { | ||
| RETURN_TRUE; | ||
| } else { | ||
| RETURN_FALSE; | ||
| } | ||
| RETURN_BOOL(zend_register_constant(&c) != NULL); | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -613,11 +609,7 @@ ZEND_FUNCTION(defined) | |
| Z_PARAM_STR(name) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| if (zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT)) { | ||
| RETURN_TRUE; | ||
| } else { | ||
| RETURN_FALSE; | ||
| } | ||
| RETURN_BOOL(zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT)); | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -631,7 +623,7 @@ ZEND_FUNCTION(get_class) | |
| } | ||
|
|
||
| if (!obj) { | ||
| zend_class_entry *scope = zend_get_executed_scope(); | ||
| const zend_class_entry *scope = zend_get_executed_scope(); | ||
|
|
||
| if (scope) { | ||
| zend_error(E_DEPRECATED, "Calling get_class() without arguments is deprecated"); | ||
|
|
@@ -652,7 +644,7 @@ ZEND_FUNCTION(get_class) | |
| /* {{{ Retrieves the "Late Static Binding" class name */ | ||
| ZEND_FUNCTION(get_called_class) | ||
| { | ||
| zend_class_entry *called_scope; | ||
| const zend_class_entry *called_scope; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_NONE(); | ||
|
|
||
|
|
@@ -697,9 +689,7 @@ static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, bool only_subclass) /* {{{ * | |
| zval *obj; | ||
| zend_string *class_name; | ||
| zend_class_entry *instance_ce; | ||
| zend_class_entry *ce; | ||
| bool allow_string = only_subclass; | ||
| bool retval; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_START(2, 3) | ||
| Z_PARAM_ZVAL(obj) | ||
|
|
@@ -726,21 +716,19 @@ static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, bool only_subclass) /* {{{ * | |
| } | ||
|
|
||
| if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) { | ||
| retval = 1; | ||
| } else { | ||
| ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); | ||
| if (!ce) { | ||
| retval = 0; | ||
| } else { | ||
| if (only_subclass && instance_ce == ce) { | ||
| retval = 0; | ||
| } else { | ||
| retval = instanceof_function(instance_ce, ce); | ||
| } | ||
| } | ||
| RETURN_TRUE; | ||
| } | ||
|
|
||
| const zend_class_entry *ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD); | ||
| if (!ce) { | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| if (only_subclass && instance_ce == ce) { | ||
| RETURN_FALSE; | ||
| } | ||
|
|
||
| RETURN_BOOL(retval); | ||
| RETURN_BOOL(instanceof_function(instance_ce, ce)); | ||
| } | ||
| /* }}} */ | ||
|
|
||
|
|
@@ -759,7 +747,7 @@ ZEND_FUNCTION(is_a) | |
| /* }}} */ | ||
|
|
||
| /* {{{ add_class_vars */ | ||
| static void add_class_vars(zend_class_entry *scope, zend_class_entry *ce, bool statics, zval *return_value) | ||
| static void add_class_vars(const zend_class_entry *scope, zend_class_entry *ce, bool statics, zval *return_value) | ||
| { | ||
| zend_property_info *prop_info; | ||
| zval *prop, prop_copy; | ||
|
|
@@ -810,7 +798,7 @@ static void add_class_vars(zend_class_entry *scope, zend_class_entry *ce, bool s | |
| /* {{{ Returns an array of default properties of the class. */ | ||
| ZEND_FUNCTION(get_class_vars) | ||
| { | ||
| zend_class_entry *ce = NULL, *scope; | ||
| zend_class_entry *ce = NULL; | ||
|
|
||
| if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce) == FAILURE) { | ||
| RETURN_THROWS(); | ||
|
|
@@ -823,7 +811,7 @@ ZEND_FUNCTION(get_class_vars) | |
| } | ||
| } | ||
|
|
||
| scope = zend_get_executed_scope(); | ||
| const zend_class_entry *scope = zend_get_executed_scope(); | ||
| add_class_vars(scope, ce, false, return_value); | ||
| add_class_vars(scope, ce, true, return_value); | ||
| } | ||
|
|
@@ -949,15 +937,14 @@ ZEND_FUNCTION(get_class_methods) | |
| { | ||
| zval method_name; | ||
| zend_class_entry *ce = NULL; | ||
| zend_class_entry *scope; | ||
| zend_function *mptr; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_START(1, 1) | ||
| Z_PARAM_OBJ_OR_CLASS_NAME(ce) | ||
| ZEND_PARSE_PARAMETERS_END(); | ||
|
|
||
| array_init(return_value); | ||
| scope = zend_get_executed_scope(); | ||
| const zend_class_entry *scope = zend_get_executed_scope(); | ||
|
|
||
| ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, mptr) { | ||
| if (zend_check_method_accessible(mptr, scope)) { | ||
|
|
@@ -1032,10 +1019,9 @@ ZEND_FUNCTION(method_exists) | |
| } | ||
| /* }}} */ | ||
|
|
||
| static void _property_exists(zval *return_value, zval *object, zend_string *property) | ||
| static void _property_exists(zval *return_value, const zval *object, zend_string *property) | ||
| { | ||
| zend_class_entry *ce; | ||
| zend_property_info *property_info; | ||
|
|
||
| if (Z_TYPE_P(object) == IS_STRING) { | ||
| ce = zend_lookup_class(Z_STR_P(object)); | ||
|
|
@@ -1049,18 +1035,17 @@ static void _property_exists(zval *return_value, zval *object, zend_string *prop | |
| RETURN_THROWS(); | ||
| } | ||
|
|
||
| property_info = zend_hash_find_ptr(&ce->properties_info, property); | ||
| const zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property); | ||
| if (property_info != NULL | ||
| && (!(property_info->flags & ZEND_ACC_PRIVATE) | ||
| || property_info->ce == ce)) { | ||
| RETURN_TRUE; | ||
| } | ||
|
|
||
| if (Z_TYPE_P(object) == IS_OBJECT && | ||
| Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, ZEND_PROPERTY_EXISTS, NULL)) { | ||
| RETURN_TRUE; | ||
| } | ||
| RETURN_FALSE; | ||
| RETURN_BOOL( | ||
| Z_TYPE_P(object) == IS_OBJECT && | ||
| Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, ZEND_PROPERTY_EXISTS, NULL) | ||
| ); | ||
| } | ||
|
|
||
| /* {{{ Checks if the object or class has a property */ | ||
|
|
@@ -1120,11 +1105,7 @@ static inline void _class_exists_impl(zval *return_value, zend_string *name, boo | |
| ce = zend_lookup_class(name); | ||
| } | ||
|
|
||
| if (ce) { | ||
| RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags)); | ||
| } else { | ||
| RETURN_FALSE; | ||
| } | ||
| RETURN_BOOL(ce && ((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags)); | ||
| } | ||
| /* {{{ */ | ||
|
|
||
|
|
@@ -1430,15 +1411,14 @@ static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int fla | |
| { | ||
| zend_string *key; | ||
| zval *zv; | ||
| zend_class_entry *ce; | ||
|
|
||
| ZEND_PARSE_PARAMETERS_NONE(); | ||
|
|
||
| array_init(return_value); | ||
| zend_hash_real_init_packed(Z_ARRVAL_P(return_value)); | ||
| ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) { | ||
| ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) { | ||
| ce = Z_PTR_P(zv); | ||
| const zend_class_entry *ce = Z_PTR_P(zv); | ||
| if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags | ||
| && key | ||
| && ZSTR_VAL(key)[0] != 0) { | ||
|
|
@@ -1556,7 +1536,7 @@ ZEND_FUNCTION(get_resource_type) | |
| if (resource_type) { | ||
| RETURN_STRING(resource_type); | ||
| } else { | ||
| RETURN_STRING("Unknown"); | ||
| RETURN_STR(ZSTR_KNOWN(ZEND_STR_UNKNOWN_CAPITALIZED)); | ||
| } | ||
| } | ||
| /* }}} */ | ||
|
|
@@ -1893,7 +1873,7 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int | |
| zend_execute_data *call, *last_call = NULL; | ||
| zend_object *object; | ||
| bool fake_frame = false; | ||
| int lineno, frameno = 0; | ||
| int frameno = 0; | ||
| zend_function *func; | ||
| zend_string *filename; | ||
| zend_string *include_filename = NULL; | ||
|
|
@@ -1914,12 +1894,12 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int | |
| EG(filename_override) = NULL; | ||
| EG(lineno_override) = -1; | ||
|
|
||
| zend_string *filename = zend_get_executed_filename_ex(); | ||
| zend_long lineno = zend_get_executed_lineno(); | ||
| if (filename && (!zend_string_equals(filename, filename_override) || lineno != lineno_override)) { | ||
| zend_string *executed_filename = zend_get_executed_filename_ex(); | ||
| uint32_t lineno = zend_get_executed_lineno(); | ||
| if (executed_filename && (!zend_string_equals(executed_filename, filename_override) || lineno != lineno_override)) { | ||
| stack_frame = zend_new_array(8); | ||
| zend_hash_real_init_mixed(stack_frame); | ||
| ZVAL_STR_COPY(&tmp, filename); | ||
| ZVAL_STR_COPY(&tmp, executed_filename); | ||
| _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1); | ||
| ZVAL_LONG(&tmp, lineno); | ||
| _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1); | ||
|
|
@@ -1982,19 +1962,21 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int | |
| zval *arg = zend_get_zval_ptr(op_data, op_data->op1_type, &op_data->op1, call); | ||
| if (Z_TYPE_P(arg) == IS_UNDEF) goto not_frameless_call; | ||
| } | ||
| zend_function *func = ZEND_FLF_FUNC(opline); | ||
| zend_function *frameless_func = ZEND_FLF_FUNC(opline); | ||
| /* Assume frameless functions are not recursive with themselves. | ||
| * This condition may be true when observers are enabled: | ||
| * Observers will put a call frame on top of the frameless opcode. */ | ||
| if (last_call && last_call->func == func) { | ||
| if (last_call && last_call->func == frameless_func) { | ||
| goto not_frameless_call; | ||
| } | ||
| stack_frame = zend_new_array(8); | ||
| zend_hash_real_init_mixed(stack_frame); | ||
| ZVAL_STR_COPY(&tmp, func->common.function_name); | ||
| ZVAL_STR_COPY(&tmp, frameless_func->common.function_name); | ||
| _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1); | ||
| /* Steal file and line from the previous frame. */ | ||
| if (call->func && ZEND_USER_CODE(call->func->common.type)) { | ||
| uint32_t lineno; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're shadowing here, you don't need this declaration |
||
|
|
||
| filename = call->func->op_array.filename; | ||
| if (call->opline->opcode == ZEND_HANDLE_EXCEPTION) { | ||
| if (EG(opline_before_exception)) { | ||
|
|
@@ -2046,6 +2028,8 @@ ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int | |
| zend_hash_real_init_mixed(stack_frame); | ||
|
|
||
| if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) { | ||
| uint32_t lineno; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're shadowing here, you don't need this declaration |
||
|
|
||
| filename = prev->func->op_array.filename; | ||
| if (prev->opline->opcode == ZEND_HANDLE_EXCEPTION) { | ||
| if (EG(opline_before_exception)) { | ||
|
|
@@ -2230,11 +2214,7 @@ ZEND_FUNCTION(extension_loaded) | |
| } | ||
|
|
||
| lcname = zend_string_tolower(extension_name); | ||
| if (zend_hash_exists(&module_registry, lcname)) { | ||
| RETVAL_TRUE; | ||
| } else { | ||
| RETVAL_FALSE; | ||
| } | ||
| RETVAL_BOOL(zend_hash_exists(&module_registry, lcname)); | ||
| zend_string_release_ex(lcname, 0); | ||
| } | ||
| /* }}} */ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could even use RETURN_INTERNED_STR