forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_path.cc
74 lines (63 loc) · 2.49 KB
/
graph_path.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
#include "graph_path.h"
#include "graph_node.h"
#include "graph_edge.h"
using namespace v8;
namespace nodex {
Persistent<ObjectTemplate> GraphPath::path_template_;
void GraphPath::Initialize() {
path_template_ = Persistent<ObjectTemplate>::New(ObjectTemplate::New());
path_template_->SetInternalFieldCount(1);
path_template_->SetAccessor(String::New("edgesCount"), GraphPath::GetEdgesCount);
path_template_->SetAccessor(String::New("from"), GraphPath::GetFromNode);
path_template_->SetAccessor(String::New("to"), GraphPath::GetToNode);
path_template_->Set(String::New("getEdge"), FunctionTemplate::New(GraphPath::GetEdge));
}
Handle<Value> GraphPath::GetEdgesCount(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
int32_t count = static_cast<HeapGraphPath*>(ptr)->GetEdgesCount();
return scope.Close(Integer::New(count));
}
Handle<Value> GraphPath::GetEdge(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<HeapGraphPath*>(ptr)->GetEdge(index);
return scope.Close(GraphEdge::New(edge));
}
Handle<Value> GraphPath::GetFromNode(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphNode* node = static_cast<HeapGraphPath*>(ptr)->GetFromNode();
return scope.Close(GraphNode::New(node));
}
Handle<Value> GraphPath::GetToNode(Local<String> property, const AccessorInfo& info) {
HandleScope scope;
Local<Object> self = info.Holder();
void* ptr = self->GetPointerFromInternalField(0);
const HeapGraphNode* node = static_cast<HeapGraphPath*>(ptr)->GetToNode();
return scope.Close(GraphNode::New(node));
}
Handle<Value> GraphPath::New(const HeapGraphPath* path) {
HandleScope scope;
if (path_template_.IsEmpty()) {
GraphPath::Initialize();
}
if(!path) {
return Undefined();
}
else {
Local<Object> obj = path_template_->NewInstance();
obj->SetPointerInInternalField(0, const_cast<HeapGraphPath*>(path));
return scope.Close(obj);
}
}
}