Skip to content

Commit 4568df4

Browse files
joyeecheungnodejs-github-bot
authored andcommitted
src: support v8::Data in heap utils
Use a std::set<> for saving the JSGraphJSNode, since implementing a proper hash function for v8::Data is complicated and this path is only used by tests anyway, where the performance difference between std::set and std::unordered_set doesn't matter. PR-URL: #52295 Refs: #40786 Refs: https://docs.google.com/document/d/1ny2Qz_EsUnXGKJRGxoA-FXIE2xpLgaMAN6jD7eAkqFQ/edit Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent e9cd476 commit 4568df4

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/heap_utils.cc

+22-24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using v8::Array;
2222
using v8::Boolean;
2323
using v8::Context;
24+
using v8::Data;
2425
using v8::EmbedderGraph;
2526
using v8::EscapableHandleScope;
2627
using 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

8478
class 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

Comments
 (0)