Browse files

Implements Snapshot::Serialize

  • Loading branch information...
1 parent b995a4b commit 3523d2df9d0e65698219307083bc03264e52f46e @c4milo committed Apr 1, 2012
Showing with 138 additions and 50 deletions.
  1. +116 −33 snapshot.cc
  2. +22 −17 snapshot.h
View
149 snapshot.cc
@@ -1,10 +1,12 @@
#include "snapshot.h"
#include "graph_node.h"
+#include "node.h"
using namespace v8;
+using namespace node;
namespace nodex {
-
+
Persistent<ObjectTemplate> Snapshot::snapshot_template_;
void Snapshot::Initialize() {
@@ -18,7 +20,7 @@ void Snapshot::Initialize() {
snapshot_template_->Set(String::New("getNodeById"), FunctionTemplate::New(Snapshot::GetNodeById));
snapshot_template_->Set(String::New("getNode"), FunctionTemplate::New(Snapshot::GetNode));
snapshot_template_->Set(String::New("delete"), FunctionTemplate::New(Snapshot::Delete));
- //TODO: serialize
+ snapshot_template_->Set(String::New("serialize"), FunctionTemplate::New(Snapshot::Serialize));
}
Handle<Value> Snapshot::GetUid(Local<String> property, const AccessorInfo& info) {
@@ -64,47 +66,47 @@ Handle<Value> Snapshot::GetType(Local<String> property, const AccessorInfo& info
}
Handle<Value> Snapshot::GetNodesCount(Local<String> property, const AccessorInfo& info) {
- HandleScope scope;
- Local<Object> self = info.Holder();
- void* ptr = self->GetPointerFromInternalField(0);
- Local<Integer> count = Integer::New(static_cast<HeapSnapshot*>(ptr)->GetNodesCount());
+ HandleScope scope;
+ Local<Object> self = info.Holder();
+ void* ptr = self->GetPointerFromInternalField(0);
+ Local<Integer> count = Integer::New(static_cast<HeapSnapshot*>(ptr)->GetNodesCount());
return scope.Close(count);
}
Handle<Value> Snapshot::GetNodeById(const Arguments& args) {
- HandleScope scope;
- if (args.Length() < 1) {
- return ThrowException(Exception::Error(String::New("No index specified")));
- } else if (!args[0]->IsUint32()) {
- return ThrowException(Exception::Error(String::New("Argument must be integer")));
- }
- uint64_t id = static_cast<uint64_t>(args[0]->Uint32Value());
- Handle<Object> self = args.This();
- void* ptr = self->GetPointerFromInternalField(0);
- const HeapGraphNode* node = static_cast<HeapSnapshot*>(ptr)->GetNodeById(id);
- return scope.Close(GraphNode::New(node));
+ HandleScope scope;
+ if (args.Length() < 1) {
+ return ThrowException(Exception::Error(String::New("No index specified")));
+ } else if (!args[0]->IsUint32()) {
+ return ThrowException(Exception::Error(String::New("Argument must be integer")));
+ }
+ uint64_t id = static_cast<uint64_t>(args[0]->Uint32Value());
+ Handle<Object> self = args.This();
+ void* ptr = self->GetPointerFromInternalField(0);
+ const HeapGraphNode* node = static_cast<HeapSnapshot*>(ptr)->GetNodeById(id);
+ return scope.Close(GraphNode::New(node));
}
Handle<Value> Snapshot::GetNode(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 HeapGraphNode* node = static_cast<HeapSnapshot*>(ptr)->GetNode(index);
- return scope.Close(GraphNode::New(node));
+ 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 HeapGraphNode* node = static_cast<HeapSnapshot*>(ptr)->GetNode(index);
+ return scope.Close(GraphNode::New(node));
}
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();
+ 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) {
@@ -123,4 +125,85 @@ Handle<Value> Snapshot::New(const 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));
+ }
+
+ 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) {
+ Local<Value> argv[2] = {
+ String::New(data),
+ Integer::New(size)
+ };
+
+ TryCatch try_catch;
+ onDataFunction->Call(obj, 2, argv);
+
+ if (try_catch.HasCaught()) {
+ FatalException(try_catch);
+ return kAbort;
+ }
+
+ return kContinue;
+ }
+
+ private:
+ 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);
}
+
+} //namespace nodex
View
39 snapshot.h
@@ -3,29 +3,34 @@
#ifndef NODE_SNAPSHOT_
#define NODE_SNAPSHOT_
-#include <v8.h>
#include <v8-profiler.h>
using namespace v8;
namespace nodex {
class Snapshot {
- public:
- static Handle<Value> New(const HeapSnapshot* snapshot);
-
- private:
- static Handle<Value> GetUid(Local<String> property, const AccessorInfo& info);
- static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info);
- static Handle<Value> GetRoot(Local<String> property, const AccessorInfo& info);
- static Handle<Value> GetType(Local<String> property, const AccessorInfo& info);
- static Handle<Value> GetNodesCount(Local<String> property, const AccessorInfo& info);
- static Handle<Value> GetNodeById(const Arguments& args);
- static Handle<Value> GetNode(const Arguments& args);
- static Handle<Value> Delete(const Arguments& args);
- static void Initialize();
- static Persistent<ObjectTemplate> snapshot_template_;
+ public:
+ static Handle<Value> New(const HeapSnapshot* snapshot);
+
+ private:
+ Snapshot(const v8::HeapSnapshot* snapshot)
+ : m_snapshot(snapshot){}
+
+ const v8::HeapSnapshot* m_snapshot;
+
+ static Handle<Value> GetUid(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetRoot(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetType(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetNodesCount(Local<String> property, const AccessorInfo& info);
+ static Handle<Value> GetNodeById(const Arguments& args);
+ static Handle<Value> GetNode(const Arguments& args);
+ static Handle<Value> Delete(const Arguments& args);
+ static Handle<Value> Serialize(const Arguments& args);
+
+ static void Initialize();
+ static Persistent<ObjectTemplate> snapshot_template_;
};
-
-}
+} //namespace nodex
#endif // NODE_SNAPSHOT_

0 comments on commit 3523d2d

Please sign in to comment.