Skip to content

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(COMMANDnode-p"require('node-addon-helper').include"WORKING_DIRECTORY${CMAKE_SOURCE_DIR}OUTPUT_VARIABLENODE_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.jsassert.equal(typeConverter.convertBool(true),true);assert.equal(typeConverter.convertBool(false),false);
c++jspredicate
boolBooleantrue/false
int8_tNumberINT8_MIN - INT8_MAX
uint8_tNumberUINT8_MIN - UINT8_MAX
int16_tNumberINT16_MIN - INT16_MAX
uint16_tNumberUINT16_MIN - UINT16_MAX
int32_tNumberINT32_MIN - INT32_MAX
uint32_tNumberUINT32_MIN - UINT32_MAX
int64_tNumberINT64_MIN - INT64_MAX
floatNumber
doubleNumber
std::stringNumber
std::vectorArray/ArrayBuffer/Buffer/TypedArrayT is integral and float_point
std::vectorArrayT not is integral and float_point
std::pair<const char*, size_t>ArrayBuffer/Buffer/TypedArraypointer 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) {
constexprsize_t length = 4;
Nhelper::CheckInfoLength(info, length);
return info.Env().Null();
}

check info's args type

// test/args_check.cpptemplate <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

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages