forked from c4milo/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph_node.cc
130 lines (116 loc) · 4.31 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
#include "graph_node.h"
#include "graph_edge.h"
#include "node_version.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("childrenCount"), GraphNode::GetChildrenCount);
node_template_->SetAccessor(String::New("size"), GraphNode::GetSize);
node_template_->Set(String::New("getChild"), FunctionTemplate::New(GraphNode::GetChild));
}
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 HeapGraphNode::kArray :
t = String::New("Array");
break;
case HeapGraphNode::kString :
t = String::New("String");
break;
case HeapGraphNode::kObject :
t = String::New("Object");
break;
case HeapGraphNode::kCode :
t = String::New("Code");
break;
case HeapGraphNode::kClosure :
t = String::New("Closure");
break;
case HeapGraphNode::kRegExp :
t = String::New("RegExp");
break;
case HeapGraphNode::kHeapNumber :
t = String::New("HeapNumber");
break;
case HeapGraphNode::kNative :
t = String::New("Native");
break;
default:
t = String::New("Hidden");
}
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::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::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::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);
}
}
}