Skip to content

Implement GH-13609: Dump wrapped object in WeakReference class #13621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2024
Merged
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
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PHP NEWS
. Fixed zend call stack size for macOs/arm64. (David Carlier)
. Added support for Zend Max Execution Timers on FreeBSD. (Kévin Dunglas)
. Ensure fiber stack is not backed by THP. (crrodriguez)
. Implement GH-13609 (Dump wrapped object in WeakReference class). (nielsdos)

- Curl:
. Deprecated the CURLOPT_BINARYTRANSFER constant. (divinity76)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ PHP 8.4 UPGRADE NOTES
. Added request_parse_body() function that allows parsing RFC1867 (multipart)
requests in non-POST HTTP requests.
RFC: https://wiki.php.net/rfc/rfc1867-non-post
. Getting the debug info for WeakReference will now also output the object
it references, or null if the reference is no longer valid.

- Curl:
. curl_version() returns an additional feature_list value, which is an
Expand Down
11 changes: 8 additions & 3 deletions Zend/tests/weakrefs/weakrefs_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,19 @@ object(stdClass)#1 (0) refcount(2){
}
object(stdClass)#1 (0) refcount(2){
}
object(WeakReference)#2 (0) {
object(WeakReference)#2 (1) {
["object"]=>
object(stdClass)#1 (0) {
}
}
object(WeakReference)#2 (0) {
object(WeakReference)#2 (1) {
["object"]=>
object(stdClass)#1 (0) {
}
}
object(stdClass)#1 (0) refcount(2){
}
object(stdClass)#1 (0) refcount(2){
}
NULL
NULL

26 changes: 26 additions & 0 deletions Zend/tests/weakrefs/weakrefs_debug_dump.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Weakrefs debug dump
--FILE--
<?php

$s = new stdClass;
$s->hello = 'world';

$weak = WeakReference::create($s);
var_dump($weak);
unset($s);
var_dump($weak);

?>
--EXPECT--
object(WeakReference)#2 (1) {
["object"]=>
object(stdClass)#1 (1) {
["hello"]=>
string(5) "world"
}
}
object(WeakReference)#2 (1) {
["object"]=>
NULL
}
20 changes: 20 additions & 0 deletions Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ static void zend_weakref_free(zend_object *zo) {
zend_object_std_dtor(&wr->std);
}

static HashTable *zend_weakref_get_debug_info(zend_object *object, int *is_temp)
{
*is_temp = 1;

HashTable *ht = zend_new_array(1);

zend_object *referent = zend_weakref_from(object)->referent;
zval value;
if (referent) {
ZVAL_OBJ_COPY(&value, referent);
} else {
ZVAL_NULL(&value);
}

zend_hash_update(ht, ZSTR_KNOWN(ZEND_STR_OBJECT), &value);

return ht;
}

ZEND_COLD ZEND_METHOD(WeakReference, __construct)
{
zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
Expand Down Expand Up @@ -749,6 +768,7 @@ void zend_register_weakref_ce(void) /* {{{ */
zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);

zend_weakref_handlers.free_obj = zend_weakref_free;
zend_weakref_handlers.get_debug_info = zend_weakref_get_debug_info;
zend_weakref_handlers.clone_obj = NULL;

zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);
Expand Down