0.1.1 • Published 4 years ago

node-addon-helper v0.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

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++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) {
    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();
}

CreateAsyncWorker

Create an async api

0.1.0

4 years ago

0.1.1

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.1

4 years ago