1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
+----------------------------------------------------------------------+
| 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, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| 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> |
| Marcus Boerger <helly@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "zend.h"
#include "zend_API.h"
#include "zend_reflection_api.h"
#include "zend_builtin_functions.h"
zend_class_entry *default_exception_ptr;
zend_class_entry *abstract_exception_ptr;
static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC)
{
zval tmp, obj;
zend_object *object;
zval *trace;
obj.value.obj = zend_objects_new(&object, class_type TSRMLS_CC);
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 *));
ALLOC_ZVAL(trace);
trace->is_ref = 0;
trace->refcount = 0;
zend_fetch_debug_backtrace(trace, 0 TSRMLS_CC);
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);
zend_update_property(class_type, &obj, "trace", sizeof("trace")-1, trace TSRMLS_CC);
return obj.value.obj;
}
ZEND_METHOD(exception, __construct)
{
char *message = NULL;
long code = 0;
zval *object;
int argc = ZEND_NUM_ARGS(), message_len;
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 ]])");
}
object = getThis();
if (message) {
zend_update_property_string(Z_OBJCE_P(object), object, "message", sizeof("message")-1, message TSRMLS_CC);
}
if (code) {
zend_update_property_long(Z_OBJCE_P(object), object, "code", sizeof("code")-1, code TSRMLS_CC);
}
}
#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)
{
zval *value;
value = zend_read_property(Z_OBJCE_P(object), object, name, name_len, 0 TSRMLS_CC);
*return_value = *value;
zval_copy_ctor(return_value);
}
ZEND_METHOD(exception, getfile)
{
DEFAULT_0_PARAMS;
_default_exception_get_entry(getThis(), "file", sizeof("file")-1, return_value TSRMLS_CC);
}
ZEND_METHOD(exception, getline)
{
DEFAULT_0_PARAMS;
_default_exception_get_entry(getThis(), "line", sizeof("line")-1, return_value TSRMLS_CC);
}
ZEND_METHOD(exception, getmessage)
{
DEFAULT_0_PARAMS;
_default_exception_get_entry(getThis(), "message", sizeof("message")-1, return_value TSRMLS_CC);
}
ZEND_METHOD(exception, getcode)
{
DEFAULT_0_PARAMS;
_default_exception_get_entry(getThis(), "code", sizeof("code")-1, return_value TSRMLS_CC);
}
ZEND_METHOD(exception, gettrace)
{
DEFAULT_0_PARAMS;
_default_exception_get_entry(getThis(), "trace", sizeof("trace")-1, return_value TSRMLS_CC);
}
/* 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
* such exceptions in that handler.
*/
static zend_function_entry default_exception_functions[] = {
ZEND_ME(exception, __construct, NULL, 0)
ZEND_ME(exception, getmessage, NULL, 0) /* non final for now */
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)
{NULL, NULL, NULL}
};
static void zend_register_default_exception(TSRMLS_D)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "exception", default_exception_functions);
default_exception_ptr = zend_register_internal_class(&ce TSRMLS_CC);
default_exception_ptr->create_object = zend_default_exception_new;
zend_declare_property_string(default_exception_ptr, "message", sizeof("message")-1, "Unknown exception", ZEND_ACC_PROTECTED);
zend_declare_property_long(default_exception_ptr, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(default_exception_ptr, "trace", sizeof("trace")-1, ZEND_ACC_PROTECTED);
INIT_CLASS_ENTRY(ce, "abstract_exception", NULL);
abstract_exception_ptr = zend_register_internal_class_ex(&ce, default_exception_ptr, NULL TSRMLS_CC);
}
ZEND_API zend_class_entry *zend_exception_get_default(void)
{
return default_exception_ptr;
}
ZEND_API zend_class_entry *zend_exception_get_abstract(void)
{
return abstract_exception_ptr;
}
ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...)
{
zval *ex;
va_list arg;
#ifdef _GNU_SOURCE
char *message;
va_start(arg, format);
vasprintf(message, format, arg);
va_end(arg);
#else
char message[1024];
va_start(arg, format);
vsnprintf(message, sizeof(message), format, arg);
va_end(arg);
#endif
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);
}
#ifdef _GNU_SOURCE
free(message);
#endif
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.
*/
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_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);
}
EG(exception) = ex;
}
static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...)
{
va_list args;
va_start(args, format);
zend_error_cb(E_ERROR, file, lineno, format, args);
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)) {
zval *message = zend_read_property(default_exception_ptr, exception, "message", sizeof("message")-1, 1 TSRMLS_CC);
zval *file = zend_read_property(default_exception_ptr, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
zval *lineno = zend_read_property(default_exception_ptr, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
zend_error_va(E_ERROR, Z_STRVAL_P(file), Z_LVAL_P(lineno), "Uncaught exception '%s' with message '%s'", Z_OBJCE_P(exception)->name, Z_STRVAL_P(message));
} else {
zend_error(E_ERROR, "Uncaught exception '%s'!", Z_OBJCE_P(exception)->name);
}
}
ZEND_API void zend_register_default_classes(TSRMLS_D)
{
zend_register_default_exception(TSRMLS_C);
zend_register_reflection_api(TSRMLS_C);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/
|