An open API service indexing awesome lists of open source software.

https://github.com/node-3d/addon-tools-raub

Helpers for Node.js addons and dependency packages
https://github.com/node-3d/addon-tools-raub

addon addons cross-platform fs gyp header header-files helpers hpp logger macros napi node node-3d node-addon node-addon-api node-js nodejs ts utils

Last synced: 5 months ago
JSON representation

Helpers for Node.js addons and dependency packages

Awesome Lists containing this project

README

        

# Addon Tools

This is a part of [Node3D](https://github.com/node-3d) project.

[![NPM](https://badge.fury.io/js/addon-tools-raub.svg)](https://badge.fury.io/js/addon-tools-raub)
[![ESLint](https://github.com/node-3d/addon-tools-raub/actions/workflows/eslint.yml/badge.svg)](https://github.com/node-3d/addon-tools-raub/actions/workflows/eslint.yml)
[![Test](https://github.com/node-3d/addon-tools-raub/actions/workflows/test.yml/badge.svg)](https://github.com/node-3d/addon-tools-raub/actions/workflows/test.yml)
[![Cpplint](https://github.com/node-3d/addon-tools-raub/actions/workflows/cpplint.yml/badge.svg)](https://github.com/node-3d/addon-tools-raub/actions/workflows/cpplint.yml)

```console
npm i -s addon-tools-raub
```

Addon Tools provide build-time and run-time helpers for Node.js C++ addons.
- C++ shortcuts to replace repetitive code in method/class
declaration and commonly used calls (such as `console.log`).
- JS helpers to deliver the precompiled addons to end-users during NPM install.
- Common Logger for both C++ and JS sides with additional control,
compared to native (`printf/cout`) and console logging.

## include/addon-tools.hpp

Macros and helpers for C++ addons using **NAPI**.
See more detailed [docs here](/doc).

Example of a C++ method definition using Addon Tools:

```c++
// hpp:
#include
DBG_EXPORT JS_METHOD(doSomething);
// cpp:
DBG_EXPORT JS_METHOD(doSomething) { NAPI_ENV;
LET_INT32_ARG(0, param0);
Napi::Value args[2] = { JS_STR("param0:"), JS_NUM(param0) };
consoleLog(env, 2, &args[0]);
RET_UNDEFINED;
}
```

Also, ES5 class helpers allow exporting a JS class directly from C++:

```cpp
// hpp:
#include
class MyClass {
DECLARE_ES5_CLASS(MyClass, MyClass);
public:
static void init(Napi::Env env, Napi::Object exports);
explicit MyClass(const Napi::CallbackInfo& info);
~MyClass();
private:
JS_DECLARE_GETTER(MyClass, a);
JS_DECLARE_GETTER(MyClass, b);
JS_DECLARE_METHOD(MyClass, test);
};

// cpp:
IMPLEMENT_ES5_CLASS(MyClass);

void MyClass::init(Napi::Env env, Napi::Object exports) {
Napi::Function ctor = wrap(env);
JS_ASSIGN_GETTER(a);
JS_ASSIGN_GETTER(b);
JS_ASSIGN_METHOD(test);
exports.Set("MyClass", ctor);
}

MyClass::MyClass(const Napi::CallbackInfo &info) {
super(info);
}

MyClass::~MyClass() {}

JS_IMPLEMENT_GETTER(MyClass, a) { NAPI_ENV;
RET_NUM(10);
}

JS_IMPLEMENT_GETTER(MyClass, b) { NAPI_ENV;
RET_NUM(20);
}

JS_IMPLEMENT_METHOD(MyClass, test) { NAPI_ENV;
consoleLog("test");
RET_STR("test");
}

Napi::Object init(Napi::Env env, Napi::Object exports) {
MyClass::init(env, exports);
return exports;
}

NODE_API_MODULE(myaddon, init)
```

## JS Addon Helpers

### Example for an ADDON's **index.js**:

Get the platform-specific directory name to import the `ADDON.node` file.

```js
const { getBin } = require('addon-tools-raub');
const core = require(`./${getBin()}/ADDON`);
```

### Example for **binding.gyp**:

Using the include directories for both Addon Tools header
and Addon API header:

```gyp
'include_dirs': [
' NOTE: the optional `node-addon-api` dependency is used by the `getInclude()`
helper. If not found,
the **napi.h** include path won't be a part of the returned string.

Using helpers for paths to dependency libs and own binaries:

```gyp
'variables': {
'bin': '