Allow removing non-existing key from WeakMap

In line with usual PHP semantics. This previously triggered an
assertion failure.
This commit is contained in:
Nikita Popov 2020-08-27 12:42:08 +02:00
parent ce83ec8790
commit 614c0b846e
2 changed files with 8 additions and 0 deletions

View File

@ -46,6 +46,9 @@ try {
echo $e->getMessage(), "\n";
}
// It's okay to unset an object that's not in the map.
unset($map[new stdClass]);
echo "\nIndirect modification:\n";
$map[$obj] = [];
$map[$obj][] = 42;

View File

@ -372,6 +372,11 @@ static void zend_weakmap_unset_dimension(zend_object *object, zval *offset)
zend_weakmap *wm = zend_weakmap_from(object);
zend_object *obj_key = Z_OBJ_P(offset);
if (!zend_hash_index_exists(&wm->ht, (zend_ulong) Z_OBJ_P(offset))) {
/* Object not in WeakMap, do nothing. */
return;
}
zend_weakref_unregister(obj_key, ZEND_WEAKREF_ENCODE(wm, ZEND_WEAKREF_TAG_MAP));
}