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

Nan 2.0 #10

Merged
merged 4 commits into from
Sep 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"dependencies": {
"event-kit": "^1.0.0",
"nan": "https://atom.io/download/atom-shell/nan-1.6.1.tgz"
"nan": "^2.0.0"
},
"devDependencies": {
"coffee-script": "^1.7.0",
Expand Down
42 changes: 21 additions & 21 deletions src/keyboard-layout-observer-mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@
using namespace v8;

void KeyboardLayoutObserver::Init(Handle<Object> target) {
NanScope();
Local<FunctionTemplate> newTemplate = NanNew<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(NanNew<String>("KeyboardLayoutObserver"));
Nan::HandleScope scope;
Local<FunctionTemplate> newTemplate = Nan::New<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked());
newTemplate->InstanceTemplate()->SetInternalFieldCount(1);

Local<ObjectTemplate> proto = newTemplate->PrototypeTemplate();

NODE_SET_METHOD(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
NODE_SET_METHOD(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLanguage);
NODE_SET_METHOD(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);
Nan::SetMethod(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
Nan::SetMethod(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLanguage);
Nan::SetMethod(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);

target->Set(NanNew<String>("KeyboardLayoutObserver"), newTemplate->GetFunction());
target->Set(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked(), newTemplate->GetFunction());
}

NODE_MODULE(keyboard_layout_observer, KeyboardLayoutObserver::Init)

NAN_METHOD(KeyboardLayoutObserver::New) {
NanScope();
Nan::HandleScope scope;

Local<Function> callbackHandle = args[0].As<Function>();
NanCallback *callback = new NanCallback(callbackHandle);
Local<Function> callbackHandle = info[0].As<Function>();
Nan::Callback *callback = new Nan::Callback(callbackHandle);

KeyboardLayoutObserver *observer = new KeyboardLayoutObserver(callback);
observer->Wrap(args.This());
NanReturnUndefined();
observer->Wrap(info.This());
return;
}

uv_loop_t *loop = uv_default_loop();
Expand All @@ -48,7 +48,7 @@ static void asyncSendHandler(uv_async_t *handle) {
(static_cast<KeyboardLayoutObserver *>(handle->data))->HandleKeyboardLayoutChanged();
}

KeyboardLayoutObserver::KeyboardLayoutObserver(NanCallback *callback) : callback(callback) {
KeyboardLayoutObserver::KeyboardLayoutObserver(Nan::Callback *callback) : callback(callback) {
uv_async_init(loop, &async, (uv_async_cb) asyncSendHandler);

CFNotificationCenterAddObserver(
Expand All @@ -70,7 +70,7 @@ static void asyncSendHandler(uv_async_t *handle) {
}

NAN_METHOD(KeyboardLayoutObserver::GetInstalledKeyboardLanguages) {
NanScope();
Nan::HandleScope scope;

@autoreleasepool {
std::vector<std::string> ret;
Expand Down Expand Up @@ -99,29 +99,29 @@ static void asyncSendHandler(uv_async_t *handle) {
ret.push_back(str);
}

Local<Array> result = NanNew<Array>(ret.size());
Local<Array> result = Nan::New<Array>(ret.size());
for (size_t i = 0; i < ret.size(); ++i) {
const std::string& lang = ret[i];
result->Set(i, NanNew<String>(lang.data(), lang.size()));
result->Set(i, Nan::New<String>(lang.data(), lang.size()).ToLocalChecked());
}

NanReturnValue(result);
info.GetReturnValue().Set(result);
}
}

NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLanguage) {
NanScope();
Nan::HandleScope scope;
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();

NSArray* langs = (NSArray*) TISGetInputSourceProperty(source, kTISPropertyInputSourceLanguages);
NSString* lang = (NSString*) [langs objectAtIndex:0];

NanReturnValue(NanNew([lang UTF8String]));
info.GetReturnValue().Set(Nan::New([lang UTF8String]).ToLocalChecked());
}

NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLayout) {
NanScope();
Nan::HandleScope scope;
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
CFStringRef sourceId = (CFStringRef) TISGetInputSourceProperty(source, kTISPropertyInputSourceID);
NanReturnValue(NanNew([(NSString *)sourceId UTF8String]));
info.GetReturnValue().Set(Nan::New([(NSString *)sourceId UTF8String]).ToLocalChecked());
}
34 changes: 17 additions & 17 deletions src/keyboard-layout-observer-non-mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
using namespace v8;

void KeyboardLayoutObserver::Init(Handle<Object> target) {
NanScope();
Local<FunctionTemplate> newTemplate = NanNew<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(NanNew<String>("KeyboardLayoutObserver"));
Nan::HandleScope scope;
Local<FunctionTemplate> newTemplate = Nan::New<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked());
newTemplate->InstanceTemplate()->SetInternalFieldCount(1);
Local<ObjectTemplate> proto = newTemplate->PrototypeTemplate();
NODE_SET_METHOD(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
NODE_SET_METHOD(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLayout); // NB: Intentionally mapped to same stub
NODE_SET_METHOD(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);
target->Set(NanNew<String>("KeyboardLayoutObserver"), newTemplate->GetFunction());
Nan::SetMethod(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
Nan::SetMethod(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLayout); // NB: Intentionally mapped to same stub
Nan::SetMethod(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);
target->Set(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked(), newTemplate->GetFunction());
}

NODE_MODULE(keyboard_layout_observer, KeyboardLayoutObserver::Init)

NAN_METHOD(KeyboardLayoutObserver::New) {
NanScope();
Nan::HandleScope scope;

Local<Function> callbackHandle = args[0].As<Function>();
NanCallback *callback = new NanCallback(callbackHandle);
Local<Function> callbackHandle = info[0].As<Function>();
Nan::Callback *callback = new Nan::Callback(callbackHandle);

KeyboardLayoutObserver *observer = new KeyboardLayoutObserver(callback);
observer->Wrap(args.This());
NanReturnUndefined();
observer->Wrap(info.This());
return;
}

KeyboardLayoutObserver::KeyboardLayoutObserver(NanCallback *callback) : callback(callback) {
KeyboardLayoutObserver::KeyboardLayoutObserver(Nan::Callback *callback) : callback(callback) {
}

KeyboardLayoutObserver::~KeyboardLayoutObserver() {
Expand All @@ -38,11 +38,11 @@ void KeyboardLayoutObserver::HandleKeyboardLayoutChanged() {
}

NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLayout) {
NanScope();
NanReturnUndefined();
Nan::HandleScope scope;
return;
}

NAN_METHOD(KeyboardLayoutObserver::GetInstalledKeyboardLanguages) {
NanScope();
NanReturnUndefined();
Nan::HandleScope scope;
return;
}
44 changes: 22 additions & 22 deletions src/keyboard-layout-observer-windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ std::string ToUTF8(const std::wstring& string) {
}

void KeyboardLayoutObserver::Init(Handle<Object> target) {
NanScope();
Local<FunctionTemplate> newTemplate = NanNew<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(NanNew<String>("KeyboardLayoutObserver"));
Nan::HandleScope scope;
Local<FunctionTemplate> newTemplate = Nan::New<FunctionTemplate>(KeyboardLayoutObserver::New);
newTemplate->SetClassName(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked());
newTemplate->InstanceTemplate()->SetInternalFieldCount(1);
Local<ObjectTemplate> proto = newTemplate->PrototypeTemplate();

NODE_SET_METHOD(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
NODE_SET_METHOD(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLanguage);
NODE_SET_METHOD(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);
target->Set(NanNew<String>("KeyboardLayoutObserver"), newTemplate->GetFunction());
Nan::SetMethod(proto, "getCurrentKeyboardLayout", KeyboardLayoutObserver::GetCurrentKeyboardLayout);
Nan::SetMethod(proto, "getCurrentKeyboardLanguage", KeyboardLayoutObserver::GetCurrentKeyboardLanguage);
Nan::SetMethod(proto, "getInstalledKeyboardLanguages", KeyboardLayoutObserver::GetInstalledKeyboardLanguages);
target->Set(Nan::New<String>("KeyboardLayoutObserver").ToLocalChecked(), newTemplate->GetFunction());
}

NODE_MODULE(keyboard_layout_observer, KeyboardLayoutObserver::Init)

NAN_METHOD(KeyboardLayoutObserver::New) {
NanScope();
Nan::HandleScope scope;

Local<Function> callbackHandle = args[0].As<Function>();
NanCallback *callback = new NanCallback(callbackHandle);
Local<Function> callbackHandle = info[0].As<Function>();
Nan::Callback *callback = new Nan::Callback(callbackHandle);

KeyboardLayoutObserver *observer = new KeyboardLayoutObserver(callback);
observer->Wrap(args.This());
NanReturnUndefined();
observer->Wrap(info.This());
return;
}

KeyboardLayoutObserver::KeyboardLayoutObserver(NanCallback *callback) : callback(callback) {
KeyboardLayoutObserver::KeyboardLayoutObserver(Nan::Callback *callback) : callback(callback) {
}

KeyboardLayoutObserver::~KeyboardLayoutObserver() {
Expand All @@ -65,17 +65,17 @@ void KeyboardLayoutObserver::HandleKeyboardLayoutChanged() {
}

NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLayout) {
NanScope();
Nan::HandleScope scope;

char layoutName[KL_NAMELENGTH];
if (::GetKeyboardLayoutName(layoutName))
NanReturnValue(NanNew(layoutName));
info.GetReturnValue().Set(Nan::New(layoutName).ToLocalChecked());
else
NanReturnValue(NanUndefined());
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLanguage) {
NanScope();
Nan::HandleScope scope;

HKL layout;
DWORD dwThreadId = 0;
Expand All @@ -93,17 +93,17 @@ NAN_METHOD(KeyboardLayoutObserver::GetCurrentKeyboardLanguage) {
wstr.assign(buf);

std::string str = ToUTF8(wstr);
NanReturnValue(NanNew<String>(str.data(), str.size()));
info.GetReturnValue().Set(Nan::New<String>(str.data(), str.size()).ToLocalChecked());
}

NAN_METHOD(KeyboardLayoutObserver::GetInstalledKeyboardLanguages) {
NanScope();
Nan::HandleScope scope;

int layoutCount = GetKeyboardLayoutList(0, NULL);
HKL* layouts = new HKL[layoutCount];
GetKeyboardLayoutList(layoutCount, layouts);

Local<Array> result = NanNew<Array>(layoutCount);
Local<Array> result = Nan::New<Array>(layoutCount);
wchar_t buf[LOCALE_NAME_MAX_LENGTH];

for (int i=0; i < layoutCount; i++) {
Expand All @@ -112,9 +112,9 @@ NAN_METHOD(KeyboardLayoutObserver::GetInstalledKeyboardLanguages) {
wstr.assign(buf);

std::string str = ToUTF8(wstr);
result->Set(i, NanNew<String>(str.data(), str.size()));
result->Set(i, Nan::New<String>(str.data(), str.size()).ToLocalChecked());
}

delete[] layouts;
NanReturnValue(result);
info.GetReturnValue().Set(result);
}
6 changes: 3 additions & 3 deletions src/keyboard-layout-observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

using namespace v8; // NOLINT

class KeyboardLayoutObserver : public node::ObjectWrap {
class KeyboardLayoutObserver : public Nan::ObjectWrap {
public:
static void Init(Handle<Object> target);
void HandleKeyboardLayoutChanged();

private:
KeyboardLayoutObserver(NanCallback *callback);
KeyboardLayoutObserver(Nan::Callback *callback);
~KeyboardLayoutObserver();
static NAN_METHOD(New);
static NAN_METHOD(GetCurrentKeyboardLayout);
static NAN_METHOD(GetCurrentKeyboardLanguage);
static NAN_METHOD(GetInstalledKeyboardLanguages);

NanCallback *callback;
Nan::Callback *callback;
};

#endif // SRC_KEYBORD_LAYOUT_OBSERVER_H_