Skip to content

Some tool functions simplify the development of node addon.

License

Notifications You must be signed in to change notification settings

xhcoding/node-addon-helper

Repository files navigation

node-addon-helper

Some tool functions simplify the development of node addon.

Setup

CMake.js

Add include directories to your CMakeLists.txt

execute_process(COMMAND node -p "require('node-addon-helper').include"
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE NODE_ADDON_HELPER_DIR
        )

string(REPLACE "\n" "" NODE_ADDON_HELPER_DIR ${NODE_ADDON_HELPER_DIR})
string(REPLACE "\"" "" NODE_ADDON_HELPER_DIR ${NODE_ADDON_HELPER_DIR})

target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_HELPER_DIR})

Usage

Look ./test/ for all usage.

TypeConveter

// test/type_converter.cpp

#include <nhelper/type_conveter.h>

template <typename T>
Napi::Value ExportConvertToT(const Napi::CallbackInfo& info) {
    return Nhelper::TypeConverter<T>::ToJSValue(
            info.Env(), Nhelper::TypeConverter<T>::ToNativeValue(info[0]));
}

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
    exports.Set("convertBool",
                Napi::Function::New(env, ExportConvertToT<bool>));
}
// test/type_converter.test.js
assert.equal(typeConverter.convertBool(true), true);
assert.equal(typeConverter.convertBool(false), false);
c++ js predicate
bool Boolean true/false
int8_t Number INT8_MIN - INT8_MAX
uint8_t Number UINT8_MIN - UINT8_MAX
int16_t Number INT16_MIN - INT16_MAX
uint16_t Number UINT16_MIN - UINT16_MAX
int32_t Number INT32_MIN - INT32_MAX
uint32_t Number UINT32_MIN - UINT32_MAX
int64_t Number INT64_MIN - INT64_MAX
float Number
double Number
std::string Number
std::vector Array/ArrayBuffer/Buffer/TypedArray T is integral and float_point
std::vector Array T not is integral and float_point
std::pair<const char*, size_t> ArrayBuffer/Buffer/TypedArray pointer of buffer's data and length

ArgsCheck

check info length

// test/args_check.cpp
#include "nhelper/args_check.h"
Napi::Value ExportCheckInfoLength(const Napi::CallbackInfo& info) {
    constexpr size_t length = 4;
    Nhelper::CheckInfoLength(info, length);
    return info.Env().Null();
}

check info's args type

// test/args_check.cpp
template <typename T>
Napi::Value ExportCheckInfoType(const Napi::CallbackInfo& info) {
    Nhelper::CheckInfoType<T>(info, 0);
    return info.Env().Null();
}

About

Some tool functions simplify the development of node addon.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published