Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: unstable
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 253 lines (219 sloc) 8.422 kb
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#include "graph_node.h"
#include "graph_edge.h"
#include "node_version.h"

#include <string.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::NewSymbol("type"), GraphNode::GetType);
  node_template_->SetAccessor(String::NewSymbol("name"), GraphNode::GetName);
  node_template_->SetAccessor(String::NewSymbol("id"), GraphNode::GetId);
  node_template_->SetAccessor(String::NewSymbol("ptr"), GraphNode::GetPtr);
  node_template_->SetAccessor(
      String::NewSymbol("childrenCount"), GraphNode::GetChildrenCount);
  node_template_->SetAccessor(
      String::NewSymbol("retainersCount"), GraphNode::GetRetainersCount);
  node_template_->SetAccessor(
      String::NewSymbol("size"), GraphNode::GetSize);
  node_template_->SetAccessor(
      String::NewSymbol("dominatorNode"), GraphNode::GetDominator);
  node_template_->Set(
      String::NewSymbol("getChild"),
      FunctionTemplate::New(GraphNode::GetChild));
  node_template_->Set(
      String::NewSymbol("retainedSize"),
      FunctionTemplate::New(GraphNode::GetRetainedSize));
  node_template_->Set(
      String::NewSymbol("getRetainer"),
      FunctionTemplate::New(GraphNode::GetRetainer));
  node_template_->Set(
      String::NewSymbol("getHeapValue"),
      FunctionTemplate::New(GraphNode::GetHeapValue));
  node_template_->Set(
      String::NewSymbol("getHeapValueSafe"),
      FunctionTemplate::New(GraphNode::GetHeapValueSafe));
}

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::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;

  Handle<Object> self = args.This();
  void* ptr = self->GetPointerFromInternalField(0);

#if NODE_VERSION_AT_LEAST(0, 7, 0)
  int32_t size = static_cast<HeapGraphNode*>(ptr)->GetRetainedSize();
#else
  bool exact = false;
  if (args.Length() > 0) {
    exact = args[0]->BooleanValue();
  }
  int32_t size = static_cast<HeapGraphNode*>(ptr)->GetRetainedSize(exact);
#endif

  return scope.Close(Integer::New(size));
}

Handle<Value> GraphNode::GetHeapValueSafe(const Arguments& args) {
  HandleScope scope;
  Handle<Object> self = args.This();
  HeapGraphNode* node = static_cast<HeapGraphNode*>(self->GetPointerFromInternalField(0));

  Handle<String> title = node->GetName();
  v8::String::AsciiValue str(title);
  char* name = *str;

  if (name[0] == '(') {
    // we assume this is not safe if name begins with a '(' as those
    // objects seem to be system objects and emperically cause a seg
    // fault.
    return scope.Close(v8::Undefined());
  }

  int type = node->GetType();

  switch(type) {
    case HeapGraphNode::kString:
      // special case as some strings seem to cause a seg fault, so return the name
      return scope.Close(title);
      break;

    case HeapGraphNode::kArray:
      // type array with an empty name seems to segfault
      if (strlen(name) == 0) return scope.Close(v8::Undefined());

    case HeapGraphNode::kClosure:
    case HeapGraphNode::kObject:
    case HeapGraphNode::kRegExp:
    case HeapGraphNode::kHeapNumber:
      return scope.Close(node->GetHeapValue());
      break;

    case HeapGraphNode::kNative:
    case HeapGraphNode::kCode:
    default:
      return scope.Close(v8::Undefined());
  }
}


Handle<Value> GraphNode::GetHeapValue(const Arguments& args) {
  HandleScope scope;
  Handle<Object> self = args.This();
  HeapGraphNode* node = static_cast<HeapGraphNode*>(self->GetPointerFromInternalField(0));
  return scope.Close(node->GetHeapValue());
}

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::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);
  }
}
}
Something went wrong with that request. Please try again.