-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_wrap.cc
78 lines (64 loc) · 2.63 KB
/
object_wrap.cc
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
#include "object_wrap.h"
Nan::Persistent<Function> object_wrap::constructor;
object_wrap::object_wrap(double value) : value_(value) {}
object_wrap::~object_wrap() {}
void object_wrap::Init(Local<Object> exports)
{
Local<Context> context = exports->CreationContext();
Nan::HandleScope scope;
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("object_wrap").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(tpl, "value", GetValue);
Nan::SetPrototypeMethod(tpl, "plusOne", PlusOne);
Nan::SetPrototypeMethod(tpl, "multiply", Multiply);
constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
exports->Set(context,
Nan::New("object_wrap").ToLocalChecked(),
tpl->GetFunction(context).ToLocalChecked());
}
void object_wrap::New(const Nan::FunctionCallbackInfo<Value> &args)
{
Local<Context> context = args.GetIsolate()->GetCurrentContext();
if (args.IsConstructCall())
{
// Invoked as constructor: `new MyObject(...)`
double value =
args[0]->IsUndefined() ? 0 : args[0]->NumberValue(context).FromJust();
object_wrap *obj = new object_wrap(value);
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
}
else
{
// Invoked as plain function `MyObject(...)`, turn into construct call.
const int argc = 1;
Local<Value> argv[argc] = {args[0]};
Local<Function> cons = Nan::New<Function>(constructor);
args.GetReturnValue().Set(
cons->NewInstance(context, argc, argv).ToLocalChecked());
}
}
void object_wrap::GetValue(const Nan::FunctionCallbackInfo<Value> &args)
{
object_wrap *obj = ObjectWrap::Unwrap<object_wrap>(args.Holder());
args.GetReturnValue().Set(Nan::New(obj->value_));
}
void object_wrap::PlusOne(const Nan::FunctionCallbackInfo<Value> &args)
{
object_wrap *obj = ObjectWrap::Unwrap<object_wrap>(args.Holder());
obj->value_ += 1;
args.GetReturnValue().Set(Nan::New(obj->value_));
}
void object_wrap::Multiply(const Nan::FunctionCallbackInfo<Value> &args)
{
Local<Context> context = args.GetIsolate()->GetCurrentContext();
object_wrap *obj = ObjectWrap::Unwrap<object_wrap>(args.Holder());
double multiple =
args[0]->IsUndefined() ? 1 : args[0]->NumberValue(context).FromJust();
Local<Function> cons = Nan::New<Function>(constructor);
const int argc = 1;
Local<Value> argv[argc] = {Nan::New(obj->value_ * multiple)};
args.GetReturnValue().Set(
cons->NewInstance(context, argc, argv).ToLocalChecked());
}