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.

PR-URL: https://github.com/nodejs/node/pull/22106
Refs: 6ee834532d
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2018-08-04 00:09:16 +08:00
parent 54c87f37f4
commit 2d72a9543c
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
2 changed files with 33 additions and 15 deletions

View File

@ -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 {
@ -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_);
@ -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>();
}
}
@ -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) {

View File

@ -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)} ` +