xref: /php-uv/php_uv.c (revision 3c89bc46)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2012 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Shuhei Tanuma <chobieeee@php.net>                           |
16    |          Bob Weinand <bobwei9@hotmail.com>                           |
17    +----------------------------------------------------------------------+
18  */
19 
20 #pragma GCC diagnostic ignored "-Wmissing-braces"
21 
22 #include "php_uv.h"
23 #include "php_main.h"
24 #include "ext/standard/info.h"
25 #include "zend_smart_str.h"
26 
27 #ifndef PHP_UV_DEBUG
28 #define PHP_UV_DEBUG 0
29 #endif
30 
31 #if defined(ZTS) && PHP_VERSION_ID < 80000
32 #undef TSRMLS_C
33 #undef TSRMLS_CC
34 #undef TSRMLS_D
35 #undef TSRMLS_DC
36 #define TSRMLS_C tsrm_ls
37 #define TSRMLS_CC , TSRMLS_C
38 #define TSRMLS_D void *tsrm_ls
39 #define TSRMLS_DC , TSRMLS_D
40 
41 #ifdef COMPILE_DL_UV
42 ZEND_TSRMLS_CACHE_DEFINE()
43 #endif
44 #endif
45 
46 ZEND_DECLARE_MODULE_GLOBALS(uv);
47 
48 #ifndef GC_ADDREF
49 	#define GC_ADDREF(ref) ++GC_REFCOUNT(ref)
50 #endif
51 
52 #if PHP_VERSION_ID < 70100
53 	#define uv_zend_wrong_parameter_class_error(throw, ...) zend_wrong_paramer_class_error(__VA_ARGS__)
54 #elif PHP_VERSION_ID < 70200 || PHP_VERSION_ID >= 70300
55 	#define uv_zend_wrong_parameter_class_error(throw, ...) zend_wrong_parameter_class_error(__VA_ARGS__)
56 #else
57 	#define uv_zend_wrong_parameter_class_error(...) zend_wrong_parameter_class_error(__VA_ARGS__)
58 #endif
59 
60 #if PHP_VERSION_ID < 70200
61 	#define UV_PARAM_PROLOGUE Z_PARAM_PROLOGUE(0)
62 #else
63 	#define UV_PARAM_PROLOGUE Z_PARAM_PROLOGUE(0, 0)
64 #endif
65 
66 #if PHP_VERSION_ID < 70400
67 	#define _error_code error_code
68 #endif
69 
70 #if PHP_VERSION_ID >= 80000
71 #define zend_internal_type_error(strict_types, ...) zend_type_error(__VA_ARGS__)
72 #endif
73 
74 #define UV_PARAM_OBJ_EX(dest, type, check_null, ce, ...) \
75 	{ \
76 		zval *zv; \
77 		UV_PARAM_PROLOGUE \
78 		if (UNEXPECTED(!uv_parse_arg_object(_arg, &zv, check_null, ce, ##__VA_ARGS__, NULL))) { \
79 			if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
80 				zend_string *names = php_uv_concat_ce_names(ce, ##__VA_ARGS__, NULL); \
81 				uv_zend_wrong_parameter_class_error(_flags & ZEND_PARSE_PARAMS_THROW, _i, ZSTR_VAL(names), _arg); \
82 				zend_string_release(names); \
83 			} \
84 			_error_code = ZPP_ERROR_FAILURE; \
85 			break; \
86 		} \
87 		if (GC_FLAGS(Z_OBJ_P(zv)) & IS_OBJ_DESTRUCTOR_CALLED) { \
88 			if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) { \
89 				php_error_docref(NULL, E_WARNING, "passed %s handle is already closed", ZSTR_VAL(Z_OBJCE_P(_arg)->name)); \
90 			} \
91 			_error_code = ZPP_ERROR_FAILURE; \
92 			break; \
93 		} \
94 		dest = zv == NULL ? NULL : (type *) Z_OBJ_P(zv); \
95 	}
96 
97 #define UV_PARAM_OBJ(dest, type, ...) UV_PARAM_OBJ_EX(dest, type, 0, ##__VA_ARGS__, NULL)
98 #define UV_PARAM_OBJ_NULL(dest, type, ...) UV_PARAM_OBJ_EX(dest, type, 1, ##__VA_ARGS__, NULL)
99 
php_uv_concat_ce_names(zend_class_entry * ce,zend_class_entry * next,...)100 static ZEND_COLD zend_string *php_uv_concat_ce_names(zend_class_entry *ce, zend_class_entry *next, ...) {
101 	va_list va;
102 	smart_str buf = {0};
103 
104 	va_start(va, next);
105 
106 	if (!next) {
107 		return zend_string_copy(ce->name);
108 	}
109 
110 	goto start;
111 	do {
112 		if (next) {
113 			smart_str_appends(&buf, ", ");
114 		} else {
115 			smart_str_appends(&buf, " or ");
116 		}
117 start:
118 		smart_str_append(&buf, ce->name);
119 		ce = next;
120 		next = (zend_class_entry *) va_arg(va, zend_class_entry *);
121 	} while (next);
122 
123 	va_end(va);
124 
125 	smart_str_0(&buf);
126 	return buf.s;
127 }
128 
129 /* gcc complains: sorry, unimplemented: function ‘uv_parse_arg_object’ can never be inlined because it uses variable argument lists */
130 #ifdef __clang__
uv_parse_arg_object(zval * arg,zval ** dest,int check_null,zend_class_entry * ce,...)131 static zend_always_inline int uv_parse_arg_object(zval *arg, zval **dest, int check_null, zend_class_entry *ce, ...) {
132 #else
133 static int uv_parse_arg_object(zval *arg, zval **dest, int check_null, zend_class_entry *ce, ...) {
134 #endif
135 	if (EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
136 		va_list va;
137 		zend_class_entry *argce = Z_OBJCE_P(arg);
138 		va_start(va, ce);
139 		do {
140 			if (instanceof_function(argce, ce)) {
141 				*dest = arg;
142 				return 1;
143 			}
144 			ce = (zend_class_entry *) va_arg(va, zend_class_entry *);
145 		} while (ce);
146 	} else if (check_null && EXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
147 		*dest = NULL;
148 		return 1;
149 	}
150 	return 0;
151 }
152 
153 #define PHP_UV_DEINIT_UV(uv) \
154 	clean_uv_handle(uv); \
155 	OBJ_RELEASE(&uv->std);
156 
157 #define PHP_UV_INIT_GENERIC(dest, type, ce) \
158 	do { \
159 		zval zv; \
160 		object_init_ex(&zv, ce); \
161 		dest = (type *) Z_OBJ(zv); \
162 	} while (0)
163 
164 #define PHP_UV_INIT_UV(uv, ce) PHP_UV_INIT_GENERIC(uv, php_uv_t, ce)
165 
166 #define PHP_UV_INIT_UV_EX(_uv, ce, cb, ...) \
167 	do { \
168 		int r; \
169 		PHP_UV_INIT_UV(_uv, ce); \
170 		r = cb(&loop->loop, (void *) &_uv->uv.handle, ##__VA_ARGS__); \
171 		if (r) { \
172 			PHP_UV_DEINIT_UV(_uv); \
173 			php_error_docref(NULL, E_WARNING, #cb " failed"); \
174 			RETURN_FALSE; \
175 		} \
176 	} while (0)
177 
178 #define PHP_UV_INIT_CONNECT(req, uv) \
179 	req = (uv_connect_t *) emalloc(sizeof(uv_connect_t)); \
180 	req->data = uv;
181 
182 #define PHP_UV_INIT_WRITE_REQ(w, uv, str, strlen, cb) \
183 	w = (write_req_t *) emalloc(sizeof(write_req_t)); \
184 	w->req.data = uv; \
185 	w->buf = uv_buf_init(estrndup(str, strlen), strlen); \
186 	w->cb = cb; \
187 
188 #define PHP_UV_INIT_SEND_REQ(w, uv, str, strlen) \
189 	w = (send_req_t *) emalloc(sizeof(send_req_t)); \
190 	w->req.data = uv; \
191 	w->buf = uv_buf_init(estrndup(str, strlen), strlen); \
192 
193 #define PHP_UV_FETCH_UV_DEFAULT_LOOP(loop) \
194 	if (loop == NULL) { \
195 		loop = php_uv_default_loop(); \
196 	}  \
197 
198 #define PHP_UV_INIT_LOCK(lock, lock_type) \
199 	PHP_UV_INIT_GENERIC(lock, php_uv_lock_t, uv_lock_ce); \
200 	lock->type = lock_type;
201 
202 #define PHP_UV_CHECK_VALID_FD(fd, zstream) \
203 	if (fd < 0) { \
204 		php_error_docref(NULL, E_WARNING, "invalid variable passed. can't convert to fd."); \
205 		PHP_UV_DEINIT_UV(uv); \
206 		RETURN_FALSE; \
207 	} \
208 	if (Z_ISUNDEF(uv->fs_fd)) { \
209 		ZVAL_COPY(&uv->fs_fd, zstream); \
210 	}
211 
212 #define PHP_UV_ZVAL_TO_VALID_POLL_FD(fd, zstream) \
213 { \
214 	fd = php_uv_zval_to_valid_poll_fd(zstream); \
215 	PHP_UV_CHECK_VALID_FD(fd, zstream) \
216 }
217 
218 #define PHP_UV_ZVAL_TO_FD(fd, zstream) \
219 { \
220 	fd = php_uv_zval_to_fd(zstream); \
221 	PHP_UV_CHECK_VALID_FD(fd, zstream) \
222 }
223 
224 #define PHP_UV_FS_ASYNC(loop, func,  ...) \
225 	error = uv_fs_##func(&loop->loop, (uv_fs_t*)&uv->uv.fs, __VA_ARGS__, php_uv_fs_cb); \
226 	if (error) { \
227 		PHP_UV_DEINIT_UV(uv); \
228 		php_error_docref(NULL, E_WARNING, "uv_" #func " failed"); \
229 		return; \
230 	}
231 
232 #define PHP_UV_INIT_ZVALS(uv) \
233 	{ \
234 		int ix = 0;\
235 		for (ix = 0; ix < PHP_UV_CB_MAX; ix++) {\
236 			uv->callback[ix] = NULL;\
237 		} \
238 		ZVAL_UNDEF(&uv->fs_fd); \
239 		ZVAL_UNDEF(&uv->fs_fd_alt); \
240 	}
241 
242 #if PHP_VERSION_ID < 70300
243  #define PHP_UV_SKIP_DTOR(uv) do { GC_FLAGS(&uv->std) |= IS_OBJ_DESTRUCTOR_CALLED; } while (0)
244 #else
245  #define PHP_UV_SKIP_DTOR(uv) do { GC_ADD_FLAGS(&uv->std, IS_OBJ_DESTRUCTOR_CALLED); } while (0)
246 #endif
247 #define PHP_UV_IS_DTORED(uv) (GC_FLAGS(&uv->std) & IS_OBJ_DESTRUCTOR_CALLED)
248 
249 #define PHP_UV_SOCKADDR_IPV4_INIT(sockaddr) PHP_UV_INIT_GENERIC(sockaddr, php_uv_sockaddr_t, uv_sockaddr_ipv4_ce);
250 #define PHP_UV_SOCKADDR_IPV6_INIT(sockaddr) PHP_UV_INIT_GENERIC(sockaddr, php_uv_sockaddr_t, uv_sockaddr_ipv6_ce);
251 
252 #define PHP_UV_SOCKADDR_IS_IPV4(sockaddr) (sockaddr->std.ce == uv_sockaddr_ipv4_ce)
253 #define PHP_UV_SOCKADDR_IS_IPV6(sockaddr) (sockaddr->std.ce == uv_sockaddr_ipv6_ce)
254 
255 #define PHP_UV_SOCKADDR_IPV4(sockaddr) sockaddr->addr.ipv4
256 #define PHP_UV_SOCKADDR_IPV4_P(sockaddr) &sockaddr->addr.ipv4
257 
258 #define PHP_UV_SOCKADDR_IPV6(sockaddr) sockaddr->addr.ipv6
259 #define PHP_UV_SOCKADDR_IPV6_P(sockaddr) &sockaddr->addr.ipv6
260 
261 #define PHP_UV_LOCK_RWLOCK_P(_lock) &_lock->lock.rwlock
262 #define PHP_UV_LOCK_MUTEX_P(_lock) &_lock->lock.mutex
263 #define PHP_UV_LOCK_SEM_P(_lock) &_lock->lock.semaphore
264 
265 #define PHP_UV_FD_TO_ZVAL(zv, fd) { php_stream *_stream = php_stream_fopen_from_fd(fd, "w+", NULL); zval *_z = (zv); php_stream_to_zval(_stream, _z); }
266 
267 #if PHP_UV_DEBUG>=1
268 #define PHP_UV_DEBUG_PRINT(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
269 #else
270 #define PHP_UV_DEBUG_PRINT(format, ...)
271 #endif
272 
273 #if PHP_UV_DEBUG>=1
274 #define PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(handler, uv) \
275 	{ \
276 		PHP_UV_DEBUG_PRINT("# %s add(%p - %s): %u->%u\n", #handler, uv, ZSTR_VAL(uv->std.ce->name), GC_REFCOUNT(&(uv)->std) - 1, GC_REFCOUNT(&(uv)->std)); \
277 	}
278 #define PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(handler, uv) \
279 	{ \
280 		PHP_UV_DEBUG_PRINT("# %s del(%p - %s): %u->%u\n", #handler, uv, ZSTR_VAL(uv->std.ce->name), GC_REFCOUNT(&(uv)->std), GC_REFCOUNT(&(uv)->std) - 1); \
281 	}
282 #else
283 #define PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(hander, uv)
284 #define PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(hander, uv)
285 #endif
286 
287 #if defined(ZTS) && PHP_VERSION_ID < 80000
288 #define UV_FETCH_ALL(ls, id, type) ((type) (*((void ***) ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])
289 #define UV_FETCH_CTX(ls, id, type, element) (((type) (*((void ***) ls))[TSRM_UNSHUFFLE_RSRC_ID(id)])->element)
290 #define UV_CG(ls, v)  UV_FETCH_CTX(ls, compiler_globals_id, zend_compiler_globals*, v)
291 #define UV_CG_ALL(ls) UV_FETCH_ALL(ls, compiler_globals_id, zend_compiler_globals*)
292 #define UV_EG(ls, v)  UV_FETCH_CTX(ls, executor_globals_id, zend_executor_globals*, v)
293 #define UV_SG(ls, v)  UV_FETCH_CTX(ls, sapi_globals_id, sapi_globals_struct*, v)
294 #define UV_EG_ALL(ls) UV_FETCH_ALL(ls, executor_globals_id, zend_executor_globals*)
295 #endif
296 
297 #if !defined(PHP_WIN32) && !(defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
298 # if PHP_VERSION_ID >= 80000
299 __attribute__((weak)) zend_class_entry *socket_ce = NULL;
300 # else
301 int (*php_sockets_le_socket_ptr)(void) = NULL;
302 int php_sockets_le_socket(void) __attribute__((weak));
303 # endif
304 #endif
305 
306 /* objects */
307 extern void php_uv_init(zend_class_entry *uv_ce);
308 
309 static zend_object_handlers uv_default_handlers;
310 
311 static zend_class_entry *uv_ce;
312 static zend_object_handlers uv_handlers;
313 
314 static zend_class_entry *uv_stream_ce;
315 
316 static zend_class_entry *uv_tcp_ce;
317 static zend_class_entry *uv_udp_ce;
318 static zend_class_entry *uv_pipe_ce;
319 static zend_class_entry *uv_idle_ce;
320 static zend_class_entry *uv_timer_ce;
321 static zend_class_entry *uv_async_ce;
322 static zend_class_entry *uv_addrinfo_ce;
323 static zend_class_entry *uv_process_ce;
324 static zend_class_entry *uv_prepare_ce;
325 static zend_class_entry *uv_check_ce;
326 static zend_class_entry *uv_work_ce;
327 static zend_class_entry *uv_fs_ce;
328 static zend_class_entry *uv_fs_event_ce;
329 static zend_class_entry *uv_tty_ce;
330 static zend_class_entry *uv_fs_poll_ce;
331 static zend_class_entry *uv_poll_ce;
332 static zend_class_entry *uv_signal_ce;
333 
334 static zend_class_entry *uv_loop_ce;
335 static zend_object_handlers uv_loop_handlers;
336 
337 static zend_class_entry *uv_sockaddr_ce;
338 
339 static zend_class_entry *uv_sockaddr_ipv4_ce;
340 static zend_class_entry *uv_sockaddr_ipv6_ce;
341 
342 static zend_class_entry *uv_lock_ce;
343 static zend_object_handlers uv_lock_handlers;
344 
345 static zend_class_entry *uv_stdio_ce;
346 static zend_object_handlers uv_stdio_handlers;
347 
348 
349 typedef struct {
350 	uv_write_t req;
351 	uv_buf_t buf;
352 	php_uv_cb_t *cb;
353 } write_req_t;
354 
355 typedef struct {
356 	uv_udp_send_t req;
357 	uv_buf_t buf;
358 } send_req_t;
359 
360 enum php_uv_socket_type {
361 	PHP_UV_TCP_IPV4 = 1,
362 	PHP_UV_TCP_IPV6 = 2,
363 	PHP_UV_TCP      = 3,
364 	PHP_UV_UDP_IPV4 = 16,
365 	PHP_UV_UDP_IPV6 = 32,
366 	PHP_UV_UDP      = 48,
367 };
368 
369 /* declarations */
370 
371 static void php_uv_fs_cb(uv_fs_t* req);
372 /**
373  * execute callback
374  *
375  * @param zval* retval_ptr non-initialized pointer. this will be allocate from zend_call_function
376  * @param php_uv_cb_t* callback callable object
377  * @param zval* params parameters.
378  * @param int param_count
379  * @return int (maybe..)
380  */
381 static int php_uv_do_callback(zval *retval_ptr, php_uv_cb_t *callback, zval *params, int param_count TSRMLS_DC);
382 
383 void static destruct_uv(zend_object *obj);
384 void static clean_uv_handle(php_uv_t *uv);
385 
386 static void php_uv_tcp_connect_cb(uv_connect_t *conn_req, int status);
387 
388 static void php_uv_write_cb(uv_write_t* req, int status);
389 
390 static void php_uv_listen_cb(uv_stream_t* server, int status);
391 
392 static void php_uv_shutdown_cb(uv_shutdown_t* req, int status);
393 
394 static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread,const uv_buf_t* buf);
395 
396 /* unused: static void php_uv_read2_cb(uv_pipe_t* handle, ssize_t nread, uv_buf_t buf, uv_handle_type pending); */
397 
398 static void php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
399 
400 static void php_uv_close(php_uv_t *uv);
401 
402 static void php_uv_timer_cb(uv_timer_t *handle);
403 
404 static void php_uv_idle_cb(uv_timer_t *handle);
405 
406 static void php_uv_signal_cb(uv_signal_t *handle, int sig_num);
407 
408 
409 static php_uv_loop_t *php_uv_default_loop()
410 {
411 	if (UV_G(default_loop) == NULL) {
412 		zval zv;
413 		object_init_ex(&zv, uv_loop_ce);
414 		UV_G(default_loop) = (php_uv_loop_t *) Z_OBJ(zv);
415 	}
416 
417 	return UV_G(default_loop);
418 }
419 
420 static php_socket_t php_uv_zval_to_valid_poll_fd(zval *ptr)
421 {
422 	php_socket_t fd = -1;
423 	php_stream *stream;
424 
425 	/* Validate Checks */
426 
427 #if !defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
428 	php_socket *socket;
429 #endif
430 	/* TODO: is this correct on windows platform? */
431 	if (Z_TYPE_P(ptr) == IS_RESOURCE) {
432 		if ((stream = (php_stream *) zend_fetch_resource_ex(ptr, NULL, php_file_le_stream()))) {
433 			/* make sure only valid resource streams are passed - plainfiles and most php streams are invalid */
434 			if (stream->wrapper && !strcmp((char *)stream->wrapper->wops->label, "PHP") && (!stream->orig_path || (strncmp(stream->orig_path, "php://std", sizeof("php://std") - 1) && strncmp(stream->orig_path, "php://fd", sizeof("php://fd") - 1)))) {
435 				php_error_docref(NULL, E_WARNING, "invalid resource passed, this resource is not supported");
436 				return -1;
437 			}
438 
439 			/* Some streams (specifically STDIO and encrypted streams) can be cast to FDs */
440 			if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&fd, 1) == SUCCESS && fd >= 0) {
441 				if (stream->wrapper && !strcmp((char *)stream->wrapper->wops->label, "plainfile")) {
442 #ifndef PHP_WIN32
443 					struct stat stat;
444 					fstat(fd, &stat);
445 					if (!S_ISFIFO(stat.st_mode))
446 #endif
447 					{
448 						php_error_docref(NULL, E_WARNING, "invalid resource passed, this plain files are not supported");
449 						return -1;
450 					}
451 				}
452 				return fd;
453 			}
454 
455 			fd = -1;
456 #if PHP_VERSION_ID < 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
457 		} else if (php_sockets_le_socket_ptr && (socket = (php_socket *) zend_fetch_resource_ex(ptr, NULL, php_sockets_le_socket_ptr()))) {
458 			fd = socket->bsd_socket;
459 #endif
460 		} else {
461 			php_error_docref(NULL, E_WARNING, "unhandled resource type detected.");
462 			fd = -1;
463 		}
464 #if PHP_VERSION_ID >= 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
465 	} else if (socket_ce && Z_TYPE_P(ptr) == IS_OBJECT && Z_OBJCE_P(ptr) == socket_ce && (socket = (php_socket *) ((char *)(Z_OBJ_P(ptr)) - XtOffsetOf(php_socket, std)))) {
466 		fd = socket->bsd_socket;
467 #endif
468 	}
469 
470 	return fd;
471 }
472 
473 static php_socket_t php_uv_zval_to_fd(zval *ptr)
474 {
475 	php_socket_t fd = -1;
476 	php_stream *stream;
477 #if !defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
478 	php_socket *socket;
479 #endif
480 	/* TODO: is this correct on windows platform? */
481 	if (Z_TYPE_P(ptr) == IS_RESOURCE) {
482 		if ((stream = (php_stream *) zend_fetch_resource_ex(ptr, NULL, php_file_le_stream()))) {
483 			if (php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void *) &fd, 1) != SUCCESS || fd < 0) {
484 				fd = -1;
485 			}
486 #if PHP_VERSION_ID < 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
487 		} else if (php_sockets_le_socket_ptr && (socket = (php_socket *) zend_fetch_resource_ex(ptr, NULL, php_sockets_le_socket_ptr()))) {
488 			fd = socket->bsd_socket;
489 #endif
490 		} else {
491 			php_error_docref(NULL, E_WARNING, "unhandled resource type detected.");
492 			fd = -1;
493 		}
494 	} else if (Z_TYPE_P(ptr) == IS_LONG) {
495 		fd = Z_LVAL_P(ptr);
496 		if (fd < 0) {
497 			fd = -1;
498 		}
499 
500 		{
501 			/* make sure that a valid resource handle was passed - issue #36 */
502 			int err = uv_guess_handle((uv_file) fd);
503 			if (err == UV_UNKNOWN_HANDLE) {
504 				php_error_docref(NULL, E_WARNING, "invalid resource type detected");
505 				fd = -1;
506 			}
507 		}
508 #if PHP_VERSION_ID >= 80000 && (!defined(PHP_WIN32) || (defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS)))
509 	} else if (socket_ce && Z_TYPE_P(ptr) == IS_OBJECT && Z_OBJCE_P(ptr) == socket_ce && (socket = (php_socket *) ((char *)(Z_OBJ_P(ptr)) - XtOffsetOf(php_socket, std)))) {
510 		fd = socket->bsd_socket;
511 #endif
512 	}
513 
514 	return fd;
515 }
516 
517 static const char* php_uv_strerror(long error_code)
518 {
519 	/* Note: uv_strerror doesn't use assert. we don't need check value here */
520 	return uv_strerror(error_code);
521 }
522 
523 static php_uv_cb_t* php_uv_cb_init_dynamic(php_uv_t *uv, zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
524 	php_uv_cb_t *cb = emalloc(sizeof(php_uv_cb_t));
525 
526 	memcpy(&cb->fci, fci, sizeof(zend_fcall_info));
527 	memcpy(&cb->fcc, fcc, sizeof(zend_fcall_info_cache));
528 
529 	if (ZEND_FCI_INITIALIZED(*fci)) {
530 		Z_TRY_ADDREF(cb->fci.function_name);
531 		if (fci->object) {
532 			GC_ADDREF(cb->fci.object);
533 		}
534 	}
535 
536 	return cb;
537 }
538 
539 static void php_uv_cb_init(php_uv_cb_t **result, php_uv_t *uv, zend_fcall_info *fci, zend_fcall_info_cache *fcc, enum php_uv_callback_type type)
540 {
541 	php_uv_cb_t *cb;
542 
543 	if (uv->callback[type] == NULL) {
544 		cb = emalloc(sizeof(php_uv_cb_t));
545 	} else {
546 		cb = uv->callback[type];
547 
548 		if (Z_TYPE(cb->fci.function_name) != IS_UNDEF) {
549 			zval_dtor(&cb->fci.function_name);
550 		}
551 		if (fci->object) {
552 			OBJ_RELEASE(fci->object);
553 		}
554 	}
555 
556 	memcpy(&cb->fci, fci, sizeof(zend_fcall_info));
557 	memcpy(&cb->fcc, fcc, sizeof(zend_fcall_info_cache));
558 
559 	if (ZEND_FCI_INITIALIZED(*fci)) {
560 		Z_TRY_ADDREF(cb->fci.function_name);
561 		if (fci->object) {
562 			GC_ADDREF(cb->fci.object);
563 		}
564 	}
565 
566 	uv->callback[type] = cb;
567 }
568 
569 static void php_uv_lock_init(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
570 {
571 	php_uv_lock_t *lock = NULL;
572 	int error = 0;
573 
574 	switch (lock_type) {
575 		case IS_UV_RWLOCK:
576 		case IS_UV_RWLOCK_WR:
577 		case IS_UV_RWLOCK_RD:
578 		{
579 			PHP_UV_INIT_LOCK(lock, IS_UV_RWLOCK);
580 			error = uv_rwlock_init(PHP_UV_LOCK_RWLOCK_P(lock));
581 		}
582 		break;
583 		case IS_UV_MUTEX:
584 		{
585 			PHP_UV_INIT_LOCK(lock, IS_UV_MUTEX);
586 			error = uv_mutex_init(PHP_UV_LOCK_MUTEX_P(lock));
587 		}
588 		break;
589 		case IS_UV_SEMAPHORE:
590 		{
591 			zend_long val = 0;
592 
593 			if (zend_parse_parameters(ZEND_NUM_ARGS(),
594 				"l", &val) == FAILURE) {
595 				return;
596 			}
597 
598 			PHP_UV_INIT_LOCK(lock, IS_UV_SEMAPHORE);
599 			error = uv_sem_init(PHP_UV_LOCK_SEM_P(lock), (int) val);
600 		}
601 		break;
602 		default:
603 			php_error_docref(NULL, E_ERROR, "unexpected type");
604 		break;
605 	}
606 
607 	if (error == 0) {
608 		RETURN_OBJ(&lock->std);
609 	} else {
610 		OBJ_RELEASE(&lock->std);
611 		RETURN_FALSE;
612 	}
613 }
614 
615 static void php_uv_lock_lock(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
616 {
617 	php_uv_lock_t *lock;
618 
619 	ZEND_PARSE_PARAMETERS_START(1, 1)
620 		UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
621 	ZEND_PARSE_PARAMETERS_END();
622 
623 	switch (lock_type) {
624 		case IS_UV_RWLOCK:
625 		case IS_UV_RWLOCK_RD:
626 		{
627 			if (lock->locked == 0x01) {
628 				zend_error(E_WARNING, "Cannot acquire a read lock while holding a write lock");
629 				RETURN_FALSE;
630 			}
631 
632 			uv_rwlock_rdlock(PHP_UV_LOCK_RWLOCK_P(lock));
633 			if (!lock->locked++) {
634 				lock->locked = 0x02;
635 			}
636 		}
637 		break;
638 		case IS_UV_RWLOCK_WR:
639 		{
640 			if (lock->locked) {
641 				zend_error(E_WARNING, "Cannot acquire a write lock when already holding a lock");
642 				RETURN_FALSE;
643 			}
644 
645 			uv_rwlock_wrlock(PHP_UV_LOCK_RWLOCK_P(lock));
646 			lock->locked = 0x01;
647 		}
648 		break;
649 		case IS_UV_MUTEX:
650 		{
651 			uv_mutex_lock(PHP_UV_LOCK_MUTEX_P(lock));
652 			lock->locked = 0x01;
653 		}
654 		break;
655 		case IS_UV_SEMAPHORE:
656 		{
657 			uv_sem_post(PHP_UV_LOCK_SEM_P(lock));
658 		}
659 		break;
660 		default:
661 			php_error_docref(NULL, E_ERROR, "unexpected type");
662 		break;
663 	}
664 }
665 
666 static void php_uv_lock_unlock(enum php_uv_lock_type  lock_type, INTERNAL_FUNCTION_PARAMETERS)
667 {
668 	php_uv_lock_t *lock;
669 
670 	ZEND_PARSE_PARAMETERS_START(1, 1)
671 		UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
672 	ZEND_PARSE_PARAMETERS_END();
673 
674 	switch (lock_type) {
675 		case IS_UV_RWLOCK:
676 		case IS_UV_RWLOCK_RD:
677 		{
678 			if (lock->locked > 0x01) {
679 				uv_rwlock_rdunlock(PHP_UV_LOCK_RWLOCK_P(lock));
680 				if (--lock->locked == 0x01) {
681 					lock->locked = 0x00;
682 				}
683 			}
684 		}
685 		break;
686 		case IS_UV_RWLOCK_WR:
687 		{
688 			if (lock->locked == 0x01) {
689 				uv_rwlock_wrunlock(PHP_UV_LOCK_RWLOCK_P(lock));
690 				lock->locked = 0x00;
691 			}
692 		}
693 		break;
694 		case IS_UV_MUTEX:
695 		{
696 			if (lock->locked == 0x01) {
697 				uv_mutex_unlock(PHP_UV_LOCK_MUTEX_P(lock));
698 				lock->locked = 0x00;
699 			}
700 		}
701 		break;
702 		case IS_UV_SEMAPHORE:
703 		{
704 			uv_sem_wait(PHP_UV_LOCK_SEM_P(lock));
705 		}
706 		break;
707 		default:
708 			php_error_docref(NULL, E_ERROR, "unexpected type");
709 		break;
710 	}
711 }
712 
713 static void php_uv_lock_trylock(enum php_uv_lock_type lock_type, INTERNAL_FUNCTION_PARAMETERS)
714 {
715 	php_uv_lock_t *lock;
716 	int error = 0;
717 
718 	ZEND_PARSE_PARAMETERS_START(1, 1)
719 		UV_PARAM_OBJ(lock, php_uv_lock_t, uv_lock_ce)
720 	ZEND_PARSE_PARAMETERS_END();
721 
722 	switch(lock_type) {
723 		case IS_UV_RWLOCK:
724 		case IS_UV_RWLOCK_RD:
725 		{
726 			if (lock->locked == 0x01) {
727 				zend_error(E_WARNING, "Cannot acquire a read lock while holding a write lock");
728 				RETURN_FALSE;
729 			}
730 
731 			error = uv_rwlock_tryrdlock(PHP_UV_LOCK_RWLOCK_P(lock));
732 			if (error == 0) {
733 				if (!lock->locked++) {
734 					lock->locked = 0x02;
735 				}
736 				RETURN_TRUE;
737 			} else {
738 				RETURN_FALSE;
739 			}
740 		}
741 		break;
742 		case IS_UV_RWLOCK_WR:
743 		{
744 			if (lock->locked) {
745 				zend_error(E_WARNING, "Cannot acquire a write lock when already holding a lock");
746 				RETURN_FALSE;
747 			}
748 
749 			error = uv_rwlock_trywrlock(PHP_UV_LOCK_RWLOCK_P(lock));
750 			if (error == 0) {
751 				lock->locked = 0x01;
752 				RETURN_TRUE;
753 			} else {
754 				RETURN_FALSE;
755 			}
756 		}
757 		break;
758 		case IS_UV_MUTEX:
759 		{
760 			error = uv_mutex_trylock(PHP_UV_LOCK_MUTEX_P(lock));
761 
762 			if (error == 0) {
763 				lock->locked = 0x01;
764 				RETURN_TRUE;
765 			} else {
766 				RETURN_FALSE;
767 			}
768 		}
769 		break;
770 		case IS_UV_SEMAPHORE:
771 		{
772 			error = uv_sem_trywait(PHP_UV_LOCK_SEM_P(lock));
773 			RETURN_LONG(error);
774 		}
775 		default:
776 			php_error_docref(NULL, E_ERROR, "unexpected type");
777 		break;
778 	}
779 }
780 
781 
782 static void php_uv_fs_common(uv_fs_type fs_type, INTERNAL_FUNCTION_PARAMETERS)
783 {
784 	int error = 0;
785 	php_uv_loop_t *loop;
786 	php_uv_t *uv;
787 	zend_fcall_info fci       = empty_fcall_info;
788 	zend_fcall_info_cache fcc = empty_fcall_info_cache;
789 	php_uv_cb_t *cb;
790 
791 #define PHP_UV_FS_PARSE_PARAMETERS_EX(num, params, required_cb) \
792 	ZEND_PARSE_PARAMETERS_START(1 + num + required_cb, 2 + num) \
793 		UV_PARAM_OBJ(loop, php_uv_loop_t, uv_loop_ce) \
794 		params \
795 		if (!required_cb) { \
796 			Z_PARAM_OPTIONAL \
797 		} \
798 		Z_PARAM_FUNC_EX(fci, fcc, 1, 0) \
799 	ZEND_PARSE_PARAMETERS_END()
800 
801 #define PHP_UV_FS_PARSE_PARAMETERS(num, params) PHP_UV_FS_PARSE_PARAMETERS_EX(num, params, 0)
802 
803 #define PHP_UV_FS_SETUP() \
804 	PHP_UV_INIT_UV(uv, uv_fs_ce); \
805 	PHP_UV_FETCH_UV_DEFAULT_LOOP(loop); \
806 	php_uv_cb_init(&cb, uv, &fci, &fcc, PHP_UV_FS_CB);
807 
808 #define PHP_UV_FS_SETUP_AND_EXECUTE(command, ...) \
809 	PHP_UV_FS_SETUP(); \
810 	PHP_UV_FS_ASYNC(loop, command, __VA_ARGS__);
811 
812 	switch (fs_type) {
813 		case UV_FS_SYMLINK:
814 		{
815 			zend_string *from, *to;
816 			zend_long flags;
817 
818 			PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_STR(from) Z_PARAM_STR(to) Z_PARAM_LONG(flags));
819 			PHP_UV_FS_SETUP_AND_EXECUTE(symlink, from->val, to->val, flags);
820 			break;
821 		}
822 		case UV_FS_LINK:
823 		{
824 			zend_string *from, *to;
825 
826 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(from) Z_PARAM_STR(to));
827 			PHP_UV_FS_SETUP_AND_EXECUTE(link, from->val, to->val);
828 			break;
829 		}
830 		case UV_FS_CHMOD:
831 		{
832 			zend_long mode;
833 			zend_string *path;
834 
835 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(path) Z_PARAM_LONG(mode));
836 			PHP_UV_FS_SETUP_AND_EXECUTE(chmod, path->val, mode);
837 			break;
838 		}
839 		case UV_FS_FCHMOD:
840 		{
841 			zval *zstream;
842 			zend_long mode;
843 			unsigned long fd;
844 
845 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(mode));
846 			PHP_UV_FS_SETUP();
847 			PHP_UV_ZVAL_TO_FD(fd, zstream);
848 			uv->fs_fd = *zstream;
849 			Z_ADDREF(uv->fs_fd);
850 			PHP_UV_FS_ASYNC(loop, fchmod, fd, mode);
851 			break;
852 		}
853 		case UV_FS_RENAME:
854 		{
855 			zend_string *from, *to;
856 
857 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(from) Z_PARAM_STR(to));
858 			PHP_UV_FS_SETUP_AND_EXECUTE(rename, from->val, to->val);
859 			break;
860 		}
861 		case UV_FS_UNLINK:
862 		{
863 			zend_string *path;
864 
865 			PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_STR(path));
866 			PHP_UV_FS_SETUP_AND_EXECUTE(unlink, path->val);
867 			break;
868 		}
869 		case UV_FS_RMDIR:
870 		{
871 			zend_string *path;
872 
873 			PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_STR(path));
874 			PHP_UV_FS_SETUP_AND_EXECUTE(rmdir, path->val);
875 			break;
876 		}
877 		case UV_FS_MKDIR:
878 		{
879 			zend_string *path;
880 			zend_long mode;
881 
882 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_STR(path) Z_PARAM_LONG(mode));
883 			PHP_UV_FS_SETUP_AND_EXECUTE(mkdir, path->val, mode);
884 			break;
885 		}
886 		case UV_FS_FTRUNCATE:
887 		{
888 			zval *zstream = NULL;
889 			zend_long offset = 0;
890 			unsigned long fd;
891 
892 			PHP_UV_FS_PARSE_PARAMETERS(2, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(offset));
893 			PHP_UV_FS_SETUP()
894 			PHP_UV_ZVAL_TO_FD(fd, zstream);
895 			uv->fs_fd = *zstream;
896 			Z_ADDREF(uv->fs_fd);
897 			PHP_UV_FS_ASYNC(loop, ftruncate, fd, offset);
898 			break;
899 		}
900 		case UV_FS_FDATASYNC:
901 		{
902 			zval *zstream = NULL;
903 			unsigned long fd;
904 
905 			PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
906 			PHP_UV_FS_SETUP()
907 			PHP_UV_ZVAL_TO_FD(fd, zstream);
908 			uv->fs_fd = *zstream;
909 			Z_ADDREF(uv->fs_fd);
910 			PHP_UV_FS_ASYNC(loop, fdatasync, fd);
911 			break;
912 		}
913 		case UV_FS_FSYNC:
914 		{
915 			zval *zstream = NULL;
916 			unsigned long fd;
917 
918 			PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
919 			PHP_UV_FS_SETUP()
920 			PHP_UV_ZVAL_TO_FD(fd, zstream);
921 			uv->fs_fd = *zstream;
922 			Z_ADDREF(uv->fs_fd);
923 			PHP_UV_FS_ASYNC(loop, fsync, fd);
924 			break;
925 		}
926 		case UV_FS_CLOSE:
927 		{
928 			zval *zstream = NULL;
929 			unsigned long fd;
930 
931 			PHP_UV_FS_PARSE_PARAMETERS(1, Z_PARAM_RESOURCE(zstream));
932 			PHP_UV_FS_SETUP()
933 			PHP_UV_ZVAL_TO_FD(fd, zstream);
934 			uv->fs_fd = *zstream;
935 			Z_ADDREF(uv->fs_fd);
936 			PHP_UV_FS_ASYNC(loop, close, fd);
937 			break;
938 		}
939 		case UV_FS_CHOWN:
940 		{
941 			zend_long uid, gid;
942 			zend_string *path;
943 
944 			PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_STR(path) Z_PARAM_LONG(uid) Z_PARAM_LONG(gid));
945 			PHP_UV_FS_SETUP_AND_EXECUTE(chown, path->val, uid, gid);
946 			break;
947 		}
948 		case UV_FS_FCHOWN:
949 		{
950 			zval *zstream = NULL;
951 			zend_long uid, gid;
952 			unsigned long fd;
953 
954 			PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(uid) Z_PARAM_LONG(gid));
955 			PHP_UV_FS_SETUP()
956 			PHP_UV_ZVAL_TO_FD(fd, zstream);
957 			uv->fs_fd = *zstream;
958 			Z_ADDREF(uv->fs_fd);
959 			PHP_UV_FS_ASYNC(loop, fchown, fd, uid, gid);
960 			break;
961 		}
962 		case UV_FS_OPEN:
963 		{
964 			zend_string *path;
965 			zend_long flag, mode;
966 
967 			PHP_UV_FS_PARSE_PARAMETERS_EX(3, Z_PARAM_STR(path) Z_PARAM_LONG(flag) Z_PARAM_LONG(mode), 1);
968 			PHP_UV_FS_SETUP_AND_EXECUTE(open, path->val, flag, mode);
969 			break;
970 		}
971 		case UV_FS_SCANDIR:
972 		{
973 			zend_string *path;
974 			zend_long flags = 0;
975 
976 			ZEND_PARSE_PARAMETERS_START(3, 4)
977 				UV_PARAM_OBJ(loop, php_uv_loop_t, uv_loop_ce)
978 				Z_PARAM_STR(path)
979 				Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
980 				Z_PARAM_OPTIONAL
981 				Z_PARAM_LONG(flags)
982 			ZEND_PARSE_PARAMETERS_END();
983 			PHP_UV_FS_SETUP_AND_EXECUTE(scandir, path->val, flags);
984 			break;
985 		}
986 		case UV_FS_LSTAT:
987 		{
988 			zend_string *path;
989 
990 			PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_STR(path), 1);
991 			PHP_UV_FS_SETUP_AND_EXECUTE(lstat, path->val);
992 			break;
993 		}
994 		case UV_FS_FSTAT:
995 		{
996 			zval *zstream = NULL;
997 			unsigned long fd;
998 
999 			PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_RESOURCE(zstream), 1);
1000 			PHP_UV_FS_SETUP()
1001 			PHP_UV_ZVAL_TO_FD(fd, zstream);
1002 			uv->fs_fd = *zstream;
1003 			Z_ADDREF(uv->fs_fd);
1004 			PHP_UV_FS_ASYNC(loop, fstat, fd);
1005 			break;
1006 		}
1007 		case UV_FS_STAT:
1008 		{
1009 			zend_string *path;
1010 
1011 			PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_STR(path), 1);
1012 			PHP_UV_FS_SETUP_AND_EXECUTE(stat, path->val);
1013 			break;
1014 		}
1015 		case UV_FS_UTIME:
1016 		{
1017 			zend_long utime, atime;
1018 			zend_string *path;
1019 
1020 			PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_STR(path) Z_PARAM_LONG(utime) Z_PARAM_LONG(atime));
1021 			PHP_UV_FS_SETUP_AND_EXECUTE(utime, path->val, utime, atime);
1022 			break;
1023 		}
1024 		case UV_FS_FUTIME:
1025 		{
1026 			zval *zstream = NULL;
1027 			zend_long utime, atime;
1028 			unsigned long fd;
1029 
1030 			PHP_UV_FS_PARSE_PARAMETERS(3, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(utime) Z_PARAM_LONG(atime));
1031 			PHP_UV_FS_SETUP()
1032 			PHP_UV_ZVAL_TO_FD(fd, zstream);
1033 			uv->fs_fd = *zstream;
1034 			Z_ADDREF(uv->fs_fd);
1035 			PHP_UV_FS_ASYNC(loop, futime, fd, utime, atime);
1036 			break;
1037 		}
1038 		case UV_FS_READLINK:
1039 		{
1040 			zend_string *path;
1041 
1042 			PHP_UV_FS_PARSE_PARAMETERS_EX(1, Z_PARAM_STR(path), 1);
1043 			PHP_UV_FS_SETUP_AND_EXECUTE(readlink, path->val);
1044 			break;
1045 		}
1046 		case UV_FS_READ:
1047 		{
1048 			zval *zstream = NULL;
1049 			unsigned long fd;
1050 			zend_long length;
1051 			zend_long offset;
1052 			uv_buf_t buf;
1053 
1054 			PHP_UV_FS_PARSE_PARAMETERS_EX(3, Z_PARAM_RESOURCE(zstream) Z_PARAM_LONG(offset) Z_PARAM_LONG(length), 1);
1055 			if (length <= 0) {
1056 				length = 0;
1057 			}
1058 			if (offset < 0) {
1059 				offset = 0;
1060 			}
1061 			PHP_UV_FS_SETUP()
1062 			PHP_UV_ZVAL_TO_FD(fd, zstream);
1063 			uv->fs_fd = *zstream;
1064 			Z_ADDREF(uv->fs_fd);
1065 
1066 			uv->buffer = (char*) emalloc(length);
1067 			buf = uv_buf_init(uv->buffer, length);
1068 
1069 			PHP_UV_FS_ASYNC(loop, read, fd, &buf, 1, offset);
1070 			break;
1071 		}
1072 		case UV_FS_SENDFILE:
1073 		{
1074 			zval *z_instream, *z_outstream = NULL;
1075 			unsigned long in_fd, out_fd;
1076 			zend_long offset, length = 0;
1077 
1078 			PHP_UV_FS_PARSE_PARAMETERS(4, Z_PARAM_RESOURCE(z_instream) Z_PARAM_RESOURCE(z_outstream) Z_PARAM_LONG(offset) Z_PARAM_LONG(length));
1079 			PHP_UV_FS_SETUP()
1080 			/* TODO */
1081 			PHP_UV_ZVAL_TO_FD(in_fd, z_instream);
1082 			PHP_UV_ZVAL_TO_FD(out_fd, z_outstream);
1083 			uv->fs_fd = *z_outstream;
1084 			Z_ADDREF(uv->fs_fd);
1085 			uv->fs_fd_alt = *z_instream;
1086 			Z_ADDREF(uv->fs_fd_alt);
1087 			PHP_UV_FS_ASYNC(loop, sendfile, in_fd, out_fd, offset, length);
1088 			break;
1089 		}
1090 		case UV_FS_WRITE:
1091 		{
1092 			zval *zstream = NULL;
1093 			zend_string *buffer;
1094 			zend_long fd, offset = -1;
1095 			uv_buf_t uv_fs_write_buf_t;
1096 
1097 			ZEND_PARSE_PARAMETERS_START(3, 5)
1098 				UV_PARAM_OBJ(loop, php_uv_loop_t, uv_loop_ce)
1099 				Z_PARAM_RESOURCE(zstream)
1100 				Z_PARAM_STR(buffer)
1101 				Z_PARAM_OPTIONAL
1102 				Z_PARAM_LONG(offset)
1103 				Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
1104 			ZEND_PARSE_PARAMETERS_END();
1105 			PHP_UV_FS_SETUP();
1106 			PHP_UV_ZVAL_TO_FD(fd, zstream);
1107 			uv->fs_fd = *zstream;
1108 			Z_ADDREF(uv->fs_fd);
1109 			uv->buffer = estrndup(buffer->val, buffer->len);
1110 
1111 			/* TODO: is this right?! */
1112 			uv_fs_write_buf_t = uv_buf_init(uv->buffer, buffer->len);
1113 			PHP_UV_FS_ASYNC(loop, write, fd, &uv_fs_write_buf_t, 1, offset);
1114 			break;
1115 		}
1116 		case UV_FS_UNKNOWN:
1117 		case UV_FS_CUSTOM:
1118 		default: {
1119 			php_error_docref(NULL, E_ERROR, "type; %d does not support yet.", fs_type);
1120 			break;
1121 		}
1122 	}
1123 
1124 #undef PHP_UV_FS_PARSE_PARAMETERS
1125 #undef PHP_UV_FS_SETUP
1126 #undef PHP_UV_FS_SETUP_AND_EXECUTE
1127 
1128 }
1129 /* util */
1130 
1131 static zval php_uv_address_to_zval(const struct sockaddr *addr)
1132 {
1133 	zval tmp = {0};
1134 	char ip[INET6_ADDRSTRLEN];
1135 	const struct sockaddr_in *a4;
1136 	const struct sockaddr_in6 *a6;
1137 	int port;
1138 
1139 	array_init(&tmp);
1140 
1141 	switch (addr->sa_family) {
1142 		case AF_INET6:
1143 		{
1144 			a6 = (const struct sockaddr_in6 *)addr;
1145 			uv_inet_ntop(AF_INET, &a6->sin6_addr, ip, sizeof ip);
1146 			port = ntohs(a6->sin6_port);
1147 
1148 			add_assoc_string_ex(&tmp, ZEND_STRL("address"), ip);
1149 			add_assoc_long_ex(&tmp, ZEND_STRL("port"), port);
1150 			add_assoc_string_ex(&tmp, ZEND_STRL("family"), "IPv6");
1151 			break;
1152 		}
1153 		case AF_INET:
1154 		{
1155 			a4 = (const struct sockaddr_in *)addr;
1156 			uv_inet_ntop(AF_INET, &a4->sin_addr, ip, sizeof ip);
1157 			port = ntohs(a4->sin_port);
1158 
1159 			add_assoc_string_ex(&tmp, ZEND_STRL("address"), ip);
1160 			add_assoc_long_ex(&tmp, ZEND_STRL("port"), port);
1161 			add_assoc_string_ex(&tmp, ZEND_STRL("family"), "IPv4");
1162 			break;
1163 		}
1164 		default:
1165 		break;
1166 	}
1167 
1168 	return tmp;
1169 }
1170 
1171 static zval php_uv_make_stat(const uv_stat_t *s)
1172 {
1173 	zval tmp = {0};
1174 	array_init(&tmp);
1175 
1176 	add_assoc_long_ex(&tmp, ZEND_STRL("dev"), s->st_dev);
1177 	add_assoc_long_ex(&tmp, ZEND_STRL("ino"), s->st_ino);
1178 	add_assoc_long_ex(&tmp, ZEND_STRL("mode"), s->st_mode);
1179 	add_assoc_long_ex(&tmp, ZEND_STRL("nlink"), s->st_nlink);
1180 	add_assoc_long_ex(&tmp, ZEND_STRL("uid"), s->st_uid);
1181 	add_assoc_long_ex(&tmp, ZEND_STRL("gid"), s->st_gid);
1182 	add_assoc_long_ex(&tmp, ZEND_STRL("rdev"), s->st_rdev);
1183 	add_assoc_long_ex(&tmp, ZEND_STRL("size"), s->st_size);
1184 
1185 #ifndef PHP_WIN32
1186 	add_assoc_long_ex(&tmp, ZEND_STRL("blksize"), s->st_blksize);
1187 	add_assoc_long_ex(&tmp, ZEND_STRL("blocks"), s->st_blocks);
1188 #endif
1189 
1190 	add_assoc_long_ex(&tmp, ZEND_STRL("atime"), s->st_atim.tv_sec);
1191 	add_assoc_long_ex(&tmp, ZEND_STRL("mtime"), s->st_mtim.tv_sec);
1192 	add_assoc_long_ex(&tmp, ZEND_STRL("ctime"), s->st_ctim.tv_sec);
1193 
1194 	return tmp;
1195 }
1196 
1197 static inline zend_bool php_uv_closeable_type(php_uv_t *uv) {
1198 	zend_class_entry *ce = uv->std.ce;
1199 	return ce == uv_pipe_ce || ce == uv_tty_ce || ce == uv_tcp_ce || ce == uv_udp_ce || ce == uv_prepare_ce || ce == uv_check_ce || ce == uv_idle_ce || ce == uv_async_ce || ce == uv_timer_ce || ce == uv_process_ce || ce == uv_fs_event_ce || ce == uv_poll_ce || ce == uv_fs_poll_ce || ce == uv_signal_ce;
1200 }
1201 
1202 /* destructor */
1203 
1204 void static destruct_uv_lock(zend_object *obj)
1205 {
1206 	php_uv_lock_t *lock = (php_uv_lock_t *) obj;
1207 
1208 	if (lock->type == IS_UV_RWLOCK) {
1209 		if (lock->locked == 0x01) {
1210 			php_error_docref(NULL, E_NOTICE, "uv_rwlock: still locked resource detected; forcing wrunlock");
1211 			uv_rwlock_wrunlock(PHP_UV_LOCK_RWLOCK_P(lock));
1212 		} else if (lock->locked) {
1213 			php_error_docref(NULL, E_NOTICE, "uv_rwlock: still locked resource detected; forcing rdunlock");
1214 			while (--lock->locked > 0) {
1215 				uv_rwlock_rdunlock(PHP_UV_LOCK_RWLOCK_P(lock));
1216 			}
1217 		}
1218 		uv_rwlock_destroy(PHP_UV_LOCK_RWLOCK_P(lock));
1219 	} else if (lock->type == IS_UV_MUTEX) {
1220 		if (lock->locked == 0x01) {
1221 			php_error_docref(NULL, E_NOTICE, "uv_mutex: still locked resource detected; forcing unlock");
1222 			uv_mutex_unlock(PHP_UV_LOCK_MUTEX_P(lock));
1223 		}
1224 		uv_mutex_destroy(PHP_UV_LOCK_MUTEX_P(lock));
1225 	} else if (lock->type == IS_UV_SEMAPHORE) {
1226 		if (lock->locked == 0x01) {
1227 			php_error_docref(NULL, E_NOTICE, "uv_sem: still locked resource detected; forcing unlock");
1228 			uv_sem_post(PHP_UV_LOCK_SEM_P(lock));
1229 		}
1230 		uv_sem_destroy(PHP_UV_LOCK_SEM_P(lock));
1231 	}
1232 }
1233 
1234 static void destruct_uv_loop_walk_cb(uv_handle_t* handle, void* arg)
1235 {
1236 	php_uv_t *uv = (php_uv_t *) handle->data;
1237 	if (!PHP_UV_IS_DTORED(uv)) { // otherwise we're already closing
1238 		php_uv_close(uv);
1239 	}
1240 }
1241 
1242 void static destruct_uv_loop(zend_object *obj)
1243 {
1244 	php_uv_loop_t *loop_obj = (php_uv_loop_t *) obj;
1245 	uv_loop_t *loop = &loop_obj->loop;
1246 	if (loop_obj != UV_G(default_loop)) {
1247 		uv_stop(loop); /* in case we haven't stopped the loop yet otherwise ... */
1248 		uv_run(loop, UV_RUN_DEFAULT); /* invalidate the stop ;-) */
1249 
1250 		/* for proper destruction: close all handles, let libuv call close callback and then close and free the loop */
1251 		uv_walk(loop, destruct_uv_loop_walk_cb, NULL);
1252 		uv_run(loop, UV_RUN_DEFAULT);
1253 	}
1254 }
1255 
1256 void static free_uv_loop(zend_object *obj)
1257 {
1258 	php_uv_loop_t *loop_obj = (php_uv_loop_t *) obj;
1259 	if (loop_obj != UV_G(default_loop)) {
1260 		uv_loop_close(&loop_obj->loop);
1261 	}
1262 	if (loop_obj->gc_buffer) {
1263 		efree(loop_obj->gc_buffer);
1264 	}
1265 }
1266 
1267 void static clean_uv_handle(php_uv_t *uv) {
1268 	int i;
1269 
1270 	/* for now */
1271 	for (i = 0; i < PHP_UV_CB_MAX; i++) {
1272 		php_uv_cb_t *cb = uv->callback[i];
1273 		if (cb != NULL) {
1274 			if (ZEND_FCI_INITIALIZED(cb->fci)) {
1275 				zval_dtor(&cb->fci.function_name);
1276 
1277 				if (cb->fci.object != NULL) {
1278 					OBJ_RELEASE(cb->fci.object);
1279 				}
1280 			}
1281 
1282 			efree(cb);
1283 			cb = NULL;
1284 		}
1285 	}
1286 
1287 	PHP_UV_SKIP_DTOR(uv);
1288 
1289 	if (!Z_ISUNDEF(uv->fs_fd)) {
1290 		zval_ptr_dtor(&uv->fs_fd);
1291 		ZVAL_UNDEF(&uv->fs_fd);
1292 		if (!Z_ISUNDEF(uv->fs_fd_alt)) {
1293 			zval_ptr_dtor(&uv->fs_fd_alt);
1294 			ZVAL_UNDEF(&uv->fs_fd_alt);
1295 		}
1296 	}
1297 }
1298 
1299 void static destruct_uv(zend_object *obj)
1300 {
1301 	php_uv_t *uv = (php_uv_t *) obj;
1302 
1303 	PHP_UV_DEBUG_PRINT("# will be free: (obj: %p)\n", obj);
1304 
1305 	if (!php_uv_closeable_type(uv)) {
1306 		if (uv_cancel(&uv->uv.req) == UV_EBUSY) {
1307 			GC_ADDREF(obj);
1308 		}
1309 		clean_uv_handle(uv);
1310 	} else {
1311 		php_uv_close(uv);
1312 		/* cleaning happens in close_cb */
1313 	}
1314 }
1315 
1316 void static php_uv_free_write_req(write_req_t *wr) {
1317 	if (wr->cb) {
1318 		if (ZEND_FCI_INITIALIZED(wr->cb->fci)) {
1319 			zval_ptr_dtor(&wr->cb->fci.function_name);
1320 			if (wr->cb->fci.object != NULL) {
1321 				OBJ_RELEASE(wr->cb->fci.object);
1322 			}
1323 		}
1324 
1325 		efree(wr->cb);
1326 	}
1327 	if (wr->buf.base) {
1328 		efree(wr->buf.base);
1329 	}
1330 	efree(wr);
1331 }
1332 
1333 /* callback */
1334 static int php_uv_do_callback(zval *retval_ptr, php_uv_cb_t *callback, zval *params, int param_count TSRMLS_DC)
1335 {
1336 	int error;
1337 
1338 #if defined(ZTS) && PHP_VERSION_ID < 80000
1339 	void *old = tsrm_set_interpreter_context(tsrm_ls);
1340 #endif
1341 
1342 	if (ZEND_FCI_INITIALIZED(callback->fci)) {
1343 		callback->fci.params = params;
1344 		callback->fci.retval = retval_ptr;
1345 		callback->fci.param_count = param_count;
1346 #if PHP_VERSION_ID < 80000
1347 		callback->fci.no_separation = 1;
1348 #endif
1349 
1350 		error = zend_call_function(&callback->fci, &callback->fcc);
1351 	} else {
1352 		error = -1;
1353 	}
1354 
1355 #if defined(ZTS) && PHP_VERSION_ID < 80000
1356 	tsrm_set_interpreter_context(old);
1357 #endif
1358 
1359 	return error;
1360 }
1361 
1362 static int php_uv_do_callback2(zval *retval_ptr, php_uv_t *uv, zval *params, int param_count, enum php_uv_callback_type type TSRMLS_DC)
1363 {
1364 	int error = 0;
1365 
1366 #if defined(ZTS) && PHP_VERSION_ID < 80000
1367 	void *old = tsrm_set_interpreter_context(tsrm_ls);
1368 #endif
1369 	if (ZEND_FCI_INITIALIZED(uv->callback[type]->fci)) {
1370 		uv->callback[type]->fci.params        = params;
1371 		uv->callback[type]->fci.retval        = retval_ptr;
1372 		uv->callback[type]->fci.param_count   = param_count;
1373 #if PHP_VERSION_ID < 80000
1374 		uv->callback[type]->fci.no_separation = 1;
1375 #endif
1376 
1377 		if (zend_call_function(&uv->callback[type]->fci, &uv->callback[type]->fcc) != SUCCESS) {
1378 			error = -1;
1379 		}
1380 	} else {
1381 		error = -2;
1382 	}
1383 
1384 #if defined(ZTS) && PHP_VERSION_ID < 80000
1385 	tsrm_set_interpreter_context(old);
1386 #endif
1387 	//zend_fcall_info_args_clear(&uv->callback[type]->fci, 0);
1388 
1389 	if (EG(exception)) {
1390 		switch (type) {
1391 			case PHP_UV_FS_CB:
1392 				uv_stop(uv->uv.fs.loop);
1393 				break;
1394 			case PHP_UV_GETADDR_CB:
1395 				uv_stop(uv->uv.addrinfo.loop);
1396 				break;
1397 			case PHP_UV_AFTER_WORK_CB:
1398 				uv_stop(uv->uv.work.loop);
1399 				break;
1400 			case PHP_UV_SHUTDOWN_CB:
1401 				uv_stop(uv->uv.shutdown.handle->loop);
1402 				break;
1403 			case PHP_UV_SEND_CB:
1404 				uv_stop(uv->uv.udp_send.handle->loop);
1405 				break;
1406 			case PHP_UV_CONNECT_CB:
1407 			case PHP_UV_PIPE_CONNECT_CB:
1408 				uv_stop(uv->uv.connect.handle->loop);
1409 				break;
1410 			default:
1411 				uv_stop(uv->uv.handle.loop);
1412 		}
1413 	}
1414 
1415 	return error;
1416 }
1417 
1418 #if defined(ZTS) && PHP_VERSION_ID < 80000
1419 
1420 static int php_uv_do_callback3(zval *retval_ptr, php_uv_t *uv, zval *params, int param_count, enum php_uv_callback_type type)
1421 {
1422 	int error = 0;
1423 	void *tsrm_ls, *old;
1424 	zend_op_array *ops;
1425 	zend_function fn, *old_fn;
1426 
1427 	if (ZEND_FCI_INITIALIZED(uv->callback[type]->fci)) {
1428 		tsrm_ls = tsrm_new_interpreter_context();
1429 		old = tsrm_set_interpreter_context(tsrm_ls);
1430 
1431 		PG(expose_php) = 0;
1432 		PG(auto_globals_jit) = 0;
1433 
1434 		php_request_startup();
1435 		EG(current_execute_data) = NULL;
1436 		EG(current_module) = phpext_uv_ptr;
1437 
1438 		uv->callback[type]->fci.params        = params;
1439 		uv->callback[type]->fci.retval        = retval_ptr;
1440 		uv->callback[type]->fci.param_count   = param_count;
1441 #if PHP_VERSION_ID < 80000
1442 		uv->callback[type]->fci.no_separation = 1;
1443 #endif
1444 		uv->callback[type]->fci.object = NULL;
1445 #if PHP_VERSION_ID >= 70300
1446 		uv->callback[type]->fci.size = sizeof(zend_fcall_info);
1447 #else
1448 		uv->callback[type]->fcc.initialized = 1;
1449 #endif
1450 
1451 		uv->callback[type]->fcc.calling_scope = NULL;
1452 		uv->callback[type]->fcc.called_scope = NULL;
1453 		uv->callback[type]->fcc.object = NULL;
1454 
1455 		if (!ZEND_USER_CODE(uv->callback[type]->fcc.function_handler->type)) {
1456 			return error = -2;
1457 		}
1458 
1459 		fn = *(old_fn = uv->callback[type]->fcc.function_handler);
1460 		uv->callback[type]->fcc.function_handler = &fn;
1461 
1462 		ops = &fn.op_array;
1463 #if PHP_VERSION_ID < 70400
1464 		ops->run_time_cache = NULL;
1465 #else
1466 		ZEND_MAP_PTR_SET(ops->run_time_cache, NULL);
1467 #endif
1468 		if (ops->fn_flags) {
1469 			ops->fn_flags &= ~ZEND_ACC_CLOSURE;
1470 			ops->prototype = NULL;
1471 		}
1472 
1473 		zend_try {
1474 			if (zend_call_function(&uv->callback[type]->fci, &uv->callback[type]->fcc) != SUCCESS) {
1475 				error = -1;
1476 			}
1477 		} zend_catch {
1478 			error = -1;
1479 		} zend_end_try();
1480 
1481 		// after PHP 7.4 this is arena allocated and automatically freed
1482 #if PHP_VERSION_ID < 70400
1483 		if (ops->run_time_cache && !ops->function_name) {
1484 			efree(ops->run_time_cache);
1485 		}
1486 #endif
1487 
1488 		uv->callback[type]->fcc.function_handler = old_fn;
1489 
1490 		php_request_shutdown(NULL);
1491 		tsrm_set_interpreter_context(old);
1492 		tsrm_free_interpreter_context(tsrm_ls);
1493 	} else {
1494 		error = -2;
1495 	}
1496 
1497 	//zend_fcall_info_args_clear(&uv->callback[type]->fci, 0);
1498 
1499 	return error;
1500 }
1501 #endif
1502 
1503 static void php_uv_tcp_connect_cb(uv_connect_t *req, int status)
1504 {
1505 	zval retval = {0};
1506 	zval params[2] = {0};
1507 	php_uv_t *uv = (php_uv_t *) req->data;
1508 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1509 
1510 	ZVAL_OBJ(&params[0], &uv->std);
1511 	ZVAL_LONG(&params[1], status);
1512 
1513 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_CONNECT_CB TSRMLS_CC);
1514 
1515 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_udp_tcp_cb, uv);
1516 	zval_ptr_dtor(&params[0]);
1517 	zval_ptr_dtor(&params[1]);
1518 
1519 	zval_ptr_dtor(&retval);
1520 	efree(req);
1521 }
1522 
1523 static void php_uv_process_close_cb(uv_process_t* process, int64_t exit_status, int term_signal)
1524 {
1525 	zval retval = {0};
1526 	zval params[3] = {0};
1527 	php_uv_t *uv = (php_uv_t *) process->data;
1528 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1529 
1530 	ZVAL_OBJ(&params[0], &uv->std);
1531 	ZVAL_LONG(&params[1], exit_status);
1532 	ZVAL_LONG(&params[2], term_signal);
1533 
1534 	php_uv_do_callback2(&retval, uv, params, 3, PHP_UV_PROC_CLOSE_CB TSRMLS_CC);
1535 
1536 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_process_close_cb, uv);
1537 	zval_ptr_dtor(&params[0]);
1538 	zval_ptr_dtor(&params[1]);
1539 	zval_ptr_dtor(&params[2]);
1540 
1541 	zval_ptr_dtor(&retval);
1542 }
1543 
1544 
1545 static void php_uv_pipe_connect_cb(uv_connect_t *req, int status)
1546 {
1547 	zval retval = {0};
1548 	zval params[2] = {0};
1549 	php_uv_t *uv = (php_uv_t*)req->data;
1550 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1551 
1552 	ZVAL_OBJ(&params[0], &uv->std);
1553 	ZVAL_LONG(&params[1], status);
1554 
1555 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_PIPE_CONNECT_CB TSRMLS_CC);
1556 
1557 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_pipe_connect_cb, uv);
1558 	zval_ptr_dtor(&params[0]);
1559 	zval_ptr_dtor(&params[1]);
1560 
1561 	zval_ptr_dtor(&retval);
1562 	efree(req);
1563 }
1564 
1565 
1566 static void php_uv_walk_cb(uv_handle_t* handle, void* arg)
1567 {
1568 /*
1569 	zval retval = {0};
1570 	zval params[2] = {0};
1571 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1572 
1573 	ZVAL_LONG(&params[0], status);
1574 	ZVAL_OBJ(&params[1], &uv->std);
1575 
1576 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_PIPE_CONNECT_CB TSRMLS_CC);
1577 
1578 	zval_ptr_dtor(&params[0]);
1579 	zval_ptr_dtor(&params[1]);
1580 	zval_ptr_dtor(&retval);
1581 	efree(req);
1582 */
1583 }
1584 
1585 static void php_uv_write_cb(uv_write_t* req, int status)
1586 {
1587 	write_req_t* wr = (write_req_t*) req;
1588 	zval retval = {0};
1589 	zval params[2] = {0};
1590 	php_uv_t *uv = (php_uv_t *) req->handle->data;
1591 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1592 
1593 	PHP_UV_DEBUG_PRINT("uv_write_cb: status: %d\n", status);
1594 
1595 	ZVAL_OBJ(&params[0], &uv->std);
1596 	ZVAL_LONG(&params[1], status);
1597 
1598 	php_uv_do_callback(&retval, wr->cb, params, 2 TSRMLS_CC);
1599 
1600 	if (EG(exception)) {
1601 		uv_stop(uv->uv.handle.loop);
1602 	}
1603 
1604 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_write_cb, uv);
1605 	zval_ptr_dtor(&params[0]);
1606 	zval_ptr_dtor(&params[1]);
1607 
1608 	zval_ptr_dtor(&retval);
1609 
1610 	php_uv_free_write_req(wr);
1611 }
1612 
1613 static void php_uv_udp_send_cb(uv_udp_send_t* req, int status)
1614 {
1615 	send_req_t* wr = (send_req_t*) req;
1616 	zval retval = {0};
1617 	zval params[2] = {0};
1618 	php_uv_t *uv = (php_uv_t *) req->data;
1619 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1620 
1621 	ZVAL_OBJ(&params[0], &uv->std);
1622 	ZVAL_LONG(&params[1], status);
1623 
1624 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_SEND_CB TSRMLS_CC);
1625 
1626 	if (!uv_is_closing(&uv->uv.handle)) { /* send_cb is invoked *before* the handle is marked as inactive - uv_close() will thus *not* increment the refcount and we must then not delete the refcount here */
1627 		PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_udp_send_cb, uv);
1628 		zval_ptr_dtor(&params[0]);
1629 	}
1630 	zval_ptr_dtor(&params[1]);
1631 
1632 	zval_ptr_dtor(&retval);
1633 
1634 	if (wr->buf.base) {
1635 		efree(wr->buf.base);
1636 	}
1637 	efree(wr);
1638 }
1639 
1640 static void php_uv_listen_cb(uv_stream_t* server, int status)
1641 {
1642 	zval retval = {0};
1643 	zval params[2] = {0};
1644 	php_uv_t *uv = (php_uv_t *) server->data;
1645 
1646 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1647 
1648 	ZVAL_OBJ(&params[0], &uv->std);
1649 	GC_ADDREF(&uv->std);
1650 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_listen_cb, uv);
1651 	ZVAL_LONG(&params[1], status);
1652 
1653 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_LISTEN_CB TSRMLS_CC);
1654 
1655 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_listen_cb, uv);
1656 	zval_ptr_dtor(&params[0]);
1657 	zval_ptr_dtor(&params[1]);
1658 
1659 	zval_ptr_dtor(&retval);
1660 }
1661 
1662 static void php_uv_shutdown_cb(uv_shutdown_t* handle, int status)
1663 {
1664 	zval retval = {0};
1665 	zval params[2] = {0};
1666 	php_uv_t *uv = (php_uv_t *) handle->data;
1667 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1668 
1669 	ZVAL_OBJ(&params[0], &uv->std);
1670 	ZVAL_LONG(&params[1], status);
1671 
1672 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_SHUTDOWN_CB TSRMLS_CC);
1673 
1674 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_shutdown_cb, uv);
1675 	zval_ptr_dtor(&params[0]);
1676 	zval_ptr_dtor(&params[1]);
1677 
1678 	zval_ptr_dtor(&retval);
1679 }
1680 
1681 static void php_uv_read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf)
1682 {
1683 	zval retval = {0};
1684 	zval params[2] = {0};
1685 	php_uv_t *uv = (php_uv_t *) handle->data;
1686 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1687 
1688 	PHP_UV_DEBUG_PRINT("uv_read_cb\n");
1689 
1690 	ZVAL_OBJ(&params[0], &uv->std);
1691 	if (nread > 0) { // uv disables itself when it reaches EOF/error
1692 		GC_ADDREF(&uv->std);
1693 		PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_read_cb, uv);
1694 	}
1695 
1696 	if (nread > 0) {
1697 		ZVAL_STRINGL(&params[1], buf->base, nread);
1698 	} else {
1699 		ZVAL_LONG(&params[1], nread);
1700 	}
1701 
1702 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_READ_CB TSRMLS_CC);
1703 
1704 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_read_cb, uv);
1705 	zval_ptr_dtor(&params[0]);
1706 	zval_ptr_dtor(&params[1]);
1707 
1708 	zval_ptr_dtor(&retval);
1709 
1710 	if (buf->base) {
1711 		efree(buf->base);
1712 	}
1713 }
1714 
1715 /* unused
1716 static void php_uv_read2_cb(uv_pipe_t* handle, ssize_t nread, uv_buf_t buf, uv_handle_type pending)
1717 {
1718 	zval retval = {0};
1719 	zval params[3] = {0};
1720 	php_uv_t *uv = (php_uv_t*)handle->data;
1721 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1722 
1723 	PHP_UV_DEBUG_PRINT("uv_read2_cb\n");
1724 
1725 	ZVAL_OBJ(&params[0], &uv->std);
1726 	if (nread > 0) { // uv disables itself when it reaches EOF/error
1727 		GC_ADDREF(&uv->std);
1728 		PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_read2_cb, uv);
1729 	}
1730 	if (nread > 0) {
1731 		ZVAL_STRINGL(&params[1], buf.base,nread);
1732 	} else {
1733 		ZVAL_LONG(&params[1], nread);
1734 	}
1735 	ZVAL_LONG(&params[2], pending);
1736 
1737 	php_uv_do_callback2(&retval, uv, params, 3, PHP_UV_READ2_CB TSRMLS_CC);
1738 
1739 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_read2_cb, uv);
1740 	zval_ptr_dtor(&params[0]);
1741 	zval_ptr_dtor(&params[1]);
1742 	zval_ptr_dtor(&params[2]);
1743 
1744 	zval_ptr_dtor(&retval);
1745 
1746 	if (buf.base) {
1747 		efree(buf.base);
1748 	}
1749 }
1750 */
1751 
1752 static void php_uv_prepare_cb(uv_prepare_t* handle)
1753 {
1754 	zval retval = {0};
1755 	zval params[1];
1756 	php_uv_t *uv = (php_uv_t*)handle->data;
1757 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1758 
1759 	PHP_UV_DEBUG_PRINT("prepare_cb\n");
1760 
1761 	ZVAL_OBJ(&params[0], &uv->std);
1762 	GC_ADDREF(&uv->std);
1763 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_prepare_cb, uv);
1764 
1765 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_PREPARE_CB TSRMLS_CC);
1766 
1767 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_prepare_cb, uv);
1768 	zval_ptr_dtor(&params[0]);
1769 
1770 	zval_ptr_dtor(&retval);
1771 }
1772 
1773 static void php_uv_check_cb(uv_check_t* handle)
1774 {
1775 	zval retval = {0};
1776 	zval params[1];
1777 	php_uv_t *uv = (php_uv_t*)handle->data;
1778 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1779 
1780 	PHP_UV_DEBUG_PRINT("check_cb\n");
1781 
1782 	ZVAL_OBJ(&params[0], &uv->std);
1783 	GC_ADDREF(&uv->std);
1784 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_check_cb, uv);
1785 
1786 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_CHECK_CB TSRMLS_CC);
1787 
1788 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_check_cb, uv);
1789 	zval_ptr_dtor(&params[0]);
1790 
1791 	zval_ptr_dtor(&retval);
1792 }
1793 
1794 
1795 static void php_uv_async_cb(uv_async_t* handle)
1796 {
1797 	zval retval = {0};
1798 	zval params[1];
1799 	php_uv_t *uv = (php_uv_t*)handle->data;
1800 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1801 
1802 	PHP_UV_DEBUG_PRINT("async_cb\n");
1803 
1804 	ZVAL_OBJ(&params[0], &uv->std);
1805 	GC_ADDREF(&uv->std);
1806 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_async_cb, uv);
1807 
1808 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_ASYNC_CB TSRMLS_CC);
1809 
1810 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_async_cb, uv);
1811 	zval_ptr_dtor(&params[0]);
1812 
1813 	zval_ptr_dtor(&retval);
1814 }
1815 
1816 #if defined(ZTS) && PHP_VERSION_ID < 80000
1817 static void php_uv_work_cb(uv_work_t* req)
1818 {
1819 	zval retval = {0};
1820 	php_uv_t *uv = (php_uv_t*)req->data;
1821 
1822 	uv = (php_uv_t*)req->data;
1823 
1824 	PHP_UV_DEBUG_PRINT("work_cb\n");
1825 
1826 	php_uv_do_callback3(&retval, uv, NULL, 0, PHP_UV_WORK_CB);
1827 }
1828 
1829 static void php_uv_after_work_cb(uv_work_t* req, int status)
1830 {
1831 	zval retval = {0};
1832 	php_uv_t *uv = (php_uv_t*)req->data;
1833 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1834 
1835 	uv = (php_uv_t*)req->data;
1836 
1837 	PHP_UV_DEBUG_PRINT("after_work_cb\n");
1838 
1839 	php_uv_do_callback2(&retval, uv, NULL, 0, PHP_UV_AFTER_WORK_CB TSRMLS_CC);
1840 
1841 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_after_work_cb, uv);
1842 
1843 	/* as uv_cancel inside destruct_uv will return EBUSY here as were still in the work callback, but freeing is safe here */
1844 	clean_uv_handle(uv); /* this avoids a cancel */
1845 	OBJ_RELEASE(&uv->std);
1846 }
1847 #endif
1848 
1849 static void php_uv_fs_cb(uv_fs_t* req)
1850 {
1851 	zval params[3] = {0};
1852 	zval retval = {0};
1853 	php_uv_t *uv = (php_uv_t*)req->data;
1854 	int argc, i = 0;
1855 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
1856 
1857 	PHP_UV_DEBUG_PRINT("# php_uv_fs_cb %p\n", uv);
1858 
1859 	if (PHP_UV_IS_DTORED(uv)) {
1860 		uv_fs_req_cleanup(req);
1861 
1862 		OBJ_RELEASE(&uv->std);
1863 		return;
1864 	}
1865 
1866 	if (!Z_ISUNDEF(uv->fs_fd)) {
1867 		ZVAL_COPY_VALUE(&params[0], &uv->fs_fd);
1868 		ZVAL_UNDEF(&uv->fs_fd);
1869 	}
1870 
1871 	switch (uv->uv.fs.fs_type) {
1872 		case UV_FS_SYMLINK:
1873 		case UV_FS_LINK:
1874 		case UV_FS_CHMOD:
1875 		case UV_FS_RENAME:
1876 		case UV_FS_UNLINK:
1877 		case UV_FS_RMDIR:
1878 		case UV_FS_MKDIR:
1879 		case UV_FS_CLOSE:
1880 		case UV_FS_CHOWN:
1881 		case UV_FS_UTIME:
1882 		case UV_FS_FUTIME:
1883 			argc = 1;
1884 			ZVAL_LONG(&params[0], uv->uv.fs.result);
1885 			break;
1886 
1887 		case UV_FS_FCHMOD:
1888 		case UV_FS_FCHOWN:
1889 		case UV_FS_FTRUNCATE:
1890 		case UV_FS_FDATASYNC:
1891 		case UV_FS_FSYNC:
1892 			argc = 2;
1893 			ZVAL_LONG(&params[1], uv->uv.fs.result);
1894 			break;
1895 
1896 		case UV_FS_OPEN:
1897 			argc = 1;
1898 			if (uv->uv.fs.result < 0) {
1899 				ZVAL_LONG(&params[0], uv->uv.fs.result);
1900 			} else {
1901 				PHP_UV_FD_TO_ZVAL(&params[0], uv->uv.fs.result)
1902 				PHP_UV_DEBUG_PRINT("Creating fs handle %p\n", Z_RES(params[0]));
1903 			}
1904 			break;
1905 
1906 		case UV_FS_SCANDIR:
1907 			argc = 1;
1908 			if (uv->uv.fs.result < 0) { /* req->ptr may be NULL here, but uv_fs_scandir_next() knows to handle it */
1909 				ZVAL_LONG(&params[0], uv->uv.fs.result);
1910 			} else {
1911 				uv_dirent_t dent;
1912 
1913 				array_init(&params[0]);
1914 				while (UV_EOF != uv_fs_scandir_next(req, &dent)) {
1915 					add_next_index_string(&params[0], dent.name);
1916 				}
1917 			}
1918 			break;
1919 
1920 		case UV_FS_LSTAT:
1921 		case UV_FS_STAT:
1922 			argc = 1;
1923 			if (req->ptr != NULL) {
1924 				params[0] = php_uv_make_stat((const uv_stat_t *) req->ptr);
1925 			} else {
1926 				ZVAL_LONG(&params[0], uv->uv.fs.result);
1927 			}
1928 			break;
1929 		case UV_FS_FSTAT:
1930 			argc = 2;
1931 			if (req->ptr != NULL) {
1932 				params[1] = php_uv_make_stat((const uv_stat_t *) req->ptr);
1933 			} else {
1934 				ZVAL_LONG(&params[1], uv->uv.fs.result);
1935 			}
1936 			break;
1937 
1938 		case UV_FS_READLINK:
1939 			argc = 1;
1940 			if (uv->uv.fs.result == 0) {
1941 				ZVAL_STRING(&params[0], req->ptr);
1942 			} else {
1943 				ZVAL_LONG(&params[0], uv->uv.fs.result);
1944 			}
1945 			break;
1946 
1947 		case UV_FS_READ:
1948 			argc = 2;
1949 			if (uv->uv.fs.result >= 0) {
1950 				ZVAL_STRINGL(&params[1], uv->buffer, uv->uv.fs.result);
1951 			} else {
1952 				ZVAL_LONG(&params[1], uv->uv.fs.result);
1953 			}
1954 			efree(uv->buffer);
1955 			break;
1956 
1957 		case UV_FS_SENDFILE:
1958 			argc = 2;
1959 			ZVAL_LONG(&params[1], uv->uv.fs.result);
1960 			break;
1961 
1962 		case UV_FS_WRITE:
1963 			argc = 2;
1964 			ZVAL_LONG(&params[1], uv->uv.fs.result);
1965 			efree(uv->buffer);
1966 			break;
1967 
1968 		case UV_FS_UNKNOWN:
1969 		case UV_FS_CUSTOM:
1970 		default:
1971 			argc = 0;
1972 			php_error_docref(NULL, E_ERROR, "type; %d does not support yet.", uv->uv.fs.fs_type);
1973 			break;
1974 	}
1975 
1976 	php_uv_do_callback2(&retval, uv, params, argc, PHP_UV_FS_CB TSRMLS_CC);
1977 
1978 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_fs_cb, uv);
1979 	for (i = 0; i < argc; i++) {
1980 		zval_ptr_dtor(&params[i]);
1981 	}
1982 
1983 	zval_ptr_dtor(&retval);
1984 
1985 	if (!Z_ISUNDEF(uv->fs_fd_alt)) {
1986 		zval_ptr_dtor(&uv->fs_fd_alt);
1987 		ZVAL_UNDEF(&uv->fs_fd_alt);
1988 	}
1989 
1990 	uv_fs_req_cleanup(req);
1991 
1992 	clean_uv_handle(uv);
1993 	OBJ_RELEASE(&uv->std);
1994 }
1995 
1996 static void php_uv_fs_event_cb(uv_fs_event_t* req, const char* filename, int events, int status)
1997 {
1998 	zval params[4] = {0};
1999 	zval retval = {0};
2000 	php_uv_t *uv = (php_uv_t*)req->data;
2001 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2002 
2003 	PHP_UV_DEBUG_PRINT("fs_event_cb: %s, %d\n", filename, status);
2004 
2005 	ZVAL_OBJ(&params[0], &uv->std);
2006 	GC_ADDREF(&uv->std);
2007 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_fs_event_cb, uv);
2008 	if (filename) {
2009 		ZVAL_STRING(&params[1], filename);
2010 	} else {
2011 		ZVAL_NULL(&params[1]);
2012 	}
2013 	ZVAL_LONG(&params[2], events);
2014 	ZVAL_LONG(&params[3], status);
2015 
2016 	php_uv_do_callback2(&retval, uv, params, 4, PHP_UV_FS_EVENT_CB TSRMLS_CC);
2017 
2018 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_fs_event_cb, uv);
2019 	zval_ptr_dtor(&params[0]);
2020 	zval_ptr_dtor(&params[1]);
2021 	zval_ptr_dtor(&params[2]);
2022 	zval_ptr_dtor(&params[3]);
2023 
2024 	zval_ptr_dtor(&retval);
2025 }
2026 
2027 static zval php_uv_stat_to_zval(const uv_stat_t *stat)
2028 {
2029 	zval result = {0};
2030 	array_init(&result);
2031 
2032 	add_assoc_long_ex(&result, ZEND_STRL("dev"), stat->st_dev);
2033 	add_assoc_long_ex(&result, ZEND_STRL("ino"), stat->st_ino);
2034 	add_assoc_long_ex(&result, ZEND_STRL("mode"), stat->st_mode);
2035 	add_assoc_long_ex(&result, ZEND_STRL("nlink"), stat->st_nlink);
2036 	add_assoc_long_ex(&result, ZEND_STRL("uid"), stat->st_uid);
2037 	add_assoc_long_ex(&result, ZEND_STRL("gid"), stat->st_gid);
2038 	add_assoc_long_ex(&result, ZEND_STRL("rdev"), stat->st_rdev);
2039 	add_assoc_long_ex(&result, ZEND_STRL("size"), stat->st_size);
2040 
2041 #ifndef PHP_WIN32
2042 	add_assoc_long_ex(&result, ZEND_STRL("blksize"), stat->st_blksize);
2043 	add_assoc_long_ex(&result, ZEND_STRL("blocks"), stat->st_blocks);
2044 #endif
2045 
2046 	add_assoc_long_ex(&result, ZEND_STRL("atime"), stat->st_atim.tv_sec);
2047 	add_assoc_long_ex(&result, ZEND_STRL("mtime"), stat->st_mtim.tv_sec);
2048 	add_assoc_long_ex(&result, ZEND_STRL("ctime"), stat->st_ctim.tv_sec);
2049 
2050 	return result;
2051 }
2052 
2053 static void php_uv_fs_poll_cb(uv_fs_poll_t* handle, int status, const uv_stat_t* prev, const uv_stat_t* curr)
2054 {
2055 	zval params[4] = {0};
2056 	zval retval = {0};
2057 	php_uv_t *uv = (php_uv_t*)handle->data;
2058 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2059 
2060 	ZVAL_OBJ(&params[0], &uv->std);
2061 	GC_ADDREF(&uv->std);
2062 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_fs_poll_cb, uv);
2063 	ZVAL_LONG(&params[1], status);
2064 	params[2] = php_uv_stat_to_zval(prev);
2065 	params[3] = php_uv_stat_to_zval(curr);
2066 
2067 	php_uv_do_callback2(&retval, uv, params, 4, PHP_UV_FS_POLL_CB TSRMLS_CC);
2068 
2069 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_fs_poll_cb, uv);
2070 	zval_ptr_dtor(&params[0]);
2071 	zval_ptr_dtor(&params[1]);
2072 	zval_ptr_dtor(&params[2]);
2073 	zval_ptr_dtor(&params[3]);
2074 
2075 	zval_ptr_dtor(&retval);
2076 }
2077 
2078 static void php_uv_poll_cb(uv_poll_t* handle, int status, int events)
2079 {
2080 	zval params[4] = {0};
2081 	zval retval = {0};
2082 	php_uv_t *uv = (php_uv_t*)handle->data;
2083 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2084 
2085 	ZVAL_OBJ(&params[0], &uv->std);
2086 	if (status == 0) {
2087 		GC_ADDREF(&uv->std);
2088 	}
2089 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_poll_cb, uv);
2090 	ZVAL_LONG(&params[1], status);
2091 	ZVAL_LONG(&params[2], events);
2092 	if (!Z_ISUNDEF(uv->fs_fd)) {
2093 		ZVAL_COPY(&params[3], &uv->fs_fd);
2094 	} else {
2095 		PHP_UV_FD_TO_ZVAL(&params[3], uv->sock);
2096 	}
2097 
2098 	php_uv_do_callback2(&retval, uv, params, 4, PHP_UV_POLL_CB TSRMLS_CC);
2099 
2100 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_poll_cb, uv);
2101 	zval_ptr_dtor(&params[0]);
2102 	zval_ptr_dtor(&params[1]);
2103 	zval_ptr_dtor(&params[2]);
2104 	zval_ptr_dtor(&params[3]);
2105 
2106 	zval_ptr_dtor(&retval);
2107 }
2108 
2109 
2110 static void php_uv_udp_recv_cb(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags)
2111 {
2112 	/* TODO: is this correctly implmented? */
2113 	zval retval = {0};
2114 	zval params[3] = {0};
2115 	php_uv_t *uv = (php_uv_t*)handle->data;
2116 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2117 
2118 	ZVAL_OBJ(&params[0], &uv->std);
2119 	GC_ADDREF(&uv->std);
2120 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_udp_recv_cb, uv);
2121 	if (nread < 0) {
2122 		ZVAL_LONG(&params[1], nread);
2123 	} else {
2124 		ZVAL_STRINGL(&params[1], buf->base, nread);
2125 	}
2126 	ZVAL_LONG(&params[2], flags);
2127 
2128 	php_uv_do_callback2(&retval, uv, params, 3, PHP_UV_RECV_CB TSRMLS_CC);
2129 
2130 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_udp_recv_cb, uv);
2131 	zval_ptr_dtor(&params[0]);
2132 	zval_ptr_dtor(&params[1]);
2133 	zval_ptr_dtor(&params[2]);
2134 
2135 	zval_ptr_dtor(&retval);
2136 
2137 	if (buf->base) {
2138 		efree(buf->base);
2139 	}
2140 }
2141 
2142 static void php_uv_read_alloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf)
2143 {
2144 	buf->base = emalloc(suggested_size);
2145 	buf->len = suggested_size;
2146 }
2147 
2148 static void php_uv_close_cb(uv_handle_t *handle)
2149 {
2150 	zval retval = {0};
2151 	zval params[1] = {0};
2152 
2153 	php_uv_t *uv = (php_uv_t *) handle->data;
2154 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2155 
2156 	if (uv->callback[PHP_UV_CLOSE_CB]) {
2157 		ZVAL_OBJ(&params[0], &uv->std);
2158 
2159 		php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_CLOSE_CB TSRMLS_CC);
2160 		zval_ptr_dtor(&retval);
2161 	}
2162 
2163 	/* manually clean the uv handle as dtor will not be called anymore here */
2164 	clean_uv_handle(uv);
2165 
2166 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(uv_close_cb, uv);
2167 	OBJ_RELEASE(&uv->std);
2168 }
2169 
2170 static inline zend_bool php_uv_is_handle_referenced(php_uv_t *uv) {
2171 	zend_class_entry *ce = uv->std.ce;
2172 
2173 	return (ce == uv_signal_ce || ce == uv_timer_ce || ce == uv_idle_ce || ce == uv_udp_ce || ce == uv_tcp_ce || ce == uv_tty_ce || ce == uv_pipe_ce || ce == uv_prepare_ce || ce == uv_check_ce || ce == uv_poll_ce || ce == uv_fs_poll_ce) && uv_is_active(&uv->uv.handle);
2174 }
2175 
2176 /* uv handle must not be cleaned or closed before called */
2177 static void php_uv_close(php_uv_t *uv) {
2178 	ZEND_ASSERT(!uv_is_closing(&uv->uv.handle));
2179 
2180 	if (!php_uv_is_handle_referenced(uv)) {
2181 		GC_ADDREF(&uv->std);
2182 		PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(php_uv_close, uv);
2183 	}
2184 
2185 	uv_close(&uv->uv.handle, php_uv_close_cb);
2186 
2187 	PHP_UV_SKIP_DTOR(uv);
2188 }
2189 
2190 static void php_uv_idle_cb(uv_timer_t *handle)
2191 {
2192 	zval retval = {0};
2193 	zval params[1] = {0};
2194 
2195 	php_uv_t *uv = (php_uv_t*)handle->data;
2196 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2197 
2198 	ZVAL_OBJ(&params[0], &uv->std);
2199 	GC_ADDREF(&uv->std);
2200 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(php_uv_idle_cb, uv);
2201 
2202 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_IDLE_CB TSRMLS_CC);
2203 
2204 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(php_uv_idle_cb, uv);
2205 	zval_ptr_dtor(&params[0]);
2206 
2207 	zval_ptr_dtor(&retval);
2208 }
2209 
2210 static void php_uv_getaddrinfo_cb(uv_getaddrinfo_t* handle, int status, struct addrinfo* res)
2211 {
2212 	zval retval = {0};
2213 	zval params[1] = {0};
2214 	struct addrinfo *address;
2215 	char ip[INET6_ADDRSTRLEN];
2216 	const char *addr;
2217 
2218 	php_uv_t *uv = (php_uv_t*)handle->data;
2219 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2220 
2221 	if (status != 0) {
2222 		ZVAL_LONG(&params[0], status);
2223 	} else {
2224 		array_init(&params[0]);
2225 
2226 		address = res;
2227 		while (address) {
2228 			if (address->ai_family == AF_INET) {
2229 				addr = (char*) &((struct sockaddr_in*) address->ai_addr)->sin_addr;
2230 				uv_inet_ntop(address->ai_family, addr, ip, INET6_ADDRSTRLEN);
2231 				add_next_index_string(&params[0], ip);
2232 			}
2233 
2234 			address = address->ai_next;
2235 		}
2236 
2237 		address = res;
2238 		while (address) {
2239 			if (address->ai_family == AF_INET6) {
2240 				addr = (char*) &((struct sockaddr_in6*) address->ai_addr)->sin6_addr;
2241 				uv_inet_ntop(address->ai_family, addr, ip, INET6_ADDRSTRLEN);
2242 				add_next_index_string(&params[0], ip);
2243 			}
2244 
2245 			address = address->ai_next;
2246 		}
2247 	}
2248 
2249 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_GETADDR_CB TSRMLS_CC);
2250 
2251 	zval_ptr_dtor(&retval);
2252 	zval_ptr_dtor(&params[0]);
2253 
2254 	uv_freeaddrinfo(res);
2255 	clean_uv_handle(uv);
2256 	OBJ_RELEASE(&uv->std);
2257 }
2258 
2259 static void php_uv_timer_cb(uv_timer_t *handle)
2260 {
2261 	zval retval = {0};
2262 	zval params[1] = {0};
2263 	php_uv_t *uv = (php_uv_t*)handle->data;
2264 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2265 
2266 	ZVAL_OBJ(&params[0], &uv->std);
2267 
2268 	if (handle->repeat) {
2269 		GC_ADDREF(&uv->std);
2270 		PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(php_uv_timer_cb, uv);
2271 	}
2272 
2273 	php_uv_do_callback2(&retval, uv, params, 1, PHP_UV_TIMER_CB TSRMLS_CC);
2274 
2275 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(php_uv_timer_cb, uv);
2276 	zval_ptr_dtor(&params[0]);
2277 
2278 	zval_ptr_dtor(&retval);
2279 }
2280 
2281 static void php_uv_signal_cb(uv_signal_t *handle, int sig_num)
2282 {
2283 	zval retval = {0};
2284 	zval params[2] = {0};
2285 	php_uv_t *uv = (php_uv_t*)handle->data;
2286 	TSRMLS_FETCH_FROM_CTX(uv->thread_ctx);
2287 
2288 	ZVAL_OBJ(&params[0], &uv->std);
2289 	GC_ADDREF(&uv->std);
2290 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(php_uv_signal_cb, uv);
2291 	ZVAL_LONG(&params[1], sig_num);
2292 
2293 	php_uv_do_callback2(&retval, uv, params, 2, PHP_UV_SIGNAL_CB TSRMLS_CC);
2294 
2295 	PHP_UV_DEBUG_OBJ_DEL_REFCOUNT(php_uv_signal_cb, uv);
2296 	zval_ptr_dtor(&params[0]);
2297 	zval_ptr_dtor(&params[1]);
2298 
2299 	zval_ptr_dtor(&retval);
2300 }
2301 
2302 void static destruct_uv_stdio(zend_object *obj)
2303 {
2304 	php_uv_stdio_t *stdio = (php_uv_stdio_t *) obj;
2305 
2306 	zval_ptr_dtor(&stdio->stream);
2307 }
2308 
2309 
2310 /* common functions */
2311 
2312 static void php_uv_ip_common(int ip_type, INTERNAL_FUNCTION_PARAMETERS)
2313 {
2314 	php_uv_sockaddr_t *addr;
2315 	char ip[INET6_ADDRSTRLEN];
2316 
2317 	ZEND_PARSE_PARAMETERS_START(1, 1)
2318 		UV_PARAM_OBJ(addr, php_uv_sockaddr_t, (ip_type == 1) ? uv_sockaddr_ipv4_ce : uv_sockaddr_ipv6_ce)
2319 	ZEND_PARSE_PARAMETERS_END();
2320 
2321 	if (ip_type == 1) {
2322 		uv_ip4_name(PHP_UV_SOCKADDR_IPV4_P(addr), ip, INET6_ADDRSTRLEN);
2323 	} else {
2324 		uv_ip6_name(PHP_UV_SOCKADDR_IPV6_P(addr), ip, INET6_ADDRSTRLEN);
2325 	}
2326 	RETVAL_STRING(ip);
2327 }
2328 
2329 static void php_uv_socket_bind(enum php_uv_socket_type ip_type, INTERNAL_FUNCTION_PARAMETERS)
2330 {
2331 	php_uv_sockaddr_t *addr;
2332 	php_uv_t *uv;
2333 	zend_long flags = 0;
2334 	int r;
2335 
2336 	if (ip_type & PHP_UV_UDP) {
2337 		ZEND_PARSE_PARAMETERS_START(2, 3)
2338 			UV_PARAM_OBJ(uv, php_uv_t, uv_udp_ce)
2339 			UV_PARAM_OBJ(addr, php_uv_sockaddr_t, (ip_type == PHP_UV_UDP_IPV4) ? uv_sockaddr_ipv4_ce : uv_sockaddr_ipv6_ce)
2340 			Z_PARAM_OPTIONAL
2341 			Z_PARAM_LONG(flags)
2342 		ZEND_PARSE_PARAMETERS_END();
2343 	} else {
2344 		ZEND_PARSE_PARAMETERS_START(2, 2)
2345 			UV_PARAM_OBJ(uv, php_uv_t, uv_tcp_ce)
2346 			UV_PARAM_OBJ(addr, php_uv_sockaddr_t, (ip_type == PHP_UV_TCP_IPV4) ? uv_sockaddr_ipv4_ce : uv_sockaddr_ipv6_ce)
2347 		ZEND_PARSE_PARAMETERS_END();
2348 	}
2349 
2350 	switch (ip_type) {
2351 		case PHP_UV_TCP_IPV4:
2352 			r = uv_tcp_bind((uv_tcp_t*)&uv->uv.tcp, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV4(addr), 0);
2353 			break;
2354 		case PHP_UV_TCP_IPV6:
2355 			r = uv_tcp_bind((uv_tcp_t*)&uv->uv.tcp, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV6(addr), 0);
2356 			break;
2357 		case PHP_UV_UDP_IPV4:
2358 			r = uv_udp_bind((uv_udp_t*)&uv->uv.udp, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV4(addr), flags);
2359 			break;
2360 		case PHP_UV_UDP_IPV6:
2361 			r = uv_udp_bind((uv_udp_t*)&uv->uv.udp, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV6(addr), flags);
2362 			break;
2363 		default:
2364 			php_error_docref(NULL, E_ERROR, "unhandled type");
2365 			return;
2366 	}
2367 
2368 	if (r) {
2369 		php_error_docref(NULL, E_WARNING, "bind failed");
2370 		RETURN_FALSE;
2371 	} else {
2372 		RETURN_TRUE;
2373 	}
2374 }
2375 
2376 static void php_uv_socket_getname(int type, INTERNAL_FUNCTION_PARAMETERS)
2377 {
2378 	php_uv_t *uv;
2379 	zval result;
2380 	int addr_len;
2381 	struct sockaddr_storage addr;
2382 	addr_len = sizeof(struct sockaddr_storage);
2383 
2384 	ZEND_PARSE_PARAMETERS_START(1, 1)
2385 		UV_PARAM_OBJ(uv, php_uv_t, type == 3 ? uv_udp_ce : uv_tcp_ce)
2386 	ZEND_PARSE_PARAMETERS_END();
2387 
2388 	switch (type) {
2389 		case 1:
2390 			uv_tcp_getsockname(&uv->uv.tcp, (struct sockaddr*)&addr, &addr_len);
2391 			break;
2392 		case 2:
2393 			uv_tcp_getpeername(&uv->uv.tcp, (struct sockaddr*)&addr, &addr_len);
2394 			break;
2395 		case 3:
2396 			uv_udp_getsockname(&uv->uv.udp, (struct sockaddr*)&addr, &addr_len);
2397 			break;
2398 		default:
2399 			php_error_docref(NULL, E_ERROR, "unexpected type");
2400 		break;
2401 	}
2402 
2403 	result = php_uv_address_to_zval((struct sockaddr*)&addr);
2404 	RETURN_ZVAL(&result, 0, 1);
2405 }
2406 
2407 static void php_uv_handle_open(int (*open_cb)(uv_handle_t *, long), zend_class_entry *ce, INTERNAL_FUNCTION_PARAMETERS) {
2408 	php_uv_t *uv;
2409 	zval *zstream;
2410 	zend_long fd; // file handle
2411 	int error;
2412 
2413 	ZEND_PARSE_PARAMETERS_START(2, 2)
2414 		UV_PARAM_OBJ(uv, php_uv_t, ce)
2415 		Z_PARAM_ZVAL(zstream)
2416 	ZEND_PARSE_PARAMETERS_END();
2417 
2418 	fd = php_uv_zval_to_fd(zstream);
2419 	if (fd < 0) {
2420 		php_error_docref(NULL, E_WARNING, "file descriptor must be unsigned value or a valid resource");
2421 		RETURN_FALSE;
2422 	}
2423 
2424 	error = open_cb(&uv->uv.handle, fd);
2425 
2426 	if (error) {
2427 		php_error_docref(NULL, E_WARNING, "%s", php_uv_strerror(error));
2428 	}
2429 
2430 	RETURN_LONG(error);
2431 }
2432 
2433 static void php_uv_udp_send(int type, INTERNAL_FUNCTION_PARAMETERS)
2434 {
2435 	zend_string *data;
2436 	php_uv_t *uv;
2437 	send_req_t *w;
2438 	php_uv_sockaddr_t *addr;
2439 	zend_fcall_info fci       = empty_fcall_info;
2440 	zend_fcall_info_cache fcc = empty_fcall_info_cache;
2441 	php_uv_cb_t *cb;
2442 
2443 	ZEND_PARSE_PARAMETERS_START(3, 4)
2444 		UV_PARAM_OBJ(uv, php_uv_t, uv_udp_ce)
2445 		Z_PARAM_STR(data)
2446 		UV_PARAM_OBJ(addr, php_uv_sockaddr_t, (type == 1) ? uv_sockaddr_ipv4_ce : uv_sockaddr_ipv6_ce, uv_sockaddr_ipv6_ce)
2447 		Z_PARAM_OPTIONAL
2448 		Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
2449 	ZEND_PARSE_PARAMETERS_END();
2450 
2451 	GC_ADDREF(&uv->std);
2452 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_udp_send, uv);
2453 
2454 	PHP_UV_INIT_SEND_REQ(w, uv, data->val, data->len);
2455 	php_uv_cb_init(&cb, uv, &fci, &fcc, PHP_UV_SEND_CB);
2456 
2457 	if (addr->std.ce == uv_sockaddr_ipv4_ce) {
2458 		uv_udp_send(&w->req, &uv->uv.udp, &w->buf, 1, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV4(addr), php_uv_udp_send_cb);
2459 	} else {
2460 		uv_udp_send(&w->req, &uv->uv.udp, &w->buf, 1, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV6(addr), php_uv_udp_send_cb);
2461 	}
2462 }
2463 
2464 static void php_uv_tcp_connect(enum php_uv_socket_type type, INTERNAL_FUNCTION_PARAMETERS)
2465 {
2466 	php_uv_t *uv;
2467 	php_uv_sockaddr_t *addr;
2468 	uv_connect_t *req;
2469 	zend_fcall_info fci       = empty_fcall_info;
2470 	zend_fcall_info_cache fcc = empty_fcall_info_cache;
2471 	php_uv_cb_t *cb;
2472 
2473 	ZEND_PARSE_PARAMETERS_START(2, 3)
2474 		UV_PARAM_OBJ(uv, php_uv_t, uv_tcp_ce)
2475 		UV_PARAM_OBJ(addr, php_uv_sockaddr_t, (type == PHP_UV_TCP_IPV4) ? uv_sockaddr_ipv4_ce : uv_sockaddr_ipv6_ce, uv_sockaddr_ipv6_ce)
2476 		Z_PARAM_OPTIONAL
2477 		Z_PARAM_FUNC_EX(fci, fcc, 1, 0)
2478 	ZEND_PARSE_PARAMETERS_END();
2479 
2480 	GC_ADDREF(&uv->std);
2481 	PHP_UV_DEBUG_OBJ_ADD_REFCOUNT(uv_tcp_connect, uv);
2482 
2483 	PHP_UV_INIT_CONNECT(req, uv)
2484 	php_uv_cb_init(&cb, uv, &fci, &fcc, PHP_UV_CONNECT_CB);
2485 
2486 	if (addr->std.ce == uv_sockaddr_ipv4_ce) {
2487 		uv_tcp_connect(req, &uv->uv.tcp, (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV4(addr), php_uv_tcp_connect_cb);
2488 	} else {
2489 		uv_tcp_connect(req, &uv->uv.tcp,  (const struct sockaddr*)&PHP_UV_SOCKADDR_IPV6(addr), php_uv_tcp_connect_cb);
2490 	}
2491 }
2492 
2493 /* zend */
2494 
2495 static zend_function_entry php_uv_empty_methods[] = {
2496 	{0}
2497 };
2498 
2499 #if PHP_VERSION_ID >= 80000
2500 int php_uv_cast_object(zend_object *readobj, zval *writeobj, int type) {
2501 #else
2502 int php_uv_cast_object(zval *readobj_zv, zval *writeobj, int type) {
2503 	zend_object *readobj = Z_OBJ_P(readobj_zv);
2504 #endif
2505 	if (type == IS_LONG) {
2506 		ZVAL_LONG(writeobj, readobj->handle);
2507 		return SUCCESS;
2508 	} else {
2509 #if PHP_VERSION_ID >= 80000
2510 		return zend_std_cast_object_tostring(readobj, writeobj, type);
2511 #else
2512 		return zend_std_cast_object_tostring(readobj_zv, writeobj, type);
2513 #endif
2514 	}
2515 }
2516 
2517 #if PHP_VERSION_ID >= 80000
2518 static HashTable *php_uv_get_debug_info(zend_object *object, int *is_temp) {
2519 	php_uv_t *uv = (php_uv_t *) object;
2520 #else
2521 static HashTable *php_uv_get_debug_info(zval *object, int *is_temp) {
2522 	php_uv_t *uv = (php_uv_t *) Z_OBJ_P(object);
2523 #endif
2524 	HashTable *ht = zend_std_get_debug_info(object, is_temp);
2525 	if (uv->std.ce == uv_poll_ce) {
2526 		if (!*is_temp) {
2527 			int fd;
2528 			if (uv_fileno(&uv->uv.handle, (uv_os_fd_t *)&fd) == 0) { /* not actually a fd on windows but a handle pointr address, but okay. */
2529 				*is_temp = 1;
2530 				ht = zend_array_dup(ht);
2531 				zval fdzv;
2532 				ZVAL_LONG(&fdzv, fd);
2533 				zend_hash_update(ht, zend_string_init("@fd", sizeof("@fd")-1, 0), &fdzv);
2534 			}
2535 		}
2536 	}
2537 	return ht;
2538 }
2539 
2540 #if PHP_VERSION_ID >= 80000
2541 static HashTable *php_uv_get_gc(zend_object *object, zval **table, int *n) {
2542 	php_uv_t *uv = (php_uv_t *) object;
2543 #else
2544 static HashTable *php_uv_get_gc(zval *object, zval **table, int *n) {
2545 	php_uv_t *uv = (php_uv_t *) Z_OBJ_P(object);
2546 #endif
2547 	int i;
2548 
2549 	if (PHP_UV_IS_DTORED(uv)) {
2550 		*n = 0;
2551 		return NULL;
2552 	}
2553 
2554 	// include trailing zvals like fs_fd/_alt
2555 	*n = (sizeof(php_uv_t) -  XtOffsetOf(php_uv_t, gc_data)) / sizeof(zval);
2556 	for (i = 0; i < PHP_UV_CB_MAX; i++) {
2557 		php_uv_cb_t *cb = uv->callback[i];
2558 		if (cb) {
2559 			ZVAL_COPY_VALUE(&uv->gc_data[i * 2], &cb->fci.function_name);
2560 			if (cb->fci.object) {
2561 				ZVAL_OBJ(&uv->gc_data[i * 2 + 1], cb->fci.object);
2562 			}
2563 		} else {
2564 			ZVAL_UNDEF(&uv->gc_data[i * 2]);
2565 			ZVAL_UNDEF(&uv->gc_data[i * 2 + 1]);
2566 		}
2567 	}
2568 	*table = uv->gc_data;
2569 
2570 	return uv->std.properties;
2571 }
2572 
2573 static void php_uv_loop_get_gc_walk_cb(uv_handle_t* handle, void *arg) {
2574 	struct { int *n; php_uv_loop_t *loop; } *data = arg;
2575 	php_uv_t *uv = (php_uv_t *) handle->data;
2576 
2577 	if (php_uv_is_handle_referenced(uv)) {
2578 		php_uv_loop_t *loop = data->loop;
2579 
2580 		if (*data->n == loop->gc_buffer_size) {
2581 			if (loop->gc_buffer_size == 0) {
2582 				loop->gc_buffer_size = 16;
2583 			} else {
2584 				loop->gc_buffer_size *= 2;
2585 			}
2586 			loop->gc_buffer = erealloc(loop->gc_buffer, loop->gc_buffer_size * sizeof(zval));
2587 		}
2588 
2589 		ZVAL_OBJ(loop->gc_buffer + (*data->n)++, &uv->std);
2590 	}
2591 }
2592 
2593 
2594 #if PHP_VERSION_ID >= 80000
2595 static HashTable *php_uv_loop_get_gc(zend_object *object, zval **table, int *n) {
2596 	php_uv_loop_t *loop = (php_uv_loop_t *) object;
2597 #else
2598 static HashTable *php_uv_loop_get_gc(zval *object, zval **table, int *n) {
2599 	php_uv_loop_t *loop = (php_uv_loop_t *) Z_OBJ_P(object);
2600 #endif
2601 	struct { int *n; php_uv_loop_t *loop; } data;
2602 	data.n = n;
2603 	data.loop = loop;
2604 
2605 	*n = 0;
2606 	if (!PHP_UV_IS_DTORED(loop)) {
2607 		uv_walk(&loop->loop, php_uv_loop_get_gc_walk_cb, &data);
2608 		*table = loop->gc_buffer;
2609 	}
2610 
2611 	return loop->std.properties;
2612 }
2613 
2614 #if PHP_VERSION_ID >= 80000
2615 static HashTable *php_uv_stdio_get_gc(zend_object *object, zval **table, int *n) {
2616 	php_uv_stdio_t *stdio = (php_uv_stdio_t *) object;
2617 #else
2618 static HashTable *php_uv_stdio_get_gc(zval *object, zval **table, int *n) {
2619 	php_uv_stdio_t *stdio = (php_uv_stdio_t *) Z_OBJ_P(object);
2620 #endif
2621 
2622 	*n = 1;
2623 	*table = &stdio->stream;
2624 
2625 	return stdio->std.properties;
2626 }
2627 
2628 static zend_object *php_uv_create_uv(zend_class_entry *ce) {
2629 	php_uv_t *uv = emalloc(sizeof(php_uv_t));
2630 	zend_object_std_init(&uv->std, ce);
2631 	uv->std.handlers = &uv_handlers;
2632 
2633 	PHP_UV_INIT_ZVALS(uv);
2634 	TSRMLS_SET_CTX(uv->thread_ctx);
2635 
2636 	uv->uv.handle.data = uv;
2637 
2638 	return &uv->std;
2639 }
2640 
2641 static zend_object *php_uv_create_uv_loop(zend_class_entry *ce) {
2642 	php_uv_loop_t *loop = emalloc(sizeof(php_uv_loop_t));
2643 	zend_object_std_init(&loop->std, ce);
2644 	loop->std.handlers = &uv_loop_handlers;
2645 
2646 	uv_loop_init(&loop->loop);
2647 
2648 	loop->gc_buffer_size = 0;
2649 	loop->gc_buffer = NULL;
2650 
2651 	return &loop->std;
2652 }
2653 
2654 static zend_object *php_uv_create_uv_sockaddr(zend_class_entry *ce) {
2655 	php_uv_sockaddr_t *sockaddr = emalloc(sizeof(php_uv_sockaddr_t));
2656 	zend_object_std_init(&sockaddr->std, ce);
2657 	sockaddr->std.handlers = &uv_default_handlers;
2658 
2659 	return &sockaddr->std;
2660 }
2661 
2662 static zend_object *php_uv_create_uv_lock(zend_class_entry *ce) {
2663 	php_uv_lock_t *lock = emalloc(sizeof(php_uv_lock_t));
2664 	zend_object_std_init(&lock->std, ce);
2665 	lock->std.handlers = &uv_lock_handlers;
2666 
2667 	lock->locked = 0;
2668 
2669 	return &lock->std;
2670 }
2671 
2672 static zend_object *php_uv_create_uv_stdio(zend_class_entry *ce) {
2673 	php_uv_stdio_t *stdio = emalloc(sizeof(php_uv_stdio_t));
2674 	zend_object_std_init(&stdio->std, ce);
2675 	stdio->std.handlers = &uv_stdio_handlers;
2676 
2677 	stdio->flags = 0;
2678 	ZVAL_UNDEF(&stdio->stream);
2679 
2680 	return &stdio->std;
2681 }
2682 
2683 static zend_class_entry *php_uv_register_internal_class_ex(const char *name, zend_class_entry *parent) {
2684 	zend_class_entry ce = {0}, *new;
2685 
2686 	ce.name = zend_new_interned_string(zend_string_init(name, strlen(name), 1));
2687 	ce.info.internal.builtin_functions = php_uv_empty_methods;
2688 	new = zend_register_internal_class_ex(&ce, parent);
2689 #if PHP_VERSION_ID < 80100
2690 	new->serialize = zend_class_serialize_deny;
2691 	new->unserialize = zend_class_unserialize_deny;
2692 #endif
2693 	new->ce_flags |= ZEND_ACC_FINAL;
2694 #if PHP_VERSION_ID >= 80100
2695 	new->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE ;
2696 #endif
2697 	new->create_object = php_uv_create_uv;
2698 
2699 	return new;
2700 }
2701 
2702 static zend_class_entry *php_uv_register_internal_class(const char *name) {
2703 	return php_uv_register_internal_class_ex(name, NULL);
2704 }
2705 
2706 static zend_function *php_uv_get_ctor(zend_object *object) {
2707 	zend_throw_error(NULL, "The UV classes cannot be instantiated manually");
2708 	return NULL;
2709 }
2710 
2711 PHP_MINIT_FUNCTION(uv)
2712 {
2713 	PHP_UV_PROBE(MINIT);
2714 
2715 #if PHP_VERSION_ID >= 70300
2716 	memcpy(&uv_default_handlers, &std_object_handlers, sizeof(zend_object_handlers));
2717 #else
2718 	memcpy(&uv_default_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2719 #endif
2720 	uv_default_handlers.clone_obj = NULL;
2721 	uv_default_handlers.get_constructor = php_uv_get_ctor;
2722 	uv_default_handlers.cast_object = php_uv_cast_object;
2723 
2724 	uv_ce = php_uv_register_internal_class("UV");
2725 	uv_ce->ce_flags |= ZEND_ACC_ABSTRACT;
2726 	uv_ce->ce_flags &= ~ZEND_ACC_FINAL;
2727 	memcpy(&uv_handlers, &uv_default_handlers, sizeof(zend_object_handlers));
2728 	uv_handlers.get_gc = php_uv_get_gc;
2729 	uv_handlers.dtor_obj = destruct_uv;
2730 	uv_handlers.get_debug_info = php_uv_get_debug_info;
2731 
2732 	php_uv_init(uv_ce);
2733 
2734 	uv_stream_ce = php_uv_register_internal_class_ex("UVStream", uv_ce);
2735 	uv_stream_ce->ce_flags |= ZEND_ACC_ABSTRACT;
2736 	uv_stream_ce->ce_flags &= ~ZEND_ACC_FINAL;
2737 
2738 	uv_tcp_ce = php_uv_register_internal_class_ex("UVTcp", uv_stream_ce);
2739 	uv_udp_ce = php_uv_register_internal_class_ex("UVUdp", uv_ce);
2740 	uv_pipe_ce = php_uv_register_internal_class_ex("UVPipe", uv_stream_ce);
2741 	uv_idle_ce = php_uv_register_internal_class_ex("UVIdle", uv_ce);
2742 	uv_timer_ce = php_uv_register_internal_class_ex("UVTimer", uv_ce);
2743 	uv_async_ce = php_uv_register_internal_class_ex("UVAsync", uv_ce);
2744 	uv_addrinfo_ce = php_uv_register_internal_class_ex("UVAddrinfo", uv_ce);
2745 	uv_process_ce = php_uv_register_internal_class_ex("UVProcess", uv_ce);
2746 	uv_prepare_ce = php_uv_register_internal_class_ex("UVPrepare", uv_ce);
2747 	uv_check_ce = php_uv_register_internal_class_ex("UVCheck", uv_ce);
2748 	uv_work_ce = php_uv_register_internal_class_ex("UVWork", uv_ce);
2749 	uv_fs_ce = php_uv_register_internal_class_ex("UVFs", uv_ce);
2750 	uv_fs_event_ce = php_uv_register_internal_class_ex("UVFsEvent", uv_ce);
2751 	uv_tty_ce = php_uv_register_internal_class_ex("UVTty", uv_stream_ce);
2752 	uv_fs_poll_ce = php_uv_register_internal_class_ex("UVFsPoll", uv_ce);
2753 	uv_poll_ce = php_uv_register_internal_class_ex("UVPoll", uv_ce);
2754 	uv_signal_ce = php_uv_register_internal_class_ex("UVSignal", uv_ce);
2755 
2756 	uv_loop_ce = php_uv_register_internal_class("UVLoop");
2757 	uv_loop_ce->create_object = php_uv_create_uv_loop;
2758 	memcpy(&uv_loop_handlers, &uv_default_handlers, sizeof(zend_object_handlers));
2759 	uv_loop_handlers.get_gc = php_uv_loop_get_gc;
2760 	uv_loop_handlers.dtor_obj = destruct_uv_loop;
2761 	uv_loop_handlers.free_obj = free_uv_loop;
2762 
2763 	uv_sockaddr_ce = php_uv_register_internal_class("UVSockAddr");
2764 	uv_sockaddr_ce->ce_flags |= ZEND_ACC_ABSTRACT;
2765 	uv_sockaddr_ce->ce_flags &= ~ZEND_ACC_FINAL;
2766 	uv_sockaddr_ce->create_object = php_uv_create_uv_sockaddr;
2767 
2768 	uv_sockaddr_ipv4_ce = php_uv_register_internal_class_ex("UVSockAddrIPv4", uv_sockaddr_ce);
2769 	uv_sockaddr_ipv4_ce->create_object = php_uv_create_uv_sockaddr;
2770 
2771 	uv_sockaddr_ipv6_ce = php_uv_register_internal_class_ex("UVSockAddrIPv6", uv_sockaddr_ce);
2772 	uv_sockaddr_ipv6_ce->create_object = php_uv_create_uv_sockaddr;
2773 
2774 	uv_lock_ce = php_uv_register_internal_class("UVLock");
2775 	uv_lock_ce->create_object = php_uv_create_uv_lock;
2776 	memcpy(&uv_lock_handlers, &uv_default_handlers, sizeof(zend_object_handlers));
2777 	uv_lock_handlers.dtor_obj = destruct_uv_lock;
2778 
2779 	uv_stdio_ce = php_uv_register_internal_class("UVStdio");
2780 	uv_stdio_ce->create_object = php_uv_create_uv_stdio;
2781 	memcpy(&uv_stdio_handlers, &uv_default_handlers, sizeof(zend_object_handlers));
2782 	uv_stdio_handlers.dtor_obj = destruct_uv_stdio;
2783 	uv_stdio_handlers.get_gc = php_uv_stdio_get_gc;
2784 
2785 #if !defined(PHP_WIN32) && !(defined(HAVE_SOCKETS) && !defined(COMPILE_DL_SOCKETS))
2786 	{
2787 		zend_module_entry *sockets;
2788 		if ((sockets = zend_hash_str_find_ptr(&module_registry, ZEND_STRL("sockets")))) {
2789 			if (sockets->handle) { // shared
2790 # if PHP_VERSION_ID >= 80000
2791 				zend_class_entry **socket_ce_ptr = (zend_class_entry **) DL_FETCH_SYMBOL(sockets->handle, "socket_ce");
2792 				if (socket_ce_ptr == NULL) {
2793 					socket_ce_ptr = (zend_class_entry **) DL_FETCH_SYMBOL(sockets->handle, "_socket_ce");
2794 				}
2795 				socket_ce = *socket_ce_ptr;
2796 # else
2797 				php_sockets_le_socket_ptr = (int (*)(void)) DL_FETCH_SYMBOL(sockets->handle, "php_sockets_le_socket");
2798 				if (php_sockets_le_socket_ptr == NULL) {
2799 					php_sockets_le_socket_ptr = (int (*)(void)) DL_FETCH_SYMBOL(sockets->handle, "_php_sockets_le_socket");
2800 				}
2801 			} else {
2802 				php_sockets_le_socket_ptr = &php_sockets_le_socket;
2803 #endif
2804 			}
2805 		}
2806 	}
2807 #endif
2808 
2809 	return SUCCESS;
2810 }
2811 
2812 PHP_RSHUTDOWN_FUNCTION(uv)
2813 {
2814 	if (UV_G(default_loop)) {
2815 		uv_loop_t *loop = &UV_G(default_loop)->loop;
2816 
2817 		/* for proper destruction: close all handles, let libuv call close callback and then close and free the loop */
2818 		uv_stop(loop); /* in case we longjmp()'ed ... */
2819 		uv_run(loop, UV_RUN_DEFAULT); /* invalidate the stop ;-) */
2820 
2821 		uv_walk(loop, destruct_uv_loop_walk_cb, NULL);
2822 		uv_run(loop, UV_RUN_DEFAULT);
2823 		uv_loop_close(loop);
2824 		OBJ_RELEASE(&UV_G(default_loop)->std);
2825 	}
2826 
2827 	return SUCCESS;
2828 }
2829 
2830 ZEND_BEGIN_ARG_INFO_EX(arginfo_void, 0, 0, 0)
2831 ZEND_END_ARG_INFO()
2832 
2833 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_run, 0, 0, 0)
2834 	ZEND_ARG_INFO(0, loop)
2835 	ZEND_ARG_INFO(0, run_mode)
2836 ZEND_END_ARG_INFO()
2837 
2838 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_stop, 0, 0, 0)
2839 	ZEND_ARG_INFO(0, loop)
2840 ZEND_END_ARG_INFO()
2841 
2842 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_loop_delete, 0, 0, 1)
2843 	ZEND_ARG_INFO(0, loop)
2844 ZEND_END_ARG_INFO()
2845 
2846 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_now, 0, 0, 0)
2847 	ZEND_ARG_INFO(0, loop)
2848 ZEND_END_ARG_INFO()
2849 
2850 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_connect, 0, 0, 2)
2851 	ZEND_ARG_INFO(0, resource)
2852 	ZEND_ARG_INFO(0, sock_addr)
2853 	ZEND_ARG_INFO(0, callback)
2854 ZEND_END_ARG_INFO()
2855 
2856 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_connect6, 0, 0, 2)
2857 	ZEND_ARG_INFO(0, resource)
2858 	ZEND_ARG_INFO(0, ipv6_addr)
2859 	ZEND_ARG_INFO(0, callback)
2860 ZEND_END_ARG_INFO()
2861 
2862 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_init, 0, 0, 0)
2863 	ZEND_ARG_INFO(0, loop)
2864 ZEND_END_ARG_INFO()
2865 
2866 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_listen, 0, 0, 3)
2867 	ZEND_ARG_INFO(0, resource)
2868 	ZEND_ARG_INFO(0, backlog)
2869 	ZEND_ARG_INFO(0, callback)
2870 ZEND_END_ARG_INFO()
2871 
2872 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_accept, 0, 0, 2)
2873 	ZEND_ARG_INFO(0, server)
2874 	ZEND_ARG_INFO(0, client)
2875 ZEND_END_ARG_INFO()
2876 
2877 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_read_start, 0, 0, 2)
2878 	ZEND_ARG_INFO(0, server)
2879 	ZEND_ARG_INFO(0, callback)
2880 ZEND_END_ARG_INFO()
2881 
2882 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_read_stop, 0, 0, 1)
2883 	ZEND_ARG_INFO(0, server)
2884 ZEND_END_ARG_INFO()
2885 
2886 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_write, 0, 0, 3)
2887 	ZEND_ARG_INFO(0, client)
2888 	ZEND_ARG_INFO(0, data)
2889 	ZEND_ARG_INFO(0, callback)
2890 ZEND_END_ARG_INFO()
2891 
2892 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_write2, 0, 0, 4)
2893 	ZEND_ARG_INFO(0, client)
2894 	ZEND_ARG_INFO(0, data)
2895 	ZEND_ARG_INFO(0, send)
2896 	ZEND_ARG_INFO(0, callback)
2897 ZEND_END_ARG_INFO()
2898 
2899 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_strerror, 0, 0, 1)
2900 	ZEND_ARG_INFO(0, error)
2901 ZEND_END_ARG_INFO()
2902 
2903 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_err_name, 0, 0, 1)
2904 	ZEND_ARG_INFO(0, error)
2905 ZEND_END_ARG_INFO()
2906 
2907 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_init, 0, 0, 0)
2908 	ZEND_ARG_INFO(0, loop)
2909 ZEND_END_ARG_INFO()
2910 
2911 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_stop, 0, 0, 1)
2912 	ZEND_ARG_INFO(0, idle)
2913 ZEND_END_ARG_INFO()
2914 
2915 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_start, 0, 0, 3)
2916 	ZEND_ARG_INFO(0, timer)
2917 	ZEND_ARG_INFO(0, timeout)
2918 	ZEND_ARG_INFO(0, repeat)
2919 	ZEND_ARG_INFO(0, callback)
2920 ZEND_END_ARG_INFO()
2921 
2922 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_stop, 0, 0, 1)
2923 	ZEND_ARG_INFO(0, timer)
2924 ZEND_END_ARG_INFO()
2925 
2926 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_again, 0, 0, 1)
2927 	ZEND_ARG_INFO(0, timer)
2928 ZEND_END_ARG_INFO()
2929 
2930 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_set_repeat, 0, 0, 2)
2931 	ZEND_ARG_INFO(0, timer)
2932 	ZEND_ARG_INFO(0, timeout)
2933 ZEND_END_ARG_INFO()
2934 
2935 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_timer_get_repeat, 0, 0, 1)
2936 	ZEND_ARG_INFO(0, timer)
2937 ZEND_END_ARG_INFO()
2938 
2939 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_start, 0, 0, 2)
2940 	ZEND_ARG_INFO(0, timer)
2941 	ZEND_ARG_INFO(0, callback)
2942 ZEND_END_ARG_INFO()
2943 
2944 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_open, 0, 0, 2)
2945 	ZEND_ARG_INFO(0, resource)
2946 	ZEND_ARG_INFO(0, tcpfd)
2947 ZEND_END_ARG_INFO()
2948 
2949 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_bind, 0, 0, 2)
2950 	ZEND_ARG_INFO(0, resource)
2951 	ZEND_ARG_INFO(0, address)
2952 ZEND_END_ARG_INFO()
2953 
2954 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_bind6, 0, 0, 2)
2955 	ZEND_ARG_INFO(0, resource)
2956 	ZEND_ARG_INFO(0, address)
2957 ZEND_END_ARG_INFO()
2958 
2959 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_shutdown, 0, 0, 2)
2960 	ZEND_ARG_INFO(0, stream)
2961 	ZEND_ARG_INFO(0, callback)
2962 ZEND_END_ARG_INFO()
2963 
2964 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_close, 0, 0, 1)
2965 	ZEND_ARG_INFO(0, stream)
2966 	ZEND_ARG_INFO(0, callback)
2967 ZEND_END_ARG_INFO()
2968 
2969 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_idle_init, 0, 0, 0)
2970 	ZEND_ARG_INFO(0, loop)
2971 ZEND_END_ARG_INFO()
2972 
2973 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_update_time, 0, 0, 0)
2974 	ZEND_ARG_INFO(0, loop)
2975 ZEND_END_ARG_INFO()
2976 
2977 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_is_active, 0, 0, 1)
2978 	ZEND_ARG_INFO(0, handle)
2979 ZEND_END_ARG_INFO()
2980 
2981 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_is_closing, 0, 0, 1)
2982 	ZEND_ARG_INFO(0, handle)
2983 ZEND_END_ARG_INFO()
2984 
2985 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_is_readable, 0, 0, 1)
2986 	ZEND_ARG_INFO(0, handle)
2987 ZEND_END_ARG_INFO()
2988 
2989 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_is_writable, 0, 0, 1)
2990 	ZEND_ARG_INFO(0, handle)
2991 ZEND_END_ARG_INFO()
2992 
2993 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_walk, 0, 0, 2)
2994 	ZEND_ARG_INFO(0, loop)
2995 	ZEND_ARG_INFO(0, callback)
2996 	ZEND_ARG_INFO(0, opaque)
2997 ZEND_END_ARG_INFO()
2998 
2999 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_guess_handle, 0, 0, 1)
3000 	ZEND_ARG_INFO(0, fd)
3001 ZEND_END_ARG_INFO()
3002 
3003 
3004 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_ref, 0, 0, 1)
3005 	ZEND_ARG_INFO(0, loop)
3006 ZEND_END_ARG_INFO()
3007 
3008 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_unref, 0, 0, 1)
3009 	ZEND_ARG_INFO(0, loop)
3010 ZEND_END_ARG_INFO()
3011 
3012 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_tcp_nodelay, 0, 0, 2)
3013 	ZEND_ARG_INFO(0, tcp)
3014 	ZEND_ARG_INFO(0, enabled)
3015 ZEND_END_ARG_INFO()
3016 
3017 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_ip4_addr, 0, 0, 2)
3018 	ZEND_ARG_INFO(0, address)
3019 	ZEND_ARG_INFO(0, port)
3020 ZEND_END_ARG_INFO()
3021 
3022 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_ip6_addr, 0, 0, 2)
3023 	ZEND_ARG_INFO(0, address)
3024 	ZEND_ARG_INFO(0, port)
3025 ZEND_END_ARG_INFO()
3026 
3027 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_init, 0, 0, 0)
3028 	ZEND_ARG_INFO(0, loop)
3029 ZEND_END_ARG_INFO()
3030 
3031 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_open, 0, 0, 2)
3032 	ZEND_ARG_INFO(0, resource)
3033 	ZEND_ARG_INFO(0, udpfd)
3034 ZEND_END_ARG_INFO()
3035 
3036 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_bind, 0, 0, 2)
3037 	ZEND_ARG_INFO(0, resource)
3038 	ZEND_ARG_INFO(0, address)
3039 	ZEND_ARG_INFO(0, flags)
3040 ZEND_END_ARG_INFO()
3041 
3042 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_bind6, 0, 0, 2)
3043 	ZEND_ARG_INFO(0, resource)
3044 	ZEND_ARG_INFO(0, address)
3045 	ZEND_ARG_INFO(0, flags)
3046 ZEND_END_ARG_INFO()
3047 
3048 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_recv_start, 0, 0, 2)
3049 	ZEND_ARG_INFO(0, server)
3050 	ZEND_ARG_INFO(0, callback)
3051 ZEND_END_ARG_INFO()
3052 
3053 ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_udp_recv_stop, 0, 0, 1)
3054 	ZEND_ARG_INFO(0, server)
3055