react-native-nitro-modules 0.9.2 → 0.11.0

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 (119) hide show
  1. package/NitroModules.podspec +2 -0
  2. package/README.md +4 -4
  3. package/android/CMakeLists.txt +3 -0
  4. package/android/build.gradle +26 -2
  5. package/android/src/main/cpp/JNIOnLoad.cpp +5 -5
  6. package/android/src/main/cpp/turbomodule/JNitroModules.cpp +39 -0
  7. package/android/src/main/cpp/turbomodule/JNitroModules.hpp +37 -0
  8. package/android/src/main/java/com/margelo/nitro/NitroModules.kt +69 -0
  9. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.kt +34 -0
  10. package/android/src/main/java/com/margelo/nitro/core/HybridObject.kt +6 -2
  11. package/android/src/newarch/NitroModulesSpec.kt +6 -0
  12. package/android/src/oldarch/NitroModulesSpec.kt +9 -0
  13. package/cpp/core/BoxedHybridObject.cpp +29 -0
  14. package/cpp/core/BoxedHybridObject.hpp +36 -0
  15. package/cpp/core/HybridFunction.hpp +12 -3
  16. package/cpp/entrypoint/HybridNitroModulesProxy.cpp +55 -0
  17. package/cpp/entrypoint/HybridNitroModulesProxy.hpp +48 -0
  18. package/cpp/entrypoint/InstallNitro.cpp +28 -0
  19. package/cpp/entrypoint/InstallNitro.hpp +41 -0
  20. package/cpp/jsi/JSIConverter+HostObject.hpp +73 -0
  21. package/cpp/jsi/JSIConverter+HybridObject.hpp +1 -1
  22. package/cpp/jsi/JSIConverter.hpp +1 -0
  23. package/cpp/templates/FutureType.hpp +1 -1
  24. package/cpp/templates/TypeIndex.hpp +29 -0
  25. package/cpp/utils/NitroDefines.hpp +5 -0
  26. package/ios/core/AnyMapHolder.hpp +8 -0
  27. package/ios/core/AnyMapHolder.swift +65 -39
  28. package/ios/core/HybridObjectSpec.swift +1 -1
  29. package/ios/platform/NitroLogger.mm +0 -1
  30. package/ios/turbomodule/NativeNitroModules+NewArch.mm +67 -0
  31. package/ios/turbomodule/NativeNitroModules+OldArch.mm +68 -0
  32. package/ios/turbomodule/NativeNitroModules.h +22 -0
  33. package/lib/BoxedHybridObject.d.ts +12 -0
  34. package/lib/BoxedHybridObject.js +1 -0
  35. package/lib/ModuleNotFoundError.js +3 -13
  36. package/lib/NitroModules.d.ts +1 -48
  37. package/lib/NitroModules.js +2 -67
  38. package/lib/NitroModulesProxy.d.ts +58 -0
  39. package/lib/NitroModulesProxy.js +1 -0
  40. package/lib/commonjs/BoxedHybridObject.js +6 -0
  41. package/lib/commonjs/BoxedHybridObject.js.map +1 -0
  42. package/lib/commonjs/ModuleNotFoundError.js +3 -15
  43. package/lib/commonjs/ModuleNotFoundError.js.map +1 -1
  44. package/lib/commonjs/NitroModules.js +11 -72
  45. package/lib/commonjs/NitroModules.js.map +1 -1
  46. package/lib/commonjs/NitroModulesProxy.js +6 -0
  47. package/lib/commonjs/NitroModulesProxy.js.map +1 -0
  48. package/lib/commonjs/turbomodule/NativeNitroModules.js +36 -0
  49. package/lib/commonjs/turbomodule/NativeNitroModules.js.map +1 -0
  50. package/lib/commonjs/turbomodule/NativeNitroModules.web.js +17 -0
  51. package/lib/commonjs/turbomodule/NativeNitroModules.web.js.map +1 -0
  52. package/lib/module/BoxedHybridObject.js +4 -0
  53. package/lib/module/BoxedHybridObject.js.map +1 -0
  54. package/lib/module/ModuleNotFoundError.js +3 -15
  55. package/lib/module/ModuleNotFoundError.js.map +1 -1
  56. package/lib/module/NitroModules.js +2 -72
  57. package/lib/module/NitroModules.js.map +1 -1
  58. package/lib/module/NitroModulesProxy.js +4 -0
  59. package/lib/module/NitroModulesProxy.js.map +1 -0
  60. package/lib/module/turbomodule/NativeNitroModules.js +31 -0
  61. package/lib/module/turbomodule/NativeNitroModules.js.map +1 -0
  62. package/lib/{NitroModulesTurboModule.web.js → module/turbomodule/NativeNitroModules.web.js} +9 -1
  63. package/lib/module/turbomodule/NativeNitroModules.web.js.map +1 -0
  64. package/lib/tsconfig.tsbuildinfo +1 -1
  65. package/lib/turbomodule/NativeNitroModules.d.ts +7 -0
  66. package/lib/turbomodule/NativeNitroModules.js +27 -0
  67. package/lib/turbomodule/NativeNitroModules.web.d.ts +2 -0
  68. package/lib/turbomodule/NativeNitroModules.web.js +9 -0
  69. package/lib/typescript/AnyMap.d.ts +20 -0
  70. package/lib/typescript/BoxedHybridObject.d.ts +13 -0
  71. package/lib/typescript/BoxedHybridObject.d.ts.map +1 -0
  72. package/lib/typescript/HybridObject.d.ts +99 -0
  73. package/lib/typescript/ModuleNotFoundError.d.ts +7 -0
  74. package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -1
  75. package/lib/typescript/NitroModules.d.ts +2 -0
  76. package/lib/typescript/NitroModules.d.ts.map +1 -1
  77. package/lib/typescript/NitroModulesProxy.d.ts +59 -0
  78. package/lib/typescript/NitroModulesProxy.d.ts.map +1 -0
  79. package/lib/typescript/__tests__/index.test.d.ts +1 -0
  80. package/lib/typescript/index.d.ts +4 -0
  81. package/lib/typescript/turbomodule/NativeNitroModules.d.ts +8 -0
  82. package/lib/typescript/turbomodule/NativeNitroModules.d.ts.map +1 -0
  83. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts +3 -0
  84. package/lib/typescript/turbomodule/NativeNitroModules.web.d.ts.map +1 -0
  85. package/package.json +9 -1
  86. package/src/BoxedHybridObject.ts +13 -0
  87. package/src/ModuleNotFoundError.ts +3 -19
  88. package/src/NitroModules.ts +2 -76
  89. package/src/NitroModulesProxy.ts +61 -0
  90. package/src/turbomodule/NativeNitroModules.ts +48 -0
  91. package/src/turbomodule/NativeNitroModules.web.ts +16 -0
  92. package/android/src/main/java/com/margelo/nitro/NitroModulesPackage.java +0 -30
  93. package/cpp/turbomodule/NativeNitroModules.cpp +0 -131
  94. package/cpp/turbomodule/NativeNitroModules.h +0 -8
  95. package/cpp/turbomodule/NativeNitroModules.hpp +0 -39
  96. package/cpp/turbomodule/RegisterNativeNitroModules.cpp +0 -33
  97. package/cpp/turbomodule/RegisterNativeNitroModules.hpp +0 -21
  98. package/ios/turbomodule/NitroModuleOnLoad.mm +0 -32
  99. package/lib/NativeNitroModules.d.ts +0 -17
  100. package/lib/NativeNitroModules.js +0 -22
  101. package/lib/NativeNitroModules.web.d.ts +0 -4
  102. package/lib/NativeNitroModules.web.js +0 -3
  103. package/lib/NitroModulesTurboModule.d.ts +0 -17
  104. package/lib/NitroModulesTurboModule.js +0 -23
  105. package/lib/NitroModulesTurboModule.web.d.ts +0 -1
  106. package/lib/commonjs/NitroModulesTurboModule.js +0 -34
  107. package/lib/commonjs/NitroModulesTurboModule.js.map +0 -1
  108. package/lib/commonjs/NitroModulesTurboModule.web.js +0 -11
  109. package/lib/commonjs/NitroModulesTurboModule.web.js.map +0 -1
  110. package/lib/module/NitroModulesTurboModule.js +0 -30
  111. package/lib/module/NitroModulesTurboModule.js.map +0 -1
  112. package/lib/module/NitroModulesTurboModule.web.js +0 -7
  113. package/lib/module/NitroModulesTurboModule.web.js.map +0 -1
  114. package/lib/typescript/NitroModulesTurboModule.d.ts +0 -18
  115. package/lib/typescript/NitroModulesTurboModule.d.ts.map +0 -1
  116. package/lib/typescript/NitroModulesTurboModule.web.d.ts +0 -2
  117. package/lib/typescript/NitroModulesTurboModule.web.d.ts.map +0 -1
  118. package/src/NitroModulesTurboModule.ts +0 -49
  119. package/src/NitroModulesTurboModule.web.ts +0 -7
@@ -0,0 +1,73 @@
1
+ //
2
+ // Created by Marc Rousavy on 07.10.24.
3
+ //
4
+
5
+ #pragma once
6
+
7
+ #include "IsSharedPtrTo.hpp"
8
+ #include "NitroDefines.hpp"
9
+ #include "TypeInfo.hpp"
10
+ #include <jsi/jsi.h>
11
+ #include <type_traits>
12
+
13
+ namespace margelo::nitro {
14
+
15
+ using namespace facebook;
16
+
17
+ // jsi::HostObject <> {}
18
+ template <typename T>
19
+ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::HostObject>>> final {
20
+ using TPointee = typename T::element_type;
21
+
22
+ static inline T fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
23
+ #ifdef NITRO_DEBUG
24
+ if (!arg.isObject()) [[unlikely]] {
25
+ if (arg.isUndefined()) [[unlikely]] {
26
+ throw jsi::JSError(runtime, invalidTypeErrorMessage("undefined", "It is undefined!"));
27
+ } else {
28
+ std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
29
+ throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not an object!"));
30
+ }
31
+ }
32
+ #endif
33
+ jsi::Object object = arg.asObject(runtime);
34
+
35
+ #ifdef NITRO_DEBUG
36
+ if (!object.isHostObject<TPointee>(runtime)) [[unlikely]] {
37
+ if (!object.isHostObject(runtime)) [[unlikely]] {
38
+ std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
39
+ throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is not a HostObject at all!"));
40
+ } else {
41
+ std::string stringRepresentation = arg.toString(runtime).utf8(runtime);
42
+ throw jsi::JSError(runtime, invalidTypeErrorMessage(stringRepresentation, "It is a different HostObject<T>!"));
43
+ }
44
+ }
45
+ #endif
46
+ return object.getHostObject<TPointee>(runtime);
47
+ }
48
+
49
+ static inline jsi::Value toJSI(jsi::Runtime& runtime, const T& arg) {
50
+ if (arg == nullptr) [[unlikely]] {
51
+ std::string typeName = TypeInfo::getFriendlyTypename<TPointee>();
52
+ throw jsi::JSError(runtime, "Cannot convert nullptr to HostObject<" + typeName + ">!");
53
+ }
54
+
55
+ return jsi::Object::createFromHostObject(runtime, arg);
56
+ }
57
+
58
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
59
+ if (value.isObject()) {
60
+ jsi::Object object = value.getObject(runtime);
61
+ return object.isHostObject<TPointee>(runtime);
62
+ }
63
+ return false;
64
+ }
65
+
66
+ private:
67
+ static std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
68
+ std::string typeName = TypeInfo::getFriendlyTypename<TPointee>();
69
+ return "Cannot convert \"" + typeDescription + "\" to HostObject<" + typeName + ">! " + reason;
70
+ }
71
+ };
72
+
73
+ } // namespace margelo::nitro
@@ -78,7 +78,7 @@ struct JSIConverter<T, std::enable_if_t<is_shared_ptr_to_v<T, jsi::NativeState>>
78
78
  }
79
79
 
80
80
  private:
81
- static inline std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
81
+ static std::string invalidTypeErrorMessage(const std::string& typeDescription, const std::string& reason) {
82
82
  std::string typeName = TypeInfo::getFriendlyTypename<TPointee>();
83
83
  return "Cannot convert \"" + typeDescription + "\" to NativeState<" + typeName + ">! " + reason;
84
84
  }
@@ -184,6 +184,7 @@ struct JSIConverter<std::string> final {
184
184
  #include "JSIConverter+AnyMap.hpp"
185
185
  #include "JSIConverter+ArrayBuffer.hpp"
186
186
  #include "JSIConverter+Function.hpp"
187
+ #include "JSIConverter+HostObject.hpp"
187
188
  #include "JSIConverter+HybridObject.hpp"
188
189
  #include "JSIConverter+Optional.hpp"
189
190
  #include "JSIConverter+Promise.hpp"
@@ -1,5 +1,5 @@
1
1
  //
2
- // BorrowingReference.hpp
2
+ // FutureType.hpp
3
3
  // NitroModules
4
4
  //
5
5
  // Created by Marc Rousavy on 21.06.24.
@@ -0,0 +1,29 @@
1
+ //
2
+ // TypeIndex.hpp
3
+ // NitroModules
4
+ //
5
+ // Created by Marc Rousavy on 21.06.24.
6
+ //
7
+
8
+ #pragma once
9
+
10
+ #include <tuple>
11
+ #include <type_traits>
12
+
13
+ namespace margelo::nitro {
14
+
15
+ // Gets the index of `T` in a `std::tuple<...>`.
16
+ template <typename T, typename... Ts>
17
+ struct type_index;
18
+
19
+ template <typename T, typename First, typename... Rest>
20
+ struct type_index<T, First, Rest...> {
21
+ static constexpr size_t value = std::is_same_v<T, First> ? 0 : 1 + type_index<T, Rest...>::value;
22
+ };
23
+
24
+ template <typename T>
25
+ struct type_index<T> {
26
+ static constexpr size_t value = -1; // Type not found
27
+ };
28
+
29
+ } // namespace margelo::nitro
@@ -15,6 +15,11 @@
15
15
  #ifdef NDEBUG
16
16
  #undef NITRO_DEBUG
17
17
  #endif
18
+ #ifdef ANDROID
19
+ #ifndef NDEBUG
20
+ #define NITRO_DEBUG
21
+ #endif
22
+ #endif
18
23
 
19
24
  // Helper to find out if a C++ compiler attribute is available
20
25
  #ifdef __has_attribute
@@ -88,4 +88,12 @@ inline std::vector<std::string> getAnyObjectKeys(const AnyObject& object) {
88
88
  return keys;
89
89
  }
90
90
 
91
+ inline AnyValue getAnyObjectValue(const AnyObject& object, const std::string& key) {
92
+ auto item = object.find(key);
93
+ if (item == object.end()) {
94
+ throw std::runtime_error("Couldn't find " + key + " in AnyObject!");
95
+ }
96
+ return item->second;
97
+ }
98
+
91
99
  } // namespace margelo::nitro
@@ -11,12 +11,13 @@ import Foundation
11
11
  * Represents any value representable by the `AnyMap`.
12
12
  * Note: Arrays are currently not implemented due to a Swift compiler bug https://github.com/swiftlang/swift/issues/75994
13
13
  */
14
- public enum AnyValue {
14
+ public indirect enum AnyValue {
15
15
  case null
16
16
  case number(Double)
17
17
  case bool(Bool)
18
18
  case bigint(Int64)
19
19
  case string(String)
20
+ case array(Array<AnyValue>)
20
21
  case object(Dictionary<String, AnyValue>)
21
22
 
22
23
  static func create(_ value: margelo.nitro.AnyValue) -> AnyValue {
@@ -30,6 +31,8 @@ public enum AnyValue {
30
31
  return .bigint(margelo.nitro.get_AnyValue_bigint(value))
31
32
  } else if margelo.nitro.is_AnyValue_string(value) {
32
33
  return .string(margelo.nitro.get_AnyValue_string(value).toSwift())
34
+ } else if margelo.nitro.is_AnyValue_AnyArray(value) {
35
+ return .array(margelo.nitro.get_AnyValue_AnyArray(value).toSwift())
33
36
  } else if margelo.nitro.is_AnyValue_AnyObject(value) {
34
37
  return .object(margelo.nitro.get_AnyValue_AnyObject(value).toSwift())
35
38
  } else {
@@ -42,22 +45,18 @@ public enum AnyValue {
42
45
  * Represents an `AnyMap` that can be passed to Swift.
43
46
  */
44
47
  public class AnyMapHolder {
45
- let _cppPart: margelo.nitro.TSharedMap
46
-
47
- public var cppPart: margelo.nitro.TSharedMap {
48
- return _cppPart
49
- }
48
+ public let cppPart: margelo.nitro.TSharedMap
50
49
 
51
50
  public init() {
52
- _cppPart = margelo.nitro.AnyMap.make()
51
+ cppPart = margelo.nitro.AnyMap.make()
53
52
  }
54
53
 
55
54
  public init(withPreallocatedSize size: Int) {
56
- _cppPart = margelo.nitro.AnyMap.make(size)
55
+ cppPart = margelo.nitro.AnyMap.make(size)
57
56
  }
58
57
 
59
- public init(withCppPart cppPart: margelo.nitro.TSharedMap) {
60
- _cppPart = cppPart
58
+ public init(withCppPart otherCppPart: margelo.nitro.TSharedMap) {
59
+ cppPart = otherCppPart
61
60
  }
62
61
 
63
62
  // pragma MARK: Common Operations
@@ -66,21 +65,21 @@ public class AnyMapHolder {
66
65
  * Returns whether the given key exists in the map.
67
66
  */
68
67
  public func contains(key: String) -> Bool {
69
- return _cppPart.pointee.contains(std.string(key))
68
+ return cppPart.pointee.contains(std.string(key))
70
69
  }
71
70
 
72
71
  /**
73
72
  * Removes the given key from the map.
74
73
  */
75
74
  public func remove(key: String) {
76
- _cppPart.pointee.remove(std.string(key))
75
+ cppPart.pointee.remove(std.string(key))
77
76
  }
78
77
 
79
78
  /**
80
79
  * Removes all keys in this map.
81
80
  */
82
81
  public func clear() {
83
- _cppPart.pointee.clear()
82
+ cppPart.pointee.clear()
84
83
  }
85
84
 
86
85
  // pragma MARK: Getters
@@ -91,44 +90,54 @@ public class AnyMapHolder {
91
90
  * this function throws.
92
91
  */
93
92
  public func getDouble(key: String) -> Double {
94
- return _cppPart.pointee.getDouble(std.string(key))
93
+ return cppPart.pointee.getDouble(std.string(key))
95
94
  }
96
95
 
97
96
  /**
98
- * Gets the double value at the given key.
97
+ * Gets the boolean value at the given key.
99
98
  * If no value exists at the given key, or if it is not a double,
100
99
  * this function throws.
101
100
  */
102
101
  public func getBoolean(key: String) -> Bool {
103
- return _cppPart.pointee.getBoolean(std.string(key))
102
+ return cppPart.pointee.getBoolean(std.string(key))
104
103
  }
105
104
 
106
105
  /**
107
- * Gets the double value at the given key.
106
+ * Gets the bigint value at the given key.
108
107
  * If no value exists at the given key, or if it is not a double,
109
108
  * this function throws.
110
109
  */
111
110
  public func getBigInt(key: String) -> Int64 {
112
- return _cppPart.pointee.getBigInt(std.string(key))
111
+ return cppPart.pointee.getBigInt(std.string(key))
113
112
  }
114
113
 
115
114
  /**
116
- * Gets the double value at the given key.
115
+ * Gets the string value at the given key.
117
116
  * If no value exists at the given key, or if it is not a double,
118
117
  * this function throws.
119
118
  */
120
119
  public func getString(key: String) -> String {
121
- let value = _cppPart.pointee.getString(std.string(key))
120
+ let value = cppPart.pointee.getString(std.string(key))
122
121
  return String(value)
123
122
  }
124
-
123
+
125
124
  /**
126
- * Gets the double value at the given key.
125
+ * Gets the array value at the given key.
126
+ * If no value exists at the given key, or if it is not a double,
127
+ * this function throws.
128
+ */
129
+ public func getArray(key: String) -> [AnyValue] {
130
+ let value = cppPart.pointee.getArray(std.string(key))
131
+ return value.toSwift()
132
+ }
133
+
134
+ /**
135
+ * Gets the object value at the given key.
127
136
  * If no value exists at the given key, or if it is not a double,
128
137
  * this function throws.
129
138
  */
130
139
  public func getObject(key: String) -> Dictionary<String, AnyValue> {
131
- let value = _cppPart.pointee.getObject(std.string(key))
140
+ let value = cppPart.pointee.getObject(std.string(key))
132
141
  return value.toSwift()
133
142
  }
134
143
 
@@ -138,42 +147,49 @@ public class AnyMapHolder {
138
147
  * Set the given key to `null`.
139
148
  */
140
149
  public func setNull(key: String) {
141
- _cppPart.pointee.setNull(std.string(key))
150
+ cppPart.pointee.setNull(std.string(key))
142
151
  }
143
152
 
144
153
  /**
145
154
  * Set the given key to the given double value.
146
155
  */
147
156
  public func setDouble(key: String, value: Double) {
148
- _cppPart.pointee.setDouble(std.string(key), value)
157
+ cppPart.pointee.setDouble(std.string(key), value)
149
158
  }
150
159
 
151
160
  /**
152
161
  * Set the given key to the given boolean value.
153
162
  */
154
163
  public func setBoolean(key: String, value: Bool) {
155
- _cppPart.pointee.setBoolean(std.string(key), value)
164
+ cppPart.pointee.setBoolean(std.string(key), value)
156
165
  }
157
166
 
158
167
  /**
159
168
  * Set the given key to the given bigint value.
160
169
  */
161
170
  public func setBigInt(key: String, value: Int64) {
162
- _cppPart.pointee.setBigInt(std.string(key), value)
171
+ cppPart.pointee.setBigInt(std.string(key), value)
163
172
  }
164
173
 
165
174
  /**
166
175
  * Set the given key to the given string value.
167
176
  */
168
177
  public func setString(key: String, value: String) {
169
- _cppPart.pointee.setString(std.string(key), std.string(value))
178
+ cppPart.pointee.setString(std.string(key), std.string(value))
170
179
  }
171
-
180
+
181
+ /**
182
+ * Set the given key to the given array value.
183
+ */
184
+ public func setArray(key: String, value: [AnyValue]) {
185
+ cppPart.pointee.setArray(std.string(key), margelo.nitro.AnyArray.create(value))
186
+ }
187
+
172
188
  /**
173
189
  * Set the given key to the given object value.
174
190
  */
175
191
  public func setObject(key: String, value: Dictionary<String, AnyValue>) {
176
- _cppPart.pointee.setObject(std.string(key), margelo.nitro.AnyObject.create(value))
192
+ cppPart.pointee.setObject(std.string(key), margelo.nitro.AnyObject.create(value))
177
193
  }
178
194
 
179
195
  // pragma MARK: Is Getters
@@ -182,42 +198,49 @@ public class AnyMapHolder {
182
198
  * Gets whether the given `key` is holding a null value, or not.
183
199
  */
184
200
  public func isNull(key: String) -> Bool {
185
- return _cppPart.pointee.isNull(std.string(key))
201
+ return cppPart.pointee.isNull(std.string(key))
186
202
  }
187
203
 
188
204
  /**
189
205
  * Gets whether the given `key` is holding a double value, or not.
190
206
  */
191
207
  public func isDouble(key: String) -> Bool {
192
- return _cppPart.pointee.isDouble(std.string(key))
208
+ return cppPart.pointee.isDouble(std.string(key))
193
209
  }
194
210
 
195
211
  /**
196
212
  * Gets whether the given `key` is holding a boolean value, or not.
197
213
  */
198
214
  public func isBool(key: String) -> Bool {
199
- return _cppPart.pointee.isBoolean(std.string(key))
215
+ return cppPart.pointee.isBoolean(std.string(key))
200
216
  }
201
217
 
202
218
  /**
203
219
  * Gets whether the given `key` is holding a bigint value, or not.
204
220
  */
205
221
  public func isBigInt(key: String) -> Bool {
206
- return _cppPart.pointee.isBigInt(std.string(key))
222
+ return cppPart.pointee.isBigInt(std.string(key))
207
223
  }
208
224
 
209
225
  /**
210
226
  * Gets whether the given `key` is holding a string value, or not.
211
227
  */
212
228
  public func isString(key: String) -> Bool {
213
- return _cppPart.pointee.isString(std.string(key))
229
+ return cppPart.pointee.isString(std.string(key))
214
230
  }
215
-
231
+
216
232
  /**
217
- * Gets whether the given `key` is holding a object value, or not.
233
+ * Gets whether the given `key` is holding an array value, or not.
234
+ */
235
+ public func isArray(key: String) -> Bool {
236
+ return cppPart.pointee.isArray(std.string(key))
237
+ }
238
+
239
+ /**
240
+ * Gets whether the given `key` is holding an object value, or not.
218
241
  */
219
242
  public func isObject(key: String) -> Bool {
220
- return _cppPart.pointee.isObject(std.string(key))
243
+ return cppPart.pointee.isObject(std.string(key))
221
244
  }
222
245
  }
223
246
 
@@ -236,6 +259,8 @@ extension margelo.nitro.AnyValue {
236
259
  return create(bigint)
237
260
  case .string(let string):
238
261
  return create(string)
262
+ case .array(let array):
263
+ return create(array)
239
264
  case .object(let object):
240
265
  return create(object)
241
266
  }
@@ -309,7 +334,8 @@ extension margelo.nitro.AnyObject {
309
334
  let keys = margelo.nitro.getAnyObjectKeys(self)
310
335
  var dictionary = Dictionary<String, AnyValue>(minimumCapacity: keys.size())
311
336
  for key in keys {
312
-
337
+ let value = margelo.nitro.getAnyObjectValue(self, key)
338
+ dictionary[String(key)] = AnyValue.create(value)
313
339
  }
314
340
  return dictionary
315
341
  }
@@ -10,7 +10,7 @@ import Foundation
10
10
  /**
11
11
  * A base protocol for all Swift-based Hybrid Objects.
12
12
  */
13
- public protocol HybridObjectSpec {
13
+ public protocol HybridObjectSpec: AnyObject {
14
14
  /**
15
15
  * Holds the C++ HybridObject and it's context.
16
16
  * Use the default initializer in your implementation, C++ will set and get this value.
@@ -6,7 +6,6 @@
6
6
  //
7
7
 
8
8
  #include "NitroLogger.hpp"
9
- #include "AnyMapHolder.hpp"
10
9
  #include "NitroDefines.hpp"
11
10
  #include <Foundation/Foundation.h>
12
11
 
@@ -0,0 +1,67 @@
1
+ //
2
+ // NativeNitroModules+NewArch.mm
3
+ // DoubleConversion
4
+ //
5
+ // Created by Marc Rousavy on 21.06.24.
6
+ //
7
+
8
+ #import "NativeNitroModules.h"
9
+
10
+ #ifdef RCT_NEW_ARCH_ENABLED
11
+
12
+ #import "CallInvokerDispatcher.hpp"
13
+ #import "InstallNitro.hpp"
14
+
15
+ #import <ReactCommon/CallInvoker.h>
16
+ #import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
17
+
18
+ using namespace facebook;
19
+ using namespace margelo;
20
+
21
+ // Make NativeNitroModules comply to RCTTurboModuleWithJSIBindings
22
+ @interface NativeNitroModules () <RCTTurboModuleWithJSIBindings>
23
+ @end
24
+
25
+ /**
26
+ * NativeNitroModules implementation for the new architecture.
27
+ * This uses `installJSIBindingsWithRuntime:` to install the `global.NitroModulesProxy` into the JS Runtime.
28
+ */
29
+ @implementation NativeNitroModules {
30
+ bool _didInstall;
31
+ std::weak_ptr<react::CallInvoker> _callInvoker;
32
+ }
33
+ RCT_EXPORT_MODULE(NitroModules)
34
+
35
+ - (void)installJSIBindingsWithRuntime:(jsi::Runtime&)runtime {
36
+ // 1. Get CallInvoker we cached statically
37
+ auto callInvoker = _callInvoker.lock();
38
+ if (callInvoker == nullptr) {
39
+ throw std::runtime_error("Cannot install global.NitroModulesProxy - CallInvoker was null!");
40
+ }
41
+
42
+ // 2. Wrap CallInvoker as Dispatcher
43
+ auto dispatcher = std::make_shared<nitro::CallInvokerDispatcher>(callInvoker);
44
+
45
+ // 3. Install Nitro
46
+ nitro::install(runtime, dispatcher);
47
+ _didInstall = true;
48
+ }
49
+
50
+ - (NSString*)install {
51
+ if (_didInstall) {
52
+ // installJSIBindingsWithRuntime ran successfully.
53
+ return nil;
54
+ } else {
55
+ return @"installJSIBindingsWithRuntime: was not called - JSI Bindings could not be installed!";
56
+ }
57
+ }
58
+
59
+ - (std::shared_ptr<react::TurboModule>)getTurboModule:(const react::ObjCTurboModule::InitParams&)params {
60
+ // Cache the CallInvoker statically (weak) - we use it later in `installJSIBindingsWithRuntime:`.
61
+ _callInvoker = params.jsInvoker;
62
+ return std::make_shared<react::NativeNitroModulesSpecJSI>(params);
63
+ }
64
+
65
+ @end
66
+
67
+ #endif
@@ -0,0 +1,68 @@
1
+ //
2
+ // NativeNitroModules+OldArch.mm
3
+ // DoubleConversion
4
+ //
5
+ // Created by Marc Rousavy on 21.06.24.
6
+ //
7
+
8
+ #import "NativeNitroModules.h"
9
+
10
+ #ifndef RCT_NEW_ARCH_ENABLED
11
+
12
+ #import "CallInvokerDispatcher.hpp"
13
+ #import "InstallNitro.hpp"
14
+
15
+ #import <React/RCTBridge+Private.h>
16
+ #import <React/RCTBridge.h>
17
+
18
+ using namespace facebook;
19
+ using namespace margelo;
20
+
21
+ // forward-declaration (private API)
22
+ @interface RCTBridge (JSIRuntime)
23
+ - (void*)runtime;
24
+ - (std::shared_ptr<react::CallInvoker>)jsCallInvoker;
25
+ @end
26
+
27
+ /**
28
+ * NativeNitroModules implementation for the old architecture.
29
+ * This uses `RCTBridge` to grab the `jsi::Runtime` and `react::CallInvoker`.
30
+ */
31
+ @implementation NativeNitroModules
32
+ RCT_EXPORT_MODULE(NitroModules)
33
+
34
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
35
+ try {
36
+ // 1. Cast RCTBridge to a RCTCxxBridge (ObjC)
37
+ RCTCxxBridge* cxxBridge = (RCTCxxBridge*)RCTBridge.currentBridge;
38
+ if (!cxxBridge) {
39
+ return @"RCTBridge is not a RCTCxxBridge!";
40
+ }
41
+
42
+ // 2. Access jsi::Runtime and cast from void*
43
+ jsi::Runtime* runtime = reinterpret_cast<jsi::Runtime*>(cxxBridge.runtime);
44
+ if (!runtime) {
45
+ return @"jsi::Runtime on RCTCxxBridge was null!";
46
+ }
47
+
48
+ // 3. Access react::CallInvoker
49
+ std::shared_ptr<react::CallInvoker> callInvoker = cxxBridge.jsCallInvoker;
50
+ if (!callInvoker) {
51
+ return @"react::CallInvoker on RCTCxxBridge was null!";
52
+ }
53
+
54
+ // 4. Wrap react::CallInvoker in nitro::Dispatcher
55
+ auto dispatcher = std::make_shared<nitro::CallInvokerDispatcher>(callInvoker);
56
+
57
+ // 5. Install Nitro
58
+ nitro::install(*runtime, dispatcher);
59
+ return nil;
60
+ } catch (std::runtime_error& error) {
61
+ // ?. Any C++ error occurred (probably in nitro::install()?)
62
+ return [NSString stringWithCString:error.what() encoding:kCFStringEncodingUTF8];
63
+ }
64
+ }
65
+
66
+ @end
67
+
68
+ #endif
@@ -0,0 +1,22 @@
1
+ //
2
+ // NativeNitroModules.h
3
+ // NitroModules
4
+ //
5
+ // Created by Marc Rousavy on 07.10.24.
6
+ //
7
+
8
+ #ifdef RCT_NEW_ARCH_ENABLED
9
+
10
+ // New Architecture uses the Codegen'd Spec (TurboModule)
11
+ #import "NitroModulesSpec.h"
12
+ @interface NativeNitroModules : NSObject <NativeNitroModulesSpec>
13
+ @end
14
+
15
+ #else
16
+
17
+ // Old Architecture is an untyped RCTBridgeModule
18
+ #import <React/RCTBridgeModule.h>
19
+ @interface NativeNitroModules : NSObject <RCTBridgeModule>
20
+ @end
21
+
22
+ #endif
@@ -0,0 +1,12 @@
1
+ import type { HybridObject } from './HybridObject';
2
+ /**
3
+ * Represents a boxed {@linkcode HybridObject} that can later be unboxed again.
4
+ * This is implemented as a `jsi::HostObject`.
5
+ */
6
+ export interface BoxedHybridObject<T extends HybridObject> {
7
+ /**
8
+ * Unboxes the {@linkcode HybridObject}.
9
+ * This can be called from a different Runtime than the one it was boxed in.
10
+ */
11
+ unbox(): T;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -20,26 +20,16 @@ function getFrameworkType() {
20
20
  }
21
21
  export class ModuleNotFoundError extends Error {
22
22
  constructor(cause) {
23
- // TurboModule not found, something went wrong!
24
- if (global.__turboModuleProxy == null) {
25
- // TurboModules are not available/new arch is not enabled.
26
- const message = 'Failed to get NitroModules: NitroModules require the new architecture to be enabled!';
27
- const suggestions = [];
28
- suggestions.push('Enable the new architecture in your app to use NitroModules. (See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md)');
29
- const error = messageWithSuggestions(message, suggestions);
30
- super(error, { cause: cause });
31
- return;
32
- }
33
23
  const framework = getFrameworkType();
34
24
  if (framework === 'expo-go') {
35
25
  super('NitroModules are not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.');
36
26
  return;
37
27
  }
38
- const message = 'Failed to get NitroModules: The native "NitroModules" TurboModule could not be found.';
28
+ const message = 'Failed to get NitroModules: The native "NitroModules" Turbo/Native-Module could not be found.';
39
29
  const suggestions = [];
40
30
  suggestions.push('Make sure react-native-nitro-modules/NitroModules is correctly autolinked (run `npx react-native config` to verify)');
41
- suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModulesCxx specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
42
- suggestions.push('Make sure you are using react-native 0.74.0 or higher, because NitroModules are built with C++ TurboModules.');
31
+ suggestions.push('Make sure you enabled the new architecture (TurboModules) and CodeGen properly generated the "NativeNitroModules"/NitroModules specs. See https://github.com/reactwg/react-native-new-architecture/blob/main/docs/enable-apps.md');
32
+ suggestions.push('Make sure you are using react-native 0.74.0 or higher.');
43
33
  suggestions.push('Make sure you rebuilt the app.');
44
34
  if (framework === 'expo') {
45
35
  suggestions.push('Make sure you ran `expo prebuild`.');