react-native-nitro-modules 0.20.0 → 0.20.1
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/JArrayBuffer.hpp +1 -2
- package/android/src/main/cpp/core/JPromise.hpp +10 -0
- package/android/src/main/cpp/registry/JHybridObjectRegistry.cpp +1 -1
- package/android/src/main/cpp/utils/JNISharedPtr.hpp +1 -1
- package/cpp/core/BoxedHybridObject.cpp +3 -2
- package/cpp/core/HybridObject.cpp +1 -1
- package/cpp/core/Promise.hpp +16 -0
- package/cpp/jsi/JSIConverter+Function.hpp +2 -5
- package/cpp/jsi/JSIConverter+Promise.hpp +2 -2
- package/cpp/prototype/HybridObjectPrototype.hpp +1 -1
- package/cpp/threading/Dispatcher.cpp +7 -0
- package/cpp/utils/NitroDefines.hpp +1 -1
- package/ios/core/ArrayBufferHolder.swift +1 -1
- package/ios/core/Promise.swift +2 -1
- package/ios/core/RuntimeError.swift +2 -2
- package/ios/utils/RuntimeError.hpp +2 -2
- package/package.json +1 -1
|
@@ -38,8 +38,7 @@ public:
|
|
|
38
38
|
/**
|
|
39
39
|
* Create a new `JArrayBuffer` that wraps the given `ByteBuffer` from Java.
|
|
40
40
|
*/
|
|
41
|
-
static jni::local_ref<JArrayBuffer::jhybriddata> initHybrid(jni::alias_ref<jhybridobject>
|
|
42
|
-
jni::alias_ref<jni::JByteBuffer> buffer) {
|
|
41
|
+
static jni::local_ref<JArrayBuffer::jhybriddata> initHybrid(jni::alias_ref<jhybridobject>, jni::alias_ref<jni::JByteBuffer> buffer) {
|
|
43
42
|
return makeCxxInstance(buffer);
|
|
44
43
|
}
|
|
45
44
|
|
|
@@ -56,6 +56,16 @@ public:
|
|
|
56
56
|
return newObjectCxxArgs();
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
public:
|
|
60
|
+
~JPromise() override {
|
|
61
|
+
if (_result == nullptr && _error == nullptr) [[unlikely]] {
|
|
62
|
+
jni::ThreadScope::WithClassLoader([&]() {
|
|
63
|
+
std::runtime_error error("Timeouted: JPromise was destroyed!");
|
|
64
|
+
this->reject(jni::getJavaExceptionForCppException(std::make_exception_ptr(error)));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
public:
|
|
60
70
|
void resolve(jni::alias_ref<jni::JObject> result) {
|
|
61
71
|
std::unique_lock lock(_mutex);
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
namespace margelo::nitro {
|
|
14
14
|
|
|
15
|
-
void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref<jni::JClass
|
|
15
|
+
void JHybridObjectRegistry::registerHybridObjectConstructor(jni::alias_ref<jni::JClass>, std::string hybridObjectName,
|
|
16
16
|
jni::alias_ref<JHybridObjectInitializer> constructorFn) {
|
|
17
17
|
auto sharedInitializer = jni::make_global(constructorFn);
|
|
18
18
|
HybridObjectRegistry::registerHybridObjectConstructor(
|
|
@@ -19,8 +19,9 @@ jsi::Value BoxedHybridObject::get(jsi::Runtime& runtime, const jsi::PropNameID&
|
|
|
19
19
|
if (name == "unbox") {
|
|
20
20
|
return jsi::Function::createFromHostFunction(
|
|
21
21
|
runtime, jsi::PropNameID::forUtf8(runtime, "unbox"), 0,
|
|
22
|
-
[hybridObject = _hybridObject](jsi::Runtime& runtime, const jsi::Value
|
|
23
|
-
|
|
22
|
+
[hybridObject = _hybridObject](jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
|
|
23
|
+
return hybridObject->toObject(runtime);
|
|
24
|
+
});
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
return jsi::Value::undefined();
|
|
@@ -23,7 +23,7 @@ bool HybridObject::equals(std::shared_ptr<HybridObject> other) {
|
|
|
23
23
|
return this == other.get();
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
jsi::Value HybridObject::disposeRaw(jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value
|
|
26
|
+
jsi::Value HybridObject::disposeRaw(jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value*, size_t) {
|
|
27
27
|
// 1. Dispose any resources - this might be overridden by child classes to perform manual cleanup.
|
|
28
28
|
dispose();
|
|
29
29
|
// 2. Remove the NativeState from `this`
|
package/cpp/core/Promise.hpp
CHANGED
|
@@ -32,6 +32,14 @@ public:
|
|
|
32
32
|
private:
|
|
33
33
|
Promise() {}
|
|
34
34
|
|
|
35
|
+
public:
|
|
36
|
+
~Promise() {
|
|
37
|
+
if (isPending()) [[unlikely]] {
|
|
38
|
+
auto message = std::string("Timeouted: Promise<") + TypeInfo::getFriendlyTypename<TResult>() + "> was destroyed!";
|
|
39
|
+
reject(std::make_exception_ptr(std::runtime_error(message)));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
public:
|
|
36
44
|
/**
|
|
37
45
|
* Creates a new pending Promise that has to be resolved
|
|
@@ -258,6 +266,14 @@ public:
|
|
|
258
266
|
private:
|
|
259
267
|
Promise() {}
|
|
260
268
|
|
|
269
|
+
public:
|
|
270
|
+
~Promise() {
|
|
271
|
+
if (isPending()) [[unlikely]] {
|
|
272
|
+
std::runtime_error error("Timeouted: Promise<void> was destroyed!");
|
|
273
|
+
reject(std::make_exception_ptr(std::move(error)));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
261
277
|
public:
|
|
262
278
|
static std::shared_ptr<Promise> create() {
|
|
263
279
|
return std::shared_ptr<Promise>(new Promise());
|
|
@@ -68,8 +68,8 @@ struct JSIConverter<std::function<ReturnType(Args...)>> final {
|
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
static inline jsi::Value toJSI(jsi::Runtime& runtime, std::function<ReturnType(Args...)>&& function) {
|
|
71
|
-
jsi::HostFunctionType jsFunction = [function = std::move(function)](jsi::Runtime& runtime, const jsi::Value
|
|
72
|
-
|
|
71
|
+
jsi::HostFunctionType jsFunction = [function = std::move(function)](jsi::Runtime& runtime, const jsi::Value&, const jsi::Value* args,
|
|
72
|
+
size_t count) -> jsi::Value {
|
|
73
73
|
if (count != sizeof...(Args)) [[unlikely]] {
|
|
74
74
|
throw jsi::JSError(runtime, "Function expected " + std::to_string(sizeof...(Args)) + " arguments, but received " +
|
|
75
75
|
std::to_string(count) + "!");
|
|
@@ -95,9 +95,6 @@ struct JSIConverter<std::function<ReturnType(Args...)>> final {
|
|
|
95
95
|
|
|
96
96
|
private:
|
|
97
97
|
static inline ResultingType callJSFunction(jsi::Runtime& runtime, const OwningReference<jsi::Function>& function, const Args&... args) {
|
|
98
|
-
// Throw a lock on the OwningReference<T> so we can guarantee safe access (Hermes GC cannot delete it while `lock` is alive)
|
|
99
|
-
OwningLock<jsi::Function> lock = function.lock();
|
|
100
|
-
|
|
101
98
|
if (!function) {
|
|
102
99
|
if constexpr (std::is_void_v<ResultingType>) {
|
|
103
100
|
// runtime has already been deleted. since this returns void, we can just ignore it being deleted.
|
|
@@ -52,8 +52,8 @@ struct JSIConverter<std::shared_ptr<Promise<TResult>>> final {
|
|
|
52
52
|
if (promise->isPending()) {
|
|
53
53
|
// Get Promise ctor from global
|
|
54
54
|
jsi::Function promiseCtor = runtime.global().getPropertyAsFunction(runtime, "Promise");
|
|
55
|
-
jsi::HostFunctionType executor = [promise](jsi::Runtime& runtime, const jsi::Value
|
|
56
|
-
size_t
|
|
55
|
+
jsi::HostFunctionType executor = [promise](jsi::Runtime& runtime, const jsi::Value&, const jsi::Value* arguments,
|
|
56
|
+
size_t) -> jsi::Value {
|
|
57
57
|
// Add resolver listener
|
|
58
58
|
if constexpr (std::is_void_v<TResult>) {
|
|
59
59
|
// It's resolving to void.
|
|
@@ -79,7 +79,7 @@ protected:
|
|
|
79
79
|
* **Do not conditionally register hybrid methods, getters or setter!**
|
|
80
80
|
*/
|
|
81
81
|
template <typename Derived>
|
|
82
|
-
inline void registerHybrids(Derived*
|
|
82
|
+
inline void registerHybrids(Derived* /* this */, RegisterFn registerFunc) {
|
|
83
83
|
const std::shared_ptr<Prototype>& prototype = _prototypeChain.extendPrototype<Derived>();
|
|
84
84
|
|
|
85
85
|
if (!prototype->hasHybrids()) {
|
|
@@ -50,7 +50,7 @@ public extension ArrayBufferHolder {
|
|
|
50
50
|
/**
|
|
51
51
|
* Copy the given `ArrayBufferHolder` into a new **owning** `ArrayBufferHolder`.
|
|
52
52
|
*/
|
|
53
|
-
static func copy(of other:
|
|
53
|
+
static func copy(of other: ArrayBufferHolder) -> ArrayBufferHolder {
|
|
54
54
|
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: other.size)
|
|
55
55
|
let pointer = other.data.assumingMemoryBound(to: UInt8.self)
|
|
56
56
|
data.initialize(from: pointer, count: other.size)
|
package/ios/core/Promise.swift
CHANGED
|
@@ -37,7 +37,8 @@ public final class Promise<T> {
|
|
|
37
37
|
|
|
38
38
|
deinit {
|
|
39
39
|
if state == nil {
|
|
40
|
-
|
|
40
|
+
let message = "Timeouted: Promise<\(String(describing: T.self))> was destroyed!"
|
|
41
|
+
reject(withError: RuntimeError.error(withMessage: message))
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
@@ -26,7 +26,7 @@ public enum RuntimeError: Error, CustomStringConvertible {
|
|
|
26
26
|
* Creates a new `RuntimeError` from the given C++ `std::exception`.
|
|
27
27
|
*/
|
|
28
28
|
public static func from(cppError: std.exception_ptr) -> RuntimeError {
|
|
29
|
-
let message = margelo.nitro.
|
|
29
|
+
let message = margelo.nitro.getExceptionMessage(cppError)
|
|
30
30
|
return .error(withMessage: String(message))
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -37,6 +37,6 @@ public extension Error {
|
|
|
37
37
|
*/
|
|
38
38
|
func toCpp() -> std.exception_ptr {
|
|
39
39
|
let message = String(describing: self)
|
|
40
|
-
return margelo.nitro.
|
|
40
|
+
return margelo.nitro.makeException(std.string(message))
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
|
|
14
14
|
namespace margelo::nitro {
|
|
15
15
|
|
|
16
|
-
static inline std::exception_ptr
|
|
16
|
+
static inline std::exception_ptr makeException(const std::string& message) {
|
|
17
17
|
return std::make_exception_ptr(std::runtime_error(message));
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
static inline std::string
|
|
20
|
+
static inline std::string getExceptionMessage(const std::exception_ptr& exception) {
|
|
21
21
|
if (exception == nullptr) [[unlikely]] {
|
|
22
22
|
throw std::runtime_error("Cannot get error message of an empty exception_ptr!");
|
|
23
23
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-nitro-modules",
|
|
3
|
-
"version": "0.20.
|
|
3
|
+
"version": "0.20.1",
|
|
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",
|