Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ext/standard/io_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ static zend_class_entry *php_io_poll_backend_class_entry;
static zend_class_entry *php_io_poll_event_class_entry;
static zend_class_entry *php_io_poll_context_class_entry;
static zend_class_entry *php_io_poll_watcher_class_entry;
static zend_class_entry *php_io_poll_handle_class_entry;
static zend_class_entry *php_io_exception_class_entry;
static zend_class_entry *php_io_poll_exception_class_entry;
static zend_class_entry *php_io_poll_failed_backend_unavailable_class_entry;
Expand Down Expand Up @@ -709,7 +708,7 @@ PHP_METHOD(Io_Poll_Context, add)
zval *data = NULL;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJECT_OF_CLASS(handle_obj, php_io_poll_handle_class_entry)
Z_PARAM_OBJECT_OF_CLASS(handle_obj, php_poll_handle_ce)
Z_PARAM_ARRAY(event_enums)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(data)
Expand Down Expand Up @@ -861,13 +860,13 @@ PHP_MINIT_FUNCTION(poll)
/* Register event enum */
php_io_poll_event_class_entry = register_class_Io_Poll_Event();

/* Register Handle interface */
php_io_poll_handle_class_entry = register_class_Io_Poll_Handle();
php_io_poll_handle_class_entry->interface_gets_implemented = php_stream_poll_handle_implement_interface;
/* Register Handle interface, which php_poll.h exports to extensions */
php_poll_handle_ce = register_class_Io_Poll_Handle();
php_poll_handle_ce->interface_gets_implemented = php_stream_poll_handle_implement_interface;

/* Register StreamPollHandle class */
php_stream_poll_handle_class_entry
= register_class_StreamPollHandle(php_io_poll_handle_class_entry);
= register_class_StreamPollHandle(php_poll_handle_ce);
php_stream_poll_handle_class_entry->create_object = php_stream_poll_handle_create_object;

memcpy(&php_io_poll_handle_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
Expand Down
18 changes: 18 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "zend_attributes.h"
#include "zend_enum.h"
#include "zend_interfaces.h"
#include "main/php_poll.h"
#include "zend_weakrefs.h"
#include "Zend/Optimizer/zend_optimizer.h"
#include "Zend/zend_alloc.h"
Expand Down Expand Up @@ -1193,6 +1194,23 @@ static ZEND_FUNCTION(zend_test_zstr_init_literal)
RETURN_STR(ZSTR_INIT_LITERAL("foo\0bar", false));
}

static ZEND_FUNCTION(zend_test_poll_handle_descriptor)
{
zval *zv;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(zv)
ZEND_PARSE_PARAMETERS_END();

php_poll_handle_object *handle = php_poll_handle_from_zval(zv);

if (handle == NULL) {
RETURN_FALSE;
}

RETURN_LONG((zend_long) php_poll_handle_get_fd(handle));
}

static ZEND_FUNCTION(zend_test_is_string_marked_as_valid_utf8)
{
zend_string *str;
Expand Down
6 changes: 6 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ function zend_test_zend_call_stack_use_all(): int {}

function zend_test_is_string_marked_as_valid_utf8(string $string): bool {}

/**
* Resolves a handle the way an extension consuming Io\Poll\Handle does, or false when
* the argument is not a handle.
*/
function zend_test_poll_handle_descriptor(mixed $handle): int|false {}

function zend_get_map_ptr_last(): int {}

function zend_test_crash(?string $message = null): void {}
Expand Down
8 changes: 7 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/zend_test/test_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion ext/zend_test/test_legacy_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions ext/zend_test/tests/io_poll_handle_api.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
An extension resolves any Io\Poll\Handle through php_poll_handle_from_zval()
--EXTENSIONS--
zend_test
--FILE--
<?php
$domain = 'WIN' === strtoupper(substr(PHP_OS, 0, 3)) ? STREAM_PF_INET : STREAM_PF_UNIX;
[$r, $w] = stream_socket_pair($domain, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

$handle = new StreamPollHandle($r);
var_dump(zend_test_poll_handle_descriptor($handle) >= 0);

// anything that is not a handle resolves to nothing
var_dump(zend_test_poll_handle_descriptor($r));
var_dump(zend_test_poll_handle_descriptor(new stdClass()));
var_dump(zend_test_poll_handle_descriptor('nope'));

fclose($r);
fclose($w);
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(false)
8 changes: 7 additions & 1 deletion main/php_poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,19 @@ struct php_poll_handle_object {
#define PHP_POLL_HANDLE_OBJ_FROM_ZV(zv) PHP_POLL_HANDLE_OBJ_FROM_ZOBJ(Z_OBJ_P(zv))

/* Default operations */
extern php_poll_handle_ops php_poll_handle_default_ops;
PHPAPI extern php_poll_handle_ops php_poll_handle_default_ops;

/* The Io\Poll\Handle interface, to declare a handle type and to accept one */
PHPAPI extern zend_class_entry *php_poll_handle_ce;

/* Utility functions for extensions */
PHPAPI php_poll_handle_object *php_poll_handle_object_create(
size_t obj_size, zend_class_entry *ce, php_poll_handle_ops *ops);
PHPAPI void php_poll_handle_object_free(zend_object *obj);

/* The handle a zval holds, or NULL when it holds anything else */
PHPAPI php_poll_handle_object *php_poll_handle_from_zval(const zval *zv);

/* Get file descriptor from any poll handle */
PHPAPI php_socket_t php_poll_handle_get_fd(php_poll_handle_object *handle);

Expand Down
15 changes: 14 additions & 1 deletion main/poll/poll_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,23 @@ static void php_poll_handle_default_cleanup(php_poll_handle_object *handle)
}

/* Default operations that call PHP userspace methods */
php_poll_handle_ops php_poll_handle_default_ops = { .get_fd = php_poll_handle_default_get_fd,
PHPAPI php_poll_handle_ops php_poll_handle_default_ops = { .get_fd = php_poll_handle_default_get_fd,
.is_valid = php_poll_handle_default_is_valid,
.cleanup = php_poll_handle_default_cleanup };

/* Set when ext/standard registers the interface */
PHPAPI zend_class_entry *php_poll_handle_ce = NULL;

PHPAPI php_poll_handle_object *php_poll_handle_from_zval(const zval *zv)
{
if (Z_TYPE_P(zv) != IS_OBJECT || php_poll_handle_ce == NULL
|| !instanceof_function(Z_OBJCE_P(zv), php_poll_handle_ce)) {
return NULL;
}

return PHP_POLL_HANDLE_OBJ_FROM_ZV(zv);
}

Comment thread
nicolas-grekas marked this conversation as resolved.
/* Allocate a new poll handle object */
PHPAPI php_poll_handle_object *php_poll_handle_object_create(
size_t obj_size, zend_class_entry *ce, php_poll_handle_ops *ops)
Expand Down
Loading