Skip to content

Add ZEND_API for weakmap functionality via zend_weakrefs_hash_add/del #7600

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

Closed
wants to merge 2 commits into from

Conversation

bwoebi
Copy link
Member

@bwoebi bwoebi commented Oct 20, 2021

Exposes a simple API for weakmap functionality (weakref functionality can be trivially emulated by adding a dummy element instead) as ZEND_API to be trivially usable in extensions.

The implementation does not actually change (it's the first element of the struct), but I'm now explicitly passing &zend_weakmap->ht instead of zend_weakmap directly, to make it clearer that the weakmap implementation is generic with respect to the zend_weakmap structure and only cares about the HashTable pointer.

@bwoebi bwoebi added the Feature label Oct 20, 2021
@bwoebi bwoebi requested a review from nikic October 20, 2021 16:37
@bwoebi bwoebi changed the base branch from master to PHP-8.0 October 20, 2021 16:38
Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Do you think it would make sense to add a basic test to zend_test (which just calls add/del internally)?

ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
zend_result result = zend_hash_index_del(ht, (zend_ulong) key);
if (result == SUCCESS) {
zend_weakref_unregister(key, ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_MAP));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zend_weakref_unregister already does the hash del, so it might make sense to replace your hash_index_del here with hash_index_exists.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. And yeah will provide a small test for it.

@bwoebi
Copy link
Member Author

bwoebi commented Oct 21, 2021

@nikic has test now - looks good for merge then, right?

var_dump(zend_weakmap_remove($id2));
var_dump(zend_weakmap_remove($id2));

var_dump(zend_weakmap_dump());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to unset($id1) and dump again here, to shows the actual weak part :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

facepalm :-D

@@ -0,0 +1,36 @@
--TEST--
Test internal weakmap API
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a SKIPIF (8.0) or EXTENSIONS (8.1) section.

@bwoebi bwoebi closed this in 471102e Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants