feat!: enforce callback signatures with C++ Concepts (#1762) Co-authored-by: vasudevan <vasudevan@syntrio.in> Co-authored-by: Chengzhong Wu <cwu631@bloomberg.net>
diff --git a/napi-inl.deprecated.h b/napi-inl.deprecated.h index 3ddbb2e..6b79a9d 100644 --- a/napi-inl.deprecated.h +++ b/napi-inl.deprecated.h
@@ -6,11 +6,12 @@ //////////////////////////////////////////////////////////////////////////////// template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - const char* utf8name, - Getter getter, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(const char* utf8name, + Getter getter, + napi_property_attributes attributes, + void* /*data*/) { using CbData = details::CallbackData<Getter, Napi::Value>; // TODO: Delete when the function is destroyed auto callbackData = new CbData({getter, nullptr}); @@ -26,20 +27,22 @@ } template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - const std::string& utf8name, - Getter getter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(const std::string& utf8name, + Getter getter, + napi_property_attributes attributes, + void* data) { return Accessor(utf8name.c_str(), getter, attributes, data); } template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - napi_value name, - Getter getter, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(napi_value name, + Getter getter, + napi_property_attributes attributes, + void* /*data*/) { using CbData = details::CallbackData<Getter, Napi::Value>; // TODO: Delete when the function is destroyed auto callbackData = new CbData({getter, nullptr}); @@ -55,6 +58,7 @@ } template <typename Getter> +NAPI_REQUIRES(concepts::GetterCallable<Getter>) inline PropertyDescriptor PropertyDescriptor::Accessor( Name name, Getter getter, napi_property_attributes attributes, void* data) { napi_value nameValue = name; @@ -62,12 +66,14 @@ } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - const char* utf8name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(const char* utf8name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* /*data*/) { using CbData = details::AccessorCallbackData<Getter, Setter>; // TODO: Delete when the function is destroyed auto callbackData = new CbData({getter, setter, nullptr}); @@ -83,22 +89,26 @@ } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - const std::string& utf8name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(const std::string& utf8name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* data) { return Accessor(utf8name.c_str(), getter, setter, attributes, data); } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - napi_value name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(napi_value name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* /*data*/) { using CbData = details::AccessorCallbackData<Getter, Setter>; // TODO: Delete when the function is destroyed auto callbackData = new CbData({getter, setter, nullptr}); @@ -114,23 +124,26 @@ } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Name name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Name name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* data) { napi_value nameValue = name; return PropertyDescriptor::Accessor( nameValue, getter, setter, attributes, data); } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - const char* utf8name, - Callable cb, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(const char* utf8name, + Callable cb, + napi_property_attributes attributes, + void* /*data*/) { using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr))); using CbData = details::CallbackData<Callable, ReturnType>; // TODO: Delete when the function is destroyed @@ -147,20 +160,22 @@ } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - const std::string& utf8name, - Callable cb, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(const std::string& utf8name, + Callable cb, + napi_property_attributes attributes, + void* data) { return Function(utf8name.c_str(), cb, attributes, data); } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - napi_value name, - Callable cb, - napi_property_attributes attributes, - void* /*data*/) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(napi_value name, + Callable cb, + napi_property_attributes attributes, + void* /*data*/) { using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr))); using CbData = details::CallbackData<Callable, ReturnType>; // TODO: Delete when the function is destroyed @@ -177,10 +192,11 @@ } template <typename Callable> +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) inline PropertyDescriptor PropertyDescriptor::Function( Name name, Callable cb, napi_property_attributes attributes, void* data) { napi_value nameValue = name; return PropertyDescriptor::Function(nameValue, cb, attributes, data); } -#endif // !SRC_NAPI_INL_DEPRECATED_H_ +#endif // SRC_NAPI_INL_DEPRECATED_H_
diff --git a/napi-inl.h b/napi-inl.h index 59f1ca1..9424fd0 100644 --- a/napi-inl.h +++ b/napi-inl.h
@@ -1904,6 +1904,7 @@ } template <typename Finalizer, typename T> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) const { details::FinalizeData<T, Finalizer>* finalizeData = new details::FinalizeData<T, Finalizer>( @@ -1918,6 +1919,7 @@ } template <typename Finalizer, typename T, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data, Hint* finalizeHint) const { @@ -2050,6 +2052,7 @@ template <typename T> template <typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) inline External<T> External<T>::New(napi_env env, T* data, Finalizer finalizeCallback) { @@ -2072,6 +2075,7 @@ template <typename T> template <typename Finalizer, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) inline External<T> External<T>::New(napi_env env, T* data, Finalizer finalizeCallback, @@ -2231,6 +2235,7 @@ } template <typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, void>) inline ArrayBuffer ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength, @@ -2255,6 +2260,7 @@ } template <typename Finalizer, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, void, Hint>) inline ArrayBuffer ArrayBuffer::New(napi_env env, void* externalData, size_t byteLength, @@ -2824,6 +2830,7 @@ } template <typename Callable> +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) inline Function Function::New(napi_env env, Callable cb, const char* utf8name, @@ -2844,6 +2851,7 @@ } template <typename Callable> +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) inline Function Function::New(napi_env env, Callable cb, const std::string& utf8name, @@ -3130,6 +3138,7 @@ template <typename T> template <typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length, @@ -3154,6 +3163,7 @@ template <typename T> template <typename Finalizer, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length, @@ -3197,6 +3207,7 @@ template <typename T> template <typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length, @@ -3231,6 +3242,7 @@ template <typename T> template <typename Finalizer, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length, @@ -4391,13 +4403,14 @@ } template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - const char* utf8name, - Getter getter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + const char* utf8name, + Getter getter, + napi_property_attributes attributes, + void* data) { using CbData = details::CallbackData<Getter, Napi::Value>; auto callbackData = new CbData({getter, data}); @@ -4418,24 +4431,26 @@ } template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - const std::string& utf8name, - Getter getter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + const std::string& utf8name, + Getter getter, + napi_property_attributes attributes, + void* data) { return Accessor(env, object, utf8name.c_str(), getter, attributes, data); } template <typename Getter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - Name name, - Getter getter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::GetterCallable<Getter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + Name name, + Getter getter, + napi_property_attributes attributes, + void* data) { using CbData = details::CallbackData<Getter, Napi::Value>; auto callbackData = new CbData({getter, data}); @@ -4456,14 +4471,16 @@ } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - const char* utf8name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + const char* utf8name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* data) { using CbData = details::AccessorCallbackData<Getter, Setter>; auto callbackData = new CbData({getter, setter, data}); @@ -4484,27 +4501,31 @@ } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - const std::string& utf8name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + const std::string& utf8name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* data) { return Accessor( env, object, utf8name.c_str(), getter, setter, attributes, data); } template <typename Getter, typename Setter> -inline PropertyDescriptor PropertyDescriptor::Accessor( - Napi::Env env, - Napi::Object object, - Name name, - Getter getter, - Setter setter, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>) +inline PropertyDescriptor + PropertyDescriptor::Accessor(Napi::Env env, + Napi::Object object, + Name name, + Getter getter, + Setter setter, + napi_property_attributes attributes, + void* data) { using CbData = details::AccessorCallbackData<Getter, Setter>; auto callbackData = new CbData({getter, setter, data}); @@ -4525,13 +4546,14 @@ } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - Napi::Env env, - Napi::Object /*object*/, - const char* utf8name, - Callable cb, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(Napi::Env env, + Napi::Object /*object*/, + const char* utf8name, + Callable cb, + napi_property_attributes attributes, + void* data) { return PropertyDescriptor({utf8name, nullptr, nullptr, @@ -4543,24 +4565,26 @@ } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - Napi::Env env, - Napi::Object object, - const std::string& utf8name, - Callable cb, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(Napi::Env env, + Napi::Object object, + const std::string& utf8name, + Callable cb, + napi_property_attributes attributes, + void* data) { return Function(env, object, utf8name.c_str(), cb, attributes, data); } template <typename Callable> -inline PropertyDescriptor PropertyDescriptor::Function( - Napi::Env env, - Napi::Object /*object*/, - Name name, - Callable cb, - napi_property_attributes attributes, - void* data) { +NAPI_REQUIRES(concepts::CallbackCallable<Callable>) +inline PropertyDescriptor + PropertyDescriptor::Function(Napi::Env env, + Napi::Object /*object*/, + Name name, + Callable cb, + napi_property_attributes attributes, + void* data) { return PropertyDescriptor({nullptr, name, nullptr, @@ -6344,6 +6368,7 @@ // static template <typename ResourceString, typename Finalizer> +NAPI_REQUIRES(concepts::Finalizer<Finalizer>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, ResourceString resourceName, @@ -6363,6 +6388,7 @@ template <typename ResourceString, typename Finalizer, typename FinalizerDataType> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, FinalizerDataType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, ResourceString resourceName, @@ -6382,6 +6408,7 @@ // static template <typename ResourceString, typename ContextType, typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, ContextType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, ResourceString resourceName, @@ -6404,6 +6431,8 @@ typename ContextType, typename Finalizer, typename FinalizerDataType> +NAPI_REQUIRES( + concepts::FinalizerWithHint<Finalizer, FinalizerDataType, ContextType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, ResourceString resourceName, @@ -6461,6 +6490,7 @@ // static template <typename ResourceString, typename Finalizer> +NAPI_REQUIRES(concepts::Finalizer<Finalizer>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, const Object& resource, @@ -6484,6 +6514,7 @@ template <typename ResourceString, typename Finalizer, typename FinalizerDataType> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, FinalizerDataType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, const Object& resource, @@ -6507,6 +6538,7 @@ // static template <typename ResourceString, typename ContextType, typename Finalizer> +NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, ContextType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, const Object& resource, @@ -6534,6 +6566,8 @@ typename ContextType, typename Finalizer, typename FinalizerDataType> +NAPI_REQUIRES( + concepts::FinalizerWithHint<Finalizer, FinalizerDataType, ContextType>) inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env, const Function& callback, const Object& resource, @@ -7209,6 +7243,7 @@ #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER template <typename FinalizerType> +NAPI_REQUIRES(concepts::Finalizer<FinalizerType>) inline void BasicEnv::PostFinalizer(FinalizerType finalizeCallback) const { using T = void*; details::FinalizeData<T, FinalizerType>* finalizeData = @@ -7228,6 +7263,7 @@ } template <typename FinalizerType, typename T> +NAPI_REQUIRES(concepts::FinalizerWithData<FinalizerType, T>) inline void BasicEnv::PostFinalizer(FinalizerType finalizeCallback, T* data) const { details::FinalizeData<T, FinalizerType>* finalizeData = @@ -7247,6 +7283,7 @@ } template <typename FinalizerType, typename T, typename Hint> +NAPI_REQUIRES(concepts::FinalizerWithHint<FinalizerType, T, Hint>) inline void BasicEnv::PostFinalizer(FinalizerType finalizeCallback, T* data, Hint* finalizeHint) const {
diff --git a/napi.h b/napi.h index 6b4692c..412dfd5 100644 --- a/napi.h +++ b/napi.h
@@ -24,6 +24,16 @@ #include <utility> #include <vector> +#if defined(__has_include) +#if __has_include(<version>) +#include <version> +#endif +#endif +#if defined(__cpp_lib_concepts) && __cpp_lib_concepts >= 202002L +#include <concepts> +#define NAPI_HAS_CPP20_CONCEPTS 1 +#endif + // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known // good version) #if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210 @@ -258,6 +268,43 @@ /// (init) function. using ModuleRegisterCallback = Object (*)(Env env, Object exports); +#if defined(NAPI_HAS_CPP20_CONCEPTS) +namespace concepts { + +template <typename F> +concept CallbackCallable = + std::invocable<F, const CallbackInfo&> && + (std::same_as<std::invoke_result_t<F, const CallbackInfo&>, void> || + std::convertible_to<std::invoke_result_t<F, const CallbackInfo&>, + napi_value>); + +template <typename F> +concept GetterCallable = + std::invocable<F, const CallbackInfo&> && + (std::convertible_to<std::invoke_result_t<F, const CallbackInfo&>, + napi_value>); + +template <typename F> +concept SetterCallable = + std::invocable<F, const CallbackInfo&> && + (std::same_as<std::invoke_result_t<F, const CallbackInfo&>, void>); + +template <typename F> +concept Finalizer = std::invocable<F, Env>; + +template <typename F, typename T> +concept FinalizerWithData = std::invocable<F, Env, T*>; + +template <typename F, typename T, typename Hint> +concept FinalizerWithHint = std::invocable<F, Env, T*, Hint*>; + +} // namespace concepts + +#define NAPI_REQUIRES(...) requires __VA_ARGS__ +#else +#define NAPI_REQUIRES(...) +#endif + class MemoryManagement; /// A simple Maybe type, representing an object which may or may not have a @@ -426,15 +473,18 @@ #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER // FinalizerType must implement `void operator()(Env env)`. template <typename FinalizerType> + NAPI_REQUIRES(concepts::Finalizer<FinalizerType>) inline void PostFinalizer(FinalizerType finalizeCallback) const; // FinalizerType must implement `void operator()(Env env, T* data)`. template <typename FinalizerType, typename T> + NAPI_REQUIRES(concepts::FinalizerWithData<FinalizerType, T>) inline void PostFinalizer(FinalizerType finalizeCallback, T* data) const; // FinalizerType must implement `void operator()(Env env, T* data, // Hint* hint)`. template <typename FinalizerType, typename T, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<FinalizerType, T, Hint>) inline void PostFinalizer(FinalizerType finalizeCallback, T* data, Hint* finalizeHint) const; @@ -1166,11 +1216,13 @@ // Finalizer must implement `void operator()(Env env, T* data)`. template <typename Finalizer, typename T> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) inline void AddFinalizer(Finalizer finalizeCallback, T* data) const; // Finalizer must implement `void operator()(Env env, T* data, // Hint* hint)`. template <typename Finalizer, typename T, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) inline void AddFinalizer(Finalizer finalizeCallback, T* data, Hint* finalizeHint) const; @@ -1216,10 +1268,12 @@ // Finalizer must implement `void operator()(Env env, T* data)`. template <typename Finalizer> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) static External New(napi_env env, T* data, Finalizer finalizeCallback); // Finalizer must implement `void operator()(Env env, T* data, // Hint* hint)`. template <typename Finalizer, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) static External New(napi_env env, T* data, Finalizer finalizeCallback, @@ -1334,22 +1388,24 @@ /// Creates a new ArrayBuffer instance, using an external buffer with /// specified byte length. template <typename Finalizer> - static ArrayBuffer New( - napi_env env, ///< Node-API environment - void* externalData, ///< Pointer to the external buffer to be used by - ///< the array - size_t byteLength, ///< Length of the external buffer to be used by the - ///< array, - /// in bytes - Finalizer finalizeCallback ///< Function to be called when the array - ///< buffer is destroyed; - /// must implement `void operator()(Env env, - /// void* externalData)` - ); + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, void>) + static ArrayBuffer + New(napi_env env, ///< Node-API environment + void* externalData, ///< Pointer to the external buffer to be used by + ///< the array + size_t byteLength, ///< Length of the external buffer to be used by + ///< the array, + /// in bytes + Finalizer finalizeCallback ///< Function to be called when the array + ///< buffer is destroyed; + /// must implement `void operator()(Env + /// env, void* externalData)` + ); /// Creates a new ArrayBuffer instance, using an external buffer with /// specified byte length. template <typename Finalizer, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, void, Hint>) static ArrayBuffer New( napi_env env, ///< Node-API environment void* externalData, ///< Pointer to the external buffer to be used by @@ -1674,6 +1730,7 @@ /// Callable must implement operator() accepting a const CallbackInfo& /// and return either void or Value. template <typename Callable> + NAPI_REQUIRES(concepts::CallbackCallable<Callable>) static Function New(napi_env env, Callable cb, const char* utf8name = nullptr, @@ -1681,6 +1738,7 @@ /// Callable must implement operator() accepting a const CallbackInfo& /// and return either void or Value. template <typename Callable> + NAPI_REQUIRES(concepts::CallbackCallable<Callable>) static Function New(napi_env env, Callable cb, const std::string& utf8name, @@ -1769,6 +1827,7 @@ // Finalizer must implement `void operator()(Env env, T* data)`. template <typename Finalizer> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) static Buffer<T> New(napi_env env, T* data, size_t length, @@ -1776,6 +1835,7 @@ // Finalizer must implement `void operator()(Env env, T* data, // Hint* hint)`. template <typename Finalizer, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) static Buffer<T> New(napi_env env, T* data, size_t length, @@ -1786,6 +1846,7 @@ static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length); // Finalizer must implement `void operator()(Env env, T* data)`. template <typename Finalizer> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, T>) static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length, @@ -1793,6 +1854,7 @@ // Finalizer must implement `void operator()(Env env, T* data, // Hint* hint)`. template <typename Finalizer, typename Hint> + NAPI_REQUIRES(concepts::FinalizerWithHint<Finalizer, T, Hint>) static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length, @@ -2175,81 +2237,97 @@ #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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<Callable>) + static PropertyDescriptor + Function(Name name, + Callable cb, + napi_property_attributes attributes = napi_default, + void* data = nullptr); #endif // !NODE_ADDON_API_DISABLE_DEPRECATED template <GetterCallback Getter> @@ -2289,80 +2367,92 @@ 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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES(concepts::GetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES( + concepts::GetterCallable<Getter>&& concepts::SetterCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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); + NAPI_REQUIRES(concepts::CallbackCallable<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, @@ -2890,6 +2980,7 @@ // This API may only be called from the main thread. // Finalizer must implement `void operator()(Env env)`. template <typename ResourceString, typename Finalizer> + NAPI_REQUIRES(concepts::Finalizer<Finalizer>) static ThreadSafeFunction New(napi_env env, const Function& callback, ResourceString resourceName, @@ -2903,6 +2994,7 @@ template <typename ResourceString, typename Finalizer, typename FinalizerDataType> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, FinalizerDataType>) static ThreadSafeFunction New(napi_env env, const Function& callback, ResourceString resourceName, @@ -2915,6 +3007,7 @@ // Finalizer must implement // `void operator()(Env env, ContextType* context)`. template <typename ResourceString, typename ContextType, typename Finalizer> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, ContextType>) static ThreadSafeFunction New(napi_env env, const Function& callback, ResourceString resourceName, @@ -2930,6 +3023,8 @@ typename ContextType, typename Finalizer, typename FinalizerDataType> + NAPI_REQUIRES( + concepts::FinalizerWithHint<Finalizer, FinalizerDataType, ContextType>) static ThreadSafeFunction New(napi_env env, const Function& callback, ResourceString resourceName, @@ -2961,6 +3056,7 @@ // This API may only be called from the main thread. // Finalizer must implement `void operator()(Env env)`. template <typename ResourceString, typename Finalizer> + NAPI_REQUIRES(concepts::Finalizer<Finalizer>) static ThreadSafeFunction New(napi_env env, const Function& callback, const Object& resource, @@ -2975,6 +3071,7 @@ template <typename ResourceString, typename Finalizer, typename FinalizerDataType> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, FinalizerDataType>) static ThreadSafeFunction New(napi_env env, const Function& callback, const Object& resource, @@ -2988,6 +3085,7 @@ // Finalizer must implement // `void operator()(Env env, ContextType* context)`. template <typename ResourceString, typename ContextType, typename Finalizer> + NAPI_REQUIRES(concepts::FinalizerWithData<Finalizer, ContextType>) static ThreadSafeFunction New(napi_env env, const Function& callback, const Object& resource, @@ -3004,6 +3102,8 @@ typename ContextType, typename Finalizer, typename FinalizerDataType> + NAPI_REQUIRES( + concepts::FinalizerWithHint<Finalizer, FinalizerDataType, ContextType>) static ThreadSafeFunction New(napi_env env, const Function& callback, const Object& resource,
diff --git a/test/binding.cc b/test/binding.cc index fa651cc..741b6a8 100644 --- a/test/binding.cc +++ b/test/binding.cc
@@ -26,6 +26,7 @@ #if (NAPI_VERSION > 2) Object InitCallbackScope(Env env); #endif +Object InitConcepts(Env env); #if (NAPI_VERSION > 4) Object InitDate(Env env); #endif @@ -118,6 +119,7 @@ #if (NAPI_VERSION > 2) exports.Set("callbackscope", InitCallbackScope(env)); #endif + exports.Set("concepts", InitConcepts(env)); exports.Set("callbackInfo", InitCallbackInfo(env)); exports.Set("dataview", InitDataView(env)); exports.Set("dataview_read_write", InitDataView(env));
diff --git a/test/binding.gyp b/test/binding.gyp index 9ff334b..7b53b15 100644 --- a/test/binding.gyp +++ b/test/binding.gyp
@@ -23,6 +23,7 @@ 'buffer_no_external.cc', 'buffer.cc', 'callbackscope.cc', + 'concepts.cc', 'dataview/dataview.cc', 'dataview/dataview_read_write.cc', 'env_cleanup.cc', @@ -92,6 +93,9 @@ 'build_sources_type_check': [ 'value_type_cast.cc' ], + 'build_sources_cpp20': [ + 'concepts.cc' + ], 'want_coverage': '<!(node -p process.env.npm_config_coverage)', 'use_node_api_headers': '<!(node -p process.env.use_node_api_headers)', 'conditions': [ @@ -119,6 +123,21 @@ 'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS'] }, { + 'target_name': 'binding_cpp20', + 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], + 'sources': ['>@(build_sources_cpp20)'], + 'defines': ['BUILDING_CPP20_MODULE'], + 'cflags_cc': ['-std=c++20'], + 'msvs_settings': { + 'VCCLCompilerTool': { + 'AdditionalOptions': ['/std:c++20', '/Zc:__cplusplus'] + } + }, + 'xcode_settings': { + 'CLANG_CXX_LANGUAGE_STANDARD': 'c++20' + } + }, + { 'target_name': 'binding_except_all', 'dependencies': ['../node_addon_api.gyp:node_addon_api_except_all'], 'sources': [ '>@(build_sources_except_all)']
diff --git a/test/concepts.cc b/test/concepts.cc new file mode 100644 index 0000000..29285bc --- /dev/null +++ b/test/concepts.cc
@@ -0,0 +1,147 @@ +#include "napi.h" + +using namespace Napi; + +#if defined(NAPI_HAS_CPP20_CONCEPTS) + +// Test CallbackCallable concept +static_assert(concepts::CallbackCallable<decltype([](const CallbackInfo& info) { + return info.Env().Undefined(); +})>); +static_assert(concepts::CallbackCallable< + decltype([](const CallbackInfo& /*info*/) -> void {})>); +static_assert(concepts::CallbackCallable<decltype([](const CallbackInfo& info) { + return String::New(info.Env(), "ok"); +})>); + +// Rejections: +// Non-copyable CallbackInfo passed by value +static_assert(!concepts::CallbackCallable<decltype([](CallbackInfo /*info*/) { + return Value(); +})>); +static_assert(!concepts::CallbackCallable<decltype([](int /*x*/) {})>); +static_assert(!concepts::CallbackCallable<decltype([]() {})>); +static_assert(!concepts::CallbackCallable< + decltype([](const CallbackInfo& /*info*/, int /*extra*/) {})>); +static_assert( + !concepts::CallbackCallable< + decltype([](const CallbackInfo& /*info*/) -> int { return 42; })>); +static_assert(!concepts::CallbackCallable<int>); +static_assert(!concepts::CallbackCallable<void*>); + +// Test GetterCallable concept +static_assert(concepts::GetterCallable<decltype([](const CallbackInfo& info) { + return info.Env().Undefined(); +})>); +static_assert(concepts::GetterCallable<decltype([](const CallbackInfo& info) { + return Number::New(info.Env(), 42); +})>); +static_assert(!concepts::GetterCallable< + decltype([](const CallbackInfo& /*info*/) -> void {})>); +static_assert(!concepts::GetterCallable<decltype([](int /*x*/) {})>); +static_assert(!concepts::GetterCallable<int>); + +// Test SetterCallable concept +static_assert( + concepts::SetterCallable<decltype([](const CallbackInfo& /*info*/) {})>); +static_assert(concepts::SetterCallable< + decltype([](const CallbackInfo& /*info*/) -> void {})>); +static_assert(!concepts::SetterCallable<decltype([](const CallbackInfo& info) { + return info.Env().Undefined(); +})>); +static_assert(!concepts::SetterCallable<decltype([](int /*x*/) {})>); +static_assert(!concepts::SetterCallable<int>); + +// Test Finalizer concept +static_assert(concepts::Finalizer<decltype([](Env /*env*/) {})>); +static_assert(!concepts::Finalizer<decltype([]() {})>); +static_assert(!concepts::Finalizer<decltype([](int /*x*/) {})>); +static_assert( + !concepts::Finalizer<decltype([](Env /*env*/, int* /*data*/) {})>); +static_assert(!concepts::Finalizer<int>); + +// Test FinalizerWithData concept +static_assert( + concepts::FinalizerWithData<decltype([](Env /*env*/, int* /*data*/) {}), + int>); +static_assert( + concepts::FinalizerWithData<decltype([](Env /*env*/, void* /*data*/) {}), + void>); +static_assert( + !concepts::FinalizerWithData<decltype([](Env /*env*/, char* /*data*/) {}), + int>); +static_assert(!concepts::FinalizerWithData<decltype([](Env /*env*/) {}), int>); +static_assert(!concepts::FinalizerWithData<decltype([](int /*x*/) {}), int>); + +// Test FinalizerWithHint concept +static_assert(concepts::FinalizerWithHint< + decltype([](Env /*env*/, int* /*data*/, char* /*hint*/) {}), + int, + char>); +static_assert( + !concepts::FinalizerWithHint<decltype([](Env /*env*/, int* /*data*/) {}), + int, + char>); +static_assert(!concepts::FinalizerWithHint< + decltype([](Env /*env*/, int* /*data*/, int* /*hint*/) {}), + int, + char>); + +#endif // defined(NAPI_HAS_CPP20_CONCEPTS) + +Object InitConcepts(Env env) { + Object exports = Object::New(env); + +#if defined(NAPI_HAS_CPP20_CONCEPTS) + exports["hasConcepts"] = Boolean::New(env, true); +#else + exports["hasConcepts"] = Boolean::New(env, false); +#endif + + exports["testFunction"] = Function::New(env, [](const CallbackInfo& info) { + return String::New(info.Env(), "concept callback ok"); + }); + + Object obj = Object::New(env); + obj.DefineProperty(PropertyDescriptor::Accessor( + env, + obj, + "testProp", + [](const CallbackInfo& info) { return Number::New(info.Env(), 42); }, + [](const CallbackInfo& /*info*/) {})); + exports["testAccessor"] = obj; + + Object fnObj = Object::New(env); + fnObj.DefineProperty(PropertyDescriptor::Function( + env, fnObj, "testMethod", [](const CallbackInfo& info) { + return Boolean::New(info.Env(), true); + })); + exports["testFunctionDescriptor"] = fnObj; + + static int finalizerData = 100; + Object finalizerObj = Object::New(env); + finalizerObj.AddFinalizer([](Env /*env*/, int* data) { *data = 200; }, + &finalizerData); + exports["testFinalizer"] = finalizerObj; + + static char abData[16] = {0}; + ArrayBuffer ab = ArrayBuffer::New( + env, abData, sizeof(abData), [](Env /*env*/, void* /*data*/) {}); + exports["testArrayBuffer"] = ab; + + static uint8_t bufData[8] = {0}; + Buffer<uint8_t> buf = Buffer<uint8_t>::New( + env, bufData, sizeof(bufData), [](Env /*env*/, uint8_t* /*data*/) {}); + exports["testBuffer"] = buf; + + return exports; +} + +#if defined(BUILDING_CPP20_MODULE) +static Object InitConceptsModule(Env env, Object exports) { + exports.Set("concepts", InitConcepts(env)); + return exports; +} + +NODE_API_MODULE(addon, InitConceptsModule) +#endif
diff --git a/test/concepts.js b/test/concepts.js new file mode 100644 index 0000000..68dc6c0 --- /dev/null +++ b/test/concepts.js
@@ -0,0 +1,51 @@ +'use strict'; + +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const common = require('./common'); + +module.exports = common.runTestWithBuildType(async (buildType) => { + await common.runTest(test, buildType); + + const cpp20BindingPath = path.resolve( + __dirname, + `../build/${buildType}/binding_cpp20.node` + ); + if (fs.existsSync(cpp20BindingPath)) { + await Promise.resolve( + test(require(cpp20BindingPath), { bindingPath: cpp20BindingPath }) + ).finally(common.mustCall()); + } +}); + +function test (binding, context) { + assert.ok(binding.concepts); + assert.strictEqual(typeof binding.concepts.hasConcepts, 'boolean'); + + if (context && context.bindingPath && context.bindingPath.includes('binding_cpp20')) { + assert.strictEqual(binding.concepts.hasConcepts, true); + } + + // Verify function created with concept-constrained CallbackCallable + assert.strictEqual(binding.concepts.testFunction(), 'concept callback ok'); + + // Verify property descriptor created with concept-constrained GetterCallable & SetterCallable + assert.strictEqual(binding.concepts.testAccessor.testProp, 42); + binding.concepts.testAccessor.testProp = 100; + assert.strictEqual(binding.concepts.testAccessor.testProp, 42); + + // Verify function descriptor created with concept-constrained CallbackCallable + assert.strictEqual(binding.concepts.testFunctionDescriptor.testMethod(), true); + + // Verify finalizer-attached object + assert.ok(binding.concepts.testFinalizer); + + // Verify array buffer with concept-constrained finalizer + assert.ok(binding.concepts.testArrayBuffer instanceof ArrayBuffer); + assert.strictEqual(binding.concepts.testArrayBuffer.byteLength, 16); + + // Verify buffer with concept-constrained finalizer + assert.ok(Buffer.isBuffer(binding.concepts.testBuffer)); + assert.strictEqual(binding.concepts.testBuffer.length, 8); +}