forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshot.cc
172 lines (135 loc) · 4.77 KB
/
snapshot.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
#include "snapshot.h"
#include "node.h"
#include "node_buffer.h"
using namespace v8;
using namespace node;
namespace nodex {
Persistent<ObjectTemplate> Snapshot::snapshot_template_;
void Snapshot::Initialize() {
snapshot_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
snapshot_template_->SetInternalFieldCount(1);
snapshot_template_->SetAccessor(String::New("title"), Snapshot::GetTitle);
snapshot_template_->SetAccessor(String::New("uid"), Snapshot::GetUid);
snapshot_template_->SetAccessor(String::New("type"), Snapshot::GetType);
snapshot_template_->Set(String::New("delete"), FunctionTemplate::New(Snapshot::Delete));
snapshot_template_->Set(String::New("serialize"), FunctionTemplate::New(Snapshot::Serialize));
}
Handle<Value> Snapshot::GetTitle(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
Handle<String> title = static_cast<HeapSnapshot*>(ptr)->GetTitle();
return scope.Close(title);
}
Handle<Value> Snapshot::GetUid(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
uint32_t uid = static_cast<HeapSnapshot*>(ptr)->GetUid();
return scope.Close(Integer::NewFromUnsigned(uid));
}
Handle<Value> Snapshot::GetType(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
HeapSnapshot::Type type = static_cast<HeapSnapshot*>(ptr)->GetType();
Local<String> t;
switch(type) {
case HeapSnapshot::kFull:
t = String::New("Full");
break;
default:
t = String::New("Unknown");
}
return scope.Close(t);
}
Handle<Value> Snapshot::Delete(const Arguments& args) {
HandleScope scope;
Handle<Object> self = args.This();
void* ptr = self->GetPointerFromInternalField(0);
static_cast<HeapSnapshot*>(ptr)->Delete();
return Undefined();
}
Handle<Value> Snapshot::New(const HeapSnapshot* snapshot) {
HandleScope scope;
if (snapshot_template_.IsEmpty()) {
Snapshot::Initialize();
}
if(!snapshot) {
return Undefined();
}
else {
Local<Object> obj = snapshot_template_->NewInstance();
obj->SetPointerInInternalField(0, const_cast<HeapSnapshot*>(snapshot));
return scope.Close(obj);
}
}
class OutputStreamAdapter : public v8::OutputStream {
public:
OutputStreamAdapter(Handle<Value> arg) {
Local<String> onEnd = String::New("onEnd");
Local<String> onData = String::New("onData");
if (!arg->IsObject()) {
ThrowException(Exception::TypeError(
String::New("You must specify an Object as first argument")));
}
obj = arg->ToObject();
if (!obj->Has(onEnd) || !obj->Has(onData)) {
ThrowException(Exception::TypeError(
String::New("You must specify properties 'onData' and 'onEnd' to invoke this function")));
}
if (!obj->Get(onEnd)->IsFunction() || !obj->Get(onData)->IsFunction()) {
ThrowException(Exception::TypeError(
String::New("Properties 'onData' and 'onEnd' have to be functions")));
}
onEndFunction = Local<Function>::Cast(obj->Get(onEnd));
onDataFunction = Local<Function>::Cast(obj->Get(onData));
abort = Local<Value>::New(Boolean::New(false));
}
void EndOfStream() {
TryCatch try_catch;
onEndFunction->Call(obj, 0, NULL);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
}
int GetChunkSize() {
return 10240;
}
WriteResult WriteAsciiChunk(char* data, int size) {
HandleScope scope;
Handle<Value> argv[2] = {
Buffer::New(data, size)->handle_,
Integer::New(size)
};
TryCatch try_catch;
abort = onDataFunction->Call(obj, 2, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
return kAbort;
}
if (abort.IsEmpty() || !abort->IsBoolean()) {
return kContinue;
}
return abort->IsTrue() ? kAbort : kContinue;
}
private:
Local<Value> abort;
Handle<Object> obj;
Handle<Function> onEndFunction;
Handle<Function> onDataFunction;
};
Handle<Value> Snapshot::Serialize(const Arguments& args) {
HandleScope scope;
Handle<Object> self = args.This();
uint32_t argslen = args.Length();
if (argslen == 0) {
return ThrowException(Exception::TypeError(
String::New("You must specify arguments to invoke this function")));
}
OutputStreamAdapter *stream = new OutputStreamAdapter(args[0]);
void* ptr = self->GetPointerFromInternalField(0);
static_cast<HeapSnapshot*>(ptr)->Serialize(stream, HeapSnapshot::kJSON);
return Undefined();
}
} //namespace nodex