From 097c5690af7e3a7baae824113b621b5d614908ff Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 16 Sep 2024 15:58:58 +0200 Subject: [PATCH 1/4] fix: Use `NITRO_DEBUG` flag to predictably run debug/release code --- .../android/createHybridObjectInitializer.ts | 1 + .../syntax/kotlin/KotlinHybridObjectRegistration.ts | 2 +- .../nitrogen/generated/android/NitroImageOnLoad.cpp | 8 ++++---- .../android/src/main/cpp/platform/NitroLogger.cpp | 3 ++- .../src/main/cpp/registry/JHybridObjectRegistry.cpp | 3 ++- .../cpp/core/HybridFunction.hpp | 9 +++++---- .../cpp/core/HybridObject.cpp | 3 ++- .../react-native-nitro-modules/cpp/jsi/JSICache.cpp | 3 ++- .../cpp/jsi/JSIConverter+ArrayBuffer.hpp | 12 ++++++++++-- .../cpp/jsi/JSIConverter+HybridObject.hpp | 5 +++-- .../cpp/jsi/JSIHelpers.hpp | 2 +- .../cpp/platform/NitroLogger.hpp | 3 ++- .../cpp/prototype/HybridObjectPrototype.cpp | 3 ++- .../cpp/registry/HybridObjectRegistry.cpp | 5 +++-- .../cpp/threading/Dispatcher.cpp | 5 +++-- .../cpp/turbomodule/NativeNitroModules.cpp | 5 +++-- .../cpp/utils/NitroDefines.hpp | 10 +++++++++- .../cpp/utils/OwningLock.hpp | 2 +- .../cpp/utils/TypeInfo.hpp | 2 +- .../ios/core/PromiseHolder.hpp | 2 +- .../ios/platform/NitroLogger.mm | 3 ++- 21 files changed, 60 insertions(+), 31 deletions(-) diff --git a/packages/nitrogen/src/autolinking/android/createHybridObjectInitializer.ts b/packages/nitrogen/src/autolinking/android/createHybridObjectInitializer.ts index de24f1d21..cb906e6da 100644 --- a/packages/nitrogen/src/autolinking/android/createHybridObjectInitializer.ts +++ b/packages/nitrogen/src/autolinking/android/createHybridObjectInitializer.ts @@ -56,6 +56,7 @@ export function createHybridObjectIntializer(): SourceFile[] { ${createFileMetadataString(`${autolinkingClassName}.hpp`)} #include +#include namespace ${cxxNamespace} { diff --git a/packages/nitrogen/src/syntax/kotlin/KotlinHybridObjectRegistration.ts b/packages/nitrogen/src/syntax/kotlin/KotlinHybridObjectRegistration.ts index b9128e919..08f9559f1 100644 --- a/packages/nitrogen/src/syntax/kotlin/KotlinHybridObjectRegistration.ts +++ b/packages/nitrogen/src/syntax/kotlin/KotlinHybridObjectRegistration.ts @@ -42,7 +42,7 @@ HybridObjectRegistry::registerHybridObjectConstructor( static auto defaultConstructor = javaClass->getConstructor<${JHybridTSpec}::javaobject()>(); auto instance = javaClass->newObject(defaultConstructor); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { throw std::runtime_error("Failed to create an instance of \\"${JHybridTSpec}\\" - the constructor returned null!"); } 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 e64c6bdfd..aafb69726 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp @@ -43,9 +43,9 @@ int initialize(JavaVM* vm) { []() -> std::shared_ptr { static auto javaClass = jni::findClassStatic("com/margelo/nitro/image/ImageFactory"); static auto defaultConstructor = javaClass->getConstructor(); - + auto instance = javaClass->newObject(defaultConstructor); - #ifndef NDEBUG + #ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { throw std::runtime_error("Failed to create an instance of \"JHybridImageFactorySpec\" - the constructor returned null!"); } @@ -68,9 +68,9 @@ int initialize(JavaVM* vm) { []() -> std::shared_ptr { static auto javaClass = jni::findClassStatic("com/margelo/nitro/image/HybridTestObjectKotlin"); static auto defaultConstructor = javaClass->getConstructor(); - + auto instance = javaClass->newObject(defaultConstructor); - #ifndef NDEBUG + #ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { throw std::runtime_error("Failed to create an instance of \"JHybridTestObjectSwiftKotlinSpec\" - the constructor returned null!"); } diff --git a/packages/react-native-nitro-modules/android/src/main/cpp/platform/NitroLogger.cpp b/packages/react-native-nitro-modules/android/src/main/cpp/platform/NitroLogger.cpp index 642fff909..17a68f910 100644 --- a/packages/react-native-nitro-modules/android/src/main/cpp/platform/NitroLogger.cpp +++ b/packages/react-native-nitro-modules/android/src/main/cpp/platform/NitroLogger.cpp @@ -6,6 +6,7 @@ // #include "NitroLogger.hpp" +#include "NitroDefines.hpp" #include namespace margelo::nitro { @@ -26,7 +27,7 @@ int levelToAndroidLevel(LogLevel level) { } void Logger::nativeLog(LogLevel level, const char* tag, const std::string& message) { -#ifndef NDEBUG +#ifdef NITRO_DEBUG int logLevel = levelToAndroidLevel(level); std::string combinedTag = "Nitro." + std::string(tag); __android_log_print(logLevel, combinedTag.c_str(), "%s", message.c_str()); diff --git a/packages/react-native-nitro-modules/android/src/main/cpp/registry/JHybridObjectRegistry.cpp b/packages/react-native-nitro-modules/android/src/main/cpp/registry/JHybridObjectRegistry.cpp index ac5611d7b..e034207b0 100644 --- a/packages/react-native-nitro-modules/android/src/main/cpp/registry/JHybridObjectRegistry.cpp +++ b/packages/react-native-nitro-modules/android/src/main/cpp/registry/JHybridObjectRegistry.cpp @@ -8,6 +8,7 @@ #include "JHybridObjectRegistry.hpp" #include "HybridObjectRegistry.hpp" #include "JNISharedPtr.hpp" +#include "NitroDefines.hpp" namespace margelo::nitro { @@ -17,7 +18,7 @@ void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref std::shared_ptr { // 1. Call the Java initializer function jni::local_ref hybridObject = sharedInitializer->call(); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (hybridObject == nullptr) [[unlikely]] { throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - the constructor returned null!"); } diff --git a/packages/react-native-nitro-modules/cpp/core/HybridFunction.hpp b/packages/react-native-nitro-modules/cpp/core/HybridFunction.hpp index 0d91aabcf..7a35097bf 100644 --- a/packages/react-native-nitro-modules/cpp/core/HybridFunction.hpp +++ b/packages/react-native-nitro-modules/cpp/core/HybridFunction.hpp @@ -14,6 +14,7 @@ struct JSIConverter; #include "CountTrailingOptionals.hpp" #include "JSIConverter.hpp" +#include "NitroDefines.hpp" #include "TypeInfo.hpp" #include #include @@ -170,7 +171,7 @@ class HybridFunction final { static inline std::shared_ptr getHybridObjectNativeState(jsi::Runtime& runtime, const jsi::Value& value, FunctionKind funcKind, const std::string& funcName) { // 1. Convert jsi::Value to jsi::Object -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (!value.isObject()) [[unlikely]] { throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo(funcKind, funcName) + " - `this` is not bound!"); } @@ -178,7 +179,7 @@ class HybridFunction final { jsi::Object object = value.getObject(runtime); // 2. Check if it even has any kind of `NativeState` -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (!object.hasNativeState(runtime)) [[unlikely]] { throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo(funcKind, funcName) + " - `this` does not have a NativeState! Suggestions:\n" @@ -191,7 +192,7 @@ class HybridFunction final { // 3. Get `NativeState` from the jsi::Object and check if it is non-null std::shared_ptr nativeState = object.getNativeState(runtime); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (nativeState == nullptr) [[unlikely]] { throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo(funcKind, funcName) + " - `this`'s `NativeState` is `nullptr`, " @@ -201,7 +202,7 @@ class HybridFunction final { // 4. Try casting it to our desired target type. std::shared_ptr hybridInstance = std::dynamic_pointer_cast(nativeState); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (hybridInstance == nullptr) [[unlikely]] { throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo(funcKind, funcName) + " - `this` has a NativeState, but it's the wrong type!"); diff --git a/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp b/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp index 3996c6203..86d344612 100644 --- a/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp +++ b/packages/react-native-nitro-modules/cpp/core/HybridObject.cpp @@ -4,6 +4,7 @@ #include "HybridObject.hpp" #include "JSIConverter.hpp" +#include "NitroDefines.hpp" namespace margelo::nitro { @@ -70,7 +71,7 @@ jsi::Value HybridObject::toObject(jsi::Runtime& runtime) { // 6. Set memory size so Hermes GC knows about actual memory object.setExternalMemoryPressure(runtime, getExternalMemorySize()); -#ifndef NDEBUG +#ifdef NITRO_DEBUG // 7. Assign a private __type property for debugging - this will be used so users know it's not just an empty object. object.setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, "NativeState<" + std::string(_name) + ">")); #endif diff --git a/packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp b/packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp index 11eb614f9..45958d06d 100644 --- a/packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp +++ b/packages/react-native-nitro-modules/cpp/jsi/JSICache.cpp @@ -7,6 +7,7 @@ #include "JSICache.hpp" #include "JSIHelpers.hpp" +#include "NitroDefines.hpp" namespace margelo::nitro { @@ -46,7 +47,7 @@ JSICacheReference JSICache::getOrCreateCache(jsi::Runtime& runtime) { Logger::log(LogLevel::Warning, TAG, "JSICache was created, but it is no longer strong!"); } -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (runtime.global().hasProperty(runtime, CACHE_PROP_NAME)) [[unlikely]] { throw std::runtime_error("The Runtime \"" + getRuntimeId(runtime) + "\" already has a global cache! (\"" + CACHE_PROP_NAME + "\")"); } diff --git a/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+ArrayBuffer.hpp b/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+ArrayBuffer.hpp index 473015a5e..5fbad2b43 100644 --- a/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+ArrayBuffer.hpp +++ b/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+ArrayBuffer.hpp @@ -18,6 +18,7 @@ struct JSIConverter; #include "ArrayBuffer.hpp" #include "IsSharedPtrTo.hpp" #include "JSICache.hpp" +#include "NitroDefines.hpp" #include #include #include @@ -30,9 +31,16 @@ using namespace facebook; template struct JSIConverter>> final { static inline std::shared_ptr fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { - jsi::Object object = arg.asObject(runtime); +#ifdef NITRO_DEBUG + if (!arg.isObject()) [[unlikely]] { + throw std::runtime_error("Value \"" + arg.toString(runtime).utf8(runtime) + + "\" is not an ArrayBuffer - " + "in fact, it's not even an object!"); + } +#endif -#ifndef NDEBUG + jsi::Object object = arg.asObject(runtime); +#ifdef NITRO_DEBUG if (!object.isArrayBuffer(runtime)) [[unlikely]] { throw std::runtime_error("Object \"" + arg.toString(runtime).utf8(runtime) + "\" is not an ArrayBuffer! " diff --git a/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+HybridObject.hpp b/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+HybridObject.hpp index 5400cad32..aae39c903 100644 --- a/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+HybridObject.hpp +++ b/packages/react-native-nitro-modules/cpp/jsi/JSIConverter+HybridObject.hpp @@ -10,6 +10,7 @@ class HybridObject; } // namespace margelo::nitro #include "IsSharedPtrTo.hpp" +#include "NitroDefines.hpp" #include "TypeInfo.hpp" #include #include @@ -24,7 +25,7 @@ struct JSIConverter> using TPointee = typename T::element_type; static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (!arg.isObject()) [[unlikely]] { if (arg.isUndefined()) [[unlikely]] { throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!")); @@ -36,7 +37,7 @@ struct JSIConverter> #endif jsi::Object object = arg.asObject(runtime); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (!object.hasNativeState(runtime)) [[unlikely]] { if (!object.hasNativeState(runtime)) [[unlikely]] { std::string stringRepresentation = arg.toString(runtime).utf8(runtime); diff --git a/packages/react-native-nitro-modules/cpp/jsi/JSIHelpers.hpp b/packages/react-native-nitro-modules/cpp/jsi/JSIHelpers.hpp index fa1bd077e..31283abd3 100644 --- a/packages/react-native-nitro-modules/cpp/jsi/JSIHelpers.hpp +++ b/packages/react-native-nitro-modules/cpp/jsi/JSIHelpers.hpp @@ -1,6 +1,6 @@ // // JSIHelpers.hpp -// Pods +// Nitro // // Created by Marc Rousavy on 07.08.24. // diff --git a/packages/react-native-nitro-modules/cpp/platform/NitroLogger.hpp b/packages/react-native-nitro-modules/cpp/platform/NitroLogger.hpp index cc54fd8da..3298f7025 100644 --- a/packages/react-native-nitro-modules/cpp/platform/NitroLogger.hpp +++ b/packages/react-native-nitro-modules/cpp/platform/NitroLogger.hpp @@ -4,6 +4,7 @@ #pragma once +#include "NitroDefines.hpp" #include #include #include @@ -22,7 +23,7 @@ class Logger final { public: template static void log(LogLevel level, const char* tag, const char* format, Args... args) { -#ifndef NDEBUG +#ifdef NITRO_DEBUG // 1. Make sure args can be passed to sprintf(..) static_assert(all_are_trivially_copyable(), "All arguments passed to Logger::log(..) must be trivially copyable! " "Did you try to pass a complex type, like std::string?"); diff --git a/packages/react-native-nitro-modules/cpp/prototype/HybridObjectPrototype.cpp b/packages/react-native-nitro-modules/cpp/prototype/HybridObjectPrototype.cpp index 568756dc1..e9d8208f0 100644 --- a/packages/react-native-nitro-modules/cpp/prototype/HybridObjectPrototype.cpp +++ b/packages/react-native-nitro-modules/cpp/prototype/HybridObjectPrototype.cpp @@ -6,6 +6,7 @@ // #include "HybridObjectPrototype.hpp" +#include "NitroDefines.hpp" #include "NitroLogger.hpp" namespace margelo::nitro { @@ -69,7 +70,7 @@ jsi::Value HybridObjectPrototype::createPrototype(jsi::Runtime& runtime, const s prototypeCache.emplace(prototype->getNativeInstanceId(), cachedObject); // 7. In DEBUG, add a __type info to the prototype object. -#ifndef NDEBUG +#ifdef NITRO_DEBUG auto typeName = "Prototype<" + std::string(prototype->getNativeInstanceId().name()) + ">"; cachedObject->setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, typeName)); #endif diff --git a/packages/react-native-nitro-modules/cpp/registry/HybridObjectRegistry.cpp b/packages/react-native-nitro-modules/cpp/registry/HybridObjectRegistry.cpp index fad5fdb8a..a6728f957 100644 --- a/packages/react-native-nitro-modules/cpp/registry/HybridObjectRegistry.cpp +++ b/packages/react-native-nitro-modules/cpp/registry/HybridObjectRegistry.cpp @@ -6,6 +6,7 @@ // #include "HybridObjectRegistry.hpp" +#include "NitroDefines.hpp" #include "NitroLogger.hpp" namespace margelo::nitro { @@ -31,7 +32,7 @@ std::vector HybridObjectRegistry::getAllHybridObjectNames() { void HybridObjectRegistry::registerHybridObjectConstructor(const std::string& hybridObjectName, HybridObjectConstructorFn&& constructorFn) { Logger::log(LogLevel::Info, TAG, "Registering HybridObject \"%s\"...", hybridObjectName.c_str()); auto& map = HybridObjectRegistry::getRegistry(); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (map.contains(hybridObjectName)) [[unlikely]] { auto message = "HybridObject \"" + std::string(hybridObjectName) + @@ -63,7 +64,7 @@ std::shared_ptr HybridObjectRegistry::createHybridObject(const std } std::shared_ptr instance = fn->second(); -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - " diff --git a/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp b/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp index cb3e0b81e..f3a83225b 100644 --- a/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp +++ b/packages/react-native-nitro-modules/cpp/threading/Dispatcher.cpp @@ -1,5 +1,6 @@ #include "Dispatcher.hpp" #include "JSIHelpers.hpp" +#include "NitroDefines.hpp" #include "NitroLogger.hpp" namespace margelo::nitro { @@ -41,8 +42,8 @@ std::shared_ptr Dispatcher::getRuntimeGlobalDispatcher(jsi::Runtime& } jsi::Value Dispatcher::getRuntimeGlobalDispatcherHolder(jsi::Runtime& runtime) { -#ifndef NDEBUG - if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) { +#ifdef NITRO_DEBUG + if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) [[unlikely]] { throw std::runtime_error("Failed to get current Dispatcher - the global Dispatcher " "holder (global." + std::string(GLOBAL_DISPATCHER_HOLDER_NAME) + diff --git a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp index 53357bc76..8406b7ca8 100644 --- a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp +++ b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp @@ -9,6 +9,7 @@ #include "CallInvokerDispatcher.hpp" #include "Dispatcher.hpp" #include "HybridObjectRegistry.hpp" +#include "NitroDefines.hpp" namespace facebook::react { @@ -34,7 +35,7 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID& return jsi::Function::createFromHostFunction( runtime, jsi::PropNameID::forUtf8(runtime, "createHybridObject"), 2, [this](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args, size_t count) -> jsi::Value { -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (count != 1 && count != 2) [[unlikely]] { throw jsi::JSError(runtime, "NitroModules.createHybridObject(..) expects 1 or 2 arguments, but " + std::to_string(count) + " were supplied!"); @@ -53,7 +54,7 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID& return jsi::Function::createFromHostFunction( runtime, jsi::PropNameID::forUtf8(runtime, "hasHybridObject"), 1, [this](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args, size_t count) -> jsi::Value { -#ifndef NDEBUG +#ifdef NITRO_DEBUG if (count != 1) [[unlikely]] { throw jsi::JSError(runtime, "NitroModules.hasHybridObject(..) expects 1 argument (name), but received " + std::to_string(count) + "!"); diff --git a/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp b/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp index 1c27520cf..0bab27630 100644 --- a/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/NitroDefines.hpp @@ -1,6 +1,6 @@ // // NitroDefines.hpp -// Pods +// Nitro // // Created by Marc Rousavy on 29.07.24. // @@ -8,6 +8,14 @@ #ifndef NitroDefines_h #define NitroDefines_h +// Sets whether to use debug or optimized production build flags +#ifdef DEBUG +#define NITRO_DEBUG +#endif +#ifdef NDEBUG +#undef NITRO_DEBUG +#endif + // Helper to find out if a C++ compiler attribute is available #ifdef __has_attribute #define _CXX_INTEROP_HAS_ATTRIBUTE(x) __has_attribute(x) diff --git a/packages/react-native-nitro-modules/cpp/utils/OwningLock.hpp b/packages/react-native-nitro-modules/cpp/utils/OwningLock.hpp index 1a8331ddd..a2926489b 100644 --- a/packages/react-native-nitro-modules/cpp/utils/OwningLock.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/OwningLock.hpp @@ -1,6 +1,6 @@ // // OwningLock.hpp -// Pods +// Nitro // // Created by Marc Rousavy on 30.07.24. // diff --git a/packages/react-native-nitro-modules/cpp/utils/TypeInfo.hpp b/packages/react-native-nitro-modules/cpp/utils/TypeInfo.hpp index 4ec3439b7..e4d5a5ada 100644 --- a/packages/react-native-nitro-modules/cpp/utils/TypeInfo.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/TypeInfo.hpp @@ -1,6 +1,6 @@ // // TypeInfo.hpp -// Pods +// Nitro // // Created by Marc Rousavy on 17.07.24. // diff --git a/packages/react-native-nitro-modules/ios/core/PromiseHolder.hpp b/packages/react-native-nitro-modules/ios/core/PromiseHolder.hpp index eeb11f789..415e2b2de 100644 --- a/packages/react-native-nitro-modules/ios/core/PromiseHolder.hpp +++ b/packages/react-native-nitro-modules/ios/core/PromiseHolder.hpp @@ -1,6 +1,6 @@ // // PromiseHolder.hpp -// Pods +// Nitro // // Created by Marc Rousavy on 15.08.24. // diff --git a/packages/react-native-nitro-modules/ios/platform/NitroLogger.mm b/packages/react-native-nitro-modules/ios/platform/NitroLogger.mm index a1c58fc1b..5cf31fdd5 100644 --- a/packages/react-native-nitro-modules/ios/platform/NitroLogger.mm +++ b/packages/react-native-nitro-modules/ios/platform/NitroLogger.mm @@ -7,6 +7,7 @@ #include "NitroLogger.hpp" #include "AnyMapHolder.hpp" +#include "NitroDefines.hpp" #include namespace margelo::nitro { @@ -27,7 +28,7 @@ } void Logger::nativeLog(LogLevel level, const char* tag, const std::string& message) { -#ifndef NDEBUG +#ifdef NITRO_DEBUG const char* logLevel = levelToString(level); NSLog(@"[%s] [Nitro.%s] %s", logLevel, tag, message.c_str()); #endif From 081a322e42c9c6fc63c5f752b0bbe042c1f3897e Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 16 Sep 2024 16:12:58 +0200 Subject: [PATCH 2/4] feat: Add `NitroModules.buildType` --- .../xcschemes/NitroExample.xcscheme | 2 +- .../src/screens/HybridObjectTestsScreen.tsx | 29 ++++++++++++------- .../cpp/turbomodule/NativeNitroModules.cpp | 7 +++++ .../src/NativeNitroModules.ts | 1 + .../src/NitroModules.ts | 8 +++++ 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme b/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme index 982948587..bf347165c 100644 --- a/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme +++ b/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme @@ -41,7 +41,7 @@ HybridObject Tests - { - setSelectedIndex(selectedSegmentIndex) - }} - /> + + { + setSelectedIndex(selectedSegmentIndex) + }} + /> + + {NitroModules.buildType} + {tests.map((t, i) => ( @@ -170,11 +175,13 @@ const styles = StyleSheet.create({ scrollContent: { paddingHorizontal: 15, }, - segmentedControl: { - alignSelf: 'flex-start', - minWidth: 180, + topControls: { marginLeft: 15, marginBottom: 10, + flexDirection: 'row', + }, + segmentedControl: { + minWidth: 180, }, box: { width: 60, diff --git a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp index 8406b7ca8..8d17e465e 100644 --- a/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp +++ b/packages/react-native-nitro-modules/cpp/turbomodule/NativeNitroModules.cpp @@ -87,6 +87,13 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID& return jsi::Value::undefined(); }); } + if (name == "buildType") { +#ifdef NITRO_DEBUG + return jsi::String::createFromAscii(runtime, "debug"); +#else + return jsi::String::createFromAscii(runtime, "release"); +#endif + } return jsi::Value::undefined(); } diff --git a/packages/react-native-nitro-modules/src/NativeNitroModules.ts b/packages/react-native-nitro-modules/src/NativeNitroModules.ts index 004bd2891..6fefcea99 100644 --- a/packages/react-native-nitro-modules/src/NativeNitroModules.ts +++ b/packages/react-native-nitro-modules/src/NativeNitroModules.ts @@ -13,6 +13,7 @@ export interface Spec extends TurboModule { // JSI Helpers hasNativeState(obj: UnsafeObject): boolean removeNativeState(obj: UnsafeObject): void + buildType: 'debug' | 'release' } let turboModule: Spec | undefined diff --git a/packages/react-native-nitro-modules/src/NitroModules.ts b/packages/react-native-nitro-modules/src/NitroModules.ts index 07608a678..325efab86 100644 --- a/packages/react-native-nitro-modules/src/NitroModules.ts +++ b/packages/react-native-nitro-modules/src/NitroModules.ts @@ -65,4 +65,12 @@ export const NitroModules = { const nitro = getNativeNitroModules() nitro.removeNativeState(object) }, + /** + * Gets the current build type configuration as defined in the `NITRO_DEBUG` + * preprocessor flag. + */ + get buildType(): 'debug' | 'release' { + const nitro = getNativeNitroModules() + return nitro.buildType + }, } From ba713c215caf18f4cf5c640b6b7804d2d49b6085 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 16 Sep 2024 16:16:07 +0200 Subject: [PATCH 3/4] fix: Style `debug` label --- .../xcschemes/NitroExample.xcscheme | 2 +- .../src/screens/HybridObjectTestsScreen.tsx | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme b/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme index bf347165c..982948587 100644 --- a/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme +++ b/example/ios/NitroExample.xcodeproj/xcshareddata/xcschemes/NitroExample.xcscheme @@ -41,7 +41,7 @@ - {NitroModules.buildType} + {NitroModules.buildType} @@ -176,9 +183,18 @@ const styles = StyleSheet.create({ paddingHorizontal: 15, }, topControls: { - marginLeft: 15, + marginHorizontal: 15, marginBottom: 10, flexDirection: 'row', + alignItems: 'center', + }, + buildTypeText: { + fontFamily: Platform.select({ + ios: 'Menlo', + macos: 'Menlo', + android: 'monospace', + }), + fontWeight: 'bold', }, segmentedControl: { minWidth: 180, From 921138e3770aea83f32582731106b793166ae6c1 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Mon, 16 Sep 2024 16:17:47 +0200 Subject: [PATCH 4/4] fix: regenerate --- .../nitrogen/generated/android/NitroImageOnLoad.cpp | 4 ++-- .../nitrogen/generated/android/NitroImageOnLoad.hpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) 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 aafb69726..2182fbf53 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.cpp @@ -43,7 +43,7 @@ int initialize(JavaVM* vm) { []() -> std::shared_ptr { static auto javaClass = jni::findClassStatic("com/margelo/nitro/image/ImageFactory"); static auto defaultConstructor = javaClass->getConstructor(); - + auto instance = javaClass->newObject(defaultConstructor); #ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { @@ -68,7 +68,7 @@ int initialize(JavaVM* vm) { []() -> std::shared_ptr { static auto javaClass = jni::findClassStatic("com/margelo/nitro/image/HybridTestObjectKotlin"); static auto defaultConstructor = javaClass->getConstructor(); - + auto instance = javaClass->newObject(defaultConstructor); #ifdef NITRO_DEBUG if (instance == nullptr) [[unlikely]] { diff --git a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.hpp b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.hpp index e6bbdd036..87d64d4dc 100644 --- a/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.hpp +++ b/packages/react-native-nitro-image/nitrogen/generated/android/NitroImageOnLoad.hpp @@ -6,6 +6,7 @@ /// #include +#include namespace margelo::nitro::image {