On Tue, Nov 10, 2020 at 4:54 AM jres...@gmail.com <jresn...@gmail.com> wrote: > > I would like to cache Persistent handles in such a way that I can retrieve > the same Persistent instance given the identical JS object, i.e. imagine a > function like so: > > Persistent<Object> GetPersistentForObject(Local<Object> obj) > { > // If we have already created a Persistent for obj, find and return it. > // Otherwise, create and return a Persistent for obj and cache it for > // future calls. > } > > The obvious thought is to store the Persistent handles in a map of some kind, > but what to use as the key? I assume it would not be possible to actually use > Local handles as keys in a map that outlives the current HandleScope. > > One thought I had was to assign a unique integer to the object using > obj.SetPrivate(...), and use that as the key, but SetPrivate is documented as > experimental, which is discouraging. > > Is there an easier way to achieve what I'm trying to do? I feel like I may be > missing something obvious... > > Jonathan
A hashmap should work if you observe the following; 1. Use `v8::Object::GetIdentityHash()` to compute the hash key. 2. Use `Persistent::operator==()` to detect duplicates (also has an overload that compares against a Local) I.e., step 1 is to find the right bucket and step 2 is to find out if that bucket already contains the object. Using just the identity hash isn't sufficient, see [1] and [2] for discussion. Caveat: use `v8::Value::SameValue()` - not `Persistent::operator==()` - if you also store primitives (numbers, strings, etc.) [1] https://github.com/denoland/deno/pull/7946#discussion_r504192165 [2] https://github.com/denoland/deno/issues/7969 -- -- v8-users mailing list v8-users@googlegroups.com http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/CAHQurc-%3DihgyFyqe3r4_Nx0-RtWgS6mNrF7GqTap3HoQzbDZ2g%40mail.gmail.com.