Skip to content

Commit

Permalink
Allow passing const pointers to js
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 12, 2024
1 parent e5759e8 commit 35c91ec
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/instance_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace internal {
// Get the base name of a type.
template<typename T, typename Enable = void>
struct TopClass {
static constexpr const char* name = Type<T>::name;
static constexpr const char* name = Type<std::remove_cv_t<T>>::name;
};

template<typename T>
Expand Down
17 changes: 15 additions & 2 deletions src/prototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ struct Type<Class<T>> {

// Default converter for pointers.
template<typename T>
struct Type<T*, typename std::enable_if<std::is_class<T>::value &&
std::is_class<Type<T>>::value>::type> {
struct Type<T*, std::enable_if_t<!std::is_const_v<T> &&
std::is_class_v<T> &&
std::is_class_v<Type<T>>>> {
static constexpr const char* name = Type<T>::name;
static inline napi_status ToNode(napi_env env, T* ptr, napi_value* result) {
static_assert(internal::HasWrap<T>::value &&
Expand Down Expand Up @@ -89,6 +90,18 @@ struct Type<T*, typename std::enable_if<std::is_class<T>::value &&
}
};

// Default converter for const pointers.
template<typename T>
struct Type<T*, std::enable_if_t<std::is_const_v<T> &&
std::is_class_v<T> &&
std::is_class_v<Type<T>>>> {
static constexpr const char* name = Type<std::decay_t<T>>::name;
static inline napi_status ToNode(napi_env env, T* ptr, napi_value* result) {
return ki::Type<std::decay_t<T>*>::ToNode(
env, const_cast<std::decay_t<T>*>(ptr), result);
}
};

} // namespace ki

#endif // SRC_PROTOTYPE_H_
12 changes: 7 additions & 5 deletions src/prototype_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,28 @@ template<typename, typename = void>
struct HasWrap : std::false_type {};

template<typename T>
struct HasWrap<T, void_t<decltype(TypeBridge<T>::Wrap)>> : std::true_type {};
struct HasWrap<T, void_t<decltype(TypeBridge<std::decay_t<T>>::Wrap)>>
: std::true_type {};

template<typename, typename = void>
struct HasUnwrap : std::false_type {};

template<typename T>
struct HasUnwrap<T, void_t<decltype(TypeBridge<T>::Unwrap)>>
struct HasUnwrap<T, void_t<decltype(TypeBridge<std::decay_t<T>>::Unwrap)>>
: std::true_type {};

template<typename, typename = void>
struct HasFinalize : std::false_type {};

template<typename T>
struct HasFinalize<T, void_t<decltype(TypeBridge<T>::Finalize)>>
struct HasFinalize<T, void_t<decltype(TypeBridge<std::decay_t<T>>::Finalize)>>
: std::true_type {};

template<typename, typename = void>
struct HasDestructor : std::false_type {};

template<typename T>
struct HasDestructor<T, void_t<decltype(Type<T>::Destructor)>>
struct HasDestructor<T, void_t<decltype(Type<std::decay_t<T>>::Destructor)>>
: std::true_type {};

// Whether the JS object can be cached using the pointer as key.
Expand All @@ -70,7 +71,8 @@ template<typename, typename = void>
struct CanCachePointer : std::true_type {};

template<typename T>
struct CanCachePointer<T, void_t<decltype(TypeBridge<T>::can_cache_pointer)>> {
struct CanCachePointer<
T, void_t<decltype(TypeBridge<std::decay_t<T>>::can_cache_pointer)>> {
static constexpr bool value = TypeBridge<T>::can_cache_pointer;
};

Expand Down

0 comments on commit 35c91ec

Please sign in to comment.