From 74f43a608c4b379b7d8afbe4d328a1d25d693dd7 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 30 Jan 2019 12:11:07 -0800 Subject: [PATCH] chore: [mac] refactor to use nan apis --- src/keyboard-layout-manager-mac.mm | 54 ++++++++++++++---------------- src/keyboard-layout-manager.h | 2 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/keyboard-layout-manager-mac.mm b/src/keyboard-layout-manager-mac.mm index b7d3f06..2440b08 100644 --- a/src/keyboard-layout-manager-mac.mm +++ b/src/keyboard-layout-manager-mac.mm @@ -7,22 +7,20 @@ #include #include -using namespace v8; - -void KeyboardLayoutManager::Init(Local exports, Local module) { +NAN_MODULE_INIT(KeyboardLayoutManager::Init) { Nan::HandleScope scope; - Local newTemplate = Nan::New(KeyboardLayoutManager::New); - newTemplate->SetClassName(Nan::New("KeyboardLayoutManager").ToLocalChecked()); + v8::Local newTemplate = Nan::New(KeyboardLayoutManager::New); + newTemplate->SetClassName(Nan::New("KeyboardLayoutManager").ToLocalChecked()); newTemplate->InstanceTemplate()->SetInternalFieldCount(1); - Local proto = newTemplate->PrototypeTemplate(); + v8::Local proto = newTemplate->PrototypeTemplate(); Nan::SetMethod(proto, "getCurrentKeyboardLayout", KeyboardLayoutManager::GetCurrentKeyboardLayout); Nan::SetMethod(proto, "getCurrentKeyboardLanguage", KeyboardLayoutManager::GetCurrentKeyboardLanguage); Nan::SetMethod(proto, "getInstalledKeyboardLanguages", KeyboardLayoutManager::GetInstalledKeyboardLanguages); Nan::SetMethod(proto, "getCurrentKeymap", KeyboardLayoutManager::GetCurrentKeymap); - module->Set(Nan::New("exports").ToLocalChecked(), newTemplate->GetFunction()); + Nan::Set(target, Nan::New("exports").ToLocalChecked(), Nan::GetFunction(newTemplate).ToLocalChecked()); } NODE_MODULE(keyboard_layout_manager, KeyboardLayoutManager::Init) @@ -30,7 +28,7 @@ NAN_METHOD(KeyboardLayoutManager::New) { Nan::HandleScope scope; - Local callbackHandle = info[0].As(); + v8::Local callbackHandle = info[0].As(); Nan::Callback *callback = new Nan::Callback(callbackHandle); KeyboardLayoutManager *manager = new KeyboardLayoutManager(callback); @@ -68,7 +66,7 @@ static void asyncSendHandler(uv_async_t *handle) { }; void KeyboardLayoutManager::HandleKeyboardLayoutChanged() { - callback->Call(0, NULL); + Nan::Call(*callback, 0, NULL); } NAN_METHOD(KeyboardLayoutManager::GetInstalledKeyboardLanguages) { @@ -101,10 +99,10 @@ static void asyncSendHandler(uv_async_t *handle) { ret.push_back(str); } - Local result = Nan::New(ret.size()); + v8::Local result = Nan::New(ret.size()); for (size_t i = 0; i < ret.size(); ++i) { const std::string& lang = ret[i]; - result->Set(i, Nan::New(lang.data(), lang.size()).ToLocalChecked()); + Nan::Set(result, i, Nan::New(lang.data(), lang.size()).ToLocalChecked()); } info.GetReturnValue().Set(result); @@ -138,7 +136,7 @@ static void asyncSendHandler(uv_async_t *handle) { #include "keycode_converter_data.inc" -Local CharacterForNativeCode(const UCKeyboardLayout* keyboardLayout, UInt16 virtualKeyCode, EventModifiers modifiers) { +v8::Local CharacterForNativeCode(const UCKeyboardLayout* keyboardLayout, UInt16 virtualKeyCode, EventModifiers modifiers) { // See https://developer.apple.com/reference/coreservices/1390584-uckeytranslate?language=objc UInt32 modifierKeyState = (modifiers >> 8) & 0xFF; UInt32 deadKeyState = 0; @@ -190,32 +188,32 @@ static void asyncSendHandler(uv_async_t *handle) { const UCKeyboardLayout* keyboardLayout = reinterpret_cast(CFDataGetBytePtr(layoutData)); - Local result = Nan::New(); - Local unmodifiedKey = Nan::New("unmodified").ToLocalChecked(); - Local withShiftKey = Nan::New("withShift").ToLocalChecked(); - Local withAltGraphKey = Nan::New("withAltGraph").ToLocalChecked(); - Local withAltGraphShiftKey = Nan::New("withAltGraphShift").ToLocalChecked(); + v8::Local result = Nan::New(); + v8::Local unmodifiedKey = Nan::New("unmodified").ToLocalChecked(); + v8::Local withShiftKey = Nan::New("withShift").ToLocalChecked(); + v8::Local withAltGraphKey = Nan::New("withAltGraph").ToLocalChecked(); + v8::Local withAltGraphShiftKey = Nan::New("withAltGraphShift").ToLocalChecked(); size_t keyCodeMapSize = sizeof(keyCodeMap) / sizeof(keyCodeMap[0]); for (size_t i = 0; i < keyCodeMapSize; i++) { const char *dom3Code = keyCodeMap[i].dom3Code; int virtualKeyCode = keyCodeMap[i].virtualKeyCode; if (dom3Code && virtualKeyCode < 0xffff) { - Local dom3CodeKey = Nan::New(dom3Code).ToLocalChecked(); + v8::Local dom3CodeKey = Nan::New(dom3Code).ToLocalChecked(); - Local unmodified = CharacterForNativeCode(keyboardLayout, virtualKeyCode, 0); - Local withShift = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << shiftKeyBit)); - Local withAltGraph = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << optionKeyBit)); - Local withAltGraphShift = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << shiftKeyBit) | (1 << optionKeyBit)); + v8::Local unmodified = CharacterForNativeCode(keyboardLayout, virtualKeyCode, 0); + v8::Local withShift = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << shiftKeyBit)); + v8::Local withAltGraph = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << optionKeyBit)); + v8::Local withAltGraphShift = CharacterForNativeCode(keyboardLayout, virtualKeyCode, (1 << shiftKeyBit) | (1 << optionKeyBit)); if (unmodified->IsString() || withShift->IsString() || withAltGraph->IsString() || withAltGraphShift->IsString()) { - Local entry = Nan::New(); - entry->Set(unmodifiedKey, unmodified); - entry->Set(withShiftKey, withShift); - entry->Set(withAltGraphKey, withAltGraph); - entry->Set(withAltGraphShiftKey, withAltGraphShift); + v8::Local entry = Nan::New(); + Nan::Set(entry, unmodifiedKey, unmodified); + Nan::Set(entry, withShiftKey, withShift); + Nan::Set(entry, withAltGraphKey, withAltGraph); + Nan::Set(entry, withAltGraphShiftKey, withAltGraphShift); - result->Set(dom3CodeKey, entry); + Nan::Set(result, dom3CodeKey, entry); } } } diff --git a/src/keyboard-layout-manager.h b/src/keyboard-layout-manager.h index 4469bc5..04f00f8 100644 --- a/src/keyboard-layout-manager.h +++ b/src/keyboard-layout-manager.h @@ -9,7 +9,7 @@ class KeyboardLayoutManager : public Nan::ObjectWrap { public: - static void Init(v8::Local target, v8::Local module); + static NAN_MODULE_INIT(Init); void HandleKeyboardLayoutChanged(); private: