[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/nodegit/nodegit/v0.1.0/src/wrapper.cc [Back]  [Original]

/**
 * This code is auto-generated; unless you know what you're doing, do not modify!
 **/
#include 
#include 
#include 

#include "../include/wrapper.h"
#include "node_buffer.h"

using namespace v8;
using namespace node;

Wrapper::Wrapper(void *raw) {
  this->raw = raw;
}

void Wrapper::Initialize(Handle target) {
  HandleScope scope;

  Local tpl = FunctionTemplate::New(New);

  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  tpl->SetClassName(String::NewSymbol("Wrapper"));

  NODE_SET_PROTOTYPE_METHOD(tpl, "toBuffer", ToBuffer);

  constructor_template = Persistent::New(tpl->GetFunction());
  target->Set(String::NewSymbol("Wrapper"), constructor_template);
}

Handle Wrapper::New(const Arguments& args) {
  HandleScope scope;

  if (args.Length() == 0 || !args[0]->IsExternal()) {
    return ThrowException(Exception::Error(String::New("void * is required.")));
  }

  Wrapper* object = new Wrapper(External::Unwrap(args[0]));
  object->Wrap(args.This());

  return scope.Close(args.This());
}

Handle Wrapper::New(void *raw) {
  HandleScope scope;

  Handle argv[1] = { External::New((void *)raw) };
  return scope.Close(Wrapper::constructor_template->NewInstance(1, argv));
}

void *Wrapper::GetValue() {
  return this->raw;
}

Handle Wrapper::ToBuffer(const Arguments& args) {
  HandleScope scope;

  if(args.Length() == 0 || !args[0]->IsNumber()) {
    return ThrowException(Exception::Error(String::New("Number is required.")));
  }

  int len = args[0]->ToNumber()->Value();

  Local bufferConstructor = Local::Cast(
    Context::GetCurrent()->Global()->Get(String::New("Buffer"))); 

  Handle constructorArgs[1] = { Integer::New(len) };
  Local nodeBuffer = bufferConstructor->NewInstance(1, constructorArgs);

  memcpy(node::Buffer::Data(nodeBuffer), ObjectWrap::Unwrap(args.This())->GetValue(), len);

  return scope.Close(nodeBuffer);
}


Persistent Wrapper::constructor_template;

Web Proxy Viewer  |  New URL  |  Original Page