-
Notifications
You must be signed in to change notification settings - Fork 5
/
profile_node.cc
145 lines (130 loc) · 5.81 KB
/
profile_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
#include "profile_node.h"
namespace nodex {
using v8::CpuProfileNode;
using v8::Exception;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Handle;
using v8::HandleScope;
using v8::Integer;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::Persistent;
using v8::PropertyCallbackInfo;
using v8::String;
using v8::ThrowException;
using v8::Undefined;
using v8::Value;
Persistent<ObjectTemplate> ProfileNode::node_template_;
void ProfileNode::Initialize (Isolate* isolate) {
Local<ObjectTemplate> o = ObjectTemplate::New();
o->SetInternalFieldCount(1);
o->SetAccessor(String::New("functionName"), ProfileNode::GetFunctionName);
o->SetAccessor(String::New("scriptName"), ProfileNode::GetScriptName);
o->SetAccessor(String::New("bailoutReason"), ProfileNode::GetBailoutReason);
o->SetAccessor(String::New("lineNumber"), ProfileNode::GetLineNumber);
o->SetAccessor(String::New("callUid"), ProfileNode::GetCallUid);
o->SetAccessor(String::New("id"), ProfileNode::GetNodeId);
o->SetAccessor(String::New("childrenCount"), ProfileNode::GetChildrenCount);
o->SetAccessor(String::New("hitCount"), ProfileNode::GetHitCount);
o->SetAccessor(String::New("scriptId"), ProfileNode::GetScriptId);
o->Set(String::New("getChild"), FunctionTemplate::New(ProfileNode::GetChild));
node_template_.Reset(isolate, o);
}
void ProfileNode::GetFunctionName (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
Handle<String> fname = static_cast<CpuProfileNode*>(ptr)->GetFunctionName();
info.GetReturnValue().Set(fname);
}
void ProfileNode::GetScriptName (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
Handle<String> sname = static_cast<CpuProfileNode*>(ptr)->GetScriptResourceName();
info.GetReturnValue().Set(sname);
}
void ProfileNode::GetBailoutReason (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
const char* breason_ptr = static_cast<CpuProfileNode*>(ptr)->GetBailoutReason();
Handle<String> breason = v8::String::New(breason_ptr);
info.GetReturnValue().Set(breason);
}
void ProfileNode::GetLineNumber (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
int32_t ln = static_cast<CpuProfileNode*>(ptr)->GetLineNumber();
info.GetReturnValue().Set(ln);
}
void ProfileNode::GetCallUid (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
uint32_t uid = static_cast<CpuProfileNode*>(ptr)->GetCallUid();
info.GetReturnValue().Set(uid);
}
void ProfileNode::GetNodeId (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
uint32_t uid = static_cast<CpuProfileNode*>(ptr)->GetNodeId();
info.GetReturnValue().Set(uid);
}
void ProfileNode::GetScriptId (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
int sid = static_cast<CpuProfileNode*>(ptr)->GetScriptId();
info.GetReturnValue().Set(Integer::New(sid));
}
void ProfileNode::GetChildrenCount (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
int32_t count = static_cast<CpuProfileNode*>(ptr)->GetChildrenCount();
info.GetReturnValue().Set(count);
}
void ProfileNode::GetHitCount (Local<String> property, const PropertyCallbackInfo<Value>& info) {
HandleScope scope(info.GetIsolate());
Local<Object> self = info.Holder();
void* ptr = self->GetAlignedPointerFromInternalField(0);
uint32_t hcount = static_cast<CpuProfileNode*>(ptr)->GetHitCount();
info.GetReturnValue().Set(hcount);
}
void ProfileNode::GetChild (const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
if (args.Length() < 1) {
ThrowException(Exception::Error(String::New("No index specified")));
return;
} else if (!args[0]->IsInt32()) {
ThrowException(Exception::Error(String::New("Argument must be integer")));
return;
}
int32_t index = args[0]->Int32Value();
Handle<Object> self = args.This();
void* ptr = self->GetAlignedPointerFromInternalField(0);
const CpuProfileNode* node = static_cast<CpuProfileNode*>(ptr)->GetChild(index);
args.GetReturnValue().Set(ProfileNode::New(isolate, node));
}
Handle<Value> ProfileNode::New (Isolate* isolate, const CpuProfileNode* node) {
if (node_template_.IsEmpty()) {
ProfileNode::Initialize(isolate);
}
if(!node) {
return Undefined();
}
else {
Local<ObjectTemplate> tpl = Local<ObjectTemplate>::New(isolate, node_template_);
Local<Object> obj = tpl->NewInstance();
obj->SetAlignedPointerInInternalField(0, const_cast<CpuProfileNode*>(node));
return obj;
}
}
}