/** * 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) { NanScope(); Local tpl = NanNew(New); tpl->InstanceTemplate()->SetInternalFieldCount(1); tpl->SetClassName(NanNew("Wrapper")); NODE_SET_PROTOTYPE_METHOD(tpl, "toBuffer", ToBuffer); NanAssignPersistent(constructor_template, tpl); target->Set(NanNew("Wrapper"), tpl->GetFunction()); } NAN_METHOD(Wrapper::New) { NanScope(); if (args.Length() == 0 || !args[0]->IsExternal()) { return NanThrowError("void * is required."); } Wrapper* object = new Wrapper(External::Cast(*args[0])->Value()); object->Wrap(args.This()); NanReturnValue(args.This()); } Handle Wrapper::New(void *raw) { NanEscapableScope(); Handle argv[1] = { NanNew((void *)raw) }; Local instance; Local constructorHandle = NanNew(constructor_template); instance = constructorHandle->GetFunction()->NewInstance(1, argv); return NanEscapeScope(instance); } void *Wrapper::GetValue() { return this->raw; } NAN_METHOD(Wrapper::ToBuffer) { NanScope(); if(args.Length() == 0 || !args[0]->IsNumber()) { return NanThrowError("Number is required."); } int len = args[0]->ToNumber()->Value(); Local bufferConstructor = Local::Cast( NanGetCurrentContext()->Global()->Get(NanNew("Buffer"))); Handle constructorArgs[1] = { NanNew(len) }; Local nodeBuffer = bufferConstructor->NewInstance(1, constructorArgs); std::memcpy(node::Buffer::Data(nodeBuffer), ObjectWrap::Unwrap(args.This())->GetValue(), len); NanReturnValue(nodeBuffer); } Persistent Wrapper::constructor_template;