react-native-nitro-modules 0.1.5 → 0.1.7

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.
Files changed (53) hide show
  1. package/cpp/core/HybridObject.cpp +4 -4
  2. package/cpp/jsi/JSICache.cpp +56 -0
  3. package/cpp/jsi/JSICache.hpp +53 -90
  4. package/cpp/jsi/JSIConverter+AnyMap.hpp +22 -0
  5. package/cpp/jsi/JSIConverter+ArrayBuffer.hpp +13 -7
  6. package/cpp/jsi/JSIConverter+Function.hpp +10 -3
  7. package/cpp/jsi/JSIConverter+HybridObject.hpp +16 -0
  8. package/cpp/jsi/JSIConverter+Optional.hpp +9 -0
  9. package/cpp/jsi/JSIConverter+Promise.hpp +4 -0
  10. package/cpp/jsi/JSIConverter+Tuple.hpp +33 -0
  11. package/cpp/jsi/JSIConverter+UnorderedMap.hpp +20 -0
  12. package/cpp/jsi/JSIConverter+Variant.hpp +25 -53
  13. package/cpp/jsi/JSIConverter+Vector.hpp +21 -0
  14. package/cpp/jsi/JSIConverter.hpp +55 -7
  15. package/cpp/jsi/JSIHelpers.hpp +52 -0
  16. package/cpp/jsi/Promise.cpp +7 -3
  17. package/cpp/threading/Dispatcher.cpp +1 -1
  18. package/cpp/turbomodule/NativeNitroModules.cpp +0 -1
  19. package/cpp/utils/OwningReference.hpp +22 -0
  20. package/ios/turbomodule/NitroModuleOnLoad.mm +0 -5
  21. package/lib/commonjs/index.js +0 -11
  22. package/lib/commonjs/index.js.map +1 -1
  23. package/lib/index.d.ts +0 -1
  24. package/lib/index.js +0 -1
  25. package/lib/module/index.js +0 -1
  26. package/lib/module/index.js.map +1 -1
  27. package/lib/tsconfig.tsbuildinfo +1 -1
  28. package/lib/typescript/AnyMap.d.ts +17 -0
  29. package/lib/typescript/AnyMap.d.ts.map +1 -0
  30. package/lib/typescript/HybridObject.d.ts +58 -0
  31. package/lib/typescript/HybridObject.d.ts.map +1 -0
  32. package/lib/typescript/ModuleNotFoundError.d.ts +7 -0
  33. package/lib/typescript/ModuleNotFoundError.d.ts.map +1 -0
  34. package/lib/typescript/NativeNitroModules.d.ts +13 -0
  35. package/lib/typescript/NativeNitroModules.d.ts.map +1 -0
  36. package/lib/typescript/NativeNitroModules.web.d.ts +5 -0
  37. package/lib/typescript/NativeNitroModules.web.d.ts.map +1 -0
  38. package/lib/typescript/NitroModules.d.ts +18 -0
  39. package/lib/typescript/NitroModules.d.ts.map +1 -0
  40. package/lib/typescript/__tests__/index.test.d.ts +1 -0
  41. package/lib/typescript/__tests__/index.test.d.ts.map +1 -0
  42. package/lib/typescript/index.d.ts +4 -0
  43. package/lib/typescript/index.d.ts.map +1 -0
  44. package/package.json +1 -1
  45. package/src/index.ts +0 -1
  46. package/cpp/test-object/TestHybridObject.cpp +0 -37
  47. package/cpp/test-object/TestHybridObject.hpp +0 -87
  48. package/cpp/utils/GetRuntimeID.hpp +0 -28
  49. package/lib/commonjs/createTestObject.js +0 -15
  50. package/lib/commonjs/createTestObject.js.map +0 -1
  51. package/lib/module/createTestObject.js +0 -8
  52. package/lib/module/createTestObject.js.map +0 -1
  53. package/src/createTestObject.ts +0 -40
@@ -25,18 +25,34 @@ using namespace facebook;
25
25
  * Value types, custom types (HostObjects), and even functions with any number of arguments/types are supported.
26
26
  * This type can be extended by just creating a new template for JSIConverter in a header.
27
27
  */
28
- template <typename ArgType, typename Enable = void>
28
+ template <typename T, typename Enable = void>
29
29
  struct JSIConverter final {
30
30
  JSIConverter() = delete;
31
31
 
32
- static inline ArgType fromJSI(jsi::Runtime&, const jsi::Value&) {
33
- static_assert(always_false<ArgType>::value, "This type is not supported by the JSIConverter!");
34
- return ArgType();
35
- }
36
- static inline jsi::Value toJSI(jsi::Runtime&, ArgType) {
37
- static_assert(always_false<ArgType>::value, "This type is not supported by the JSIConverter!");
32
+ /**
33
+ * Converts the given `jsi::Value` to type `T`.
34
+ * By default, this static-asserts.
35
+ */
36
+ static inline T fromJSI(jsi::Runtime&, const jsi::Value&) {
37
+ static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
38
+ return T();
39
+ }
40
+ /**
41
+ * Converts `T` to a `jsi::Value`.
42
+ * By default, this static-asserts.
43
+ */
44
+ static inline jsi::Value toJSI(jsi::Runtime&, T) {
45
+ static_assert(always_false<T>::value, "This type is not supported by the JSIConverter!");
38
46
  return jsi::Value::undefined();
39
47
  }
48
+ /**
49
+ * Returns whether the given `jsi::Value` can be converted to `T`.
50
+ * This involves runtime type-checks.
51
+ * By default, this returns `false`.
52
+ */
53
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value&) {
54
+ return false;
55
+ }
40
56
 
41
57
  private:
42
58
  template <typename>
@@ -52,6 +68,9 @@ struct JSIConverter<int> {
52
68
  static inline jsi::Value toJSI(jsi::Runtime&, int arg) {
53
69
  return jsi::Value(arg);
54
70
  }
71
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
72
+ return value.isNumber();
73
+ }
55
74
  };
56
75
 
57
76
  // std::monostate <> null
@@ -63,6 +82,9 @@ struct JSIConverter<std::monostate> {
63
82
  static inline jsi::Value toJSI(jsi::Runtime&, std::monostate arg) {
64
83
  return jsi::Value::null();
65
84
  }
85
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
86
+ return value.isNull() || value.isUndefined();
87
+ }
66
88
  };
67
89
 
68
90
  // double <> number
@@ -74,6 +96,9 @@ struct JSIConverter<double> {
74
96
  static inline jsi::Value toJSI(jsi::Runtime&, double arg) {
75
97
  return jsi::Value(arg);
76
98
  }
99
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
100
+ return value.isNumber();
101
+ }
77
102
  };
78
103
 
79
104
  // float <> number
@@ -85,6 +110,9 @@ struct JSIConverter<float> {
85
110
  static inline jsi::Value toJSI(jsi::Runtime&, float arg) {
86
111
  return jsi::Value(static_cast<double>(arg));
87
112
  }
113
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
114
+ return value.isNumber();
115
+ }
88
116
  };
89
117
 
90
118
  // int64_t <> BigInt
@@ -96,6 +124,13 @@ struct JSIConverter<int64_t> {
96
124
  static inline jsi::Value toJSI(jsi::Runtime& runtime, int64_t arg) {
97
125
  return jsi::BigInt::fromInt64(runtime, arg);
98
126
  }
127
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
128
+ if (value.isBigInt()) {
129
+ jsi::BigInt bigint = value.getBigInt(runtime);
130
+ return bigint.isInt64(runtime);
131
+ }
132
+ return false;
133
+ }
99
134
  };
100
135
 
101
136
  // uint64_t <> BigInt
@@ -107,6 +142,13 @@ struct JSIConverter<uint64_t> {
107
142
  static inline jsi::Value toJSI(jsi::Runtime& runtime, uint64_t arg) {
108
143
  return jsi::BigInt::fromUint64(runtime, arg);
109
144
  }
145
+ static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
146
+ if (value.isBigInt()) {
147
+ jsi::BigInt bigint = value.getBigInt(runtime);
148
+ return bigint.isUint64(runtime);
149
+ }
150
+ return false;
151
+ }
110
152
  };
111
153
 
112
154
  // bool <> boolean
@@ -118,6 +160,9 @@ struct JSIConverter<bool> {
118
160
  static inline jsi::Value toJSI(jsi::Runtime&, bool arg) {
119
161
  return jsi::Value(arg);
120
162
  }
163
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
164
+ return value.isBool();
165
+ }
121
166
  };
122
167
 
123
168
  // std::string <> string
@@ -129,6 +174,9 @@ struct JSIConverter<std::string> {
129
174
  static inline jsi::Value toJSI(jsi::Runtime& runtime, const std::string& arg) {
130
175
  return jsi::String::createFromUtf8(runtime, arg);
131
176
  }
177
+ static inline bool canConvert(jsi::Runtime&, const jsi::Value& value) {
178
+ return value.isString();
179
+ }
132
180
  };
133
181
 
134
182
  } // namespace margelo::nitro
@@ -0,0 +1,52 @@
1
+ //
2
+ // JSIHelpers.hpp
3
+ // Pods
4
+ //
5
+ // Created by Marc Rousavy on 07.08.24.
6
+ //
7
+
8
+ #pragma once
9
+
10
+ #include "ThreadUtils.hpp"
11
+ #include <jsi/jsi.h>
12
+
13
+ namespace margelo::nitro {
14
+
15
+ using namespace facebook;
16
+
17
+ /**
18
+ * Returns whether the given `jsi::Object` is a plain-JS object, or not.
19
+ * If it is not a plain-JS object, it could be an Array, ArrayBuffer, Function,
20
+ * HostObject or NativeState.
21
+ */
22
+ static inline bool isPlainObject(jsi::Runtime& runtime, const jsi::Object& object) {
23
+ if (object.isArray(runtime)) {
24
+ return false;
25
+ }
26
+ if (object.isArrayBuffer(runtime)) {
27
+ return false;
28
+ }
29
+ if (object.isFunction(runtime)) {
30
+ return false;
31
+ }
32
+ if (object.isHostObject(runtime)) {
33
+ return false;
34
+ }
35
+ if (object.hasNativeState(runtime)) {
36
+ return false;
37
+ }
38
+ return true;
39
+ }
40
+
41
+ /**
42
+ * Get an ID for the given Runtime.
43
+ *
44
+ * The ID usually consists of a Runtime description (e.g. "HermesRuntime"),
45
+ * and it's Thread (e.g. "com.facebook.react.runtime.JavaScript")
46
+ */
47
+ static inline std::string getRuntimeId(jsi::Runtime& runtime) {
48
+ std::string threadName = ThreadUtils::getThreadName();
49
+ return runtime.description() + std::string(" (") + threadName + std::string(")");
50
+ }
51
+
52
+ } // namespace margelo::nitro
@@ -8,9 +8,9 @@ namespace margelo::nitro {
8
8
  using namespace facebook;
9
9
 
10
10
  Promise::Promise(jsi::Runtime& runtime, jsi::Function&& resolver, jsi::Function&& rejecter) {
11
- auto functionCache = JSICache<jsi::Function>::getOrCreateCache(runtime);
12
- _resolver = functionCache.makeGlobal(std::move(resolver));
13
- _rejecter = functionCache.makeGlobal(std::move(rejecter));
11
+ auto functionCache = JSICache::getOrCreateCache(runtime);
12
+ _resolver = functionCache.makeGlobal<jsi::Function>(std::move(resolver));
13
+ _rejecter = functionCache.makeGlobal<jsi::Function>(std::move(rejecter));
14
14
  }
15
15
 
16
16
  jsi::Value Promise::createPromise(jsi::Runtime& runtime, RunPromise&& run) {
@@ -35,6 +35,8 @@ jsi::Value Promise::createPromise(jsi::Runtime& runtime, RunPromise&& run) {
35
35
  }
36
36
 
37
37
  void Promise::resolve(jsi::Runtime& runtime, jsi::Value&& result) {
38
+ OwningLock<jsi::Function> lock = _resolver.lock();
39
+
38
40
  if (!_resolver) {
39
41
  Logger::log(TAG, "Promise resolver function has already been deleted! Ignoring call..");
40
42
  return;
@@ -43,6 +45,8 @@ void Promise::resolve(jsi::Runtime& runtime, jsi::Value&& result) {
43
45
  }
44
46
 
45
47
  void Promise::reject(jsi::Runtime& runtime, std::string message) {
48
+ OwningLock<jsi::Function> lock = _rejecter.lock();
49
+
46
50
  if (!_rejecter) {
47
51
  Logger::log(TAG, "Promise rejecter function has already been deleted! Ignoring call..");
48
52
  return;
@@ -1,5 +1,5 @@
1
1
  #include "Dispatcher.hpp"
2
- #include "GetRuntimeID.hpp"
2
+ #include "JSIHelpers.hpp"
3
3
  #include "NitroLogger.hpp"
4
4
 
5
5
  namespace margelo::nitro {
@@ -9,7 +9,6 @@
9
9
  #include "CallInvokerDispatcher.hpp"
10
10
  #include "Dispatcher.hpp"
11
11
  #include "HybridObjectRegistry.hpp"
12
- #include "TestHybridObject.hpp"
13
12
 
14
13
  namespace facebook::react {
15
14
 
@@ -76,12 +76,25 @@ public:
76
76
  }
77
77
 
78
78
  private:
79
+ // BorrowingReference<T> -> OwningReference<T> Lock-constructor
79
80
  OwningReference(const BorrowingReference<T>& ref)
80
81
  : _value(ref._value), _isDeleted(ref._isDeleted), _strongRefCount(ref._strongRefCount), _weakRefCount(ref._weakRefCount),
81
82
  _mutex(ref._mutex) {
82
83
  (*_strongRefCount)++;
83
84
  }
84
85
 
86
+ private:
87
+ // OwningReference<C> -> OwningReference<T> Cast-constructor
88
+ template <typename OldT>
89
+ OwningReference(T* value, const OwningReference<OldT>& originalRef)
90
+ : _value(value), _isDeleted(originalRef._isDeleted), _strongRefCount(originalRef._strongRefCount),
91
+ _weakRefCount(originalRef._weakRefCount), _mutex(originalRef._mutex) {
92
+ (*_strongRefCount)++;
93
+ }
94
+
95
+ template <typename C>
96
+ friend class OwningReference;
97
+
85
98
  public:
86
99
  ~OwningReference() {
87
100
  if (_strongRefCount == nullptr) {
@@ -94,6 +107,15 @@ public:
94
107
  maybeDestroy();
95
108
  }
96
109
 
110
+ public:
111
+ /**
112
+ Casts this `OwningReference<T>` to a `OwningReference<C>`.
113
+ */
114
+ template <typename C>
115
+ OwningReference<C> as() {
116
+ return OwningReference<C>(static_cast<C*>(_value), *this);
117
+ }
118
+
97
119
  public:
98
120
  /**
99
121
  Creates an `OwningLock<T>` for the given `OwningReference<T>` to guarantee safe
@@ -7,7 +7,6 @@
7
7
 
8
8
  #import "HybridObjectRegistry.hpp"
9
9
  #import "RegisterNativeNitroModules.hpp"
10
- #import "TestHybridObject.hpp"
11
10
  #import <Foundation/Foundation.h>
12
11
 
13
12
  @interface NitroModulesOnLoad : NSObject
@@ -22,10 +21,6 @@ using namespace margelo::nitro;
22
21
  // We need Objective-C here because these things do not get compiled out - meaning this will always be
23
22
  // called when the app starts.
24
23
  RegisterNativeNitroModules::registerNativeNitroModules();
25
-
26
- // Register Test HybridObject so it can be created from JS.
27
- HybridObjectRegistry::registerHybridObjectConstructor(
28
- "TestHybridObject", []() -> std::shared_ptr<TestHybridObject> { return std::make_shared<TestHybridObject>(); });
29
24
  }
30
25
 
31
26
  @end
@@ -3,17 +3,6 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- var _createTestObject = require("./createTestObject");
7
- Object.keys(_createTestObject).forEach(function (key) {
8
- if (key === "default" || key === "__esModule") return;
9
- if (key in exports && exports[key] === _createTestObject[key]) return;
10
- Object.defineProperty(exports, key, {
11
- enumerable: true,
12
- get: function () {
13
- return _createTestObject[key];
14
- }
15
- });
16
- });
17
6
  var _HybridObject = require("./HybridObject");
18
7
  Object.keys(_HybridObject).forEach(function (key) {
19
8
  if (key === "default" || key === "__esModule") return;
@@ -1 +1 @@
1
- {"version":3,"names":["_createTestObject","require","Object","keys","forEach","key","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AAS,OAAA,EAAAR,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAO,OAAA,CAAAP,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAG,OAAA,CAAAP,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
1
+ {"version":3,"names":["_HybridObject","require","Object","keys","forEach","key","exports","defineProperty","enumerable","get","_NitroModules","_AnyMap"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;AAAA,IAAAA,aAAA,GAAAC,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAH,aAAA,EAAAI,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAL,aAAA,CAAAK,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAT,aAAA,CAAAK,GAAA;IAAA;EAAA;AAAA;AACA,IAAAK,aAAA,GAAAT,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAO,aAAA,EAAAN,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAK,aAAA,CAAAL,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAC,aAAA,CAAAL,GAAA;IAAA;EAAA;AAAA;AACA,IAAAM,OAAA,GAAAV,OAAA;AAAAC,MAAA,CAAAC,IAAA,CAAAQ,OAAA,EAAAP,OAAA,WAAAC,GAAA;EAAA,IAAAA,GAAA,kBAAAA,GAAA;EAAA,IAAAA,GAAA,IAAAC,OAAA,IAAAA,OAAA,CAAAD,GAAA,MAAAM,OAAA,CAAAN,GAAA;EAAAH,MAAA,CAAAK,cAAA,CAAAD,OAAA,EAAAD,GAAA;IAAAG,UAAA;IAAAC,GAAA,WAAAA,CAAA;MAAA,OAAAE,OAAA,CAAAN,GAAA;IAAA;EAAA;AAAA","ignoreList":[]}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- export * from './createTestObject';
2
1
  export * from './HybridObject';
3
2
  export * from './NitroModules';
4
3
  export * from './AnyMap';
package/lib/index.js CHANGED
@@ -1,4 +1,3 @@
1
- export * from './createTestObject';
2
1
  export * from './HybridObject';
3
2
  export * from './NitroModules';
4
3
  export * from './AnyMap';
@@ -1,4 +1,3 @@
1
- export * from './createTestObject';
2
1
  export * from './HybridObject';
3
2
  export * from './NitroModules';
4
3
  export * from './AnyMap';
@@ -1 +1 @@
1
- {"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,oBAAoB;AAClC,cAAc,gBAAgB;AAC9B,cAAc,gBAAgB;AAC9B,cAAc,UAAU","ignoreList":[]}
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,gBAAgB;AAC9B,cAAc,UAAU","ignoreList":[]}