Skip to content

Commit fbf4fdd

Browse files
authored
Fix build on Node.js 12 (#109)
* Fix build on Node.js 12 * update badge in README.md
1 parent 0f3ea58 commit fbf4fdd

File tree

16 files changed

+48
-31
lines changed

16 files changed

+48
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Debug/
44
Release/
55
*.lock
66
*.log
7+
package-lock.json
78
npm-debug.log*
89

910
.idea/

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: node_js
22
node_js:
33
- "8"
44
- "10"
5-
# - "12" enable this when fix issue on Node.js 12
5+
- "12"
66
cache:
77
npm
88
before_script:

1_hello_world/nan/hello.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ void Method(const Nan::FunctionCallbackInfo<v8::Value>& info) {
55
}
66

77
void Init(v8::Local<v8::Object> exports) {
8+
v8::Local<v8::Context> context = exports->CreationContext();
89
exports->Set(Nan::New("hello").ToLocalChecked(),
9-
Nan::New<v8::FunctionTemplate>(Method)->GetFunction());
10+
Nan::New<v8::FunctionTemplate>(Method)->GetFunction(context).ToLocalChecked());
1011
}
1112

1213
NODE_MODULE(hello, Init)

2_function_arguments/nan/addon.cc

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

33
void Add(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
45

56
if (info.Length() < 2) {
67
Nan::ThrowTypeError("Wrong number of arguments");
@@ -12,16 +13,17 @@ void Add(const Nan::FunctionCallbackInfo<v8::Value>& info) {
1213
return;
1314
}
1415

15-
double arg0 = info[0]->NumberValue();
16-
double arg1 = info[1]->NumberValue();
16+
double arg0 = info[0]->NumberValue(context).FromJust();
17+
double arg1 = info[1]->NumberValue(context).FromJust();
1718
v8::Local<v8::Number> num = Nan::New(arg0 + arg1);
1819

1920
info.GetReturnValue().Set(num);
2021
}
2122

2223
void Init(v8::Local<v8::Object> exports) {
24+
v8::Local<v8::Context> context = exports->CreationContext();
2325
exports->Set(Nan::New("add").ToLocalChecked(),
24-
Nan::New<v8::FunctionTemplate>(Add)->GetFunction());
26+
Nan::New<v8::FunctionTemplate>(Add)->GetFunction(context).ToLocalChecked());
2527
}
2628

2729
NODE_MODULE(addon, Init)

3_callbacks/nan/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"private": true,
77
"gypfile": true,
88
"dependencies": {
9-
"bindings": "~1.2.1",
10-
"nan": "^2.0.0"
9+
"bindings": "~1.5.0",
10+
"nan": "^2.14.0"
1111
}
1212
}

4_object_factory/nan/addon.cc

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

33
void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
45
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
5-
obj->Set(Nan::New("msg").ToLocalChecked(), info[0]->ToString());
6+
obj->Set(Nan::New("msg").ToLocalChecked(), info[0]->ToString(context).ToLocalChecked());
67

78
info.GetReturnValue().Set(obj);
89
}
910

1011
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
12+
v8::Local<v8::Context> context = exports->CreationContext();
1113
module->Set(Nan::New("exports").ToLocalChecked(),
12-
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction());
14+
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction(context).ToLocalChecked());
1315
}
1416

1517
NODE_MODULE(addon, Init)

4_object_factory/nan/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"private": true,
77
"gypfile": true,
88
"dependencies": {
9-
"bindings": "~1.2.1",
10-
"nan": "^2.0.0"
9+
"bindings": "~1.5.0",
10+
"nan": "^2.14.0"
1111
}
1212
}

5_function_factory/nan/addon.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ void MyFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {
55
}
66

77
void CreateFunction(const Nan::FunctionCallbackInfo<v8::Value>& info) {
8+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
89
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(MyFunction);
9-
v8::Local<v8::Function> fn = tpl->GetFunction();
10+
v8::Local<v8::Function> fn = tpl->GetFunction(context).ToLocalChecked();
1011

1112
// omit this to make it anonymous
1213
fn->SetName(Nan::New("theFunction").ToLocalChecked());

6_object_wrap/nan/myobject.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ MyObject::~MyObject() {
99
}
1010

1111
void MyObject::Init(v8::Local<v8::Object> exports) {
12+
v8::Local<v8::Context> context = exports->CreationContext();
1213
Nan::HandleScope scope;
1314

1415
// Prepare constructor template
@@ -21,14 +22,16 @@ void MyObject::Init(v8::Local<v8::Object> exports) {
2122
Nan::SetPrototypeMethod(tpl, "plusOne", PlusOne);
2223
Nan::SetPrototypeMethod(tpl, "multiply", Multiply);
2324

24-
constructor.Reset(tpl->GetFunction());
25-
exports->Set(Nan::New("MyObject").ToLocalChecked(), tpl->GetFunction());
25+
constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
26+
exports->Set(Nan::New("MyObject").ToLocalChecked(),
27+
tpl->GetFunction(context).ToLocalChecked());
2628
}
2729

2830
void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
31+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
2932
if (info.IsConstructCall()) {
3033
// Invoked as constructor: `new MyObject(...)`
31-
double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue();
34+
double value = info[0]->IsUndefined() ? 0 : info[0]->NumberValue(context).FromJust();
3235
MyObject* obj = new MyObject(value);
3336
obj->Wrap(info.This());
3437
info.GetReturnValue().Set(info.This());
@@ -37,7 +40,6 @@ void MyObject::New(const Nan::FunctionCallbackInfo<v8::Value>& info) {
3740
const int argc = 1;
3841
v8::Local<v8::Value> argv[argc] = { info[0] };
3942
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
40-
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
4143
info.GetReturnValue().Set(
4244
cons->NewInstance(context, argc, argv).ToLocalChecked());
4345
}
@@ -55,15 +57,15 @@ void MyObject::PlusOne(const Nan::FunctionCallbackInfo<v8::Value>& info) {
5557
}
5658

5759
void MyObject::Multiply(const Nan::FunctionCallbackInfo<v8::Value>& info) {
60+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
5861
MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.Holder());
59-
double multiple = info[0]->IsUndefined() ? 1 : info[0]->NumberValue();
62+
double multiple = info[0]->IsUndefined() ? 1 : info[0]->NumberValue(context).FromJust();
6063

6164
v8::Local<v8::Function> cons = Nan::New<v8::Function>(constructor);
6265

6366
const int argc = 1;
6467
v8::Local<v8::Value> argv[argc] = { Nan::New(obj->value_ * multiple) };
6568

66-
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
6769
info.GetReturnValue().Set(
6870
cons->NewInstance(context, argc, argv).ToLocalChecked());
6971
}

7_factory_wrap/nan/addon.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ void CreateObject(const Nan::FunctionCallbackInfo<v8::Value>& info) {
66
}
77

88
void InitAll(v8::Local<v8::Object> exports, v8::Local<v8::Object> module) {
9+
v8::Local<v8::Context> context = exports->CreationContext();
10+
911
Nan::HandleScope scope;
1012

1113
MyObject::Init();
1214

1315
module->Set(Nan::New("exports").ToLocalChecked(),
14-
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction());
16+
Nan::New<v8::FunctionTemplate>(CreateObject)->GetFunction(context).ToLocalChecked());
1517
}
1618

1719
NODE_MODULE(addon, InitAll)

0 commit comments

Comments
 (0)