Skip to content

Commit

Permalink
chore: Import react-native version from ReactNativeVersion.h (#505)
Browse files Browse the repository at this point in the history
* feat: Import react-native version from `ReactNativeVersion.h`

* fix: Use NitroDefines import

* chore: Use actual types
  • Loading branch information
mrousavy authored Jan 21, 2025
1 parent 24567eb commit 35a4efb
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 107 deletions.
11 changes: 6 additions & 5 deletions packages/nitrogen/src/views/CppHybridViewComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ ${createFileMetadataString(`${component}.hpp`)}
#pragma once
#if REACT_NATIVE_VERSION >= 78
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78
#include <optional>
#include <NitroModules/NitroDefines.hpp>
#include <NitroModules/NitroHash.hpp>
#include <NitroModules/CachedProp.hpp>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -192,9 +192,10 @@ ${name}([&]() -> CachedProp<${type}> {
const componentCode = `
${createFileMetadataString(`${component}.cpp`)}
#if REACT_NATIVE_VERSION >= 78
#include "${component}.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78
#include <string>
#include <exception>
#include <utility>
Expand Down
10 changes: 6 additions & 4 deletions packages/nitrogen/src/views/kotlin/KotlinHybridViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ ${createFileMetadataString(`J${stateUpdaterName}.hpp`)}
#pragma once
#if REACT_NATIVE_VERSION >= 78
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78
#include <fbjni/fbjni.h>
#include <react/fabric/StateWrapperImpl.h>
Expand Down Expand Up @@ -152,9 +153,10 @@ if (props.${name}.isDirty) {
const updaterJniCppCode = `
${createFileMetadataString(`J${stateUpdaterName}.cpp`)}
#if REACT_NATIVE_VERSION >= 78
#include "J${stateUpdaterName}.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78
#include "views/${component}.hpp"
namespace ${cxxNamespace} {
Expand Down Expand Up @@ -192,7 +194,7 @@ void J${stateUpdaterName}::updateViewProps(jni::alias_ref<jni::JClass>,
space: 'user',
language: 'c++',
},
ifGuard: `REACT_NATIVE_VERSION >= 78`,
ifGuard: `REACT_NATIVE_VERSION_MINOR >= 78`,
})

return [
Expand Down
5 changes: 3 additions & 2 deletions packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ if (newViewProps.${name}.isDirty) {
const mmFile = `
${createFileMetadataString(`${component}.mm`)}
#if REACT_NATIVE_VERSION >= 78
#import "${component}.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78
#import <memory>
#import <react/renderer/componentregistry/ComponentDescriptorProvider.h>
#import <React/RCTViewComponentView.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#if REACT_NATIVE_VERSION >= 78

#include "JHybridTestViewStateUpdater.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78

#include "views/HybridTestViewComponent.hpp"

namespace margelo::nitro::image::views {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

#pragma once

#if REACT_NATIVE_VERSION >= 78
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78

#include <fbjni/fbjni.h>
#include <react/fabric/StateWrapperImpl.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#if REACT_NATIVE_VERSION >= 78

#import "HybridTestViewComponent.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78

#import <memory>
#import <react/renderer/componentregistry/ComponentDescriptorProvider.h>
#import <React/RCTViewComponentView.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
/// Copyright © 2025 Marc Rousavy @ Margelo
///

#if REACT_NATIVE_VERSION >= 78

#include "HybridTestViewComponent.hpp"
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78

#include <string>
#include <exception>
#include <utility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#pragma once

#if REACT_NATIVE_VERSION >= 78
#include <NitroModules/NitroDefines.hpp>
#if REACT_NATIVE_VERSION_MINOR >= 78

#include <optional>
#include <NitroModules/NitroDefines.hpp>
#include <NitroModules/NitroHash.hpp>
#include <NitroModules/CachedProp.hpp>
#include <react/renderer/core/ConcreteComponentDescriptor.h>
Expand Down Expand Up @@ -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
23 changes: 6 additions & 17 deletions packages/react-native-nitro-modules/NitroModules.podspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "json"
require_relative './nitro_pod_utils'

package = JSON.parse(File.read(File.join(__dir__, "package.json")))

Expand Down Expand Up @@ -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",
Expand All @@ -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
6 changes: 0 additions & 6 deletions packages/react-native-nitro-modules/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 1 addition & 28 deletions packages/react-native-nitro-modules/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#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)))
#else
#define CLOSED_ENUM
#endif

// Swift Support
#if __has_include(<swift/bridging>)
// Swift's bridging header defines those things
#include <swift/bridging>
Expand All @@ -50,4 +52,14 @@
#define SWIFT_NONCOPYABLE
#endif

// React Native Support
#if __has_include(<cxxreact/ReactNativeVersion.h>)
#include <cxxreact/ReactNativeVersion.h>
#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 */
6 changes: 3 additions & 3 deletions packages/react-native-nitro-modules/cpp/views/CachedProp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ struct CachedProp {
// jsi::Value hasn't changed - no need to convert it again!
return oldProp;
}
auto converted = JSIConverter<T>::fromJSI(runtime, value);
auto cache = JSICache::getOrCreateCache(runtime);
auto cached = cache.makeShared(jsi::Value(runtime, value));
T converted = JSIConverter<T>::fromJSI(runtime, value);
JSICacheReference cache = JSICache::getOrCreateCache(runtime);
OwningReference<jsi::Value> cached = cache.makeShared(jsi::Value(runtime, value));
return CachedProp<T>(std::move(converted), std::move(cached), /* isDirty */ true);
}
};
Expand Down
31 changes: 0 additions & 31 deletions packages/react-native-nitro-modules/nitro_pod_utils.rb

This file was deleted.

0 comments on commit 35a4efb

Please sign in to comment.