Skip to content

Commit 390fc85

Browse files
joyeecheungtargos
authored andcommitted
src: implement the new EmbedderGraph::AddEdge()
The signature of EmbedderGraph::AddEdge() has been changed so the current implementation of JSGraph no longer compiles. This patch updates the implementation accordingly. Backport-PR-URL: #23295 PR-URL: #22106 Refs: v8/v8@6ee8345 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5a83967 commit 390fc85

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/heap_utils.cc

+27-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ class JSGraph : public EmbedderGraph {
7777
}
7878

7979
void AddEdge(Node* from, Node* to) override {
80-
edges_[from].insert(to);
80+
edges_[from].insert(std::make_pair(nullptr, to));
81+
}
82+
83+
// For ABI compatibility, we did not backport the virtual function
84+
// AddEdge() with the name as last argument back to v10.x.
85+
// This is only here to reduce the amount of churn.
86+
void AddEdge(Node* from, Node* to, const char* name = nullptr) {
87+
edges_[from].insert(std::make_pair(name, to));
8188
}
8289

8390
MaybeLocal<Array> CreateObject() const {
@@ -92,6 +99,7 @@ class JSGraph : public EmbedderGraph {
9299
Local<String> size_string = FIXED_ONE_BYTE_STRING(isolate_, "size");
93100
Local<String> value_string = FIXED_ONE_BYTE_STRING(isolate_, "value");
94101
Local<String> wraps_string = FIXED_ONE_BYTE_STRING(isolate_, "wraps");
102+
Local<String> to_string = FIXED_ONE_BYTE_STRING(isolate_, "to");
95103

96104
for (const std::unique_ptr<Node>& n : nodes_)
97105
info_objects[n.get()] = Object::New(isolate_);
@@ -141,10 +149,23 @@ class JSGraph : public EmbedderGraph {
141149
}
142150

143151
size_t i = 0;
144-
for (Node* target : edge_info.second) {
145-
if (edges.As<Array>()->Set(context,
146-
i++,
147-
info_objects[target]).IsNothing()) {
152+
size_t j = 0;
153+
for (const auto& edge : edge_info.second) {
154+
Local<Object> to_object = info_objects[edge.second];
155+
Local<Object> edge_info = Object::New(isolate_);
156+
Local<Value> edge_name_value;
157+
const char* edge_name = edge.first;
158+
if (edge_name != nullptr &&
159+
!String::NewFromUtf8(
160+
isolate_, edge_name, v8::NewStringType::kNormal)
161+
.ToLocal(&edge_name_value)) {
162+
return MaybeLocal<Array>();
163+
} else {
164+
edge_name_value = Number::New(isolate_, j++);
165+
}
166+
if (edge_info->Set(context, name_string, edge_name_value).IsNothing() ||
167+
edge_info->Set(context, to_string, to_object).IsNothing() ||
168+
edges.As<Array>()->Set(context, i++, edge_info).IsNothing()) {
148169
return MaybeLocal<Array>();
149170
}
150171
}
@@ -158,7 +179,7 @@ class JSGraph : public EmbedderGraph {
158179
std::unordered_set<std::unique_ptr<Node>> nodes_;
159180
std::unordered_set<JSGraphJSNode*, JSGraphJSNode::Hash, JSGraphJSNode::Equal>
160181
engine_nodes_;
161-
std::unordered_map<Node*, std::unordered_set<Node*>> edges_;
182+
std::unordered_map<Node*, std::set<std::pair<const char*, Node*>>> edges_;
162183
};
163184

164185
void BuildEmbedderGraph(const FunctionCallbackInfo<Value>& args) {

test/common/heap.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,19 @@ class State {
5454
else
5555
assert.strictEqual(graph.length, expected.length);
5656
for (const expectedNode of expected) {
57-
if (expectedNode.edges) {
57+
if (expectedNode.children) {
5858
for (const expectedChild of expectedNode.children) {
59-
const check = typeof expectedChild === 'function' ?
60-
expectedChild : (node) => {
61-
return node.name === expectedChild.name ||
62-
(node.value &&
63-
node.value.constructor &&
64-
node.value.constructor.name === expectedChild.name);
65-
};
59+
const check = (edge) => {
60+
// TODO(joyeecheung): check the edge names
61+
const node = edge.to;
62+
if (typeof expectedChild === 'function') {
63+
return expectedChild(node);
64+
}
65+
return node.name === expectedChild.name ||
66+
(node.value &&
67+
node.value.constructor &&
68+
node.value.constructor.name === expectedChild.name);
69+
};
6670

6771
// Don't use assert with a custom message here. Otherwise the
6872
// inspection in the message is done eagerly and wastes a lot of CPU

0 commit comments

Comments
 (0)