Skip to content

Commit 4c8175b

Browse files
authored
* Compatible with php-8.1 * Fix compilation errors * fix
1 parent 6732ba6 commit 4c8175b

File tree

4 files changed

+88
-69
lines changed

4 files changed

+88
-69
lines changed

ext-src/php_swoole.cc

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_mime_type_read, 0, 0, 1)
179179
ZEND_ARG_INFO(0, filename)
180180
ZEND_END_ARG_INFO()
181181

182-
const zend_function_entry swoole_functions[] =
183-
{
182+
const zend_function_entry swoole_functions[] = {
184183
PHP_FE(swoole_version, arginfo_swoole_void)
185184
PHP_FE(swoole_cpu_num, arginfo_swoole_void)
186185
PHP_FE(swoole_last_error, arginfo_swoole_void)
@@ -218,33 +217,24 @@ const zend_function_entry swoole_functions[] =
218217
PHP_FE(swoole_internal_call_user_shutdown_begin, arginfo_swoole_void)
219218
PHP_FE_END /* Must be the last line in swoole_functions[] */
220219
};
221-
// clang-format on
222-
223-
zend_class_entry *swoole_exception_ce;
224-
zend_object_handlers swoole_exception_handlers;
225-
226-
zend_class_entry *swoole_error_ce;
227-
zend_object_handlers swoole_error_handlers;
228220

229221
static const zend_module_dep swoole_deps[] = {
230222
#ifdef SW_USE_JSON
231-
ZEND_MOD_REQUIRED("json")
223+
ZEND_MOD_REQUIRED("json")
232224
#endif
233225
#ifdef SW_USE_MYSQLND
234-
ZEND_MOD_REQUIRED("mysqlnd")
226+
ZEND_MOD_REQUIRED("mysqlnd")
235227
#endif
236228
#ifdef SW_SOCKETS
237-
ZEND_MOD_REQUIRED("sockets")
229+
ZEND_MOD_REQUIRED("sockets")
238230
#endif
239231
#ifdef SW_USE_CURL
240-
ZEND_MOD_REQUIRED("curl")
232+
ZEND_MOD_REQUIRED("curl")
241233
#endif
242-
ZEND_MOD_END
234+
ZEND_MOD_END
243235
};
244236

245-
// clang-format off
246-
zend_module_entry swoole_module_entry =
247-
{
237+
zend_module_entry swoole_module_entry = {
248238
STANDARD_MODULE_HEADER_EX,
249239
nullptr,
250240
swoole_deps,
@@ -260,6 +250,12 @@ zend_module_entry swoole_module_entry =
260250
};
261251
// clang-format on
262252

253+
zend_class_entry *swoole_exception_ce;
254+
zend_object_handlers swoole_exception_handlers;
255+
256+
zend_class_entry *swoole_error_ce;
257+
zend_object_handlers swoole_error_handlers;
258+
263259
#ifdef COMPILE_DL_SWOOLE
264260
ZEND_GET_MODULE(swoole)
265261
#endif
@@ -304,6 +300,14 @@ static void php_swoole_init_globals(zend_swoole_globals *swoole_globals) {
304300

305301
void php_swoole_register_shutdown_function(const char *function) {
306302
php_shutdown_function_entry shutdown_function_entry;
303+
#if PHP_VERSION_ID >= 80100
304+
zval function_name;
305+
ZVAL_STRING(&function_name, function);
306+
zend_fcall_info_init(
307+
&function_name, 0, &shutdown_function_entry.fci, &shutdown_function_entry.fci_cache, NULL, NULL);
308+
register_user_shutdown_function(Z_STRVAL(function_name), Z_STRLEN(function_name), &shutdown_function_entry);
309+
zval_ptr_dtor(&function_name);
310+
#else
307311
zval *function_name;
308312
#if PHP_VERSION_ID >= 80000
309313
shutdown_function_entry.arg_count = 0;
@@ -315,8 +319,8 @@ void php_swoole_register_shutdown_function(const char *function) {
315319
function_name = &shutdown_function_entry.arguments[0];
316320
#endif
317321
ZVAL_STRING(function_name, function);
318-
register_user_shutdown_function(
319-
Z_STRVAL_P(function_name), Z_STRLEN_P(function_name), &shutdown_function_entry);
322+
register_user_shutdown_function(Z_STRVAL_P(function_name), Z_STRLEN_P(function_name), &shutdown_function_entry);
323+
#endif
320324
}
321325

322326
void php_swoole_set_global_option(HashTable *vht) {
@@ -398,14 +402,17 @@ SW_API bool php_swoole_is_enable_coroutine() {
398402
static void fatal_error(int code, const char *format, ...) {
399403
va_list args;
400404
va_start(args, format);
401-
zend_object *exception = zend_throw_exception(swoole_error_ce, swoole::std_string::vformat(format, args).c_str(), code);
405+
zend_object *exception =
406+
zend_throw_exception(swoole_error_ce, swoole::std_string::vformat(format, args).c_str(), code);
402407
va_end(args);
403408

404409
zend_try {
405410
zend_exception_error(exception, E_ERROR);
406-
} zend_catch {
411+
}
412+
zend_catch {
407413
exit(255);
408-
} zend_end_try();
414+
}
415+
zend_end_try();
409416
}
410417

411418
/* {{{ PHP_MINIT_FUNCTION
@@ -967,14 +974,14 @@ static void _sw_zend_string_free(void *address) {
967974
zend_string_free(zend::fetch_zend_string_by_val(address));
968975
}
969976

970-
static swoole::Allocator php_allocator {
977+
static swoole::Allocator php_allocator{
971978
_sw_emalloc,
972979
_sw_ecalloc,
973980
_sw_erealloc,
974981
_sw_efree,
975982
};
976983

977-
static swoole::Allocator zend_string_allocator {
984+
static swoole::Allocator zend_string_allocator{
978985
_sw_zend_string_malloc,
979986
_sw_zend_string_calloc,
980987
_sw_zend_string_realloc,
@@ -1335,17 +1342,17 @@ static PHP_FUNCTION(swoole_internal_call_user_shutdown_begin) {
13351342

13361343
static PHP_FUNCTION(swoole_substr_unserialize) {
13371344
zend_long offset, length = 0;
1338-
char *buf = NULL;
1339-
size_t buf_len;
1345+
char *buf = NULL;
1346+
size_t buf_len;
13401347
zval *options = NULL;
13411348

1342-
ZEND_PARSE_PARAMETERS_START(2, 4)
1349+
ZEND_PARSE_PARAMETERS_START(2, 4)
13431350
Z_PARAM_STRING(buf, buf_len)
13441351
Z_PARAM_LONG(offset)
13451352
Z_PARAM_OPTIONAL
13461353
Z_PARAM_LONG(length)
13471354
Z_PARAM_ARRAY(options)
1348-
ZEND_PARSE_PARAMETERS_END();
1355+
ZEND_PARSE_PARAMETERS_END();
13491356

13501357
if (buf_len == 0) {
13511358
RETURN_FALSE;
@@ -1366,21 +1373,21 @@ static PHP_FUNCTION(swoole_substr_unserialize) {
13661373
static PHP_FUNCTION(swoole_substr_json_decode) {
13671374
zend_long offset, length = 0;
13681375
char *str;
1369-
size_t str_len;
1370-
zend_bool assoc = 0; /* return JS objects as PHP objects by default */
1371-
zend_bool assoc_null = 1;
1372-
zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
1373-
zend_long options = 0;
1376+
size_t str_len;
1377+
zend_bool assoc = 0; /* return JS objects as PHP objects by default */
1378+
zend_bool assoc_null = 1;
1379+
zend_long depth = PHP_JSON_PARSER_DEFAULT_DEPTH;
1380+
zend_long options = 0;
13741381

1375-
ZEND_PARSE_PARAMETERS_START(2, 6)
1382+
ZEND_PARSE_PARAMETERS_START(2, 6)
13761383
Z_PARAM_STRING(str, str_len)
13771384
Z_PARAM_LONG(offset)
13781385
Z_PARAM_OPTIONAL
13791386
Z_PARAM_LONG(length)
13801387
Z_PARAM_BOOL_EX(assoc, assoc_null, 1, 0)
13811388
Z_PARAM_LONG(depth)
13821389
Z_PARAM_LONG(options)
1383-
ZEND_PARSE_PARAMETERS_END();
1390+
ZEND_PARSE_PARAMETERS_END();
13841391

13851392
if (str_len == 0) {
13861393
RETURN_FALSE;
@@ -1397,12 +1404,11 @@ static PHP_FUNCTION(swoole_substr_json_decode) {
13971404
/* For BC reasons, the bool $assoc overrides the long $options bit for PHP_JSON_OBJECT_AS_ARRAY */
13981405
if (!assoc_null) {
13991406
if (assoc) {
1400-
options |= PHP_JSON_OBJECT_AS_ARRAY;
1407+
options |= PHP_JSON_OBJECT_AS_ARRAY;
14011408
} else {
14021409
options &= ~PHP_JSON_OBJECT_AS_ARRAY;
14031410
}
14041411
}
14051412
zend::json_decode(return_value, str + offset, length, options, depth);
14061413
}
14071414
#endif
1408-

ext-src/swoole_coroutine.cc

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ using swoole::PHPCoroutine;
3636
using swoole::coroutine::Socket;
3737
using swoole::coroutine::System;
3838

39-
#define PHP_CORO_TASK_SLOT \
40-
((int) ((ZEND_MM_ALIGNED_SIZE(sizeof(PHPContext)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / \
39+
#define PHP_CORO_TASK_SLOT \
40+
((int) ((ZEND_MM_ALIGNED_SIZE(sizeof(PHPContext)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / \
4141
ZEND_MM_ALIGNED_SIZE(sizeof(zval))))
4242

4343
enum sw_exit_flags { SW_EXIT_IN_COROUTINE = 1 << 1, SW_EXIT_IN_SERVER = 1 << 2 };
4444

4545
bool PHPCoroutine::activated = false;
4646
zend_array *PHPCoroutine::options = nullptr;
4747

48-
PHPCoroutine::Config PHPCoroutine::config {
48+
PHPCoroutine::Config PHPCoroutine::config{
4949
SW_DEFAULT_MAX_CORO_NUM,
5050
0,
5151
false,
5252
true,
5353
};
5454

55-
PHPContext PHPCoroutine::main_task {};
55+
PHPContext PHPCoroutine::main_task{};
5656
std::thread PHPCoroutine::interrupt_thread;
5757
bool PHPCoroutine::interrupt_thread_running = false;
5858

@@ -72,9 +72,15 @@ static unordered_map<long, Coroutine *> user_yield_coros;
7272
#define ZEND_ERROR_CB_LAST_ARG_RELAY message
7373
#endif
7474

75+
#if PHP_VERSION_ID < 80100
76+
typedef const char error_filename_t;
77+
#else
78+
typedef zend_string error_filename_t;
79+
#endif
80+
7581
static void (*orig_interrupt_function)(zend_execute_data *execute_data) = nullptr;
7682
static void (*orig_error_function)(int type,
77-
const char *error_filename,
83+
error_filename_t *error_filename,
7884
const uint32_t error_lineno,
7985
ZEND_ERROR_CB_LAST_ARG_D) = nullptr;
8086

@@ -323,28 +329,31 @@ void PHPCoroutine::activate() {
323329

324330
/* replace the error function to save execute_data */
325331
orig_error_function = zend_error_cb;
326-
zend_error_cb = [](int type, const char *error_filename, const uint32_t error_lineno, ZEND_ERROR_CB_LAST_ARG_D) {
327-
if (sw_unlikely(type & E_FATAL_ERRORS)) {
328-
if (activated) {
329-
/* update the last coroutine's info */
330-
save_task(get_context());
331-
}
332-
if (sw_reactor()) {
333-
sw_reactor()->running = false;
334-
sw_reactor()->bailout = true;
335-
}
332+
zend_error_cb =
333+
[](int type, error_filename_t *error_filename, const uint32_t error_lineno, ZEND_ERROR_CB_LAST_ARG_D) {
334+
if (sw_unlikely(type & E_FATAL_ERRORS)) {
335+
if (activated) {
336+
/* update the last coroutine's info */
337+
save_task(get_context());
338+
}
339+
if (sw_reactor()) {
340+
sw_reactor()->running = false;
341+
sw_reactor()->bailout = true;
342+
}
336343
#ifdef SW_EXIT_WHEN_OCCURS_FATAL_ERROR
337-
zend_try {
338-
orig_error_function(type, error_filename, error_lineno, ZEND_ERROR_CB_LAST_ARG_RELAY);
339-
} zend_catch {
340-
exit(255);
341-
} zend_end_try();
344+
zend_try {
345+
orig_error_function(type, error_filename, error_lineno, ZEND_ERROR_CB_LAST_ARG_RELAY);
346+
}
347+
zend_catch {
348+
exit(255);
349+
}
350+
zend_end_try();
342351
#endif
343-
}
344-
if (sw_likely(orig_error_function)) {
345-
orig_error_function(type, error_filename, error_lineno, ZEND_ERROR_CB_LAST_ARG_RELAY);
346-
}
347-
};
352+
}
353+
if (sw_likely(orig_error_function)) {
354+
orig_error_function(type, error_filename, error_lineno, ZEND_ERROR_CB_LAST_ARG_RELAY);
355+
}
356+
};
348357

349358
if (SWOOLE_G(enable_preemptive_scheduler) || config.enable_preemptive_scheduler) {
350359
/* create a thread to interrupt the coroutine that takes up too much time */
@@ -757,10 +766,10 @@ void PHPCoroutine::main_func(void *arg) {
757766
defer_fci->fci.param_count = 1;
758767
defer_fci->fci.params = retval;
759768
#else
760-
if (Z_TYPE_P(retval) != IS_UNDEF) {
761-
defer_fci->fci.param_count = 1;
762-
defer_fci->fci.params = retval;
763-
}
769+
if (Z_TYPE_P(retval) != IS_UNDEF) {
770+
defer_fci->fci.param_count = 1;
771+
defer_fci->fci.params = retval;
772+
}
764773
#endif
765774

766775
if (UNEXPECTED(sw_zend_call_function_anyway(&defer_fci->fci, &defer_fci->fci_cache) != SUCCESS)) {
@@ -1075,7 +1084,7 @@ static PHP_METHOD(swoole_coroutine, yield) {
10751084
Coroutine *co = Coroutine::get_current_safe();
10761085
user_yield_coros[co->get_cid()] = co;
10771086

1078-
Coroutine::CancelFunc cancel_fn = [](Coroutine *co){
1087+
Coroutine::CancelFunc cancel_fn = [](Coroutine *co) {
10791088
user_yield_coros.erase(co->get_cid());
10801089
co->resume();
10811090
return true;

ext-src/swoole_runtime.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,9 +1699,8 @@ static int stream_array_emulate_read_fd_set(zval *stream_array) {
16991699
return 0;
17001700
}
17011701

1702-
ZVAL_NEW_ARR(&new_array);
1702+
array_init_size(&new_array, zend_hash_num_elements(Z_ARRVAL_P(stream_array)));
17031703
ht = Z_ARRVAL(new_array);
1704-
zend_hash_init(ht, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), nullptr, ZVAL_PTR_DTOR, 0);
17051704

17061705
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) {
17071706
ZVAL_DEREF(elem);

ext-src/swoole_server.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ void php_swoole_server_rshutdown() {
100100
#else
101101
PG(last_error_message)->val,
102102
#endif
103+
104+
#if PHP_VERSION_ID >= 80100
105+
PG(last_error_file) ? PG(last_error_file)->val : "-",
106+
#else
103107
PG(last_error_file) ? PG(last_error_file) : "-",
108+
#endif
104109
PG(last_error_lineno));
105110
} else {
106111
swoole_error_log(

0 commit comments

Comments
 (0)