• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / parallel / 20431104443

22 Dec 2025 11:51AM UTC coverage: 94.99% (-1.9%) from 96.873%
20431104443

push

github

realFlowControl
bump version

2844 of 2994 relevant lines covered (94.99%)

6449.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.54
/src/exceptions.c
1
/*
2
  +----------------------------------------------------------------------+
3
  | parallel                                                             |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2019-2024                                  |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP license,      |
8
  | that is bundled with this package in the file LICENSE, and is        |
9
  | available through the world-wide-web at the following url:           |
10
  | http://www.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: krakjoe                                                      |
16
  +----------------------------------------------------------------------+
17
 */
18
#ifndef HAVE_PARALLEL_EXCEPTIONS
19
#define HAVE_PARALLEL_EXCEPTIONS
20

21
#include "parallel.h"
22

23
struct _php_parallel_exception_t {
24
        zval class;
25
        zval                        file;
26
        zval                        line;
27
        zval                        code;
28
        zval                        message;
29
        zval                        trace;
30
        zval                        previous;
31
        const zend_object_handlers *handlers;
32
};
33

34
zend_class_entry               *php_parallel_error_ce;
35
zend_class_entry               *php_parallel_error_invalid_arguments_ce;
36

37
zend_class_entry               *php_parallel_runtime_error_ce;
38
zend_class_entry               *php_parallel_runtime_error_bootstrap_ce;
39
zend_class_entry               *php_parallel_runtime_error_closed_ce;
40
zend_class_entry               *php_parallel_runtime_error_killed_ce;
41
zend_class_entry               *php_parallel_runtime_error_illegal_function_ce;
42
zend_class_entry               *php_parallel_runtime_error_illegal_instruction_ce;
43
zend_class_entry               *php_parallel_runtime_error_illegal_variable_ce;
44
zend_class_entry               *php_parallel_runtime_error_illegal_parameter_ce;
45
zend_class_entry               *php_parallel_runtime_error_illegal_type_ce;
46
zend_class_entry               *php_parallel_runtime_error_illegal_return_ce;
47

48
zend_class_entry               *php_parallel_future_error_ce;
49
zend_class_entry               *php_parallel_future_error_killed_ce;
50
zend_class_entry               *php_parallel_future_error_cancelled_ce;
51
zend_class_entry               *php_parallel_future_error_foreign_ce;
52

53
zend_class_entry               *php_parallel_channel_error_ce;
54
zend_class_entry               *php_parallel_channel_error_existence_ce;
55
zend_class_entry               *php_parallel_channel_error_illegal_value_ce;
56
zend_class_entry               *php_parallel_channel_error_closed_ce;
57

58
zend_class_entry               *php_parallel_sync_error_ce;
59
zend_class_entry               *php_parallel_sync_error_illegal_value_ce;
60
zend_class_entry               *php_parallel_sync_error_illegal_type_ce;
61
zend_class_entry               *php_parallel_sync_error_illegal_offset_ce;
62
zend_class_entry               *php_parallel_sync_error_illegal_access_ce;
63

64
zend_class_entry               *php_parallel_events_error_ce;
65
zend_class_entry               *php_parallel_events_error_existence_ce;
66
zend_class_entry               *php_parallel_events_error_timeout_ce;
67

68
zend_class_entry               *php_parallel_events_input_error_ce;
69
zend_class_entry               *php_parallel_events_input_error_existence_ce;
70
zend_class_entry               *php_parallel_events_input_error_illegal_value_ce;
71

72
zend_class_entry               *php_parallel_events_event_error_ce;
73

74
static zend_always_inline zval *php_parallel_exceptions_read(zend_object *exception, zend_string *property)
360✔
75
{
76
        zend_property_info *info;
360✔
77
        zend_class_entry   *scope = EG(fake_scope);
360✔
78

79
        EG(fake_scope) = zend_ce_error;
360✔
80

81
        info = zend_get_property_info(zend_ce_error, property, 1);
432✔
82

83
        EG(fake_scope) = scope;
360✔
84

85
        return OBJ_PROP(exception, info->offset);
360✔
86
}
87

88
static zend_always_inline void php_parallel_exceptions_write(zend_object *exception, zend_string *property, zval *value)
432✔
89
{
90
        zend_property_info *info;
432✔
91
        zend_class_entry   *scope = EG(fake_scope);
432✔
92
        zval               *slot;
432✔
93

94
        EG(fake_scope) = zend_ce_error;
432✔
95

96
        info = zend_get_property_info(zend_ce_error, property, 1);
864✔
97

98
        slot = OBJ_PROP(exception, info->offset);
432✔
99

100
        if (Z_REFCOUNTED_P(slot)) {
432✔
101
                zval_ptr_dtor(slot);
×
102
        }
103

104
        ZVAL_COPY_VALUE(slot, value);
432✔
105

106
        EG(fake_scope) = scope;
432✔
107
}
108

109
zend_object *php_parallel_exception_object(zend_class_entry *ce, const char *message, zend_long code, zend_string *file,
×
110
                                           zend_long line)
111
{
112
        zend_object *exception = zend_objects_new(ce);
×
113

114
        object_properties_init(exception, ce);
×
115

116
        if (message) {
×
117
                zend_update_property_string(ce, exception, "message", sizeof("message") - 1, message);
×
118
        }
119

120
        if (code) {
×
121
                zend_update_property_long(ce, exception, "code", sizeof("code") - 1, code);
×
122
        }
123

124
        if (file) {
×
125
                zend_update_property_str(ce, exception, "file", sizeof("file") - 1, file);
×
126
        }
127

128
        if (line) {
×
129
                zend_update_property_long(ce, exception, "line", sizeof("line") - 1, line);
×
130
        }
131

132
        return exception;
×
133
}
134

135
void php_parallel_exceptions_destroy(php_parallel_exception_t *ex)
72✔
136
{
137
        PARALLEL_ZVAL_DTOR(&ex->class);
72✔
138
        PARALLEL_ZVAL_DTOR(&ex->file);
72✔
139
        PARALLEL_ZVAL_DTOR(&ex->line);
72✔
140
        PARALLEL_ZVAL_DTOR(&ex->message);
72✔
141
        PARALLEL_ZVAL_DTOR(&ex->code);
72✔
142
        PARALLEL_ZVAL_DTOR(&ex->trace);
72✔
143
        PARALLEL_ZVAL_DTOR(&ex->previous);
72✔
144

145
        pefree(ex, 1);
72✔
146
}
72✔
147

148
void php_parallel_exceptions_save(zval *saved, zend_object *exception)
72✔
149
{
150
        zval class, *file = php_parallel_exceptions_read(exception, ZSTR_KNOWN(ZEND_STR_FILE)),
72✔
151
                    *line = php_parallel_exceptions_read(exception, ZSTR_KNOWN(ZEND_STR_LINE)),
72✔
152
                    *message = php_parallel_exceptions_read(exception, ZSTR_KNOWN(ZEND_STR_MESSAGE)),
72✔
153
                    *code = php_parallel_exceptions_read(exception, ZSTR_KNOWN(ZEND_STR_CODE)),
72✔
154
                    *trace = php_parallel_exceptions_read(exception, ZSTR_KNOWN(ZEND_STR_TRACE)), previous;
72✔
155

156
        php_parallel_exception_t *ex = (php_parallel_exception_t *)pecalloc(1, sizeof(php_parallel_exception_t), 1);
72✔
157

158
        /* todo */
159
        ZVAL_NULL(&previous);
72✔
160

161
        ZVAL_STR(&class, exception->ce->name);
72✔
162

163
        PARALLEL_ZVAL_COPY(&ex->class, &class, 1);
72✔
164
        PARALLEL_ZVAL_COPY(&ex->file, file, 1);
72✔
165
        PARALLEL_ZVAL_COPY(&ex->line, line, 1);
72✔
166
        PARALLEL_ZVAL_COPY(&ex->message, message, 1);
72✔
167
        PARALLEL_ZVAL_COPY(&ex->code, code, 1);
72✔
168
        PARALLEL_ZVAL_COPY(&ex->trace, trace, 1);
72✔
169
        PARALLEL_ZVAL_COPY(&ex->previous, &previous, 1);
72✔
170

171
        ex->handlers = exception->handlers;
72✔
172

173
        ZVAL_PTR(saved, ex);
72✔
174

175
        zend_clear_exception();
72✔
176
}
72✔
177

178
zend_object *php_parallel_exceptions_restore(zval *exception)
72✔
179
{
180
        zend_object              *object;
72✔
181
        zend_class_entry         *type;
72✔
182
        zval                      file, line, message, code, trace, previous;
72✔
183

184
        php_parallel_exception_t *ex = (php_parallel_exception_t *)Z_PTR_P(exception);
72✔
185

186
        PARALLEL_ZVAL_COPY(&file, &ex->file, 0);
72✔
187
        PARALLEL_ZVAL_COPY(&line, &ex->line, 0);
72✔
188
        PARALLEL_ZVAL_COPY(&message, &ex->message, 0);
72✔
189
        PARALLEL_ZVAL_COPY(&code, &ex->code, 0);
72✔
190
        PARALLEL_ZVAL_COPY(&trace, &ex->trace, 0);
72✔
191
        PARALLEL_ZVAL_COPY(&previous, &ex->previous, 0);
72✔
192

193
        type = zend_lookup_class(Z_STR(ex->class));
72✔
194

195
        if (!type) {
72✔
196
                /* we lost type info */
197
                type = php_parallel_future_error_foreign_ce;
×
198
        }
199

200
        object = zend_objects_new(type);
72✔
201
        object->handlers = ex->handlers;
72✔
202
        object_properties_init(object, type);
72✔
203

204
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_FILE), &file);
72✔
205
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_LINE), &line);
72✔
206
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_MESSAGE), &message);
72✔
207
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_CODE), &code);
72✔
208
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
72✔
209
        php_parallel_exceptions_write(object, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &previous);
72✔
210

211
        return object;
72✔
212
}
213

214
PHP_MINIT_FUNCTION(PARALLEL_EXCEPTIONS)
3,210✔
215
{
216
        zend_class_entry ce;
3,210✔
217

218
        /*
219
         * Base Exceptions
220
         */
221
        INIT_NS_CLASS_ENTRY(ce, "parallel", "Error", NULL);
3,210✔
222
        php_parallel_error_ce = zend_register_internal_class_ex(&ce, zend_ce_error);
3,210✔
223

224
        /*
225
         * Runtime Exceptions
226
         */
227
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime", "Error", NULL);
3,210✔
228
        php_parallel_runtime_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
229

230
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "Bootstrap", NULL);
3,210✔
231
        php_parallel_runtime_error_bootstrap_ce = zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
232

233
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "Closed", NULL);
3,210✔
234
        php_parallel_runtime_error_closed_ce = zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
235

236
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "Killed", NULL);
3,210✔
237
        php_parallel_runtime_error_killed_ce = zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
238

239
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "IllegalFunction", NULL);
3,210✔
240
        php_parallel_runtime_error_illegal_function_ce =
6,420✔
241
            zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
242

243
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "IllegalVariable", NULL);
3,210✔
244
        php_parallel_runtime_error_illegal_variable_ce =
6,420✔
245
            zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
246

247
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "IllegalParameter", NULL);
3,210✔
248
        php_parallel_runtime_error_illegal_parameter_ce =
6,420✔
249
            zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
250

251
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "IllegalInstruction", NULL);
3,210✔
252
        php_parallel_runtime_error_illegal_instruction_ce =
6,420✔
253
            zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
254

255
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Runtime\\Error", "IllegalReturn", NULL);
3,210✔
256
        php_parallel_runtime_error_illegal_return_ce = zend_register_internal_class_ex(&ce, php_parallel_runtime_error_ce);
3,210✔
257

258
        /*
259
         * Future Exceptions
260
         */
261
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Future", "Error", NULL);
3,210✔
262
        php_parallel_future_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
263

264
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Future\\Error", "Killed", NULL);
3,210✔
265
        php_parallel_future_error_killed_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
266

267
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Future\\Error", "Cancelled", NULL);
3,210✔
268
        php_parallel_future_error_cancelled_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
269

270
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Future\\Error", "Foreign", NULL);
3,210✔
271
        php_parallel_future_error_foreign_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
272

273
        /*
274
         * Channel Exceptions
275
         */
276
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Channel", "Error", NULL);
3,210✔
277
        php_parallel_channel_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
278

279
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Channel\\Error", "Existence", NULL);
3,210✔
280
        php_parallel_channel_error_existence_ce = zend_register_internal_class_ex(&ce, php_parallel_channel_error_ce);
3,210✔
281

282
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Channel\\Error", "IllegalValue", NULL);
3,210✔
283
        php_parallel_channel_error_illegal_value_ce = zend_register_internal_class_ex(&ce, php_parallel_channel_error_ce);
3,210✔
284

285
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Channel\\Error", "Closed", NULL);
3,210✔
286
        php_parallel_channel_error_closed_ce = zend_register_internal_class_ex(&ce, php_parallel_channel_error_ce);
3,210✔
287

288
        /*
289
         * Sync Exceptions
290
         */
291
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Sync", "Error", NULL);
3,210✔
292
        php_parallel_sync_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
293

294
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Sync\\Error", "IllegalValue", NULL);
3,210✔
295
        php_parallel_sync_error_illegal_value_ce = zend_register_internal_class_ex(&ce, php_parallel_sync_error_ce);
3,210✔
296

297
        /*
298
         * Events Exceptions
299
         */
300
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events", "Error", NULL);
3,210✔
301
        php_parallel_events_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
302

303
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Error", "Existence", NULL);
3,210✔
304
        php_parallel_events_error_existence_ce = zend_register_internal_class_ex(&ce, php_parallel_events_error_ce);
3,210✔
305

306
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Error", "Timeout", NULL);
3,210✔
307
        php_parallel_events_error_timeout_ce = zend_register_internal_class_ex(&ce, php_parallel_events_error_ce);
3,210✔
308

309
        /*
310
         * Input Exceptions
311
         */
312
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Input", "Error", NULL);
3,210✔
313
        php_parallel_events_input_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
314

315
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Input\\Error", "Existence", NULL);
3,210✔
316
        php_parallel_events_input_error_existence_ce =
6,420✔
317
            zend_register_internal_class_ex(&ce, php_parallel_events_input_error_ce);
3,210✔
318

319
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Input\\Error", "IllegalValue", NULL);
3,210✔
320
        php_parallel_events_input_error_illegal_value_ce =
6,420✔
321
            zend_register_internal_class_ex(&ce, php_parallel_events_input_error_ce);
3,210✔
322

323
        /*
324
         * Event Exceptions
325
         */
326
        INIT_NS_CLASS_ENTRY(ce, "parallel\\Events\\Event", "Error", NULL);
3,210✔
327
        php_parallel_events_event_error_ce = zend_register_internal_class_ex(&ce, php_parallel_error_ce);
3,210✔
328

329
        return SUCCESS;
3,210✔
330
}
331

332
PHP_MSHUTDOWN_FUNCTION(PARALLEL_EXCEPTIONS) { return SUCCESS; }
3,210✔
333
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc