From b6a79363443e2fa9423a46f540744de7c5f5fb2b Mon Sep 17 00:00:00 2001 From: raub Date: Mon, 26 Mar 2018 14:19:22 +0300 Subject: [PATCH] add shortcut types --- .eslintrc | 116 ++++++++++++++++++++++++++++++++++++++ README.md | 15 +++++ include/addon-tools.hpp | 14 +++++ include/event-emitter.hpp | 63 +++++++++++---------- package.json | 12 +++- 5 files changed, 187 insertions(+), 33 deletions(-) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..c3126e7 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,116 @@ +{ + "root": true, + "env": { + "node" : true, + "es6" : true, + "mocha" : true + }, + "globals": { + "expect" : true, + "chai" : true, + "sinon" : true + }, + "extends": ["eslint:recommended"], + "parserOptions": { + "ecmaVersion": 8, + "ecmaFeatures": { + "experimentalObjectRestSpread": true + } + }, + "rules": { + "arrow-parens": ["error", "as-needed"], + "no-trailing-spaces": [ + "error", + { + "skipBlankLines": true + } + ], + "indent": [ + "error", + "tab", + { + "SwitchCase": 1 + } + ], + "linebreak-style": [ + "error", + "unix" + ], + "max-len": ["error", 110], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "always" + ], + "no-multiple-empty-lines": ["error", { "max": 3, "maxEOF": 1, "maxBOF": 1 }], + "keyword-spacing": ["error", { "before": true, "after": true }], + "space-before-blocks": ["error"], + "space-before-function-paren": ["error", {"anonymous": "always", "named": "never", "asyncArrow": "always"}], + "space-infix-ops": ["error"], + "space-unary-ops": [ + "error", { + "words": true, + "nonwords": false, + "overrides": { + "!": true + } + } + ], + "spaced-comment": [0], + "camelcase": ["error"], + "no-tabs": [0], + "comma-dangle": [0], + "global-require": [0], + "func-names": [0], + "no-param-reassign": [0], + "no-underscore-dangle": [0], + "no-restricted-syntax": [ + "error", + { + "selector": "LabeledStatement", + "message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand." + }, + { + "selector": "WithStatement", + "message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize." + } + ], + "no-mixed-operators": [0], + "no-plusplus": [0], + "comma-spacing": [0], + "default-case": [0], + "no-shadow": [0], + "no-console": [0], + "key-spacing": [0], + "no-return-assign": [0], + "consistent-return": [0], + "class-methods-use-this": [0], + "no-multi-spaces": [ + "error", + { + "exceptions": { + "VariableDeclarator": true, + "Property": true, + "ImportDeclaration": true + } + } + ], + "array-callback-return": [0], + "no-use-before-define": [ + "error", + { + "functions": false, + "classes": true, + "variables": true + } + ], + "padded-blocks": [0], + "space-in-parens": [0], + "valid-jsdoc": [0], + "no-unused-expressions": [0], + "import/no-dynamic-require": [0] + } +} diff --git a/README.md b/README.md index 19c7b38..417908d 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,21 @@ glfwSetWindowFocusCallback(window, windowFocusCB); * `RET_UNDEFINED` - set method return value as undefined +#### Shortcut types + +* `V8_VAR_VAL` = `v8::Local` +* `V8_VAR_OBJ` = `v8::Local` +* `V8_VAR_ARR` = `v8::Local` +* `V8_VAR_STR` = `v8::Local` +* `V8_VAR_FUNC` = `v8::Local` +* `V8_VAR_FT` = `v8::Local` +* `V8_VAR_OT` = `v8::Local` +* `V8_STORE_FT` = `Nan::Persistent` +* `V8_STORE_FUNC` = `Nan::Persistent` +* `V8_STORE_OBJ` = `Nan::Persistent` +* `V8_STORE_VAL` = `Nan::Persistent` + + #### New JS value * `JS_STR(...)` - create a string value diff --git a/include/addon-tools.hpp b/include/addon-tools.hpp index 040a936..b13320f 100644 --- a/include/addon-tools.hpp +++ b/include/addon-tools.hpp @@ -12,6 +12,20 @@ #define RET_UNDEFINED RET_VALUE(Nan::Undefined()); +typedef v8::Local V8_VAR_VAL; +typedef v8::Local V8_VAR_OBJ; +typedef v8::Local V8_VAR_ARR; +typedef v8::Local V8_VAR_STR; +typedef v8::Local V8_VAR_FUNC; +typedef v8::Local V8_VAR_FT; +typedef v8::Local V8_VAR_OT; + +typedef Nan::Persistent V8_STORE_FT; +typedef Nan::Persistent V8_STORE_FUNC; +typedef Nan::Persistent V8_STORE_OBJ; +typedef Nan::Persistent V8_STORE_VAL; + + #define JS_STR(...) Nan::New(__VA_ARGS__).ToLocalChecked() #define JS_UTF8(...) Nan::New(__VA_ARGS__).ToLocalChecked() #define JS_INT(val) Nan::New(val) diff --git a/include/event-emitter.hpp b/include/event-emitter.hpp index 175321a..1e7ba42 100644 --- a/include/event-emitter.hpp +++ b/include/event-emitter.hpp @@ -4,6 +4,7 @@ #include +#include #include #include #include // -> std::cout << "..." << std::endl; @@ -20,11 +21,11 @@ template class StaticHolder { protected: - static Nan::Persistent _protoEventEmitter; - static Nan::Persistent _ctorEventEmitter; + static V8_STORE_FT _protoEventEmitter; + static V8_STORE_FUNC _ctorEventEmitter; }; -template Nan::Persistent StaticHolder::_protoEventEmitter; -template Nan::Persistent StaticHolder::_ctorEventEmitter; +template V8_STORE_FT StaticHolder::_protoEventEmitter; +template V8_STORE_FUNC StaticHolder::_ctorEventEmitter; class EventEmitter : public StaticHolder, public Nan::ObjectWrap { @@ -40,16 +41,16 @@ class EventEmitter : public StaticHolder, public Nan::ObjectWrap { public: // Public V8 init - static void init(v8::Local target) { + static void init(V8_VAR_OBJ target) { - v8::Local proto = Nan::New(newCtor); + V8_VAR_FT proto = Nan::New(newCtor); proto->InstanceTemplate()->SetInternalFieldCount(1); proto->SetClassName(JS_STR("EventEmitter")); // Accessors - v8::Local obj = proto->PrototypeTemplate(); + V8_VAR_OT obj = proto->PrototypeTemplate(); ACCESSOR_R(obj, isDestroyed); @@ -72,9 +73,9 @@ public: // -------- static - v8::Local ctor = Nan::GetFunction(proto).ToLocalChecked(); + V8_VAR_FUNC ctor = Nan::GetFunction(proto).ToLocalChecked(); - v8::Local ctorObj = v8::Local::Cast(ctor); + V8_VAR_OBJ ctorObj = V8_VAR_OBJ::Cast(ctor); Nan::SetMethod(ctorObj, "listenerCount", jsStaticListenerCount); @@ -88,7 +89,7 @@ public: // C++ side emit() method - void emit(const std::string &name, int argc = 0, v8::Local *argv = NULL) { + void emit(const std::string &name, int argc = 0, V8_VAR_VAL *argv = NULL) { // Important! As actual get map[key] produces a new (empty) map entry if ( _listeners.find(name) == _listeners.end() ) { @@ -117,19 +118,19 @@ public: // C++ side on() method - void on(const std::string &name, v8::Local that, const std::string &method) { + void on(const std::string &name, V8_VAR_VAL that, const std::string &method) { v8::Local code = JS_STR( "((emitter, name, that, method) => emitter.on(name, that[method]))" ); - v8::Local connector = v8::Local::Cast(v8::Script::Compile(code)->Run()); + V8_VAR_FUNC connector = V8_VAR_FUNC::Cast(v8::Script::Compile(code)->Run()); Nan::Callback connectorCb(connector); - v8::Local emitter = Nan::New(); + V8_VAR_OBJ emitter = Nan::New(); this->Wrap(emitter); - v8::Local argv[] = { emitter, JS_STR(name.c_str()), that, JS_STR(method.c_str()) }; + V8_VAR_VAL argv[] = { emitter, JS_STR(name.c_str()), that, JS_STR(method.c_str()) }; Nan::AsyncResource async("EventEmitter::cpp_on()"); connectorCb.Call(4, argv, &async); @@ -204,7 +205,7 @@ private: int length = info.Length(); - std::vector< v8::Local > args; + std::vector< V8_VAR_VAL > args; for (int i = 1; i < length; i++) { args.push_back(info[i]); @@ -282,11 +283,11 @@ private: static inline void _addListener( const Nan::FunctionCallbackInfo &info, const std::string &name, - Nan::Persistent &cb, + V8_STORE_FUNC &cb, bool isFront ) { THIS_EVENT_EMITTER; - v8::Local args[] = { info[0], info[1] }; + V8_VAR_VAL args[] = { info[0], info[1] }; eventEmitter->emit("newListener", 2, args); if (isFront) { @@ -328,7 +329,7 @@ private: REQ_UTF8_ARG(0, name); REQ_FUN_ARG(1, cb); - Nan::Persistent persistentCb; + V8_STORE_FUNC persistentCb; persistentCb.Reset(cb); _addListener(info, *name, persistentCb, isFront); @@ -339,12 +340,12 @@ private: static inline void _addOnceListener( const Nan::FunctionCallbackInfo &info, const std::string &name, - Nan::Persistent &raw, - Nan::Persistent &cb, + V8_STORE_FUNC &raw, + V8_STORE_FUNC &cb, bool isFront ) { THIS_EVENT_EMITTER; - v8::Local args[] = { info[0], info[1] }; + V8_VAR_VAL args[] = { info[0], info[1] }; eventEmitter->emit("newListener", 2, args); if (isFront) { @@ -397,17 +398,17 @@ private: })" ); - v8::Local decor = v8::Local::Cast(v8::Script::Compile(code)->Run()); + V8_VAR_FUNC decor = V8_VAR_FUNC::Cast(v8::Script::Compile(code)->Run()); Nan::Callback decorCb(decor); - v8::Local argv[] = { info.This(), info[0], raw }; + V8_VAR_VAL argv[] = { info.This(), info[0], raw }; Nan::AsyncResource async("EventEmitter::js_once()"); - v8::Local wrapValue = decorCb.Call(3, argv, &async).ToLocalChecked(); - v8::Local wrap = v8::Local::Cast(wrapValue); + V8_VAR_VAL wrapValue = decorCb.Call(3, argv, &async).ToLocalChecked(); + V8_VAR_FUNC wrap = V8_VAR_FUNC::Cast(wrapValue); - Nan::Persistent persistentWrap; + V8_STORE_FUNC persistentWrap; persistentWrap.Reset(wrap); - Nan::Persistent persistentRaw; + V8_STORE_FUNC persistentRaw; persistentRaw.Reset(raw); _addOnceListener(info, *name, persistentRaw, persistentWrap, isFront); @@ -442,7 +443,7 @@ private: for (IT_TYPE it = list.begin(); it != list.end(); ++it) { - v8::Local args[] = { JS_STR(current.c_str()), Nan::New(*it) }; + V8_VAR_VAL args[] = { JS_STR(current.c_str()), Nan::New(*it) }; eventEmitter->emit("removeListener", 2, args); } @@ -497,7 +498,7 @@ private: for (IT_TYPE it = tmpVec.begin(); it != tmpVec.end(); ++it) { - v8::Local args[] = { JS_STR(name.c_str()), Nan::New(*it) }; + V8_VAR_VAL args[] = { JS_STR(name.c_str()), Nan::New(*it) }; eventEmitter->emit("removeListener", 2, args); } @@ -510,7 +511,7 @@ private: REQ_UTF8_ARG(0, n); REQ_FUN_ARG(1, raw); - Nan::Persistent persistentRaw; + V8_STORE_FUNC persistentRaw; persistentRaw.Reset(raw); std::string name = std::string(*n); @@ -521,7 +522,7 @@ private: return; } - v8::Local args[] = { info[0], info[1] }; + V8_VAR_VAL args[] = { info[0], info[1] }; for (IT_TYPE it = rawList.begin(); it != rawList.end(); ++it) { diff --git a/package.json b/package.json index 0741527..be02adb 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "addon-tools-raub", + "version": "0.1.5", "author": "Luis Blanco ", "description": "A set of extra tools for Node.js addons", - "version": "0.1.4", "main": "index.js", "keywords": [ "node", @@ -11,7 +11,15 @@ "include", "platform", "build", - "paths" + "paths", + "events", + "eventemitter", + "emitter", + "cpp", + "compile", + "macros", + "helpers", + "utils" ], "maintainers": [ {