Weak reference of CefV8Value

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Weak reference of CefV8Value

Postby rishgarg » Fri Dec 13, 2019 6:13 am

Is there a way to perform SetWeak/ClearWeak on a CEFV8Value? (https://v8docs.nodesource.com/node-0.12 ... e5ae7042b9)

We need the above support to manage lifespan of a CEFV8Value. We want to wrap the CEFV8Value as a Reference object where each reference has an associated count with a value of 0 or higher. The count determines if the reference will keep the corresponding object live. References with a count of 0 do not prevent the object from being collected and are often called 'weak' references. Any count greater than 0 will prevent the object from being collected.

(https://nodejs.org/docs/latest/api/n-ap ... ive_method)
rishgarg
Techie
 
Posts: 16
Joined: Wed Feb 13, 2019 1:11 pm

Re: Weak reference of CefV8Value

Postby magreenblatt » Fri Dec 13, 2019 2:46 pm

Keep a CefRefPtr reference to the CefV8Value. The underlying V8 object will be marked as weak when that last reference is released.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Weak reference of CefV8Value

Postby rishgarg » Sat Dec 14, 2019 2:30 am

The underlying V8 object will be marked as weak when that last reference is released.

How can we revive the weak underlying V8 object/CEFV8Value before it is garbage collected?
rishgarg
Techie
 
Posts: 16
Joined: Wed Feb 13, 2019 1:11 pm

Re: Weak reference of CefV8Value

Postby magreenblatt » Sat Dec 14, 2019 9:40 am

Why do you want to do that?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Weak reference of CefV8Value

Postby rishgarg » Mon Dec 16, 2019 5:16 am

We want to keep strong references to the javascript objects on the native side. The reference count can go to 0 in which case the reference becomes weak ( like when we do something like v8Persistent.setWeak() ). But we also want to allow increasing the reference count on the native side if the object is not yet garbage collected (due to strong reference on the JS side) like what we can do with v8Persistent.clearWeak().
rishgarg
Techie
 
Posts: 16
Joined: Wed Feb 13, 2019 1:11 pm

Re: Weak reference of CefV8Value

Postby magreenblatt » Mon Dec 16, 2019 3:43 pm

What is the use case for this in your application?
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Weak reference of CefV8Value

Postby rishgarg » Fri Jan 03, 2020 1:10 am

We are implementing CEF backend for NAPI(https://nodejs.org/docs/latest/api/n-api.html).
N-API provides methods to create persistent references to an object. Each persistent reference has an associated count with a value of 0 or higher. The count determines if the reference will keep the corresponding object live. References with a count of 0 do not prevent the object from being collected and are often called 'weak' references. Any count greater than 0 will prevent the object from being collected.
References can be created with an initial reference count. The count can then be modified through napi_reference_ref and napi_reference_unref. If an object is collected while the count for a reference is 0, all subsequent calls to get the object associated with the reference napi_get_reference_value will return NULL for the returned napi_value. An attempt to call napi_reference_ref for a reference whose object has been collected will result in an error.
Please have a look at https://nodejs.org/docs/latest/api/n-ap ... ive_method section in order to understand the problem better.
rishgarg
Techie
 
Posts: 16
Joined: Wed Feb 13, 2019 1:11 pm

Re: Weak reference of CefV8Value

Postby magreenblatt » Fri Jan 03, 2020 4:03 am

It sounds like a bad idea to create objects that may disappear randomly or unexpectedly due to GC. This is not something that I would want to support with the CEF API.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Weak reference of CefV8Value

Postby rishgarg » Mon Jan 06, 2020 5:27 am

The object disappearing randomly/unexpectedly is not a problem as we will always call the napi_get_reference_value API. This API will return NULL if the underlying object is garbage collected.
We have a couple of requirements for the correct implementation of napi_get_reference_value API.
1) We need a way to know that the object is garbage collected, something like FinalizeCallback which is set using _persistent.SetWeak. The _persistent is reset only in the FinalizeCallback and not before ( code below)
2) We need to keep the underlying object to be valid with a reference count of 0 until it is garbage collected( if someone tries to Ref the object we perform ClearWeak on the _persistent ).

Code: Select all
 
  v8::Persistent<T> _persistent;

  static void FinalizeCallback(const v8::WeakCallbackInfo<Reference>& data) {
    Reference* reference = data.GetParameter();
    reference->_persistent.Reset();
  }

  uint32_t Ref() {
    if (++_refcount == 1) {
      _persistent.ClearWeak();
    }
    return _refcount;
  }

  uint32_t Unref() {
    if (_refcount == 0) {
        return 0;
    }
    if (--_refcount == 0) {
      _persistent.SetWeak(
          this, FinalizeCallback, v8::WeakCallbackType::kParameter);
      _persistent.MarkIndependent();
    }
    return _refcount;
  }

  uint32_t RefCount() {
    return _refcount;
  }

  v8::Local<T> Get() {
    if (_persistent.IsEmpty()) {
      return NULL;
    } else {
      return v8::Local<T>::New(_env->isolate, _persistent);
    }
  }
rishgarg
Techie
 
Posts: 16
Joined: Wed Feb 13, 2019 1:11 pm

Re: Weak reference of CefV8Value

Postby magreenblatt » Mon Jan 06, 2020 5:31 am

I follow what you're writing. However, this is not functionality that is useful to the average CEF user, and consequently it is not functionality that we are going to add in CEF.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 55 guests