Skip to content

Commit d6d3370

Browse files
authored
Implement GH-13609: Dump wrapped object in WeakReference class (#13621)
I chose "object" as that's also the argument name in WeakReference::create.
1 parent e7888a4 commit d6d3370

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ PHP NEWS
1515
. Fixed zend call stack size for macOs/arm64. (David Carlier)
1616
. Added support for Zend Max Execution Timers on FreeBSD. (Kévin Dunglas)
1717
. Ensure fiber stack is not backed by THP. (crrodriguez)
18+
. Implement GH-13609 (Dump wrapped object in WeakReference class). (nielsdos)
1819

1920
- Curl:
2021
. Deprecated the CURLOPT_BINARYTRANSFER constant. (divinity76)

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ PHP 8.4 UPGRADE NOTES
164164
. Added request_parse_body() function that allows parsing RFC1867 (multipart)
165165
requests in non-POST HTTP requests.
166166
RFC: https://wiki.php.net/rfc/rfc1867-non-post
167+
. Getting the debug info for WeakReference will now also output the object
168+
it references, or null if the reference is no longer valid.
167169

168170
- Curl:
169171
. curl_version() returns an additional feature_list value, which is an

Zend/tests/weakrefs/weakrefs_001.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ object(stdClass)#1 (0) refcount(2){
2626
}
2727
object(stdClass)#1 (0) refcount(2){
2828
}
29-
object(WeakReference)#2 (0) {
29+
object(WeakReference)#2 (1) {
30+
["object"]=>
31+
object(stdClass)#1 (0) {
32+
}
3033
}
31-
object(WeakReference)#2 (0) {
34+
object(WeakReference)#2 (1) {
35+
["object"]=>
36+
object(stdClass)#1 (0) {
37+
}
3238
}
3339
object(stdClass)#1 (0) refcount(2){
3440
}
3541
object(stdClass)#1 (0) refcount(2){
3642
}
3743
NULL
3844
NULL
39-
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Weakrefs debug dump
3+
--FILE--
4+
<?php
5+
6+
$s = new stdClass;
7+
$s->hello = 'world';
8+
9+
$weak = WeakReference::create($s);
10+
var_dump($weak);
11+
unset($s);
12+
var_dump($weak);
13+
14+
?>
15+
--EXPECT--
16+
object(WeakReference)#2 (1) {
17+
["object"]=>
18+
object(stdClass)#1 (1) {
19+
["hello"]=>
20+
string(5) "world"
21+
}
22+
}
23+
object(WeakReference)#2 (1) {
24+
["object"]=>
25+
NULL
26+
}

Zend/zend_weakrefs.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@ static void zend_weakref_free(zend_object *zo) {
271271
zend_object_std_dtor(&wr->std);
272272
}
273273

274+
static HashTable *zend_weakref_get_debug_info(zend_object *object, int *is_temp)
275+
{
276+
*is_temp = 1;
277+
278+
HashTable *ht = zend_new_array(1);
279+
280+
zend_object *referent = zend_weakref_from(object)->referent;
281+
zval value;
282+
if (referent) {
283+
ZVAL_OBJ_COPY(&value, referent);
284+
} else {
285+
ZVAL_NULL(&value);
286+
}
287+
288+
zend_hash_update(ht, ZSTR_KNOWN(ZEND_STR_OBJECT), &value);
289+
290+
return ht;
291+
}
292+
274293
ZEND_COLD ZEND_METHOD(WeakReference, __construct)
275294
{
276295
zend_throw_error(NULL, "Direct instantiation of WeakReference is not allowed, use WeakReference::create instead");
@@ -749,6 +768,7 @@ void zend_register_weakref_ce(void) /* {{{ */
749768
zend_weakref_handlers.offset = XtOffsetOf(zend_weakref, std);
750769

751770
zend_weakref_handlers.free_obj = zend_weakref_free;
771+
zend_weakref_handlers.get_debug_info = zend_weakref_get_debug_info;
752772
zend_weakref_handlers.clone_obj = NULL;
753773

754774
zend_ce_weakmap = register_class_WeakMap(zend_ce_arrayaccess, zend_ce_countable, zend_ce_aggregate);

0 commit comments

Comments
 (0)