forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_node.cc
207 lines (186 loc) · 7.67 KB
/
graph_node.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "graph_node.h"
#include "graph_edge.h"
#include "graph_path.h"
using namespace v8;
namespace nodex {
Persistent<ObjectTemplate> GraphNode::node_template_;
void GraphNode::Initialize() {
node_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
node_template_->SetInternalFieldCount(1);
node_template_->SetAccessor(String::New("type"), GraphNode::GetType);
node_template_->SetAccessor(String::New("name"), GraphNode::GetName);
node_template_->SetAccessor(String::New("id"), GraphNode::GetId);
node_template_->SetAccessor(String::New("ptr"), GraphNode::GetPtr);
node_template_->SetAccessor(String::New("instancesCount"), GraphNode::GetInstancesCount);
node_template_->SetAccessor(String::New("childrenCount"), GraphNode::GetChildrenCount);
node_template_->SetAccessor(String::New("retainersCount"), GraphNode::GetRetainersCount);
node_template_->SetAccessor(String::New("size"), GraphNode::GetSize);
node_template_->SetAccessor(String::New("retainingPathsCount"), GraphNode::GetRetainingPathsCount);
node_template_->SetAccessor(String::New("dominatorNode"), GraphNode::GetDominator);
node_template_->Set(String::New("getChild"), FunctionTemplate::New(GraphNode::GetChild));
node_template_->Set(String::New("retainedSize"), FunctionTemplate::New(GraphNode::GetRetainedSize));
node_template_->Set(String::New("getRetainer"), FunctionTemplate::New(GraphNode::GetRetainer));
node_template_->Set(String::New("getRetainingPath"), FunctionTemplate::New(GraphNode::GetRetainingPath));
}
Handle<Value> GraphNode::GetType(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t type = static_cast<int32_t>(static_cast<HeapGraphNode*>(ptr)->GetType());
Local<String> t;
switch(type) {
case 1: //HeapGraphNode::kArray :
t = String::New("Array");
break;
case 2: //HeapGraphNode::kString :
t = String::New("String");
break;
case 3: //HeapGraphNode::kObject :
t = String::New("Object");
break;
case 4: //HeapGraphNode::kCode :
t = String::New("Code");
break;
case 5: //HeapGraphNode::kClosure :
t = String::New("Closure");
break;
case 6: //HeapGraphNode::kRegExp :
t = String::New("RegExp");
break;
case 7: //HeapGraphNode::kHeapNumber :
t = String::New("HeapNumber");
break;
default:
t = String::New("Hidden");
break;
}
return scope.Close(t);
}
Handle<Value> GraphNode::GetName(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
Handle<String> title = static_cast<HeapGraphNode*>(ptr)->GetName();
return scope.Close(title);
}
Handle<Value> GraphNode::GetId(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
uint64_t id = static_cast<HeapGraphNode*>(ptr)->GetId();
return scope.Close(Integer::NewFromUnsigned(id));
}
Handle<Value> GraphNode::GetPtr(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
uint64_t id = reinterpret_cast<uint64_t>(ptr);
return scope.Close(Integer::NewFromUnsigned(id));
}
Handle<Value> GraphNode::GetInstancesCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphNode*>(ptr)->GetInstancesCount();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphNode::GetChildrenCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphNode*>(ptr)->GetChildrenCount();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphNode::GetRetainersCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphNode*>(ptr)->GetRetainersCount();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphNode::GetSize(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphNode*>(ptr)->GetSelfSize();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphNode::GetChild(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::Error(String::New("No index specified")));
} else if (!args[0]->IsInt32()) {
return ThrowException(Exception::Error(String::New("Argument must be integer")));
}
int32_t index = args[0]->Int32Value();
Handle<Object> self = args.This();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphEdge* edge = static_cast<HeapGraphNode*>(ptr)->GetChild(index);
return scope.Close(GraphEdge::New(edge));
}
Handle<Value> GraphNode::GetRetainedSize(const Arguments& args) {
HandleScope scope;
bool exact = false;
if (args.Length() > 0) {
exact = args[0]->BooleanValue();
}
Handle<Object> self = args.This();
void* ptr = self->GetPointerFromInternalField(0);
int32_t size = static_cast<HeapGraphNode*>(ptr)->GetRetainedSize(exact);
return scope.Close(Integer::New(size));
}
Handle<Value> GraphNode::GetRetainer(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::Error(String::New("No index specified")));
} else if (!args[0]->IsInt32()) {
return ThrowException(Exception::Error(String::New("Argument must be integer")));
}
int32_t index = args[0]->Int32Value();
Handle<Object> self = args.This();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphEdge* edge = static_cast<HeapGraphNode*>(ptr)->GetRetainer(index);
return scope.Close(GraphEdge::New(edge));
}
Handle<Value> GraphNode::GetRetainingPathsCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphNode*>(ptr)->GetRetainingPathsCount();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphNode::GetRetainingPath(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
return ThrowException(Exception::Error(String::New("No index specified")));
} else if (!args[0]->IsInt32()) {
return ThrowException(Exception::Error(String::New("Argument must be integer")));
}
int32_t index = args[0]->Int32Value();
Handle<Object> self = args.This();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphPath* path = static_cast<HeapGraphNode*>(ptr)->GetRetainingPath(index);
return scope.Close(GraphPath::New(path));
}
Handle<Value> GraphNode::GetDominator(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphNode* node = static_cast<HeapGraphNode*>(ptr)->GetDominatorNode();
return scope.Close(GraphNode::New(node));
}
Handle<Value> GraphNode::New(const HeapGraphNode* node) {
HandleScope scope;
if (node_template_.IsEmpty()) {
GraphNode::Initialize();
}
if(!node) {
return Undefined();
}
else {
Local<Object> obj = node_template_->NewInstance();
obj->SetPointerInInternalField(0, const_cast<HeapGraphNode*>(node));
return scope.Close(obj);
}
}
}