From a01b96fb48b773d6d02e1ef3eecacbf8cd4f743a Mon Sep 17 00:00:00 2001 From: Tanli Su Date: Wed, 23 Jun 2021 15:43:53 -0400 Subject: [PATCH 1/6] PHPC-1490: Add support for var_export() and __set_state() in CursorId.c --- php_phongo_structs.h | 2 + src/MongoDB/CursorId.c | 50 +++++++++++++++------ tests/cursorid/cursorid-debug-001.phpt | 20 +++++++++ tests/cursorid/cursorid-var_export-001.phpt | 19 ++++++++ 4 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 tests/cursorid/cursorid-debug-001.phpt create mode 100644 tests/cursorid/cursorid-var_export-001.phpt diff --git a/php_phongo_structs.h b/php_phongo_structs.h index f0090e8d3..044ceb5d1 100644 --- a/php_phongo_structs.h +++ b/php_phongo_structs.h @@ -64,7 +64,9 @@ typedef struct { } php_phongo_cursor_t; typedef struct { + bool initialized; int64_t id; + HashTable* properties; zend_object std; } php_phongo_cursorid_t; diff --git a/src/MongoDB/CursorId.c b/src/MongoDB/CursorId.c index 16e872e96..d3167a635 100644 --- a/src/MongoDB/CursorId.c +++ b/src/MongoDB/CursorId.c @@ -40,6 +40,7 @@ static bool php_phongo_cursorid_init_from_string(php_phongo_cursorid_t* intern, } intern->id = id; + intern->initialized = true; return true; } /* }}} */ @@ -57,6 +58,29 @@ static bool php_phongo_cursorid_init_from_hash(php_phongo_cursorid_t* intern, Ha return false; } /* }}} */ +static HashTable* php_phongo_cursorid_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */ +{ + php_phongo_cursorid_t* intern; + HashTable* props; + + intern = Z_OBJ_CURSORID(PHONGO_COMPAT_GET_OBJ(object)); + + PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2); + + if (!intern->initialized) { + return props; + } + + { + zval value; + + ZVAL_INT64_STRING(&value, intern->id); + zend_hash_str_update(props, "id", sizeof("id") - 1, &value); + } + + return props; +} /* }}} */ + /* {{{ proto string MongoDB\Driver\CursorId::__toString() Returns the string representation of the CursorId */ static PHP_METHOD(CursorId, __toString) @@ -175,6 +199,11 @@ static void php_phongo_cursorid_free_object(zend_object* object) /* {{{ */ php_phongo_cursorid_t* intern = Z_OBJ_CURSORID(object); zend_object_std_dtor(&intern->std); + + if (intern->properties) { + zend_hash_destroy(intern->properties); + FREE_HASHTABLE(intern->properties); + } } /* }}} */ static zend_object* php_phongo_cursorid_create_object(zend_class_entry* class_type) /* {{{ */ @@ -193,24 +222,16 @@ static zend_object* php_phongo_cursorid_create_object(zend_class_entry* class_ty static HashTable* php_phongo_cursorid_get_debug_info(phongo_compat_object_handler_type* object, int* is_temp) /* {{{ */ { - php_phongo_cursorid_t* intern; - zval retval = ZVAL_STATIC_INIT; - *is_temp = 1; - intern = Z_OBJ_CURSORID(PHONGO_COMPAT_GET_OBJ(object)); - - array_init(&retval); - -#if SIZEOF_ZEND_LONG == 4 - ADD_ASSOC_INT64_AS_STRING(&retval, "id", intern->id); -#else - ADD_ASSOC_LONG_EX(&retval, "id", intern->id); -#endif - - return Z_ARRVAL(retval); + return php_phongo_cursorid_get_properties_hash(object, true); } /* }}} */ /* }}} */ +static HashTable* php_phongo_cursorid_get_properties(phongo_compat_object_handler_type* object) /* {{{ */ +{ + return php_phongo_cursorid_get_properties_hash(object, false); +} /* }}} */ + void php_phongo_cursorid_init_ce(INIT_FUNC_ARGS) /* {{{ */ { zend_class_entry ce; @@ -224,6 +245,7 @@ void php_phongo_cursorid_init_ce(INIT_FUNC_ARGS) /* {{{ */ memcpy(&php_phongo_handler_cursorid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers)); php_phongo_handler_cursorid.get_debug_info = php_phongo_cursorid_get_debug_info; + php_phongo_handler_cursorid.get_properties = php_phongo_cursorid_get_properties; php_phongo_handler_cursorid.free_obj = php_phongo_cursorid_free_object; php_phongo_handler_cursorid.offset = XtOffsetOf(php_phongo_cursorid_t, std); } /* }}} */ diff --git a/tests/cursorid/cursorid-debug-001.phpt b/tests/cursorid/cursorid-debug-001.phpt new file mode 100644 index 000000000..d4eeb7328 --- /dev/null +++ b/tests/cursorid/cursorid-debug-001.phpt @@ -0,0 +1,20 @@ +--TEST-- +MongoDB\Driver\CursorID debug output +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\CursorId)#%d (%d) { + ["id"]=> + %rint\(\d+\)|string\(\d+\) "\d+"%r +} +===DONE=== diff --git a/tests/cursorid/cursorid-var_export-001.phpt b/tests/cursorid/cursorid-var_export-001.phpt new file mode 100644 index 000000000..12f7d2cba --- /dev/null +++ b/tests/cursorid/cursorid-var_export-001.phpt @@ -0,0 +1,19 @@ +--TEST-- +MongoDB\Driver\CursorID: var_export() +--FILE-- + +===DONE=== + +--EXPECT-- +MongoDB\Driver\CursorId::__set_state(array( + 'id' => '7250031947823432848', +)) +===DONE=== From d3496d2e92a85219c4fc2ff1d5df499ae5bb1ebe Mon Sep 17 00:00:00 2001 From: Tanli Su Date: Thu, 24 Jun 2021 12:08:07 -0400 Subject: [PATCH 2/6] PHPC-1490: Add support for var_export() and __set_state() in CursorId.c --- src/MongoDB/CursorId.c | 11 ++++++++++- tests/cursorid/cursorid-debug-001.phpt | 2 +- tests/cursorid/cursorid-debug-002.phpt | 22 ++++++++++++++++++++++ tests/cursorid/cursorid-debug-003.phpt | 22 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/cursorid/cursorid-debug-002.phpt create mode 100644 tests/cursorid/cursorid-debug-003.phpt diff --git a/src/MongoDB/CursorId.c b/src/MongoDB/CursorId.c index d3167a635..597caea99 100644 --- a/src/MongoDB/CursorId.c +++ b/src/MongoDB/CursorId.c @@ -74,7 +74,16 @@ static HashTable* php_phongo_cursorid_get_properties_hash(phongo_compat_object_h { zval value; - ZVAL_INT64_STRING(&value, intern->id); + if (is_debug) { + #if SIZEOF_ZEND_LONG == 4 + ZVAL_INT64_STRING(&value, intern->id); + #else + ZVAL_LONG(&value, intern->id); + #endif + } + else { + ZVAL_INT64_STRING(&value, intern->id); + } zend_hash_str_update(props, "id", sizeof("id") - 1, &value); } diff --git a/tests/cursorid/cursorid-debug-001.phpt b/tests/cursorid/cursorid-debug-001.phpt index d4eeb7328..ca7458acd 100644 --- a/tests/cursorid/cursorid-debug-001.phpt +++ b/tests/cursorid/cursorid-debug-001.phpt @@ -15,6 +15,6 @@ var_dump($cursorId); --EXPECTF-- object(MongoDB\Driver\CursorId)#%d (%d) { ["id"]=> - %rint\(\d+\)|string\(\d+\) "\d+"%r + %rint\(7250031947823432848\)|string\(1\) "7250031947823432848"%r } ===DONE=== diff --git a/tests/cursorid/cursorid-debug-002.phpt b/tests/cursorid/cursorid-debug-002.phpt new file mode 100644 index 000000000..c88168e54 --- /dev/null +++ b/tests/cursorid/cursorid-debug-002.phpt @@ -0,0 +1,22 @@ +--TEST-- +MongoDB\Driver\CursorID debug output on 32-bit platform +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\CursorId)#%d (%d) { + ["id"]=> + string(1) "7250031947823432848" +} +===DONE=== diff --git a/tests/cursorid/cursorid-debug-003.phpt b/tests/cursorid/cursorid-debug-003.phpt new file mode 100644 index 000000000..c7b319d07 --- /dev/null +++ b/tests/cursorid/cursorid-debug-003.phpt @@ -0,0 +1,22 @@ +--TEST-- +MongoDB\Driver\CursorID debug output on 64-bit platform +--SKIPIF-- + +--FILE-- + +===DONE=== + +--EXPECTF-- +object(MongoDB\Driver\CursorId)#%d (%d) { + ["id"]=> + int(7250031947823432848) +} +===DONE=== From e9faf2661bf03273bbc76d42f3f674df3da52e47 Mon Sep 17 00:00:00 2001 From: Tanli Su Date: Thu, 24 Jun 2021 16:22:49 -0400 Subject: [PATCH 3/6] PHPC-1490: Add support __set_state() in CursorId.c --- src/MongoDB/Cursor.c | 1 + src/MongoDB/CursorId.c | 46 +++++++++++++++---- tests/cursorid/cursorid-set_state-001.phpt | 21 +++++++++ .../cursorid-set_state_error-001.phpt | 18 ++++++++ 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 tests/cursorid/cursorid-set_state-001.phpt create mode 100644 tests/cursorid/cursorid-set_state_error-001.phpt diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index 3415840f0..2dee9e0bf 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -126,6 +126,7 @@ static void php_phongo_cursor_id_new_from_id(zval* object, int64_t cursorid) /* intern = Z_CURSORID_OBJ_P(object); intern->id = cursorid; + intern->initialized = true; } /* }}} */ /* {{{ proto array MongoDB\Driver\Cursor::toArray() diff --git a/src/MongoDB/CursorId.c b/src/MongoDB/CursorId.c index 597caea99..d0bd3f5a6 100644 --- a/src/MongoDB/CursorId.c +++ b/src/MongoDB/CursorId.c @@ -39,7 +39,7 @@ static bool php_phongo_cursorid_init_from_string(php_phongo_cursorid_t* intern, return false; } - intern->id = id; + intern->id = id; intern->initialized = true; return true; } /* }}} */ @@ -61,7 +61,7 @@ static bool php_phongo_cursorid_init_from_hash(php_phongo_cursorid_t* intern, Ha static HashTable* php_phongo_cursorid_get_properties_hash(phongo_compat_object_handler_type* object, bool is_debug) /* {{{ */ { php_phongo_cursorid_t* intern; - HashTable* props; + HashTable* props; intern = Z_OBJ_CURSORID(PHONGO_COMPAT_GET_OBJ(object)); @@ -75,13 +75,12 @@ static HashTable* php_phongo_cursorid_get_properties_hash(phongo_compat_object_h zval value; if (is_debug) { - #if SIZEOF_ZEND_LONG == 4 - ZVAL_INT64_STRING(&value, intern->id); - #else - ZVAL_LONG(&value, intern->id); - #endif - } - else { +#if SIZEOF_ZEND_LONG == 4 + ZVAL_INT64_STRING(&value, intern->id); +#else + ZVAL_LONG(&value, intern->id); +#endif + } else { ZVAL_INT64_STRING(&value, intern->id); } zend_hash_str_update(props, "id", sizeof("id") - 1, &value); @@ -90,6 +89,30 @@ static HashTable* php_phongo_cursorid_get_properties_hash(phongo_compat_object_h return props; } /* }}} */ +/* {{{ proto MongoDB\Driver\CursorId MongoDB\Driver\CursorId::__set_state(array $properties) +*/ +static PHP_METHOD(CursorId, __set_state) +{ + zend_error_handling error_handling; + php_phongo_cursorid_t* intern; + HashTable* props; + zval* array; + + zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling); + if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &array) == FAILURE) { + zend_restore_error_handling(&error_handling); + return; + } + zend_restore_error_handling(&error_handling); + + object_init_ex(return_value, php_phongo_cursorid_ce); + + intern = Z_CURSORID_OBJ_P(return_value); + props = Z_ARRVAL_P(array); + + php_phongo_cursorid_init_from_hash(intern, props); +} /* }}} */ + /* {{{ proto string MongoDB\Driver\CursorId::__toString() Returns the string representation of the CursorId */ static PHP_METHOD(CursorId, __toString) @@ -181,6 +204,10 @@ static PHP_METHOD(CursorId, unserialize) } /* }}} */ /* {{{ MongoDB\Driver\CursorId function entries */ +ZEND_BEGIN_ARG_INFO_EX(ai_CursorId___set_state, 0, 0, 1) + ZEND_ARG_ARRAY_INFO(0, properties, 0) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(ai_CursorId_unserialize, 0, 0, 1) ZEND_ARG_INFO(0, serialized) ZEND_END_ARG_INFO() @@ -190,6 +217,7 @@ ZEND_END_ARG_INFO() static zend_function_entry php_phongo_cursorid_me[] = { /* clang-format off */ + PHP_ME(CursorId, __set_state, ai_CursorId___set_state, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC) PHP_ME(CursorId, __toString, ai_CursorId_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(CursorId, serialize, ai_CursorId_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) PHP_ME(CursorId, unserialize, ai_CursorId_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL) diff --git a/tests/cursorid/cursorid-set_state-001.phpt b/tests/cursorid/cursorid-set_state-001.phpt new file mode 100644 index 000000000..7da89bf4b --- /dev/null +++ b/tests/cursorid/cursorid-set_state-001.phpt @@ -0,0 +1,21 @@ +--TEST-- +MongoDB\Driver\CursorId::__set_state() +--FILE-- + "$cursorId", +])); +echo "\n\n"; + +?> +===DONE=== + +--EXPECTF-- +MongoDB\Driver\CursorId::__set_state(array( +%w'id' => '7250031947823432848', +)) + +===DONE=== diff --git a/tests/cursorid/cursorid-set_state_error-001.phpt b/tests/cursorid/cursorid-set_state_error-001.phpt new file mode 100644 index 000000000..dab1a0c6e --- /dev/null +++ b/tests/cursorid/cursorid-set_state_error-001.phpt @@ -0,0 +1,18 @@ +--TEST-- +MongoDB\Driver\CursorId::__set_state() requires "id" string field +--FILE-- + 0]); +}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n"; + +?> +===DONE=== + +--EXPECT-- +OK: Got MongoDB\Driver\Exception\InvalidArgumentException +MongoDB\Driver\CursorId initialization requires "id" string field +===DONE=== From 1f4d118937e1860b5ca9c736398b692d0c24d2ae Mon Sep 17 00:00:00 2001 From: Tanli Su Date: Thu, 24 Jun 2021 16:42:50 -0400 Subject: [PATCH 4/6] PHPC-1490: Add support for __set_state() in CursorId.c --- src/MongoDB/Cursor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index 2dee9e0bf..aa05b7082 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -124,8 +124,8 @@ static void php_phongo_cursor_id_new_from_id(zval* object, int64_t cursorid) /* object_init_ex(object, php_phongo_cursorid_ce); - intern = Z_CURSORID_OBJ_P(object); - intern->id = cursorid; + intern = Z_CURSORID_OBJ_P(object); + intern->id = cursorid; intern->initialized = true; } /* }}} */ From fad228b3fa2cff252e6e624d7fb94b0a5d1d23e5 Mon Sep 17 00:00:00 2001 From: Tanli Su Date: Fri, 25 Jun 2021 17:38:04 -0400 Subject: [PATCH 5/6] PHPC-1490: Add support for var_export() and __set_state() in CursorId.c --- tests/cursorid/cursorid-001.phpt | 2 +- tests/cursorid/cursorid-002.phpt | 2 +- tests/cursorid/cursorid-debug-001.phpt | 4 ++-- tests/cursorid/cursorid-debug-002.phpt | 4 ++-- tests/cursorid/cursorid-debug-003.phpt | 4 ++-- tests/cursorid/cursorid-serialization-001.phpt | 2 +- tests/cursorid/cursorid-set_state-001.phpt | 11 ++++------- tests/cursorid/cursorid-set_state_error-001.phpt | 4 ++-- tests/cursorid/cursorid-tostring-001.phpt | 15 +++++++++++++++ tests/cursorid/cursorid-var_export-001.phpt | 2 +- 10 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 tests/cursorid/cursorid-tostring-001.phpt diff --git a/tests/cursorid/cursorid-001.phpt b/tests/cursorid/cursorid-001.phpt index e1643490e..34cfbbc3e 100644 --- a/tests/cursorid/cursorid-001.phpt +++ b/tests/cursorid/cursorid-001.phpt @@ -1,5 +1,5 @@ --TEST-- -MongoDB\Driver\CursorID BSON serialization +MongoDB\Driver\CursorId BSON serialization --SKIPIF-- diff --git a/tests/cursorid/cursorid-002.phpt b/tests/cursorid/cursorid-002.phpt index 7143a756f..1ca48ad39 100644 --- a/tests/cursorid/cursorid-002.phpt +++ b/tests/cursorid/cursorid-002.phpt @@ -1,5 +1,5 @@ --TEST-- -MongoDB\Driver\CursorID BSON serialization for killCursors command +MongoDB\Driver\CursorId BSON serialization for killCursors command --SKIPIF-- diff --git a/tests/cursorid/cursorid-debug-001.phpt b/tests/cursorid/cursorid-debug-001.phpt index ca7458acd..c08c99cff 100644 --- a/tests/cursorid/cursorid-debug-001.phpt +++ b/tests/cursorid/cursorid-debug-001.phpt @@ -1,5 +1,5 @@ --TEST-- -MongoDB\Driver\CursorID debug output +MongoDB\Driver\CursorId debug output --FILE-- - %rint\(7250031947823432848\)|string\(1\) "7250031947823432848"%r + %rint\(|string\(19\) "|%r7250031947823432848%r"|\)%r } ===DONE=== diff --git a/tests/cursorid/cursorid-debug-002.phpt b/tests/cursorid/cursorid-debug-002.phpt index c88168e54..6591a22ca 100644 --- a/tests/cursorid/cursorid-debug-002.phpt +++ b/tests/cursorid/cursorid-debug-002.phpt @@ -1,5 +1,5 @@ --TEST-- -MongoDB\Driver\CursorID debug output on 32-bit platform +MongoDB\Driver\CursorId debug output on 32-bit platform --SKIPIF-- --FILE-- @@ -17,6 +17,6 @@ var_dump($cursorId); --EXPECTF-- object(MongoDB\Driver\CursorId)#%d (%d) { ["id"]=> - string(1) "7250031947823432848" + string(19) "7250031947823432848" } ===DONE=== diff --git a/tests/cursorid/cursorid-debug-003.phpt b/tests/cursorid/cursorid-debug-003.phpt index c7b319d07..2ff1b6448 100644 --- a/tests/cursorid/cursorid-debug-003.phpt +++ b/tests/cursorid/cursorid-debug-003.phpt @@ -1,7 +1,7 @@ --TEST-- -MongoDB\Driver\CursorID debug output on 64-bit platform +MongoDB\Driver\CursorId debug output on 64-bit platform --SKIPIF-- - + --FILE-- "$cursorId", + 'id' => '7250031947823432848', ])); -echo "\n\n"; +echo "\n"; ?> ===DONE=== ---EXPECTF-- +--EXPECT-- MongoDB\Driver\CursorId::__set_state(array( -%w'id' => '7250031947823432848', + 'id' => '7250031947823432848', )) - ===DONE=== diff --git a/tests/cursorid/cursorid-set_state_error-001.phpt b/tests/cursorid/cursorid-set_state_error-001.phpt index dab1a0c6e..99d7af95a 100644 --- a/tests/cursorid/cursorid-set_state_error-001.phpt +++ b/tests/cursorid/cursorid-set_state_error-001.phpt @@ -7,12 +7,12 @@ require_once __DIR__ . '/../utils/basic.inc'; echo throws(function() { MongoDB\Driver\CursorId::__set_state(['id' => 0]); -}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n"; +}, 'MongoDB\Driver\Exception\InvalidArgumentException::class'), "\n"; ?> ===DONE=== --EXPECT-- -OK: Got MongoDB\Driver\Exception\InvalidArgumentException +ALMOST: Got MongoDB\Driver\Exception\InvalidArgumentException (MongoDB\Driver\CursorId initialization requires "id" string field) - expected MongoDB\Driver\Exception\InvalidArgumentException::class MongoDB\Driver\CursorId initialization requires "id" string field ===DONE=== diff --git a/tests/cursorid/cursorid-tostring-001.phpt b/tests/cursorid/cursorid-tostring-001.phpt new file mode 100644 index 000000000..7625f3d58 --- /dev/null +++ b/tests/cursorid/cursorid-tostring-001.phpt @@ -0,0 +1,15 @@ +--TEST-- +MongoDB\Driver\CursorId::__toString() +--FILE-- + +===DONE=== + +--EXPECT-- +string(19) "7250031947823432848" +===DONE=== diff --git a/tests/cursorid/cursorid-var_export-001.phpt b/tests/cursorid/cursorid-var_export-001.phpt index 12f7d2cba..e4ba0b817 100644 --- a/tests/cursorid/cursorid-var_export-001.phpt +++ b/tests/cursorid/cursorid-var_export-001.phpt @@ -1,5 +1,5 @@ --TEST-- -MongoDB\Driver\CursorID: var_export() +MongoDB\Driver\CursorId: var_export() --FILE-- Date: Mon, 28 Jun 2021 11:13:53 -0400 Subject: [PATCH 6/6] PHPC-1490: Add support for var_export() and __set_state() in CursorId.c --- tests/cursorid/cursorid-set_state_error-001.phpt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cursorid/cursorid-set_state_error-001.phpt b/tests/cursorid/cursorid-set_state_error-001.phpt index 99d7af95a..863940589 100644 --- a/tests/cursorid/cursorid-set_state_error-001.phpt +++ b/tests/cursorid/cursorid-set_state_error-001.phpt @@ -7,12 +7,12 @@ require_once __DIR__ . '/../utils/basic.inc'; echo throws(function() { MongoDB\Driver\CursorId::__set_state(['id' => 0]); -}, 'MongoDB\Driver\Exception\InvalidArgumentException::class'), "\n"; +}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n"; ?> ===DONE=== --EXPECT-- -ALMOST: Got MongoDB\Driver\Exception\InvalidArgumentException (MongoDB\Driver\CursorId initialization requires "id" string field) - expected MongoDB\Driver\Exception\InvalidArgumentException::class +OK: Got MongoDB\Driver\Exception\InvalidArgumentException MongoDB\Driver\CursorId initialization requires "id" string field ===DONE===