minor improvements, more readme

This commit is contained in:
raub 2018-03-21 09:35:06 +03:00
parent 3a3b246806
commit d66062ba43
4 changed files with 100 additions and 66 deletions

124
README.md
View File

@ -161,7 +161,7 @@ Here `MY_ADDON` should be replaced by any name you like. Then require like
this: this:
``` ```
module.exports = require('./binary/addon'); module.exports = require('./binary/MY_ADDON');
``` ```
#### Generic addon snippet #### Generic addon snippet
@ -304,7 +304,7 @@ module.exports = require('./binary/addon');
There is a C++ header file, `addon-tools.hpp`, shipped with this package. It There is a C++ header file, `addon-tools.hpp`, shipped with this package. It
introduces several useful macros and utilities. Also it includes Nan automatically, introduces several useful macros and utilities. Also it includes Nan automatically,
so that you can replace. so that you can replace:
``` ```
// #include <v8.h> // node.h includes it // #include <v8.h> // node.h includes it
@ -323,7 +323,8 @@ As it was mentioned above, this can be done automatically. Also an actual path t
directory is exported from the module and is accessible like this: directory is exported from the module and is accessible like this:
``` ```
require('addon-tools-raub').include require('addon-tools-raub').include() // implicit console.log()
require('addon-tools-raub').includePath // just a string
``` ```
In the file, currently there are following helpers: In the file, currently there are following helpers:
@ -334,7 +335,7 @@ In the file, currently there are following helpers:
* `NAN_HS` - creates a HandleScope. Also, you do not need them within `NAN_METHOD`, * `NAN_HS` - creates a HandleScope. Also, you do not need them within `NAN_METHOD`,
`NAN_SETTER`, and `NAN_GETTER`, as it is stated in `NAN_SETTER`, and `NAN_GETTER`, as it is stated in
[Nan doc](https://github.com/nodejs/nan/blob/master/doc/methods.md#api_nan_method). [Nan doc](https://github.com/nodejs/nan/blob/master/doc/methods.md#api_nan_method).
So it is most likely to be used in native callbacks. So it is most likely to be used in parts of code called from C++ land.
``` ```
void windowFocusCB(GLFWwindow *window, int focused) { NAN_HS; void windowFocusCB(GLFWwindow *window, int focused) { NAN_HS;
@ -347,17 +348,25 @@ glfwSetWindowFocusCallback(window, windowFocusCB);
#### Method return #### Method return
* RET_VALUE(VAL) - set method return value * `RET_VALUE(VAL)` - set method return value
* RET_UNDEFINED - set method return value as undefined * `RET_UNDEFINED` - set method return value as undefined
#### New JS value #### New JS value
* JS_STR(...) - create a string value * `JS_STR(...)` - create a string value
* JS_INT(val) - create an integer value * `JS_UTF8(...)` - same as JS_STR
* JS_NUM(val) - create a numeric value * `JS_INT(val)` - create an integer value
* JS_EXT(val) - create an external (pointer) value * `JS_INT32(val)` - same as `JS_INT`
* JS_BOOL(val) - create a boolean value * `JS_UINT32(val)` - same as `JS_INT`
* `JS_NUM(val)` - create a numeric value
* `JS_OFFS(val)` - same as `JS_NUM`, but has a cast designed to avoid `size_t -> double` warning
* `JS_FLOAT(val)` - same as `JS_NUM`
* `JS_DOUBLE(val)` - same as `JS_NUM`
* `JS_EXT(val)` - create an external (pointer) value
* `JS_BOOL(val)` - create a boolean value
* `JS_FUN(val)` - get a function from persistent.
* `JS_OBJ(val)` - get an object from persistent.
#### Method check #### Method check
@ -368,12 +377,13 @@ in error messages. `C` is
check method, like `IsObject()`. `I` is the index of argument as in `info[I]`, check method, like `IsObject()`. `I` is the index of argument as in `info[I]`,
starting from `0`. starting from `0`.
* REQ_ARGS(N) - check if at least `N` arguments passed * `REQ_ARGS(N)` - check if at least `N` arguments passed
* IS_ARG_EMPTY(I) - check if argument `I` is `undefined` or `null` * `IS_ARG_EMPTY(I)` - check if argument `I` is `undefined` or `null`
* CHECK_REQ_ARG(I, C, T) - check if argument `I` is approved by `C` check. * `CHECK_REQ_ARG(I, C, T)` - check if argument `I` is approved by `C` check.
* CHECK_LET_ARG(I, C, T) - check if argument `I` is approved by `C` check or empty. * `CHECK_LET_ARG(I, C, T) - check if argument `I` is approved by `C` check or empty.
* CTOR_CHECK(T) - check if method is called as a constructor * `CTOR_CHECK(T)` - check if method is called as a constructor
* SETTER_CHECK(C, T) - check if setter `value` is approved by `C` check. * `SETTER_CHECK(C, T)` - check if setter `value` is approved by `C` check.
* `DES_CHECK` - within dynamic method check if the instance wasn't destroyed by `_destroy()`.
#### Method arguments #### Method arguments
@ -383,28 +393,32 @@ is that `LET_` allows the argument to be empty, using some zero-default in this
`I` is the index of argument as in `info[I]`, `I` is the index of argument as in `info[I]`,
starting from `0`. `VAR` is the name of the `Local<Value>` variable to be created. starting from `0`. `VAR` is the name of the `Local<Value>` variable to be created.
* REQ_UTF8_ARG(I, VAR) * `REQ_UTF8_ARG(I, VAR)` - require `I`'th argument to be a `string`. Stored at `Nan::Utf8String VAR`.
* LET_UTF8_ARG(I, VAR) * `LET_UTF8_ARG(I, VAR)` - let optional `I`'th argument to be a `string`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_INT32_ARG(I, VAR) * `REQ_STR_ARG(I, VAR)` - require `I`'th argument to be a `string`. Stored at `Nan::Utf8String VAR`.
* LET_INT32_ARG(I, VAR) * `LET_STR_ARG(I, VAR)` - let optional `I`'th argument to be a `string`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_BOOL_ARG(I, VAR) * `REQ_INT32_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `int VAR`.
* LET_BOOL_ARG(I, VAR) * `LET_INT32_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `0`. Stored at `int VAR`.
* REQ_UINT32_ARG(I, VAR) * `REQ_INT32_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `int VAR`.
* LET_UINT32_ARG(I, VAR) * `LET_INT32_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `0`. Stored at `int VAR`.
* REQ_OFFS_ARG(I, VAR) * `REQ_UINT32_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `unsigned VAR`.
* LET_OFFS_ARG(I, VAR) * `LET_UINT32_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `0`. Stored at `unsigned VAR`.
* REQ_DOUBLE_ARG(I, VAR) * `REQ_BOOL_ARG(I, VAR)` - require `I`'th argument to be a `boolean`. Stored at `bool VAR`.
* LET_DOUBLE_ARG(I, VAR) * `LET_BOOL_ARG(I, VAR)` - let optional `I`'th argument to be a `boolean`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_FLOAT_ARG(I, VAR) * `REQ_OFFS_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `size_t VAR`.
* LET_FLOAT_ARG(I, VAR) * `LET_OFFS_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_EXT_ARG(I, VAR) * `REQ_DOUBLE_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `double VAR`.
* LET_EXT_ARG(I, VAR) * `LET_DOUBLE_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_FUN_ARG(I, VAR) * `REQ_FLOAT_ARG(I, VAR)` - require `I`'th argument to be a `number`. Stored at `float VAR`.
* REQ_OBJ_ARG(I, VAR) * `LET_FLOAT_ARG(I, VAR)` - let optional `I`'th argument to be a `number`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* REQ_ARRV_ARG(I, VAR) * `REQ_EXT_ARG(I, VAR)` - require `I`'th argument to be an `external`. Stored at `Local<External> VAR`.
* `LET_EXT_ARG(I, VAR)` - let optional `I`'th argument to be an `external`, the default is `""`. Stored at `Nan::Utf8String VAR`.
* `REQ_FUN_ARG(I, VAR)` - require `I`'th argument to be a `function`. Stored at `Local<Function> VAR`.
* `REQ_OBJ_ARG(I, VAR)` - require `I`'th argument to be an `object`. Stored at `Local<Object> VAR`.
* `REQ_ARRV_ARG(I, VAR)` - require `I`'th argument to be a `TypedArray`. Stored at `Local<ArrayBufferView> VAR`.
``` ```
NAN_METHOD(testScene) { NAN_METHOD(test) {
REQ_UINT32_ARG(0, width); REQ_UINT32_ARG(0, width);
REQ_UINT32_ARG(1, height); REQ_UINT32_ARG(1, height);
@ -413,14 +427,16 @@ NAN_METHOD(testScene) {
... ...
``` ```
NOTE: The conversion from `Nan::Utf8String` to `std::string` (via `char *`) is possible with unary `*` operator.
#### Set properties #### Set properties
Set-helpers for string and numeric keys. String keys are converted to JS strings Set-helpers for string and numeric keys. String keys are converted to JS strings
automatically. automatically.
* SET_PROP(OBJ, KEY, VAL) * `SET_PROP(OBJ, KEY, VAL)`
* SET_I(ARR, I, VAL) * `SET_I(ARR, I, VAL)`
#### Set object accessors #### Set object accessors
@ -428,8 +444,8 @@ automatically.
Simplified accessor assignment, adds accessors of NAME for OBJ. Read accessor is Simplified accessor assignment, adds accessors of NAME for OBJ. Read accessor is
assumed to have the name `NAME+'Getter'` and write accessor is `NAME+'Setter'`. assumed to have the name `NAME+'Getter'` and write accessor is `NAME+'Setter'`.
* ACCESSOR_RW(OBJ, NAME) - add read and write accessors of NAME for OBJ. * `ACCESSOR_RW(OBJ, NAME)` - add read and write accessors of NAME for OBJ.
* ACCESSOR_R(OBJ, NAME) - read-only property. * `ACCESSOR_R(OBJ, NAME)` - read-only property.
``` ```
void MyClass::init(Handle<Object> target) { void MyClass::init(Handle<Object> target) {
@ -445,18 +461,22 @@ NAN_SETTER(MyClass::messageSetter) { ...
#### Setter argument #### Setter argument
Useful addition to NAN_SETTER macro. Useful addition to NAN_SETTER macro. Works similar to method arguments. But there
is always only one required argument stored in `v`.
* SETTER_UTF8_ARG * `SETTER_UTF8_ARG` - require the value to be a `string`. Stored at `Nan::Utf8String v`.
* SETTER_INT32_ARG * `SETTER_STR_ARG` - require the value to be a `string`. Stored at `Nan::Utf8String v`.
* SETTER_BOOL_ARG * `SETTER_INT32_ARG` - require the value to be a `number`. Stored at `int v`.
* SETTER_UINT32_ARG * `SETTER_INT_ARG` - require the value to be a `number`. Stored at `int v`.
* SETTER_OFFS_ARG * `SETTER_UINT32_ARG` - require the value to be a `number`. Stored at `unsigned v`.
* SETTER_DOUBLE_ARG * `SETTER_BOOL_ARG` - require the value to be a `boolean`. Stored at `bool v`.
* SETTER_FLOAT_ARG * `SETTER_OFFS_ARG` - require the value to be a `number`. Stored at `size_t v`.
* SETTER_EXT_ARG * `SETTER_DOUBLE_ARG` - require the value to be a `number`. Stored at `double v`.
* SETTER_FUN_ARG * `SETTER_FLOAT_ARG` - require the value to be a `number`. Stored at `float v`.
* SETTER_OBJ_ARG * `SETTER_EXT_ARG` - require the value to be an `external`. Stored at `Local<External> v`.
* `SETTER_FUN_ARG` - require the value to be a `function`. Stored at `Local<Function> v`.
* `SETTER_OBJ_ARG` - require the value to be an `object`. Stored at `Local<Object> v`.
* `SETTER_ARRV_ARG` - require the value to be a `TypedArray`. Stored at `Local<ArrayBufferView> v`.
``` ```
NAN_SETTER(MyClass::messageSetter) { SETTER_UTF8_ARG; NAN_SETTER(MyClass::messageSetter) { SETTER_UTF8_ARG;

View File

@ -20,6 +20,7 @@ protected:
private: private:
static NAN_METHOD(newCtor); static NAN_METHOD(newCtor);
static NAN_METHOD(destroy); static NAN_METHOD(destroy);

View File

@ -25,7 +25,6 @@
#define JS_BOOL(val) (val) ? Nan::True() : Nan::False() #define JS_BOOL(val) (val) ? Nan::True() : Nan::False()
#define JS_FUN(val) Nan::New<v8::Function>(val) #define JS_FUN(val) Nan::New<v8::Function>(val)
#define JS_OBJ(val) Nan::New<v8::Object>(val) #define JS_OBJ(val) Nan::New<v8::Object>(val)
#define JS_ARRV(val) Nan::New<v8::Object>(val)
#define REQ_ARGS(N) \ #define REQ_ARGS(N) \
@ -50,8 +49,10 @@
#define LET_UTF8_ARG(I, VAR) \ #define LET_UTF8_ARG(I, VAR) \
CHECK_LET_ARG(I, IsString(), "string"); \ CHECK_LET_ARG(I, IsString(), "string"); \
Nan::Utf8String VAR(info[I]); Nan::Utf8String VAR(JS_STR(""));
#define REQ_STR_ARG(I, VAR) REQ_UTF8_ARG(I, VAR)
#define LET_STR_ARG(I, VAR) LET_UTF8_ARG(I, VAR)
#define REQ_INT32_ARG(I, VAR) \ #define REQ_INT32_ARG(I, VAR) \
CHECK_REQ_ARG(I, IsInt32(), "int32"); \ CHECK_REQ_ARG(I, IsInt32(), "int32"); \
@ -61,16 +62,6 @@
CHECK_LET_ARG(I, IsInt32(), "int32"); \ CHECK_LET_ARG(I, IsInt32(), "int32"); \
int VAR = IS_ARG_EMPTY(I) ? 0 : info[I]->Int32Value(); int VAR = IS_ARG_EMPTY(I) ? 0 : info[I]->Int32Value();
#define REQ_BOOL_ARG(I, VAR) \
CHECK_REQ_ARG(I, IsBoolean(), "bool"); \
bool VAR = info[I]->BooleanValue();
#define LET_BOOL_ARG(I, VAR) \
CHECK_LET_ARG(I, IsBoolean(), "bool"); \
bool VAR = IS_ARG_EMPTY(I) ? false : info[I]->BooleanValue();
#define REQ_UINT32_ARG(I, VAR) \ #define REQ_UINT32_ARG(I, VAR) \
CHECK_REQ_ARG(I, IsUint32(), "uint32"); \ CHECK_REQ_ARG(I, IsUint32(), "uint32"); \
unsigned int VAR = info[I]->Uint32Value(); unsigned int VAR = info[I]->Uint32Value();
@ -79,6 +70,16 @@
CHECK_LET_ARG(I, IsUint32(), "uint32"); \ CHECK_LET_ARG(I, IsUint32(), "uint32"); \
unsigned int VAR = IS_ARG_EMPTY(I) ? 0 : info[I]->Uint32Value(); unsigned int VAR = IS_ARG_EMPTY(I) ? 0 : info[I]->Uint32Value();
#define REQ_INT_ARG(I, VAR) LET_INT32_ARG(I, VAR)
#define LET_INT_ARG(I, VAR) REQ_UINT32_ARG(I, VAR)
#define REQ_BOOL_ARG(I, VAR) \
CHECK_REQ_ARG(I, IsBoolean(), "bool"); \
bool VAR = info[I]->BooleanValue();
#define LET_BOOL_ARG(I, VAR) \
CHECK_LET_ARG(I, IsBoolean(), "bool"); \
bool VAR = IS_ARG_EMPTY(I) ? false : info[I]->BooleanValue();
#define REQ_OFFS_ARG(I, VAR) \ #define REQ_OFFS_ARG(I, VAR) \
CHECK_REQ_ARG(I, IsNumber(), "number"); \ CHECK_REQ_ARG(I, IsNumber(), "number"); \
@ -161,10 +162,14 @@
SETTER_CHECK(IsString(), "string"); \ SETTER_CHECK(IsString(), "string"); \
Nan::Utf8String v(value); Nan::Utf8String v(value);
#define SETTER_UTF8_ARG SETTER_STR_ARG
#define SETTER_INT32_ARG \ #define SETTER_INT32_ARG \
SETTER_CHECK(IsInt32(), "int32"); \ SETTER_CHECK(IsInt32(), "int32"); \
int v = value->Int32Value(); int v = value->Int32Value();
#define SETTER_INT_ARG SETTER_INT32_ARG
#define SETTER_BOOL_ARG \ #define SETTER_BOOL_ARG \
SETTER_CHECK(IsBoolean(), "bool"); \ SETTER_CHECK(IsBoolean(), "bool"); \
bool v = value->BooleanValue(); bool v = value->BooleanValue();
@ -197,6 +202,14 @@
SETTER_CHECK(IsObject(), "object"); \ SETTER_CHECK(IsObject(), "object"); \
v8::Local<v8::Object> v = v8::Local<v8::Object>::Cast(value); v8::Local<v8::Object> v = v8::Local<v8::Object>::Cast(value);
#define SETTER_ARRV_ARG \
SETTER_CHECK(IsObject(), "object"); \
v8::Local<v8::Object> _obj_v = v8::Local<v8::Object>::Cast(value); \
if( ! _obj_v->IsArrayBufferView() ) \
return Nan::ThrowTypeError("The value must be an array buffer"); \
v8::Local<v8::ArrayBufferView> v = v8::Local<v8::ArrayBufferView>::Cast(_obj_v);
template<typename Type> template<typename Type>
inline Type* getArrayData(v8::Local<v8::Value> arg, int *num = NULL) { inline Type* getArrayData(v8::Local<v8::Value> arg, int *num = NULL) {

View File

@ -2,7 +2,7 @@
"name": "addon-tools-raub", "name": "addon-tools-raub",
"author": "Luis Blanco <raubtierxxx@gmail.com>", "author": "Luis Blanco <raubtierxxx@gmail.com>",
"description": "A set of extra tools for Node.js addons", "description": "A set of extra tools for Node.js addons",
"version": "0.1.2", "version": "0.1.3",
"main": "index.js", "main": "index.js",
"keywords": [ "keywords": [
"node", "node",