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.
@@ -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> jThis,
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> clazz, std::string hybridObjectName,
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(
@@ -15,7 +15,7 @@ template <typename T>
15
15
  struct GlobalRefDeleter {
16
16
  explicit GlobalRefDeleter(jni::global_ref<typename T::javaobject> ref) : _ref(ref) {}
17
17
 
18
- void operator()(T* ptr) {
18
+ void operator()(T* /* cthis */) {
19
19
  if (_ref) {
20
20
  _ref.release();
21
21
  }
@@ -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& thisArg, const jsi::Value* args,
23
- size_t count) -> jsi::Value { return hybridObject->toObject(runtime); });
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* args, size_t count) {
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`
@@ -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& thisValue,
72
- const jsi::Value* args, size_t count) -> jsi::Value {
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& thisValue, const jsi::Value* arguments,
56
- size_t count) -> jsi::Value {
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* thisInstance, RegisterFn registerFunc) {
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()) {
@@ -1,3 +1,10 @@
1
+ //
2
+ // Dispatcher.cpp
3
+ // react-native-nitro
4
+ //
5
+ // Created by Marc Rousavy on 22.07.24.
6
+ //
7
+
1
8
  #include "Dispatcher.hpp"
2
9
  #include "JSIHelpers.hpp"
3
10
  #include "NitroDefines.hpp"
@@ -9,7 +9,7 @@
9
9
  #define NitroDefines_h
10
10
 
11
11
  // Sets the version of the native Nitro core library
12
- #define NITRO_VERSION "0.20.0"
12
+ #define NITRO_VERSION "0.20.1"
13
13
 
14
14
  // Sets whether to use debug or optimized production build flags
15
15
  #ifdef DEBUG
@@ -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: borrowing ArrayBufferHolder) -> ArrayBufferHolder {
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)
@@ -37,7 +37,8 @@ public final class Promise<T> {
37
37
 
38
38
  deinit {
39
39
  if state == nil {
40
- print("⚠️ Promise<\(String(describing: T.self))> got destroyed, but was never resolved or rejected! It is probably left hanging in JS now.")
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.get_exception_message(cppError)
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.make_exception(std.string(message))
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 make_exception(const std::string& message) {
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 get_exception_message(const std::exception_ptr& exception) {
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.0",
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",