Skip to content

Commit

Permalink
Merge pull request #11 from hyj1991/long2int
Browse files Browse the repository at this point in the history
Long2int
  • Loading branch information
hyj1991 authored Apr 24, 2018
2 parents 1abb0f0 + cdeb787 commit b21715e
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 146 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "devtoolx",
"version": "0.1.8",
"version": "0.1.9",
"description": "dev tools box",
"main": "devtoolx.js",
"bin": {
Expand Down
22 changes: 11 additions & 11 deletions src/memory/edge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace snapshot_edge {
Edge::Edge(snapshot_parser::SnapshotParser* parser): parser_(parser) {}

std::string Edge::GetType(long id, bool source) {
std::string Edge::GetType(int id, bool source) {
int edge_field_length = parser_->edge_field_length;
if(source && id % edge_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge source id is wrong!").ToLocalChecked());
return "error";
}
long edge_source_index = source ? id : id * edge_field_length;
int edge_source_index = source ? id : id * edge_field_length;
if(edge_source_index / edge_field_length >= parser_->edge_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge id larger than edges.length!").ToLocalChecked());
return "error";
Expand All @@ -21,33 +21,33 @@ std::string Edge::GetType(long id, bool source) {
return types[type];
}

int Edge::GetTypeForInt(long id, bool source) {
int Edge::GetTypeForInt(int id, bool source) {
int edge_field_length = parser_->edge_field_length;
if(source && id % edge_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge source id is wrong!").ToLocalChecked());
return -1;
}
long edge_source_index = source ? id : id * edge_field_length;
int edge_source_index = source ? id : id * edge_field_length;
if(edge_source_index / edge_field_length >= parser_->edge_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge id larger than edges.length!").ToLocalChecked());
return -1;
}
return static_cast<int>(parser_->edges[edge_source_index + parser_->edge_type_offset]);
}

std::string Edge::GetNameOrIndex(long id, bool source) {
std::string Edge::GetNameOrIndex(int id, bool source) {
int edge_field_length = parser_->edge_field_length;
if(source && id % edge_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge source id is wrong!").ToLocalChecked());
return "error";
}
long edge_source_index = source ? id : id * edge_field_length;
int edge_source_index = source ? id : id * edge_field_length;
if(edge_source_index / edge_field_length >= parser_->edge_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge id larger than edges.length!").ToLocalChecked());
return "error";
}
int type = static_cast<int>(parser_->edges[edge_source_index + parser_->edge_type_offset]);
long name_or_index = static_cast<long>(parser_->edges[edge_source_index + parser_->edge_name_or_index_offset]);
int name_or_index = static_cast<int>(parser_->edges[edge_source_index + parser_->edge_name_or_index_offset]);
if(type == KELEMENT) {
return "[" + std::to_string(name_or_index) + "]";
} else if(type == KHIDDEN) {
Expand All @@ -57,23 +57,23 @@ std::string Edge::GetNameOrIndex(long id, bool source) {
};
}

long Edge::GetTargetNode(long id, bool source) {
int Edge::GetTargetNode(int id, bool source) {
int edge_field_length = parser_->edge_field_length;
if(source && id % edge_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge source id is wrong!").ToLocalChecked());
return -1;
}
long edge_source_index = source ? id : id * edge_field_length;
int edge_source_index = source ? id : id * edge_field_length;
if(edge_source_index / edge_field_length >= parser_->edge_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("edge id larger than edges.length!").ToLocalChecked());
return -1;
}
long target_node_source_id = static_cast<long>(parser_->edges[edge_source_index + parser_->edge_to_node_offset]);
int target_node_source_id = static_cast<int>(parser_->edges[edge_source_index + parser_->edge_to_node_offset]);
int node_field_length = parser_->node_field_length;
if(target_node_source_id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("target node source id is wrong!").ToLocalChecked());
return -1;
}
return static_cast<long>(target_node_source_id / node_field_length);
return static_cast<int>(target_node_source_id / node_field_length);
}
}
8 changes: 4 additions & 4 deletions src/memory/edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class Edge {
public:
explicit Edge(snapshot_parser::SnapshotParser* parser);
~Edge();
std::string GetType(long id, bool source);
int GetTypeForInt(long id, bool source);
std::string GetNameOrIndex(long id, bool source);
long GetTargetNode(long id, bool source);
std::string GetType(int id, bool source);
int GetTypeForInt(int id, bool source);
std::string GetNameOrIndex(int id, bool source);
int GetTargetNode(int id, bool source);
private:
snapshot_parser::SnapshotParser* parser_;
};
Expand Down
56 changes: 28 additions & 28 deletions src/memory/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
namespace snapshot_node {
Node::Node(snapshot_parser::SnapshotParser* parser): parser_(parser) {}

long Node::GetNodeId(long source) {
int Node::GetNodeId(int source) {
int node_field_length = parser_->node_field_length;
if(source % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
return static_cast<long>(source / node_field_length);
return static_cast<int>(source / node_field_length);
}

long Node::GetAddress(long id, bool source) {
long Node::GetAddress(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return -1;
}
return static_cast<long>(parser_->nodes[node_source_index + parser_->node_address_offset]);
}

std::string Node::GetType(long id, bool source) {
std::string Node::GetType(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return "error";
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return "error";
Expand All @@ -48,21 +48,21 @@ std::string Node::GetType(long id, bool source) {
return types[type];
}

int Node::GetTypeForInt(long id, bool source) {
int Node::GetTypeForInt(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return -1;
}
return static_cast<int>(parser_->nodes[node_source_index + parser_->node_type_offset]);
}

std::string Node::GetName(long id, bool source) {
std::string Node::GetName(int id, bool source) {
if(id == parser_->root_index) {
return "SYNTTETICROOT";
}
Expand All @@ -71,62 +71,62 @@ std::string Node::GetName(long id, bool source) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return "error";
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return "error";
}
json strings = parser_->strings;
long name = static_cast<long>(parser_->nodes[node_source_index + parser_->node_name_offset]);
int name = static_cast<int>(parser_->nodes[node_source_index + parser_->node_name_offset]);
return strings[name];
}

long Node::GetNameForLong(long id, bool source) {
int Node::GetNameForInt(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return -1;
}
return static_cast<long>(parser_->nodes[node_source_index + parser_->node_name_offset]);
return static_cast<int>(parser_->nodes[node_source_index + parser_->node_name_offset]);
}

long* Node::GetEdges(long id, bool source) {
int* Node::GetEdges(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return new long[0];
return new int[0];
}
long node_ordinal_index = source ? id / node_field_length : id;
int node_ordinal_index = source ? id / node_field_length : id;
if(node_ordinal_index >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return new long[0];
return new int[0];
}
long first_edge_index = parser_->first_edge_indexes[node_ordinal_index];
long next_first_edge_index = 0;
int first_edge_index = parser_->first_edge_indexes[node_ordinal_index];
int next_first_edge_index = 0;
if(node_ordinal_index + 1 >= parser_->node_count) {
next_first_edge_index = static_cast<long>(parser_->edges.size());
next_first_edge_index = static_cast<int>(parser_->edges.size());
} else {
next_first_edge_index = parser_->first_edge_indexes[node_ordinal_index + 1];
}
long* edges = new long[(next_first_edge_index - first_edge_index) / parser_->edge_field_length];
for (long i = first_edge_index; i < next_first_edge_index; i += parser_->edge_field_length) {
int* edges = new int[(next_first_edge_index - first_edge_index) / parser_->edge_field_length];
for (int i = first_edge_index; i < next_first_edge_index; i += parser_->edge_field_length) {
edges[(i - first_edge_index) / parser_->edge_field_length] = i;
}
return edges;
}

int Node::GetEdgeCount(long id, bool source) {
int Node::GetEdgeCount(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return -1;
Expand All @@ -135,17 +135,17 @@ int Node::GetEdgeCount(long id, bool source) {
return static_cast<int>(parser_->nodes[node_source_index + parser_->node_edge_count_offset]);
}

long Node::GetSelfSize(long id, bool source) {
int Node::GetSelfSize(int id, bool source) {
int node_field_length = parser_->node_field_length;
if(source && id % node_field_length != 0) {
Nan::ThrowTypeError(Nan::New<v8::String>("node source id is wrong!").ToLocalChecked());
return -1;
}
long node_source_index = source ? id : id * node_field_length;
int node_source_index = source ? id : id * node_field_length;
if(node_source_index / node_field_length >= parser_->node_count) {
Nan::ThrowTypeError(Nan::New<v8::String>("node id larger than nodes.length!").ToLocalChecked());
return -1;
}
return static_cast<long>(parser_->nodes[node_source_index + parser_->node_self_size_offset]);
return static_cast<int>(parser_->nodes[node_source_index + parser_->node_self_size_offset]);
}
}
18 changes: 9 additions & 9 deletions src/memory/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class Node {
public:
explicit Node(snapshot_parser::SnapshotParser* parser);
~Node();
long GetNodeId(long source );
long GetAddress(long id, bool source);
std::string GetType(long id, bool source);
int GetTypeForInt(long id, bool source);
std::string GetName(long id, bool source);
long GetNameForLong(long id, bool source);
long* GetEdges(long id, bool source);
int GetEdgeCount(long id, bool source);
long GetSelfSize(long id, bool source);
int GetNodeId(int source );
long GetAddress(int id, bool source);
std::string GetType(int id, bool source);
int GetTypeForInt(int id, bool source);
std::string GetName(int id, bool source);
int GetNameForInt(int id, bool source);
int* GetEdges(int id, bool source);
int GetEdgeCount(int id, bool source);
int GetSelfSize(int id, bool source);
private:
snapshot_parser::SnapshotParser* parser_;
};
Expand Down
24 changes: 12 additions & 12 deletions src/memory/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void Parser::Parse(const Nan::FunctionCallbackInfo<Value>& info) {
}
}

Local<Object> Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes get_node_type) {
Local<Object> Parser::GetNodeById_(int id, int current, int limit, GetNodeTypes get_node_type) {
Local<Object> node = Nan::New<Object>();
node->Set(Nan::New<String>("id").ToLocalChecked(), Nan::New<Number>(id));
std::string type = snapshot_parser->node_util->GetType(id, false);
Expand All @@ -92,15 +92,15 @@ Local<Object> Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes
node->Set(Nan::New<String>("name").ToLocalChecked(), Nan::New<String>(name).ToLocalChecked());
std::string address = "@" + std::to_string(snapshot_parser->node_util->GetAddress(id, false));
node->Set(Nan::New<String>("address").ToLocalChecked(), Nan::New<String>(address).ToLocalChecked());
long self_size = snapshot_parser->node_util->GetSelfSize(id, false);
int self_size = snapshot_parser->node_util->GetSelfSize(id, false);
node->Set(Nan::New<String>("self_size").ToLocalChecked(), Nan::New<Number>(self_size));
int distance = snapshot_parser->GetDistance(id);
node->Set(Nan::New<String>("distance").ToLocalChecked(), Nan::New<Number>(distance));
bool is_gcroot = snapshot_parser->IsGCRoot(id);
node->Set(Nan::New<String>("is_gcroot").ToLocalChecked(), Nan::New<Number>(is_gcroot));
// get edges
if(get_node_type == KALL || get_node_type == KEDGES) {
long* edges_local = snapshot_parser->node_util->GetEdges(id, false);
int* edges_local = snapshot_parser->node_util->GetEdges(id, false);
int edges_length = snapshot_parser->node_util->GetEdgeCount(id, false);
int start_edge_index = current;
int stop_edge_index = current + limit;
Expand All @@ -115,7 +115,7 @@ Local<Object> Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes
Local<Object> edge = Nan::New<Object>();
std::string edge_type = snapshot_parser->edge_util->GetType(edges_local[i], true);
std::string name_or_index = snapshot_parser->edge_util->GetNameOrIndex(edges_local[i], true);
long to_node = snapshot_parser->edge_util->GetTargetNode(edges_local[i], true);
int to_node = snapshot_parser->edge_util->GetTargetNode(edges_local[i], true);
edge->Set(Nan::New<String>("type").ToLocalChecked(), Nan::New<String>(edge_type).ToLocalChecked());
edge->Set(Nan::New<String>("name_or_index").ToLocalChecked(), Nan::New<String>(name_or_index).ToLocalChecked());
edge->Set(Nan::New<String>("to_node").ToLocalChecked(), Nan::New<Number>(to_node));
Expand All @@ -132,7 +132,7 @@ Local<Object> Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes
}
// get retainers
if(get_node_type == KALL || get_node_type == KRETAINERS) {
long* retainers_local = snapshot_parser->GetRetainers(id);
int* retainers_local = snapshot_parser->GetRetainers(id);
int retainers_length = snapshot_parser->GetRetainersCount(id);
int start_retainer_index = current;
int stop_retainer_index = current + limit;
Expand All @@ -144,8 +144,8 @@ Local<Object> Parser::GetNodeById_(long id, int current, int limit, GetNodeTypes
}
Local<Array> retainers = Nan::New<Array>(stop_retainer_index - start_retainer_index);
for(int i = start_retainer_index; i < stop_retainer_index; i++) {
long node = retainers_local[i * 2];
long edge = retainers_local[i * 2 + 1];
int node = retainers_local[i * 2];
int edge = retainers_local[i * 2 + 1];
Local<Object> retainer = Nan::New<Object>();
std::string edge_type = snapshot_parser->edge_util->GetType(edge, true);
std::string name_or_index = snapshot_parser->edge_util->GetNameOrIndex(edge, true);
Expand All @@ -171,9 +171,9 @@ void Parser::GetNodeId(const Nan::FunctionCallbackInfo<Value>& info) {
Nan::ThrowTypeError(Nan::New<String>("argument must be number!").ToLocalChecked());
return;
}
long source = static_cast<long>(info[0]->ToInteger()->Value());
int source = static_cast<int>(info[0]->ToInteger()->Value());
Parser* parser = ObjectWrap::Unwrap<Parser>(info.Holder());
long nodeid = parser->snapshot_parser->node_util->GetNodeId(source);
int nodeid = parser->snapshot_parser->node_util->GetNodeId(source);
info.GetReturnValue().Set(Nan::New<Number>(nodeid));
}

Expand Down Expand Up @@ -210,7 +210,7 @@ void Parser::GetNodeByOrdinalId(const Nan::FunctionCallbackInfo<Value>& info) {
}
int error_id_count = 0;
for(int i = 0; i < length; i++) {
long id = static_cast<long>(list->Get(Nan::New<Number>(i))->ToInteger()->Value());
int id = static_cast<int>(list->Get(Nan::New<Number>(i))->ToInteger()->Value());
if(id >= parser->snapshot_parser->node_count) {
error_id_count++;
break;
Expand Down Expand Up @@ -239,7 +239,7 @@ void Parser::GetNodeByAddress(const Nan::FunctionCallbackInfo<Value>& info) {
Nan::ThrowTypeError(Nan::New<String>("argument 0 must be startwith \"@\"!").ToLocalChecked());
return;
}
long id = parser->snapshot_parser->SearchOrdinalByAddress(atol((*addr) + 1));
int id = parser->snapshot_parser->SearchOrdinalByAddress(atoi((*addr) + 1));
if(id == -1) {
std::string addrs = *addr;
std::string error = "address \"" + addrs + "\" is wrong!";
Expand Down Expand Up @@ -270,7 +270,7 @@ void Parser::GetNodeIdByAddress(const Nan::FunctionCallbackInfo<Value>& info) {
return;
}
Parser* parser = ObjectWrap::Unwrap<Parser>(info.Holder());
long id = parser->snapshot_parser->SearchOrdinalByAddress(atol((*addr) + 1));
int id = parser->snapshot_parser->SearchOrdinalByAddress(atoi((*addr) + 1));
if(id == -1) {
std::string addrs = *addr;
std::string error = "address \"" + addrs + "\" is wrong!";
Expand Down
Loading

0 comments on commit b21715e

Please sign in to comment.