react-native-nitro-modules 0.0.2 → 0.0.4
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/NitroModules.podspec +49 -0
- package/android/CMakeLists.txt +44 -7
- package/android/build.gradle +28 -24
- package/cpp/core/AnyMap.cpp +181 -0
- package/cpp/core/AnyMap.hpp +191 -0
- package/cpp/core/HybridContext.hpp +51 -0
- package/cpp/core/HybridObject.cpp +220 -0
- package/cpp/core/HybridObject.hpp +241 -0
- package/cpp/core/PointerHolder.hpp +93 -0
- package/cpp/jsi/ArrayBuffer.hpp +79 -0
- package/cpp/jsi/JSICache.hpp +145 -0
- package/cpp/jsi/JSIConverter.hpp +610 -0
- package/cpp/jsi/Promise.cpp +54 -0
- package/cpp/jsi/Promise.hpp +54 -0
- package/cpp/platform/ThreadUtils.hpp +23 -0
- package/cpp/registry/HybridObjectRegistry.cpp +57 -0
- package/cpp/registry/HybridObjectRegistry.hpp +44 -0
- package/cpp/test-object/TestHybridObject.cpp +37 -0
- package/cpp/test-object/TestHybridObject.hpp +87 -0
- package/cpp/threading/CallInvokerDispatcher.hpp +33 -0
- package/cpp/threading/Dispatcher.cpp +56 -0
- package/cpp/threading/Dispatcher.hpp +82 -0
- package/cpp/turbomodule/NativeNitroModules.cpp +70 -0
- package/cpp/turbomodule/NativeNitroModules.h +7 -0
- package/cpp/turbomodule/NativeNitroModules.hpp +35 -0
- package/cpp/turbomodule/RegisterNativeNitroModules.cpp +33 -0
- package/cpp/turbomodule/RegisterNativeNitroModules.hpp +21 -0
- package/cpp/utils/BorrowingReference+Owning.hpp +34 -0
- package/cpp/utils/BorrowingReference.hpp +115 -0
- package/cpp/utils/DoesClassExist.hpp +23 -0
- package/cpp/utils/GetRuntimeID.hpp +28 -0
- package/cpp/utils/NitroDefines.hpp +32 -0
- package/cpp/utils/NitroHash.hpp +42 -0
- package/cpp/utils/NitroLogger.hpp +55 -0
- package/cpp/utils/OwningLock.hpp +54 -0
- package/cpp/utils/OwningReference.hpp +214 -0
- package/cpp/utils/TypeInfo.hpp +81 -0
- package/ios/core/HybridObjectSpec.swift +52 -0
- package/ios/core/RuntimeError.swift +17 -0
- package/ios/platform/ThreadUtils.cpp +28 -0
- package/ios/turbomodule/NitroModuleOnLoad.mm +31 -0
- package/lib/AnyMap.d.ts +16 -0
- package/lib/AnyMap.js +1 -0
- package/lib/HybridObject.d.ts +57 -0
- package/lib/HybridObject.js +1 -0
- package/lib/ModuleNotFoundError.d.ts +6 -0
- package/lib/ModuleNotFoundError.js +61 -0
- package/lib/NativeNitro.d.ts +8 -0
- package/lib/NativeNitro.js +3 -0
- package/lib/NativeNitroModules.d.ts +7 -0
- package/lib/NativeNitroModules.js +17 -0
- package/lib/NitroModules.d.ts +17 -0
- package/lib/NitroModules.js +21 -0
- package/lib/__tests__/index.test.d.ts +0 -0
- package/lib/__tests__/index.test.js +2 -0
- package/lib/commonjs/AnyMap.js +2 -0
- package/lib/commonjs/AnyMap.js.map +1 -0
- package/lib/commonjs/HybridObject.js +2 -0
- package/lib/commonjs/HybridObject.js.map +1 -0
- package/lib/commonjs/ModuleNotFoundError.js +72 -0
- package/lib/commonjs/ModuleNotFoundError.js.map +1 -0
- package/lib/commonjs/NativeNitroModules.js +24 -0
- package/lib/commonjs/NativeNitroModules.js.map +1 -0
- package/lib/commonjs/NitroModules.js +32 -0
- package/lib/commonjs/NitroModules.js.map +1 -0
- package/lib/commonjs/createTestObject.js +15 -0
- package/lib/commonjs/createTestObject.js.map +1 -0
- package/lib/commonjs/index.js +44 -5
- package/lib/commonjs/index.js.map +1 -1
- package/lib/createTestObject.d.ts +22 -0
- package/lib/createTestObject.js +7 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +4 -0
- package/lib/module/AnyMap.js +2 -0
- package/lib/module/AnyMap.js.map +1 -0
- package/lib/module/HybridObject.js +2 -0
- package/lib/module/HybridObject.js.map +1 -0
- package/lib/module/ModuleNotFoundError.js +65 -0
- package/lib/module/ModuleNotFoundError.js.map +1 -0
- package/lib/module/NativeNitroModules.js +18 -0
- package/lib/module/NativeNitroModules.js.map +1 -0
- package/lib/module/NitroModules.js +27 -0
- package/lib/module/NitroModules.js.map +1 -0
- package/lib/module/createTestObject.js +8 -0
- package/lib/module/createTestObject.js.map +1 -0
- package/lib/module/index.js +4 -4
- package/lib/module/index.js.map +1 -1
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +76 -49
- package/react-native.config.js +16 -0
- package/src/AnyMap.ts +22 -0
- package/src/HybridObject.ts +58 -0
- package/src/ModuleNotFoundError.ts +90 -0
- package/src/NativeNitroModules.ts +26 -0
- package/src/NitroModules.ts +30 -0
- package/src/__tests__/index.test.tsx +1 -0
- package/src/createTestObject.ts +40 -0
- package/src/index.ts +4 -0
- package/LICENSE +0 -20
- package/README.md +0 -32
- package/android/cpp-adapter.cpp +0 -8
- package/android/src/main/AndroidManifest.xml +0 -3
- package/android/src/main/AndroidManifestNew.xml +0 -2
- package/android/src/main/java/com/nitro/NitroModule.java +0 -34
- package/android/src/main/java/com/nitro/NitroPackage.java +0 -44
- package/cpp/react-native-nitro.cpp +0 -7
- package/cpp/react-native-nitro.h +0 -8
- package/ios/Nitro.h +0 -15
- package/ios/Nitro.mm +0 -21
- package/lib/commonjs/NativeNitro.js +0 -9
- package/lib/commonjs/NativeNitro.js.map +0 -1
- package/lib/module/NativeNitro.js +0 -3
- package/lib/module/NativeNitro.js.map +0 -1
- package/lib/typescript/src/NativeNitro.d.ts +0 -7
- package/lib/typescript/src/NativeNitro.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -2
- package/lib/typescript/src/index.d.ts.map +0 -1
- package/react-native-nitro.podspec +0 -41
- package/src/NativeNitro.ts +0 -8
- package/src/index.tsx +0 -5
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
//
|
|
2
|
+
// JSICache.hpp
|
|
3
|
+
// NitroModules
|
|
4
|
+
//
|
|
5
|
+
// Created by Marc Rousavy on 20.06.24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#pragma once
|
|
9
|
+
|
|
10
|
+
#include "BorrowingReference.hpp"
|
|
11
|
+
#include "GetRuntimeID.hpp"
|
|
12
|
+
#include "NitroLogger.hpp"
|
|
13
|
+
#include "OwningReference.hpp"
|
|
14
|
+
#include <jsi/jsi.h>
|
|
15
|
+
#include <memory>
|
|
16
|
+
#include <mutex>
|
|
17
|
+
#include <unordered_map>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
namespace margelo::nitro {
|
|
21
|
+
|
|
22
|
+
using namespace facebook;
|
|
23
|
+
|
|
24
|
+
static constexpr auto CACHE_PROP_NAME = "__nitroModulesJSICache";
|
|
25
|
+
|
|
26
|
+
template <typename T> class JSICache;
|
|
27
|
+
|
|
28
|
+
template <typename T> class JSICacheReference final {
|
|
29
|
+
public:
|
|
30
|
+
JSICacheReference() = delete;
|
|
31
|
+
JSICacheReference(const JSICacheReference&) = delete;
|
|
32
|
+
JSICacheReference(JSICacheReference&&) = delete;
|
|
33
|
+
|
|
34
|
+
~JSICacheReference() {
|
|
35
|
+
_strongCache->_mutex.unlock();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public:
|
|
39
|
+
/**
|
|
40
|
+
Creates a reference to a `jsi::Value` that can be stored in memory and accessed later.
|
|
41
|
+
The `jsi::Value` will be managed by the `jsi::Runtime`, if the `jsi::Runtime` gets the destroyed,
|
|
42
|
+
so will the `jsi::Value`.
|
|
43
|
+
|
|
44
|
+
To access the `jsi::Value`, try to `.lock()` the `weak_ptr`.
|
|
45
|
+
If it can be locked, it is still valid, otherwise the Runtime has already been deleted.
|
|
46
|
+
Do not hold the returned `shared_ptr` in memory, only use it in the calling function's scope.
|
|
47
|
+
Note: By design, this is not thread-safe, the returned `weak_ptr` must only be locked on the same thread as it was created on.
|
|
48
|
+
*/
|
|
49
|
+
OwningReference<T> makeGlobal(T&& value) {
|
|
50
|
+
auto owning = OwningReference<T>(new T(std::move(value)));
|
|
51
|
+
_strongCache->_cache.push_back(owning.weak());
|
|
52
|
+
return owning;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private:
|
|
56
|
+
explicit JSICacheReference(const std::shared_ptr<JSICache<T>>& cache) : _strongCache(cache) {
|
|
57
|
+
_strongCache->_mutex.lock();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private:
|
|
61
|
+
std::shared_ptr<JSICache<T>> _strongCache;
|
|
62
|
+
|
|
63
|
+
friend class JSICache<T>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A `JSICache` can safely store `jsi::Pointer` instances (e.g. `jsi::Object` or
|
|
68
|
+
* `jsi::Function`) inside `OwningReference<T>`.
|
|
69
|
+
*
|
|
70
|
+
* `jsi::Pointer`s are managed by a `jsi::Runtime`, and will be deleted if the `jsi::Runtime`
|
|
71
|
+
* is deleted - even if there are still strong references to the `jsi::Pointer`.
|
|
72
|
+
*/
|
|
73
|
+
template <typename T = jsi::Pointer> class JSICache final : public jsi::NativeState {
|
|
74
|
+
public:
|
|
75
|
+
explicit JSICache(jsi::Runtime* runtime) : _runtime(runtime) {}
|
|
76
|
+
|
|
77
|
+
~JSICache() {
|
|
78
|
+
std::unique_lock lock(_mutex);
|
|
79
|
+
|
|
80
|
+
for (auto& func : _cache) {
|
|
81
|
+
OwningReference<T> owning = func.lock();
|
|
82
|
+
if (owning) {
|
|
83
|
+
// Destroy all functions that we might still have in cache, some callbacks and Promises may now become invalid.
|
|
84
|
+
owning.destroy();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public:
|
|
90
|
+
JSICache() = delete;
|
|
91
|
+
JSICache(const JSICache&) = delete;
|
|
92
|
+
JSICache(JSICache&&) = delete;
|
|
93
|
+
|
|
94
|
+
public:
|
|
95
|
+
/**
|
|
96
|
+
Gets or creates a `JSICache` for the given `jsi::Runtime`.
|
|
97
|
+
The returned `shared_ptr` should not be stored in
|
|
98
|
+
If it can be locked, you can access data in the cache. Otherwise the Runtime has already been deleted.
|
|
99
|
+
Do not hold the returned `shared_ptr` in memory, only use it in the calling function's scope.
|
|
100
|
+
*/
|
|
101
|
+
[[nodiscard]]
|
|
102
|
+
static JSICacheReference<T> getOrCreateCache(jsi::Runtime& runtime) {
|
|
103
|
+
auto found = _globalCache.find(&runtime);
|
|
104
|
+
if (found != _globalCache.end()) {
|
|
105
|
+
// Fast path: get weak_ptr to JSICache from our global list.
|
|
106
|
+
std::weak_ptr<JSICache<T>> weak = found->second;
|
|
107
|
+
std::shared_ptr<JSICache<T>> strong = weak.lock();
|
|
108
|
+
if (strong) {
|
|
109
|
+
// It's still alive! Return it
|
|
110
|
+
return JSICacheReference<T>(strong);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Cache doesn't exist yet.
|
|
115
|
+
Logger::log(TAG, "Creating new JSICache<T> for runtime %s..", getRuntimeId(runtime));
|
|
116
|
+
// Create new cache
|
|
117
|
+
auto nativeState = std::make_shared<JSICache<T>>(&runtime);
|
|
118
|
+
// Wrap it in a jsi::Value using NativeState
|
|
119
|
+
jsi::Object cache(runtime);
|
|
120
|
+
cache.setNativeState(runtime, nativeState);
|
|
121
|
+
// Inject it into the jsi::Runtime's global so it's memory is managed by it
|
|
122
|
+
runtime.global().setProperty(runtime, CACHE_PROP_NAME, std::move(cache));
|
|
123
|
+
// Add it to our map of caches
|
|
124
|
+
_globalCache[&runtime] = nativeState;
|
|
125
|
+
// Return it
|
|
126
|
+
return JSICacheReference<T>(nativeState);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public:
|
|
130
|
+
private:
|
|
131
|
+
friend class JSICacheReference<T>;
|
|
132
|
+
|
|
133
|
+
private:
|
|
134
|
+
jsi::Runtime* _runtime;
|
|
135
|
+
std::mutex _mutex;
|
|
136
|
+
std::vector<BorrowingReference<T>> _cache;
|
|
137
|
+
|
|
138
|
+
private:
|
|
139
|
+
static inline std::unordered_map<jsi::Runtime*, std::weak_ptr<JSICache<T>>> _globalCache;
|
|
140
|
+
|
|
141
|
+
private:
|
|
142
|
+
static constexpr auto TAG = "JSICache";
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
} // namespace margelo::nitro
|