diff --git a/packages/nitrogen/src/views/CppHybridViewComponent.ts b/packages/nitrogen/src/views/CppHybridViewComponent.ts index d2095b64..20354812 100644 --- a/packages/nitrogen/src/views/CppHybridViewComponent.ts +++ b/packages/nitrogen/src/views/CppHybridViewComponent.ts @@ -70,10 +70,10 @@ ${createFileMetadataString(`${component}.hpp`)} #pragma once -#if REACT_NATIVE_VERSION >= 78 +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 #include -#include #include #include #include @@ -160,7 +160,7 @@ namespace ${namespace} { } // namespace ${namespace} #else - #warning "View Component '${HybridT}' will be unavailable in React Native, because it requires React Native 78 or higher." +#warning "View Component '${HybridT}' will be unavailable in React Native, because it requires React Native 78 or higher." #endif `.trim() @@ -192,9 +192,10 @@ ${name}([&]() -> CachedProp<${type}> { const componentCode = ` ${createFileMetadataString(`${component}.cpp`)} -#if REACT_NATIVE_VERSION >= 78 - #include "${component}.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #include #include #include diff --git a/packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts b/packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts index 11c62b2b..fbc96d21 100644 --- a/packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts +++ b/packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts @@ -109,7 +109,8 @@ ${createFileMetadataString(`J${stateUpdaterName}.hpp`)} #pragma once -#if REACT_NATIVE_VERSION >= 78 +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 #include #include @@ -152,9 +153,10 @@ if (props.${name}.isDirty) { const updaterJniCppCode = ` ${createFileMetadataString(`J${stateUpdaterName}.cpp`)} -#if REACT_NATIVE_VERSION >= 78 - #include "J${stateUpdaterName}.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #include "views/${component}.hpp" namespace ${cxxNamespace} { @@ -192,7 +194,7 @@ void J${stateUpdaterName}::updateViewProps(jni::alias_ref, space: 'user', language: 'c++', }, - ifGuard: `REACT_NATIVE_VERSION >= 78`, + ifGuard: `REACT_NATIVE_VERSION_MINOR >= 78`, }) return [ diff --git a/packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts b/packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts index 83f55e25..3efd917c 100644 --- a/packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts +++ b/packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts @@ -46,9 +46,10 @@ if (newViewProps.${name}.isDirty) { const mmFile = ` ${createFileMetadataString(`${component}.mm`)} -#if REACT_NATIVE_VERSION >= 78 - #import "${component}.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #import #import #import diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp index 0011713a..b2104a50 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp @@ -59,7 +59,7 @@ int initialize(JavaVM* vm) { margelo::nitro::image::JHybridBaseSpec::registerNatives(); margelo::nitro::image::JHybridChildSpec::registerNatives(); margelo::nitro::image::JHybridTestViewSpec::registerNatives(); - #if REACT_NATIVE_VERSION >= 78 + #if REACT_NATIVE_VERSION_MINOR >= 78 margelo::nitro::image::views::JHybridTestViewStateUpdater::registerNatives(); #endif diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.cpp b/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.cpp index 81e414a2..ef73cf55 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.cpp @@ -5,9 +5,10 @@ /// Copyright © 2025 Marc Rousavy @ Margelo /// -#if REACT_NATIVE_VERSION >= 78 - #include "JHybridTestViewStateUpdater.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #include "views/HybridTestViewComponent.hpp" namespace margelo::nitro::image::views { diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.hpp b/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.hpp index 34d8d325..444c63ee 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/c++/views/JHybridTestViewStateUpdater.hpp @@ -7,7 +7,8 @@ #pragma once -#if REACT_NATIVE_VERSION >= 78 +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 #include #include diff --git a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/views/HybridTestViewComponent.mm b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/views/HybridTestViewComponent.mm index 1f857f3d..9e8b08a1 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/ios/c++/views/HybridTestViewComponent.mm +++ b/packages/react-native-nitro-image/nitrogen/generated/ios/c++/views/HybridTestViewComponent.mm @@ -5,9 +5,10 @@ /// Copyright © 2025 Marc Rousavy @ Margelo /// -#if REACT_NATIVE_VERSION >= 78 - #import "HybridTestViewComponent.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #import #import #import diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp index cc037734..5edf6d43 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.cpp @@ -5,9 +5,10 @@ /// Copyright © 2025 Marc Rousavy @ Margelo /// -#if REACT_NATIVE_VERSION >= 78 - #include "HybridTestViewComponent.hpp" +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 + #include #include #include diff --git a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.hpp b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.hpp index e1cb5094..c6faca73 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/shared/c++/views/HybridTestViewComponent.hpp @@ -7,10 +7,10 @@ #pragma once -#if REACT_NATIVE_VERSION >= 78 +#include +#if REACT_NATIVE_VERSION_MINOR >= 78 #include -#include #include #include #include @@ -98,5 +98,5 @@ namespace margelo::nitro::image::views { } // namespace margelo::nitro::image::views #else - #warning "View Component 'HybridTestView' will be unavailable in React Native, because it requires React Native 78 or higher." +#warning "View Component 'HybridTestView' will be unavailable in React Native, because it requires React Native 78 or higher." #endif diff --git a/packages/react-native-nitro-modules/NitroModules.podspec b/packages/react-native-nitro-modules/NitroModules.podspec index 1b3ea144..137a0e69 100644 --- a/packages/react-native-nitro-modules/NitroModules.podspec +++ b/packages/react-native-nitro-modules/NitroModules.podspec @@ -1,5 +1,4 @@ require "json" -require_relative './nitro_pod_utils' package = JSON.parse(File.read(File.join(__dir__, "package.json"))) @@ -55,12 +54,6 @@ Pod::Spec.new do |s| "ios/utils/SwiftClosure.hpp", ] - compiler_flags = "$(inherited) FOLLY_NO_CONFIG FOLLY_CFG_NO_COROUTINES" - if has_react_native() - react_native_version = get_react_native_version() - compiler_flags += " HAS_REACT_NATIVE REACT_NATIVE_VERSION=#{react_native_version}" - end - s.pod_target_xcconfig = { # Use C++ 20 "CLANG_CXX_LANGUAGE_STANDARD" => "c++20", @@ -69,16 +62,12 @@ Pod::Spec.new do |s| # Enables stricter modular headers "DEFINES_MODULE" => "YES", # C++ compiler flags, mainly for RN version and folly. - "GCC_PREPROCESSOR_DEFINITIONS" => compiler_flags + "GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) FOLLY_NO_CONFIG FOLLY_CFG_NO_COROUTINES" } - if has_react_native() - # Using Nitro in react-native - s.dependency 'React-jsi' - s.dependency 'React-callinvoker' - install_modules_dependencies(s) - else - # Using Nitro somewhere else (NativeScript? Bare iOS?) - raise "Couldn't find react-native - are you trying to use Nitro outside of React Native?" - end + # Nitro depends on JSI. + s.dependency 'React-jsi' + # For React Native, we implement nitro::Dispatcher using react::CallInvoker + s.dependency 'React-callinvoker' + install_modules_dependencies(s) end diff --git a/packages/react-native-nitro-modules/android/CMakeLists.txt b/packages/react-native-nitro-modules/android/CMakeLists.txt index e60d9f72..4311db93 100644 --- a/packages/react-native-nitro-modules/android/CMakeLists.txt +++ b/packages/react-native-nitro-modules/android/CMakeLists.txt @@ -18,12 +18,6 @@ add_library(NitroModules SHARED ${android_files} ) -# Add C++ flags -if (HAS_REACT_NATIVE) - message(STATUS "Building with React Native v${REACT_NATIVE_VERSION}...") - add_compile_definitions(HAS_REACT_NATIVE REACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}) -endif () - # Specifies a path to native header files. include_directories( # Shared C++ includes diff --git a/packages/react-native-nitro-modules/android/build.gradle b/packages/react-native-nitro-modules/android/build.gradle index edba5e2d..9ee40088 100644 --- a/packages/react-native-nitro-modules/android/build.gradle +++ b/packages/react-native-nitro-modules/android/build.gradle @@ -11,28 +11,6 @@ buildscript { } } -def getReactNativePackagePath() { - return file(["node", "--print", "require.resolve('react-native/package.json')"].execute(null, rootDir).text.trim()) -} - -def hasReactNative() { - def path = getReactNativePackagePath() - return path.exists() -} - -def getReactNativeVersion() { - logger.info("[NitroModules] Finding react-native package...") - def reactNativePackage = getReactNativePackagePath() - if (!reactNativePackage.exists()) { - throw new GradleException("[NitroModules] Couldn't find react-native path!") - } - def json = new JsonSlurper().parse(reactNativePackage) - def version = json.version - def minorVersion = version.tokenize('.')[1].toInteger() - logger.info("[NitroModules] Found react-native ${json.version} (${minorVersion}) at ${reactNativePackage}!") - return minorVersion -} - def reactNativeArchitectures() { def value = rootProject.getProperties().get("reactNativeArchitectures") return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"] @@ -73,12 +51,7 @@ android { externalNativeBuild { cmake { cppFlags "-frtti -fexceptions -Wall -Wextra -fstack-protector-all" - if (hasReactNative()) { - def reactNativeVersion = getReactNativeVersion() - arguments "-DANDROID_STL=c++_shared", "-DHAS_REACT_NATIVE=YES", "-DREACT_NATIVE_VERSION=${reactNativeVersion}" - } else { - arguments "-DANDROID_STL=c++_shared" - } + arguments "-DANDROID_STL=c++_shared" abiFilters (*reactNativeArchitectures()) buildTypes { diff --git a/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp b/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp index d1a94748..b2b55772 100644 --- a/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp @@ -31,6 +31,7 @@ #define _CXX_INTEROP_HAS_ATTRIBUTE(x) 0 #endif +// Closed/Final Enums #if _CXX_INTEROP_HAS_ATTRIBUTE(enum_extensibility) // Enum is marked as closed/not extensible #define CLOSED_ENUM __attribute__((enum_extensibility(closed))) @@ -38,6 +39,7 @@ #define CLOSED_ENUM #endif +// Swift Support #if __has_include() // Swift's bridging header defines those things #include @@ -50,4 +52,14 @@ #define SWIFT_NONCOPYABLE #endif +// React Native Support +#if __has_include() +#include +#endif +#ifndef REACT_NATIVE_VERSION_MINOR +#define REACT_NATIVE_VERSION_MAJOR 0 +#define REACT_NATIVE_VERSION_MINOR 0 +#define REACT_NATIVE_VERSION_PATCH 0 +#endif + #endif /* NitroDefines_h */ diff --git a/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp b/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp index e1caa908..370d4dee 100644 --- a/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp +++ b/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp @@ -33,9 +33,9 @@ struct CachedProp { // jsi::Value hasn't changed - no need to convert it again! return oldProp; } - auto converted = JSIConverter::fromJSI(runtime, value); - auto cache = JSICache::getOrCreateCache(runtime); - auto cached = cache.makeShared(jsi::Value(runtime, value)); + T converted = JSIConverter::fromJSI(runtime, value); + JSICacheReference cache = JSICache::getOrCreateCache(runtime); + OwningReference cached = cache.makeShared(jsi::Value(runtime, value)); return CachedProp(std::move(converted), std::move(cached), /* isDirty */ true); } }; diff --git a/packages/react-native-nitro-modules/nitro_pod_utils.rb b/packages/react-native-nitro-modules/nitro_pod_utils.rb deleted file mode 100644 index 538bd84b..00000000 --- a/packages/react-native-nitro-modules/nitro_pod_utils.rb +++ /dev/null @@ -1,31 +0,0 @@ -require "json" - -# Gets the path of the react-native/package.json file. -def get_react_native_package_path() - pod_root = Pod::Config.instance.installation_root.to_s - return `cd "#{pod_root}" && node --print "require.resolve('react-native/package.json')"`.strip! -end - -# Finds out whether react-native is available, or not. -# This works by checking if the react-native node package can be resolved. -def has_react_native() - begin - react_native_package_path = get_react_native_package_path() - return File.exist?(react_native_package_path) - rescue - return false - end -end - -# Gets the minor react-native version (e.g 76 for 0.76.4) -def get_react_native_version() - react_native_package_path = get_react_native_package_path() - if !File.exist?(react_native_package_path) - raise "[NitroModules] Couldn't find react-native path! File '#{react_native_package_path}' doesn't exist!" - end - react_native_package = JSON.parse(File.read(react_native_package_path)) - react_native_version = react_native_package['version'] - react_native_minor_version = react_native_version.split('.')[1].to_i - Pod::UI.puts "[NitroModules] 🔥 Found react-native #{react_native_version} (#{react_native_minor_version}) in #{File.dirname(react_native_package_path)}!" - return react_native_minor_version -end