react-native-nitro-modules 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/android/src/main/cpp/platform/NitroLogger.cpp +2 -1
  2. package/android/src/main/cpp/registry/JHybridObjectRegistry.cpp +2 -1
  3. package/cpp/core/ArrayBuffer.hpp +0 -1
  4. package/cpp/core/HybridFunction.hpp +5 -4
  5. package/cpp/core/HybridObject.cpp +4 -56
  6. package/cpp/core/HybridObject.hpp +0 -1
  7. package/cpp/jsi/JSICache.cpp +2 -1
  8. package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +10 -2
  9. package/cpp/jsi/JSIConverter+HybridObject.hpp +3 -2
  10. package/cpp/jsi/JSIHelpers.hpp +1 -1
  11. package/cpp/platform/NitroLogger.hpp +2 -1
  12. package/cpp/prototype/HybridObjectPrototype.cpp +2 -1
  13. package/cpp/registry/HybridObjectRegistry.cpp +15 -9
  14. package/cpp/threading/Dispatcher.cpp +3 -2
  15. package/cpp/turbomodule/NativeNitroModules.cpp +10 -2
  16. package/cpp/utils/NitroDefines.hpp +9 -1
  17. package/cpp/utils/OwningLock.hpp +1 -1
  18. package/cpp/utils/TypeInfo.hpp +1 -1
  19. package/ios/core/PromiseHolder.hpp +1 -1
  20. package/ios/platform/NitroLogger.mm +2 -1
  21. package/lib/NativeNitroModules.d.ts +1 -0
  22. package/lib/NitroModules.d.ts +5 -0
  23. package/lib/NitroModules.js +9 -1
  24. package/lib/NitroModulesTurboModule.d.ts +17 -0
  25. package/lib/NitroModulesTurboModule.js +23 -0
  26. package/lib/NitroModulesTurboModule.web.d.ts +1 -0
  27. package/lib/NitroModulesTurboModule.web.js +4 -0
  28. package/lib/commonjs/NitroModules.js +14 -6
  29. package/lib/commonjs/NitroModules.js.map +1 -1
  30. package/lib/commonjs/{NativeNitroModules.js → NitroModulesTurboModule.js} +5 -1
  31. package/lib/commonjs/NitroModulesTurboModule.js.map +1 -0
  32. package/lib/commonjs/NitroModulesTurboModule.web.js +11 -0
  33. package/lib/commonjs/NitroModulesTurboModule.web.js.map +1 -0
  34. package/lib/module/NitroModules.js +9 -1
  35. package/lib/module/NitroModules.js.map +1 -1
  36. package/lib/module/{NativeNitroModules.js → NitroModulesTurboModule.js} +6 -1
  37. package/lib/module/NitroModulesTurboModule.js.map +1 -0
  38. package/lib/module/NitroModulesTurboModule.web.js +7 -0
  39. package/lib/module/NitroModulesTurboModule.web.js.map +1 -0
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/typescript/AnyMap.d.ts.map +1 -0
  42. package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -0
  43. package/lib/typescript/NitroModules.d.ts.map +1 -1
  44. package/lib/typescript/NitroModulesTurboModule.d.ts +18 -0
  45. package/lib/typescript/NitroModulesTurboModule.d.ts.map +1 -0
  46. package/lib/typescript/NitroModulesTurboModule.web.d.ts +2 -0
  47. package/lib/typescript/NitroModulesTurboModule.web.d.ts.map +1 -0
  48. package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
  49. package/package.json +1 -1
  50. package/src/NitroModules.ts +9 -1
  51. package/src/{NativeNitroModules.ts → NitroModulesTurboModule.ts} +9 -4
  52. package/src/NitroModulesTurboModule.web.ts +7 -0
  53. package/lib/commonjs/NativeNitroModules.js.map +0 -1
  54. package/lib/commonjs/NativeNitroModules.web.js +0 -10
  55. package/lib/commonjs/NativeNitroModules.web.js.map +0 -1
  56. package/lib/module/NativeNitroModules.js.map +0 -1
  57. package/lib/module/NativeNitroModules.web.js +0 -6
  58. package/lib/module/NativeNitroModules.web.js.map +0 -1
  59. package/lib/typescript/HybridObject.d.ts +0 -99
  60. package/src/NativeNitroModules.web.ts +0 -9
@@ -6,6 +6,7 @@
6
6
  //
7
7
 
8
8
  #include "NitroLogger.hpp"
9
+ #include "NitroDefines.hpp"
9
10
  #include <android/log.h>
10
11
 
11
12
  namespace margelo::nitro {
@@ -26,7 +27,7 @@ int levelToAndroidLevel(LogLevel level) {
26
27
  }
27
28
 
28
29
  void Logger::nativeLog(LogLevel level, const char* tag, const std::string& message) {
29
- #ifndef NDEBUG
30
+ #ifdef NITRO_DEBUG
30
31
  int logLevel = levelToAndroidLevel(level);
31
32
  std::string combinedTag = "Nitro." + std::string(tag);
32
33
  __android_log_print(logLevel, combinedTag.c_str(), "%s", message.c_str());
@@ -8,6 +8,7 @@
8
8
  #include "JHybridObjectRegistry.hpp"
9
9
  #include "HybridObjectRegistry.hpp"
10
10
  #include "JNISharedPtr.hpp"
11
+ #include "NitroDefines.hpp"
11
12
 
12
13
  namespace margelo::nitro {
13
14
 
@@ -17,7 +18,7 @@ void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref<jni::
17
18
  HybridObjectRegistry::registerHybridObjectConstructor(hybridObjectName, [=]() -> std::shared_ptr<HybridObject> {
18
19
  // 1. Call the Java initializer function
19
20
  jni::local_ref<JHybridObject::javaobject> hybridObject = sharedInitializer->call();
20
- #ifndef NDEBUG
21
+ #ifdef NITRO_DEBUG
21
22
  if (hybridObject == nullptr) [[unlikely]] {
22
23
  throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - the constructor returned null!");
23
24
  }
@@ -88,7 +88,6 @@ private:
88
88
  uint8_t* _data;
89
89
  size_t _size;
90
90
  DeleteFn _deleteFunc;
91
- void* _deleteFuncContext;
92
91
  };
93
92
 
94
93
  /**
@@ -14,6 +14,7 @@ struct JSIConverter;
14
14
 
15
15
  #include "CountTrailingOptionals.hpp"
16
16
  #include "JSIConverter.hpp"
17
+ #include "NitroDefines.hpp"
17
18
  #include "TypeInfo.hpp"
18
19
  #include <functional>
19
20
  #include <jsi/jsi.h>
@@ -170,7 +171,7 @@ private:
170
171
  static inline std::shared_ptr<THybrid> getHybridObjectNativeState(jsi::Runtime& runtime, const jsi::Value& value, FunctionKind funcKind,
171
172
  const std::string& funcName) {
172
173
  // 1. Convert jsi::Value to jsi::Object
173
- #ifndef NDEBUG
174
+ #ifdef NITRO_DEBUG
174
175
  if (!value.isObject()) [[unlikely]] {
175
176
  throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo<THybrid>(funcKind, funcName) + " - `this` is not bound!");
176
177
  }
@@ -178,7 +179,7 @@ private:
178
179
  jsi::Object object = value.getObject(runtime);
179
180
 
180
181
  // 2. Check if it even has any kind of `NativeState`
181
- #ifndef NDEBUG
182
+ #ifdef NITRO_DEBUG
182
183
  if (!object.hasNativeState(runtime)) [[unlikely]] {
183
184
  throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo<THybrid>(funcKind, funcName) +
184
185
  " - `this` does not have a NativeState! Suggestions:\n"
@@ -191,7 +192,7 @@ private:
191
192
 
192
193
  // 3. Get `NativeState` from the jsi::Object and check if it is non-null
193
194
  std::shared_ptr<jsi::NativeState> nativeState = object.getNativeState(runtime);
194
- #ifndef NDEBUG
195
+ #ifdef NITRO_DEBUG
195
196
  if (nativeState == nullptr) [[unlikely]] {
196
197
  throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo<THybrid>(funcKind, funcName) +
197
198
  " - `this`'s `NativeState` is `nullptr`, "
@@ -201,7 +202,7 @@ private:
201
202
 
202
203
  // 4. Try casting it to our desired target type.
203
204
  std::shared_ptr<THybrid> hybridInstance = std::dynamic_pointer_cast<THybrid>(nativeState);
204
- #ifndef NDEBUG
205
+ #ifdef NITRO_DEBUG
205
206
  if (hybridInstance == nullptr) [[unlikely]] {
206
207
  throw jsi::JSError(runtime, "Cannot " + getHybridFuncDebugInfo<THybrid>(funcKind, funcName) +
207
208
  " - `this` has a NativeState, but it's the wrong type!");
@@ -4,64 +4,12 @@
4
4
 
5
5
  #include "HybridObject.hpp"
6
6
  #include "JSIConverter.hpp"
7
- #include "NitroLogger.hpp"
8
-
9
- #define LOG_MEMORY_ALLOCATIONS false
7
+ #include "NitroDefines.hpp"
10
8
 
11
9
  namespace margelo::nitro {
12
10
 
13
- #if LOG_MEMORY_ALLOCATIONS
14
- static std::mutex _instanceCounterMutex;
15
- static std::unordered_map<const char*, uint32_t> _aliveInstances;
16
- static std::unordered_map<const char*, int> _instanceIds;
17
-
18
- static uint32_t incrementAliveInstancesAndGet(const char* name) {
19
- std::unique_lock lock(_instanceCounterMutex);
20
- return ++_aliveInstances[name];
21
- }
22
-
23
- static uint32_t decrementAliveInstancesAndGet(const char* name) {
24
- std::unique_lock lock(_instanceCounterMutex);
25
- return --_aliveInstances[name];
26
- }
27
-
28
- static uint32_t getTotalAliveInstances() {
29
- std::unique_lock lock(_instanceCounterMutex);
30
- uint32_t total = 0;
31
- for (const auto& iter : _aliveInstances) {
32
- total += iter.second;
33
- }
34
- return total;
35
- }
36
-
37
- static int getId(const char* name) {
38
- std::unique_lock lock(_instanceCounterMutex);
39
- if (_instanceIds.find(name) == _instanceIds.end()) {
40
- _instanceIds.insert({name, 1});
41
- }
42
- auto iterator = _instanceIds.find(name);
43
- return iterator->second++;
44
- }
45
- #endif
46
-
47
- HybridObject::HybridObject(const char* name) : HybridObjectPrototype(), _name(name) {
48
- #if LOG_MEMORY_ALLOCATIONS
49
- _instanceId = getId(name);
50
- uint32_t alive = incrementAliveInstancesAndGet(_name);
51
- uint32_t totalObjects = getTotalAliveInstances();
52
- Logger::log(LogLevel::Info, TAG, "(MEMORY) ✅ Creating %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i)", _name, _instanceId,
53
- _name, alive, totalObjects);
54
- #endif
55
- }
56
-
57
- HybridObject::~HybridObject() {
58
- #if LOG_MEMORY_ALLOCATIONS
59
- uint32_t alive = decrementAliveInstancesAndGet(_name);
60
- uint32_t totalObjects = getTotalAliveInstances();
61
- Logger::log(LogLevel::Info, TAG, "(MEMORY) ❌ Deleting %s (#%i)... (Total %s(s): %i | Total HybridObjects: %i) ", _name, _instanceId,
62
- _name, alive, totalObjects);
63
- #endif
64
- }
11
+ HybridObject::HybridObject(const char* name) : HybridObjectPrototype(), _name(name) {}
12
+ HybridObject::~HybridObject() {}
65
13
 
66
14
  std::string HybridObject::toString() {
67
15
  return "[HybridObject " + std::string(_name) + "]";
@@ -123,7 +71,7 @@ jsi::Value HybridObject::toObject(jsi::Runtime& runtime) {
123
71
  // 6. Set memory size so Hermes GC knows about actual memory
124
72
  object.setExternalMemoryPressure(runtime, getExternalMemorySize());
125
73
 
126
- #ifndef NDEBUG
74
+ #ifdef NITRO_DEBUG
127
75
  // 7. Assign a private __type property for debugging - this will be used so users know it's not just an empty object.
128
76
  object.setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, "NativeState<" + std::string(_name) + ">"));
129
77
  #endif
@@ -136,7 +136,6 @@ protected:
136
136
  private:
137
137
  static constexpr auto TAG = "HybridObject";
138
138
  const char* _name = TAG;
139
- int _instanceId = 1;
140
139
  std::unordered_map<jsi::Runtime*, OwningReference<jsi::WeakObject>> _objectCache;
141
140
  };
142
141
 
@@ -7,6 +7,7 @@
7
7
 
8
8
  #include "JSICache.hpp"
9
9
  #include "JSIHelpers.hpp"
10
+ #include "NitroDefines.hpp"
10
11
 
11
12
  namespace margelo::nitro {
12
13
 
@@ -46,7 +47,7 @@ JSICacheReference JSICache::getOrCreateCache(jsi::Runtime& runtime) {
46
47
  Logger::log(LogLevel::Warning, TAG, "JSICache was created, but it is no longer strong!");
47
48
  }
48
49
 
49
- #ifndef NDEBUG
50
+ #ifdef NITRO_DEBUG
50
51
  if (runtime.global().hasProperty(runtime, CACHE_PROP_NAME)) [[unlikely]] {
51
52
  throw std::runtime_error("The Runtime \"" + getRuntimeId(runtime) + "\" already has a global cache! (\"" + CACHE_PROP_NAME + "\")");
52
53
  }
@@ -18,6 +18,7 @@ struct JSIConverter;
18
18
  #include "ArrayBuffer.hpp"
19
19
  #include "IsSharedPtrTo.hpp"
20
20
  #include "JSICache.hpp"
21
+ #include "NitroDefines.hpp"
21
22
  #include <jsi/jsi.h>
22
23
  #include <memory>
23
24
  #include <type_traits>
@@ -30,9 +31,16 @@ using namespace facebook;
30
31
  template <typename T>
31
32
  struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::MutableBuffer>>> final {
32
33
  static inline std::shared_ptr<ArrayBuffer> fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
33
- jsi::Object object = arg.asObject(runtime);
34
+ #ifdef NITRO_DEBUG
35
+ if (!arg.isObject()) [[unlikely]] {
36
+ throw std::runtime_error("Value \"" + arg.toString(runtime).utf8(runtime) +
37
+ "\" is not an ArrayBuffer - "
38
+ "in fact, it's not even an object!");
39
+ }
40
+ #endif
34
41
 
35
- #ifndef NDEBUG
42
+ jsi::Object object = arg.asObject(runtime);
43
+ #ifdef NITRO_DEBUG
36
44
  if (!object.isArrayBuffer(runtime)) [[unlikely]] {
37
45
  throw std::runtime_error("Object \"" + arg.toString(runtime).utf8(runtime) +
38
46
  "\" is not an ArrayBuffer! "
@@ -10,6 +10,7 @@ class HybridObject;
10
10
  } // namespace margelo::nitro
11
11
 
12
12
  #include "IsSharedPtrTo.hpp"
13
+ #include "NitroDefines.hpp"
13
14
  #include "TypeInfo.hpp"
14
15
  #include <jsi/jsi.h>
15
16
  #include <type_traits>
@@ -24,7 +25,7 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
24
25
  using TPointee = typename T::element_type;
25
26
 
26
27
  static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
27
- #ifndef NDEBUG
28
+ #ifdef NITRO_DEBUG
28
29
  if (!arg.isObject()) [[unlikely]] {
29
30
  if (arg.isUndefined()) [[unlikely]] {
30
31
  throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!"));
@@ -36,7 +37,7 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
36
37
  #endif
37
38
  jsi::Object object = arg.asObject(runtime);
38
39
 
39
- #ifndef NDEBUG
40
+ #ifdef NITRO_DEBUG
40
41
  if (!object.hasNativeState<TPointee>(runtime)) [[unlikely]] {
41
42
  if (!object.hasNativeState(runtime)) [[unlikely]] {
42
43
  std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
@@ -1,6 +1,6 @@
1
1
  //
2
2
  // JSIHelpers.hpp
3
- // Pods
3
+ // Nitro
4
4
  //
5
5
  // Created by Marc Rousavy on 07.08.24.
6
6
  //
@@ -4,6 +4,7 @@
4
4
 
5
5
  #pragma once
6
6
 
7
+ #include "NitroDefines.hpp"
7
8
  #include <cstdarg>
8
9
  #include <cstdio>
9
10
  #include <iostream>
@@ -22,7 +23,7 @@ private:
22
23
  public:
23
24
  template <typename... Args>
24
25
  static void log(LogLevel level, const char* tag, const char* format, Args... args) {
25
- #ifndef NDEBUG
26
+ #ifdef NITRO_DEBUG
26
27
  // 1. Make sure args can be passed to sprintf(..)
27
28
  static_assert(all_are_trivially_copyable<Args...>(), "All arguments passed to Logger::log(..) must be trivially copyable! "
28
29
  "Did you try to pass a complex type, like std::string?");
@@ -6,6 +6,7 @@
6
6
  //
7
7
 
8
8
  #include "HybridObjectPrototype.hpp"
9
+ #include "NitroDefines.hpp"
9
10
  #include "NitroLogger.hpp"
10
11
 
11
12
  namespace margelo::nitro {
@@ -69,7 +70,7 @@ jsi::Value HybridObjectPrototype::createPrototype(jsi::Runtime& runtime, const s
69
70
  prototypeCache.emplace(prototype->getNativeInstanceId(), cachedObject);
70
71
 
71
72
  // 7. In DEBUG, add a __type info to the prototype object.
72
- #ifndef NDEBUG
73
+ #ifdef NITRO_DEBUG
73
74
  auto typeName = "Prototype<" + std::string(prototype->getNativeInstanceId().name()) + ">";
74
75
  cachedObject->setProperty(runtime, "__type", jsi::String::createFromUtf8(runtime, typeName));
75
76
  #endif
@@ -6,6 +6,7 @@
6
6
  //
7
7
 
8
8
  #include "HybridObjectRegistry.hpp"
9
+ #include "NitroDefines.hpp"
9
10
  #include "NitroLogger.hpp"
10
11
 
11
12
  namespace margelo::nitro {
@@ -31,7 +32,7 @@ std::vector<std::string> HybridObjectRegistry::getAllHybridObjectNames() {
31
32
  void HybridObjectRegistry::registerHybridObjectConstructor(const std::string& hybridObjectName, HybridObjectConstructorFn&& constructorFn) {
32
33
  Logger::log(LogLevel::Info, TAG, "Registering HybridObject \"%s\"...", hybridObjectName.c_str());
33
34
  auto& map = HybridObjectRegistry::getRegistry();
34
- #ifndef NDEBUG
35
+ #ifdef NITRO_DEBUG
35
36
  if (map.contains(hybridObjectName)) [[unlikely]] {
36
37
  auto message =
37
38
  "HybridObject \"" + std::string(hybridObjectName) +
@@ -57,22 +58,27 @@ std::shared_ptr<HybridObject> HybridObjectRegistry::createHybridObject(const std
57
58
  auto fn = map.find(hybridObjectName);
58
59
  if (fn == map.end()) [[unlikely]] {
59
60
  auto message = "Cannot create an instance of HybridObject \"" + std::string(hybridObjectName) +
60
- "\" - "
61
- "It has not yet been registered in the Nitro Modules HybridObjectRegistry!";
61
+ "\" - It has not yet been registered in the Nitro Modules HybridObjectRegistry! Suggestions:\n"
62
+ "- If you use Nitrogen, make sure your `nitro.json` contains `" +
63
+ std::string(hybridObjectName) +
64
+ "` on this platform.\n"
65
+ "- If you use Nitrogen, make sure your library (*Package.java)/app (MainApplication.java) calls "
66
+ "`System.loadLibrary(\"<<androidCxxLibName>>\")` somewhere on app-startup.\n"
67
+ "- If you use Nitrogen, make sure your cpp-adapter.cpp calls `margelo::nitro::<<cxxNamespace>>::initialize(vm)`.\n"
68
+ "- If you use Nitrogen, inspect the generated `<<androidCxxLibName>>OnLoad.cpp` file.\n"
69
+ "- If you don't use Nitrogen, make sure you called `HybridObjectRegistry.registerHybridObject(...)`.";
62
70
  throw std::runtime_error(message);
63
71
  }
64
72
  std::shared_ptr<HybridObject> instance = fn->second();
65
73
 
66
- #ifndef NDEBUG
74
+ #ifdef NITRO_DEBUG
67
75
  if (instance == nullptr) [[unlikely]] {
68
- throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName +
69
- "\" - "
70
- "The constructor returned a nullptr!");
76
+ throw std::runtime_error("Failed to create HybridObject \"" + hybridObjectName + "\" - The constructor returned a nullptr!");
71
77
  }
72
78
  if (instance->getName() != hybridObjectName) [[unlikely]] {
73
79
  throw std::runtime_error("HybridObject's name (\"" + instance->getName() +
74
- "\") does not match"
75
- "the name it was registered with (\"" +
80
+ "\") "
81
+ "does not match the name it was registered with (\"" +
76
82
  hybridObjectName + "\")!");
77
83
  }
78
84
  #endif
@@ -1,5 +1,6 @@
1
1
  #include "Dispatcher.hpp"
2
2
  #include "JSIHelpers.hpp"
3
+ #include "NitroDefines.hpp"
3
4
  #include "NitroLogger.hpp"
4
5
 
5
6
  namespace margelo::nitro {
@@ -41,8 +42,8 @@ std::shared_ptr<Dispatcher> Dispatcher::getRuntimeGlobalDispatcher(jsi::Runtime&
41
42
  }
42
43
 
43
44
  jsi::Value Dispatcher::getRuntimeGlobalDispatcherHolder(jsi::Runtime& runtime) {
44
- #ifndef NDEBUG
45
- if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) {
45
+ #ifdef NITRO_DEBUG
46
+ if (!runtime.global().hasProperty(runtime, GLOBAL_DISPATCHER_HOLDER_NAME)) [[unlikely]] {
46
47
  throw std::runtime_error("Failed to get current Dispatcher - the global Dispatcher "
47
48
  "holder (global." +
48
49
  std::string(GLOBAL_DISPATCHER_HOLDER_NAME) +
@@ -9,6 +9,7 @@
9
9
  #include "CallInvokerDispatcher.hpp"
10
10
  #include "Dispatcher.hpp"
11
11
  #include "HybridObjectRegistry.hpp"
12
+ #include "NitroDefines.hpp"
12
13
 
13
14
  namespace facebook::react {
14
15
 
@@ -34,7 +35,7 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID&
34
35
  return jsi::Function::createFromHostFunction(
35
36
  runtime, jsi::PropNameID::forUtf8(runtime, "createHybridObject"), 2,
36
37
  [this](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args, size_t count) -> jsi::Value {
37
- #ifndef NDEBUG
38
+ #ifdef NITRO_DEBUG
38
39
  if (count != 1 && count != 2) [[unlikely]] {
39
40
  throw jsi::JSError(runtime, "NitroModules.createHybridObject(..) expects 1 or 2 arguments, but " + std::to_string(count) +
40
41
  " were supplied!");
@@ -53,7 +54,7 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID&
53
54
  return jsi::Function::createFromHostFunction(
54
55
  runtime, jsi::PropNameID::forUtf8(runtime, "hasHybridObject"), 1,
55
56
  [this](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args, size_t count) -> jsi::Value {
56
- #ifndef NDEBUG
57
+ #ifdef NITRO_DEBUG
57
58
  if (count != 1) [[unlikely]] {
58
59
  throw jsi::JSError(runtime,
59
60
  "NitroModules.hasHybridObject(..) expects 1 argument (name), but received " + std::to_string(count) + "!");
@@ -86,6 +87,13 @@ jsi::Value NativeNitroModules::get(jsi::Runtime& runtime, const jsi::PropNameID&
86
87
  return jsi::Value::undefined();
87
88
  });
88
89
  }
90
+ if (name == "buildType") {
91
+ #ifdef NITRO_DEBUG
92
+ return jsi::String::createFromAscii(runtime, "debug");
93
+ #else
94
+ return jsi::String::createFromAscii(runtime, "release");
95
+ #endif
96
+ }
89
97
 
90
98
  return jsi::Value::undefined();
91
99
  }
@@ -1,6 +1,6 @@
1
1
  //
2
2
  // NitroDefines.hpp
3
- // Pods
3
+ // Nitro
4
4
  //
5
5
  // Created by Marc Rousavy on 29.07.24.
6
6
  //
@@ -8,6 +8,14 @@
8
8
  #ifndef NitroDefines_h
9
9
  #define NitroDefines_h
10
10
 
11
+ // Sets whether to use debug or optimized production build flags
12
+ #ifdef DEBUG
13
+ #define NITRO_DEBUG
14
+ #endif
15
+ #ifdef NDEBUG
16
+ #undef NITRO_DEBUG
17
+ #endif
18
+
11
19
  // Helper to find out if a C++ compiler attribute is available
12
20
  #ifdef __has_attribute
13
21
  #define _CXX_INTEROP_HAS_ATTRIBUTE(x) __has_attribute(x)
@@ -1,6 +1,6 @@
1
1
  //
2
2
  // OwningLock.hpp
3
- // Pods
3
+ // Nitro
4
4
  //
5
5
  // Created by Marc Rousavy on 30.07.24.
6
6
  //
@@ -1,6 +1,6 @@
1
1
  //
2
2
  // TypeInfo.hpp
3
- // Pods
3
+ // Nitro
4
4
  //
5
5
  // Created by Marc Rousavy on 17.07.24.
6
6
  //
@@ -1,6 +1,6 @@
1
1
  //
2
2
  // PromiseHolder.hpp
3
- // Pods
3
+ // Nitro
4
4
  //
5
5
  // Created by Marc Rousavy on 15.08.24.
6
6
  //
@@ -7,6 +7,7 @@
7
7
 
8
8
  #include "NitroLogger.hpp"
9
9
  #include "AnyMapHolder.hpp"
10
+ #include "NitroDefines.hpp"
10
11
  #include <Foundation/Foundation.h>
11
12
 
12
13
  namespace margelo::nitro {
@@ -27,7 +28,7 @@ const char* levelToString(LogLevel level) {
27
28
  }
28
29
 
29
30
  void Logger::nativeLog(LogLevel level, const char* tag, const std::string& message) {
30
- #ifndef NDEBUG
31
+ #ifdef NITRO_DEBUG
31
32
  const char* logLevel = levelToString(level);
32
33
  NSLog(@"[%s] [Nitro.%s] %s", logLevel, tag, message.c_str());
33
34
  #endif
@@ -7,6 +7,7 @@ export interface Spec extends TurboModule {
7
7
  getAllHybridObjectNames(): string[];
8
8
  hasNativeState(obj: UnsafeObject): boolean;
9
9
  removeNativeState(obj: UnsafeObject): void;
10
+ buildType: 'debug' | 'release';
10
11
  }
11
12
  export declare function getNativeNitroModules(): Spec;
12
13
  declare global {
@@ -40,4 +40,9 @@ export declare const NitroModules: {
40
40
  * Forcefully removes the `NativeState` of the given {@linkcode object}.
41
41
  */
42
42
  removeNativeState(object: object): void;
43
+ /**
44
+ * Gets the current build type configuration as defined in the `NITRO_DEBUG`
45
+ * preprocessor flag.
46
+ */
47
+ readonly buildType: "debug" | "release";
43
48
  };
@@ -1,4 +1,4 @@
1
- import { getNativeNitroModules } from './NativeNitroModules';
1
+ import { getNativeNitroModules } from './NitroModulesTurboModule';
2
2
  /**
3
3
  * A lazy proxy for initializing Nitro Modules HybridObjects.
4
4
  */
@@ -56,4 +56,12 @@ export const NitroModules = {
56
56
  const nitro = getNativeNitroModules();
57
57
  nitro.removeNativeState(object);
58
58
  },
59
+ /**
60
+ * Gets the current build type configuration as defined in the `NITRO_DEBUG`
61
+ * preprocessor flag.
62
+ */
63
+ get buildType() {
64
+ const nitro = getNativeNitroModules();
65
+ return nitro.buildType;
66
+ },
59
67
  };
@@ -0,0 +1,17 @@
1
+ import type { TurboModule } from 'react-native';
2
+ import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
3
+ export interface NativeNitroSpec extends TurboModule {
4
+ install(): void;
5
+ createHybridObject(name: string, args?: UnsafeObject): UnsafeObject;
6
+ hasHybridObject(name: string): boolean;
7
+ getAllHybridObjectNames(): string[];
8
+ hasNativeState(obj: UnsafeObject): boolean;
9
+ removeNativeState(obj: UnsafeObject): void;
10
+ buildType: 'debug' | 'release';
11
+ }
12
+ export declare function getNativeNitroModules(): NativeNitroSpec;
13
+ declare global {
14
+ var __nitroModulesJSICache: {};
15
+ var __nitroDispatcher: {};
16
+ }
17
+ export declare function isRuntimeAlive(): boolean;
@@ -0,0 +1,23 @@
1
+ import { TurboModuleRegistry } from 'react-native';
2
+ import { ModuleNotFoundError } from './ModuleNotFoundError';
3
+ let turboModule;
4
+ export function getNativeNitroModules() {
5
+ if (turboModule == null) {
6
+ try {
7
+ // 1. Get (and initialize) the C++ TurboModule
8
+ turboModule =
9
+ TurboModuleRegistry.getEnforcing('NitroModulesCxx');
10
+ // 2. Install Dispatcher and required bindings into the Runtime
11
+ turboModule.install();
12
+ }
13
+ catch (e) {
14
+ throw new ModuleNotFoundError(e);
15
+ }
16
+ }
17
+ return turboModule;
18
+ }
19
+ export function isRuntimeAlive() {
20
+ const cache = global.__nitroModulesJSICache;
21
+ const dispatcher = global.__nitroDispatcher;
22
+ return cache != null && dispatcher != null;
23
+ }
@@ -0,0 +1 @@
1
+ export declare function getNativeNitroModules(): never;
@@ -0,0 +1,4 @@
1
+ import { Platform } from 'react-native';
2
+ export function getNativeNitroModules() {
3
+ throw new Error(`Native NitroModules are not available on ${Platform.OS}! Make sure you're not calling getNativeNitroModules() in a ${Platform.OS} (.${Platform.OS}.ts) environment.`);
4
+ }
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.NitroModules = void 0;
7
- var _NativeNitroModules = require("./NativeNitroModules");
7
+ var _NitroModulesTurboModule = require("./NitroModulesTurboModule");
8
8
  // TODO: Do we wanna support such constructors?
9
9
  // @ts-expect-error
10
10
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -24,7 +24,7 @@ const NitroModules = exports.NitroModules = {
24
24
  * @throws an Error if {@linkcode T} has not been registered under the name {@linkcode name}.
25
25
  */
26
26
  createHybridObject(name) {
27
- const nitro = (0, _NativeNitroModules.getNativeNitroModules)();
27
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
28
28
  const instance = nitro.createHybridObject(name);
29
29
  return instance;
30
30
  },
@@ -32,14 +32,14 @@ const NitroModules = exports.NitroModules = {
32
32
  * Get a list of all registered Hybrid Objects.
33
33
  */
34
34
  getAllHybridObjectNames() {
35
- const nitro = (0, _NativeNitroModules.getNativeNitroModules)();
35
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
36
36
  return nitro.getAllHybridObjectNames();
37
37
  },
38
38
  /**
39
39
  * Returns whether a HybridObject under the given {@linkcode name} is registered, or not.
40
40
  */
41
41
  hasHybridObject(name) {
42
- const nitro = (0, _NativeNitroModules.getNativeNitroModules)();
42
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
43
43
  return nitro.hasHybridObject(name);
44
44
  },
45
45
  /**
@@ -56,15 +56,23 @@ const NitroModules = exports.NitroModules = {
56
56
  * ```
57
57
  */
58
58
  hasNativeState(object) {
59
- const nitro = (0, _NativeNitroModules.getNativeNitroModules)();
59
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
60
60
  return nitro.hasNativeState(object);
61
61
  },
62
62
  /**
63
63
  * Forcefully removes the `NativeState` of the given {@linkcode object}.
64
64
  */
65
65
  removeNativeState(object) {
66
- const nitro = (0, _NativeNitroModules.getNativeNitroModules)();
66
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
67
67
  nitro.removeNativeState(object);
68
+ },
69
+ /**
70
+ * Gets the current build type configuration as defined in the `NITRO_DEBUG`
71
+ * preprocessor flag.
72
+ */
73
+ get buildType() {
74
+ const nitro = (0, _NitroModulesTurboModule.getNativeNitroModules)();
75
+ return nitro.buildType;
68
76
  }
69
77
  };
70
78
  //# sourceMappingURL=NitroModules.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_NativeNitroModules","require","NitroModules","exports","createHybridObject","name","nitro","getNativeNitroModules","instance","getAllHybridObjectNames","hasHybridObject","hasNativeState","object","removeNativeState"],"sourceRoot":"../../src","sources":["NitroModules.ts"],"mappings":";;;;;;AAAA,IAAAA,mBAAA,GAAAC,OAAA;AAGA;AACA;AACA;;AAKA;AACA;AACA;AACO,MAAMC,YAAY,GAAAC,OAAA,CAAAD,YAAA,GAAG;EAC1B;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,kBAAkBA,CAA8BC,IAAY,EAAK;IAC/D,MAAMC,KAAK,GAAG,IAAAC,yCAAqB,EAAC,CAAC;IACrC,MAAMC,QAAQ,GAAGF,KAAK,CAACF,kBAAkB,CAACC,IAAI,CAAC;IAC/C,OAAOG,QAAQ;EACjB,CAAC;EACD;AACF;AACA;EACEC,uBAAuBA,CAAA,EAAa;IAClC,MAAMH,KAAK,GAAG,IAAAC,yCAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACG,uBAAuB,CAAC,CAAC;EACxC,CAAC;EACD;AACF;AACA;EACEC,eAAeA,CAACL,IAAY,EAAW;IACrC,MAAMC,KAAK,GAAG,IAAAC,yCAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACI,eAAe,CAACL,IAAI,CAAC;EACpC,CAAC;EACD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEM,cAAcA,CAACC,MAAc,EAAW;IACtC,MAAMN,KAAK,GAAG,IAAAC,yCAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACK,cAAc,CAACC,MAAM,CAAC;EACrC,CAAC;EACD;AACF;AACA;EACEC,iBAAiBA,CAACD,MAAc,EAAQ;IACtC,MAAMN,KAAK,GAAG,IAAAC,yCAAqB,EAAC,CAAC;IACrCD,KAAK,CAACO,iBAAiB,CAACD,MAAM,CAAC;EACjC;AACF,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["_NitroModulesTurboModule","require","NitroModules","exports","createHybridObject","name","nitro","getNativeNitroModules","instance","getAllHybridObjectNames","hasHybridObject","hasNativeState","object","removeNativeState","buildType"],"sourceRoot":"../../src","sources":["NitroModules.ts"],"mappings":";;;;;;AAAA,IAAAA,wBAAA,GAAAC,OAAA;AAGA;AACA;AACA;;AAKA;AACA;AACA;AACO,MAAMC,YAAY,GAAAC,OAAA,CAAAD,YAAA,GAAG;EAC1B;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,kBAAkBA,CAA8BC,IAAY,EAAK;IAC/D,MAAMC,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,MAAMC,QAAQ,GAAGF,KAAK,CAACF,kBAAkB,CAACC,IAAI,CAAC;IAC/C,OAAOG,QAAQ;EACjB,CAAC;EACD;AACF;AACA;EACEC,uBAAuBA,CAAA,EAAa;IAClC,MAAMH,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACG,uBAAuB,CAAC,CAAC;EACxC,CAAC;EACD;AACF;AACA;EACEC,eAAeA,CAACL,IAAY,EAAW;IACrC,MAAMC,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACI,eAAe,CAACL,IAAI,CAAC;EACpC,CAAC;EACD;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEM,cAAcA,CAACC,MAAc,EAAW;IACtC,MAAMN,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACK,cAAc,CAACC,MAAM,CAAC;EACrC,CAAC;EACD;AACF;AACA;EACEC,iBAAiBA,CAACD,MAAc,EAAQ;IACtC,MAAMN,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrCD,KAAK,CAACO,iBAAiB,CAACD,MAAM,CAAC;EACjC,CAAC;EACD;AACF;AACA;AACA;EACE,IAAIE,SAASA,CAAA,EAAwB;IACnC,MAAMR,KAAK,GAAG,IAAAC,8CAAqB,EAAC,CAAC;IACrC,OAAOD,KAAK,CAACQ,SAAS;EACxB;AACF,CAAC","ignoreList":[]}
@@ -7,6 +7,10 @@ exports.getNativeNitroModules = getNativeNitroModules;
7
7
  exports.isRuntimeAlive = isRuntimeAlive;
8
8
  var _reactNative = require("react-native");
9
9
  var _ModuleNotFoundError = require("./ModuleNotFoundError");
10
+ // This TurboModule is *not* codegen'd.
11
+ // It's handwritten, because otherwise the app's CMakeLists wants to build it.
12
+ // Instead, we want to build it ourselves and have full control over the CMakeLists.
13
+
10
14
  let turboModule;
11
15
  function getNativeNitroModules() {
12
16
  if (turboModule == null) {
@@ -27,4 +31,4 @@ function isRuntimeAlive() {
27
31
  const dispatcher = global.__nitroDispatcher;
28
32
  return cache != null && dispatcher != null;
29
33
  }
30
- //# sourceMappingURL=NativeNitroModules.js.map
34
+ //# sourceMappingURL=NitroModulesTurboModule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","_ModuleNotFoundError","turboModule","getNativeNitroModules","TurboModuleRegistry","getEnforcing","install","e","ModuleNotFoundError","isRuntimeAlive","cache","global","__nitroModulesJSICache","dispatcher","__nitroDispatcher"],"sourceRoot":"../../src","sources":["NitroModulesTurboModule.ts"],"mappings":";;;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAEA,IAAAC,oBAAA,GAAAD,OAAA;AAEA;AACA;AACA;;AAcA,IAAIE,WAAwC;AACrC,SAASC,qBAAqBA,CAAA,EAAoB;EACvD,IAAID,WAAW,IAAI,IAAI,EAAE;IACvB,IAAI;MACF;MACAA,WAAW,GACTE,gCAAmB,CAACC,YAAY,CAAkB,iBAAiB,CAAC;;MAEtE;MACAH,WAAW,CAACI,OAAO,CAAC,CAAC;IACvB,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,MAAM,IAAIC,wCAAmB,CAACD,CAAC,CAAC;IAClC;EACF;EAEA,OAAOL,WAAW;AACpB;AAOO,SAASO,cAAcA,CAAA,EAAG;EAC/B,MAAMC,KAAK,GAAGC,MAAM,CAACC,sBAAsB;EAC3C,MAAMC,UAAU,GAAGF,MAAM,CAACG,iBAAiB;EAC3C,OAAOJ,KAAK,IAAI,IAAI,IAAIG,UAAU,IAAI,IAAI;AAC5C","ignoreList":[]}