add shortcut types

This commit is contained in:
raub 2018-03-26 14:19:22 +03:00
parent 58df0274b9
commit b6a7936344
5 changed files with 187 additions and 33 deletions

116
.eslintrc Normal file
View File

@ -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]
}
}

View File

@ -352,6 +352,21 @@ glfwSetWindowFocusCallback(window, windowFocusCB);
* `RET_UNDEFINED` - set method return value as undefined
#### Shortcut types
* `V8_VAR_VAL` = `v8::Local<v8::Value>`
* `V8_VAR_OBJ` = `v8::Local<v8::Object>`
* `V8_VAR_ARR` = `v8::Local<v8::Array>`
* `V8_VAR_STR` = `v8::Local<v8::String>`
* `V8_VAR_FUNC` = `v8::Local<v8::Function>`
* `V8_VAR_FT` = `v8::Local<v8::FunctionTemplate>`
* `V8_VAR_OT` = `v8::Local<v8::ObjectTemplate>`
* `V8_STORE_FT` = `Nan::Persistent<v8::FunctionTemplate>`
* `V8_STORE_FUNC` = `Nan::Persistent<v8::Function>`
* `V8_STORE_OBJ` = `Nan::Persistent<v8::Object>`
* `V8_STORE_VAL` = `Nan::Persistent<v8::Value>`
#### New JS value
* `JS_STR(...)` - create a string value

View File

@ -12,6 +12,20 @@
#define RET_UNDEFINED RET_VALUE(Nan::Undefined());
typedef v8::Local<v8::Value> V8_VAR_VAL;
typedef v8::Local<v8::Object> V8_VAR_OBJ;
typedef v8::Local<v8::Array> V8_VAR_ARR;
typedef v8::Local<v8::String> V8_VAR_STR;
typedef v8::Local<v8::Function> V8_VAR_FUNC;
typedef v8::Local<v8::FunctionTemplate> V8_VAR_FT;
typedef v8::Local<v8::ObjectTemplate> V8_VAR_OT;
typedef Nan::Persistent<v8::FunctionTemplate> V8_STORE_FT;
typedef Nan::Persistent<v8::Function> V8_STORE_FUNC;
typedef Nan::Persistent<v8::Object> V8_STORE_OBJ;
typedef Nan::Persistent<v8::Value> V8_STORE_VAL;
#define JS_STR(...) Nan::New<v8::String>(__VA_ARGS__).ToLocalChecked()
#define JS_UTF8(...) Nan::New<v8::String>(__VA_ARGS__).ToLocalChecked()
#define JS_INT(val) Nan::New<v8::Integer>(val)

View File

@ -4,6 +4,7 @@
#include <addon-tools.hpp>
#include <string>
#include <map>
#include <deque>
#include <iostream> // -> std::cout << "..." << std::endl;
@ -20,11 +21,11 @@
template <typename T>
class StaticHolder {
protected:
static Nan::Persistent<v8::FunctionTemplate> _protoEventEmitter;
static Nan::Persistent<v8::Function> _ctorEventEmitter;
static V8_STORE_FT _protoEventEmitter;
static V8_STORE_FUNC _ctorEventEmitter;
};
template <typename T> Nan::Persistent<v8::FunctionTemplate> StaticHolder<T>::_protoEventEmitter;
template <typename T> Nan::Persistent<v8::Function> StaticHolder<T>::_ctorEventEmitter;
template <typename T> V8_STORE_FT StaticHolder<T>::_protoEventEmitter;
template <typename T> V8_STORE_FUNC StaticHolder<T>::_ctorEventEmitter;
class EventEmitter : public StaticHolder<int>, public Nan::ObjectWrap {
@ -40,16 +41,16 @@ class EventEmitter : public StaticHolder<int>, public Nan::ObjectWrap {
public:
// Public V8 init
static void init(v8::Local<v8::Object> target) {
static void init(V8_VAR_OBJ target) {
v8::Local<v8::FunctionTemplate> proto = Nan::New<v8::FunctionTemplate>(newCtor);
V8_VAR_FT proto = Nan::New<v8::FunctionTemplate>(newCtor);
proto->InstanceTemplate()->SetInternalFieldCount(1);
proto->SetClassName(JS_STR("EventEmitter"));
// Accessors
v8::Local<v8::ObjectTemplate> obj = proto->PrototypeTemplate();
V8_VAR_OT obj = proto->PrototypeTemplate();
ACCESSOR_R(obj, isDestroyed);
@ -72,9 +73,9 @@ public:
// -------- static
v8::Local<v8::Function> ctor = Nan::GetFunction(proto).ToLocalChecked();
V8_VAR_FUNC ctor = Nan::GetFunction(proto).ToLocalChecked();
v8::Local<v8::Object> ctorObj = v8::Local<v8::Object>::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<v8::Value> *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<v8::Value> that, const std::string &method) {
void on(const std::string &name, V8_VAR_VAL that, const std::string &method) {
v8::Local<v8::String> code = JS_STR(
"((emitter, name, that, method) => emitter.on(name, that[method]))"
);
v8::Local<v8::Function> connector = v8::Local<v8::Function>::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<v8::Object> emitter = Nan::New<v8::Object>();
V8_VAR_OBJ emitter = Nan::New<v8::Object>();
this->Wrap(emitter);
v8::Local<v8::Value> 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<v8::Value> > 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<v8::Value> &info,
const std::string &name,
Nan::Persistent<v8::Function> &cb,
V8_STORE_FUNC &cb,
bool isFront
) { THIS_EVENT_EMITTER;
v8::Local<v8::Value> 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<v8::Function> 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<v8::Value> &info,
const std::string &name,
Nan::Persistent<v8::Function> &raw,
Nan::Persistent<v8::Function> &cb,
V8_STORE_FUNC &raw,
V8_STORE_FUNC &cb,
bool isFront
) { THIS_EVENT_EMITTER;
v8::Local<v8::Value> 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<v8::Function> decor = v8::Local<v8::Function>::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<v8::Value> argv[] = { info.This(), info[0], raw };
V8_VAR_VAL argv[] = { info.This(), info[0], raw };
Nan::AsyncResource async("EventEmitter::js_once()");
v8::Local<v8::Value> wrapValue = decorCb.Call(3, argv, &async).ToLocalChecked();
v8::Local<v8::Function> wrap = v8::Local<v8::Function>::Cast(wrapValue);
V8_VAR_VAL wrapValue = decorCb.Call(3, argv, &async).ToLocalChecked();
V8_VAR_FUNC wrap = V8_VAR_FUNC::Cast(wrapValue);
Nan::Persistent<v8::Function> persistentWrap;
V8_STORE_FUNC persistentWrap;
persistentWrap.Reset(wrap);
Nan::Persistent<v8::Function> 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<v8::Value> 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<v8::Value> 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<v8::Function> persistentRaw;
V8_STORE_FUNC persistentRaw;
persistentRaw.Reset(raw);
std::string name = std::string(*n);
@ -521,7 +522,7 @@ private:
return;
}
v8::Local<v8::Value> args[] = { info[0], info[1] };
V8_VAR_VAL args[] = { info[0], info[1] };
for (IT_TYPE it = rawList.begin(); it != rawList.end(); ++it) {

View File

@ -1,8 +1,8 @@
{
"name": "addon-tools-raub",
"version": "0.1.5",
"author": "Luis Blanco <raubtierxxx@gmail.com>",
"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": [
{