C++ access to WeakMap and WeakSet

56 views
Skip to first unread message

Alex Kodat

unread,
Aug 26, 2022, 8:50:10 AM8/26/22
to v8-users
Is there a way to create and manipulate WeakMaps and WeakSets from C++? This seems like such an obvious missing piece in the C++ API that I'm sure the answer will be embarrassingly obvious (apologies in advance).

Shu-yu Guo

unread,
Aug 26, 2022, 12:24:08 PM8/26/22
to v8-u...@googlegroups.com
Hi Alex,

There is no WeakMap and WeakSet support in API. The V8 API doesn't expose many standard library JS values and objects. At least currently, there are no immediate plans to expose them. More exposed surface means higher maintenance burden going forward, so we're likely to consider exposing new API surface on a use case-by-use case basis.

Is it possible to do what you'd like to do by running a bit of script?

Cheers,
shu

On Fri, Aug 26, 2022 at 5:50 AM Alex Kodat <ako...@rocketsoftware.com> wrote:
Is there a way to create and manipulate WeakMaps and WeakSets from C++? This seems like such an obvious missing piece in the C++ API that I'm sure the answer will be embarrassingly obvious (apologies in advance).

--
--
v8-users mailing list
v8-u...@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+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-users/c8ac347e-f1b3-4111-90ee-7ec822d097a8n%40googlegroups.com.

Alex Kodat

unread,
Aug 26, 2022, 2:32:23 PM8/26/22
to v8-users
Thanks Shu,

Interesting. I always thought of the C++ API as fairly complete but maybe I just always happened to use the bits that are there. Of course I could create a script that contains functions that C++ can call to create and update a WeakMap (no real use for a WeakSet, at the moment), it's just kinda yucky. While it's not hyper-critical for me, it seems a WeakMap would be a nice general purpose facility for C++ code to manage heap objects. In some sense, a WeakDataMap (maps Data rather than Value) might be even more useful though, of course, that would have no JS analogue.

My specific use case is that I want to map a Module object to my own JS object in a HostInitializeImportMetaObjectCallback callback (before calling a JS function). Of course, I can't do that because Module is Data not Value but I can use GetModuleNamespace to get a Value that maps one-to-one to Module so can be used in a WeakMap. Of course, if Module had an embedder settable/retrievable value, I wouldn't need a WeakMap but, like I said, it seems a WeakMap (or WeakDataMap) would be a useful, general-purpose C++ facility (though I agree people should use as little C++ as possible). And yes, I know there are lots of other ways to do what I want to do (I haven't looked at Node.js's approach, but it must have one), but the WeakMap approach seemed the tidiest.

Would it affect the decision whether to add WeakMap and WeakSet to the C++ API if I offered to do the work myself (I've contributed before)? Or is it not so much an effort/resource issue as an API bloat issue? 

Thanks again

Shu-yu Guo

unread,
Aug 26, 2022, 5:16:41 PM8/26/22
to v8-u...@googlegroups.com
On Fri, Aug 26, 2022 at 11:32 AM Alex Kodat <ako...@rocketsoftware.com> wrote:
Thanks Shu,

Interesting. I always thought of the C++ API as fairly complete but maybe I just always happened to use the bits that are there. Of course I could create a script that contains functions that C++ can call to create and update a WeakMap (no real use for a WeakSet, at the moment), it's just kinda yucky. While it's not hyper-critical for me, it seems a WeakMap would be a nice general purpose facility for C++ code to manage heap objects. In some sense, a WeakDataMap (maps Data rather than Value) might be even more useful though, of course, that would have no JS analogue. 

My specific use case is that I want to map a Module object to my own JS object in a HostInitializeImportMetaObjectCallback callback (before calling a JS function). Of course, I can't do that because Module is Data not Value but I can use GetModuleNamespace to get a Value that maps one-to-one to Module so can be used in a WeakMap. Of course, if Module had an embedder settable/retrievable value, I wouldn't need a WeakMap but, like I said, it seems a WeakMap (or WeakDataMap) would be a useful, general-purpose C++ facility (though I agree people should use as little C++ as possible). And yes, I know there are lots of other ways to do what I want to do (I haven't looked at Node.js's approach, but it must have one), but the WeakMap approach seemed the tidiest.

Would it affect the decision whether to add WeakMap and WeakSet to the C++ API if I offered to do the work myself (I've contributed before)? Or is it not so much an effort/resource issue as an API bloat issue? 

I see it more as ongoing maintenance rule of thumb than bloat per se: it's hard for us to remove public APIs once it's out there, and it increases the likelihood of tying our hands for making engine changes.

In this particular case if you'd like to take a stab at exposing JSWeakMap I'd be happy to take a look. The main constraint is that the API version should be no more expressive than what can already be done in JS (e.g. definitely no iteration).
 
Reply all
Reply to author
Forward
0 new messages