react-native-nitro-modules 0.27.3 → 0.27.5
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.
- package/android/build.gradle +1 -1
- package/android/src/main/cpp/core/JHybridObject.cpp +33 -0
- package/android/src/main/cpp/core/JHybridObject.hpp +8 -0
- package/cpp/core/HybridObject.cpp +11 -1
- package/cpp/core/HybridObject.hpp +6 -2
- package/cpp/utils/NitroDefines.hpp +1 -1
- package/package.json +2 -2
package/android/build.gradle
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//
|
|
2
|
+
// JHybridObject.cpp
|
|
3
|
+
// react-native-nitro
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 11.08.25.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#include "JHybridObject.hpp"
|
|
9
|
+
#include "JNISharedPtr.hpp"
|
|
10
|
+
#include <fbjni/fbjni.h>
|
|
11
|
+
|
|
12
|
+
namespace margelo::nitro {
|
|
13
|
+
|
|
14
|
+
using namespace facebook;
|
|
15
|
+
|
|
16
|
+
std::shared_ptr<HybridObject> JHybridObject::shared() {
|
|
17
|
+
if (auto shared = weak_from_this().lock()) {
|
|
18
|
+
// We have a cached shared_ptr
|
|
19
|
+
return shared;
|
|
20
|
+
}
|
|
21
|
+
if (_javaPart == nullptr) [[unlikely]] {
|
|
22
|
+
// We don't have a _javaPart! Maybe the implementing JHybridObject
|
|
23
|
+
// was generated with an old version of nitrogen which doesn't call the JHybridObject(…)
|
|
24
|
+
// constructor yet. This is bad!
|
|
25
|
+
throw std::runtime_error(std::string("JHybridObject \"") + getName() +
|
|
26
|
+
"\" does not have a _javaPart, "
|
|
27
|
+
"and wasn't constructed from a std::shared_ptr! "
|
|
28
|
+
"Try upgrading nitrogen and re-generate your specs.");
|
|
29
|
+
}
|
|
30
|
+
return JNISharedPtr::make_shared_from_jni<JHybridObject>(_javaPart);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
} // namespace margelo::nitro
|
|
@@ -23,9 +23,17 @@ class JHybridObject : public jni::HybridClass<JHybridObject>, public virtual Hyb
|
|
|
23
23
|
public:
|
|
24
24
|
static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/core/HybridObject;";
|
|
25
25
|
|
|
26
|
+
// C++ constructor (called from Java via `initHybrid()`)
|
|
27
|
+
explicit JHybridObject(jni::alias_ref<jhybridobject> jThis) : _javaPart(jni::make_global(jThis)) {}
|
|
28
|
+
// C++ default constructor used by older Nitro versions (deprecated in favor of the jThis one)
|
|
29
|
+
[[deprecated]] JHybridObject() = default;
|
|
26
30
|
~JHybridObject() override = default;
|
|
27
31
|
|
|
32
|
+
// `shared()` has custom logic because we ref-count using `jni::global_ref`!
|
|
33
|
+
std::shared_ptr<HybridObject> shared() override;
|
|
34
|
+
|
|
28
35
|
private:
|
|
36
|
+
jni::global_ref<JHybridObject::javaobject> _javaPart;
|
|
29
37
|
friend HybridBase;
|
|
30
38
|
};
|
|
31
39
|
|
|
@@ -22,6 +22,16 @@ bool HybridObject::equals(const std::shared_ptr<HybridObject>& other) {
|
|
|
22
22
|
return this == other.get();
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
std::shared_ptr<HybridObject> HybridObject::shared() {
|
|
26
|
+
std::weak_ptr<HybridObject> weak = weak_from_this();
|
|
27
|
+
if (auto shared = weak.lock()) [[likely]] {
|
|
28
|
+
// We can lock to a shared_ptr!
|
|
29
|
+
return shared;
|
|
30
|
+
}
|
|
31
|
+
// We are not managed inside a shared_ptr..
|
|
32
|
+
throw std::runtime_error(std::string("HybridObject \"") + _name + "\" is not managed inside a std::shared_ptr - cannot access shared()!");
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
jsi::Value HybridObject::disposeRaw(jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value*, size_t) {
|
|
26
36
|
// 1. Dispose any resources - this might be overridden by child classes to perform manual cleanup.
|
|
27
37
|
dispose();
|
|
@@ -71,7 +81,7 @@ jsi::Value HybridObject::toObject(jsi::Runtime& runtime) {
|
|
|
71
81
|
jsi::Object object = create.call(runtime, prototype).asObject(runtime);
|
|
72
82
|
|
|
73
83
|
// 5. Assign NativeState to the object so the prototype can resolve the native methods
|
|
74
|
-
object.setNativeState(runtime,
|
|
84
|
+
object.setNativeState(runtime, shared());
|
|
75
85
|
|
|
76
86
|
// 6. Set memory size so Hermes GC knows about actual memory
|
|
77
87
|
object.setExternalMemoryPressure(runtime, getExternalMemorySize());
|
|
@@ -61,13 +61,17 @@ public:
|
|
|
61
61
|
|
|
62
62
|
public:
|
|
63
63
|
/**
|
|
64
|
-
* Get the `std::shared_ptr` instance of this HybridObject.
|
|
64
|
+
* Get the `std::shared_ptr` instance of this HybridObject as it's concrete type.
|
|
65
65
|
* The HybridObject must be managed inside a `shared_ptr` already, otherwise this will fail.
|
|
66
66
|
*/
|
|
67
67
|
template <typename Derived>
|
|
68
68
|
std::shared_ptr<Derived> shared() {
|
|
69
|
-
return std::dynamic_pointer_cast<Derived>(
|
|
69
|
+
return std::dynamic_pointer_cast<Derived>(shared());
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Get the `std::shared_ptr` instance of this HybridObject.
|
|
73
|
+
*/
|
|
74
|
+
virtual std::shared_ptr<HybridObject> shared();
|
|
71
75
|
|
|
72
76
|
public:
|
|
73
77
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-modules",
|
|
3
|
-
"version": "0.27.
|
|
3
|
+
"version": "0.27.5",
|
|
4
4
|
"description": "Insanely fast native C++, Swift or Kotlin modules with a statically compiled binding layer to JSI.",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
"@types/react": "*",
|
|
76
76
|
"jest": "*",
|
|
77
77
|
"react": "19.1.0",
|
|
78
|
-
"react-native": "0.80.
|
|
78
|
+
"react-native": "0.80.2",
|
|
79
79
|
"react-native-builder-bob": "^0.37.0"
|
|
80
80
|
},
|
|
81
81
|
"peerDependencies": {
|