summaryrefslogtreecommitdiff
path: root/ext/spl/php_spl.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/spl/php_spl.c')
-rw-r--r--ext/spl/php_spl.c533
1 files changed, 248 insertions, 285 deletions
diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c
index 6b886b7ef3..917411ab6f 100644
--- a/ext/spl/php_spl.c
+++ b/ext/spl/php_spl.c
@@ -1,6 +1,6 @@
/*
+----------------------------------------------------------------------+
- | PHP Version 5 |
+ | PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2015 The PHP Group |
+----------------------------------------------------------------------+
@@ -56,35 +56,30 @@ ZEND_DECLARE_MODULE_GLOBALS(spl)
static PHP_GINIT_FUNCTION(spl)
{
spl_globals->autoload_extensions = NULL;
- spl_globals->autoload_extensions_len = 0;
spl_globals->autoload_functions = NULL;
spl_globals->autoload_running = 0;
}
/* }}} */
-static zend_class_entry * spl_find_ce_by_name(char *name, int len, zend_bool autoload TSRMLS_DC)
+static zend_class_entry * spl_find_ce_by_name(zend_string *name, zend_bool autoload)
{
- zend_class_entry **ce;
- int found;
+ zend_class_entry *ce;
if (!autoload) {
- char *lc_name;
- ALLOCA_FLAG(use_heap)
-
- lc_name = do_alloca(len + 1, use_heap);
- zend_str_tolower_copy(lc_name, name, len);
+ zend_string *lc_name = zend_string_alloc(name->len, 0);
+ zend_str_tolower_copy(lc_name->val, name->val, name->len);
- found = zend_hash_find(EG(class_table), lc_name, len +1, (void **) &ce);
- free_alloca(lc_name, use_heap);
+ ce = zend_hash_find_ptr(EG(class_table), lc_name);
+ zend_string_free(lc_name);
} else {
- found = zend_lookup_class(name, len, &ce TSRMLS_CC);
+ ce = zend_lookup_class(name);
}
- if (found != SUCCESS) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class %s does not exist%s", name, autoload ? " and could not be loaded" : "");
+ if (ce == NULL) {
+ php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", name->val, autoload ? " and could not be loaded" : "");
return NULL;
}
-
- return *ce;
+
+ return ce;
}
/* {{{ proto array class_parents(object instance [, boolean autoload = true])
@@ -95,27 +90,27 @@ PHP_FUNCTION(class_parents)
zend_class_entry *parent_class, *ce;
zend_bool autoload = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
RETURN_FALSE;
}
-
+
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "object or string expected");
+ php_error_docref(NULL, E_WARNING, "object or string expected");
RETURN_FALSE;
}
-
+
if (Z_TYPE_P(obj) == IS_STRING) {
- if (NULL == (ce = spl_find_ce_by_name(Z_STRVAL_P(obj), Z_STRLEN_P(obj), autoload TSRMLS_CC))) {
+ if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
RETURN_FALSE;
}
} else {
ce = Z_OBJCE_P(obj);
}
-
+
array_init(return_value);
parent_class = ce->parent;
while (parent_class) {
- spl_add_class_name(return_value, parent_class, 0, 0 TSRMLS_CC);
+ spl_add_class_name(return_value, parent_class, 0, 0);
parent_class = parent_class->parent;
}
}
@@ -128,25 +123,25 @@ PHP_FUNCTION(class_implements)
zval *obj;
zend_bool autoload = 1;
zend_class_entry *ce;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "object or string expected");
+ php_error_docref(NULL, E_WARNING, "object or string expected");
RETURN_FALSE;
}
-
+
if (Z_TYPE_P(obj) == IS_STRING) {
- if (NULL == (ce = spl_find_ce_by_name(Z_STRVAL_P(obj), Z_STRLEN_P(obj), autoload TSRMLS_CC))) {
+ if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
RETURN_FALSE;
}
} else {
ce = Z_OBJCE_P(obj);
}
-
+
array_init(return_value);
- spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE TSRMLS_CC);
+ spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE);
}
/* }}} */
@@ -157,30 +152,30 @@ PHP_FUNCTION(class_uses)
zval *obj;
zend_bool autoload = 1;
zend_class_entry *ce;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &obj, &autoload) == FAILURE) {
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
RETURN_FALSE;
}
if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "object or string expected");
+ php_error_docref(NULL, E_WARNING, "object or string expected");
RETURN_FALSE;
}
-
+
if (Z_TYPE_P(obj) == IS_STRING) {
- if (NULL == (ce = spl_find_ce_by_name(Z_STRVAL_P(obj), Z_STRLEN_P(obj), autoload TSRMLS_CC))) {
+ if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
RETURN_FALSE;
}
} else {
ce = Z_OBJCE_P(obj);
}
-
+
array_init(return_value);
- spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT TSRMLS_CC);
+ spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT);
}
/* }}} */
#define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
- spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags TSRMLS_CC)
+ spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags)
#define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
@@ -245,66 +240,63 @@ PHP_FUNCTION(class_uses)
PHP_FUNCTION(spl_classes)
{
array_init(return_value);
-
+
SPL_LIST_CLASSES(return_value, 0, 0, 0)
}
/* }}} */
-static int spl_autoload(const char *class_name, const char * lc_name, int class_name_len, const char * file_extension TSRMLS_DC) /* {{{ */
+static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
{
char *class_file;
int class_file_len;
- int dummy = 1;
+ zval dummy;
zend_file_handle file_handle;
zend_op_array *new_op_array;
- zval *result = NULL;
+ zval result;
int ret;
- class_file_len = spprintf(&class_file, 0, "%s%s", lc_name, file_extension);
+ class_file_len = (int)spprintf(&class_file, 0, "%s%.*s", lc_name->val, ext_len, ext);
#if DEFAULT_SLASH != '\\'
{
char *ptr = class_file;
char *end = ptr + class_file_len;
-
+
while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
*ptr = DEFAULT_SLASH;
}
}
#endif
- ret = php_stream_open_for_zend_ex(class_file, &file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE TSRMLS_CC);
+ ret = php_stream_open_for_zend_ex(class_file, &file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
if (ret == SUCCESS) {
+ zend_string *opened_path;
if (!file_handle.opened_path) {
- file_handle.opened_path = estrndup(class_file, class_file_len);
+ file_handle.opened_path = zend_string_init(class_file, class_file_len, 0);
}
- if (zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) {
- new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);
- zend_destroy_file_handle(&file_handle TSRMLS_CC);
+ opened_path = zend_string_copy(file_handle.opened_path);
+ ZVAL_NULL(&dummy);
+ if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
+ new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
+ zend_destroy_file_handle(&file_handle);
} else {
new_op_array = NULL;
- zend_file_handle_dtor(&file_handle TSRMLS_CC);
+ zend_file_handle_dtor(&file_handle);
}
+ zend_string_release(opened_path);
if (new_op_array) {
- EG(return_value_ptr_ptr) = &result;
- EG(active_op_array) = new_op_array;
- if (!EG(active_symbol_table)) {
- zend_rebuild_symbol_table(TSRMLS_C);
- }
+ ZVAL_UNDEF(&result);
+ zend_execute(new_op_array, &result);
- zend_execute(new_op_array TSRMLS_CC);
-
- destroy_op_array(new_op_array TSRMLS_CC);
+ destroy_op_array(new_op_array);
efree(new_op_array);
if (!EG(exception)) {
- if (EG(return_value_ptr_ptr)) {
- zval_ptr_dtor(EG(return_value_ptr_ptr));
- }
+ zval_ptr_dtor(&result);
}
efree(class_file);
- return zend_hash_exists(EG(class_table), (char*)lc_name, class_name_len+1);
+ return zend_hash_exists(EG(class_table), lc_name);
}
}
efree(class_file);
@@ -315,53 +307,56 @@ static int spl_autoload(const char *class_name, const char * lc_name, int class_
Default implementation for __autoload() */
PHP_FUNCTION(spl_autoload)
{
- char *class_name, *lc_name, *file_exts = SPL_G(autoload_extensions);
- int class_name_len, file_exts_len = SPL_G(autoload_extensions_len), found = 0;
- char *copy, *pos1, *pos2;
- zval **original_return_value = EG(return_value_ptr_ptr);
- zend_op **original_opline_ptr = EG(opline_ptr);
- zend_op_array *original_active_op_array = EG(active_op_array);
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &class_name, &class_name_len, &file_exts, &file_exts_len) == FAILURE) {
+ int found = 0, pos_len, pos1_len;
+ char *pos, *pos1;
+ zend_string *class_name, *lc_name, *file_exts = SPL_G(autoload_extensions);
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S", &class_name, &file_exts) == FAILURE) {
RETURN_FALSE;
}
if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
- copy = pos1 = estrndup(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS)-1);
+ pos = SPL_DEFAULT_FILE_EXTENSIONS;
+ pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
} else {
- copy = pos1 = estrndup(file_exts, file_exts_len);
- }
- lc_name = zend_str_tolower_dup(class_name, class_name_len);
- while(pos1 && *pos1 && !EG(exception)) {
- EG(return_value_ptr_ptr) = original_return_value;
- EG(opline_ptr) = original_opline_ptr;
- EG(active_op_array) = original_active_op_array;
- pos2 = strchr(pos1, ',');
- if (pos2) *pos2 = '\0';
- if (spl_autoload(class_name, lc_name, class_name_len, pos1 TSRMLS_CC)) {
+ pos = file_exts->val;
+ pos_len = (int)file_exts->len;
+ }
+
+ lc_name = zend_string_alloc(class_name->len, 0);
+ zend_str_tolower_copy(lc_name->val, class_name->val, class_name->len);
+ while (pos && *pos && !EG(exception)) {
+ pos1 = strchr(pos, ',');
+ if (pos1) {
+ pos1_len = (int)(pos1 - pos);
+ } else {
+ pos1_len = pos_len;
+ }
+ if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
found = 1;
break; /* loaded */
}
- pos1 = pos2 ? pos2 + 1 : NULL;
- }
- efree(lc_name);
- if (copy) {
- efree(copy);
+ pos = pos1 ? pos1 + 1 : NULL;
+ pos_len = pos1? pos_len - pos1_len - 1 : 0;
}
-
- EG(return_value_ptr_ptr) = original_return_value;
- EG(opline_ptr) = original_opline_ptr;
- EG(active_op_array) = original_active_op_array;
+ zend_string_free(lc_name);
if (!found && !SPL_G(autoload_running)) {
/* For internal errors, we generate E_ERROR, for direct calls an exception is thrown.
* The "scope" is determined by an opcode, if it is ZEND_FETCH_CLASS we know function was called indirectly by
* the Zend engine.
*/
- if (active_opline->opcode != ZEND_FETCH_CLASS) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Class %s could not be loaded", class_name);
+ zend_execute_data *ex = EX(prev_execute_data);
+
+ while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
+ ex = ex->prev_execute_data;
+ }
+ if (ex &&
+ ex->opline->opcode != ZEND_FETCH_CLASS &&
+ ex->opline->opcode != ZEND_NEW) {
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Class %s could not be loaded", class_name->val);
} else {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s could not be loaded", class_name);
+ php_error_docref(NULL, E_ERROR, "Class %s could not be loaded", class_name->val);
}
}
} /* }}} */
@@ -370,42 +365,43 @@ PHP_FUNCTION(spl_autoload)
Register and return default file extensions for spl_autoload */
PHP_FUNCTION(spl_autoload_extensions)
{
- char *file_exts = NULL;
- int file_exts_len;
+ zend_string *file_exts = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &file_exts, &file_exts_len) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S", &file_exts) == FAILURE) {
return;
}
if (file_exts) {
if (SPL_G(autoload_extensions)) {
- efree(SPL_G(autoload_extensions));
+ zend_string_release(SPL_G(autoload_extensions));
}
- SPL_G(autoload_extensions) = estrndup(file_exts, file_exts_len);
- SPL_G(autoload_extensions_len) = file_exts_len;
+ SPL_G(autoload_extensions) = zend_string_copy(file_exts);
}
if (SPL_G(autoload_extensions) == NULL) {
- RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1, 1);
+ RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
} else {
- RETURN_STRINGL(SPL_G(autoload_extensions), SPL_G(autoload_extensions_len), 1);
+ zend_string_addref(SPL_G(autoload_extensions));
+ RETURN_STR(SPL_G(autoload_extensions));
}
} /* }}} */
typedef struct {
zend_function *func_ptr;
- zval *obj;
- zval *closure;
+ zval obj;
+ zval closure;
zend_class_entry *ce;
} autoload_func_info;
-static void autoload_func_info_dtor(autoload_func_info *alfi)
+static void autoload_func_info_dtor(zval *element)
{
- if (alfi->obj) {
+ autoload_func_info *alfi = (autoload_func_info*)Z_PTR_P(element);
+ if (!Z_ISUNDEF(alfi->obj)) {
zval_ptr_dtor(&alfi->obj);
}
- if (alfi->closure) {
+ if (!Z_ISUNDEF(alfi->closure)) {
zval_ptr_dtor(&alfi->closure);
}
+ efree(alfi);
}
/* {{{ proto void spl_autoload_call(string class_name)
@@ -413,39 +409,31 @@ static void autoload_func_info_dtor(autoload_func_info *alfi)
PHP_FUNCTION(spl_autoload_call)
{
zval *class_name, *retval = NULL;
- int class_name_len;
- char *func_name, *lc_name;
- uint func_name_len;
- ulong dummy;
- HashPosition function_pos;
+ zend_string *lc_name, *func_name;
autoload_func_info *alfi;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &class_name) == FAILURE || Z_TYPE_P(class_name) != IS_STRING) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &class_name) == FAILURE || Z_TYPE_P(class_name) != IS_STRING) {
return;
}
if (SPL_G(autoload_functions)) {
int l_autoload_running = SPL_G(autoload_running);
SPL_G(autoload_running) = 1;
- class_name_len = Z_STRLEN_P(class_name);
- lc_name = zend_str_tolower_dup(Z_STRVAL_P(class_name), class_name_len);
- zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &function_pos);
- while(zend_hash_has_more_elements_ex(SPL_G(autoload_functions), &function_pos) == SUCCESS) {
- zend_hash_get_current_key_ex(SPL_G(autoload_functions), &func_name, &func_name_len, &dummy, 0, &function_pos);
- zend_hash_get_current_data_ex(SPL_G(autoload_functions), (void **) &alfi, &function_pos);
- zend_call_method(alfi->obj ? &alfi->obj : NULL, alfi->ce, &alfi->func_ptr, func_name, func_name_len, &retval, 1, class_name, NULL TSRMLS_CC);
- zend_exception_save(TSRMLS_C);
+ lc_name = zend_string_alloc(Z_STRLEN_P(class_name), 0);
+ zend_str_tolower_copy(lc_name->val, Z_STRVAL_P(class_name), Z_STRLEN_P(class_name));
+ ZEND_HASH_FOREACH_STR_KEY_PTR(SPL_G(autoload_functions), func_name, alfi) {
+ zend_call_method(Z_ISUNDEF(alfi->obj)? NULL : &alfi->obj, alfi->ce, &alfi->func_ptr, func_name->val, func_name->len, retval, 1, class_name, NULL);
+ zend_exception_save();
if (retval) {
- zval_ptr_dtor(&retval);
+ zval_ptr_dtor(retval);
retval = NULL;
}
- if (zend_hash_exists(EG(class_table), lc_name, class_name_len + 1)) {
+ if (zend_hash_exists(EG(class_table), lc_name)) {
break;
}
- zend_hash_move_forward_ex(SPL_G(autoload_functions), &function_pos);
- }
- zend_exception_restore(TSRMLS_C);
- efree(lc_name);
+ } ZEND_HASH_FOREACH_END();
+ zend_exception_restore();
+ zend_string_free(lc_name);
SPL_G(autoload_running) = l_autoload_running;
} else {
/* do not use or overwrite &EG(autoload_func) here */
@@ -453,157 +441,154 @@ PHP_FUNCTION(spl_autoload_call)
}
} /* }}} */
-#define HT_MOVE_TAIL_TO_HEAD(ht) \
- (ht)->pListTail->pListNext = (ht)->pListHead; \
- (ht)->pListHead = (ht)->pListTail; \
- (ht)->pListTail = (ht)->pListHead->pListLast; \
- (ht)->pListHead->pListNext->pListLast = (ht)->pListHead;\
- (ht)->pListTail->pListNext = NULL; \
- (ht)->pListHead->pListLast = NULL;
+#define HT_MOVE_TAIL_TO_HEAD(ht) \
+ do { \
+ Bucket tmp = (ht)->arData[(ht)->nNumUsed-1]; \
+ memmove((ht)->arData + 1, (ht)->arData, \
+ sizeof(Bucket) * ((ht)->nNumUsed - 1)); \
+ (ht)->arData[0] = tmp; \
+ zend_hash_rehash(ht); \
+ } while (0)
/* {{{ proto bool spl_autoload_register([mixed autoload_function = "spl_autoload" [, throw = true [, prepend]]])
Register given function as __autoload() implementation */
PHP_FUNCTION(spl_autoload_register)
{
- char *func_name, *error = NULL;
- int func_name_len;
- char *lc_name = NULL;
+ zend_string *func_name;
+ char *error = NULL;
+ zend_string *lc_name;
zval *zcallable = NULL;
zend_bool do_throw = 1;
zend_bool prepend = 0;
zend_function *spl_func_ptr;
autoload_func_info alfi;
- zval *obj_ptr;
+ zend_object *obj_ptr;
zend_fcall_info_cache fcc;
- if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|zbb", &zcallable, &do_throw, &prepend) == FAILURE) {
+ if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|zbb", &zcallable, &do_throw, &prepend) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS()) {
- if (Z_TYPE_P(zcallable) == IS_STRING) {
- if (Z_STRLEN_P(zcallable) == sizeof("spl_autoload_call") - 1) {
- if (!zend_binary_strcasecmp(Z_STRVAL_P(zcallable), sizeof("spl_autoload_call"), "spl_autoload_call", sizeof("spl_autoload_call"))) {
- if (do_throw) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Function spl_autoload_call() cannot be registered");
- }
- RETURN_FALSE;
- }
- }
- }
-
- if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_STRICT, &func_name, &func_name_len, &fcc, &error TSRMLS_CC)) {
+ if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_STRICT, &func_name, &fcc, &error)) {
alfi.ce = fcc.calling_scope;
alfi.func_ptr = fcc.function_handler;
- obj_ptr = fcc.object_ptr;
+ obj_ptr = fcc.object;
if (Z_TYPE_P(zcallable) == IS_ARRAY) {
if (!obj_ptr && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (do_throw) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array specifies a non static method but no object (%s)", error);
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array specifies a non static method but no object (%s)", error);
}
if (error) {
efree(error);
}
- efree(func_name);
+ zend_string_release(func_name);
RETURN_FALSE;
- }
- else if (do_throw) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
+ } else if (do_throw) {
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
}
if (error) {
efree(error);
}
- efree(func_name);
+ zend_string_release(func_name);
RETURN_FALSE;
} else if (Z_TYPE_P(zcallable) == IS_STRING) {
if (do_throw) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Function '%s' not %s (%s)", func_name, alfi.func_ptr ? "callable" : "found", error);
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Function '%s' not %s (%s)", func_name->val, alfi.func_ptr ? "callable" : "found", error);
}
if (error) {
efree(error);
}
- efree(func_name);
+ zend_string_release(func_name);
RETURN_FALSE;
} else {
if (do_throw) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Illegal value passed (%s)", error);
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Illegal value passed (%s)", error);
}
if (error) {
efree(error);
}
- efree(func_name);
+ zend_string_release(func_name);
RETURN_FALSE;
}
+ } else if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
+ fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
+ if (do_throw) {
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Function spl_autoload_call() cannot be registered");
+ }
+ if (error) {
+ efree(error);
+ }
+ zend_string_release(func_name);
+ RETURN_FALSE;
}
- alfi.closure = NULL;
alfi.ce = fcc.calling_scope;
alfi.func_ptr = fcc.function_handler;
- obj_ptr = fcc.object_ptr;
+ obj_ptr = fcc.object;
if (error) {
efree(error);
}
-
- lc_name = safe_emalloc(func_name_len, 1, sizeof(long) + 1);
- zend_str_tolower_copy(lc_name, func_name, func_name_len);
- efree(func_name);
if (Z_TYPE_P(zcallable) == IS_OBJECT) {
- alfi.closure = zcallable;
- Z_ADDREF_P(zcallable);
-
- lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
- memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(zcallable),
- sizeof(zend_object_handle));
- func_name_len += sizeof(zend_object_handle);
- lc_name[func_name_len] = '\0';
+ ZVAL_COPY(&alfi.closure, zcallable);
+
+ lc_name = zend_string_alloc(func_name->len + sizeof(uint32_t), 0);
+ zend_str_tolower_copy(lc_name->val, func_name->val, func_name->len);
+ memcpy(lc_name->val + func_name->len, &Z_OBJ_HANDLE_P(zcallable), sizeof(uint32_t));
+ lc_name->val[lc_name->len] = '\0';
+ } else {
+ ZVAL_UNDEF(&alfi.closure);
+ lc_name = zend_string_alloc(func_name->len, 0);
+ zend_str_tolower_copy(lc_name->val, func_name->val, func_name->len);
}
+ zend_string_release(func_name);
- if (SPL_G(autoload_functions) && zend_hash_exists(SPL_G(autoload_functions), (char*)lc_name, func_name_len+1)) {
- if (alfi.closure) {
- Z_DELREF_P(zcallable);
+ if (SPL_G(autoload_functions) && zend_hash_exists(SPL_G(autoload_functions), lc_name)) {
+ if (!Z_ISUNDEF(alfi.closure)) {
+ Z_DELREF_P(&alfi.closure);
}
goto skip;
}
if (obj_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
/* add object id to the hash to ensure uniqueness, for more reference look at bug #40091 */
- lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
- memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_object_handle));
- func_name_len += sizeof(zend_object_handle);
- lc_name[func_name_len] = '\0';
- alfi.obj = obj_ptr;
- Z_ADDREF_P(alfi.obj);
+ lc_name = zend_string_extend(lc_name, lc_name->len + sizeof(uint32_t), 0);
+ memcpy(lc_name->val + lc_name->len - sizeof(uint32_t), &obj_ptr->handle, sizeof(uint32_t));
+ lc_name->val[lc_name->len] = '\0';
+ ZVAL_OBJ(&alfi.obj, obj_ptr);
+ Z_ADDREF(alfi.obj);
} else {
- alfi.obj = NULL;
+ ZVAL_UNDEF(&alfi.obj);
}
if (!SPL_G(autoload_functions)) {
ALLOC_HASHTABLE(SPL_G(autoload_functions));
- zend_hash_init(SPL_G(autoload_functions), 1, NULL, (dtor_func_t) autoload_func_info_dtor, 0);
+ zend_hash_init(SPL_G(autoload_functions), 1, NULL, autoload_func_info_dtor, 0);
}
- zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &spl_func_ptr);
+ spl_func_ptr = zend_hash_str_find_ptr(EG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
if (EG(autoload_func) == spl_func_ptr) { /* registered already, so we insert that first */
autoload_func_info spl_alfi;
spl_alfi.func_ptr = spl_func_ptr;
- spl_alfi.obj = NULL;
+ ZVAL_UNDEF(&spl_alfi.obj);
+ ZVAL_UNDEF(&spl_alfi.closure);
spl_alfi.ce = NULL;
- spl_alfi.closure = NULL;
- zend_hash_add(SPL_G(autoload_functions), "spl_autoload", sizeof("spl_autoload"), &spl_alfi, sizeof(autoload_func_info), NULL);
+ zend_hash_str_add_mem(SPL_G(autoload_functions), "spl_autoload", sizeof("spl_autoload") - 1,
+ &spl_alfi, sizeof(autoload_func_info));
if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
/* Move the newly created element to the head of the hashtable */
HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
}
}
- if (zend_hash_add(SPL_G(autoload_functions), lc_name, func_name_len+1, &alfi.func_ptr, sizeof(autoload_func_info), NULL) == FAILURE) {
+ if (zend_hash_add_mem(SPL_G(autoload_functions), lc_name, &alfi, sizeof(autoload_func_info)) == NULL) {
if (obj_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
- Z_DELREF_P(alfi.obj);
- }
- if (alfi.closure) {
- Z_DELREF_P(alfi.closure);
+ Z_DELREF(alfi.obj);
+ }
+ if (!Z_ISUNDEF(alfi.closure)) {
+ Z_DELREF(alfi.closure);
}
}
if (prepend && SPL_G(autoload_functions)->nNumOfElements > 1) {
@@ -611,14 +596,15 @@ PHP_FUNCTION(spl_autoload_register)
HT_MOVE_TAIL_TO_HEAD(SPL_G(autoload_functions));
}
skip:
- efree(lc_name);
+ zend_string_release(lc_name);
}
if (SPL_G(autoload_functions)) {
- zend_hash_find(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call"), (void **) &EG(autoload_func));
+ EG(autoload_func) = zend_hash_str_find_ptr(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call") - 1);
} else {
- zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &EG(autoload_func));
+ EG(autoload_func) = zend_hash_str_find_ptr(EG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
}
+
RETURN_TRUE;
} /* }}} */
@@ -626,48 +612,47 @@ skip:
Unregister given function as __autoload() implementation */
PHP_FUNCTION(spl_autoload_unregister)
{
- char *func_name, *error = NULL;
- int func_name_len;
- char *lc_name = NULL;
+ zend_string *func_name = NULL;
+ char *error = NULL;
+ zend_string *lc_name;
zval *zcallable;
int success = FAILURE;
zend_function *spl_func_ptr;
- zval *obj_ptr;
+ zend_object *obj_ptr;
zend_fcall_info_cache fcc;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcallable) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcallable) == FAILURE) {
return;
}
- if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_CHECK_SYNTAX_ONLY, &func_name, &func_name_len, &fcc, &error TSRMLS_CC)) {
- zend_throw_exception_ex(spl_ce_LogicException, 0 TSRMLS_CC, "Unable to unregister invalid function (%s)", error);
+ if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_CHECK_SYNTAX_ONLY, &func_name, &fcc, &error)) {
+ zend_throw_exception_ex(spl_ce_LogicException, 0, "Unable to unregister invalid function (%s)", error);
if (error) {
efree(error);
}
if (func_name) {
- efree(func_name);
+ zend_string_release(func_name);
}
RETURN_FALSE;
}
- obj_ptr = fcc.object_ptr;
+ obj_ptr = fcc.object;
if (error) {
efree(error);
}
- lc_name = safe_emalloc(func_name_len, 1, sizeof(long) + 1);
- zend_str_tolower_copy(lc_name, func_name, func_name_len);
- efree(func_name);
-
if (Z_TYPE_P(zcallable) == IS_OBJECT) {
- lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
- memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(zcallable),
- sizeof(zend_object_handle));
- func_name_len += sizeof(zend_object_handle);
- lc_name[func_name_len] = '\0';
+ lc_name = zend_string_alloc(func_name->len + sizeof(uint32_t), 0);
+ zend_str_tolower_copy(lc_name->val, func_name->val, func_name->len);
+ memcpy(lc_name->val + func_name->len, &Z_OBJ_HANDLE_P(zcallable), sizeof(uint32_t));
+ lc_name->val[lc_name->len] = '\0';
+ } else {
+ lc_name = zend_string_alloc(func_name->len, 0);
+ zend_str_tolower_copy(lc_name->val, func_name->val, func_name->len);
}
+ zend_string_release(func_name);
if (SPL_G(autoload_functions)) {
- if (func_name_len == sizeof("spl_autoload_call")-1 && !strcmp(lc_name, "spl_autoload_call")) {
+ if (lc_name->len == sizeof("spl_autoload_call") - 1 && !strcmp(lc_name->val, "spl_autoload_call")) {
/* remove all */
zend_hash_destroy(SPL_G(autoload_functions));
FREE_HASHTABLE(SPL_G(autoload_functions));
@@ -676,18 +661,17 @@ PHP_FUNCTION(spl_autoload_unregister)
success = SUCCESS;
} else {
/* remove specific */
- success = zend_hash_del(SPL_G(autoload_functions), lc_name, func_name_len+1);
+ success = zend_hash_del(SPL_G(autoload_functions), lc_name);
if (success != SUCCESS && obj_ptr) {
- lc_name = erealloc(lc_name, func_name_len + 2 + sizeof(zend_object_handle));
- memcpy(lc_name + func_name_len, &Z_OBJ_HANDLE_P(obj_ptr), sizeof(zend_object_handle));
- func_name_len += sizeof(zend_object_handle);
- lc_name[func_name_len] = '\0';
- success = zend_hash_del(SPL_G(autoload_functions), lc_name, func_name_len+1);
+ lc_name = zend_string_extend(lc_name, lc_name->len + sizeof(uint32_t), 0);
+ memcpy(lc_name->val + lc_name->len - sizeof(uint32_t), &obj_ptr->handle, sizeof(uint32_t));
+ lc_name->val[lc_name->len] = '\0';
+ success = zend_hash_del(SPL_G(autoload_functions), lc_name);
}
}
- } else if (func_name_len == sizeof("spl_autoload")-1 && !strcmp(lc_name, "spl_autoload")) {
+ } else if (lc_name->len == sizeof("spl_autoload")-1 && !strcmp(lc_name->val, "spl_autoload")) {
/* register single spl_autoload() */
- zend_hash_find(EG(function_table), "spl_autoload", sizeof("spl_autoload"), (void **) &spl_func_ptr);
+ spl_func_ptr = zend_hash_str_find_ptr(EG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
if (EG(autoload_func) == spl_func_ptr) {
success = SUCCESS;
@@ -695,7 +679,7 @@ PHP_FUNCTION(spl_autoload_unregister)
}
}
- efree(lc_name);
+ zend_string_release(lc_name);
RETURN_BOOL(success == SUCCESS);
} /* }}} */
@@ -704,64 +688,55 @@ PHP_FUNCTION(spl_autoload_unregister)
PHP_FUNCTION(spl_autoload_functions)
{
zend_function *fptr;
- HashPosition function_pos;
autoload_func_info *alfi;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
-
+
if (!EG(autoload_func)) {
- if (zend_hash_find(EG(function_table), ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME), (void **) &fptr) == SUCCESS) {
+ if ((fptr = zend_hash_str_find_ptr(EG(function_table), ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1))) {
array_init(return_value);
- add_next_index_stringl(return_value, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME)-1, 1);
+ add_next_index_stringl(return_value, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME)-1);
return;
}
RETURN_FALSE;
}
- zend_hash_find(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call"), (void **) &fptr);
+ fptr = zend_hash_str_find_ptr(EG(function_table), "spl_autoload_call", sizeof("spl_autoload_call") - 1);
if (EG(autoload_func) == fptr) {
+ zend_string *key;
array_init(return_value);
- zend_hash_internal_pointer_reset_ex(SPL_G(autoload_functions), &function_pos);
- while(zend_hash_has_more_elements_ex(SPL_G(autoload_functions), &function_pos) == SUCCESS) {
- zend_hash_get_current_data_ex(SPL_G(autoload_functions), (void **) &alfi, &function_pos);
- if (alfi->closure) {
- Z_ADDREF_P(alfi->closure);
- add_next_index_zval(return_value, alfi->closure);
+ ZEND_HASH_FOREACH_STR_KEY_PTR(SPL_G(autoload_functions), key, alfi) {
+ if (!Z_ISUNDEF(alfi->closure)) {
+ Z_ADDREF(alfi->closure);
+ add_next_index_zval(return_value, &alfi->closure);
} else if (alfi->func_ptr->common.scope) {
- zval *tmp;
- MAKE_STD_ZVAL(tmp);
- array_init(tmp);
+ zval tmp;
- if (alfi->obj) {
- Z_ADDREF_P(alfi->obj);
- add_next_index_zval(tmp, alfi->obj);
+ array_init(&tmp);
+ if (!Z_ISUNDEF(alfi->obj)) {
+ Z_ADDREF(alfi->obj);
+ add_next_index_zval(&tmp, &alfi->obj);
} else {
- add_next_index_string(tmp, alfi->ce->name, 1);
+ add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
}
- add_next_index_string(tmp, alfi->func_ptr->common.function_name, 1);
- add_next_index_zval(return_value, tmp);
+ add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
+ add_next_index_zval(return_value, &tmp);
} else {
- if (strncmp(alfi->func_ptr->common.function_name, "__lambda_func", sizeof("__lambda_func") - 1)) {
- add_next_index_string(return_value, alfi->func_ptr->common.function_name, 1);
+ if (strncmp(alfi->func_ptr->common.function_name->val, "__lambda_func", sizeof("__lambda_func") - 1)) {
+ add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
} else {
- char *key;
- uint len;
- long dummy;
- zend_hash_get_current_key_ex(SPL_G(autoload_functions), &key, &len, &dummy, 0, &function_pos);
- add_next_index_stringl(return_value, key, len - 1, 1);
+ add_next_index_str(return_value, zend_string_copy(key));
}
}
-
- zend_hash_move_forward_ex(SPL_G(autoload_functions), &function_pos);
- }
+ } ZEND_HASH_FOREACH_END();
return;
}
array_init(return_value);
- add_next_index_string(return_value, EG(autoload_func)->common.function_name, 1);
+ add_next_index_str(return_value, zend_string_copy(EG(autoload_func)->common.function_name));
} /* }}} */
/* {{{ proto string spl_object_hash(object obj)
@@ -769,49 +744,41 @@ PHP_FUNCTION(spl_autoload_functions)
PHP_FUNCTION(spl_object_hash)
{
zval *obj;
- char* hash;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &obj) == FAILURE) {
return;
}
-
- hash = emalloc(33);
- php_spl_object_hash(obj, hash TSRMLS_CC);
-
- RETVAL_STRING(hash, 0);
+
+ RETURN_NEW_STR(php_spl_object_hash(obj));
}
/* }}} */
-PHPAPI void php_spl_object_hash(zval *obj, char *result TSRMLS_DC) /* {{{*/
+PHPAPI zend_string *php_spl_object_hash(zval *obj) /* {{{*/
{
intptr_t hash_handle, hash_handlers;
- char *hex;
if (!SPL_G(hash_mask_init)) {
if (!BG(mt_rand_is_seeded)) {
- php_mt_srand(GENERATE_SEED() TSRMLS_CC);
+ php_mt_srand((uint32_t)GENERATE_SEED());
}
- SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1);
- SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand(TSRMLS_C) >> 1);
+ SPL_G(hash_mask_handle) = (intptr_t)(php_mt_rand() >> 1);
+ SPL_G(hash_mask_handlers) = (intptr_t)(php_mt_rand() >> 1);
SPL_G(hash_mask_init) = 1;
}
hash_handle = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj);
hash_handlers = SPL_G(hash_mask_handlers)^(intptr_t)Z_OBJ_HT_P(obj);
- spprintf(&hex, 32, "%016lx%016lx", hash_handle, hash_handlers);
-
- strlcpy(result, hex, 33);
- efree(hex);
+ return strpprintf(32, "%016lx%016lx", hash_handle, hash_handlers);
}
/* }}} */
-int spl_build_class_list_string(zval **entry, char **list TSRMLS_DC) /* {{{ */
+int spl_build_class_list_string(zval *entry, char **list) /* {{{ */
{
char *res;
-
- spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_PP(entry));
+
+ spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
efree(*list);
*list = res;
return ZEND_HASH_APPLY_KEEP;
@@ -827,20 +794,18 @@ PHP_MINFO_FUNCTION(spl)
php_info_print_table_start();
php_info_print_table_header(2, "SPL support", "enabled");
- INIT_PZVAL(&list);
array_init(&list);
SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
strg = estrdup("");
- zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg TSRMLS_CC);
+ zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg);
zval_dtor(&list);
php_info_print_table_row(2, "Interfaces", strg + 2);
efree(strg);
- INIT_PZVAL(&list);
array_init(&list);
SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
strg = estrdup("");
- zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg TSRMLS_CC);
+ zend_hash_apply_with_argument(Z_ARRVAL_P(&list), (apply_func_arg_t)spl_build_class_list_string, &strg);
zval_dtor(&list);
php_info_print_table_row(2, "Classes", strg + 2);
efree(strg);
@@ -958,7 +923,6 @@ PHP_MINIT_FUNCTION(spl)
PHP_RINIT_FUNCTION(spl) /* {{{ */
{
SPL_G(autoload_extensions) = NULL;
- SPL_G(autoload_extensions_len) = 0;
SPL_G(autoload_functions) = NULL;
SPL_G(hash_mask_init) = 0;
return SUCCESS;
@@ -967,9 +931,8 @@ PHP_RINIT_FUNCTION(spl) /* {{{ */
PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
{
if (SPL_G(autoload_extensions)) {
- efree(SPL_G(autoload_extensions));
+ zend_string_release(SPL_G(autoload_extensions));
SPL_G(autoload_extensions) = NULL;
- SPL_G(autoload_extensions_len) = 0;
}
if (SPL_G(autoload_functions)) {
zend_hash_destroy(SPL_G(autoload_functions));
@@ -993,7 +956,7 @@ zend_module_entry spl_module_entry = {
PHP_RINIT(spl),
PHP_RSHUTDOWN(spl),
PHP_MINFO(spl),
- "0.2",
+ PHP_SPL_VERSION,
PHP_MODULE_GLOBALS(spl),
PHP_GINIT(spl),
NULL,