Skip to content

Commit d72d1ac

Browse files
committed
upgrade NAN examples to NAN v2
1 parent b945171 commit d72d1ac

File tree

15 files changed

+139
-189
lines changed

15 files changed

+139
-189
lines changed

1_hello_world/nan/hello.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#include <nan.h>
22

3-
using namespace v8;
4-
5-
NAN_METHOD(Method) {
6-
NanScope();
7-
NanReturnValue(NanNew("world"));
3+
void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
info.GetReturnValue().Set(Nan::New("world").ToLocalChecked());
85
}
96

10-
void Init(Handle<Object> exports) {
11-
exports->Set(NanNew("hello"), NanNew<FunctionTemplate>(Method)->GetFunction());
7+
void Init(v8::Local<v8::Object> exports) {
8+
exports->Set(Nan::New("hello").ToLocalChecked(),
9+
Nan::New<v8::FunctionTemplate>(Method)->GetFunction());
1210
}
1311

1412
NODE_MODULE(hello, Init)

2_function_arguments/nan/addon.cc

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
#include <nan.h>
22

3-
using namespace v8;
3+
void Add(const Nan::FunctionCallbackInfo<v8::Value>& info) {
44

5-
NAN_METHOD(Add) {
6-
NanScope();
7-
8-
if (args.Length() < 2) {
9-
NanThrowTypeError("Wrong number of arguments");
10-
NanReturnUndefined();
5+
if (info.Length() < 2) {
6+
Nan::ThrowTypeError("Wrong number of arguments");
7+
return;
118
}
129

13-
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
14-
NanThrowTypeError("Wrong arguments");
15-
NanReturnUndefined();
10+
if (!info[0]->IsNumber() || !info[1]->IsNumber()) {
11+
Nan::ThrowTypeError("Wrong arguments");
12+
return;
1613
}
1714

18-
double arg0 = args[0]->NumberValue();
19-
double arg1 = args[1]->NumberValue();
20-
Local<Number> num = NanNew(arg0 + arg1);
15+
double arg0 = info[0]->NumberValue();
16+
double arg1 = info[1]->NumberValue();
17+
v8::Local<v8::Number> num = Nan::New(arg0 + arg1);
2118

22-
NanReturnValue(num);
19+
info.GetReturnValue().Set(num);
2320
}
2421

25-
void Init(Handle<Object> exports) {
26-
exports->Set(NanNew("add"), NanNew<FunctionTemplate>(Add)->GetFunction());
22+
void Init(v8::Local<v8::Object> exports) {
23+
exports->Set(Nan::New("add").ToLocalChecked(),
24+
Nan::New<v8::FunctionTemplate>(Add)->GetFunction());
2725
}
2826

2927
NODE_MODULE(addon, Init)

3_callbacks/nan/addon.cc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#include <nan.h>
22

3-
using namespace v8;
4-
5-
NAN_METHOD(RunCallback) {
6-
NanScope();
7-
8-
Local<Function> cb = args[0].As<Function>();
3+
void RunCallback(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
v8::Local<v8::Function> cb = info[0].As<v8::Function>();
95
const unsigned argc = 1;
10-
Local<Value> argv[argc] = { NanNew("hello world") };
11-
NanMakeCallback(NanGetCurrentContext()->Global(), cb, argc, argv);
12-
13-
NanReturnUndefined();
6+
v8::Local<v8::Value> argv[argc] = { Nan::New("hello world").ToLocalChecked() };
7+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), cb, argc, argv);
148
}
159

16-
void Init(Handle<Object> exports, Handle<Object> module) {
17-
NODE_SET_METHOD(module, "exports", RunCallback);
10+
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
11+
Nan::SetMethod(module, "exports", RunCallback);
1812
}
1913

2014
NODE_MODULE(addon, Init)

4_object_factory/nan/addon.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
#include <nan.h>
22

3-
using namespace v8;
3+
void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
5+
obj->Set(Nan::New("msg").ToLocalChecked(), info[0]->ToString());
46

5-
NAN_METHOD(CreateObject) {
6-
NanScope();
7-
8-
Local<Object> obj = NanNew<Object>();
9-
obj->Set(NanNew("msg"), args[0]->ToString());
10-
11-
NanReturnValue(obj);
7+
info.GetReturnValue().Set(obj);
128
}
139

14-
void Init(Handle<Object> exports, Handle<Object> module) {
15-
module->Set(NanNew("exports"),
16-
NanNew<FunctionTemplate>(CreateObject)->GetFunction());
10+
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
11+
module->Set(Nan::New("exports").ToLocalChecked(),
12+
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction());
1713
}
1814

1915
NODE_MODULE(addon, Init)

4_object_factory/nan/addon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var addon = require('./build/Release/addon');
1+
var addon = require('bindings')('addon');
22

33
var obj1 = addon('hello');
44
var obj2 = addon('world');

5_function_factory/nan/addon.cc

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
#include <nan.h>
22

3-
using namespace v8;
4-
5-
NAN_METHOD(MyFunction) {
6-
NanScope();
7-
NanReturnValue(NanNew("hello world"));
3+
void MyFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
info.GetReturnValue().Set(Nan::New("hello world").ToLocalChecked());
85
}
96

10-
NAN_METHOD(CreateFunction) {
11-
NanScope();
12-
13-
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(MyFunction);
14-
Local<Function> fn = tpl->GetFunction();
7+
void CreateFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {
8+
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(MyFunction);
9+
v8::Local<v8::Function> fn = tpl->GetFunction();
1510

1611
// omit this to make it anonymous
17-
fn->SetName(NanNew("theFunction"));
12+
fn->SetName(Nan::New("theFunction").ToLocalChecked());
1813

19-
NanReturnValue(fn);
14+
info.GetReturnValue().Set(fn);
2015
}
2116

22-
void Init(Handle<Object> exports, Handle<Object> module) {
23-
NODE_SET_METHOD(module, "exports", CreateFunction);
17+
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
18+
Nan::SetMethod(module, "exports", CreateFunction);
2419
}
2520

2621
NODE_MODULE(addon, Init)

6_object_wrap/nan/addon.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#include <nan.h>
22
#include "myobject.h"
33

4-
using namespace v8;
5-
6-
void InitAll(Handle<Object> exports) {
4+
void InitAll(v8::Local<v8::Object> exports) {
75
MyObject::Init(exports);
86
}
97

6_object_wrap/nan/myobject.cc

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,65 @@
11
#include "myobject.h"
22

3-
using namespace v8;
4-
5-
Persistent<Function> MyObject::constructor;
3+
Nan::Persistent<v8::Function> MyObject::constructor;
64

75
MyObject::MyObject(double value) : value_(value) {
86
}
97

108
MyObject::~MyObject() {
119
}
1210

13-
void MyObject::Init(Handle<Object> exports) {
14-
NanScope();
11+
void MyObject::Init(v8::Local<v8::Object> exports) {
12+
Nan::HandleScope scope;
1513

1614
// Prepare constructor template
17-
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
18-
tpl->SetClassName(NanNew("MyObject"));
15+
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
16+
tpl->SetClassName(Nan::New("MyObject").ToLocalChecked());
1917
tpl->InstanceTemplate()->SetInternalFieldCount(1);
2018

2119
// Prototype
22-
NODE_SET_PROTOTYPE_METHOD(tpl, "value", GetValue);
23-
NODE_SET_PROTOTYPE_METHOD(tpl, "plusOne", PlusOne);
24-
NODE_SET_PROTOTYPE_METHOD(tpl, "multiply", Multiply);
20+
Nan::SetPrototypeMethod(tpl, "value", GetValue);
21+
Nan::SetPrototypeMethod(tpl, "plusOne", PlusOne);
22+
Nan::SetPrototypeMethod(tpl, "multiply", Multiply);
2523

26-
NanAssignPersistent(constructor, tpl->GetFunction());
27-
exports->Set(NanNew("MyObject"), tpl->GetFunction());
24+
constructor.Reset(tpl->GetFunction());
25+
exports->Set(Nan::New("MyObject").ToLocalChecked(), tpl->GetFunction());
2826
}
2927

30-
NAN_METHOD(MyObject::New) {
31-
NanScope();
32-
33-
if (args.IsConstructCall()) {
28+
void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
29+
if (info.IsConstructCall()) {
3430
// Invoked as constructor: `new MyObject(...)`
35-
double value = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
31+
double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue();
3632
MyObject* obj = new MyObject(value);
37-
obj->Wrap(args.This());
38-
NanReturnValue(args.This());
33+
obj->Wrap(info.This());
34+
info.GetReturnValue().Set(info.This());
3935
} else {
4036
// Invoked as plain function `MyObject(...)`, turn into construct call.
4137
const int argc = 1;
42-
Local<Value> argv[argc] = { args[0] };
43-
Local<Function> cons = NanNew<Function>(constructor);
44-
NanReturnValue(cons->NewInstance(argc, argv));
38+
v8::Local<v8::Value> argv[argc] = { info[0] };
39+
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
40+
info.GetReturnValue().Set(cons->NewInstance(argc, argv));
4541
}
4642
}
4743

48-
NAN_METHOD(MyObject::GetValue) {
49-
NanScope();
50-
51-
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
52-
53-
NanReturnValue(NanNew(obj->value_));
44+
void MyObject::GetValue(const Nan::FunctionCallbackInfo<v8::Value>& info) {
45+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
46+
info.GetReturnValue().Set(Nan::New(obj->value_));
5447
}
5548

56-
NAN_METHOD(MyObject::PlusOne) {
57-
NanScope();
58-
59-
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
49+
void MyObject::PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info) {
50+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
6051
obj->value_ += 1;
61-
62-
NanReturnValue(NanNew(obj->value_));
52+
info.GetReturnValue().Set(Nan::New(obj->value_));
6353
}
6454

65-
NAN_METHOD(MyObject::Multiply) {
66-
NanScope();
67-
68-
MyObject* obj = ObjectWrap::Unwrap<MyObject>(args.Holder());
69-
double multiple = args[0]->IsUndefined() ? 1 : args[0]->NumberValue();
55+
void MyObject::Multiply(const Nan::FunctionCallbackInfo<v8::Value>& info) {
56+
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
57+
double multiple = info[0]->IsUndefined() ? 1 : info[0]->NumberValue();
7058

71-
Local<Function> cons = NanNew<Function>(constructor);
59+
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
7260

7361
const int argc = 1;
74-
Local<Value> argv[argc] = { NanNew(obj->value_ * multiple) };
62+
v8::Local<v8::Value> argv[argc] = { Nan::New(obj->value_ * multiple) };
7563

76-
NanReturnValue(cons->NewInstance(argc, argv));
64+
info.GetReturnValue().Set(cons->NewInstance(argc, argv));
7765
}

6_object_wrap/nan/myobject.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
#include <nan.h>
55

6-
class MyObject : public node::ObjectWrap {
6+
class MyObject : public Nan::ObjectWrap {
77
public:
8-
static void Init(v8::Handle<v8::Object> exports);
8+
static void Init(v8::Local<v8::Object> exports);
99

1010
private:
1111
explicit MyObject(double value = 0);
1212
~MyObject();
1313

14-
static NAN_METHOD(New);
15-
static NAN_METHOD(GetValue);
16-
static NAN_METHOD(PlusOne);
17-
static NAN_METHOD(Multiply);
18-
static v8::Persistent<v8::Function> constructor;
14+
static void New(const Nan::FunctionCallbackInfo<v8::Value>& info);
15+
static void GetValue(const Nan::FunctionCallbackInfo<v8::Value>& info);
16+
static void PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info);
17+
static void Multiply(const Nan::FunctionCallbackInfo<v8::Value>& info);
18+
static Nan::Persistent<v8::Function> constructor;
1919
double value_;
2020
};
2121

7_factory_wrap/nan/addon.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
#include <nan.h>
22
#include "myobject.h"
33

4-
using namespace v8;
5-
6-
NAN_METHOD(CreateObject) {
7-
NanScope();
8-
NanReturnValue(MyObject::NewInstance(args[0]));
4+
void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {
5+
info.GetReturnValue().Set(MyObject::NewInstance(info[0]));
96
}
107

11-
void InitAll(Handle<Object> exports, Handle<Object> module) {
12-
NanScope();
8+
void InitAll(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
9+
Nan::HandleScope scope;
1310

1411
MyObject::Init();
1512

16-
module->Set(NanNew("exports"),
17-
NanNew<FunctionTemplate>(CreateObject)->GetFunction());
13+
module->Set(Nan::New("exports").ToLocalChecked(),
14+
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction());
1815
}
1916

2017
NODE_MODULE(addon, InitAll)

0 commit comments

Comments
 (0)