react-native-nitro-modules 0.27.4 → 0.27.6
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/src/main/cpp/core/JHybridObject.cpp +38 -0
- package/android/src/main/cpp/core/JHybridObject.hpp +13 -1
- package/android/src/main/cpp/utils/JNISharedPtr.hpp +3 -0
- package/cpp/core/HybridObject.cpp +11 -1
- package/cpp/core/HybridObject.hpp +7 -3
- package/cpp/utils/NitroDefines.hpp +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
JHybridObject::~JHybridObject() {
|
|
34
|
+
// Hermes GC can destroy JS objects on a non-JNI Thread.
|
|
35
|
+
jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
} // namespace margelo::nitro
|
|
@@ -23,9 +23,21 @@ 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
|
-
|
|
26
|
+
public:
|
|
27
|
+
// C++ constructor (called from Java via `initHybrid()`)
|
|
28
|
+
explicit JHybridObject(jni::alias_ref<jhybridobject> jThis) : _javaPart(jni::make_global(jThis)) {}
|
|
29
|
+
// C++ default constructor used by older Nitro versions (deprecated in favor of the jThis one)
|
|
30
|
+
[[deprecated]] JHybridObject() = default;
|
|
31
|
+
|
|
32
|
+
public:
|
|
33
|
+
~JHybridObject() override;
|
|
34
|
+
|
|
35
|
+
public:
|
|
36
|
+
// `shared()` has custom logic because we ref-count using `jni::global_ref`!
|
|
37
|
+
std::shared_ptr<HybridObject> shared() override;
|
|
27
38
|
|
|
28
39
|
private:
|
|
40
|
+
jni::global_ref<JHybridObject::javaobject> _javaPart;
|
|
29
41
|
friend HybridBase;
|
|
30
42
|
};
|
|
31
43
|
|
|
@@ -49,6 +49,9 @@ public:
|
|
|
49
49
|
public:
|
|
50
50
|
/**
|
|
51
51
|
* Creates a new `std::shared_ptr<T>` from the given `jni::global_ref<T::javaobject>`.
|
|
52
|
+
* Note: This does not perform any caching and will just re-create the shared_ptr control-block
|
|
53
|
+
* each time. It is not safe to call this multiple times if you use enable_shared_from_this.
|
|
54
|
+
* Instead, use HybridObject::shared().
|
|
52
55
|
*/
|
|
53
56
|
template <typename T, typename std::enable_if<is_base_template_of<T, jni::HybridClass>::value, int>::type = 0>
|
|
54
57
|
static std::shared_ptr<T> make_shared_from_jni(const jni::global_ref<typename T::javaobject>& ref) {
|
|
@@ -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
|
-
std::shared_ptr<Derived>
|
|
69
|
-
return std::dynamic_pointer_cast<Derived>(
|
|
68
|
+
std::shared_ptr<Derived> shared_cast() {
|
|
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.6",
|
|
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",
|