keeping wrapped objects alive

46 views
Skip to first unread message

ibon

unread,
May 7, 2018, 7:19:17 PM5/7/18
to v8-users
For my embedding needs, sometimes i need to keep a native wrapped object alive.
Normally, I set a Persistent ref where i install a finalisation callback with custom wrapper class is. This gives me fine control of all native wrapped objects at Isolate level, and the GC kicks in as expected.
I have found that to keep an object alive, I can set a Private property on a parent object. E.g a TouchEvent, keeps a private ref to a TouchList. Private properties is an experimental feature, so not sure if this is discouraged though.
This works for this specific case, where a TouchEvent refers just one TouchList.

While implementing other types of objects, like an Image, I need to keep the image javascript wrapper alive until async loading finishes so that I get the chance to notify registered event observers. For this purpose, I use MarkActive, which keeps the object alive, and as expected, it also keeps live the whole object tree where it is contained.

Since there's no equivalent to `unmark active`, I guess MarkActive'd objects survive just one GC cycle ? If necessary, should I call MarkActive again if I still need to keep my object alive ?
Is this the way to go for keeping objects alive ?

Thanks.

- ibon

Ben Noordhuis

unread,
May 8, 2018, 3:35:59 AM5/8/18
to v8-users
Yes, the effect of MarkActive() only persists until the next garbage collection.

I wonder though, why don't you call ClearWeak()? That keeps the
object alive until MakeWeak() is called again.

ibon

unread,
May 8, 2018, 3:13:47 PM5/8/18
to v8-users
oh, I see. 
Yes, tried ClearWeak/SetWeak, and worked as expected, thanks a lot !!

What about the v8::Private::ForAPI stuff ? Is it recommended despite being experimental, or should I avoid it ?

Thanks.

Ben Noordhuis

unread,
May 10, 2018, 3:17:44 AM5/10/18
to v8-users
On Tue, May 8, 2018 at 9:13 PM, ibon <ibon.t...@gmail.com> wrote:
> What about the v8::Private::ForAPI stuff ? Is it recommended despite being
> experimental, or should I avoid it ?

I'd say it's fine. "Experimental" in this case means the API can
change, not that it's known to be buggy.
Reply all
Reply to author
Forward
0 new messages