Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 032d04c

Browse files
committed
Merge pull request #20 from paulcbetts/nan-2
Nan 2.0
2 parents 656a270 + 353a33c commit 032d04c

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
"jasmine-focused": "1.x"
2525
},
2626
"dependencies": {
27-
"nan": "https://atom.io/download/atom-shell/nan-1.6.1.tgz"
27+
"nan": "^2.0.0"
2828
}
2929
}

src/main.cc

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,110 @@
11
#include "nan.h"
22
#include "spellchecker.h"
33

4-
using node::ObjectWrap;
4+
using Nan::ObjectWrap;
55
using namespace spellchecker;
66
using namespace v8;
77

88
namespace {
99

10-
class Spellchecker : public ObjectWrap {
10+
class Spellchecker : public Nan::ObjectWrap {
1111
SpellcheckerImplementation* impl;
1212

1313
static NAN_METHOD(New) {
14-
NanScope();
14+
Nan::HandleScope scope;
1515
Spellchecker* that = new Spellchecker();
16-
that->Wrap(args.This());
16+
that->Wrap(info.This());
1717

18-
NanReturnValue(args.This());
18+
info.GetReturnValue().Set(info.This());
1919
}
2020

2121
static NAN_METHOD(SetDictionary) {
22-
NanScope();
22+
Nan::HandleScope scope;
2323

24-
if (args.Length() < 1) {
25-
return NanThrowError("Bad argument");
24+
if (info.Length() < 1) {
25+
return Nan::ThrowError("Bad argument");
2626
}
2727

28-
Spellchecker* that = ObjectWrap::Unwrap<Spellchecker>(args.Holder());
28+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
2929

30-
std::string language = *String::Utf8Value(args[0]);
30+
std::string language = *String::Utf8Value(info[0]);
3131
std::string directory = ".";
32-
if (args.Length() > 1) {
33-
directory = *String::Utf8Value(args[1]);
32+
if (info.Length() > 1) {
33+
directory = *String::Utf8Value(info[1]);
3434
}
3535

3636
bool result = that->impl->SetDictionary(language, directory);
37-
NanReturnValue(NanNew(result));
37+
info.GetReturnValue().Set(Nan::New(result));
3838
}
3939

4040
static NAN_METHOD(IsMisspelled) {
41-
NanScope();
42-
if (args.Length() < 1) {
43-
return NanThrowError("Bad argument");
41+
Nan::HandleScope scope;
42+
if (info.Length() < 1) {
43+
return Nan::ThrowError("Bad argument");
4444
}
4545

46-
Spellchecker* that = ObjectWrap::Unwrap<Spellchecker>(args.Holder());
47-
std::string word = *String::Utf8Value(args[0]);
46+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
47+
std::string word = *String::Utf8Value(info[0]);
4848

49-
NanReturnValue(NanNew(that->impl->IsMisspelled(word)));
49+
info.GetReturnValue().Set(Nan::New(that->impl->IsMisspelled(word)));
5050
}
5151

5252
static NAN_METHOD(Add) {
53-
NanScope();
54-
if (args.Length() < 1) {
55-
return NanThrowError("Bad argument");
53+
Nan::HandleScope scope;
54+
if (info.Length() < 1) {
55+
return Nan::ThrowError("Bad argument");
5656
}
5757

58-
Spellchecker* that = ObjectWrap::Unwrap<Spellchecker>(args.Holder());
59-
std::string word = *String::Utf8Value(args[0]);
58+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
59+
std::string word = *String::Utf8Value(info[0]);
6060

6161
that->impl->Add(word);
62-
NanReturnUndefined();
62+
return;
6363
}
6464

6565
static NAN_METHOD(GetAvailableDictionaries) {
66-
NanScope();
66+
Nan::HandleScope scope;
6767

68-
Spellchecker* that = ObjectWrap::Unwrap<Spellchecker>(args.Holder());
68+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
6969

7070
std::string path = ".";
71-
if (args.Length() > 0) {
72-
std::string path = *String::Utf8Value(args[0]);
71+
if (info.Length() > 0) {
72+
std::string path = *String::Utf8Value(info[0]);
7373
}
7474

7575
std::vector<std::string> dictionaries =
7676
that->impl->GetAvailableDictionaries(path);
7777

78-
Local<Array> result = NanNew<Array>(dictionaries.size());
78+
Local<Array> result = Nan::New<Array>(dictionaries.size());
7979
for (size_t i = 0; i < dictionaries.size(); ++i) {
8080
const std::string& dict = dictionaries[i];
81-
result->Set(i, NanNew(dict.data(), dict.size()));
81+
result->Set(i, Nan::New(dict.data(), dict.size()).ToLocalChecked());
8282
}
8383

84-
NanReturnValue(result);
84+
info.GetReturnValue().Set(result);
8585
}
8686

8787
static NAN_METHOD(GetCorrectionsForMisspelling) {
88-
NanScope();
89-
if (args.Length() < 1) {
90-
return NanThrowError("Bad argument");
88+
Nan::HandleScope scope;
89+
if (info.Length() < 1) {
90+
return Nan::ThrowError("Bad argument");
9191
}
9292

93-
Spellchecker* that = ObjectWrap::Unwrap<Spellchecker>(args.Holder());
93+
Spellchecker* that = Nan::ObjectWrap::Unwrap<Spellchecker>(info.Holder());
9494

95-
std::string word = *String::Utf8Value(args[0]);
95+
std::string word = *String::Utf8Value(info[0]);
9696
std::vector<std::string> corrections =
9797
that->impl->GetCorrectionsForMisspelling(word);
9898

99-
Local<Array> result = NanNew<Array>(corrections.size());
99+
Local<Array> result = Nan::New<Array>(corrections.size());
100100
for (size_t i = 0; i < corrections.size(); ++i) {
101101
const std::string& word = corrections[i];
102-
result->Set(i, NanNew<String>(word.data(), word.size()));
102+
103+
Nan::MaybeLocal<String> val = Nan::New<String>(word.data(), word.size());
104+
result->Set(i, val.ToLocalChecked());
103105
}
104106

105-
NanReturnValue(result);
107+
info.GetReturnValue().Set(result);
106108
}
107109

108110
Spellchecker() {
@@ -116,18 +118,18 @@ class Spellchecker : public ObjectWrap {
116118

117119
public:
118120
static void Init(Handle<Object> exports) {
119-
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(Spellchecker::New);
121+
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(Spellchecker::New);
120122

121-
tpl->SetClassName(NanNew<String>("Spellchecker"));
123+
tpl->SetClassName(Nan::New<String>("Spellchecker").ToLocalChecked());
122124
tpl->InstanceTemplate()->SetInternalFieldCount(1);
123125

124-
NODE_SET_METHOD(tpl->InstanceTemplate(), "setDictionary", Spellchecker::SetDictionary);
125-
NODE_SET_METHOD(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries);
126-
NODE_SET_METHOD(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling);
127-
NODE_SET_METHOD(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled);
128-
NODE_SET_METHOD(tpl->InstanceTemplate(), "add", Spellchecker::Add);
126+
Nan::SetMethod(tpl->InstanceTemplate(), "setDictionary", Spellchecker::SetDictionary);
127+
Nan::SetMethod(tpl->InstanceTemplate(), "getAvailableDictionaries", Spellchecker::GetAvailableDictionaries);
128+
Nan::SetMethod(tpl->InstanceTemplate(), "getCorrectionsForMisspelling", Spellchecker::GetCorrectionsForMisspelling);
129+
Nan::SetMethod(tpl->InstanceTemplate(), "isMisspelled", Spellchecker::IsMisspelled);
130+
Nan::SetMethod(tpl->InstanceTemplate(), "add", Spellchecker::Add);
129131

130-
exports->Set(NanNew("Spellchecker"), tpl->GetFunction());
132+
exports->Set(Nan::New("Spellchecker").ToLocalChecked(), tpl->GetFunction());
131133
}
132134
};
133135

0 commit comments

Comments
 (0)