-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexceptions.c
More file actions
401 lines (323 loc) · 11.7 KB
/
exceptions.c
File metadata and controls
401 lines (323 loc) · 11.7 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Edmond |
+----------------------------------------------------------------------+
*/
#include "php_async.h"
#include "exceptions.h"
#include <zend_API.h>
#include <zend_exceptions.h>
#include "php.h"
#include "exceptions_arginfo.h"
#include "zend_common.h"
zend_class_entry *async_ce_async_exception = NULL;
zend_class_entry *async_ce_cancellation_exception = NULL;
zend_class_entry *async_ce_operation_cancelled_exception = NULL;
zend_class_entry *async_ce_input_output_exception = NULL;
zend_class_entry *async_ce_timeout_exception = NULL;
zend_class_entry *async_ce_poll_exception = NULL;
zend_class_entry *async_ce_dns_exception = NULL;
zend_class_entry *async_ce_deadlock_error = NULL;
zend_class_entry *async_ce_composite_exception = NULL;
zend_class_entry *async_ce_service_unavailable_exception = NULL;
PHP_METHOD(Async_CompositeException, addException)
{
zval *exception;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS(exception, zend_ce_throwable)
ZEND_PARSE_PARAMETERS_END();
zval *object = ZEND_THIS;
async_composite_exception_add_exception(Z_OBJ_P(object), Z_OBJ_P(exception), true);
}
PHP_METHOD(Async_CompositeException, getExceptions)
{
ZEND_PARSE_PARAMETERS_NONE();
zval *object = ZEND_THIS;
zval *exceptions_prop = zend_read_property(
async_ce_composite_exception, Z_OBJ_P(object), "exceptions", sizeof("exceptions") - 1, 0, NULL);
if (Z_TYPE_P(exceptions_prop) == IS_ARRAY) {
RETURN_ZVAL(exceptions_prop, 1, 0);
} else {
array_init(return_value);
}
}
void async_register_exceptions_ce(void)
{
async_ce_async_exception = register_class_Async_AsyncException(zend_ce_exception);
async_ce_cancellation_exception = register_class_Async_AsyncCancellation(zend_ce_cancellation);
async_ce_operation_cancelled_exception =
register_class_Async_OperationCanceledException(async_ce_cancellation_exception);
async_ce_input_output_exception = register_class_Async_InputOutputException(zend_ce_exception);
async_ce_timeout_exception = register_class_Async_TimeoutException(zend_ce_exception);
async_ce_poll_exception = register_class_Async_PollException(zend_ce_exception);
async_ce_dns_exception = register_class_Async_DnsException(zend_ce_exception);
async_ce_deadlock_error = register_class_Async_DeadlockError(zend_ce_error);
async_ce_composite_exception = register_class_Async_CompositeException(zend_ce_exception);
async_ce_service_unavailable_exception = register_class_Async_ServiceUnavailableException(async_ce_async_exception);
}
PHP_ASYNC_API zend_object *async_new_exception(zend_class_entry *exception_ce, const char *format, ...)
{
zval exception, message_val;
if (!exception_ce) {
exception_ce = zend_ce_exception;
}
ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable) && "Exceptions must implement Throwable");
object_init_ex(&exception, exception_ce);
va_list args;
va_start(args, format);
zend_string *message = zend_vstrpprintf(0, format, args);
va_end(args);
if (message) {
ZVAL_STR(&message_val, message);
zend_update_property_ex(exception_ce, Z_OBJ(exception), ZSTR_KNOWN(ZEND_STR_MESSAGE), &message_val);
}
zend_string_release(message);
return Z_OBJ(exception);
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_error(const char *format, ...)
{
va_list args;
va_start(args, format);
zend_string *message = zend_vstrpprintf(0, format, args);
va_end(args);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception(async_ce_async_exception, ZSTR_VAL(message), 0);
} else {
obj = async_new_exception(async_ce_async_exception, ZSTR_VAL(message));
async_apply_exception_to_context(obj);
}
zend_string_release(message);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_cancellation(const char *format, ...)
{
const zend_object *previous_exception = EG(exception);
if (format == NULL && previous_exception != NULL &&
instanceof_function(previous_exception->ce, async_ce_cancellation_exception)) {
format = "The operation was canceled by timeout";
} else {
format = format ? format : "The operation was canceled";
}
va_list args;
va_start(args, format);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception_ex(async_ce_cancellation_exception, 0, format, args);
} else {
obj = async_new_exception(async_ce_cancellation_exception, format, args);
async_apply_exception_to_context(obj);
}
va_end(args);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_input_output(const char *format, ...)
{
format = format ? format : "An input/output error occurred.";
va_list args;
va_start(args, format);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception_ex(async_ce_input_output_exception, 0, format, args);
} else {
obj = async_new_exception(async_ce_input_output_exception, format, args);
async_apply_exception_to_context(obj);
}
va_end(args);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_timeout(const char *format, const zend_long timeout)
{
format = format ? format : "A timeout of %u microseconds occurred";
if (EXPECTED(EG(current_execute_data))) {
return zend_throw_exception_ex(async_ce_timeout_exception, 0, format, timeout);
} else {
zend_object *obj = async_new_exception(async_ce_timeout_exception, format, timeout);
async_apply_exception_to_context(obj);
return obj;
}
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_poll(const char *format, ...)
{
va_list args;
va_start(args, format);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception_ex(async_ce_poll_exception, 0, format, args);
} else {
obj = async_new_exception(async_ce_poll_exception, format, args);
async_apply_exception_to_context(obj);
}
va_end(args);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_deadlock(const char *format, ...)
{
format = format ? format : "A deadlock was detected";
va_list args;
va_start(args, format);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception_ex(async_ce_deadlock_error, 0, format, args);
} else {
obj = async_new_exception(async_ce_deadlock_error, format, args);
async_apply_exception_to_context(obj);
}
va_end(args);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_throw_service_unavailable(const char *format, ...)
{
format = format ? format : "Service is unavailable";
va_list args;
va_start(args, format);
zend_object *obj = NULL;
if (EXPECTED(EG(current_execute_data))) {
obj = zend_throw_exception_ex(async_ce_service_unavailable_exception, 0, format, args);
} else {
obj = async_new_exception(async_ce_service_unavailable_exception, format, args);
async_apply_exception_to_context(obj);
}
va_end(args);
return obj;
}
PHP_ASYNC_API ZEND_COLD zend_object *async_new_composite_exception(void)
{
zval composite;
object_init_ex(&composite, async_ce_composite_exception);
return Z_OBJ(composite);
}
PHP_ASYNC_API void
async_composite_exception_add_exception(zend_object *composite, zend_object *exception, bool transfer)
{
if (composite == NULL || exception == NULL) {
return;
}
zval *exceptions_prop = &composite->properties_table[7];
if (Z_TYPE_P(exceptions_prop) == IS_UNDEF) {
array_init(exceptions_prop);
}
zval exception_zval;
ZVAL_OBJ(&exception_zval, exception);
if (UNEXPECTED(zend_hash_next_index_insert(Z_ARRVAL_P(exceptions_prop), &exception_zval) == NULL)) {
zend_error(E_CORE_WARNING, "Failed to add exception to composite exception");
if (transfer) {
OBJ_RELEASE(exception);
}
} else if (false == transfer) {
GC_ADDREF(exception);
}
}
static void exception_coroutine_dispose(zend_coroutine_t *coroutine)
{
if (coroutine->extended_data != NULL) {
zend_object *exception_obj = coroutine->extended_data;
coroutine->extended_data = NULL;
async_rethrow_exception(exception_obj);
}
}
static void exception_coroutine_entry(void)
{
zend_coroutine_t *coroutine = ZEND_ASYNC_CURRENT_COROUTINE;
if (UNEXPECTED(coroutine == NULL || coroutine->extended_data == NULL)) {
return;
}
zend_object *exception = coroutine->extended_data;
coroutine->extended_data = NULL;
async_rethrow_exception(exception);
}
bool async_spawn_and_throw(zend_object *exception, zend_async_scope_t *scope, int32_t priority)
{
if (scope == NULL) {
scope = ZEND_ASYNC_CURRENT_SCOPE;
}
// If the current Scope can no longer create coroutines, we use a trick.
// We create a child Scope with a single coroutine.
if (ZEND_ASYNC_SCOPE_IS_CANCELLED(scope) || ZEND_ASYNC_SCOPE_IS_CLOSED(scope)) {
scope = ZEND_ASYNC_NEW_SCOPE(scope);
if (UNEXPECTED(scope == NULL)) {
async_warning("Failed to create a new Scope for throwing an exception in a coroutine.");
return false;
}
}
zend_coroutine_t *coroutine = ZEND_ASYNC_SPAWN_WITH_SCOPE_EX(scope, priority);
if (UNEXPECTED(coroutine == NULL)) {
async_warning("Failed to spawn a coroutine for throwing an exception.");
return false;
}
coroutine->internal_entry = exception_coroutine_entry;
coroutine->extended_data = exception;
coroutine->extended_dispose = exception_coroutine_dispose;
GC_ADDREF(exception);
return true;
}
/**
* Extracts the current exception from the global state, saves it, and clears it.
*
* @return The extracted exception object with an increased reference count.
*/
zend_object *async_extract_exception(void)
{
zend_object *exception = EG(exception);
GC_ADDREF(exception);
zend_clear_exception();
return exception;
}
/**
* Applies the current exception to the provided exception pointer.
*
* If the current exception is not a cancellation exception or a graceful/unwind exit,
* it extracts the current exception and sets it as the new exception.
* If `to_exception` is not NULL, it sets the previous exception to the extracted one.
*
* @param to_exception Pointer to a pointer where the new exception will be set.
*/
void async_apply_exception(zend_object **to_exception)
{
if (UNEXPECTED(
EG(exception) &&
false ==
(instanceof_function(EG(exception)->ce, ZEND_ASYNC_GET_CE(ZEND_ASYNC_EXCEPTION_CANCELLATION)) ||
zend_is_graceful_exit(EG(exception)) || zend_is_unwind_exit(EG(exception))))) {
zend_object *exception = async_extract_exception();
if (*to_exception != NULL) {
zend_exception_set_previous(exception, *to_exception);
}
*to_exception = exception;
}
}
PHP_ASYNC_API void async_rethrow_exception(zend_object *exception)
{
if (EG(current_execute_data)) {
zend_throw_exception_internal(exception);
} else {
async_apply_exception_to_context(exception);
}
}
void async_apply_exception_to_context(zend_object *exception)
{
if (UNEXPECTED(exception == NULL)) {
return;
}
zend_object *previous = EG(exception);
if (previous && zend_is_unwind_exit(previous)) {
/* Don't replace unwinding exception with different exception. */
OBJ_RELEASE(exception);
return;
}
zend_exception_set_previous(exception, previous);
EG(exception) = exception;
if (previous) {
return;
}
}