cvs: ZendEngine2 / zend_exceptions.c zend_exceptions.h

From: Date: Thu, 15 Jul 2004 22:21:36 +0000
Subject: cvs: ZendEngine2 / zend_exceptions.c zend_exceptions.h
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-2738@lists.php.net to get a copy of this message
helly Thu Jul 15 18:21:36 2004 EDT Modified files: /ZendEngine2 zend_exceptions.c zend_exceptions.h Log: - Add new class ErrorException to encapsulate errors in exceptions

http://cvs.php.net/diff.php/ZendEngine2/zend_exceptions.c?r1=1.65&r2=1.66&ty=u Index: ZendEngine2/zend_exceptions.c diff -u ZendEngine2/zend_exceptions.c:1.65 ZendEngine2/zend_exceptions.c:1.66 --- ZendEngine2/zend_exceptions.c:1.65 Thu May 20 13:59:27 2004 +++ ZendEngine2/zend_exceptions.c Thu Jul 15 18:21:36 2004 @@ -19,17 +19,18 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_exceptions.c,v 1.65 2004/05/20 17:59:27 wez Exp $ */ +/* $Id: zend_exceptions.c,v 1.66 2004/07/15 22:21:36 helly Exp $ */ #include "zend.h" #include "zend_API.h" #include "zend_reflection_api.h" #include "zend_builtin_functions.h" #include "zend_interfaces.h" +#include "zend_exceptions.h" zend_class_entry *default_exception_ce; +zend_class_entry *error_exception_ce; static zend_object_handlers default_exception_handlers; -ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC); ZEND_API void (*zend_throw_exception_hook)(zval *ex TSRMLS_DC); @@ -74,7 +75,7 @@ #endif } -static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC) +static zend_object_value zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces TSRMLS_DC) { zval tmp, obj; zend_object *object; @@ -90,7 +91,7 @@ ALLOC_ZVAL(trace); trace->is_ref = 0; trace->refcount = 0; - zend_fetch_debug_backtrace(trace, 0 TSRMLS_CC); + zend_fetch_debug_backtrace(trace, skip_top_traces TSRMLS_CC); zend_update_property_string(default_exception_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC); zend_update_property_long(default_exception_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC); @@ -99,6 +100,16 @@ return obj.value.obj; } +static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC) +{ + return zend_default_exception_new_ex(class_type, 0 TSRMLS_CC); +} + +static zend_object_value zend_error_exception_new(zend_class_entry *class_type TSRMLS_DC) +{ + return zend_default_exception_new_ex(class_type, 2 TSRMLS_CC); +} + ZEND_METHOD(exception, __clone) { /* Should never be executable */ @@ -127,6 +138,30 @@ } } +ZEND_METHOD(error_exception, __construct) +{ + char *message = NULL; + long code = 0, severity = E_ERROR; + zval *object; + int argc = ZEND_NUM_ARGS(), message_len; + + if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sll", &message, &message_len, &code, &severity) == FAILURE) { + zend_error(E_CORE_ERROR, "Wrong parameter count for exception([string $exception [, long $code ]])"); + } + + object = getThis(); + + if (message) { + zend_update_property_string(default_exception_ce, object, "message", sizeof("message")-1, message TSRMLS_CC); + } + + if (code) { + zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC); + } + + zend_update_property_long(default_exception_ce, object, "severity", sizeof("severity")-1, severity TSRMLS_CC); +} + #define DEFAULT_0_PARAMS \ if (ZEND_NUM_ARGS() > 0) { \ ZEND_WRONG_PARAM_COUNT(); \ @@ -177,6 +212,13 @@ _default_exception_get_entry(getThis(), "trace", sizeof("trace")-1, return_value TSRMLS_CC); } +ZEND_METHOD(error_exception, getSeverity) +{ + DEFAULT_0_PARAMS; + + _default_exception_get_entry(getThis(), "severity", sizeof("severity")-1, return_value TSRMLS_CC); +} + /* {{{ ZEND_METHOD(exception, gettraceasstring) */ #define TRACE_APPEND_CHR(chr) \ *str = (char*)erealloc(*str, *len + 1 + 1); \ @@ -427,9 +469,15 @@ * And never try to change the state of exceptions and never implement anything * that gives the user anything to accomplish this. */ +static +ZEND_BEGIN_ARG_INFO(arginfo_exception___construct, 0) + ZEND_ARG_INFO(0, message) + ZEND_ARG_INFO(0, code) +ZEND_END_ARG_INFO(); + static zend_function_entry default_exception_functions[] = { ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL) - ZEND_ME(exception, __construct, NULL, 0) + ZEND_ME(exception, __construct, arginfo_exception___construct, 0) 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) @@ -440,6 +488,19 @@ {NULL, NULL, NULL} }; +static +ZEND_BEGIN_ARG_INFO(arginfo_error_exception___construct, 0) + ZEND_ARG_INFO(0, message) + ZEND_ARG_INFO(0, code) + ZEND_ARG_INFO(0, severity) +ZEND_END_ARG_INFO(); + +static zend_function_entry error_exception_functions[] = { + ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, 0) + ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + {NULL, NULL, NULL} +}; + void zend_register_default_exception(TSRMLS_D) { zend_class_entry ce; @@ -456,6 +517,11 @@ zend_declare_property_null(default_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(default_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_null(default_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC); + + INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions); + error_exception_ce = zend_register_internal_class_ex(&ce, default_exception_ce, NULL TSRMLS_CC); + error_exception_ce->create_object = zend_error_exception_new; + zend_declare_property_long(error_exception_ce, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED TSRMLS_CC); } ZEND_API zend_class_entry *zend_exception_get_default(void) @@ -463,15 +529,15 @@ return default_exception_ce; } -ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...) +ZEND_API zend_class_entry *zend_get_error_exception(void) { - zval *ex; - va_list arg; - char *message; + return error_exception_ce; +} - va_start(arg, format); - zend_vspprintf(&message, 0, format, arg); - va_end(arg); + +ZEND_API zval * zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC) +{ + zval *ex; MAKE_STD_ZVAL(ex); if (exception_ce) { @@ -492,17 +558,29 @@ zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC); } - efree(message); - zend_throw_exception_internal(ex TSRMLS_CC); + return ex; } -ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC) +ZEND_API zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...) { - zend_throw_exception_ex(exception_ce, code TSRMLS_CC, "%s", message); + va_list arg; + char *message; + + va_start(arg, format); + zend_vspprintf(&message, 0, format, arg); + va_end(arg); + return zend_throw_exception(exception_ce, message, code TSRMLS_CC); } + +ZEND_API zval * zend_throw_error_exception(zend_class_entry *exception_ce, char *message, long code, int severity TSRMLS_DC) +{ + zval *ex = zend_throw_exception(exception_ce, message, code TSRMLS_CC); + zend_update_property_long(default_exception_ce, ex, "severity", sizeof("severity")-1, severity TSRMLS_CC); + return ex; +} static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) { http://cvs.php.net/diff.php/ZendEngine2/zend_exceptions.h?r1=1.19&r2=1.20&ty=u Index: ZendEngine2/zend_exceptions.h diff -u ZendEngine2/zend_exceptions.h:1.19 ZendEngine2/zend_exceptions.h:1.20 --- ZendEngine2/zend_exceptions.h:1.19 Thu May 20 13:59:27 2004 +++ ZendEngine2/zend_exceptions.h Thu Jul 15 18:21:36 2004 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_exceptions.h,v 1.19 2004/05/20 17:59:27 wez Exp $ */ +/* $Id: zend_exceptions.h,v 1.20 2004/07/15 22:21:36 helly Exp $ */ #ifndef ZEND_EXCEPTIONS_H #define ZEND_EXCEPTIONS_H @@ -31,14 +31,17 @@ void zend_register_default_exception(TSRMLS_D); ZEND_API zend_class_entry *zend_exception_get_default(void); +ZEND_API zend_class_entry *zend_get_error_exception(void); ZEND_API void zend_register_default_classes(TSRMLS_D); /* exception_ce NULL or zend_exception_get_default() or a derived class * message NULL or the message of the exception */ -ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC); -ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...); +ZEND_API zval * zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC); +ZEND_API zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...); ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC); ZEND_API void zend_clear_exception(TSRMLS_D); + +ZEND_API zval * zend_throw_error_exception(zend_class_entry *exception_ce, char *message, long code, int severity TSRMLS_DC); extern ZEND_API void (*zend_throw_exception_hook)(zval *ex TSRMLS_DC);
« previous php.zend-engine.cvs (#2738) next »