2121using v8::Array;
2222using v8::Boolean;
2323using v8::Context;
24+ using v8::Data;
2425using v8::EmbedderGraph;
2526using v8::EscapableHandleScope;
2627using v8::FunctionCallbackInfo;
@@ -50,42 +51,35 @@ class JSGraphJSNode : public EmbedderGraph::Node {
5051 const char * Name () override { return " <JS Node>" ; }
5152 size_t SizeInBytes () override { return 0 ; }
5253 bool IsEmbedderNode () override { return false ; }
53- Local<Value> JSValue () { return PersistentToLocal::Strong (persistent_); }
54+ Local<Data> V8Value () { return PersistentToLocal::Strong (persistent_); }
5455
55- int IdentityHash () {
56- Local<Value> v = JSValue ();
57- if (v->IsObject ()) return v.As <Object>()->GetIdentityHash ();
58- if (v->IsName ()) return v.As <v8::Name>()->GetIdentityHash ();
59- if (v->IsInt32 ()) return v.As <v8::Int32>()->Value ();
60- return 0 ;
61- }
62-
63- JSGraphJSNode (Isolate* isolate, Local<Value> val)
64- : persistent_(isolate, val) {
56+ JSGraphJSNode (Isolate* isolate, Local<Data> val) : persistent_(isolate, val) {
6557 CHECK (!val.IsEmpty ());
6658 }
6759
68- struct Hash {
69- inline size_t operator ()(JSGraphJSNode* n) const {
70- return static_cast <size_t >(n->IdentityHash ());
71- }
72- };
73-
7460 struct Equal {
7561 inline bool operator ()(JSGraphJSNode* a, JSGraphJSNode* b) const {
76- return a->JSValue ()->SameValue (b->JSValue ());
62+ Local<Data> data_a = a->V8Value ();
63+ Local<Data> data_b = a->V8Value ();
64+ if (data_a->IsValue ()) {
65+ if (!data_b->IsValue ()) {
66+ return false ;
67+ }
68+ return data_a.As <Value>()->SameValue (data_b.As <Value>());
69+ }
70+ return data_a == data_b;
7771 }
7872 };
7973
8074 private:
81- Global<Value > persistent_;
75+ Global<Data > persistent_;
8276};
8377
8478class JSGraph : public EmbedderGraph {
8579 public:
8680 explicit JSGraph (Isolate* isolate) : isolate_(isolate) {}
8781
88- Node* V8Node (const Local<Value >& value) override {
82+ Node* V8Node (const Local<v8::Data >& value) override {
8983 std::unique_ptr<JSGraphJSNode> n { new JSGraphJSNode (isolate_, value) };
9084 auto it = engine_nodes_.find (n.get ());
9185 if (it != engine_nodes_.end ())
@@ -94,6 +88,10 @@ class JSGraph : public EmbedderGraph {
9488 return AddNode (std::unique_ptr<Node>(n.release ()));
9589 }
9690
91+ Node* V8Node (const Local<v8::Value>& value) override {
92+ return V8Node (value.As <v8::Data>());
93+ }
94+
9795 Node* AddNode (std::unique_ptr<Node> node) override {
9896 Node* n = node.get ();
9997 nodes_.emplace (std::move (node));
@@ -154,8 +152,9 @@ class JSGraph : public EmbedderGraph {
154152 if (nodes->Set (context, i++, obj).IsNothing ())
155153 return MaybeLocal<Array>();
156154 if (!n->IsEmbedderNode ()) {
157- value = static_cast <JSGraphJSNode*>(n.get ())->JSValue ();
158- if (obj->Set (context, value_string, value).IsNothing ())
155+ Local<Data> data = static_cast <JSGraphJSNode*>(n.get ())->V8Value ();
156+ if (data->IsValue () &&
157+ obj->Set (context, value_string, data.As <Value>()).IsNothing ())
159158 return MaybeLocal<Array>();
160159 }
161160 }
@@ -207,8 +206,7 @@ class JSGraph : public EmbedderGraph {
207206 private:
208207 Isolate* isolate_;
209208 std::unordered_set<std::unique_ptr<Node>> nodes_;
210- std::unordered_set<JSGraphJSNode*, JSGraphJSNode::Hash, JSGraphJSNode::Equal>
211- engine_nodes_;
209+ std::set<JSGraphJSNode*, JSGraphJSNode::Equal> engine_nodes_;
212210 std::unordered_map<Node*, std::set<std::pair<const char *, Node*>>> edges_;
213211};
214212
0 commit comments