Skip to content

Commit

Permalink
Added 8.4 supports, Fix #5451
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 4, 2024
1 parent 4f07fe8 commit 5e226cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ SW_THREAD_LOCAL bool PHPCoroutine::interrupt_thread_running = false;
extern void php_swoole_load_library();

static zend_atomic_bool *zend_vm_interrupt = nullptr;
#if PHP_VERSION_ID < 80400
static user_opcode_handler_t ori_exit_handler = nullptr;
#endif
static user_opcode_handler_t ori_begin_silence_handler = nullptr;
static user_opcode_handler_t ori_end_silence_handler = nullptr;
static unordered_map<long, Coroutine *> user_yield_coros;
Expand Down Expand Up @@ -176,6 +178,7 @@ static const zend_function_entry swoole_exit_exception_methods[] = {
};
// clang-format on

#if PHP_VERSION_ID < 80400
static int coro_exit_handler(zend_execute_data *execute_data) {
zval ex;
zend_object *obj;
Expand Down Expand Up @@ -220,6 +223,37 @@ static int coro_exit_handler(zend_execute_data *execute_data) {

return ZEND_USER_OPCODE_DISPATCH;
}
#else
extern ZEND_FUNCTION(exit);
PHP_FUNCTION(swoole_exit) {
zend_string *message = NULL;
zend_long status = 0;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STR_OR_LONG(message, status)
ZEND_PARSE_PARAMETERS_END();

zend_long flags = 0;
if (Coroutine::get_current()) {
flags |= SW_EXIT_IN_COROUTINE;
}

if (sw_server() && sw_server()->is_started()) {
flags |= SW_EXIT_IN_SERVER;
}

if (flags) {
zval ex = {};
zend_object *obj = zend_throw_exception(swoole_exit_exception_ce, (message ? ZSTR_VAL(message) : "swoole exit"), 0);
ZVAL_OBJ(&ex, obj);
zend_update_property_long(swoole_exit_exception_ce, SW_Z8_OBJ_P(&ex), ZEND_STRL("flags"), flags);
zend_update_property_long(swoole_exit_exception_ce, SW_Z8_OBJ_P(&ex), ZEND_STRL("status"), status);
} else {
ZEND_FN(exit)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
}
#endif

static int coro_begin_silence_handler(zend_execute_data *execute_data) {
PHPContext *task = PHPCoroutine::get_context();
Expand Down Expand Up @@ -987,8 +1021,10 @@ void php_swoole_coroutine_minit(int module_number) {

void php_swoole_coroutine_rinit() {
if (SWOOLE_G(cli)) {
#if PHP_VERSION_ID < 80400
ori_exit_handler = zend_get_user_opcode_handler(ZEND_EXIT);
zend_set_user_opcode_handler(ZEND_EXIT, coro_exit_handler);
#endif

ori_begin_silence_handler = zend_get_user_opcode_handler(ZEND_BEGIN_SILENCE);
zend_set_user_opcode_handler(ZEND_BEGIN_SILENCE, coro_begin_silence_handler);
Expand Down
6 changes: 6 additions & 0 deletions ext-src/swoole_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ static PHP_FUNCTION(swoole_time_sleep_until);
static PHP_FUNCTION(swoole_stream_select);
static PHP_FUNCTION(swoole_stream_socket_pair);
static PHP_FUNCTION(swoole_user_func_handler);
#if PHP_VERSION_ID >= 80400
extern PHP_FUNCTION(swoole_exit);
#endif
SW_EXTERN_C_END

static void inherit_class(const char *child_name, size_t child_length, const char *parent_name, size_t parent_length);
Expand Down Expand Up @@ -233,6 +236,9 @@ struct real_func {
void php_swoole_runtime_rinit() {
tmp_function_table = (zend_array *) emalloc(sizeof(zend_array));
zend_hash_init(tmp_function_table, 8, nullptr, nullptr, 0);
#if PHP_VERSION_ID >= 80400
SW_HOOK_FUNC(exit);
#endif
}

void php_swoole_runtime_rshutdown() {
Expand Down

0 comments on commit 5e226cd

Please sign in to comment.