2003-03-23 12:32:24 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1998-2003 Zend Technologies Ltd. (http://www.zend.com) |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
2003-03-23 14:57:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2003-03-23 12:32:24 +08:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Sterling Hughes <sterling@php.net> |
|
2003-08-24 03:48:52 +08:00
|
|
|
| Marcus Boerger <helly@php.net> |
|
2003-03-23 12:32:24 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "zend.h"
|
|
|
|
#include "zend_API.h"
|
2003-07-01 04:03:56 +08:00
|
|
|
#include "zend_reflection_api.h"
|
2003-08-29 04:35:54 +08:00
|
|
|
#include "zend_builtin_functions.h"
|
2003-10-23 04:04:48 +08:00
|
|
|
#include "zend_interfaces.h"
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-09-05 19:40:30 +08:00
|
|
|
static zend_class_entry *default_exception_ptr;
|
|
|
|
static zend_object_handlers default_exception_handlers;
|
2003-10-26 03:13:06 +08:00
|
|
|
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-08-24 03:41:22 +08:00
|
|
|
static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC)
|
|
|
|
{
|
|
|
|
zval tmp, obj;
|
|
|
|
zend_object *object;
|
2003-08-29 04:35:54 +08:00
|
|
|
zval *trace;
|
2003-08-24 03:41:22 +08:00
|
|
|
|
|
|
|
obj.value.obj = zend_objects_new(&object, class_type TSRMLS_CC);
|
2003-09-05 19:40:30 +08:00
|
|
|
obj.value.obj.handlers = &default_exception_handlers;
|
2003-08-24 03:41:22 +08:00
|
|
|
|
|
|
|
ALLOC_HASHTABLE(object->properties);
|
|
|
|
zend_hash_init(object->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
|
|
|
|
zend_hash_copy(object->properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
|
|
|
|
|
2003-08-29 04:35:54 +08:00
|
|
|
ALLOC_ZVAL(trace);
|
|
|
|
trace->is_ref = 0;
|
|
|
|
trace->refcount = 0;
|
2003-08-29 08:16:00 +08:00
|
|
|
zend_fetch_debug_backtrace(trace, 0 TSRMLS_CC);
|
2003-08-29 04:35:54 +08:00
|
|
|
|
2003-08-24 03:41:22 +08:00
|
|
|
zend_update_property_string(class_type, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
|
|
|
|
zend_update_property_long(class_type, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
|
2003-08-29 04:35:54 +08:00
|
|
|
zend_update_property(class_type, &obj, "trace", sizeof("trace")-1, trace TSRMLS_CC);
|
2003-08-24 03:41:22 +08:00
|
|
|
|
|
|
|
return obj.value.obj;
|
|
|
|
}
|
|
|
|
|
2003-10-26 03:13:06 +08:00
|
|
|
ZEND_METHOD(exception, __clone)
|
2003-10-26 03:07:09 +08:00
|
|
|
{
|
|
|
|
/* Should never be executable */
|
2003-10-26 03:13:06 +08:00
|
|
|
zend_throw_exception(NULL, "Cannot clone object using __clone()", 0 TSRMLS_CC);
|
2003-10-26 03:07:09 +08:00
|
|
|
}
|
|
|
|
|
2003-08-24 19:25:08 +08:00
|
|
|
ZEND_METHOD(exception, __construct)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
2003-08-23 22:22:28 +08:00
|
|
|
char *message = NULL;
|
|
|
|
long code = 0;
|
2003-03-23 12:32:24 +08:00
|
|
|
zval *object;
|
2003-08-23 22:22:28 +08:00
|
|
|
int argc = ZEND_NUM_ARGS(), message_len;
|
2003-03-23 14:57:16 +08:00
|
|
|
|
2003-08-23 22:22:28 +08:00
|
|
|
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sl", &message, &message_len, &code) == FAILURE) {
|
|
|
|
zend_error(E_CORE_ERROR, "Wrong parameter count for exception([string $exception [, long $code ]])");
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
object = getThis();
|
|
|
|
|
2003-08-24 03:41:22 +08:00
|
|
|
if (message) {
|
|
|
|
zend_update_property_string(Z_OBJCE_P(object), object, "message", sizeof("message")-1, message TSRMLS_CC);
|
|
|
|
}
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-08-24 03:41:22 +08:00
|
|
|
if (code) {
|
|
|
|
zend_update_property_long(Z_OBJCE_P(object), object, "code", sizeof("code")-1, code TSRMLS_CC);
|
|
|
|
}
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DEFAULT_0_PARAMS \
|
|
|
|
if (ZEND_NUM_ARGS() > 0) { \
|
|
|
|
ZEND_WRONG_PARAM_COUNT(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _default_exception_get_entry(zval *object, char *name, int name_len, zval *return_value TSRMLS_DC)
|
|
|
|
{
|
2003-08-24 08:36:53 +08:00
|
|
|
zval *value;
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
value = zend_read_property(Z_OBJCE_P(object), object, name, name_len, 0 TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
*return_value = *value;
|
2003-03-23 12:32:24 +08:00
|
|
|
zval_copy_ctor(return_value);
|
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getFile)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
|
|
|
DEFAULT_0_PARAMS;
|
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
_default_exception_get_entry(getThis(), "file", sizeof("file")-1, return_value TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getLine)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
|
|
|
DEFAULT_0_PARAMS;
|
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
_default_exception_get_entry(getThis(), "line", sizeof("line")-1, return_value TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getMessage)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
|
|
|
DEFAULT_0_PARAMS;
|
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
_default_exception_get_entry(getThis(), "message", sizeof("message")-1, return_value TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getCode)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
|
|
|
DEFAULT_0_PARAMS;
|
|
|
|
|
2003-08-24 08:36:53 +08:00
|
|
|
_default_exception_get_entry(getThis(), "code", sizeof("code")-1, return_value TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getTrace)
|
2003-08-29 07:43:56 +08:00
|
|
|
{
|
|
|
|
DEFAULT_0_PARAMS;
|
|
|
|
|
|
|
|
_default_exception_get_entry(getThis(), "trace", sizeof("trace")-1, return_value TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
2003-08-31 02:28:24 +08:00
|
|
|
/* {{{ ZEND_METHOD(exception, gettraceasstring) */
|
|
|
|
#define TRACE_APPEND_CHR(chr) \
|
|
|
|
*str = (char*)erealloc(*str, *len + 1 + 1); \
|
|
|
|
(*str)[(*len)++] = chr
|
|
|
|
|
|
|
|
#define TRACE_APPEND_STRL(val, vallen) \
|
|
|
|
{ \
|
|
|
|
int l = vallen; \
|
|
|
|
*str = (char*)erealloc(*str, *len + l + 1); \
|
|
|
|
memcpy((*str) + *len, val, l); \
|
|
|
|
*len += l; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRACE_APPEND_STR(val) \
|
|
|
|
TRACE_APPEND_STRL(val, sizeof(val)-1)
|
|
|
|
|
|
|
|
#define TRACE_APPEND_KEY(key) \
|
|
|
|
if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
|
|
|
|
TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _build_trace_args(zval **arg, int num_args, va_list args, zend_hash_key *hash_key)
|
|
|
|
{
|
|
|
|
char **str;
|
|
|
|
int *len;
|
|
|
|
|
|
|
|
str = va_arg(args, char**);
|
|
|
|
len = va_arg(args, int*);
|
|
|
|
|
|
|
|
/* the trivial way would be to do:
|
|
|
|
* conver_to_string_ex(arg);
|
|
|
|
* append it and kill the now tmp arg.
|
|
|
|
* but that could cause some E_NOTICE and also damn long lines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (Z_TYPE_PP(arg)) {
|
|
|
|
case IS_NULL:
|
|
|
|
TRACE_APPEND_STR("NULL, ");
|
|
|
|
break;
|
2003-08-31 20:40:29 +08:00
|
|
|
case IS_STRING: {
|
|
|
|
int l_added;
|
2003-08-31 02:28:24 +08:00
|
|
|
TRACE_APPEND_CHR('\'');
|
|
|
|
if (Z_STRLEN_PP(arg) > 15) {
|
|
|
|
TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15);
|
|
|
|
TRACE_APPEND_STR("...', ");
|
2003-08-31 20:40:29 +08:00
|
|
|
l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
|
2003-08-31 02:28:24 +08:00
|
|
|
} else {
|
2003-08-31 20:40:29 +08:00
|
|
|
l_added = Z_STRLEN_PP(arg);
|
|
|
|
TRACE_APPEND_STRL(Z_STRVAL_PP(arg), l_added);
|
2003-08-31 02:28:24 +08:00
|
|
|
TRACE_APPEND_STR("', ");
|
2003-08-31 20:40:29 +08:00
|
|
|
l_added += 3 + 1;
|
|
|
|
}
|
|
|
|
while (--l_added) {
|
|
|
|
if ((*str)[*len - l_added] < 32) {
|
|
|
|
(*str)[*len - l_added] = '?';
|
|
|
|
}
|
2003-08-31 02:28:24 +08:00
|
|
|
}
|
|
|
|
break;
|
2003-08-31 20:40:29 +08:00
|
|
|
}
|
2003-08-31 02:28:24 +08:00
|
|
|
case IS_BOOL:
|
|
|
|
if (Z_LVAL_PP(arg)) {
|
|
|
|
TRACE_APPEND_STR("true, ");
|
|
|
|
} else {
|
|
|
|
TRACE_APPEND_STR("false, ");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IS_RESOURCE:
|
|
|
|
TRACE_APPEND_STR("Resource id #");
|
|
|
|
/* break; */
|
|
|
|
case IS_LONG: {
|
|
|
|
long lval = Z_LVAL_PP(arg);
|
|
|
|
char s_tmp[MAX_LENGTH_OF_LONG + 1];
|
|
|
|
int l_tmp = zend_sprintf(s_tmp, "%ld", lval); /* SAFE */
|
|
|
|
TRACE_APPEND_STRL(s_tmp, l_tmp);
|
|
|
|
TRACE_APPEND_STR(", ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IS_DOUBLE: {
|
|
|
|
double dval = Z_DVAL_PP(arg);
|
|
|
|
char *s_tmp;
|
|
|
|
int l_tmp;
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
|
|
|
|
l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval); /* SAFE */
|
|
|
|
TRACE_APPEND_STRL(s_tmp, l_tmp);
|
|
|
|
/* %G already handles removing trailing zeros from the fractional part, yay */
|
|
|
|
efree(s_tmp);
|
|
|
|
TRACE_APPEND_STR(", ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IS_ARRAY:
|
|
|
|
TRACE_APPEND_STR("Array, ");
|
|
|
|
break;
|
|
|
|
case IS_OBJECT: {
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
TRACE_APPEND_STR("Object(");
|
|
|
|
TRACE_APPEND_STRL(Z_OBJCE_PP(arg)->name, strlen(Z_OBJCE_PP(arg)->name));
|
|
|
|
TRACE_APPEND_STR("), ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ZEND_HASH_APPLY_KEEP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _build_trace_string(zval **frame, int num_args, va_list args, zend_hash_key *hash_key)
|
|
|
|
{
|
|
|
|
char *s_tmp, **str;
|
2003-08-31 02:58:40 +08:00
|
|
|
int *len, *num;
|
2003-08-31 02:28:24 +08:00
|
|
|
long line;
|
|
|
|
HashTable *ht = Z_ARRVAL_PP(frame);
|
|
|
|
zval **file, **tmp;
|
|
|
|
|
|
|
|
str = va_arg(args, char**);
|
|
|
|
len = va_arg(args, int*);
|
2003-08-31 07:55:04 +08:00
|
|
|
num = va_arg(args, int*);
|
2003-08-31 02:28:24 +08:00
|
|
|
|
2003-08-31 17:35:54 +08:00
|
|
|
s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 1 + 1);
|
2003-08-31 02:58:40 +08:00
|
|
|
sprintf(s_tmp, "#%d ", (*num)++);
|
|
|
|
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
|
|
|
|
efree(s_tmp);
|
2003-08-31 02:28:24 +08:00
|
|
|
if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
|
|
|
|
if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
|
|
|
|
line = Z_LVAL_PP(tmp);
|
|
|
|
} else {
|
|
|
|
line = 0;
|
|
|
|
}
|
|
|
|
s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 2 + 1);
|
|
|
|
sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
|
|
|
|
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
|
|
|
|
efree(s_tmp);
|
|
|
|
} else {
|
|
|
|
TRACE_APPEND_STR("[internal function]: ");
|
|
|
|
}
|
|
|
|
TRACE_APPEND_KEY("class");
|
|
|
|
TRACE_APPEND_KEY("type");
|
|
|
|
TRACE_APPEND_KEY("function");
|
|
|
|
TRACE_APPEND_CHR('(');
|
|
|
|
if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
|
2003-09-02 05:40:58 +08:00
|
|
|
int last_len = *len;
|
2003-08-31 02:28:24 +08:00
|
|
|
zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp), (apply_func_args_t)_build_trace_args, 2, str, len);
|
2003-09-02 05:40:58 +08:00
|
|
|
if (last_len != *len) {
|
|
|
|
*len -= 2; /* remove last ', ' */
|
|
|
|
}
|
2003-08-31 02:28:24 +08:00
|
|
|
}
|
|
|
|
TRACE_APPEND_STR(")\n");
|
|
|
|
return ZEND_HASH_APPLY_KEEP;
|
|
|
|
}
|
|
|
|
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_METHOD(exception, getTraceAsString)
|
2003-08-31 02:28:24 +08:00
|
|
|
{
|
|
|
|
zval *trace;
|
2003-09-02 04:54:48 +08:00
|
|
|
char *res = estrdup(""), **str = &res, *s_tmp;
|
|
|
|
int res_len = 0, *len = &res_len, num = 0;
|
2003-08-31 02:28:24 +08:00
|
|
|
|
|
|
|
trace = zend_read_property(Z_OBJCE_P(getThis()), getThis(), "trace", sizeof("trace")-1, 1 TSRMLS_CC);
|
2003-09-02 04:54:48 +08:00
|
|
|
zend_hash_apply_with_arguments(Z_ARRVAL_P(trace), (apply_func_args_t)_build_trace_string, 3, str, len, &num);
|
2003-08-31 02:28:24 +08:00
|
|
|
|
2003-09-02 04:54:48 +08:00
|
|
|
s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 7 + 1);
|
|
|
|
sprintf(s_tmp, "#%d {main}", num);
|
|
|
|
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
|
|
|
|
efree(s_tmp);
|
|
|
|
|
|
|
|
res[res_len] = '\0';
|
|
|
|
RETURN_STRINGL(res, res_len, 0);
|
2003-08-31 02:28:24 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-10-23 03:00:42 +08:00
|
|
|
ZEND_METHOD(exception, __toString)
|
2003-08-31 02:28:24 +08:00
|
|
|
{
|
|
|
|
zval *message, *file, *line, *trace;
|
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
zend_fcall_info fci;
|
|
|
|
zval fname;
|
|
|
|
|
|
|
|
message = zend_read_property(Z_OBJCE_P(getThis()), getThis(), "message", sizeof("message")-1, 1 TSRMLS_CC);
|
|
|
|
file = zend_read_property(Z_OBJCE_P(getThis()), getThis(), "file", sizeof("file")-1, 1 TSRMLS_CC);
|
|
|
|
line = zend_read_property(Z_OBJCE_P(getThis()), getThis(), "line", sizeof("line")-1, 1 TSRMLS_CC);
|
|
|
|
|
|
|
|
ZVAL_STRINGL(&fname, "gettraceasstring", sizeof("gettraceasstring")-1, 0);
|
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
|
|
|
fci.function_table = &Z_OBJCE_P(getThis())->function_table;
|
|
|
|
fci.function_name = &fname;
|
|
|
|
fci.symbol_table = NULL;
|
|
|
|
fci.object_pp = &getThis();
|
|
|
|
fci.retval_ptr_ptr = &trace;
|
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
|
|
|
fci.no_separation = 1;
|
|
|
|
|
|
|
|
zend_call_function(&fci, NULL TSRMLS_CC);
|
|
|
|
|
|
|
|
len = 11 + strlen(Z_OBJCE_P(getThis())->name) + 16 + Z_STRLEN_P(message) + 5 + Z_STRLEN_P(file) + 1 + MAX_LENGTH_OF_LONG + 14 + Z_STRLEN_P(trace) + 1;
|
|
|
|
str = emalloc(len);
|
|
|
|
sprintf(str, "exception '%s' with message '%s' in %s:%ld\nStack trace:\n%s",
|
|
|
|
Z_OBJCE_P(getThis())->name, Z_STRVAL_P(message), Z_STRVAL_P(file), Z_LVAL_P(line),
|
2003-09-02 04:54:48 +08:00
|
|
|
Z_STRLEN_P(trace) ? Z_STRVAL_P(trace) : "#0 {main}\n");
|
2003-08-31 02:28:24 +08:00
|
|
|
len = strlen(str);
|
|
|
|
|
|
|
|
/* We store the result in the private property string so we can access
|
|
|
|
* the result in uncaught exception handlers without memleaks. */
|
|
|
|
zend_update_property_string(default_exception_ptr, getThis(), "string", sizeof("string")-1, str TSRMLS_CC);
|
|
|
|
|
|
|
|
zval_ptr_dtor(&trace);
|
|
|
|
|
|
|
|
RETURN_STRINGL(str, len, 0);
|
|
|
|
}
|
|
|
|
|
2003-08-30 19:33:41 +08:00
|
|
|
/* All functions that may be used in uncaught exception handlers must be final
|
|
|
|
* and must not throw exceptions. Otherwise we would need a facility to handle
|
2003-09-02 04:02:47 +08:00
|
|
|
* such exceptions in that handler.
|
|
|
|
* Also all getXY() methods are final because thy serve as read only access to
|
|
|
|
* their corresponding properties, no more, no less. If after all you need to
|
2003-10-23 03:00:42 +08:00
|
|
|
* override somthing then it is method __toString().
|
2003-09-02 04:02:47 +08:00
|
|
|
* And never try to change the state of exceptions and never implement anything
|
|
|
|
* that gives the user anything to accomplish this.
|
2003-08-30 19:33:41 +08:00
|
|
|
*/
|
2003-03-23 12:32:24 +08:00
|
|
|
static zend_function_entry default_exception_functions[] = {
|
2003-10-26 03:07:09 +08:00
|
|
|
ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
|
2003-08-24 19:25:08 +08:00
|
|
|
ZEND_ME(exception, __construct, NULL, 0)
|
2003-09-17 18:15:00 +08:00
|
|
|
ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
|
|
|
ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
|
|
|
ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
|
|
|
ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
|
|
|
ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
|
|
|
ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
|
2003-10-23 03:00:42 +08:00
|
|
|
ZEND_ME(exception, __toString, NULL, 0)
|
2003-03-23 12:32:24 +08:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2003-10-15 14:23:07 +08:00
|
|
|
int zend_cast_exception(zval *readobj, zval *writeobj, int type, int should_free TSRMLS_DC)
|
2003-09-05 19:40:30 +08:00
|
|
|
{
|
|
|
|
if (type == IS_STRING) {
|
|
|
|
zval fname, *retval;
|
|
|
|
|
2003-10-23 03:00:42 +08:00
|
|
|
ZVAL_STRING(&fname, "__tostring", 0);
|
2003-09-05 19:40:30 +08:00
|
|
|
if (call_user_function_ex(NULL, &readobj, &fname, &retval, 0, NULL, 0, NULL TSRMLS_CC) == SUCCESS) {
|
|
|
|
ZVAL_STRING(writeobj, Z_STRVAL_P(retval), 1);
|
|
|
|
zval_ptr_dtor(&retval);
|
2003-10-15 14:23:07 +08:00
|
|
|
return SUCCESS;
|
2003-09-05 19:40:30 +08:00
|
|
|
}
|
|
|
|
}
|
2003-10-15 14:23:07 +08:00
|
|
|
return FAILURE;
|
2003-09-05 19:40:30 +08:00
|
|
|
}
|
|
|
|
|
2003-03-24 01:18:31 +08:00
|
|
|
static void zend_register_default_exception(TSRMLS_D)
|
2003-03-23 12:32:24 +08:00
|
|
|
{
|
2003-08-30 19:40:37 +08:00
|
|
|
zend_class_entry ce;
|
2003-03-23 12:32:24 +08:00
|
|
|
|
2003-08-30 19:40:37 +08:00
|
|
|
INIT_CLASS_ENTRY(ce, "exception", default_exception_functions);
|
|
|
|
default_exception_ptr = zend_register_internal_class(&ce TSRMLS_CC);
|
2003-09-05 19:40:30 +08:00
|
|
|
default_exception_ptr->create_object = zend_default_exception_new;
|
|
|
|
memcpy(&default_exception_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
|
|
|
|
default_exception_handlers.cast_object = zend_cast_exception;
|
2003-08-24 03:41:22 +08:00
|
|
|
|
2003-09-04 02:01:22 +08:00
|
|
|
zend_declare_property_string(default_exception_ptr, "message", sizeof("message")-1, "Unknown exception", ZEND_ACC_PROTECTED TSRMLS_CC);
|
|
|
|
zend_declare_property_string(default_exception_ptr, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
|
|
|
|
zend_declare_property_long(default_exception_ptr, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
|
|
|
|
zend_declare_property_null(default_exception_ptr, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
|
|
|
|
zend_declare_property_null(default_exception_ptr, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
|
2003-09-06 22:06:40 +08:00
|
|
|
zend_declare_property_null(default_exception_ptr, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
2003-05-04 07:54:57 +08:00
|
|
|
ZEND_API zend_class_entry *zend_exception_get_default(void)
|
|
|
|
{
|
|
|
|
return default_exception_ptr;
|
|
|
|
}
|
|
|
|
|
2003-08-29 06:56:41 +08:00
|
|
|
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
|
|
|
|
{
|
|
|
|
zval *ex;
|
|
|
|
va_list arg;
|
|
|
|
char *message;
|
2003-08-31 17:35:54 +08:00
|
|
|
|
2003-08-29 06:56:41 +08:00
|
|
|
va_start(arg, format);
|
2003-08-31 17:35:54 +08:00
|
|
|
zend_vspprintf(&message, 0, format, arg);
|
2003-08-29 06:56:41 +08:00
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(ex);
|
|
|
|
if (exception_ce) {
|
|
|
|
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
|
|
|
|
zend_error(E_NOTICE, "Exceptions must be derived from exception");
|
|
|
|
exception_ce = default_exception_ptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exception_ce = default_exception_ptr;
|
|
|
|
}
|
|
|
|
object_init_ex(ex, exception_ce);
|
|
|
|
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
|
|
|
}
|
|
|
|
if (code) {
|
|
|
|
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
|
|
|
}
|
|
|
|
|
2003-08-31 17:35:54 +08:00
|
|
|
efree(message);
|
2003-08-29 06:56:41 +08:00
|
|
|
|
|
|
|
EG(exception) = ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* at the moment we can't use zend_throw_exception_ex because we don't have a protable
|
|
|
|
* vsnprintf that tells us the number of characters needed nor do we have spprintf from
|
|
|
|
* php or asprintf from glibc always.
|
|
|
|
*/
|
2003-08-24 23:07:00 +08:00
|
|
|
ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
|
2003-08-22 07:32:13 +08:00
|
|
|
{
|
|
|
|
zval *ex;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(ex);
|
2003-08-24 23:07:00 +08:00
|
|
|
if (exception_ce) {
|
|
|
|
if (!instanceof_function(exception_ce, default_exception_ptr TSRMLS_CC)) {
|
|
|
|
zend_error(E_NOTICE, "Exceptions must be derived from exception");
|
|
|
|
exception_ce = default_exception_ptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
exception_ce = default_exception_ptr;
|
|
|
|
}
|
|
|
|
object_init_ex(ex, exception_ce);
|
|
|
|
|
2003-08-22 07:32:13 +08:00
|
|
|
|
2003-08-24 03:48:52 +08:00
|
|
|
if (message) {
|
2003-08-24 23:07:00 +08:00
|
|
|
zend_update_property_string(exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
|
2003-08-24 03:48:52 +08:00
|
|
|
}
|
|
|
|
if (code) {
|
2003-08-24 23:07:00 +08:00
|
|
|
zend_update_property_long(exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
|
2003-08-24 03:48:52 +08:00
|
|
|
}
|
2003-08-22 07:32:13 +08:00
|
|
|
|
|
|
|
EG(exception) = ex;
|
|
|
|
}
|
|
|
|
|
2003-08-24 21:10:03 +08:00
|
|
|
static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, format);
|
2003-09-11 18:26:47 +08:00
|
|
|
zend_error_cb(type, file, lineno, format, args);
|
2003-08-24 21:10:03 +08:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
|
|
|
|
{
|
|
|
|
if (instanceof_function(Z_OBJCE_P(exception), default_exception_ptr TSRMLS_CC)) {
|
2003-08-31 02:58:40 +08:00
|
|
|
zend_fcall_info fci;
|
|
|
|
zval fname;
|
|
|
|
zval *str, *file, *line;
|
|
|
|
|
|
|
|
file = zend_read_property(default_exception_ptr, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
|
|
|
|
line = zend_read_property(default_exception_ptr, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
|
|
|
|
|
2003-10-23 03:00:42 +08:00
|
|
|
ZVAL_STRINGL(&fname, "__tostring", sizeof("__tostring")-1, 0);
|
2003-08-31 02:58:40 +08:00
|
|
|
|
|
|
|
fci.size = sizeof(fci);
|
|
|
|
fci.function_table = &default_exception_ptr->function_table;
|
|
|
|
fci.function_name = &fname;
|
|
|
|
fci.symbol_table = NULL;
|
|
|
|
fci.object_pp = &exception;
|
|
|
|
fci.retval_ptr_ptr = &str;
|
|
|
|
fci.param_count = 0;
|
|
|
|
fci.params = NULL;
|
|
|
|
fci.no_separation = 1;
|
|
|
|
|
|
|
|
zend_call_function(&fci, NULL TSRMLS_CC);
|
|
|
|
zval_ptr_dtor(&str);
|
|
|
|
|
|
|
|
str = zend_read_property(default_exception_ptr, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
|
|
|
|
|
|
|
|
zend_error_va(E_ERROR, Z_STRVAL_P(file), Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
|
2003-08-24 21:10:03 +08:00
|
|
|
} else {
|
2003-08-31 02:28:24 +08:00
|
|
|
zend_error(E_ERROR, "Uncaught exception '%s'", Z_OBJCE_P(exception)->name);
|
2003-08-24 21:10:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-23 12:32:24 +08:00
|
|
|
ZEND_API void zend_register_default_classes(TSRMLS_D)
|
|
|
|
{
|
2003-10-23 04:04:48 +08:00
|
|
|
zend_register_interfaces(TSRMLS_C);
|
2003-03-23 14:57:16 +08:00
|
|
|
zend_register_default_exception(TSRMLS_C);
|
2003-07-01 04:03:56 +08:00
|
|
|
zend_register_reflection_api(TSRMLS_C);
|
2003-10-18 01:19:44 +08:00
|
|
|
zend_register_iterator_wrapper(TSRMLS_C);
|
2003-03-23 12:32:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|