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,18 +51,20 @@ 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
56
int IdentityHash () {
56
- Local<Value> v = JSValue ();
57
+ Local<Data> d = V8Value ();
58
+ // TODO(joyeecheung): return something better?
59
+ if (!d->IsValue ()) return reinterpret_cast <std::uintptr_t >(this );
60
+ Local<Value> v = d.As <Value>();
57
61
if (v->IsObject ()) return v.As <Object>()->GetIdentityHash ();
58
62
if (v->IsName ()) return v.As <v8::Name>()->GetIdentityHash ();
59
63
if (v->IsInt32 ()) return v.As <v8::Int32>()->Value ();
60
64
return 0 ;
61
65
}
62
66
63
- JSGraphJSNode (Isolate* isolate, Local<Value> val)
64
- : persistent_(isolate, val) {
67
+ JSGraphJSNode (Isolate* isolate, Local<Data> val) : persistent_(isolate, val) {
65
68
CHECK (!val.IsEmpty ());
66
69
}
67
70
@@ -73,19 +76,27 @@ class JSGraphJSNode : public EmbedderGraph::Node {
73
76
74
77
struct Equal {
75
78
inline bool operator ()(JSGraphJSNode* a, JSGraphJSNode* b) const {
76
- return a->JSValue ()->SameValue (b->JSValue ());
79
+ Local<Data> data_a = a->V8Value ();
80
+ Local<Data> data_b = a->V8Value ();
81
+ if (data_a->IsValue ()) {
82
+ if (!data_b->IsValue ()) {
83
+ return false ;
84
+ }
85
+ return data_a.As <Value>()->SameValue (data_b.As <Value>());
86
+ }
87
+ return data_a == data_b;
77
88
}
78
89
};
79
90
80
91
private:
81
- Global<Value > persistent_;
92
+ Global<Data > persistent_;
82
93
};
83
94
84
95
class JSGraph : public EmbedderGraph {
85
96
public:
86
97
explicit JSGraph (Isolate* isolate) : isolate_(isolate) {}
87
98
88
- Node* V8Node (const Local<Value >& value) override {
99
+ Node* V8Node (const Local<v8::Data >& value) override {
89
100
std::unique_ptr<JSGraphJSNode> n { new JSGraphJSNode (isolate_, value) };
90
101
auto it = engine_nodes_.find (n.get ());
91
102
if (it != engine_nodes_.end ())
@@ -94,6 +105,10 @@ class JSGraph : public EmbedderGraph {
94
105
return AddNode (std::unique_ptr<Node>(n.release ()));
95
106
}
96
107
108
+ Node* V8Node (const Local<v8::Value>& value) override {
109
+ return V8Node (value.As <v8::Data>());
110
+ }
111
+
97
112
Node* AddNode (std::unique_ptr<Node> node) override {
98
113
Node* n = node.get ();
99
114
nodes_.emplace (std::move (node));
@@ -154,8 +169,9 @@ class JSGraph : public EmbedderGraph {
154
169
if (nodes->Set (context, i++, obj).IsNothing ())
155
170
return MaybeLocal<Array>();
156
171
if (!n->IsEmbedderNode ()) {
157
- value = static_cast <JSGraphJSNode*>(n.get ())->JSValue ();
158
- if (obj->Set (context, value_string, value).IsNothing ())
172
+ Local<Data> data = static_cast <JSGraphJSNode*>(n.get ())->V8Value ();
173
+ if (data->IsValue () &&
174
+ obj->Set (context, value_string, data.As <Value>()).IsNothing ())
159
175
return MaybeLocal<Array>();
160
176
}
161
177
}
0 commit comments