forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.cc
More file actions
80 lines (57 loc) · 2.05 KB
/
wrapper.cc
File metadata and controls
80 lines (57 loc) · 2.05 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
/**
* This code is auto-generated; unless you know what you're doing, do not modify!
**/
#include <v8.h>
#include <node.h>
#include <string>
#include <cstring>
#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<v8::Object> target) {
NanScope();
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(NanSymbol("Wrapper"));
NODE_SET_PROTOTYPE_METHOD(tpl, "toBuffer", ToBuffer);
NanAssignPersistent(FunctionTemplate, constructor_template, tpl);
target->Set(String::NewSymbol("Wrapper"), tpl->GetFunction());
}
NAN_METHOD(Wrapper::New) {
NanScope();
if (args.Length() == 0 || !args[0]->IsExternal()) {
return NanThrowError(String::New("void * is required."));
}
Wrapper* object = new Wrapper(External::Cast(*args[0])->Value());
object->Wrap(args.This());
NanReturnValue(args.This());
}
Handle<Value> Wrapper::New(void *raw) {
NanScope();
Handle<Value> argv[1] = { External::New((void *)raw) };
Local<Object> instance;
Local<FunctionTemplate> constructorHandle = NanPersistentToLocal(constructor_template);
instance = constructorHandle->GetFunction()->NewInstance(1, argv);
return scope.Close(instance);
}
void *Wrapper::GetValue() {
return this->raw;
}
NAN_METHOD(Wrapper::ToBuffer) {
NanScope();
if(args.Length() == 0 || !args[0]->IsNumber()) {
return NanThrowError(String::New("Number is required."));
}
int len = args[0]->ToNumber()->Value();
Local<Function> bufferConstructor = Local<Function>::Cast(
Context::GetCurrent()->Global()->Get(String::New("Buffer")));
Handle<Value> constructorArgs[1] = { Integer::New(len) };
Local<Object> nodeBuffer = bufferConstructor->NewInstance(1, constructorArgs);
std::memcpy(node::Buffer::Data(nodeBuffer), ObjectWrap::Unwrap<Wrapper>(args.This())->GetValue(), len);
NanReturnValue(nodeBuffer);
}
Persistent<FunctionTemplate> Wrapper::constructor_template;