| #ifndef SRC_NAPI_H_ |
| #define SRC_NAPI_H_ |
|
|
| #ifndef NAPI_HAS_THREADS |
| #if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \ |
| (defined(__wasi__) && defined(_REENTRANT))) |
| #define NAPI_HAS_THREADS 1 |
| #else |
| #define NAPI_HAS_THREADS 0 |
| #endif |
| #endif |
|
|
| #include <node_api.h> |
| #include <functional> |
| #include <initializer_list> |
| #include <memory> |
| #if NAPI_HAS_THREADS |
| #include <mutex> |
| #endif |
| #include <string> |
| #include <vector> |
|
|
| |
| |
| #if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210 |
| #define NAPI_HAS_CONSTEXPR 1 |
| #endif |
|
|
| |
| |
| |
| #if defined(_MSC_VER) && _MSC_VER <= 1800 |
| static_assert(sizeof(char16_t) == sizeof(wchar_t), |
| "Size mismatch between char16_t and wchar_t"); |
| #define NAPI_WIDE_TEXT(x) reinterpret_cast<char16_t*>(L##x) |
| #else |
| #define NAPI_WIDE_TEXT(x) u##x |
| #endif |
|
|
| |
| |
| #if !defined(NAPI_CPP_EXCEPTIONS) && !defined(NAPI_DISABLE_CPP_EXCEPTIONS) |
| #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) |
| #define NAPI_CPP_EXCEPTIONS |
| #else |
| #error Exception support not detected. \ |
| Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS. |
| #endif |
| #endif |
|
|
| |
| |
| #if defined(NAPI_CPP_EXCEPTIONS) && defined(NODE_ADDON_API_ENABLE_MAYBE) |
| #error NODE_ADDON_API_ENABLE_MAYBE should not be set when \ |
| NAPI_CPP_EXCEPTIONS is defined. |
| #endif |
|
|
| #ifdef _NOEXCEPT |
| #define NAPI_NOEXCEPT _NOEXCEPT |
| #else |
| #define NAPI_NOEXCEPT noexcept |
| #endif |
|
|
| #ifdef NAPI_CPP_EXCEPTIONS |
|
|
| |
| |
| |
| |
| |
|
|
| #define NAPI_THROW(e, ...) throw e |
| #define NAPI_THROW_VOID(e) throw e |
|
|
| #define NAPI_THROW_IF_FAILED(env, status, ...) \ |
| if ((status) != napi_ok) throw Napi::Error::New(env); |
|
|
| #define NAPI_THROW_IF_FAILED_VOID(env, status) \ |
| if ((status) != napi_ok) throw Napi::Error::New(env); |
|
|
| #else |
|
|
| |
| |
| |
| |
| |
|
|
| #define NAPI_THROW(e, ...) \ |
| do { \ |
| (e).ThrowAsJavaScriptException(); \ |
| return __VA_ARGS__; \ |
| } while (0) |
|
|
| #define NAPI_THROW_VOID(e) \ |
| do { \ |
| (e).ThrowAsJavaScriptException(); \ |
| return; \ |
| } while (0) |
|
|
| #define NAPI_THROW_IF_FAILED(env, status, ...) \ |
| if ((status) != napi_ok) { \ |
| Napi::Error::New(env).ThrowAsJavaScriptException(); \ |
| return __VA_ARGS__; \ |
| } |
|
|
| #define NAPI_THROW_IF_FAILED_VOID(env, status) \ |
| if ((status) != napi_ok) { \ |
| Napi::Error::New(env).ThrowAsJavaScriptException(); \ |
| return; \ |
| } |
|
|
| #endif |
|
|
| #ifdef NODE_ADDON_API_ENABLE_MAYBE |
| #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \ |
| NAPI_THROW_IF_FAILED(env, status, Napi::Nothing<type>()) |
|
|
| #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \ |
| NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \ |
| return Napi::Just<type>(result); |
| #else |
| #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \ |
| NAPI_THROW_IF_FAILED(env, status, type()) |
|
|
| #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \ |
| NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \ |
| return result; |
| #endif |
|
|
| #define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete; |
| #define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete; |
|
|
| #define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \ |
| NAPI_DISALLOW_ASSIGN(CLASS) \ |
| NAPI_DISALLOW_COPY(CLASS) |
|
|
| #define NAPI_CHECK(condition, location, message) \ |
| do { \ |
| if (!(condition)) { \ |
| Napi::Error::Fatal((location), (message)); \ |
| } \ |
| } while (0) |
|
|
| #define NAPI_FATAL_IF_FAILED(status, location, message) \ |
| NAPI_CHECK((status) == napi_ok, location, message) |
|
|
| |
| |
| |
| |
| |
| |
| |
| namespace Napi { |
|
|
| #ifdef NAPI_CPP_CUSTOM_NAMESPACE |
| |
| |
|
|
| |
| |
| namespace NAPI_CPP_CUSTOM_NAMESPACE {} |
| using namespace NAPI_CPP_CUSTOM_NAMESPACE; |
|
|
| namespace NAPI_CPP_CUSTOM_NAMESPACE { |
| #endif |
|
|
| |
| class Env; |
| class Value; |
| class Boolean; |
| class Number; |
| #if NAPI_VERSION > 5 |
| class BigInt; |
| #endif |
| #if (NAPI_VERSION > 4) |
| class Date; |
| #endif |
| class String; |
| class Object; |
| class Array; |
| class ArrayBuffer; |
| class Function; |
| class Error; |
| class PropertyDescriptor; |
| class CallbackInfo; |
| class TypedArray; |
| template <typename T> |
| class TypedArrayOf; |
|
|
| using Int8Array = |
| TypedArrayOf<int8_t>; |
| using Uint8Array = |
| TypedArrayOf<uint8_t>; |
| using Int16Array = |
| TypedArrayOf<int16_t>; |
| using Uint16Array = |
| TypedArrayOf<uint16_t>; |
| using Int32Array = |
| TypedArrayOf<int32_t>; |
| using Uint32Array = |
| TypedArrayOf<uint32_t>; |
| using Float32Array = |
| TypedArrayOf<float>; |
| using Float64Array = |
| TypedArrayOf<double>; |
| #if NAPI_VERSION > 5 |
| using BigInt64Array = |
| TypedArrayOf<int64_t>; |
| using BigUint64Array = |
| TypedArrayOf<uint64_t>; |
| #endif |
|
|
| |
| |
| using ModuleRegisterCallback = Object (*)(Env env, Object exports); |
|
|
| class MemoryManagement; |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <class T> |
| class Maybe { |
| public: |
| bool IsNothing() const; |
| bool IsJust() const; |
|
|
| |
| |
| |
| |
| void Check() const; |
|
|
| |
| |
| T Unwrap() const; |
|
|
| |
| |
| T UnwrapOr(const T& default_value) const; |
|
|
| |
| |
| bool UnwrapTo(T* out) const; |
|
|
| bool operator==(const Maybe& other) const; |
| bool operator!=(const Maybe& other) const; |
|
|
| private: |
| Maybe(); |
| explicit Maybe(const T& t); |
|
|
| bool _has_value; |
| T _value; |
|
|
| template <class U> |
| friend Maybe<U> Nothing(); |
| template <class U> |
| friend Maybe<U> Just(const U& u); |
| }; |
|
|
| template <class T> |
| inline Maybe<T> Nothing(); |
|
|
| template <class T> |
| inline Maybe<T> Just(const T& t); |
|
|
| #if defined(NODE_ADDON_API_ENABLE_MAYBE) |
| template <typename T> |
| using MaybeOrValue = Maybe<T>; |
| #else |
| template <typename T> |
| using MaybeOrValue = T; |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Env { |
| private: |
| napi_env _env; |
| #if NAPI_VERSION > 5 |
| template <typename T> |
| static void DefaultFini(Env, T* data); |
| template <typename DataType, typename HintType> |
| static void DefaultFiniWithHint(Env, DataType* data, HintType* hint); |
| #endif |
| public: |
| Env(napi_env env); |
|
|
| operator napi_env() const; |
|
|
| Object Global() const; |
| Value Undefined() const; |
| Value Null() const; |
|
|
| bool IsExceptionPending() const; |
| Error GetAndClearPendingException() const; |
|
|
| MaybeOrValue<Value> RunScript(const char* utf8script) const; |
| MaybeOrValue<Value> RunScript(const std::string& utf8script) const; |
| MaybeOrValue<Value> RunScript(String script) const; |
|
|
| #if NAPI_VERSION > 2 |
| template <typename Hook, typename Arg = void> |
| class CleanupHook; |
|
|
| template <typename Hook> |
| CleanupHook<Hook> AddCleanupHook(Hook hook); |
|
|
| template <typename Hook, typename Arg> |
| CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg); |
| #endif |
|
|
| #if NAPI_VERSION > 5 |
| template <typename T> |
| T* GetInstanceData() const; |
|
|
| template <typename T> |
| using Finalizer = void (*)(Env, T*); |
| template <typename T, Finalizer<T> fini = Env::DefaultFini<T>> |
| void SetInstanceData(T* data) const; |
|
|
| template <typename DataType, typename HintType> |
| using FinalizerWithHint = void (*)(Env, DataType*, HintType*); |
| template <typename DataType, |
| typename HintType, |
| FinalizerWithHint<DataType, HintType> fini = |
| Env::DefaultFiniWithHint<DataType, HintType>> |
| void SetInstanceData(DataType* data, HintType* hint) const; |
| #endif |
|
|
| #if NAPI_VERSION > 2 |
| template <typename Hook, typename Arg> |
| class CleanupHook { |
| public: |
| CleanupHook(); |
| CleanupHook(Env env, Hook hook, Arg* arg); |
| CleanupHook(Env env, Hook hook); |
| bool Remove(Env env); |
| bool IsEmpty() const; |
|
|
| private: |
| static inline void Wrapper(void* data) NAPI_NOEXCEPT; |
| static inline void WrapperWithArg(void* data) NAPI_NOEXCEPT; |
|
|
| void (*wrapper)(void* arg); |
| struct CleanupData { |
| Hook hook; |
| Arg* arg; |
| } * data; |
| }; |
| #endif |
|
|
| #if NAPI_VERSION > 8 |
| const char* GetModuleFileName() const; |
| #endif |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Value { |
| public: |
| Value(); |
| Value(napi_env env, |
| napi_value value); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| static Value From(napi_env env, const T& value); |
|
|
| |
| |
| |
| operator napi_value() const; |
|
|
| |
| bool operator==(const Value& other) const; |
|
|
| |
| bool operator!=(const Value& other) const; |
|
|
| |
| bool StrictEquals(const Value& other) const; |
|
|
| |
| Napi::Env Env() const; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool IsEmpty() const; |
|
|
| napi_valuetype Type() const; |
|
|
| bool IsUndefined() |
| const; |
| bool IsNull() const; |
| bool IsBoolean() const; |
| bool IsNumber() const; |
| #if NAPI_VERSION > 5 |
| bool IsBigInt() const; |
| #endif |
| #if (NAPI_VERSION > 4) |
| bool IsDate() const; |
| #endif |
| bool IsString() const; |
| bool IsSymbol() const; |
| bool IsArray() const; |
| bool IsArrayBuffer() |
| const; |
| bool IsTypedArray() const; |
| bool IsObject() const; |
| bool IsFunction() const; |
| bool IsPromise() const; |
| bool IsDataView() const; |
| bool IsBuffer() const; |
| bool IsExternal() const; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| T As() const; |
|
|
| MaybeOrValue<Boolean> ToBoolean() |
| const; |
| MaybeOrValue<Number> ToNumber() |
| const; |
| MaybeOrValue<String> ToString() |
| const; |
| MaybeOrValue<Object> ToObject() |
| const; |
|
|
| protected: |
| |
| napi_env _env; |
| napi_value _value; |
| |
| }; |
|
|
| |
| class Boolean : public Value { |
| public: |
| static Boolean New(napi_env env, |
| bool value |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Boolean(); |
| Boolean(napi_env env, |
| napi_value value); |
|
|
| operator bool() const; |
| bool Value() const; |
| }; |
|
|
| |
| class Number : public Value { |
| public: |
| static Number New(napi_env env, |
| double value |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Number(); |
| Number(napi_env env, |
| napi_value value); |
|
|
| operator int32_t() |
| const; |
| operator uint32_t() |
| const; |
| operator int64_t() |
| const; |
| operator float() |
| const; |
| operator double() |
| const; |
|
|
| int32_t Int32Value() |
| const; |
| uint32_t Uint32Value() |
| const; |
| int64_t Int64Value() |
| const; |
| float FloatValue() |
| const; |
| double DoubleValue() |
| const; |
| }; |
|
|
| #if NAPI_VERSION > 5 |
| |
| class BigInt : public Value { |
| public: |
| static BigInt New(napi_env env, |
| int64_t value |
| ); |
| static BigInt New(napi_env env, |
| uint64_t value |
| ); |
|
|
| |
| |
| |
| |
| static BigInt New(napi_env env, |
| int sign_bit, |
| size_t word_count, |
| const uint64_t* words |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| BigInt(); |
| BigInt(napi_env env, |
| napi_value value); |
|
|
| int64_t Int64Value(bool* lossless) |
| const; |
| uint64_t Uint64Value(bool* lossless) |
| const; |
|
|
| size_t WordCount() const; |
| |
|
|
| |
| |
| |
| |
| |
| |
| void ToWords(int* sign_bit, size_t* word_count, uint64_t* words); |
| }; |
| #endif |
|
|
| #if (NAPI_VERSION > 4) |
| |
| class Date : public Value { |
| public: |
| |
| static Date New(napi_env env, |
| double value |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Date(); |
| Date(napi_env env, napi_value value); |
| operator double() const; |
|
|
| double ValueOf() const; |
| }; |
| #endif |
|
|
| |
| class Name : public Value { |
| public: |
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Name(); |
| Name(napi_env env, |
| napi_value value); |
| }; |
|
|
| |
| class String : public Name { |
| public: |
| |
| static String New(napi_env env, |
| const std::string& value |
| ); |
|
|
| |
| static String New(napi_env env, |
| const std::u16string& value |
| ); |
|
|
| |
| static String New( |
| napi_env env, |
| const char* value |
| ); |
|
|
| |
| static String New( |
| napi_env env, |
| const char16_t* value |
| ); |
|
|
| |
| |
| static String New(napi_env env, |
| const char* value, |
| |
| size_t length |
| ); |
|
|
| |
| |
| static String New( |
| napi_env env, |
| const char16_t* value, |
| |
| size_t length |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| static String From(napi_env env, const T& value); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| String(); |
| String(napi_env env, |
| napi_value value); |
|
|
| operator std::string() |
| const; |
| operator std::u16string() |
| const; |
| std::string Utf8Value() |
| const; |
| std::u16string Utf16Value() |
| const; |
| }; |
|
|
| |
| class Symbol : public Name { |
| public: |
| |
| static Symbol New( |
| napi_env env, |
| const char* description = |
| nullptr |
| |
| ); |
|
|
| |
| static Symbol New( |
| napi_env env, |
| const std::string& |
| description |
| ); |
|
|
| |
| static Symbol New(napi_env env, |
| String description |
| ); |
|
|
| |
| static Symbol New( |
| napi_env env, |
| napi_value description |
| ); |
|
|
| |
| static MaybeOrValue<Symbol> WellKnown(napi_env, const std::string& name); |
|
|
| |
| static MaybeOrValue<Symbol> For(napi_env env, const std::string& description); |
|
|
| |
| static MaybeOrValue<Symbol> For(napi_env env, const char* description); |
|
|
| |
| static MaybeOrValue<Symbol> For(napi_env env, String description); |
|
|
| |
| static MaybeOrValue<Symbol> For(napi_env env, napi_value description); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Symbol(); |
| Symbol(napi_env env, |
| napi_value value); |
| }; |
|
|
| class TypeTaggable : public Value { |
| public: |
| #if NAPI_VERSION >= 8 |
| void TypeTag(const napi_type_tag* type_tag) const; |
| bool CheckTypeTag(const napi_type_tag* type_tag) const; |
| #endif |
| protected: |
| TypeTaggable(); |
| TypeTaggable(napi_env env, napi_value value); |
| }; |
|
|
| |
| class Object : public TypeTaggable { |
| public: |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename Key> |
| class PropertyLValue { |
| public: |
| |
| operator Value() const; |
|
|
| |
| |
| template <typename ValueType> |
| PropertyLValue& operator=(ValueType value); |
|
|
| private: |
| PropertyLValue() = delete; |
| PropertyLValue(Object object, Key key); |
| napi_env _env; |
| napi_value _object; |
| Key _key; |
|
|
| friend class Napi::Object; |
| }; |
|
|
| |
| static Object New(napi_env env |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Object(); |
| Object(napi_env env, |
| napi_value value); |
|
|
| |
| PropertyLValue<std::string> operator[]( |
| const char* utf8name |
| ); |
|
|
| |
| PropertyLValue<std::string> operator[]( |
| const std::string& utf8name |
| ); |
|
|
| |
| PropertyLValue<uint32_t> operator[]( |
| uint32_t index |
| ); |
|
|
| |
| PropertyLValue<Value> operator[](Value index |
| ) const; |
|
|
| |
| MaybeOrValue<Value> operator[]( |
| const char* utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<Value> operator[]( |
| const std::string& utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<Value> operator[](uint32_t index |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Has(napi_value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Has(Value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Has( |
| const char* utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Has( |
| const std::string& utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<bool> HasOwnProperty(napi_value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> HasOwnProperty(Value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> HasOwnProperty( |
| const char* utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<bool> HasOwnProperty( |
| const std::string& utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<Value> Get(napi_value key |
| ) const; |
|
|
| |
| MaybeOrValue<Value> Get(Value key |
| ) const; |
|
|
| |
| MaybeOrValue<Value> Get( |
| const char* utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<Value> Get( |
| const std::string& utf8name |
| ) const; |
|
|
| |
| template <typename ValueType> |
| MaybeOrValue<bool> Set(napi_value key, |
| const ValueType& value |
| ) const; |
|
|
| |
| template <typename ValueType> |
| MaybeOrValue<bool> Set(Value key, |
| const ValueType& value |
| ) const; |
|
|
| |
| template <typename ValueType> |
| MaybeOrValue<bool> Set( |
| const char* utf8name, |
| const ValueType& value) const; |
|
|
| |
| template <typename ValueType> |
| MaybeOrValue<bool> Set( |
| const std::string& utf8name, |
| const ValueType& value |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Delete(napi_value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Delete(Value key |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Delete( |
| const char* utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Delete( |
| const std::string& utf8name |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Has(uint32_t index |
| ) const; |
|
|
| |
| MaybeOrValue<Value> Get(uint32_t index |
| ) const; |
|
|
| |
| template <typename ValueType> |
| MaybeOrValue<bool> Set(uint32_t index, |
| const ValueType& value |
| ) const; |
|
|
| |
| MaybeOrValue<bool> Delete(uint32_t index |
| ) const; |
|
|
| |
| |
| |
| |
| |
| |
| MaybeOrValue<Array> GetPropertyNames() const; |
|
|
| |
| |
| |
| |
| |
| MaybeOrValue<bool> DefineProperty( |
| const PropertyDescriptor& |
| property |
| ) const; |
|
|
| |
| |
| |
| |
| |
| MaybeOrValue<bool> DefineProperties( |
| const std::initializer_list<PropertyDescriptor>& properties |
| |
| ) const; |
|
|
| |
| |
| |
| |
| |
| MaybeOrValue<bool> DefineProperties( |
| const std::vector<PropertyDescriptor>& properties |
| |
| ) const; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| MaybeOrValue<bool> InstanceOf( |
| const Function& constructor |
| ) const; |
|
|
| template <typename Finalizer, typename T> |
| inline void AddFinalizer(Finalizer finalizeCallback, T* data) const; |
|
|
| template <typename Finalizer, typename T, typename Hint> |
| inline void AddFinalizer(Finalizer finalizeCallback, |
| T* data, |
| Hint* finalizeHint) const; |
|
|
| #ifdef NAPI_CPP_EXCEPTIONS |
| class const_iterator; |
|
|
| inline const_iterator begin() const; |
|
|
| inline const_iterator end() const; |
|
|
| class iterator; |
|
|
| inline iterator begin(); |
|
|
| inline iterator end(); |
| #endif |
|
|
| #if NAPI_VERSION >= 8 |
| |
| |
| |
| |
| MaybeOrValue<bool> Freeze() const; |
| |
| |
| |
| |
| MaybeOrValue<bool> Seal() const; |
| #endif |
| }; |
|
|
| template <typename T> |
| class External : public TypeTaggable { |
| public: |
| static External New(napi_env env, T* data); |
|
|
| |
| template <typename Finalizer> |
| static External New(napi_env env, T* data, Finalizer finalizeCallback); |
| |
| template <typename Finalizer, typename Hint> |
| static External New(napi_env env, |
| T* data, |
| Finalizer finalizeCallback, |
| Hint* finalizeHint); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| External(); |
| External(napi_env env, napi_value value); |
|
|
| T* Data() const; |
| }; |
|
|
| class Array : public Object { |
| public: |
| static Array New(napi_env env); |
| static Array New(napi_env env, size_t length); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Array(); |
| Array(napi_env env, napi_value value); |
|
|
| uint32_t Length() const; |
| }; |
|
|
| #ifdef NAPI_CPP_EXCEPTIONS |
| class Object::const_iterator { |
| private: |
| enum class Type { BEGIN, END }; |
|
|
| inline const_iterator(const Object* object, const Type type); |
|
|
| public: |
| inline const_iterator& operator++(); |
|
|
| inline bool operator==(const const_iterator& other) const; |
|
|
| inline bool operator!=(const const_iterator& other) const; |
|
|
| inline const std::pair<Value, Object::PropertyLValue<Value>> operator*() |
| const; |
|
|
| private: |
| const Napi::Object* _object; |
| Array _keys; |
| uint32_t _index; |
|
|
| friend class Object; |
| }; |
|
|
| class Object::iterator { |
| private: |
| enum class Type { BEGIN, END }; |
|
|
| inline iterator(Object* object, const Type type); |
|
|
| public: |
| inline iterator& operator++(); |
|
|
| inline bool operator==(const iterator& other) const; |
|
|
| inline bool operator!=(const iterator& other) const; |
|
|
| inline std::pair<Value, Object::PropertyLValue<Value>> operator*(); |
|
|
| private: |
| Napi::Object* _object; |
| Array _keys; |
| uint32_t _index; |
|
|
| friend class Object; |
| }; |
| #endif |
|
|
| |
| class ArrayBuffer : public Object { |
| public: |
| |
| |
| static ArrayBuffer New( |
| napi_env env, |
| size_t byteLength |
| ); |
|
|
| #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED |
| |
| |
| static ArrayBuffer New( |
| napi_env env, |
| void* externalData, |
| |
| size_t byteLength |
| |
| ); |
|
|
| |
| |
| template <typename Finalizer> |
| static ArrayBuffer New( |
| napi_env env, |
| void* externalData, |
| |
| size_t byteLength, |
| |
| |
| Finalizer finalizeCallback |
| |
| |
| |
| ); |
|
|
| |
| |
| template <typename Finalizer, typename Hint> |
| static ArrayBuffer New( |
| napi_env env, |
| void* externalData, |
| |
| size_t byteLength, |
| |
| |
| Finalizer finalizeCallback, |
| |
| |
| |
| Hint* finalizeHint |
| |
| ); |
| #endif |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| ArrayBuffer(); |
| ArrayBuffer(napi_env env, |
| napi_value value); |
|
|
| void* Data(); |
| size_t ByteLength(); |
|
|
| #if NAPI_VERSION >= 7 |
| bool IsDetached() const; |
| void Detach(); |
| #endif |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class TypedArray : public Object { |
| public: |
| static void CheckCast(napi_env env, napi_value value); |
|
|
| TypedArray(); |
| TypedArray(napi_env env, |
| napi_value value); |
|
|
| napi_typedarray_type TypedArrayType() |
| const; |
| Napi::ArrayBuffer ArrayBuffer() const; |
|
|
| uint8_t ElementSize() |
| const; |
| size_t ElementLength() const; |
| size_t ByteOffset() |
| const; |
| size_t ByteLength() const; |
|
|
| protected: |
| |
| napi_typedarray_type _type; |
| size_t _length; |
|
|
| TypedArray(napi_env env, |
| napi_value value, |
| napi_typedarray_type type, |
| size_t length); |
|
|
| template <typename T> |
| static |
| #if defined(NAPI_HAS_CONSTEXPR) |
| constexpr |
| #endif |
| napi_typedarray_type |
| TypedArrayTypeForPrimitiveType() { |
| return std::is_same<T, int8_t>::value ? napi_int8_array |
| : std::is_same<T, uint8_t>::value ? napi_uint8_array |
| : std::is_same<T, int16_t>::value ? napi_int16_array |
| : std::is_same<T, uint16_t>::value ? napi_uint16_array |
| : std::is_same<T, int32_t>::value ? napi_int32_array |
| : std::is_same<T, uint32_t>::value ? napi_uint32_array |
| : std::is_same<T, float>::value ? napi_float32_array |
| : std::is_same<T, double>::value ? napi_float64_array |
| #if NAPI_VERSION > 5 |
| : std::is_same<T, int64_t>::value ? napi_bigint64_array |
| : std::is_same<T, uint64_t>::value ? napi_biguint64_array |
| #endif |
| : napi_int8_array; |
| } |
| |
| }; |
|
|
| |
| |
| |
| |
| template <typename T> |
| class TypedArrayOf : public TypedArray { |
| public: |
| |
| |
| |
| |
| |
| |
| |
| static TypedArrayOf New( |
| napi_env env, |
| size_t elementLength, |
| |
| #if defined(NAPI_HAS_CONSTEXPR) |
| napi_typedarray_type type = |
| TypedArray::TypedArrayTypeForPrimitiveType<T>() |
| #else |
| napi_typedarray_type type |
| #endif |
| |
| |
| ); |
|
|
| |
| |
| |
| |
| |
| |
| static TypedArrayOf New( |
| napi_env env, |
| size_t elementLength, |
| |
| Napi::ArrayBuffer arrayBuffer, |
| size_t bufferOffset, |
| |
| #if defined(NAPI_HAS_CONSTEXPR) |
| napi_typedarray_type type = |
| TypedArray::TypedArrayTypeForPrimitiveType<T>() |
| #else |
| napi_typedarray_type type |
| #endif |
| |
| |
| ); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| TypedArrayOf(); |
| TypedArrayOf(napi_env env, |
| napi_value value); |
|
|
| T& operator[](size_t index); |
| const T& operator[](size_t index) const; |
|
|
| |
| |
| |
| |
| |
| T* Data(); |
|
|
| |
| |
| |
| |
| |
| const T* Data() const; |
|
|
| private: |
| T* _data; |
|
|
| TypedArrayOf(napi_env env, |
| napi_value value, |
| napi_typedarray_type type, |
| size_t length, |
| T* data); |
| }; |
|
|
| |
| |
| class DataView : public Object { |
| public: |
| static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer); |
| static DataView New(napi_env env, |
| Napi::ArrayBuffer arrayBuffer, |
| size_t byteOffset); |
| static DataView New(napi_env env, |
| Napi::ArrayBuffer arrayBuffer, |
| size_t byteOffset, |
| size_t byteLength); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| DataView(); |
| DataView(napi_env env, |
| napi_value value); |
|
|
| Napi::ArrayBuffer ArrayBuffer() const; |
| size_t ByteOffset() |
| const; |
| size_t ByteLength() const; |
|
|
| void* Data() const; |
|
|
| float GetFloat32(size_t byteOffset) const; |
| double GetFloat64(size_t byteOffset) const; |
| int8_t GetInt8(size_t byteOffset) const; |
| int16_t GetInt16(size_t byteOffset) const; |
| int32_t GetInt32(size_t byteOffset) const; |
| uint8_t GetUint8(size_t byteOffset) const; |
| uint16_t GetUint16(size_t byteOffset) const; |
| uint32_t GetUint32(size_t byteOffset) const; |
|
|
| void SetFloat32(size_t byteOffset, float value) const; |
| void SetFloat64(size_t byteOffset, double value) const; |
| void SetInt8(size_t byteOffset, int8_t value) const; |
| void SetInt16(size_t byteOffset, int16_t value) const; |
| void SetInt32(size_t byteOffset, int32_t value) const; |
| void SetUint8(size_t byteOffset, uint8_t value) const; |
| void SetUint16(size_t byteOffset, uint16_t value) const; |
| void SetUint32(size_t byteOffset, uint32_t value) const; |
|
|
| private: |
| template <typename T> |
| T ReadData(size_t byteOffset) const; |
|
|
| template <typename T> |
| void WriteData(size_t byteOffset, T value) const; |
|
|
| void* _data; |
| size_t _length; |
| }; |
|
|
| class Function : public Object { |
| public: |
| using VoidCallback = void (*)(const CallbackInfo& info); |
| using Callback = Value (*)(const CallbackInfo& info); |
|
|
| template <VoidCallback cb> |
| static Function New(napi_env env, |
| const char* utf8name = nullptr, |
| void* data = nullptr); |
|
|
| template <Callback cb> |
| static Function New(napi_env env, |
| const char* utf8name = nullptr, |
| void* data = nullptr); |
|
|
| template <VoidCallback cb> |
| static Function New(napi_env env, |
| const std::string& utf8name, |
| void* data = nullptr); |
|
|
| template <Callback cb> |
| static Function New(napi_env env, |
| const std::string& utf8name, |
| void* data = nullptr); |
|
|
| |
| |
| template <typename Callable> |
| static Function New(napi_env env, |
| Callable cb, |
| const char* utf8name = nullptr, |
| void* data = nullptr); |
| |
| |
| template <typename Callable> |
| static Function New(napi_env env, |
| Callable cb, |
| const std::string& utf8name, |
| void* data = nullptr); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Function(); |
| Function(napi_env env, napi_value value); |
|
|
| MaybeOrValue<Value> operator()( |
| const std::initializer_list<napi_value>& args) const; |
|
|
| MaybeOrValue<Value> Call(const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Value> Call(const std::vector<napi_value>& args) const; |
| MaybeOrValue<Value> Call(const std::vector<Value>& args) const; |
| MaybeOrValue<Value> Call(size_t argc, const napi_value* args) const; |
| MaybeOrValue<Value> Call(napi_value recv, |
| const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Value> Call(napi_value recv, |
| const std::vector<napi_value>& args) const; |
| MaybeOrValue<Value> Call(napi_value recv, |
| const std::vector<Value>& args) const; |
| MaybeOrValue<Value> Call(napi_value recv, |
| size_t argc, |
| const napi_value* args) const; |
|
|
| MaybeOrValue<Value> MakeCallback( |
| napi_value recv, |
| const std::initializer_list<napi_value>& args, |
| napi_async_context context = nullptr) const; |
| MaybeOrValue<Value> MakeCallback(napi_value recv, |
| const std::vector<napi_value>& args, |
| napi_async_context context = nullptr) const; |
| MaybeOrValue<Value> MakeCallback(napi_value recv, |
| size_t argc, |
| const napi_value* args, |
| napi_async_context context = nullptr) const; |
|
|
| MaybeOrValue<Object> New(const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Object> New(const std::vector<napi_value>& args) const; |
| MaybeOrValue<Object> New(size_t argc, const napi_value* args) const; |
| }; |
|
|
| class Promise : public Object { |
| public: |
| class Deferred { |
| public: |
| static Deferred New(napi_env env); |
| Deferred(napi_env env); |
|
|
| Napi::Promise Promise() const; |
| Napi::Env Env() const; |
|
|
| void Resolve(napi_value value) const; |
| void Reject(napi_value value) const; |
|
|
| private: |
| napi_env _env; |
| napi_deferred _deferred; |
| napi_value _promise; |
| }; |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Promise(napi_env env, napi_value value); |
| }; |
|
|
| template <typename T> |
| class Buffer : public Uint8Array { |
| public: |
| static Buffer<T> New(napi_env env, size_t length); |
| #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED |
| static Buffer<T> New(napi_env env, T* data, size_t length); |
|
|
| |
| template <typename Finalizer> |
| static Buffer<T> New(napi_env env, |
| T* data, |
| size_t length, |
| Finalizer finalizeCallback); |
| |
| template <typename Finalizer, typename Hint> |
| static Buffer<T> New(napi_env env, |
| T* data, |
| size_t length, |
| Finalizer finalizeCallback, |
| Hint* finalizeHint); |
| #endif |
|
|
| static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length); |
| |
| template <typename Finalizer> |
| static Buffer<T> NewOrCopy(napi_env env, |
| T* data, |
| size_t length, |
| Finalizer finalizeCallback); |
| |
| template <typename Finalizer, typename Hint> |
| static Buffer<T> NewOrCopy(napi_env env, |
| T* data, |
| size_t length, |
| Finalizer finalizeCallback, |
| Hint* finalizeHint); |
|
|
| static Buffer<T> Copy(napi_env env, const T* data, size_t length); |
|
|
| static void CheckCast(napi_env env, napi_value value); |
|
|
| Buffer(); |
| Buffer(napi_env env, napi_value value); |
| size_t Length() const; |
| T* Data() const; |
|
|
| private: |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class Reference { |
| public: |
| static Reference<T> New(const T& value, uint32_t initialRefcount = 0); |
|
|
| Reference(); |
| Reference(napi_env env, napi_ref ref); |
| ~Reference(); |
|
|
| |
| Reference(Reference<T>&& other); |
| Reference<T>& operator=(Reference<T>&& other); |
| NAPI_DISALLOW_ASSIGN(Reference<T>) |
|
|
| operator napi_ref() const; |
| bool operator==(const Reference<T>& other) const; |
| bool operator!=(const Reference<T>& other) const; |
|
|
| Napi::Env Env() const; |
| bool IsEmpty() const; |
|
|
| |
| |
| T Value() const; |
|
|
| uint32_t Ref() const; |
| uint32_t Unref() const; |
| void Reset(); |
| void Reset(const T& value, uint32_t refcount = 0); |
|
|
| |
| |
| |
| |
| |
| void SuppressDestruct(); |
|
|
| protected: |
| Reference(const Reference<T>&); |
|
|
| |
| napi_env _env; |
| napi_ref _ref; |
| |
|
|
| private: |
| bool _suppressDestruct; |
| }; |
|
|
| class ObjectReference : public Reference<Object> { |
| public: |
| ObjectReference(); |
| ObjectReference(napi_env env, napi_ref ref); |
|
|
| |
| ObjectReference(Reference<Object>&& other); |
| ObjectReference& operator=(Reference<Object>&& other); |
| ObjectReference(ObjectReference&& other); |
| ObjectReference& operator=(ObjectReference&& other); |
| NAPI_DISALLOW_ASSIGN(ObjectReference) |
|
|
| MaybeOrValue<Napi::Value> Get(const char* utf8name) const; |
| MaybeOrValue<Napi::Value> Get(const std::string& utf8name) const; |
| MaybeOrValue<bool> Set(const char* utf8name, napi_value value) const; |
| MaybeOrValue<bool> Set(const char* utf8name, Napi::Value value) const; |
| MaybeOrValue<bool> Set(const char* utf8name, const char* utf8value) const; |
| MaybeOrValue<bool> Set(const char* utf8name, bool boolValue) const; |
| MaybeOrValue<bool> Set(const char* utf8name, double numberValue) const; |
| MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value) const; |
| MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value) const; |
| MaybeOrValue<bool> Set(const std::string& utf8name, |
| std::string& utf8value) const; |
| MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue) const; |
| MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue) const; |
|
|
| MaybeOrValue<Napi::Value> Get(uint32_t index) const; |
| MaybeOrValue<bool> Set(uint32_t index, const napi_value value) const; |
| MaybeOrValue<bool> Set(uint32_t index, const Napi::Value value) const; |
| MaybeOrValue<bool> Set(uint32_t index, const char* utf8value) const; |
| MaybeOrValue<bool> Set(uint32_t index, const std::string& utf8value) const; |
| MaybeOrValue<bool> Set(uint32_t index, bool boolValue) const; |
| MaybeOrValue<bool> Set(uint32_t index, double numberValue) const; |
|
|
| protected: |
| ObjectReference(const ObjectReference&); |
| }; |
|
|
| class FunctionReference : public Reference<Function> { |
| public: |
| FunctionReference(); |
| FunctionReference(napi_env env, napi_ref ref); |
|
|
| |
| FunctionReference(Reference<Function>&& other); |
| FunctionReference& operator=(Reference<Function>&& other); |
| FunctionReference(FunctionReference&& other); |
| FunctionReference& operator=(FunctionReference&& other); |
| NAPI_DISALLOW_ASSIGN_COPY(FunctionReference) |
|
|
| MaybeOrValue<Napi::Value> operator()( |
| const std::initializer_list<napi_value>& args) const; |
|
|
| MaybeOrValue<Napi::Value> Call( |
| const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Napi::Value> Call(const std::vector<napi_value>& args) const; |
| MaybeOrValue<Napi::Value> Call( |
| napi_value recv, const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Napi::Value> Call(napi_value recv, |
| const std::vector<napi_value>& args) const; |
| MaybeOrValue<Napi::Value> Call(napi_value recv, |
| size_t argc, |
| const napi_value* args) const; |
|
|
| MaybeOrValue<Napi::Value> MakeCallback( |
| napi_value recv, |
| const std::initializer_list<napi_value>& args, |
| napi_async_context context = nullptr) const; |
| MaybeOrValue<Napi::Value> MakeCallback( |
| napi_value recv, |
| const std::vector<napi_value>& args, |
| napi_async_context context = nullptr) const; |
| MaybeOrValue<Napi::Value> MakeCallback( |
| napi_value recv, |
| size_t argc, |
| const napi_value* args, |
| napi_async_context context = nullptr) const; |
|
|
| MaybeOrValue<Object> New(const std::initializer_list<napi_value>& args) const; |
| MaybeOrValue<Object> New(const std::vector<napi_value>& args) const; |
| }; |
|
|
| |
| template <typename T> |
| Reference<T> Weak(T value); |
| ObjectReference Weak(Object value); |
| FunctionReference Weak(Function value); |
|
|
| |
| template <typename T> |
| Reference<T> Persistent(T value); |
| ObjectReference Persistent(Object value); |
| FunctionReference Persistent(Function value); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Error : public ObjectReference |
| #ifdef NAPI_CPP_EXCEPTIONS |
| , |
| public std::exception |
| #endif |
| { |
| public: |
| static Error New(napi_env env); |
| static Error New(napi_env env, const char* message); |
| static Error New(napi_env env, const std::string& message); |
|
|
| static NAPI_NO_RETURN void Fatal(const char* location, const char* message); |
|
|
| Error(); |
| Error(napi_env env, napi_value value); |
|
|
| |
| Error(Error&& other); |
| Error& operator=(Error&& other); |
| Error(const Error&); |
| Error& operator=(const Error&); |
|
|
| const std::string& Message() const NAPI_NOEXCEPT; |
| void ThrowAsJavaScriptException() const; |
|
|
| Object Value() const; |
|
|
| #ifdef NAPI_CPP_EXCEPTIONS |
| const char* what() const NAPI_NOEXCEPT override; |
| #endif |
|
|
| protected: |
| |
| using create_error_fn = napi_status (*)(napi_env envb, |
| napi_value code, |
| napi_value msg, |
| napi_value* result); |
|
|
| template <typename TError> |
| static TError New(napi_env env, |
| const char* message, |
| size_t length, |
| create_error_fn create_error); |
| |
|
|
| private: |
| static inline const char* ERROR_WRAP_VALUE() NAPI_NOEXCEPT; |
| mutable std::string _message; |
| }; |
|
|
| class TypeError : public Error { |
| public: |
| static TypeError New(napi_env env, const char* message); |
| static TypeError New(napi_env env, const std::string& message); |
|
|
| TypeError(); |
| TypeError(napi_env env, napi_value value); |
| }; |
|
|
| class RangeError : public Error { |
| public: |
| static RangeError New(napi_env env, const char* message); |
| static RangeError New(napi_env env, const std::string& message); |
|
|
| RangeError(); |
| RangeError(napi_env env, napi_value value); |
| }; |
|
|
| #if NAPI_VERSION > 8 |
| class SyntaxError : public Error { |
| public: |
| static SyntaxError New(napi_env env, const char* message); |
| static SyntaxError New(napi_env env, const std::string& message); |
|
|
| SyntaxError(); |
| SyntaxError(napi_env env, napi_value value); |
| }; |
| #endif |
|
|
| class CallbackInfo { |
| public: |
| CallbackInfo(napi_env env, napi_callback_info info); |
| ~CallbackInfo(); |
|
|
| |
| NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo) |
|
|
| Napi::Env Env() const; |
| Value NewTarget() const; |
| bool IsConstructCall() const; |
| size_t Length() const; |
| const Value operator[](size_t index) const; |
| Value This() const; |
| void* Data() const; |
| void SetData(void* data); |
| explicit operator napi_callback_info() const; |
|
|
| private: |
| const size_t _staticArgCount = 6; |
| napi_env _env; |
| napi_callback_info _info; |
| napi_value _this; |
| size_t _argc; |
| napi_value* _argv; |
| napi_value _staticArgs[6]; |
| napi_value* _dynamicArgs; |
| void* _data; |
| }; |
|
|
| class PropertyDescriptor { |
| public: |
| using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info); |
| using SetterCallback = void (*)(const Napi::CallbackInfo& info); |
|
|
| #ifndef NODE_ADDON_API_DISABLE_DEPRECATED |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| const char* utf8name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| const std::string& utf8name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| napi_value name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| Name name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| const char* utf8name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| const std::string& utf8name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| napi_value name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| Name name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| const char* utf8name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| const std::string& utf8name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| napi_value name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| Name name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| #endif |
|
|
| template <GetterCallback Getter> |
| static PropertyDescriptor Accessor( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <GetterCallback Getter> |
| static PropertyDescriptor Accessor( |
| const std::string& utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <GetterCallback Getter> |
| static PropertyDescriptor Accessor( |
| Name name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <GetterCallback Getter, SetterCallback Setter> |
| static PropertyDescriptor Accessor( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <GetterCallback Getter, SetterCallback Setter> |
| static PropertyDescriptor Accessor( |
| const std::string& utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <GetterCallback Getter, SetterCallback Setter> |
| static PropertyDescriptor Accessor( |
| Name name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
|
|
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| const char* utf8name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| const std::string& utf8name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| Name name, |
| Getter getter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| const char* utf8name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| const std::string& utf8name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Getter, typename Setter> |
| static PropertyDescriptor Accessor( |
| Napi::Env env, |
| Napi::Object object, |
| Name name, |
| Getter getter, |
| Setter setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| Napi::Env env, |
| Napi::Object object, |
| const char* utf8name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| Napi::Env env, |
| Napi::Object object, |
| const std::string& utf8name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <typename Callable> |
| static PropertyDescriptor Function( |
| Napi::Env env, |
| Napi::Object object, |
| Name name, |
| Callable cb, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor Value( |
| const char* utf8name, |
| napi_value value, |
| napi_property_attributes attributes = napi_default); |
| static PropertyDescriptor Value( |
| const std::string& utf8name, |
| napi_value value, |
| napi_property_attributes attributes = napi_default); |
| static PropertyDescriptor Value( |
| napi_value name, |
| napi_value value, |
| napi_property_attributes attributes = napi_default); |
| static PropertyDescriptor Value( |
| Name name, |
| Napi::Value value, |
| napi_property_attributes attributes = napi_default); |
|
|
| PropertyDescriptor(napi_property_descriptor desc); |
|
|
| operator napi_property_descriptor&(); |
| operator const napi_property_descriptor&() const; |
|
|
| private: |
| napi_property_descriptor _desc; |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class ClassPropertyDescriptor { |
| public: |
| ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {} |
|
|
| operator napi_property_descriptor&() { return _desc; } |
| operator const napi_property_descriptor&() const { return _desc; } |
|
|
| private: |
| napi_property_descriptor _desc; |
| }; |
|
|
| template <typename T, typename TCallback> |
| struct MethodCallbackData { |
| TCallback callback; |
| void* data; |
| }; |
|
|
| template <typename T, typename TGetterCallback, typename TSetterCallback> |
| struct AccessorCallbackData { |
| TGetterCallback getterCallback; |
| TSetterCallback setterCallback; |
| void* data; |
| }; |
|
|
| template <typename T> |
| class InstanceWrap { |
| public: |
| using InstanceVoidMethodCallback = void (T::*)(const CallbackInfo& info); |
| using InstanceMethodCallback = Napi::Value (T::*)(const CallbackInfo& info); |
| using InstanceGetterCallback = Napi::Value (T::*)(const CallbackInfo& info); |
| using InstanceSetterCallback = void (T::*)(const CallbackInfo& info, |
| const Napi::Value& value); |
|
|
| using PropertyDescriptor = ClassPropertyDescriptor<T>; |
|
|
| static PropertyDescriptor InstanceMethod( |
| const char* utf8name, |
| InstanceVoidMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceMethod( |
| const char* utf8name, |
| InstanceMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceMethod( |
| Symbol name, |
| InstanceVoidMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceMethod( |
| Symbol name, |
| InstanceMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceVoidMethodCallback method> |
| static PropertyDescriptor InstanceMethod( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceMethodCallback method> |
| static PropertyDescriptor InstanceMethod( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceVoidMethodCallback method> |
| static PropertyDescriptor InstanceMethod( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceMethodCallback method> |
| static PropertyDescriptor InstanceMethod( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceAccessor( |
| const char* utf8name, |
| InstanceGetterCallback getter, |
| InstanceSetterCallback setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceAccessor( |
| Symbol name, |
| InstanceGetterCallback getter, |
| InstanceSetterCallback setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceGetterCallback getter, |
| InstanceSetterCallback setter = nullptr> |
| static PropertyDescriptor InstanceAccessor( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <InstanceGetterCallback getter, |
| InstanceSetterCallback setter = nullptr> |
| static PropertyDescriptor InstanceAccessor( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor InstanceValue( |
| const char* utf8name, |
| Napi::Value value, |
| napi_property_attributes attributes = napi_default); |
| static PropertyDescriptor InstanceValue( |
| Symbol name, |
| Napi::Value value, |
| napi_property_attributes attributes = napi_default); |
|
|
| protected: |
| static void AttachPropData(napi_env env, |
| napi_value value, |
| const napi_property_descriptor* prop); |
|
|
| private: |
| using This = InstanceWrap<T>; |
|
|
| using InstanceVoidMethodCallbackData = |
| MethodCallbackData<T, InstanceVoidMethodCallback>; |
| using InstanceMethodCallbackData = |
| MethodCallbackData<T, InstanceMethodCallback>; |
| using InstanceAccessorCallbackData = |
| AccessorCallbackData<T, InstanceGetterCallback, InstanceSetterCallback>; |
|
|
| static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value InstanceMethodCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value InstanceGetterCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value InstanceSetterCallbackWrapper(napi_env env, |
| napi_callback_info info); |
|
|
| template <InstanceSetterCallback method> |
| static napi_value WrappedMethod(napi_env env, |
| napi_callback_info info) NAPI_NOEXCEPT; |
|
|
| template <InstanceSetterCallback setter> |
| struct SetterTag {}; |
|
|
| template <InstanceSetterCallback setter> |
| static napi_callback WrapSetter(SetterTag<setter>) NAPI_NOEXCEPT { |
| return &This::WrappedMethod<setter>; |
| } |
| static napi_callback WrapSetter(SetterTag<nullptr>) NAPI_NOEXCEPT { |
| return nullptr; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class ObjectWrap : public InstanceWrap<T>, public Reference<Object> { |
| public: |
| ObjectWrap(const CallbackInfo& callbackInfo); |
| virtual ~ObjectWrap(); |
|
|
| static T* Unwrap(Object wrapper); |
|
|
| |
| |
| using StaticVoidMethodCallback = void (*)(const CallbackInfo& info); |
| using StaticMethodCallback = Napi::Value (*)(const CallbackInfo& info); |
| using StaticGetterCallback = Napi::Value (*)(const CallbackInfo& info); |
| using StaticSetterCallback = void (*)(const CallbackInfo& info, |
| const Napi::Value& value); |
|
|
| using PropertyDescriptor = ClassPropertyDescriptor<T>; |
|
|
| static Function DefineClass( |
| Napi::Env env, |
| const char* utf8name, |
| const std::initializer_list<PropertyDescriptor>& properties, |
| void* data = nullptr); |
| static Function DefineClass(Napi::Env env, |
| const char* utf8name, |
| const std::vector<PropertyDescriptor>& properties, |
| void* data = nullptr); |
| static PropertyDescriptor StaticMethod( |
| const char* utf8name, |
| StaticVoidMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticMethod( |
| const char* utf8name, |
| StaticMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticMethod( |
| Symbol name, |
| StaticVoidMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticMethod( |
| Symbol name, |
| StaticMethodCallback method, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticVoidMethodCallback method> |
| static PropertyDescriptor StaticMethod( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticVoidMethodCallback method> |
| static PropertyDescriptor StaticMethod( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticMethodCallback method> |
| static PropertyDescriptor StaticMethod( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticMethodCallback method> |
| static PropertyDescriptor StaticMethod( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticAccessor( |
| const char* utf8name, |
| StaticGetterCallback getter, |
| StaticSetterCallback setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticAccessor( |
| Symbol name, |
| StaticGetterCallback getter, |
| StaticSetterCallback setter, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticGetterCallback getter, StaticSetterCallback setter = nullptr> |
| static PropertyDescriptor StaticAccessor( |
| const char* utf8name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| template <StaticGetterCallback getter, StaticSetterCallback setter = nullptr> |
| static PropertyDescriptor StaticAccessor( |
| Symbol name, |
| napi_property_attributes attributes = napi_default, |
| void* data = nullptr); |
| static PropertyDescriptor StaticValue( |
| const char* utf8name, |
| Napi::Value value, |
| napi_property_attributes attributes = napi_default); |
| static PropertyDescriptor StaticValue( |
| Symbol name, |
| Napi::Value value, |
| napi_property_attributes attributes = napi_default); |
| static Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& callbackInfo); |
| virtual void Finalize(Napi::Env env); |
|
|
| private: |
| using This = ObjectWrap<T>; |
|
|
| static napi_value ConstructorCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value StaticVoidMethodCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value StaticMethodCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value StaticGetterCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static napi_value StaticSetterCallbackWrapper(napi_env env, |
| napi_callback_info info); |
| static void FinalizeCallback(napi_env env, void* data, void* hint); |
| static Function DefineClass(Napi::Env env, |
| const char* utf8name, |
| const size_t props_count, |
| const napi_property_descriptor* props, |
| void* data = nullptr); |
|
|
| using StaticVoidMethodCallbackData = |
| MethodCallbackData<T, StaticVoidMethodCallback>; |
| using StaticMethodCallbackData = MethodCallbackData<T, StaticMethodCallback>; |
|
|
| using StaticAccessorCallbackData = |
| AccessorCallbackData<T, StaticGetterCallback, StaticSetterCallback>; |
|
|
| template <StaticSetterCallback method> |
| static napi_value WrappedMethod(napi_env env, |
| napi_callback_info info) NAPI_NOEXCEPT; |
|
|
| template <StaticSetterCallback setter> |
| struct StaticSetterTag {}; |
|
|
| template <StaticSetterCallback setter> |
| static napi_callback WrapStaticSetter(StaticSetterTag<setter>) NAPI_NOEXCEPT { |
| return &This::WrappedMethod<setter>; |
| } |
| static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>) |
| NAPI_NOEXCEPT { |
| return nullptr; |
| } |
|
|
| bool _construction_failed = true; |
| }; |
|
|
| class HandleScope { |
| public: |
| HandleScope(napi_env env, napi_handle_scope scope); |
| explicit HandleScope(Napi::Env env); |
| ~HandleScope(); |
|
|
| |
| NAPI_DISALLOW_ASSIGN_COPY(HandleScope) |
|
|
| operator napi_handle_scope() const; |
|
|
| Napi::Env Env() const; |
|
|
| private: |
| napi_env _env; |
| napi_handle_scope _scope; |
| }; |
|
|
| class EscapableHandleScope { |
| public: |
| EscapableHandleScope(napi_env env, napi_escapable_handle_scope scope); |
| explicit EscapableHandleScope(Napi::Env env); |
| ~EscapableHandleScope(); |
|
|
| |
| NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope) |
|
|
| operator napi_escapable_handle_scope() const; |
|
|
| Napi::Env Env() const; |
| Value Escape(napi_value escapee); |
|
|
| private: |
| napi_env _env; |
| napi_escapable_handle_scope _scope; |
| }; |
|
|
| #if (NAPI_VERSION > 2) |
| class CallbackScope { |
| public: |
| CallbackScope(napi_env env, napi_callback_scope scope); |
| CallbackScope(napi_env env, napi_async_context context); |
| virtual ~CallbackScope(); |
|
|
| |
| NAPI_DISALLOW_ASSIGN_COPY(CallbackScope) |
|
|
| operator napi_callback_scope() const; |
|
|
| Napi::Env Env() const; |
|
|
| private: |
| napi_env _env; |
| napi_callback_scope _scope; |
| }; |
| #endif |
|
|
| class AsyncContext { |
| public: |
| explicit AsyncContext(napi_env env, const char* resource_name); |
| explicit AsyncContext(napi_env env, |
| const char* resource_name, |
| const Object& resource); |
| virtual ~AsyncContext(); |
|
|
| AsyncContext(AsyncContext&& other); |
| AsyncContext& operator=(AsyncContext&& other); |
| NAPI_DISALLOW_ASSIGN_COPY(AsyncContext) |
|
|
| operator napi_async_context() const; |
|
|
| Napi::Env Env() const; |
|
|
| private: |
| napi_env _env; |
| napi_async_context _context; |
| }; |
|
|
| #if NAPI_HAS_THREADS |
| class AsyncWorker { |
| public: |
| virtual ~AsyncWorker(); |
|
|
| NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker) |
|
|
| operator napi_async_work() const; |
|
|
| Napi::Env Env() const; |
|
|
| void Queue(); |
| void Cancel(); |
| void SuppressDestruct(); |
|
|
| ObjectReference& Receiver(); |
| FunctionReference& Callback(); |
|
|
| virtual void OnExecute(Napi::Env env); |
| virtual void OnWorkComplete(Napi::Env env, napi_status status); |
|
|
| protected: |
| explicit AsyncWorker(const Function& callback); |
| explicit AsyncWorker(const Function& callback, const char* resource_name); |
| explicit AsyncWorker(const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
| explicit AsyncWorker(const Object& receiver, const Function& callback); |
| explicit AsyncWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name); |
| explicit AsyncWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
|
|
| explicit AsyncWorker(Napi::Env env); |
| explicit AsyncWorker(Napi::Env env, const char* resource_name); |
| explicit AsyncWorker(Napi::Env env, |
| const char* resource_name, |
| const Object& resource); |
|
|
| virtual void Execute() = 0; |
| virtual void OnOK(); |
| virtual void OnError(const Error& e); |
| virtual void Destroy(); |
| virtual std::vector<napi_value> GetResult(Napi::Env env); |
|
|
| void SetError(const std::string& error); |
|
|
| private: |
| static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker); |
| static inline void OnAsyncWorkComplete(napi_env env, |
| napi_status status, |
| void* asyncworker); |
|
|
| napi_env _env; |
| napi_async_work _work; |
| ObjectReference _receiver; |
| FunctionReference _callback; |
| std::string _error; |
| bool _suppress_destruct; |
| }; |
| #endif |
|
|
| #if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) |
| class ThreadSafeFunction { |
| public: |
| |
| template <typename ResourceString> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount); |
|
|
| |
| template <typename ResourceString, typename ContextType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context); |
|
|
| |
| template <typename ResourceString, typename Finalizer> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| Finalizer finalizeCallback); |
|
|
| |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data); |
|
|
| |
| template <typename ResourceString, typename ContextType, typename Finalizer> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback); |
|
|
| |
| template <typename ResourceString, |
| typename ContextType, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data); |
|
|
| |
| template <typename ResourceString> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount); |
|
|
| |
| template <typename ResourceString, typename ContextType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context); |
|
|
| |
| template <typename ResourceString, typename Finalizer> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| Finalizer finalizeCallback); |
|
|
| |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data); |
|
|
| |
| template <typename ResourceString, typename ContextType, typename Finalizer> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback); |
|
|
| |
| template <typename ResourceString, |
| typename ContextType, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data); |
|
|
| ThreadSafeFunction(); |
| ThreadSafeFunction(napi_threadsafe_function tsFunctionValue); |
|
|
| operator napi_threadsafe_function() const; |
|
|
| |
| napi_status BlockingCall() const; |
|
|
| |
| template <typename Callback> |
| napi_status BlockingCall(Callback callback) const; |
|
|
| |
| template <typename DataType, typename Callback> |
| napi_status BlockingCall(DataType* data, Callback callback) const; |
|
|
| |
| napi_status NonBlockingCall() const; |
|
|
| |
| template <typename Callback> |
| napi_status NonBlockingCall(Callback callback) const; |
|
|
| |
| template <typename DataType, typename Callback> |
| napi_status NonBlockingCall(DataType* data, Callback callback) const; |
|
|
| |
| void Ref(napi_env env) const; |
|
|
| |
| void Unref(napi_env env) const; |
|
|
| |
| napi_status Acquire() const; |
|
|
| |
| napi_status Release() const; |
|
|
| |
| napi_status Abort() const; |
|
|
| struct ConvertibleContext { |
| template <class T> |
| operator T*() { |
| return static_cast<T*>(context); |
| } |
| void* context; |
| }; |
|
|
| |
| ConvertibleContext GetContext() const; |
|
|
| private: |
| using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>; |
|
|
| template <typename ResourceString, |
| typename ContextType, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static ThreadSafeFunction New(napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data, |
| napi_finalize wrapper); |
|
|
| napi_status CallInternal(CallbackWrapper* callbackWrapper, |
| napi_threadsafe_function_call_mode mode) const; |
|
|
| static void CallJS(napi_env env, |
| napi_value jsCallback, |
| void* context, |
| void* data); |
|
|
| napi_threadsafe_function _tsfn; |
| }; |
|
|
| |
| |
| template <typename ContextType = std::nullptr_t, |
| typename DataType = void, |
| void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) = |
| nullptr> |
| class TypedThreadSafeFunction { |
| public: |
| |
| |
| |
| |
| |
| #if NAPI_VERSION > 4 |
| static std::nullptr_t EmptyFunctionFactory(Napi::Env env); |
| #else |
| static Napi::Function EmptyFunctionFactory(Napi::Env env); |
| #endif |
| static Napi::Function FunctionOrEmpty(Napi::Env env, |
| Napi::Function& callback); |
|
|
| #if NAPI_VERSION > 4 |
| |
| |
| |
| template <typename ResourceString> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context = nullptr); |
|
|
| |
| |
| |
| template <typename ResourceString> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context = nullptr); |
|
|
| |
| |
| |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType = void> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data = nullptr); |
|
|
| |
| |
| |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType = void> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data = nullptr); |
| #endif |
|
|
| |
| |
| |
| template <typename ResourceString> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context = nullptr); |
|
|
| |
| |
| |
| template <typename ResourceString> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context = nullptr); |
|
|
| |
| |
| |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType = void> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Function& callback, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data = nullptr); |
|
|
| |
| |
| |
| template <typename CallbackType, |
| typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| CallbackType callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data = nullptr); |
|
|
| TypedThreadSafeFunction(); |
| TypedThreadSafeFunction(napi_threadsafe_function tsFunctionValue); |
|
|
| operator napi_threadsafe_function() const; |
|
|
| |
| napi_status BlockingCall(DataType* data = nullptr) const; |
|
|
| |
| napi_status NonBlockingCall(DataType* data = nullptr) const; |
|
|
| |
| void Ref(napi_env env) const; |
|
|
| |
| void Unref(napi_env env) const; |
|
|
| |
| napi_status Acquire() const; |
|
|
| |
| napi_status Release() const; |
|
|
| |
| napi_status Abort() const; |
|
|
| |
| ContextType* GetContext() const; |
|
|
| private: |
| template <typename ResourceString, |
| typename Finalizer, |
| typename FinalizerDataType> |
| static TypedThreadSafeFunction<ContextType, DataType, CallJs> New( |
| napi_env env, |
| const Function& callback, |
| const Object& resource, |
| ResourceString resourceName, |
| size_t maxQueueSize, |
| size_t initialThreadCount, |
| ContextType* context, |
| Finalizer finalizeCallback, |
| FinalizerDataType* data, |
| napi_finalize wrapper); |
|
|
| static void CallJsInternal(napi_env env, |
| napi_value jsCallback, |
| void* context, |
| void* data); |
|
|
| protected: |
| napi_threadsafe_function _tsfn; |
| }; |
| template <typename DataType> |
| class AsyncProgressWorkerBase : public AsyncWorker { |
| public: |
| virtual void OnWorkProgress(DataType* data) = 0; |
| class ThreadSafeData { |
| public: |
| ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data) |
| : _asyncprogressworker(asyncprogressworker), _data(data) {} |
|
|
| AsyncProgressWorkerBase* asyncprogressworker() { |
| return _asyncprogressworker; |
| }; |
| DataType* data() { return _data; }; |
|
|
| private: |
| AsyncProgressWorkerBase* _asyncprogressworker; |
| DataType* _data; |
| }; |
| void OnWorkComplete(Napi::Env env, napi_status status) override; |
|
|
| protected: |
| explicit AsyncProgressWorkerBase(const Object& receiver, |
| const Function& callback, |
| const char* resource_name, |
| const Object& resource, |
| size_t queue_size = 1); |
| virtual ~AsyncProgressWorkerBase(); |
|
|
| |
| |
| #if NAPI_VERSION > 4 |
| explicit AsyncProgressWorkerBase(Napi::Env env, |
| const char* resource_name, |
| const Object& resource, |
| size_t queue_size = 1); |
| #endif |
|
|
| static inline void OnAsyncWorkProgress(Napi::Env env, |
| Napi::Function jsCallback, |
| void* data); |
|
|
| napi_status NonBlockingCall(DataType* data); |
|
|
| private: |
| ThreadSafeFunction _tsfn; |
| bool _work_completed = false; |
| napi_status _complete_status; |
| static inline void OnThreadSafeFunctionFinalize( |
| Napi::Env env, void* data, AsyncProgressWorkerBase* context); |
| }; |
|
|
| template <class T> |
| class AsyncProgressWorker : public AsyncProgressWorkerBase<void> { |
| public: |
| virtual ~AsyncProgressWorker(); |
|
|
| class ExecutionProgress { |
| friend class AsyncProgressWorker; |
|
|
| public: |
| void Signal() const; |
| void Send(const T* data, size_t count) const; |
|
|
| private: |
| explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {} |
| AsyncProgressWorker* const _worker; |
| }; |
|
|
| void OnWorkProgress(void*) override; |
|
|
| protected: |
| explicit AsyncProgressWorker(const Function& callback); |
| explicit AsyncProgressWorker(const Function& callback, |
| const char* resource_name); |
| explicit AsyncProgressWorker(const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
| explicit AsyncProgressWorker(const Object& receiver, |
| const Function& callback); |
| explicit AsyncProgressWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name); |
| explicit AsyncProgressWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
|
|
| |
| |
| #if NAPI_VERSION > 4 |
| explicit AsyncProgressWorker(Napi::Env env); |
| explicit AsyncProgressWorker(Napi::Env env, const char* resource_name); |
| explicit AsyncProgressWorker(Napi::Env env, |
| const char* resource_name, |
| const Object& resource); |
| #endif |
| virtual void Execute(const ExecutionProgress& progress) = 0; |
| virtual void OnProgress(const T* data, size_t count) = 0; |
|
|
| private: |
| void Execute() override; |
| void Signal(); |
| void SendProgress_(const T* data, size_t count); |
|
|
| std::mutex _mutex; |
| T* _asyncdata; |
| size_t _asyncsize; |
| bool _signaled; |
| }; |
|
|
| template <class T> |
| class AsyncProgressQueueWorker |
| : public AsyncProgressWorkerBase<std::pair<T*, size_t>> { |
| public: |
| virtual ~AsyncProgressQueueWorker(){}; |
|
|
| class ExecutionProgress { |
| friend class AsyncProgressQueueWorker; |
|
|
| public: |
| void Signal() const; |
| void Send(const T* data, size_t count) const; |
|
|
| private: |
| explicit ExecutionProgress(AsyncProgressQueueWorker* worker) |
| : _worker(worker) {} |
| AsyncProgressQueueWorker* const _worker; |
| }; |
|
|
| void OnWorkComplete(Napi::Env env, napi_status status) override; |
| void OnWorkProgress(std::pair<T*, size_t>*) override; |
|
|
| protected: |
| explicit AsyncProgressQueueWorker(const Function& callback); |
| explicit AsyncProgressQueueWorker(const Function& callback, |
| const char* resource_name); |
| explicit AsyncProgressQueueWorker(const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
| explicit AsyncProgressQueueWorker(const Object& receiver, |
| const Function& callback); |
| explicit AsyncProgressQueueWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name); |
| explicit AsyncProgressQueueWorker(const Object& receiver, |
| const Function& callback, |
| const char* resource_name, |
| const Object& resource); |
|
|
| |
| |
| #if NAPI_VERSION > 4 |
| explicit AsyncProgressQueueWorker(Napi::Env env); |
| explicit AsyncProgressQueueWorker(Napi::Env env, const char* resource_name); |
| explicit AsyncProgressQueueWorker(Napi::Env env, |
| const char* resource_name, |
| const Object& resource); |
| #endif |
| virtual void Execute(const ExecutionProgress& progress) = 0; |
| virtual void OnProgress(const T* data, size_t count) = 0; |
|
|
| private: |
| void Execute() override; |
| void Signal() const; |
| void SendProgress_(const T* data, size_t count); |
| }; |
| #endif |
|
|
| |
| class MemoryManagement { |
| public: |
| static int64_t AdjustExternalMemory(Env env, int64_t change_in_bytes); |
| }; |
|
|
| |
| class VersionManagement { |
| public: |
| static uint32_t GetNapiVersion(Env env); |
| static const napi_node_version* GetNodeVersion(Env env); |
| }; |
|
|
| #if NAPI_VERSION > 5 |
| template <typename T> |
| class Addon : public InstanceWrap<T> { |
| public: |
| static inline Object Init(Env env, Object exports); |
| static T* Unwrap(Object wrapper); |
|
|
| protected: |
| using AddonProp = ClassPropertyDescriptor<T>; |
| void DefineAddon(Object exports, |
| const std::initializer_list<AddonProp>& props); |
| Napi::Object DefineProperties(Object object, |
| const std::initializer_list<AddonProp>& props); |
|
|
| private: |
| Object entry_point_; |
| }; |
| #endif |
|
|
| #ifdef NAPI_CPP_CUSTOM_NAMESPACE |
| } |
| #endif |
|
|
| } |
|
|
| |
| #include "napi-inl.h" |
|
|
| #endif |
|
|