v8::Local<v8::Object> equality check "=="

58 views
Skip to first unread message

Hanyun Tao

unread,
Mar 21, 2017, 2:15:22 PM3/21/17
to v8-users
Hi all,

I'm new to v8 and I want to know more about the equality check (==) between two v8::Local<v8::Object>.

Currently I'm using instrumenting chromium browser. In chromium, registered event listeners are stored inside a map like data structure, and I can use the getListenerObject() method to get the v8::Local<v8::Object> correspond to each event listener, which could be a function reference, or reference to an object with handleEvent property.

My goal is to tell if two different event listener will invoke the same javascript or not. Someone told me that I can do this by comparing two v8::Local<v8::Object> by value. I followed the suggestion and implements a function that returns unique integer ID for unique v8::Local<v8::Object> value. However when I test it on real webpage, I found that almost every event listeners are mapped to the same ID. 

I'm not confident with this result so I want to ask a question here. What does the equality check (==) between two v8::Local<v8::Object> check? Does it implies that the two object are structurally equal (contains the same function/object reference), or it means something else?

Best regards, 
 

Vytautas Antanavicius

unread,
Mar 22, 2017, 8:48:31 AM3/22/17
to v8-users

Jakob Kummerow

unread,
Mar 22, 2017, 9:09:22 AM3/22/17
to v8-users
The == operator on v8::Local compares if two Locals are referring to the same JavaScript object (which can, of course, be a function) by object identity (aka reference). (Structural equality is the opposite!)

Note that two functions can contain the same code, but have different object identity:

function GetFun() {
  return function() { /* do something funny */ }
}
var f1 = GetFun();
var f2 = GetFun();
f1 === f2;  // false!
f1.property = "yay";
f2.property === undefined;  // true!

If you had v8::Locals for f1 and f2, then local1 == local2 would be false.

--
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hanyun Tao

unread,
Mar 22, 2017, 2:34:05 PM3/22/17
to v8-users
Thanks for your answer!

I'm confused about your example, why would the first comparison 'f1==f2' return false if they refer to the same function?

I checked the v8 document and found two member functions from v8 Function class, which are int ScriptID() and int GetIdentityHash().

Could I use them to distinguish two v8::Local<v8::Function>? If two v8::Local<v8::Function> refer to the same function, would this two function return the same value?
  
Best regards!
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.

Ben Noordhuis

unread,
Mar 23, 2017, 9:50:53 AM3/23/17
to v8-users
On Wed, Mar 22, 2017 at 7:34 PM, Hanyun Tao <taoha...@gmail.com> wrote:
> I'm confused about your example, why would the first comparison 'f1==f2'
> return false if they refer to the same function?

Because they refer to different function objects. Setting `f1.x = 42`
won't magically make .x appear on f2.
Reply all
Reply to author
Forward
0 new messages