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.

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 committed Aug 18, 2018
1 parent 54c87f3 commit 2d72a95
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
28 changes: 21 additions & 7 deletions src/heap_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class JSGraph : public EmbedderGraph {
return n;
}

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

MaybeLocal<Array> CreateObject() const {
Expand All @@ -92,6 +92,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 +142,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 +172,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 @@ -47,15 +47,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);
};

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

0 comments on commit 2d72a95

Please sign in to comment.