|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Aleksandr Cherednikov |
| 3 | + * Licensed under the MIT License. See LICENSE for details. |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef PHP_EVENTLOOP_COMPAT_H |
| 7 | +#define PHP_EVENTLOOP_COMPAT_H |
| 8 | + |
| 9 | +#include "php.h" |
| 10 | +#include "zend_API.h" |
| 11 | + |
| 12 | +/* |
| 13 | + * Compatibility shims for building across PHP 8.1 – 8.5+. |
| 14 | + * |
| 15 | + * The extension targets PHP >= 8.1 (Fibers). Internal Zend APIs evolved |
| 16 | + * between releases, so we polyfill the newer signatures here and let |
| 17 | + * each call-site use a single, modern spelling. |
| 18 | + */ |
| 19 | + |
| 20 | +/* ------------------------------------------------------------------ */ |
| 21 | +/* zend_register_internal_class_with_flags() — added in PHP 8.4 */ |
| 22 | +/* In 8.1-8.3 we register + set flags manually. */ |
| 23 | +/* ------------------------------------------------------------------ */ |
| 24 | +#if PHP_VERSION_ID < 80400 |
| 25 | +static inline zend_class_entry *zend_register_internal_class_with_flags( |
| 26 | + zend_class_entry *ce, zend_class_entry *parent, uint32_t flags) |
| 27 | +{ |
| 28 | + zend_class_entry *registered = zend_register_internal_class_ex(ce, parent); |
| 29 | + registered->ce_flags |= flags; |
| 30 | + return registered; |
| 31 | +} |
| 32 | +#endif |
| 33 | + |
| 34 | +/* ------------------------------------------------------------------ */ |
| 35 | +/* ce->default_object_handlers — added in PHP 8.3 */ |
| 36 | +/* ------------------------------------------------------------------ */ |
| 37 | +#if PHP_VERSION_ID >= 80300 |
| 38 | +# define EVENTLOOP_SET_DEFAULT_HANDLERS(ce, h) \ |
| 39 | + (ce)->default_object_handlers = (h) |
| 40 | +#else |
| 41 | +# define EVENTLOOP_SET_DEFAULT_HANDLERS(ce, h) \ |
| 42 | + /* not available before 8.3 — handlers set in create_object */ |
| 43 | +#endif |
| 44 | + |
| 45 | +/* ------------------------------------------------------------------ */ |
| 46 | +/* Fiber suspend / resume internal API */ |
| 47 | +/* */ |
| 48 | +/* PHP 8.4+ exposes zend_fiber_suspend() / zend_fiber_resume() with a */ |
| 49 | +/* three-arg signature. Earlier versions do not export these symbols, */ |
| 50 | +/* so we call the userland Fiber::suspend() / $fiber->resume() via */ |
| 51 | +/* zend_call_method, which performs the same context switch. */ |
| 52 | +/* ------------------------------------------------------------------ */ |
| 53 | +#include "zend_fibers.h" |
| 54 | +#include "zend_interfaces.h" |
| 55 | + |
| 56 | +#if PHP_VERSION_ID < 80400 |
| 57 | + |
| 58 | +static inline void eventloop_compat_fiber_suspend( |
| 59 | + zend_fiber *fiber, zval *value, zval *return_value) |
| 60 | +{ |
| 61 | + (void)fiber; /* Fiber::suspend() operates on the active fiber */ |
| 62 | + if (value && Z_TYPE_P(value) != IS_NULL) { |
| 63 | + zend_call_method_with_1_params(NULL, zend_ce_fiber, NULL, "suspend", return_value, value); |
| 64 | + } else { |
| 65 | + zend_call_method_with_0_params(NULL, zend_ce_fiber, NULL, "suspend", return_value); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +static inline void eventloop_compat_fiber_resume( |
| 70 | + zend_fiber *fiber, zval *value, zval *error) |
| 71 | +{ |
| 72 | + zval fiber_zv, retval; |
| 73 | + (void)error; |
| 74 | + ZVAL_OBJ(&fiber_zv, &fiber->std); |
| 75 | + if (value && Z_TYPE_P(value) != IS_NULL) { |
| 76 | + zend_call_method_with_1_params(&fiber_zv, zend_ce_fiber, NULL, "resume", &retval, value); |
| 77 | + } else { |
| 78 | + zend_call_method_with_0_params(&fiber_zv, zend_ce_fiber, NULL, "resume", &retval); |
| 79 | + } |
| 80 | + zval_ptr_dtor(&retval); |
| 81 | +} |
| 82 | + |
| 83 | +# define zend_fiber_suspend(f, v, r) eventloop_compat_fiber_suspend((f), (v), (r)) |
| 84 | +# define zend_fiber_resume(f, v, e) eventloop_compat_fiber_resume((f), (v), (e)) |
| 85 | + |
| 86 | +#endif /* PHP_VERSION_ID < 80400 */ |
| 87 | + |
| 88 | +#endif /* PHP_EVENTLOOP_COMPAT_H */ |
0 commit comments