Skip to content

Commit

Permalink
src: implement the new EmbedderGraph::AddEdge()
Browse files Browse the repository at this point in the history
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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Oct 7, 2018
1 parent 5a83967 commit 390fc85
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
33 changes: 27 additions & 6 deletions src/heap_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ class JSGraph : public EmbedderGraph {
}

void AddEdge(Node* from, Node* to) override {
edges_[from].insert(to);
edges_[from].insert(std::make_pair(nullptr, to));
}

// For ABI compatibility, we did not backport the virtual function
// AddEdge() with the name as last argument back to v10.x.
// This is only here to reduce the amount of churn.
void AddEdge(Node* from, Node* to, const char* name = nullptr) {
edges_[from].insert(std::make_pair(name, to));
}

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

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

size_t i = 0;
for (Node* target : edge_info.second) {
if (edges.As<Array>()->Set(context,
i++,
info_objects[target]).IsNothing()) {
size_t j = 0;
for (const auto& edge : edge_info.second) {
Local<Object> to_object = info_objects[edge.second];
Local<Object> edge_info = Object::New(isolate_);
Local<Value> edge_name_value;
const char* edge_name = edge.first;
if (edge_name != nullptr &&
!String::NewFromUtf8(
isolate_, edge_name, v8::NewStringType::kNormal)
.ToLocal(&edge_name_value)) {
return MaybeLocal<Array>();
} else {
edge_name_value = Number::New(isolate_, j++);
}
if (edge_info->Set(context, name_string, edge_name_value).IsNothing() ||
edge_info->Set(context, to_string, to_object).IsNothing() ||
edges.As<Array>()->Set(context, i++, edge_info).IsNothing()) {
return MaybeLocal<Array>();
}
}
Expand All @@ -158,7 +179,7 @@ class JSGraph : public EmbedderGraph {
std::unordered_set<std::unique_ptr<Node>> nodes_;
std::unordered_set<JSGraphJSNode*, JSGraphJSNode::Hash, JSGraphJSNode::Equal>
engine_nodes_;
std::unordered_map<Node*, std::unordered_set<Node*>> edges_;
std::unordered_map<Node*, std::set<std::pair<const char*, Node*>>> edges_;
};

void BuildEmbedderGraph(const FunctionCallbackInfo<Value>& args) {
Expand Down
20 changes: 12 additions & 8 deletions test/common/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ class State {
else
assert.strictEqual(graph.length, expected.length);
for (const expectedNode of expected) {
if (expectedNode.edges) {
if (expectedNode.children) {
for (const expectedChild of expectedNode.children) {
const check = typeof expectedChild === 'function' ?
expectedChild : (node) => {
return node.name === expectedChild.name ||
(node.value &&
node.value.constructor &&
node.value.constructor.name === expectedChild.name);
};
const check = (edge) => {
// TODO(joyeecheung): check the edge names
const node = edge.to;
if (typeof expectedChild === 'function') {
return expectedChild(node);
}
return node.name === expectedChild.name ||
(node.value &&
node.value.constructor &&
node.value.constructor.name === expectedChild.name);
};

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

0 comments on commit 390fc85

Please sign in to comment.