]> granicus.if.org Git - php/commitdiff
Exceptions updates:
authorZeev Suraski <zeev@php.net>
Thu, 12 Feb 2004 10:24:40 +0000 (10:24 +0000)
committerZeev Suraski <zeev@php.net>
Thu, 12 Feb 2004 10:24:40 +0000 (10:24 +0000)
- Enforce exceptions to be derived from class Exception.  This allows
  users to perform catch-all.  It's not yet complete, so don't get
  comfortable with it just yet :)  Updates are coming soon.
- Implement zend_throw_exception() using zend_throw_exception_ex()

Zend/zend_default_classes.c
Zend/zend_default_classes.h
Zend/zend_exceptions.c
Zend/zend_exceptions.h
Zend/zend_execute.h
Zend/zend_execute_API.c

index d9a88dce9cfccc66291dc52bff2e1c18220a0706..1195d5c8b28eea7bbea50bee52860f726a0b10bb 100644 (file)
@@ -25,7 +25,7 @@
 #include "zend_builtin_functions.h"
 #include "zend_interfaces.h"
 
-static zend_class_entry *default_exception_ce;
+zend_class_entry *default_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);
 
@@ -452,36 +452,13 @@ ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code
        zend_throw_exception_internal(ex TSRMLS_CC);
 }
 
-/* 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.
- */
+
 ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
 {
-       zval *ex;
-
-       MAKE_STD_ZVAL(ex);
-       if (exception_ce) {
-               if (!instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
-                       zend_error(E_NOTICE, "Exceptions must be derived from exception");
-                       exception_ce = default_exception_ce;
-               }
-       } else {
-               exception_ce = default_exception_ce;
-       }
-       object_init_ex(ex, exception_ce);
-       
-
-       if (message) {
-               zend_update_property_string(default_exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
-       }
-       if (code) {
-               zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
-       }
-
-       zend_throw_exception_internal(ex TSRMLS_CC);
+       zend_throw_exception_ex(exception_ce, code TSRMLS_CC, "%s", message);
 }
 
+
 static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
 {
        va_list args;
@@ -534,6 +511,24 @@ ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
        }
 }
 
+
+ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC)
+{
+       zend_class_entry *exception_ce;
+
+       if (exception == NULL || exception->type != IS_OBJECT) {
+               zend_error(E_ERROR, "Need to supply an object when throwing an exception");
+       }
+
+       exception_ce = Z_OBJCE_P(exception);
+
+       if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
+               zend_error(E_ERROR, "Exceptions must valid objects that are derived from class Exception");
+       }
+       zend_throw_exception_internal(exception TSRMLS_CC);
+}
+
+
 ZEND_API void zend_register_default_classes(TSRMLS_D)
 {
        zend_register_interfaces(TSRMLS_C);
index def3440e55d60aa879d2d4db096599298a5a404a..68dd208f2087edbc129e6456ec33c0326764c5de 100644 (file)
@@ -31,6 +31,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
  * 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 void zend_throw_exception_object(zval *exception TSRMLS_DC);
 
 /* show an exception using zend_error(E_ERROR,...) */
 ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
index d9a88dce9cfccc66291dc52bff2e1c18220a0706..1195d5c8b28eea7bbea50bee52860f726a0b10bb 100644 (file)
@@ -25,7 +25,7 @@
 #include "zend_builtin_functions.h"
 #include "zend_interfaces.h"
 
-static zend_class_entry *default_exception_ce;
+zend_class_entry *default_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);
 
@@ -452,36 +452,13 @@ ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code
        zend_throw_exception_internal(ex TSRMLS_CC);
 }
 
-/* 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.
- */
+
 ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC)
 {
-       zval *ex;
-
-       MAKE_STD_ZVAL(ex);
-       if (exception_ce) {
-               if (!instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
-                       zend_error(E_NOTICE, "Exceptions must be derived from exception");
-                       exception_ce = default_exception_ce;
-               }
-       } else {
-               exception_ce = default_exception_ce;
-       }
-       object_init_ex(ex, exception_ce);
-       
-
-       if (message) {
-               zend_update_property_string(default_exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
-       }
-       if (code) {
-               zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
-       }
-
-       zend_throw_exception_internal(ex TSRMLS_CC);
+       zend_throw_exception_ex(exception_ce, code TSRMLS_CC, "%s", message);
 }
 
+
 static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
 {
        va_list args;
@@ -534,6 +511,24 @@ ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
        }
 }
 
+
+ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC)
+{
+       zend_class_entry *exception_ce;
+
+       if (exception == NULL || exception->type != IS_OBJECT) {
+               zend_error(E_ERROR, "Need to supply an object when throwing an exception");
+       }
+
+       exception_ce = Z_OBJCE_P(exception);
+
+       if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
+               zend_error(E_ERROR, "Exceptions must valid objects that are derived from class Exception");
+       }
+       zend_throw_exception_internal(exception TSRMLS_CC);
+}
+
+
 ZEND_API void zend_register_default_classes(TSRMLS_D)
 {
        zend_register_interfaces(TSRMLS_C);
index def3440e55d60aa879d2d4db096599298a5a404a..68dd208f2087edbc129e6456ec33c0326764c5de 100644 (file)
@@ -31,6 +31,7 @@ ZEND_API void zend_register_default_classes(TSRMLS_D);
  * 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 void zend_throw_exception_object(zval *exception TSRMLS_DC);
 
 /* show an exception using zend_error(E_ERROR,...) */
 ZEND_API void zend_exception_error(zval *exception TSRMLS_DC);
index db339692519c28b7ad3c3ff1d6014219f57e7b21..3f5d5cc4f8d82aefc0a7dbeb9b83e89b798e2528 100644 (file)
@@ -65,8 +65,6 @@ ZEND_API int zend_lookup_class(char *name, int name_length, zend_class_entry ***
 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
 
-ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC);
-
 static inline int i_zend_is_true(zval *op)
 {
        int result;
index ee1d88ca6886b3a3ea6518cc548c8a3a407c214c..72dd623979abcfd22b10cb072efe677ad984dbda 100644 (file)
@@ -1221,13 +1221,6 @@ void zend_throw_exception_internal(zval *exception TSRMLS_DC)
        EG(current_execute_data)->opline = &EG(active_op_array)->opcodes[EG(active_op_array)->last-1-1];
 }
 
-ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC)
-{
-       if (exception == NULL) {
-               zend_error(E_ERROR, "Need to supply object when throwing exception");
-       }
-       zend_throw_exception_internal(exception TSRMLS_CC);
-}