-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature.patch
More file actions
1151 lines (1133 loc) · 36.9 KB
/
Copy pathfeature.patch
File metadata and controls
1151 lines (1133 loc) · 36.9 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
diff --git a/Zend/tests/context.phpt b/Zend/tests/context.phpt
new file mode 100644
index 00000000..3a06339c
--- /dev/null
+++ b/Zend/tests/context.phpt
@@ -0,0 +1,77 @@
+--TEST--
+context parameters / provide (implicits): resolution, propagation, scope
+--FILE--
+<?php
+
+interface Clock { public function time(): int; }
+class SystemClock implements Clock {
+ public function __construct(private int $t) {}
+ public function time(): int { return $this->t; }
+}
+
+function now(context Clock $c): int { return $c->time(); }
+
+// 1. block form: value active for everything called inside, auto-supplied
+provide new SystemClock(42) { echo now(), "\n"; }
+
+// 2. deep propagation: a context param several calls down sees it
+function a(): string { return b(); }
+function b(): string { return c(); }
+function c(context Clock $c): string { return "deep=" . $c->time(); }
+provide new SystemClock(7) { echo a(), "\n"; }
+
+// 3. statement form: active for the rest of the enclosing function
+function handler(): void {
+ provide new SystemClock(99);
+ echo "stmt=" . now() . "\n";
+}
+handler();
+
+// 4. nearest-wins: a nested provide shadows the outer, restored on exit
+provide new SystemClock(1) {
+ echo now(), "\n"; // 1
+ provide new SystemClock(2) { echo now(), "\n"; } // 2
+ echo now(), "\n"; // 1 again
+}
+
+// 5. pop on exception: the value does not leak past the block
+try {
+ provide new SystemClock(5) { throw new Exception("boom"); }
+} catch (Exception $e) {
+ echo "caught\n";
+}
+try { now(); } catch (\Error $e) { echo $e->getMessage(), "\n"; }
+
+// 6. default is used when nothing is provided
+function withDefault(context Clock $c = new SystemClock(-1)): int { return $c->time(); }
+echo "default=" . withDefault() . "\n";
+provide new SystemClock(8) { echo "provided=" . withDefault() . "\n"; }
+
+// 7. interface/inheritance match
+class Toc extends SystemClock {}
+provide new Toc(123) { echo now(), "\n"; }
+
+// 8. context/provide remain usable as method names; `class Context` still parses
+class Holder {
+ public function context($x) { return "m$x"; }
+ public function provide() { return "p"; }
+}
+echo (new Holder)->context(1), (new Holder)->provide(), "\n";
+class Context { public int $x = 314; }
+echo (new Context)->x, "\n";
+
+?>
+--EXPECT--
+42
+deep=7
+stmt=99
+1
+2
+1
+caught
+No context value of type Clock is available
+default=-1
+provided=8
+123
+m1p
+314
diff --git a/Zend/tests/context_comprehensive.phpt b/Zend/tests/context_comprehensive.phpt
new file mode 100644
index 00000000..8f869084
--- /dev/null
+++ b/Zend/tests/context_comprehensive.phpt
@@ -0,0 +1,47 @@
+--TEST--
+context/provide — resolution, nesting, statement form, default, missing, pop-on-throw
+--FILE--
+<?php
+interface Clock { public function now(): int; }
+class Fixed implements Clock { public function __construct(private int $t) {} public function now(): int { return $this->t; } }
+class RequestId { public function __construct(public string $id) {} }
+
+function currentTime(context Clock $c): int { return $c->now(); }
+function deep(context Clock $c): string { return "t=" . $c->now(); }
+
+// block form + resolution by interface
+provide new Fixed(1000) {
+ echo currentTime(), "\n"; // 1000, auto-supplied
+ echo deep(), "\n"; // propagates down the call chain
+}
+// nearest wins + shadowing restore
+provide new Fixed(1) {
+ echo currentTime(), "\n"; // 1
+ provide new Fixed(2) { echo currentTime(), "\n"; } // 2
+ echo currentTime(), "\n"; // 1 again
+}
+// statement form: active for rest of function
+function handler(): void {
+ provide new RequestId("abc");
+ log_it();
+}
+function log_it(context RequestId $r): void { echo "req={$r->id}\n"; }
+handler();
+// default value when nothing provided
+function withDefault(context RequestId $r = new RequestId("default")): string { return $r->id; }
+echo withDefault(), "\n";
+// missing value is an error
+try { currentTime(); } catch (\Throwable $e) { echo "error: ", get_class($e), "\n"; }
+// provide pops even on throw
+try { provide new Fixed(9) { throw new Exception("boom"); } } catch (Throwable $e) {}
+try { currentTime(); } catch (\Throwable $e) { echo "popped ok\n"; }
+--EXPECT--
+1000
+t=1000
+1
+2
+1
+req=abc
+default
+error: Error
+popped ok
diff --git a/Zend/tests/defer.phpt b/Zend/tests/defer.phpt
new file mode 100644
index 00000000..caeb295a
--- /dev/null
+++ b/Zend/tests/defer.phpt
@@ -0,0 +1,102 @@
+--TEST--
+defer — Go-style scope-exit cleanup (LIFO, on return and on exception)
+--FILE--
+<?php
+// LIFO on normal return: deferred calls run after the body, last-scheduled first.
+function lifo() {
+ defer printf("a\n");
+ defer printf("b\n");
+ echo "body\n";
+}
+lifo();
+
+// Runs during exception unwind too.
+function boom() {
+ defer printf("cleanup\n");
+ throw new RuntimeException("boom");
+}
+try {
+ boom();
+} catch (Throwable $e) {
+ echo "caught: ", $e->getMessage(), "\n";
+}
+
+// A defer inside a loop schedules one call per iteration; all run LIFO at exit.
+function loop() {
+ for ($i = 0; $i < 3; $i++) {
+ defer printf("[%d]", $i);
+ }
+ echo "loop-body\n";
+}
+loop();
+echo "\n";
+
+// Go-faithful capture: the arguments are evaluated at the defer point, not at exit.
+function capture() {
+ $x = 1;
+ defer printf("x=%d\n", $x);
+ $x = 2;
+}
+capture();
+
+// The receiver of a method call is likewise captured at the defer point.
+class Logger {
+ public function __construct(public string $tag) {}
+ public function flush() { printf("flush %s\n", $this->tag); }
+}
+function receiver() {
+ $log = new Logger("first");
+ defer $log->flush();
+ $log = new Logger("second");
+}
+receiver();
+
+// Static calls work too.
+class Greeter {
+ public static function bye($name) { printf("bye %s\n", $name); }
+}
+function static_call() {
+ defer Greeter::bye("x");
+ echo "work\n";
+}
+static_call();
+
+// Spread arguments are captured (flattened) at the defer point.
+function spread() {
+ $a = [1, 2, 3];
+ defer printf("%d-%d-%d\n", ...$a);
+ $a = [9];
+ echo "spread-body\n";
+}
+spread();
+
+// Deferred calls do not alter the return value.
+function ret() {
+ defer printf("after\n");
+ return 42;
+}
+var_dump(ret());
+
+// `defer` is only semi-reserved: still usable as a method name.
+class HasMethod {
+ public function defer() { return "method-ok\n"; }
+}
+echo (new HasMethod)->defer();
+?>
+--EXPECT--
+body
+b
+a
+cleanup
+caught: boom
+loop-body
+[2][1][0]
+x=1
+flush first
+work
+bye x
+spread-body
+1-2-3
+after
+int(42)
+method-ok
diff --git a/Zend/tests/magic_methods/bug36214.phpt b/Zend/tests/magic_methods/bug36214.phpt
index b2e3e5dd..72fdc83b 100644
--- a/Zend/tests/magic_methods/bug36214.phpt
+++ b/Zend/tests/magic_methods/bug36214.phpt
@@ -2,7 +2,7 @@
Bug #36214 (__get method works properly only when conditional operator is used)
--FILE--
<?php
-class context {
+class ctxholder {
public $stack = array();
public function __set($name,$var) {
@@ -14,7 +14,7 @@ class context {
}
}
-$ctx = new context;
+$ctx = new ctxholder;
$ctx->comment_preview = array();
$ctx->comment_preview[0] = 1;
$ctx->comment_preview[1] = 2;
diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c
index 9cb3c7aa..bad69ff7 100644
--- a/Zend/zend_ast.c
+++ b/Zend/zend_ast.c
@@ -2394,6 +2394,21 @@ simple_list:
APPEND_STR("__HALT_COMPILER()");
case ZEND_AST_ECHO:
APPEND_NODE_1("echo");
+ case ZEND_AST_DEFER:
+ APPEND_NODE_1("defer");
+ case ZEND_AST_PROVIDE_STMT:
+ smart_str_appends(str, "provide ");
+ zend_ast_export_ex(str, ast->child[0], 0, indent);
+ smart_str_appendc(str, ';');
+ break;
+ case ZEND_AST_PROVIDE:
+ smart_str_appends(str, "provide ");
+ zend_ast_export_ex(str, ast->child[0], 0, indent);
+ smart_str_appends(str, " {\n");
+ zend_ast_export_stmt(str, ast->child[1], indent + 1);
+ zend_ast_export_indent(str, indent);
+ smart_str_appendc(str, '}');
+ break;
case ZEND_AST_THROW:
APPEND_NODE_1("throw");
case ZEND_AST_GOTO:
diff --git a/Zend/zend_ast.h b/Zend/zend_ast.h
index fb48b187..05578ac6 100644
--- a/Zend/zend_ast.h
+++ b/Zend/zend_ast.h
@@ -99,6 +99,8 @@ enum _zend_ast_kind {
ZEND_AST_YIELD_FROM,
ZEND_AST_CLASS_NAME,
+ ZEND_AST_DEFER,
+ ZEND_AST_PROVIDE_STMT,
ZEND_AST_GLOBAL,
ZEND_AST_UNSET,
ZEND_AST_RETURN,
@@ -155,6 +157,8 @@ enum _zend_ast_kind {
ZEND_AST_PARENT_PROPERTY_HOOK_CALL,
ZEND_AST_PIPE,
+ ZEND_AST_PROVIDE,
+
/* 3 child nodes */
ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
ZEND_AST_NULLSAFE_METHOD_CALL,
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 8be1ee14..7d833815 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -6729,6 +6729,162 @@ static void zend_compile_match(znode *result, zend_ast *ast)
efree(jmp_end_opnums);
}
+/* --- `defer CALL;` (Go-style deferred call) --- */
+
+/* A ZEND_AST_VAR for the hidden per-frame DeferScope CV (leading digit -> not
+ * writable from userland, so it can never collide with a user variable). */
+static zend_ast *defer_hidden_var(void) /* {{{ */
+{
+ return zend_ast_create(ZEND_AST_VAR,
+ zend_ast_create_zval_from_str(zend_string_init("0defer", sizeof("0defer") - 1, 0)));
+}
+/* }}} */
+
+/* Desugar `defer callee(args);` to:
+ * $<hidden> ??= new DeferScope();
+ * $<hidden>->push(callee(...), [args]);
+ * The first-class-callable `callee(...)` captures the callee/receiver now and the
+ * `[args]` array captures the arguments now (Go-faithful); the DeferScope runs the
+ * collected calls LIFO when the frame is torn down (return or exception). */
+static void zend_compile_defer(zend_ast *ast) /* {{{ */
+{
+ zend_ast *call = ast->child[0];
+ uint32_t args_idx;
+
+ switch (call->kind) {
+ case ZEND_AST_CALL:
+ args_idx = 1;
+ break;
+ case ZEND_AST_METHOD_CALL:
+ case ZEND_AST_NULLSAFE_METHOD_CALL:
+ case ZEND_AST_STATIC_CALL:
+ args_idx = 2;
+ break;
+ default:
+ zend_error_noreturn(E_COMPILE_ERROR, "defer requires a function or method call");
+ return;
+ }
+
+ zend_ast_list *arglist = zend_ast_get_list(call->child[args_idx]);
+
+ /* [args...] — capture each argument now (positional + spread; named args
+ * are not supported in v1). A spread `...$x` is carried over verbatim as an
+ * ZEND_AST_UNPACK element of the array literal, so its elements are flattened
+ * into the captured array at the defer point (Go-faithful). */
+ zend_ast *args_array = zend_ast_create_list(0, ZEND_AST_ARRAY);
+ for (uint32_t i = 0; i < arglist->children; i++) {
+ zend_ast *arg = arglist->child[i];
+ if (arg->kind == ZEND_AST_NAMED_ARG) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "defer does not support named arguments");
+ }
+ arglist->child[i] = NULL; /* detach: reused in args_array */
+ if (arg->kind == ZEND_AST_UNPACK) {
+ args_array = zend_ast_list_add(args_array, arg);
+ } else {
+ args_array = zend_ast_list_add(args_array,
+ zend_ast_create_ex(ZEND_AST_ARRAY_ELEM, 0, arg, NULL));
+ }
+ }
+
+ /* Turn the call into a first-class callable `callee(...)` (captures callee now). */
+ call->child[args_idx] = zend_ast_create(ZEND_AST_CALLABLE_CONVERT);
+ ast->child[0] = NULL; /* detach the call; now owned by push_call below */
+ zend_ast *callee_fcc = call;
+
+ /* $<hidden> ??= new DeferScope(); */
+ zend_ast *cls = zend_ast_create_zval_from_str(
+ zend_string_init("DeferScope", sizeof("DeferScope") - 1, 0));
+ cls->attr = ZEND_NAME_FQ;
+ zend_ast *new_scope = zend_ast_create(ZEND_AST_NEW, cls,
+ zend_ast_create_list(0, ZEND_AST_ARG_LIST));
+ zend_ast *coalesce = zend_ast_create(ZEND_AST_ASSIGN_COALESCE,
+ defer_hidden_var(), new_scope);
+ znode r1;
+ zend_compile_expr(&r1, coalesce);
+ zend_do_free(&r1);
+ zend_ast_destroy(coalesce);
+
+ /* $<hidden>->push(callee(...), [args]); */
+ zend_ast *push_call = zend_ast_create(ZEND_AST_METHOD_CALL,
+ defer_hidden_var(),
+ zend_ast_create_zval_from_str(zend_string_init("push", sizeof("push") - 1, 0)),
+ zend_ast_create_list(2, ZEND_AST_ARG_LIST, callee_fcc, args_array));
+ znode r2;
+ zend_compile_expr(&r2, push_call);
+ zend_do_free(&r2);
+ zend_ast_destroy(push_call);
+}
+/* }}} */
+
+static void zend_compile_try(zend_ast *ast);
+
+/* Build a call to an internal context helper: `\name(arg)` (arg may be NULL). */
+static zend_ast *ctx_helper_call(const char *name, zend_ast *arg) /* {{{ */
+{
+ zend_ast *args = arg
+ ? zend_ast_create_list(1, ZEND_AST_ARG_LIST, arg)
+ : zend_ast_create_list(0, ZEND_AST_ARG_LIST);
+ zend_ast *fname = zend_ast_create_zval_from_str(
+ zend_string_init(name, strlen(name), 0));
+ fname->attr = ZEND_NAME_FQ;
+ return zend_ast_create(ZEND_AST_CALL, fname, args);
+}
+/* }}} */
+
+/* Desugar the block form `provide V { B }` to:
+ * __ctx_push(V);
+ * try { B } finally { __ctx_pop(); }
+ * so the value is active for everything called inside B and is always popped on
+ * exit (return, break, or exception). */
+static void zend_compile_provide(zend_ast *ast) /* {{{ */
+{
+ zend_ast *value = ast->child[0];
+ ast->child[0] = NULL; /* detach: moved into the push call */
+ zend_ast *body = ast->child[1];
+ ast->child[1] = NULL; /* detach: moved into the try */
+
+ /* __ctx_push(V); */
+ zend_ast *push = ctx_helper_call("__ctx_push", value);
+ znode r;
+ zend_compile_expr(&r, push);
+ zend_do_free(&r);
+ zend_ast_destroy(push);
+
+ /* try { B } finally { __ctx_pop(); } */
+ zend_ast *finally = zend_ast_create_list(1, ZEND_AST_STMT_LIST,
+ ctx_helper_call("__ctx_pop", NULL));
+ zend_ast *catches = zend_ast_create_list(0, ZEND_AST_CATCH_LIST);
+ zend_ast *try_ast = zend_ast_create(ZEND_AST_TRY, body, catches, finally);
+ zend_compile_try(try_ast);
+ zend_ast_destroy(try_ast);
+}
+/* }}} */
+
+/* Desugar the statement form `provide V;` to:
+ * __ctx_push(V);
+ * defer __ctx_pop();
+ * making the value active for the rest of the enclosing function; the `defer`
+ * pops it when the frame is torn down (return or exception). */
+static void zend_compile_provide_stmt(zend_ast *ast) /* {{{ */
+{
+ zend_ast *value = ast->child[0];
+ ast->child[0] = NULL; /* detach: moved into the push call */
+
+ /* __ctx_push(V); */
+ zend_ast *push = ctx_helper_call("__ctx_push", value);
+ znode r;
+ zend_compile_expr(&r, push);
+ zend_do_free(&r);
+ zend_ast_destroy(push);
+
+ /* defer __ctx_pop(); */
+ zend_ast *defer_node = zend_ast_create(ZEND_AST_DEFER,
+ ctx_helper_call("__ctx_pop", NULL));
+ zend_compile_defer(defer_node);
+}
+/* }}} */
+
static void zend_compile_try(zend_ast *ast) /* {{{ */
{
zend_ast *try_ast = ast->child[0];
@@ -8432,12 +8588,108 @@ static zend_string *zend_begin_func_decl(znode *result, zend_op_array *op_array,
}
/* }}} */
+/* Rewrite `context` parameters into a body prologue that pulls the value from the
+ * runtime context stack, then drop them from the signature. A param `context T $c`
+ * becomes, prepended to the body:
+ *
+ * $c = \__ctx_resolve("T"); // no default
+ * $c = \__ctx_has("T") ? \__ctx_resolve("T") : (DEFAULT); // with default
+ *
+ * so arg_info / RECV numbering / reflection see a normal, context-free signature.
+ * Applied to named functions, methods and closures (block body required). */
+static zend_ast *ctx_name_call(const char *name, zend_ast *arglist) /* {{{ */
+{
+ zend_ast *fname = zend_ast_create_zval_from_str(zend_string_init(name, strlen(name), 0));
+ fname->attr = ZEND_NAME_FQ;
+ return zend_ast_create(ZEND_AST_CALL, fname, arglist);
+}
+/* }}} */
+
+static void zend_rewrite_context_params(zend_ast_decl *decl) /* {{{ */
+{
+ if (decl->kind != ZEND_AST_FUNC_DECL
+ && decl->kind != ZEND_AST_METHOD
+ && decl->kind != ZEND_AST_CLOSURE) {
+ return;
+ }
+ zend_ast *params_ast = decl->child[0];
+ if (!params_ast) {
+ return;
+ }
+ zend_ast_list *params = zend_ast_get_list(params_ast);
+
+ bool has_ctx = false;
+ for (uint32_t i = 0; i < params->children; i++) {
+ if (params->child[i] && (params->child[i]->attr & ZEND_PARAM_CONTEXT)) {
+ has_ctx = true;
+ break;
+ }
+ }
+ if (!has_ctx) {
+ return;
+ }
+ if (!decl->child[2]) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "A context parameter requires a function body");
+ }
+
+ zend_ast *new_body = zend_ast_create_list(0, ZEND_AST_STMT_LIST);
+ uint32_t w = 0;
+ for (uint32_t i = 0; i < params->children; i++) {
+ zend_ast *param = params->child[i];
+ if (!param || !(param->attr & ZEND_PARAM_CONTEXT)) {
+ params->child[w++] = param;
+ continue;
+ }
+
+ zend_ast *type_ast = param->child[0];
+ if (!type_ast || type_ast->kind != ZEND_AST_ZVAL
+ || Z_TYPE_P(zend_ast_get_zval(type_ast)) != IS_STRING
+ || (type_ast->attr & ZEND_TYPE_NULLABLE)) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "A context parameter must declare a single, non-nullable class type");
+ }
+ zend_string *type_name = zend_ast_get_str(type_ast);
+
+ zend_ast *var = zend_ast_create(ZEND_AST_VAR,
+ zend_ast_create_zval_from_str(zend_string_copy(zend_ast_get_str(param->child[1]))));
+ zend_ast *resolve = ctx_name_call("__ctx_resolve",
+ zend_ast_create_list(1, ZEND_AST_ARG_LIST,
+ zend_ast_create_zval_from_str(zend_string_copy(type_name))));
+
+ zend_ast *value;
+ if (param->child[2]) {
+ zend_ast *has = ctx_name_call("__ctx_has",
+ zend_ast_create_list(1, ZEND_AST_ARG_LIST,
+ zend_ast_create_zval_from_str(zend_string_copy(type_name))));
+ zend_ast *def = param->child[2];
+ param->child[2] = NULL; /* detach: reused below */
+ value = zend_ast_create(ZEND_AST_CONDITIONAL, has, resolve, def);
+ } else {
+ value = resolve;
+ }
+
+ new_body = zend_ast_list_add(new_body,
+ zend_ast_create(ZEND_AST_ASSIGN, var, value));
+ zend_ast_destroy(param);
+ }
+ params->children = w;
+
+ zend_ast_list *old_body = zend_ast_get_list(decl->child[2]);
+ for (uint32_t i = 0; i < old_body->children; i++) {
+ new_body = zend_ast_list_add(new_body, old_body->child[i]);
+ }
+ decl->child[2] = new_body;
+}
+/* }}} */
+
static zend_op_array *zend_compile_func_decl_ex(
znode *result, zend_ast *ast, enum func_decl_level level,
zend_string *property_info_name,
zend_property_hook_kind hook_kind
) {
zend_ast_decl *decl = (zend_ast_decl *) ast;
+ zend_rewrite_context_params(decl);
zend_ast *params_ast = decl->child[0];
zend_ast *uses_ast = decl->child[1];
zend_ast *stmt_ast = decl->child[2];
@@ -11708,6 +11960,15 @@ static void zend_compile_stmt(zend_ast *ast) /* {{{ */
case ZEND_AST_RETURN:
zend_compile_return(ast);
break;
+ case ZEND_AST_DEFER:
+ zend_compile_defer(ast);
+ break;
+ case ZEND_AST_PROVIDE:
+ zend_compile_provide(ast);
+ break;
+ case ZEND_AST_PROVIDE_STMT:
+ zend_compile_provide_stmt(ast);
+ break;
case ZEND_AST_ECHO:
zend_compile_echo(ast);
break;
diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h
index c07fa9bf..edbf9f61 100644
--- a/Zend/zend_compile.h
+++ b/Zend/zend_compile.h
@@ -1041,6 +1041,10 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
/* These should not clash with ZEND_ACC_PPP_MASK and ZEND_ACC_PPP_SET_MASK */
#define ZEND_PARAM_REF (1<<3)
#define ZEND_PARAM_VARIADIC (1<<4)
+/* `context` parameter marker. Stripped by the compile-time pre-pass that turns
+ * such params into a body prologue, so it never reaches real arg_info. Bit 6
+ * aliases ZEND_ACC_ABSTRACT, which is never a valid parameter modifier. */
+#define ZEND_PARAM_CONTEXT (1<<6)
#define ZEND_NAME_FQ 0
#define ZEND_NAME_NOT_FQ 1
diff --git a/Zend/zend_context.c b/Zend/zend_context.c
new file mode 100644
index 00000000..349a1ba9
--- /dev/null
+++ b/Zend/zend_context.c
@@ -0,0 +1,92 @@
+/*
+ +----------------------------------------------------------------------+
+ | Runtime support for `context` parameters / `provide` (implicits). |
+ | |
+ | `provide V { B }` and `provide V;` push an object onto a per-request |
+ | context stack (EG(context_stack)); a `context T $c` parameter is |
+ | compiled into a body prologue that resolves the nearest provided |
+ | value whose class/interface matches T. Push/pop/has/resolve are the |
+ | four internal helpers the compiler emits. |
+ +----------------------------------------------------------------------+
+*/
+
+#include "zend.h"
+#include "zend_API.h"
+#include "zend_compile.h"
+#include "zend_exceptions.h"
+#include "zend_execute.h"
+#include "zend_context.h"
+#include "zend_context_arginfo.h"
+
+ZEND_FUNCTION(__ctx_push)
+{
+ zval *value;
+ ZEND_PARSE_PARAMETERS_START(1, 1)
+ Z_PARAM_OBJECT(value)
+ ZEND_PARSE_PARAMETERS_END();
+
+ zval copy;
+ ZVAL_COPY(©, value);
+ zend_stack_push(&EG(context_stack), ©);
+}
+
+ZEND_FUNCTION(__ctx_pop)
+{
+ ZEND_PARSE_PARAMETERS_NONE();
+
+ if (zend_stack_is_empty(&EG(context_stack))) {
+ return;
+ }
+ zval *top = zend_stack_top(&EG(context_stack));
+ zval_ptr_dtor(top);
+ zend_stack_del_top(&EG(context_stack));
+}
+
+/* Walk the context stack newest-first, returning the first provided object whose
+ * class or one of its interfaces matches `type`. NULL if none / unknown type. */
+static zval *ctx_find(zend_string *type)
+{
+ zend_class_entry *want = zend_lookup_class(type);
+ if (!want) {
+ return NULL;
+ }
+ int count = zend_stack_count(&EG(context_stack));
+ zval *base = (zval *) zend_stack_base(&EG(context_stack));
+ for (int i = count - 1; i >= 0; i--) {
+ zval *v = &base[i];
+ if (Z_TYPE_P(v) == IS_OBJECT && instanceof_function(Z_OBJCE_P(v), want)) {
+ return v;
+ }
+ }
+ return NULL;
+}
+
+ZEND_FUNCTION(__ctx_has)
+{
+ zend_string *type;
+ ZEND_PARSE_PARAMETERS_START(1, 1)
+ Z_PARAM_STR(type)
+ ZEND_PARSE_PARAMETERS_END();
+
+ RETURN_BOOL(ctx_find(type) != NULL);
+}
+
+ZEND_FUNCTION(__ctx_resolve)
+{
+ zend_string *type;
+ ZEND_PARSE_PARAMETERS_START(1, 1)
+ Z_PARAM_STR(type)
+ ZEND_PARSE_PARAMETERS_END();
+
+ zval *v = ctx_find(type);
+ if (!v) {
+ zend_throw_error(NULL, "No context value of type %s is available", ZSTR_VAL(type));
+ RETURN_THROWS();
+ }
+ RETURN_COPY(v);
+}
+
+void zend_register_context(void)
+{
+ zend_register_functions(NULL, ext_functions, NULL, MODULE_PERSISTENT);
+}
diff --git a/Zend/zend_context.h b/Zend/zend_context.h
new file mode 100644
index 00000000..744f0b90
--- /dev/null
+++ b/Zend/zend_context.h
@@ -0,0 +1,18 @@
+/*
+ +----------------------------------------------------------------------+
+ | Runtime support for `context` parameters / `provide` (implicits). |
+ +----------------------------------------------------------------------+
+*/
+
+#ifndef ZEND_CONTEXT_H
+#define ZEND_CONTEXT_H
+
+#include "zend.h"
+
+BEGIN_EXTERN_C()
+
+void zend_register_context(void);
+
+END_EXTERN_C()
+
+#endif /* ZEND_CONTEXT_H */
diff --git a/Zend/zend_context.stub.php b/Zend/zend_context.stub.php
new file mode 100644
index 00000000..0e12684f
--- /dev/null
+++ b/Zend/zend_context.stub.php
@@ -0,0 +1,19 @@
+<?php
+
+/** @generate-class-entries */
+
+/**
+ * Internal helpers backing `context` parameters and `provide`. The compiler
+ * desugars `provide V { B }` to `__ctx_push(V); try { B } finally { __ctx_pop(); }`
+ * and `provide V;` to `__ctx_push(V); defer __ctx_pop();`, and rewrites a
+ * `context T $c` parameter into a body prologue that calls `__ctx_resolve("T")`
+ * (or `__ctx_has`/`__ctx_resolve` when a default is given). Not intended to be
+ * called directly.
+ */
+function __ctx_push(object $value): void {}
+
+function __ctx_pop(): void {}
+
+function __ctx_has(string $type): bool {}
+
+function __ctx_resolve(string $type): mixed {}
diff --git a/Zend/zend_context_arginfo.h b/Zend/zend_context_arginfo.h
new file mode 100644
index 00000000..22dbfa1c
--- /dev/null
+++ b/Zend/zend_context_arginfo.h
@@ -0,0 +1,30 @@
+/* This is a generated file, edit the .stub.php file instead.
+ * Stub hash: 43075f6121f338268c9891b95cba787642ee88f2 */
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo___ctx_push, 0, 1, IS_VOID, 0)
+ ZEND_ARG_TYPE_INFO(0, value, IS_OBJECT, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo___ctx_pop, 0, 0, IS_VOID, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo___ctx_has, 0, 1, _IS_BOOL, 0)
+ ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo___ctx_resolve, 0, 1, IS_MIXED, 0)
+ ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_FUNCTION(__ctx_push);
+ZEND_FUNCTION(__ctx_pop);
+ZEND_FUNCTION(__ctx_has);
+ZEND_FUNCTION(__ctx_resolve);
+
+static const zend_function_entry ext_functions[] = {
+ ZEND_FE(__ctx_push, arginfo___ctx_push)
+ ZEND_FE(__ctx_pop, arginfo___ctx_pop)
+ ZEND_FE(__ctx_has, arginfo___ctx_has)
+ ZEND_FE(__ctx_resolve, arginfo___ctx_resolve)
+ ZEND_FE_END
+};
diff --git a/Zend/zend_default_classes.c b/Zend/zend_default_classes.c
index 7ab9b832..16684bb3 100644
--- a/Zend/zend_default_classes.c
+++ b/Zend/zend_default_classes.c
@@ -28,6 +28,8 @@
#include "zend_weakrefs.h"
#include "zend_enum.h"
#include "zend_fibers.h"
+#include "zend_defer.h"
+#include "zend_context.h"
ZEND_API void zend_register_default_classes(void)
{
@@ -40,4 +42,6 @@ ZEND_API void zend_register_default_classes(void)
zend_register_attribute_ce();
zend_register_enum_ce();
zend_register_fiber_ce();
+ zend_register_defer();
+ zend_register_context();
}
diff --git a/Zend/zend_defer.c b/Zend/zend_defer.c
new file mode 100644
index 00000000..5e103bd8
--- /dev/null
+++ b/Zend/zend_defer.c
@@ -0,0 +1,81 @@
+#include "zend.h"
+#include "zend_API.h"
+#include "zend_exceptions.h"
+#include "zend_defer.h"
+#include "zend_defer_arginfo.h"
+
+ZEND_API zend_class_entry *zend_ce_defer_scope;
+
+ZEND_METHOD(DeferScope, push)
+{
+ zval *fn, *args;
+ ZEND_PARSE_PARAMETERS_START(2, 2)
+ Z_PARAM_ZVAL(fn)
+ Z_PARAM_ZVAL(args)
+ ZEND_PARSE_PARAMETERS_END();
+
+ zval pair;
+ array_init_size(&pair, 2);
+ Z_TRY_ADDREF_P(fn);
+ add_next_index_zval(&pair, fn);
+ Z_TRY_ADDREF_P(args);
+ add_next_index_zval(&pair, args);
+
+ zval *entries = OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0);
+ ZVAL_DEREF(entries);
+ /* The declared default `[]` is the engine's *immutable* empty array, which
+ * SEPARATE_ARRAY won't clone (its refcount is 1). Install a fresh, owned
+ * array the first time we push; thereafter separate as usual. */
+ HashTable *ht;
+ if (!Z_REFCOUNTED_P(entries)) {
+ ht = zend_new_array(8);
+ ZVAL_ARR(entries, ht);
+ } else {
+ SEPARATE_ARRAY(entries);
+ ht = Z_ARR_P(entries);
+ }
+ zend_hash_next_index_insert(ht, &pair);
+}
+
+ZEND_METHOD(DeferScope, __destruct)
+{
+ ZEND_PARSE_PARAMETERS_NONE();
+
+ zval *entries = OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0);
+ ZVAL_DEREF(entries);
+ if (Z_TYPE_P(entries) != IS_ARRAY) {
+ return;
+ }
+
+ zval *pair;
+ ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL_P(entries), pair) {
+ ZVAL_DEREF(pair);
+ zval *fn = zend_hash_index_find(Z_ARRVAL_P(pair), 0);
+ zval *args = zend_hash_index_find(Z_ARRVAL_P(pair), 1);
+ if (!fn || !args) {
+ continue;
+ }
+ zend_fcall_info fci;
+ zend_fcall_info_cache fcc;
+ char *error = NULL;
+ if (zend_fcall_info_init(fn, 0, &fci, &fcc, NULL, &error) == SUCCESS) {
+ zval ret;
+ ZVAL_UNDEF(&ret);
+ fci.retval = &ret;
+ zend_fcall_info_args(&fci, args);
+ zend_call_function(&fci, &fcc);
+ zend_fcall_info_args_clear(&fci, 1);
+ zval_ptr_dtor(&ret);
+ } else if (error) {
+ efree(error);
+ }
+ if (EG(exception)) {
+ break; /* stop running further defers once one throws */
+ }
+ } ZEND_HASH_FOREACH_END();
+}
+
+void zend_register_defer(void)
+{
+ zend_ce_defer_scope = register_class_DeferScope();
+}
diff --git a/Zend/zend_defer.h b/Zend/zend_defer.h
new file mode 100644
index 00000000..eb97a95c
--- /dev/null
+++ b/Zend/zend_defer.h
@@ -0,0 +1,21 @@
+/*
+ +----------------------------------------------------------------------+
+ | DeferScope — runtime support for the `defer` statement. |
+ | |
+ | A per-frame collector: the compiler lowers each `defer CALL;` to |
+ | `$<hidden> ??= new DeferScope(); $<hidden>->push(callee(...), [args])`|
+ | and the destructor replays the collected calls LIFO on scope exit. |
+ +----------------------------------------------------------------------+
+*/
+
+#ifndef ZEND_DEFER_H
+#define ZEND_DEFER_H
+
+#include "zend.h"
+
+BEGIN_EXTERN_C()
+extern ZEND_API zend_class_entry *zend_ce_defer_scope;
+void zend_register_defer(void);
+END_EXTERN_C()
+
+#endif /* ZEND_DEFER_H */
diff --git a/Zend/zend_defer.stub.php b/Zend/zend_defer.stub.php
new file mode 100644
index 00000000..21a379ef
--- /dev/null
+++ b/Zend/zend_defer.stub.php
@@ -0,0 +1,15 @@
+<?php
+
+/** @generate-class-entries */
+
+/**
+ * Per-frame collector for `defer` statements (internal). The compiler emits
+ * `$<hidden> ??= new DeferScope(); $<hidden>->push(callee(...), [args]);` and the
+ * destructor runs the collected calls LIFO when the frame is torn down.
+ */
+final class DeferScope
+{
+ private array $entries = [];
+ public function push(callable $fn, array $args): void {}
+ public function __destruct() {}
+}
diff --git a/Zend/zend_defer_arginfo.h b/Zend/zend_defer_arginfo.h
new file mode 100644
index 00000000..a85465ea
--- /dev/null
+++ b/Zend/zend_defer_arginfo.h
@@ -0,0 +1,35 @@
+/* This is a generated file, edit the .stub.php file instead.
+ * Stub hash: 6ecc478aedc186cf4d459c1daa826b1fdc6112cd */
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_DeferScope_push, 0, 2, IS_VOID, 0)
+ ZEND_ARG_TYPE_INFO(0, fn, IS_CALLABLE, 0)
+ ZEND_ARG_TYPE_INFO(0, args, IS_ARRAY, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DeferScope___destruct, 0, 0, 0)
+ZEND_END_ARG_INFO()
+
+ZEND_METHOD(DeferScope, push);
+ZEND_METHOD(DeferScope, __destruct);
+
+static const zend_function_entry class_DeferScope_methods[] = {
+ ZEND_ME(DeferScope, push, arginfo_class_DeferScope_push, ZEND_ACC_PUBLIC)
+ ZEND_ME(DeferScope, __destruct, arginfo_class_DeferScope___destruct, ZEND_ACC_PUBLIC)
+ ZEND_FE_END
+};
+
+static zend_class_entry *register_class_DeferScope(void)
+{
+ zend_class_entry ce, *class_entry;
+
+ INIT_CLASS_ENTRY(ce, "DeferScope", class_DeferScope_methods);
+ class_entry = zend_register_internal_class_with_flags(&ce, NULL, ZEND_ACC_FINAL);
+
+ zval property_entries_default_value;
+ ZVAL_EMPTY_ARRAY(&property_entries_default_value);
+ zend_string *property_entries_name = zend_string_init("entries", sizeof("entries") - 1, 1);
+ zend_declare_typed_property(class_entry, property_entries_name, &property_entries_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
+ zend_string_release(property_entries_name);
+
+ return class_entry;
+}