how to bind a C++ object to the same v8 Object

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.

how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 4:04 am

when I bind a C++ object to v8, it will be different v8 object in v8 that will make a confuse while we compare the v8 objects to make a logic.
Is there a way to perform SetWeak/ClearWeak on a CEFV8Value? That would solve the problem.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby magreenblatt » Thu Sep 16, 2021 4:24 am

What are you trying to do? What, exactly, is the problem? Please show code to make it clearer.
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Re: how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 7:21 am

Code: Select all
struct UserData : public CefBaseRefCounted
        {
            UserData(void* _data)
                : data(_data)
            {
            }
           ~RefUserData()
            {
                if (data) delete (A*)data;
            }
           void * data;
            IMPLEMENT_REFCOUNTING(UserData);
        };

class A{};


Code: Select all
       
       void create(CefRefPtr<CefV8Value>& retval){
        static A * value= new A();
   CefRefPtr<CefV8Value> retval = CefV8Value::CreateObject(&*Class<A>::classAccessor, nullptr);
   retval->SetUserData(new UserData(value));
   setGettersAndMethods(retval, value);
      }
     

ignore the binding progress, we assume the function create will be binded into a js funtion named 'createJs' which return the retval variable of fucntion create.
so we get something like this in js:

var a = createjs() ;//wrap the static variable of class A C++ object in create.
var b = createjs() ;//the same C++ object
a == b//? usually we need true, but it will be false in the way we wrap C++ Object.
in other words,
in the function create , I wrap a C++ object into a cefvalue which will be binded to jsscript.
but every time we call , it will create a new v8 object, as we call CefV8Value::CreateObject to make a new cefvalue to wrap.

My question is how to wrap a C++ object into the same v8 object all the time , and does not influence the gc of js.
Last edited by colinChan008 on Thu Sep 16, 2021 7:44 am, edited 1 time in total.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby magreenblatt » Thu Sep 16, 2021 7:31 am

Why not have createjs() return the same CefV8Value object that was created previously?
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Re: how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 7:41 am

if we save the cefvalue in to a map (std::unordered_map<int64_t, CefRefPtr<CefV8Value>> objects_;) and return the same cefvalue from the map.
in this way, it will influence the gc of cefvalue.but I don't want this, as we need the gc of cefvalue to release the C++ object.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 7:47 am

so I want to know how to bind a C++ object into the same v8 Object.
if we save to a map, what the type should be which will not influence the gc of js.
in v8, we can do this by setWeak
Last edited by colinChan008 on Thu Sep 16, 2021 7:50 am, edited 2 times in total.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby magreenblatt » Thu Sep 16, 2021 7:47 am

It sounds like you need to reconsider your approach. For example, make the C++ object refcounted so that the same object can be referenced from multiple CefV8Value and provide a comparison function that looks at the underlying C++ object. Or, cache the object in the JS layer instead of the C++ layer so that lifespan is completely controlled by JS.
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Re: how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 7:57 am

cache the object in the Js layer doest not help anything, as our cache will retain the object all the time. as a result, the C++ object will not release.
in your c++ layer's suggestion, we need compare in js, not in c++.can u explain exactly.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby colinChan008 » Thu Sep 16, 2021 8:07 am

or can u tell me how to get v8object's reference pointer which doesnot influence the gc, so that i can make a new cefvalue with it. so that they will be equal in js.
colinChan008
Techie
 
Posts: 12
Joined: Thu Sep 16, 2021 3:27 am

Re: how to bind a C++ object to the same v8 Object

Postby magreenblatt » Thu Sep 16, 2021 8:44 am

colinChan008 wrote:cache the object in the Js layer doest not help anything, as our cache will retain the object all the time. as a result, the C++ object will not release.

If the JS code releases the reference then the object should be GC'd and the C++ object will be released. That is up to your JS code (e.g. by assigning null to the variable that stores the natively bound object).
magreenblatt
Site Admin
 
Posts: 12402
Joined: Fri May 29, 2009 6:57 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 36 guests