Skip to content

Commit de527a9

Browse files
committed
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.
1 parent 2949154 commit de527a9

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/heap_utils.cc

+21-7
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class JSGraph : public EmbedderGraph {
7676
return n;
7777
}
7878

79-
void AddEdge(Node* from, Node* to) override {
80-
edges_[from].insert(to);
79+
void AddEdge(Node* from, Node* to, const char* name = nullptr) override {
80+
edges_[from].insert(std::make_pair(name, to));
8181
}
8282

8383
MaybeLocal<Array> CreateObject() const {
@@ -92,6 +92,7 @@ class JSGraph : public EmbedderGraph {
9292
Local<String> size_string = FIXED_ONE_BYTE_STRING(isolate_, "size");
9393
Local<String> value_string = FIXED_ONE_BYTE_STRING(isolate_, "value");
9494
Local<String> wraps_string = FIXED_ONE_BYTE_STRING(isolate_, "wraps");
95+
Local<String> to_string = FIXED_ONE_BYTE_STRING(isolate_, "to");
9596

9697
for (const std::unique_ptr<Node>& n : nodes_)
9798
info_objects[n.get()] = Object::New(isolate_);
@@ -141,10 +142,23 @@ class JSGraph : public EmbedderGraph {
141142
}
142143

143144
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()) {
145+
size_t j = 0;
146+
for (const auto& edge : edge_info.second) {
147+
Local<Object> to_object = info_objects[edge.second];
148+
Local<Object> edge_info = Object::New(isolate_);
149+
Local<Value> edge_name_value;
150+
const char* edge_name = edge.first;
151+
if (edge_name != nullptr &&
152+
!String::NewFromUtf8(
153+
isolate_, edge_name, v8::NewStringType::kNormal)
154+
.ToLocal(&edge_name_value)) {
155+
return MaybeLocal<Array>();
156+
} else {
157+
edge_name_value = Number::New(isolate_, j++);
158+
}
159+
if (edge_info->Set(context, name_string, edge_name_value).IsNothing() ||
160+
edge_info->Set(context, to_string, to_object).IsNothing() ||
161+
edges.As<Array>()->Set(context, i++, edge_info).IsNothing()) {
148162
return MaybeLocal<Array>();
149163
}
150164
}
@@ -158,7 +172,7 @@ class JSGraph : public EmbedderGraph {
158172
std::unordered_set<std::unique_ptr<Node>> nodes_;
159173
std::unordered_set<JSGraphJSNode*, JSGraphJSNode::Hash, JSGraphJSNode::Equal>
160174
engine_nodes_;
161-
std::unordered_map<Node*, std::unordered_set<Node*>> edges_;
175+
std::unordered_map<Node*, std::set<std::pair<const char*, Node*>>> edges_;
162176
};
163177

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

test/common/heap.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,19 @@ class State {
4747
else
4848
assert.strictEqual(graph.length, expected.length);
4949
for (const expectedNode of expected) {
50-
if (expectedNode.edges) {
50+
if (expectedNode.children) {
5151
for (const expectedChild of expectedNode.children) {
52-
const check = typeof expectedChild === 'function' ?
53-
expectedChild : (node) => {
54-
return node.name === expectedChild.name ||
55-
(node.value &&
56-
node.value.constructor &&
57-
node.value.constructor.name === expectedChild.name);
58-
};
52+
const check = (edge) => {
53+
// TODO(joyeecheung): check the edge names
54+
const node = edge.to;
55+
if (typeof expectedChild === 'function') {
56+
return expectedChild(node);
57+
}
58+
return node.name === expectedChild.name ||
59+
(node.value &&
60+
node.value.constructor &&
61+
node.value.constructor.name === expectedChild.name);
62+
};
5963

6064
assert(graph.some((node) => node.edges.some(check)),
6165
`expected to find child ${util.inspect(expectedChild)} ` +

0 commit comments

Comments
 (0)