-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfuture.c
More file actions
1807 lines (1429 loc) · 51.6 KB
/
future.c
File metadata and controls
1807 lines (1429 loc) · 51.6 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| 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 "future.h"
#include "php_async.h"
#include "exceptions.h"
#include "async_API.h"
#include "future_arginfo.h"
#include "zend_exceptions.h"
#include "zend_closures.h"
#include "zend_common.h"
#include "scheduler.h"
#include "iterator.h"
#include "zend_smart_str.h"
///////////////////////////////////////////////////////////
/// Architecture Overview
///////////////////////////////////////////////////////////
/*
* Future Module Architecture
* ==========================
*
* This module implements Future pattern on top of the core zend_future_t event.
*
* Core Layer (Zend/zend_async_API.h):
* ------------------------------------
* zend_future_t - Low-level event container
* - Stores result/exception
* - Manages event lifecycle (start/stop)
* - Maintains callback list
* - NO business logic, only event mechanics
*
* Module Layer (ext/async/):
* --------------------------
* FutureState (async_future_state_t) - Mutable container
* - PHP object wrapper around zend_future_t
* - Provides complete() and error() methods
* - Owns the underlying zend_future_t
*
* Future (async_future_t) - Readonly container with transformation chain
* - PHP object wrapper around the SAME zend_future_t
* - Provides map(), catch(), finally(), await() methods
* - Stores child_futures created by transformations
* - Stores mapper callable (if this Future is a child)
* - Subscribes to zend_future_t via callback mechanism
*
* Object Ownership Scheme:
* ------------------------
*
* FutureState (PHP object)
* │
* │ owns (ref_count)
* ▼
* zend_future_t (core event) ◄────┐
* │ │ references (callback subscription)
* │ has callbacks │
* │ │
* ├─► callback 1 │
* ├─► callback 2 │
* └─► Future callback ─────────┘
* │
* │ when triggered
* ▼
* Future (PHP object)
* │
* │ owns
* ▼
* child_futures[] ──► [Future2, Future3, ...]
*
* Event Flow:
* -----------
* 1. FutureState->complete() called
* └─► zend_future_t->stop() triggered (core)
* └─► ZEND_ASYNC_CALLBACKS_NOTIFY() (core - simple iteration)
* └─► Future callback handler invoked (module)
* └─► async_iterator_run_in_coroutine() (module)
* └─► Process each child future in SEPARATE coroutine
* └─► Call mapper callable
* └─► Resolve child future
*
* Key Design Principles:
* ----------------------
* - Core (zend_future_t) has NO knowledge of Future/FutureState
* - Core only manages event lifecycle and callback notification
* - Module implements business logic via callback subscription
* - Child futures are processed in separate coroutine (NOT synchronously!)
* - Iterator ensures proper async execution without blocking
*/
#define FUTURE_METHOD(name) PHP_METHOD(Async_Future, name)
#define FUTURE_STATE_METHOD(name) PHP_METHOD(Async_FutureState, name)
#define SCHEDULER_LAUNCH \
if (UNEXPECTED(ZEND_ASYNC_CURRENT_COROUTINE == NULL)) { \
async_scheduler_launch(); \
if (UNEXPECTED(EG(exception) != NULL)) { \
RETURN_THROWS(); \
} \
}
zend_class_entry *async_ce_future_state = NULL;
zend_class_entry *async_ce_future = NULL;
static zend_object_handlers async_future_state_handlers;
static zend_object_handlers async_future_handlers;
///////////////////////////////////////////////////////////
/// Future callback structure
///////////////////////////////////////////////////////////
/**
* Callback structure for Future object.
* When parent future completes, this callback processes child futures.
*/
typedef struct
{
zend_async_event_callback_t base;
async_future_t *future_obj;
} async_future_callback_t;
/**
* Context structure for processing a single mapper in a coroutine.
* Used when source future is already completed.
*/
typedef struct
{
async_future_t *child_future_obj;
zend_future_t *parent_future;
} future_mapper_context_t;
///////////////////////////////////////////////////////////
/// Future Iterator (zend_object_iterator)
///////////////////////////////////////////////////////////
/**
* Custom zend_object_iterator for efficient future chain traversal.
* Instead of creating a new async_iterator for each future level,
* this iterator maintains a queue of futures and iterates through
* their children in a flat manner.
*
* Flow:
* 1. Parent future resolves, adds itself to future_queue
* 2. Iterator picks parent from queue, iterates its child_futures
* 3. When child resolves and has its own children, child is added to queue
* 4. Single iterator processes entire future tree
*/
typedef struct
{
zend_object_iterator it;
/* Queue of futures to process (FIFO) */
zend_array *future_queue;
uint32_t queue_hash_iter;
HashPosition queue_pos;
/* Current parent future being processed */
async_future_t *current_parent;
/* Position in current_parent->child_futures */
uint32_t child_hash_iter;
HashPosition child_pos;
/* Current child for get_current_data() */
zval current_child;
} future_iterator_t;
/* Forward declarations for future_iterator functions */
static void future_iterator_dtor(zend_object_iterator *iter);
static zend_result future_iterator_valid(zend_object_iterator *iter);
static zval *future_iterator_get_current_data(zend_object_iterator *iter);
static void future_iterator_get_current_key(zend_object_iterator *iter, zval *key);
static void future_iterator_move_forward(zend_object_iterator *iter);
static void future_iterator_rewind(zend_object_iterator *iter);
static const zend_object_iterator_funcs future_iterator_funcs = {
.dtor = future_iterator_dtor,
.valid = future_iterator_valid,
.get_current_data = future_iterator_get_current_data,
.get_current_key = future_iterator_get_current_key,
.move_forward = future_iterator_move_forward,
.rewind = future_iterator_rewind,
.invalidate_current = NULL,
.get_gc = NULL,
};
/**
* Create a new future_iterator_t.
*/
static future_iterator_t *future_iterator_create(async_future_t *first_future)
{
future_iterator_t *iterator = ecalloc(1, sizeof(future_iterator_t));
zend_iterator_init(&iterator->it);
iterator->it.funcs = &future_iterator_funcs;
/* Create queue and add first future */
iterator->future_queue = zend_new_array(4);
iterator->queue_hash_iter = (uint32_t) -1;
zval first;
ZVAL_OBJ(&first, &first_future->std);
Z_ADDREF(first);
zend_hash_next_index_insert(iterator->future_queue, &first);
iterator->current_parent = NULL;
iterator->child_hash_iter = (uint32_t) -1;
ZVAL_UNDEF(&iterator->current_child);
return iterator;
}
/**
* Destructor for future_iterator_t.
*/
static void future_iterator_dtor(zend_object_iterator *iter)
{
future_iterator_t *iterator = (future_iterator_t *) iter;
if (iterator->queue_hash_iter != (uint32_t) -1) {
zend_hash_iterator_del(iterator->queue_hash_iter);
iterator->queue_hash_iter = (uint32_t) -1;
}
if (iterator->child_hash_iter != (uint32_t) -1) {
zend_hash_iterator_del(iterator->child_hash_iter);
iterator->child_hash_iter = (uint32_t) -1;
}
if (iterator->future_queue != NULL) {
zend_array_destroy(iterator->future_queue);
iterator->future_queue = NULL;
}
zval_ptr_dtor(&iterator->current_child);
ZVAL_UNDEF(&iterator->current_child);
iterator->current_parent = NULL;
// Note: don't efree here - zend_objects_store handles the memory
}
/**
* Extended destructor for async_iterator_t.
* Called from iterator_dtor to properly clean up the zend_iterator.
*/
static void future_async_iterator_dtor(zend_async_iterator_t *async_iter)
{
async_iterator_t *iterator = (async_iterator_t *) async_iter;
// Clear extended_data since zend_iterator_dtor will free the memory
iterator->extended_data = NULL;
if (iterator->zend_iterator != NULL) {
zend_object_iterator *zend_iter = iterator->zend_iterator;
iterator->zend_iterator = NULL;
zend_iterator_dtor(zend_iter);
}
}
/**
* Move to next parent from the queue.
* Returns true if a new parent was found, false if queue is empty.
*/
static bool future_iterator_next_parent(future_iterator_t *iterator)
{
if (iterator->child_hash_iter != (uint32_t) -1) {
zend_hash_iterator_del(iterator->child_hash_iter);
iterator->child_hash_iter = (uint32_t) -1;
}
iterator->current_parent = NULL;
if (iterator->future_queue == NULL || zend_hash_num_elements(iterator->future_queue) == 0) {
return false;
}
/* Get next future from queue */
if (iterator->queue_hash_iter == (uint32_t) -1) {
zend_hash_internal_pointer_reset_ex(iterator->future_queue, &iterator->queue_pos);
iterator->queue_hash_iter = zend_hash_iterator_add(iterator->future_queue, iterator->queue_pos);
} else {
iterator->queue_pos = zend_hash_iterator_pos(iterator->queue_hash_iter, iterator->future_queue);
zend_hash_move_forward_ex(iterator->future_queue, &iterator->queue_pos);
EG(ht_iterators)[iterator->queue_hash_iter].pos = iterator->queue_pos;
}
zval *next_future_zval = zend_hash_get_current_data_ex(iterator->future_queue, &iterator->queue_pos);
if (next_future_zval == NULL) {
return false;
}
iterator->current_parent = ASYNC_FUTURE_FROM_OBJ(Z_OBJ_P(next_future_zval));
/* Initialize child iteration if parent has children */
if (iterator->current_parent->child_futures != NULL &&
zend_hash_num_elements(iterator->current_parent->child_futures) > 0) {
zend_hash_internal_pointer_reset_ex(iterator->current_parent->child_futures, &iterator->child_pos);
iterator->child_hash_iter =
zend_hash_iterator_add(iterator->current_parent->child_futures, iterator->child_pos);
return true;
}
/* Parent has no children, try next parent */
return future_iterator_next_parent(iterator);
}
/**
* Check if iteration is valid.
* If current parent has no more children, switches to next parent from queue.
*/
static zend_result future_iterator_valid(zend_object_iterator *iter)
{
future_iterator_t *iterator = (future_iterator_t *) iter;
while (true) {
if (iterator->current_parent == NULL) {
/* Try to get next parent from queue */
if (!future_iterator_next_parent(iterator)) {
return FAILURE;
}
continue;
}
if (iterator->current_parent->child_futures == NULL) {
/* Parent has no children, try next */
if (!future_iterator_next_parent(iterator)) {
return FAILURE;
}
continue;
}
zval *current = zend_hash_get_current_data_ex(iterator->current_parent->child_futures, &iterator->child_pos);
if (current != NULL) {
return SUCCESS;
}
/* No more children, try next parent */
if (!future_iterator_next_parent(iterator)) {
return FAILURE;
}
}
}
/**
* Get current child future.
*/
static zval *future_iterator_get_current_data(zend_object_iterator *iter)
{
future_iterator_t *iterator = (future_iterator_t *) iter;
if (iterator->current_parent == NULL || iterator->current_parent->child_futures == NULL) {
return NULL;
}
zval *current = zend_hash_get_current_data_ex(iterator->current_parent->child_futures, &iterator->child_pos);
if (current != NULL) {
zval_ptr_dtor(&iterator->current_child);
ZVAL_COPY(&iterator->current_child, current);
return &iterator->current_child;
}
return NULL;
}
/**
* Get current key (numeric index).
*/
static void future_iterator_get_current_key(zend_object_iterator *iter, zval *key)
{
ZVAL_LONG(key, iter->index);
}
/**
* Move to next child or next parent.
*
* IMPORTANT: move_forward is called BEFORE the handler in PHP's iterate() loop.
* We must NOT reset current_parent here because the handler still needs it
* to process the current element. Instead, we just move the position forward.
* The actual parent switch happens in valid() when we detect no more children.
*/
static void future_iterator_move_forward(zend_object_iterator *iter)
{
future_iterator_t *iterator = (future_iterator_t *) iter;
iter->index++;
if (iterator->current_parent == NULL || iterator->current_parent->child_futures == NULL) {
/* Will be handled by valid() */
return;
}
/* Move to next child */
zend_hash_move_forward_ex(iterator->current_parent->child_futures, &iterator->child_pos);
if (iterator->child_hash_iter != (uint32_t) -1) {
EG(ht_iterators)[iterator->child_hash_iter].pos = iterator->child_pos;
}
/* Don't call future_iterator_next_parent here!
* valid() will detect there are no more children and switch parent. */
}
/**
* Rewind to start.
*/
static void future_iterator_rewind(zend_object_iterator *iter)
{
future_iterator_t *iterator = (future_iterator_t *) iter;
iter->index = 0;
/* Reset queue position */
if (iterator->queue_hash_iter != (uint32_t) -1) {
zend_hash_iterator_del(iterator->queue_hash_iter);
iterator->queue_hash_iter = (uint32_t) -1;
}
if (iterator->child_hash_iter != (uint32_t) -1) {
zend_hash_iterator_del(iterator->child_hash_iter);
iterator->child_hash_iter = (uint32_t) -1;
}
iterator->current_parent = NULL;
/* Start from first parent in queue */
future_iterator_next_parent(iterator);
}
/* Forward declarations */
static void
process_future_mapper(zend_future_t *parent_future, async_future_t *child_future_obj, future_iterator_t *iterator);
static zend_result future_mappers_handler(async_iterator_t *iterator, zval *current, zval *key);
static void async_future_callback_handler(zend_async_event_t *event,
zend_async_event_callback_t *callback,
void *result,
zend_object *exception);
static void async_future_callback_dispose(zend_async_event_callback_t *callback, zend_async_event_t *event);
static void async_future_create_mapper(INTERNAL_FUNCTION_PARAMETERS, async_future_mapper_type_t mapper_type);
async_future_t *async_future_obj_create(void);
static void future_mapper_coroutine_entry(void);
static void future_mapper_context_dispose(zend_coroutine_t *coroutine);
///////////////////////////////////////////////////////////
/// zend_future_t event handlers
///////////////////////////////////////////////////////////
static bool zend_future_event_start(zend_async_event_t *event)
{
/* Nothing to start for zend_future_t */
return true;
}
/**
* The method that is called to resolve the Future (complete or reject).
* This method must be triggered only once.
* This is the proper method for completing futures, NOT stop().
*
* @param event The future event
* @param iterator If not NULL, the future_iterator_t to add children to
*/
static bool zend_future_resolve(zend_async_event_t *event, void *iterator)
{
if (ZEND_ASYNC_EVENT_IS_CLOSED(event)) {
return true;
}
ZEND_ASYNC_EVENT_SET_CLOSED(event);
zend_future_t *future = (zend_future_t *) event;
/* Record where the Future was completed */
zend_apply_current_filename_and_line(&future->completed_filename, &future->completed_lineno);
// Notify regular callbacks (awaiters) with result/exception
ZEND_ASYNC_CALLBACKS_NOTIFY(event, &future->result, future->exception);
// Notify resolve_callbacks (map/catch/finally chains) with iterator
zend_async_callbacks_vector_notify(&future->resolve_callbacks, event, iterator);
if (ZEND_ASYNC_EVENT_IS_EXCEPTION_HANDLED(event)) {
ZEND_FUTURE_SET_EXCEPTION_CAUGHT(future);
}
return true;
}
/**
* Standard event stop method.
* For futures, this does nothing - all completion logic is in resolve().
*/
static bool zend_future_event_stop(zend_async_event_t *event)
{
return true;
}
static bool zend_future_add_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_push(event, callback);
}
static bool zend_future_del_callback(zend_async_event_t *event, zend_async_event_callback_t *callback)
{
return zend_async_callbacks_remove(event, callback);
}
static bool zend_future_replay(zend_async_event_t *event,
zend_async_event_callback_t *callback,
zval *result,
zend_object **exception)
{
zend_future_t *future = (zend_future_t *) event;
if (!ZEND_FUTURE_IS_COMPLETED(future)) {
return false;
}
if (callback != NULL) {
callback->callback(event, callback, &future->result, future->exception);
return EG(exception) == NULL;
}
if (result != NULL && future->exception == NULL) {
ZVAL_COPY(result, &future->result);
}
if (future->exception != NULL) {
if (exception != NULL) {
*exception = future->exception;
GC_ADDREF(future->exception);
} else {
GC_ADDREF(future->exception);
async_rethrow_exception(future->exception);
}
}
return EG(exception) == NULL;
}
static zend_string *zend_future_info(zend_async_event_t *event)
{
const zend_future_t *future = (zend_future_t *) event;
return zend_strpprintf(0, "FutureState(%s)", ZEND_FUTURE_IS_COMPLETED(future) ? "completed" : "pending");
}
static bool zend_future_dispose(zend_async_event_t *event)
{
zend_future_t *future = (zend_future_t *) event;
// Check for unused future (strict mode)
if (!ZEND_FUTURE_IS_IGNORED(future) && !ZEND_FUTURE_IS_USED(future)) {
if (future->filename != NULL) {
async_warning("Future was never used; "
"call await(), map(), catch(), finally() or ignore() to suppress this warning. "
"Created at %s:%d",
ZSTR_VAL(future->filename),
future->lineno);
} else {
async_warning("Future was never used; "
"call await(), map(), catch(), finally() or ignore() to suppress this warning");
}
}
// Check for unhandled exception
if (future->exception != NULL && !ZEND_FUTURE_IS_EXCEPTION_CAUGHT(future) && !ZEND_FUTURE_IS_IGNORED(future)) {
// Get exception message
zval rv;
zval *message =
zend_read_property_ex(future->exception->ce, future->exception, ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &rv);
const char *msg_str =
(message != NULL && Z_TYPE_P(message) == IS_STRING) ? Z_STRVAL_P(message) : "Unknown error";
if (future->filename != NULL && future->completed_filename != NULL) {
async_warning("Unhandled exception in Future: %s; "
"use catch() or ignore() to handle. "
"Created at %s:%d, completed at %s:%d",
msg_str,
ZSTR_VAL(future->filename),
future->lineno,
ZSTR_VAL(future->completed_filename),
future->completed_lineno);
} else if (future->filename != NULL) {
async_warning("Unhandled exception in Future: %s; "
"use catch() or ignore() to handle. "
"Created at %s:%d",
msg_str,
ZSTR_VAL(future->filename),
future->lineno);
} else {
async_warning("Unhandled exception in Future: %s; "
"use catch() or ignore() to handle",
msg_str);
}
}
zval_ptr_dtor(&future->result);
if (future->exception != NULL) {
OBJ_RELEASE(future->exception);
future->exception = NULL;
}
if (future->filename != NULL) {
zend_string_release(future->filename);
future->filename = NULL;
}
if (future->completed_filename != NULL) {
zend_string_release(future->completed_filename);
future->completed_filename = NULL;
}
zend_async_callbacks_free(event);
zend_async_callbacks_vector_free(&future->resolve_callbacks, event);
efree(future);
return true;
}
static zend_always_inline void init_zend_future(zend_async_event_t *event)
{
zend_future_t *future = (zend_future_t *) event;
event->start = zend_future_event_start;
event->stop = zend_future_event_stop;
event->add_callback = zend_future_add_callback;
event->del_callback = zend_future_del_callback;
event->replay = zend_future_replay;
event->info = zend_future_info;
event->dispose = zend_future_dispose;
event->ref_count = 1;
/* Set the resolve method for completing the future */
future->resolve = zend_future_resolve;
/* Initialize resolve_callbacks vector (for map/catch/finally chains) */
future->resolve_callbacks.data = NULL;
future->resolve_callbacks.length = 0;
future->resolve_callbacks.capacity = 0;
/* Initialize file/line tracking fields */
future->filename = NULL;
future->lineno = 0;
future->completed_filename = NULL;
future->completed_lineno = 0;
}
///////////////////////////////////////////////////////////
/// FutureState object lifecycle
///////////////////////////////////////////////////////////
static zend_object *async_future_state_object_create(zend_class_entry *ce)
{
async_future_state_t *state = zend_object_alloc(sizeof(async_future_state_t), ce);
// Internal future object
zend_future_t *future = ecalloc(1, sizeof(zend_future_t));
zend_async_event_t *event = &future->event;
ZVAL_UNDEF(&future->result);
/* Set event handlers */
init_zend_future(event);
/* Record where the Future was created */
zend_apply_current_filename_and_line(&future->filename, &future->lineno);
ZEND_ASYNC_EVENT_REF_SET(state, XtOffsetOf(async_future_state_t, std), event);
ZEND_ASYNC_EVENT_SET_ZVAL_RESULT(state->event);
zend_object_std_init(&state->std, ce);
object_properties_init(&state->std, ce);
return &state->std;
}
static void async_future_state_object_free(zend_object *object)
{
async_future_state_t *state = ASYNC_FUTURE_STATE_FROM_OBJ(object);
zend_future_t *future = (zend_future_t *) state->event;
state->event = NULL;
if (future != NULL) {
ZEND_ASYNC_EVENT_RELEASE(&future->event);
}
zend_object_std_dtor(&state->std);
}
///////////////////////////////////////////////////////////
/// Future object lifecycle
///////////////////////////////////////////////////////////
static zend_object *async_future_object_create(zend_class_entry *ce)
{
async_future_t *future = zend_object_alloc(sizeof(async_future_t), ce);
ZEND_ASYNC_EVENT_REF_SET(future, XtOffsetOf(async_future_t, std), NULL);
future->child_futures = NULL;
ZVAL_UNDEF(&future->mapper);
future->mapper_type = ASYNC_FUTURE_MAPPER_SUCCESS;
zend_object_std_init(&future->std, ce);
object_properties_init(&future->std, ce);
return &future->std;
}
static HashTable *async_future_get_gc(zend_object *object, zval **table, int *n)
{
async_future_t *future = ASYNC_FUTURE_FROM_OBJ(object);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
zend_future_t *zend_future = (zend_future_t *) future->event;
if (zend_future != NULL) {
if (zend_future->exception != NULL) {
zend_get_gc_buffer_add_obj(gc_buffer, zend_future->exception);
}
if (Z_TYPE(zend_future->result) != IS_UNDEF) {
zend_get_gc_buffer_add_zval(gc_buffer, &zend_future->result);
}
}
if (Z_TYPE(future->mapper) != IS_UNDEF) {
zend_get_gc_buffer_add_zval(gc_buffer, &future->mapper);
}
if (future->child_futures != NULL) {
zend_get_gc_buffer_add_ht(gc_buffer, future->child_futures);
}
zend_get_gc_buffer_use(gc_buffer, table, n);
return NULL;
}
static void async_future_object_free(zend_object *object)
{
async_future_t *future = ASYNC_FUTURE_FROM_OBJ(object);
if (future->child_futures != NULL) {
zend_array_destroy(future->child_futures);
future->child_futures = NULL;
}
zval_ptr_dtor(&future->mapper);
zend_future_t *zend_future = (zend_future_t *) future->event;
future->event = NULL;
if (zend_future != NULL) {
ZEND_ASYNC_EVENT_RELEASE(&zend_future->event);
}
zend_object_std_dtor(&future->std);
}
///////////////////////////////////////////////////////////
/// FutureState methods
///////////////////////////////////////////////////////////
#define THIS_FUTURE_STATE ((async_future_state_t *) ASYNC_FUTURE_STATE_FROM_OBJ(Z_OBJ_P(ZEND_THIS)))
FUTURE_STATE_METHOD(__construct)
{
ZEND_PARSE_PARAMETERS_NONE();
}
FUTURE_STATE_METHOD(complete)
{
zval *result;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(result)
ZEND_PARSE_PARAMETERS_END();
const async_future_state_t *state = THIS_FUTURE_STATE;
if (state->event == NULL) {
async_throw_error("FutureState is already destroyed");
RETURN_THROWS();
}
zend_future_t *future = (zend_future_t *) state->event;
if (ZEND_FUTURE_IS_COMPLETED(future)) {
if (future->completed_filename != NULL) {
async_throw_error("FutureState is already completed at %s:%d",
ZSTR_VAL(future->completed_filename),
future->completed_lineno);
} else {
async_throw_error("FutureState is already completed at Unknown:0");
}
RETURN_THROWS();
}
ZEND_FUTURE_COMPLETE(future, result);
}
FUTURE_STATE_METHOD(error)
{
zval *throwable;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS(throwable, zend_ce_throwable)
ZEND_PARSE_PARAMETERS_END();
const async_future_state_t *state = THIS_FUTURE_STATE;
if (state->event == NULL) {
async_throw_error("FutureState is already destroyed");
RETURN_THROWS();
}
zend_future_t *future = (zend_future_t *) state->event;
if (ZEND_FUTURE_IS_COMPLETED(future)) {
if (future->completed_filename != NULL) {
async_throw_error("FutureState is already completed at %s:%d",
ZSTR_VAL(future->completed_filename),
future->completed_lineno);
} else {
async_throw_error("FutureState is already completed at Unknown:0");
}
RETURN_THROWS();
}
ZEND_FUTURE_REJECT(future, Z_OBJ_P(throwable));
}
FUTURE_STATE_METHOD(isCompleted)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
const zend_future_t *future = (zend_future_t *) state->event;
if (UNEXPECTED(future == NULL)) {
RETURN_TRUE;
}
RETURN_BOOL(ZEND_FUTURE_IS_COMPLETED(future));
}
FUTURE_STATE_METHOD(ignore)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
if (UNEXPECTED(future == NULL)) {
async_throw_error("FutureState is already destroyed");
RETURN_THROWS();
}
ZEND_FUTURE_SET_IGNORED(future);
}
FUTURE_STATE_METHOD(getAwaitingInfo)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
if (UNEXPECTED(future == NULL)) {
RETURN_EMPTY_ARRAY();
}
zend_string *state_info = future->event.info(&future->event);
zval z_state_info;
ZVAL_STR(&z_state_info, state_info);
// new array zend array
zend_array *info = zend_new_array(0);
zend_hash_index_add_new(info, 0, &z_state_info);
RETURN_ARR(info);
}
FUTURE_STATE_METHOD(getCreatedFileAndLine)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
array_init(return_value);
if (future != NULL && future->filename != NULL) {
add_next_index_str(return_value, zend_string_copy(future->filename));
} else {
add_next_index_null(return_value);
}
add_next_index_long(return_value, future != NULL ? future->lineno : 0);
}
FUTURE_STATE_METHOD(getCreatedLocation)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
if (future != NULL && future->filename != NULL) {
RETURN_STR(zend_strpprintf(0, "%s:%d", ZSTR_VAL(future->filename), future->lineno));
} else {
RETURN_STRING("unknown");
}
}
FUTURE_STATE_METHOD(getCompletedFileAndLine)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
array_init(return_value);
if (future != NULL && future->completed_filename != NULL) {
add_next_index_str(return_value, zend_string_copy(future->completed_filename));
} else {
add_next_index_null(return_value);
}
add_next_index_long(return_value, future != NULL ? future->completed_lineno : 0);
}
FUTURE_STATE_METHOD(getCompletedLocation)
{
ZEND_PARSE_PARAMETERS_NONE();
const async_future_state_t *state = THIS_FUTURE_STATE;
zend_future_t *future = (zend_future_t *) state->event;
if (future != NULL && future->completed_filename != NULL) {
RETURN_STR(zend_strpprintf(0, "%s:%d", ZSTR_VAL(future->completed_filename), future->completed_lineno));
} else {
RETURN_STRING("unknown");
}
}
///////////////////////////////////////////////////////////
/// Future methods
///////////////////////////////////////////////////////////
#define THIS_FUTURE ((async_future_t *) ASYNC_FUTURE_FROM_OBJ(Z_OBJ_P(ZEND_THIS)))
FUTURE_METHOD(__construct)
{
zval *state_obj;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJECT_OF_CLASS(state_obj, async_ce_future_state)
ZEND_PARSE_PARAMETERS_END();
async_future_t *future = THIS_FUTURE;
const async_future_state_t *state = ASYNC_FUTURE_STATE_FROM_OBJ(Z_OBJ_P(state_obj));
if (UNEXPECTED(state->event == NULL)) {
async_throw_error("FutureState is already destroyed");
RETURN_THROWS();
}
future->event = state->event;
ZEND_ASYNC_EVENT_ADD_REF(state->event);
ZEND_ASYNC_EVENT_REF_SET(future, XtOffsetOf(async_future_t, std), state->event);
}
FUTURE_METHOD(completed)
{
zval *value = NULL;
ZEND_PARSE_PARAMETERS_START(0, 1)