21
21
using v8::Array;
22
22
using v8::Boolean ;
23
23
using v8::Context;
24
+ using v8::Data;
24
25
using v8::EmbedderGraph;
25
26
using v8::EscapableHandleScope;
26
27
using v8::FunctionCallbackInfo;
@@ -50,42 +51,35 @@ class JSGraphJSNode : public EmbedderGraph::Node {
50
51
const char * Name () override { return " <JS Node>" ; }
51
52
size_t SizeInBytes () override { return 0 ; }
52
53
bool IsEmbedderNode () override { return false ; }
53
- Local<Value> JSValue () { return PersistentToLocal::Strong (persistent_); }
54
+ Local<Data> V8Value () { return PersistentToLocal::Strong (persistent_); }
54
55
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) {
65
57
CHECK (!val.IsEmpty ());
66
58
}
67
59
68
- struct Hash {
69
- inline size_t operator ()(JSGraphJSNode* n) const {
70
- return static_cast <size_t >(n->IdentityHash ());
71
- }
72
- };
73
-
74
60
struct Equal {
75
61
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;
77
71
}
78
72
};
79
73
80
74
private:
81
- Global<Value > persistent_;
75
+ Global<Data > persistent_;
82
76
};
83
77
84
78
class JSGraph : public EmbedderGraph {
85
79
public:
86
80
explicit JSGraph (Isolate* isolate) : isolate_(isolate) {}
87
81
88
- Node* V8Node (const Local<Value >& value) override {
82
+ Node* V8Node (const Local<v8::Data >& value) override {
89
83
std::unique_ptr<JSGraphJSNode> n { new JSGraphJSNode (isolate_, value) };
90
84
auto it = engine_nodes_.find (n.get ());
91
85
if (it != engine_nodes_.end ())
@@ -94,6 +88,10 @@ class JSGraph : public EmbedderGraph {
94
88
return AddNode (std::unique_ptr<Node>(n.release ()));
95
89
}
96
90
91
+ Node* V8Node (const Local<v8::Value>& value) override {
92
+ return V8Node (value.As <v8::Data>());
93
+ }
94
+
97
95
Node* AddNode (std::unique_ptr<Node> node) override {
98
96
Node* n = node.get ();
99
97
nodes_.emplace (std::move (node));
@@ -154,8 +152,9 @@ class JSGraph : public EmbedderGraph {
154
152
if (nodes->Set (context, i++, obj).IsNothing ())
155
153
return MaybeLocal<Array>();
156
154
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 ())
159
158
return MaybeLocal<Array>();
160
159
}
161
160
}
@@ -207,8 +206,7 @@ class JSGraph : public EmbedderGraph {
207
206
private:
208
207
Isolate* isolate_;
209
208
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_;
212
210
std::unordered_map<Node*, std::set<std::pair<const char *, Node*>>> edges_;
213
211
};
214
212
0 commit comments