php-src/Zend/zend_constants.c

341 lines
9.6 KiB
C
Raw Normal View History

1999-04-07 18:10:10 +00:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2002-12-31 15:59:15 +00:00
| Copyright (c) 1998-2003 Zend Technologies Ltd. (http://www.zend.com) |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
2001-12-11 15:16:21 +00:00
| This source file is subject to version 2.00 of the Zend license, |
1999-07-16 14:58:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2001-12-11 15:16:21 +00:00
| http://www.zend.com/license/2_00.txt. |
1999-07-16 14:58:16 +00:00
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-04-07 18:10:10 +00:00
#include "zend.h"
#include "zend_constants.h"
2003-06-15 15:34:00 +00:00
#include "zend_execute.h"
1999-04-07 18:10:10 +00:00
#include "zend_variables.h"
#include "zend_operators.h"
#include "zend_globals.h"
void free_zend_constant(zend_constant *c)
1999-04-07 18:10:10 +00:00
{
2002-06-22 13:52:07 +00:00
if (!(c->flags & CONST_PERSISTENT)) {
1999-04-07 18:10:10 +00:00
zval_dtor(&c->value);
}
free(c->name);
}
void copy_zend_constant(zend_constant *c)
{
c->name = zend_strndup(c->name, c->name_len);
2000-02-13 22:15:06 +00:00
if (!(c->flags & CONST_PERSISTENT)) {
zval_copy_ctor(&c->value);
}
}
void zend_copy_constants(HashTable *target, HashTable *source)
{
zend_constant tmp_constant;
zend_hash_copy(target, source, (copy_ctor_func_t) copy_zend_constant, &tmp_constant, sizeof(zend_constant));
}
2001-07-31 04:53:54 +00:00
static int clean_non_persistent_constant(zend_constant *c TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
if (c->flags & CONST_PERSISTENT) {
2001-10-23 01:23:36 +00:00
return EG(full_tables_cleanup) ? 0 : ZEND_HASH_APPLY_STOP;
1999-04-07 18:10:10 +00:00
} else {
2001-10-23 01:23:36 +00:00
return EG(full_tables_cleanup) ? 1 : ZEND_HASH_APPLY_REMOVE;
1999-04-07 18:10:10 +00:00
}
}
2001-07-31 04:53:54 +00:00
static int clean_module_constant(zend_constant *c, int *module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
if (c->module_number == *module_number) {
return 1;
} else {
return 0;
}
}
2001-07-30 07:43:02 +00:00
void clean_module_constants(int module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
2001-07-31 04:53:54 +00:00
zend_hash_apply_with_argument(EG(zend_constants), (apply_func_arg_t) clean_module_constant, (void *) &module_number TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
int zend_startup_constants(TSRMLS_D)
1999-04-07 18:10:10 +00:00
{
2003-06-02 12:13:11 +00:00
EG(zend_constants) = (HashTable *) malloc(sizeof(HashTable));
1999-04-07 18:10:10 +00:00
if (zend_hash_init(EG(zend_constants), 20, NULL, ZEND_CONSTANT_DTOR, 1)==FAILURE) {
return FAILURE;
}
return SUCCESS;
}
void zend_register_standard_constants(TSRMLS_D)
{
1999-04-07 18:10:10 +00:00
REGISTER_MAIN_LONG_CONSTANT("E_ERROR", E_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_WARNING", E_WARNING, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_PARSE", E_PARSE, CONST_PERSISTENT | CONST_CS);
1999-07-22 22:17:49 +00:00
REGISTER_MAIN_LONG_CONSTANT("E_NOTICE", E_NOTICE, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_STRICT", E_STRICT, CONST_PERSISTENT | CONST_CS);
1999-07-22 22:17:49 +00:00
REGISTER_MAIN_LONG_CONSTANT("E_CORE_ERROR", E_CORE_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_CORE_WARNING", E_CORE_WARNING, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_ERROR", E_COMPILE_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_COMPILE_WARNING", E_COMPILE_WARNING, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_USER_ERROR", E_USER_ERROR, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_USER_WARNING", E_USER_WARNING, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("E_USER_NOTICE", E_USER_NOTICE, CONST_PERSISTENT | CONST_CS);
1999-07-22 22:17:49 +00:00
1999-04-07 18:10:10 +00:00
REGISTER_MAIN_LONG_CONSTANT("E_ALL", E_ALL, CONST_PERSISTENT | CONST_CS);
/* true/false constants */
{
zend_constant c;
c.value.type = IS_BOOL;
c.flags = CONST_PERSISTENT;
c.module_number = 0;
c.name = zend_strndup(ZEND_STRS("TRUE"));
c.name_len = sizeof("TRUE");
1999-04-07 18:10:10 +00:00
c.value.value.lval = 1;
c.value.type = IS_BOOL;
zend_register_constant(&c TSRMLS_CC);
1999-04-07 18:10:10 +00:00
c.name = zend_strndup(ZEND_STRS("FALSE"));
c.name_len = sizeof("FALSE");
1999-04-07 18:10:10 +00:00
c.value.value.lval = 0;
c.value.type = IS_BOOL;
zend_register_constant(&c TSRMLS_CC);
c.name = zend_strndup(ZEND_STRS("ZEND_THREAD_SAFE"));
c.name_len = sizeof("ZEND_THREAD_SAFE");
c.value.value.lval = ZTS_V;
c.value.type = IS_BOOL;
zend_register_constant(&c TSRMLS_CC);
c.name = zend_strndup(ZEND_STRS("NULL"));
c.name_len = sizeof("NULL");
2000-01-04 13:22:58 +00:00
c.value.type = IS_NULL;
zend_register_constant(&c TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
}
int zend_shutdown_constants(TSRMLS_D)
1999-04-07 18:10:10 +00:00
{
zend_hash_destroy(EG(zend_constants));
2003-06-02 12:13:11 +00:00
free(EG(zend_constants));
1999-04-07 18:10:10 +00:00
return SUCCESS;
}
2001-07-30 07:43:02 +00:00
void clean_non_persistent_constants(TSRMLS_D)
1999-04-07 18:10:10 +00:00
{
2001-10-23 01:23:36 +00:00
if (EG(full_tables_cleanup)) {
zend_hash_apply(EG(zend_constants), (apply_func_t) clean_non_persistent_constant TSRMLS_CC);
} else {
zend_hash_reverse_apply(EG(zend_constants), (apply_func_t) clean_non_persistent_constant TSRMLS_CC);
}
1999-04-07 18:10:10 +00:00
}
ZEND_API void zend_register_long_constant(char *name, uint name_len, long lval, int flags, int module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_constant c;
c.value.type = IS_LONG;
c.value.value.lval = lval;
c.flags = flags;
2001-08-11 15:56:40 +00:00
c.name = zend_strndup(name, name_len);
1999-04-07 18:10:10 +00:00
c.name_len = name_len;
c.module_number = module_number;
zend_register_constant(&c TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
ZEND_API void zend_register_double_constant(char *name, uint name_len, double dval, int flags, int module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_constant c;
c.value.type = IS_DOUBLE;
c.value.value.dval = dval;
c.flags = flags;
2001-08-11 15:56:40 +00:00
c.name = zend_strndup(name, name_len);
1999-04-07 18:10:10 +00:00
c.name_len = name_len;
c.module_number = module_number;
zend_register_constant(&c TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
ZEND_API void zend_register_stringl_constant(char *name, uint name_len, char *strval, uint strlen, int flags, int module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_constant c;
c.value.type = IS_STRING;
c.value.value.str.val = strval;
c.value.value.str.len = strlen;
c.flags = flags;
2001-08-11 15:56:40 +00:00
c.name = zend_strndup(name, name_len);
1999-04-07 18:10:10 +00:00
c.name_len = name_len;
c.module_number = module_number;
zend_register_constant(&c TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
ZEND_API void zend_register_string_constant(char *name, uint name_len, char *strval, int flags, int module_number TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_register_stringl_constant(name, name_len, strval, strlen(strval), flags, module_number TSRMLS_CC);
1999-04-07 18:10:10 +00:00
}
2001-07-30 07:43:02 +00:00
ZEND_API int zend_get_constant(char *name, uint name_len, zval *result TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
zend_constant *c;
int retval = 1;
char *lookup_name;
char *colon;
if ((colon = memchr(name, ':', name_len)) && colon[1] == ':') {
/* class constant */
zend_class_entry **ce = NULL, *scope;
int class_name_len = colon-name;
int const_name_len = name_len - class_name_len - 2;
char *constant_name = colon+2;
zval **ret_constant;
char *class_name;
if (EG(in_execution)) {
scope = EG(scope);
} else {
scope = CG(active_class_entry);
}
class_name = do_alloca(class_name_len+1);
memcpy(class_name, name, class_name_len);
class_name[class_name_len] = '\0';
if (class_name_len == sizeof("self")-1 && strcmp(class_name, "self") == 0) {
if (scope) {
ce = &scope;
} else {
zend_error(E_ERROR, "Cannot access self:: when no class scope is active");
retval = 0;
}
} else if (class_name_len == sizeof("parent")-1 && strcmp(class_name, "parent") == 0) {
if (!scope) {
zend_error(E_ERROR, "Cannot access parent:: when no class scope is active");
} else if (!scope->parent) {
zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent");
} else {
ce = &scope->parent;
}
} else {
if (zend_lookup_class(class_name, class_name_len, &ce TSRMLS_CC) != SUCCESS) {
retval = 0;
}
}
free_alloca(class_name);
if (retval && ce) {
if (zend_hash_find(&((*ce)->constants_table), constant_name, const_name_len+1, (void **) &ret_constant) != SUCCESS) {
retval = 0;
}
} else {
retval = 0;
}
1999-04-07 18:10:10 +00:00
if (retval) {
*result = **ret_constant;
zval_copy_ctor(result);
}
return retval;
}
if (zend_hash_find(EG(zend_constants), name, name_len+1, (void **) &c) == FAILURE) {
lookup_name = do_alloca(name_len+1);
2003-05-21 22:57:51 +00:00
zend_str_tolower_copy(lookup_name, name, name_len);
2003-06-15 14:40:38 +00:00
lookup_name[name_len] = '\0';
if (zend_hash_find(EG(zend_constants), lookup_name, name_len+1, (void **) &c)==SUCCESS) {
if ((c->flags & CONST_CS) && memcmp(c->name, name, name_len)!=0) {
retval=0;
}
1999-04-07 18:10:10 +00:00
} else {
retval=0;
1999-04-07 18:10:10 +00:00
}
free_alloca(lookup_name);
1999-04-07 18:10:10 +00:00
}
2001-12-01 08:33:48 +00:00
if (retval) {
*result = c->value;
zval_copy_ctor(result);
result->refcount = 1;
result->is_ref = 0;
}
2001-12-01 08:33:48 +00:00
1999-04-07 18:10:10 +00:00
return retval;
}
ZEND_API int zend_register_constant(zend_constant *c TSRMLS_DC)
1999-04-07 18:10:10 +00:00
{
char *lowercase_name;
int ret = SUCCESS;
1999-04-07 18:10:10 +00:00
#if 0
2001-08-11 15:56:40 +00:00
printf("Registering constant for module %d\n", c->module_number);
1999-04-07 18:10:10 +00:00
#endif
if (!(c->flags & CONST_CS)) {
2003-06-01 16:20:23 +00:00
/* keep in mind that c->name_len already contains the '\0' */
lowercase_name = do_alloca(c->name_len);
2003-05-31 15:57:59 +00:00
zend_str_tolower_copy(lowercase_name, c->name, c->name_len - 1);
2003-05-21 22:57:51 +00:00
} else {
2003-06-01 16:20:23 +00:00
lowercase_name = do_alloca(c->name_len + 1);
2003-05-21 22:57:51 +00:00
memcpy(lowercase_name, c->name, c->name_len);
lowercase_name[c->name_len] = '\0';
}
1999-04-07 18:10:10 +00:00
if (zend_hash_add(EG(zend_constants), lowercase_name, c->name_len, (void *) c, sizeof(zend_constant), NULL)==FAILURE) {
free(c->name);
2002-06-22 13:52:07 +00:00
if (!(c->flags & CONST_PERSISTENT)) {
2001-08-14 15:22:34 +00:00
zval_dtor(&c->value);
}
2001-08-11 15:56:40 +00:00
zend_error(E_NOTICE,"Constant %s already defined", lowercase_name);
ret = FAILURE;
1999-04-07 18:10:10 +00:00
}
free_alloca(lowercase_name);
return ret;
1999-04-07 18:10:10 +00:00
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
1999-04-07 18:10:10 +00:00
* End:
*/