cvs: ZendEngine2 / zend_builtin_functions.c zend_compile.h zend_execute.c

From: Date: Wed, 02 Apr 2003 15:28:31 +0000
Subject: cvs: ZendEngine2 / zend_builtin_functions.c zend_compile.h zend_execute.c
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-1097@lists.php.net to get a copy of this message
stas Wed Apr 2 10:28:31 2003 EDT Modified files: /ZendEngine2 zend_compile.h zend_execute.c zend_builtin_functions.c Log: allow class_exists() to work with namespaces too. add CLASS_IS_NAMESPACE macro Index: ZendEngine2/zend_compile.h diff -u ZendEngine2/zend_compile.h:1.230 ZendEngine2/zend_compile.h:1.231 --- ZendEngine2/zend_compile.h:1.230 Tue Apr 1 14:37:04 2003 +++ ZendEngine2/zend_compile.h Wed Apr 2 10:28:30 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_compile.h,v 1.230 2003/04/01 19:37:04 andrei Exp $ */ +/* $Id: zend_compile.h,v 1.231 2003/04/02 15:28:30 stas Exp $ */ #ifndef ZEND_COMPILE_H #define ZEND_COMPILE_H @@ -752,6 +752,8 @@ #define ZEND_ARG_SEND_BY_REF (1<<0) #define ZEND_ARG_COMPILE_TIME_BOUND (1<<1) + +#define CLASS_IS_NAMESPACE(c) ((c)->type == ZEND_INTERNAL_NAMESPACE || (c)->type == ZEND_USER_NAMESPACE) #define AI_USE_PTR(ai) \ if ((ai).ptr_ptr) { \ Index: ZendEngine2/zend_execute.c diff -u ZendEngine2/zend_execute.c:1.453 ZendEngine2/zend_execute.c:1.454 --- ZendEngine2/zend_execute.c:1.453 Tue Apr 1 16:02:02 2003 +++ ZendEngine2/zend_execute.c Wed Apr 2 10:28:30 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_execute.c,v 1.453 2003/04/01 21:02:02 andrei Exp $ */ +/* $Id: zend_execute.c,v 1.454 2003/04/02 15:28:30 stas Exp $ */ #define ZEND_INTENSIVE_DEBUGGING 0 @@ -2371,7 +2371,7 @@ if(retval == FAILURE) { /* try namespace */ - if(zend_hash_find(&EG(global_namespace_ptr)->class_table, class_name_strval, class_name_strlen+1, (void **)&pce) == SUCCESS && ((*pce)->type == ZEND_USER_NAMESPACE || (*pce)->type == ZEND_INTERNAL_NAMESPACE)) { + if(zend_hash_find(&EG(global_namespace_ptr)->class_table, class_name_strval, class_name_strlen+1, (void **)&pce) == SUCCESS && CLASS_IS_NAMESPACE((*pce))) { retval = SUCCESS; } } @@ -2382,7 +2382,7 @@ if(EX(opline)->extended_value == ZEND_FETCH_CLASS_GLOBAL) { ns = EG(global_namespace_ptr); } else { - if (zend_hash_find(&EG(global_namespace_ptr)->class_table, EX(opline)->op1.u.constant.value.str.val, EX(opline)->op1.u.constant.value.str.len+1, (void **)&pce) == FAILURE || ((*pce)->type != ZEND_USER_NAMESPACE && (*pce)->type != ZEND_INTERNAL_NAMESPACE)) { + if (zend_hash_find(&EG(global_namespace_ptr)->class_table, EX(opline)->op1.u.constant.value.str.val, EX(opline)->op1.u.constant.value.str.len+1, (void **)&pce) == FAILURE || !CLASS_IS_NAMESPACE((*pce))) { zend_error(E_ERROR, "Namespace '%s' not found", EX(opline)->op1.u.constant.value.str.val); } ns = *pce; Index: ZendEngine2/zend_builtin_functions.c diff -u ZendEngine2/zend_builtin_functions.c:1.182 ZendEngine2/zend_builtin_functions.c:1.183 --- ZendEngine2/zend_builtin_functions.c:1.182 Wed Apr 2 10:03:24 2003 +++ ZendEngine2/zend_builtin_functions.c Wed Apr 2 10:28:31 2003 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_builtin_functions.c,v 1.182 2003/04/02 15:03:24 stas Exp $ */ +/* $Id: zend_builtin_functions.c,v 1.183 2003/04/02 15:28:31 stas Exp $ */ #include "zend.h" #include "zend_API.h" @@ -792,21 +792,62 @@ } /* }}} */ +static inline zend_namespace *get_namespace_from_zval(zval **namespace_name TSRMLS_DC) +{ + zend_namespace **pns; + char *str_ns_name; + + convert_to_string_ex(namespace_name); + if(!Z_STRVAL_PP(namespace_name) || !Z_STRLEN_PP(namespace_name)) { + return EG(global_namespace_ptr); + } + + str_ns_name = estrndup(Z_STRVAL_PP(namespace_name), Z_STRLEN_PP(namespace_name)); + zend_str_tolower(str_ns_name, Z_STRLEN_PP(namespace_name)); + if(zend_hash_find(&EG(global_namespace_ptr)->class_table, str_ns_name, Z_STRLEN_PP(namespace_name)+1, (void **)&pns) == FAILURE || !CLASS_IS_NAMESPACE((*pns))) { + zend_error(E_WARNING, "Namespace '%s' is not defined!", Z_STRVAL_PP(namespace_name)); + efree(str_ns_name); + return NULL; + } + + efree(str_ns_name); + return *pns; +} -/* {{{ proto bool class_exists(string classname) +/* {{{ proto bool class_exists(string classname[, string namespace]) Checks if the class exists */ ZEND_FUNCTION(class_exists) { - zval **class_name; + zval **class_name, **namespace_name; char *lcname; + zend_namespace *ns; - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &class_name)==FAILURE) { - ZEND_WRONG_PARAM_COUNT(); + switch(ZEND_NUM_ARGS()) { + case 0: + ZEND_WRONG_PARAM_COUNT(); + case 1: + if(zend_get_parameters_ex(1, &class_name)==FAILURE) { + ZEND_WRONG_PARAM_COUNT(); + } + ns = EG(active_namespace); + break; + case 2: + if(zend_get_parameters_ex(2, &class_name, &namespace_name)==FAILURE) { + ZEND_WRONG_PARAM_COUNT(); + } + ns = get_namespace_from_zval(namespace_name TSRMLS_CC); + if(!ns) { + RETURN_FALSE; + } + break; + default: + ZEND_WRONG_PARAM_COUNT(); } + convert_to_string_ex(class_name); lcname = estrndup((*class_name)->value.str.val, (*class_name)->value.str.len); zend_str_tolower(lcname, (*class_name)->value.str.len); - if (zend_hash_exists(EG(class_table), lcname, (*class_name)->value.str.len+1)) { + if (zend_hash_exists(&ns->class_table, lcname, (*class_name)->value.str.len+1)) { efree(lcname); RETURN_TRUE; } else { @@ -1070,33 +1111,23 @@ Returns an array of all declared classes. */ ZEND_FUNCTION(get_declared_classes) { - int global_ns = 1; zval **namespace_name; - zend_namespace **pns = NULL, *ns; + zend_namespace *ns; if (ZEND_NUM_ARGS() != 0) { if(ZEND_NUM_ARGS() > 1 || zend_get_parameters_ex(1, &namespace_name)==FAILURE) { ZEND_WRONG_PARAM_COUNT(); - } else { - global_ns = 0; - convert_to_string_ex(namespace_name); - if(!Z_STRVAL_PP(namespace_name) || !Z_STRLEN_PP(namespace_name)) { - global_ns = 1; - } - } - } + } - if(!global_ns) { - zend_str_tolower(Z_STRVAL_PP(namespace_name), Z_STRLEN_PP(namespace_name)); - if(zend_hash_find(&EG(global_namespace_ptr)->class_table, Z_STRVAL_PP(namespace_name), Z_STRLEN_PP(namespace_name)+1, (void **)&pns) == FAILURE) { - zend_error(E_WARNING, "Namespace '%s' is not defined!", Z_STRVAL_PP(namespace_name)); + ns = get_namespace_from_zval(namespace_name TSRMLS_CC); + + if(!ns) { RETURN_FALSE; } - ns = *pns; } else { ns = EG(global_namespace_ptr); } - + array_init(return_value); zend_hash_apply_with_arguments(&ns->class_table, (apply_func_args_t) copy_class_name, 1, return_value); }

« previous php.zend-engine.cvs (#1097) next »