forked from nodejs/node-addon-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyobject.cc
More file actions
56 lines (42 loc) · 1.69 KB
/
myobject.cc
File metadata and controls
56 lines (42 loc) · 1.69 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
#include "myobject.h"
#include <nan.h>
using namespace v8;
MyObject::MyObject(){};
MyObject::~MyObject(){};
Nan::Persistent<v8::Function> MyObject::constructor;
void MyObject::Init() {
Nan::HandleScope scope;
// Prepare constructor template
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
tpl->PrototypeTemplate()->Set(Nan::New("plusOne").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(PlusOne));
constructor.Reset(
tpl->GetFunction(Nan::GetCurrentContext()).ToLocalChecked());
}
void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
MyObject* obj = new MyObject();
obj->counter_ =
info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
v8::Local<v8::Object> MyObject::NewInstance(v8::Local<v8::Value> arg) {
Nan::EscapableHandleScope scope;
const unsigned argc = 1;
v8::Local<v8::Value> argv[argc] = {arg};
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
v8::Local<v8::Context> context =
v8::Isolate::GetCurrent()->GetCurrentContext();
v8::Local<v8::Object> instance =
cons->NewInstance(context, argc, argv).ToLocalChecked();
return scope.Escape(instance);
}
void MyObject::PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info) {
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.This());
obj->counter_ += 1;
info.GetReturnValue().Set(Nan::New(obj->counter_));
}