Skip to content

Commit

Permalink
feat: Move to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Aug 2, 2024
1 parent a522f09 commit 87eae31
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 58 deletions.
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- NitroModules (0.0.6):
- NitroModules (0.0.7):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1397,7 +1397,7 @@ SPEC CHECKSUMS:
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
hermes-engine: 1f547997900dd0752dc0cc0ae6dd16173c49e09b
NitroImage: d298d1e42faaea39e6abbced44de2b1ba08819c6
NitroModules: f2dc5c454f755e5bf7237f408a891c901f020290
NitroModules: 109286992ea4be2022fea1020e9b09091e2cddaf
RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47
RCTDeprecation: 4c7eeb42be0b2e95195563c49be08d0b839d22b4
RCTRequired: d530a0f489699c8500e944fde963102c42dcd0c2
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-modules/NitroModules.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Pod::Spec.new do |s|
]
s.public_header_files = [
# Public C++ headers will be exposed in modulemap (for Swift)
"cpp/core/ArrayBuffer.hpp",
"cpp/core/HybridObject.hpp",
"cpp/core/HybridContext.hpp",
"cpp/registry/HybridObjectRegistry.hpp",
"cpp/jsi/JSIConverter.hpp",
"cpp/jsi/ArrayBuffer.hpp",
"cpp/threading/Dispatcher.hpp",
"cpp/utils/NitroHash.hpp",
"cpp/utils/NitroDefines.hpp",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-nitro-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ The following C++ / JS types are supported out of the box:
</tr>
<tr>
<td><code>ArrayBuffer</code></td>
<td><code>std::shared_ptr&lt;<a href="./cpp/jsi/ArrayBuffer.hpp">ArrayBuffer</a>&gt;</code></td>
<td><code>std::shared_ptr&lt;<a href="./cpp/core/ArrayBuffer.hpp">ArrayBuffer</a>&gt;</code></td>
</tr>
<tr>
<td><code><a href="./src/HybridObject.ts">HybridObject</a></code></td>
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-nitro-modules/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ include_directories(
../cpp/platform
../cpp/registry
../cpp/test-object
../cpp/templates
../cpp/threading
../cpp/turbomodule
../cpp/utils
Expand Down
35 changes: 7 additions & 28 deletions packages/react-native-nitro-modules/cpp/jsi/JSIConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class HybridObject;
#include "NitroHash.hpp"
#include "Promise.hpp"
#include "TypeInfo.hpp"
#include "FutureType.hpp"
#include "IsInPack.hpp"
#include "IsHostObject.hpp"
#include "IsNativeState.hpp"
#include <array>
#include <future>
#include <jsi/jsi.h>
Expand Down Expand Up @@ -222,22 +226,11 @@ struct JSIConverter<std::future<TResult>> {
}
};

template <typename T>
struct future_traits {
using type = void;
};
template <typename T>
struct future_traits<std::future<T>> {
using type = T;
};
template <typename T>
using future_traits_t = typename future_traits<std::remove_reference_t<T>>::type;

// [](Args...) -> T {} <> (Args...) => T
template <typename ReturnType, typename... Args>
struct JSIConverter<std::function<ReturnType(Args...)>> {
// std::future<T> -> T
using ResultingType = future_traits_t<ReturnType>;
using ResultingType = future_type_v<ReturnType>;

static inline ResultingType callJSFunction(jsi::Runtime& runtime, const OwningReference<jsi::Function>& function, const Args&... args) {
// Throw a lock on the OwningReference<T> so we can guarantee safe access (Hermes GC cannot delete it while `lock` is alive)
Expand Down Expand Up @@ -414,12 +407,6 @@ struct JSIConverter<std::tuple<Types...>> {
}
};

// Helper struct to check if a type is present in a parameter pack
template <typename T, typename... Types>
struct is_in_pack : std::disjunction<std::is_same<T, Types>...> {};
template <typename T, typename... Types>
inline constexpr bool is_in_pack_v = is_in_pack<T, Types...>::value;

// std::variant<A, B, C> <> A | B | C
template <typename... Types>
struct JSIConverter<std::variant<Types...>> {
Expand Down Expand Up @@ -541,11 +528,7 @@ struct JSIConverter<std::shared_ptr<jsi::MutableBuffer>> {

// HybridObject <> {}
template <typename T>
struct is_shared_ptr_to_host_object : std::false_type {};
template <typename T>
struct is_shared_ptr_to_host_object<std::shared_ptr<T>> : std::is_base_of<jsi::HostObject, T> {};
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object<T>::value>> {
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object_v<T>>> {
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
Expand Down Expand Up @@ -593,11 +576,7 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_host_object<T>::value>>

// NativeState <> {}
template <typename T>
struct is_shared_ptr_to_native_state : std::false_type {};
template <typename T>
struct is_shared_ptr_to_native_state<std::shared_ptr<T>> : std::is_base_of<jsi::NativeState, T> {};
template <typename T>
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_native_state<T>::value>> {
struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_native_state<T>>> {
using TPointee = typename T::element_type;

static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
Expand Down
28 changes: 28 additions & 0 deletions packages/react-native-nitro-modules/cpp/templates/FutureType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// BorrowingReference.hpp
// NitroModules
//
// Created by Marc Rousavy on 21.06.24.
//

#pragma once

#include <future>
#include <type_traits>

namespace margelo::nitro {

// Gets the `T` in `std::future<T>`.
template <typename T>
struct future_type {
using type = void;
};
template <typename T>
struct future_type<std::future<T>> {
using type = T;
};

template <typename T>
using future_type_v = typename future_type<std::remove_reference_t<T>>::type;

} // namespace margelo::nitro
27 changes: 27 additions & 0 deletions packages/react-native-nitro-modules/cpp/templates/IsHostObject.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// BorrowingReference.hpp
// NitroModules
//
// Created by Marc Rousavy on 21.06.24.
//

#pragma once

#include <jsi/jsi.h>
#include <type_traits>

namespace margelo::nitro {

using namespace facebook;

// Returns whether the given type `T` is a `shared_ptr` to a `HostObject`
template <typename T>
struct is_shared_ptr_to_host_object : std::false_type {};

template <typename T>
struct is_shared_ptr_to_host_object<std::shared_ptr<T>> : std::is_base_of<jsi::HostObject, T> {};

template <typename T>
using is_shared_ptr_to_host_object_v = typename is_shared_ptr_to_host_object<std::remove_reference_t<T>>::value;

} // namespace margelo::nitro
22 changes: 22 additions & 0 deletions packages/react-native-nitro-modules/cpp/templates/IsInPack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// BorrowingReference.hpp
// NitroModules
//
// Created by Marc Rousavy on 21.06.24.
//

#pragma once

#include <future>
#include <type_traits>

namespace margelo::nitro {

// Returns whether the given type `T` is inside `Types...`
template <typename T, typename... Types>
struct is_in_pack : std::disjunction<std::is_same<T, Types>...> {};

template <typename T, typename... Types>
inline constexpr bool is_in_pack_v = is_in_pack<T, Types...>::value;

} // namespace margelo::nitro
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// BorrowingReference.hpp
// NitroModules
//
// Created by Marc Rousavy on 21.06.24.
//

#pragma once

#include <jsi/jsi.h>
#include <type_traits>

namespace margelo::nitro {

using namespace facebook;

// Returns whether the given type `T` is a `shared_ptr` to a `NativeStaet`
template <typename T>
struct is_shared_ptr_to_native_state : std::false_type {};

template <typename T>
struct is_shared_ptr_to_native_state<std::shared_ptr<T>> : std::is_base_of<jsi::NativeState, T> {};

template <typename T>
using is_shared_ptr_to_native_state_v = typename is_shared_ptr_to_native_state<std::remove_reference_t<T>>::value;

} // namespace margelo::nitro
26 changes: 0 additions & 26 deletions packages/react-native-nitro-modules/cpp/utils/DoesClassExist.hpp

This file was deleted.

0 comments on commit 87eae31

Please sign in to comment.