@react-native-ohos/react-native-reanimated 3.18.1-rc.1 → 3.18.1-rc.2

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 (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.OpenSource +11 -0
  3. package/README.md +10 -0
  4. package/harmony/reanimated/BuildProfile.ets +16 -16
  5. package/harmony/reanimated/build-profile.json5 +11 -11
  6. package/harmony/reanimated/oh-package.json5 +11 -11
  7. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp +86 -86
  8. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h +47 -47
  9. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.cpp +68 -68
  10. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.h +68 -68
  11. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp +101 -101
  12. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h +51 -51
  13. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitShadowNode.h +49 -49
  14. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpp +87 -87
  15. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.h +33 -33
  16. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.cpp +95 -95
  17. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.h +29 -29
  18. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationType.h +9 -9
  19. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.cpp +232 -232
  20. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h +90 -90
  21. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp +870 -870
  22. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h +147 -147
  23. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.cpp +91 -91
  24. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.h +176 -176
  25. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +971 -971
  26. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +252 -252
  27. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.cpp +231 -231
  28. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.h +109 -109
  29. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.cpp +56 -56
  30. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.h +20 -20
  31. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.cpp +70 -70
  32. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.h +35 -35
  33. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/CollectionUtils.h +14 -14
  34. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.cpp +5 -5
  35. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.h +19 -19
  36. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/PlatformDepMethodsHolder.h +98 -98
  37. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/ReanimatedSystraceSection.h +135 -135
  38. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +47 -47
  39. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +59 -59
  40. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +27 -27
  41. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +25 -25
  42. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -94
  43. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -49
  44. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -8
  45. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -39
  46. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -326
  47. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.h +345 -345
  48. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -52
  49. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.h +35 -35
  50. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/Defs.h +10 -10
  51. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -342
  52. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.h +47 -47
  53. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.cpp +16 -16
  54. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.h +20 -20
  55. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.cpp +20 -20
  56. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.h +29 -29
  57. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/PlatformLogger.h +16 -16
  58. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.cpp +26 -26
  59. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.h +199 -199
  60. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.cpp +95 -95
  61. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.h +19 -19
  62. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -72
  63. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -49
  64. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.cpp +20 -20
  65. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.h +22 -22
  66. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -29
  67. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -41
  68. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +16 -16
  69. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -19
  70. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.cpp +112 -112
  71. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.h +127 -127
  72. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.cpp +53 -53
  73. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.h +22 -22
  74. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -191
  75. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +155 -155
  76. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -90
  77. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -36
  78. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +182 -182
  79. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -22
  80. package/harmony/reanimated/src/main/cpp/WorkletsModule.cpp +57 -57
  81. package/harmony/reanimated/src/main/cpp/patches/AnimatedSensor/AnimatedSensorModule.cpp +89 -89
  82. package/harmony/reanimated/src/main/cpp/patches/NativeModules/ReanimatedModuleProxy.cpp +3 -0
  83. package/harmony/reanimated/src/main/module.json5 +12 -12
  84. package/harmony/reanimated/src/main/resources/base/element/color.json +7 -7
  85. package/harmony/reanimated/src/main/resources/base/element/string.json +15 -15
  86. package/harmony/reanimated/src/main/resources/base/profile/main_pages.json +5 -5
  87. package/harmony/reanimated/src/main/resources/en_US/element/string.json +15 -15
  88. package/harmony/reanimated/src/main/resources/zh_CN/element/string.json +15 -15
  89. package/harmony/reanimated/ts.ts +6 -6
  90. package/harmony/reanimated.har +0 -0
  91. package/package.json +50 -50
@@ -1,345 +1,345 @@
1
- #pragma once
2
-
3
- #include <worklets/Registries/WorkletRuntimeRegistry.h>
4
-
5
- #include <jsi/jsi.h>
6
-
7
- #include <memory>
8
- #include <string>
9
- #include <utility>
10
- #include <vector>
11
-
12
- using namespace facebook;
13
-
14
- namespace worklets {
15
-
16
- jsi::Function getValueUnpacker(jsi::Runtime &rt);
17
-
18
- #ifndef NDEBUG
19
- jsi::Function getCallGuard(jsi::Runtime &rt);
20
- #endif // NDEBUG
21
-
22
- // If possible, please use `WorkletRuntime::runGuarded` instead.
23
- template <typename... Args>
24
- inline jsi::Value runOnRuntimeGuarded(
25
- jsi::Runtime &rt,
26
- const jsi::Value &function,
27
- Args &&...args) {
28
- // We only use callGuard in debug mode, otherwise we call the provided
29
- // function directly. CallGuard provides a way of capturing exceptions in
30
- // JavaScript and propagating them to the main React Native thread such that
31
- // they can be presented using RN's LogBox.
32
- #ifndef NDEBUG
33
- return getCallGuard(rt).call(rt, function, args...);
34
- #else
35
- return function.asObject(rt).asFunction(rt).call(rt, args...);
36
- #endif
37
- }
38
-
39
- inline void cleanupIfRuntimeExists(
40
- jsi::Runtime *rt,
41
- std::unique_ptr<jsi::Value> &value) {
42
- if (rt != nullptr && !WorkletRuntimeRegistry::isRuntimeAlive(rt)) {
43
- // The below use of unique_ptr.release prevents the smart pointer from
44
- // calling the destructor of the kept object. This effectively results in
45
- // leaking some memory. We do this on purpose, as sometimes we would keep
46
- // references to JSI objects past the lifetime of its runtime (e.g.,
47
- // shared values references from the RN VM holds reference to JSI objects
48
- // on the UI runtime). When the UI runtime is terminated, the orphaned JSI
49
- // objects would crash the app when their destructors are called, because
50
- // they call into a memory that's managed by the terminated runtime. We
51
- // accept the tradeoff of leaking memory here, as it has a limited impact.
52
- // This scenario can only occur when the React instance is torn down which
53
- // happens in development mode during app reloads, or in production when
54
- // the app is being shut down gracefully by the system. An alternative
55
- // solution would require us to keep track of all JSI values that are in
56
- // use which would require additional data structure and compute spent on
57
- // bookkeeping that only for the sake of destroying the values in time
58
- // before the runtime is terminated. Note that the underlying memory that
59
- // jsi::Value refers to is managed by the VM and gets freed along with the
60
- // runtime.
61
- value.release();
62
- }
63
- }
64
-
65
- class Shareable {
66
- public:
67
- virtual jsi::Value toJSValue(jsi::Runtime &rt) = 0;
68
-
69
- virtual ~Shareable();
70
-
71
- enum ValueType {
72
- UndefinedType,
73
- NullType,
74
- BooleanType,
75
- NumberType,
76
- // SymbolType, TODO
77
- BigIntType,
78
- StringType,
79
- ObjectType,
80
- ArrayType,
81
- WorkletType,
82
- RemoteFunctionType,
83
- HandleType,
84
- HostObjectType,
85
- HostFunctionType,
86
- ArrayBufferType,
87
- };
88
-
89
- explicit Shareable(ValueType valueType) : valueType_(valueType) {}
90
-
91
- inline ValueType valueType() const {
92
- return valueType_;
93
- }
94
-
95
- static std::shared_ptr<Shareable> undefined();
96
-
97
- protected:
98
- ValueType valueType_;
99
- };
100
-
101
- template <typename BaseClass>
102
- class RetainingShareable : virtual public BaseClass {
103
- private:
104
- jsi::Runtime *primaryRuntime_;
105
- jsi::Runtime *secondaryRuntime_;
106
- std::unique_ptr<jsi::Value> secondaryValue_;
107
-
108
- public:
109
- template <typename... Args>
110
- explicit RetainingShareable(jsi::Runtime &rt, Args &&...args)
111
- : BaseClass(rt, std::forward<Args>(args)...), primaryRuntime_(&rt) {}
112
-
113
- jsi::Value toJSValue(jsi::Runtime &rt);
114
-
115
- ~RetainingShareable() {
116
- cleanupIfRuntimeExists(secondaryRuntime_, secondaryValue_);
117
- }
118
- };
119
-
120
- class ShareableJSRef : public jsi::HostObject {
121
- private:
122
- const std::shared_ptr<Shareable> value_;
123
-
124
- public:
125
- explicit ShareableJSRef(const std::shared_ptr<Shareable> &value)
126
- : value_(value) {}
127
-
128
- virtual ~ShareableJSRef();
129
-
130
- std::shared_ptr<Shareable> value() const {
131
- return value_;
132
- }
133
-
134
- static jsi::Object newHostObject(
135
- jsi::Runtime &rt,
136
- const std::shared_ptr<Shareable> &value) {
137
- return jsi::Object::createFromHostObject(
138
- rt, std::make_shared<ShareableJSRef>(value));
139
- }
140
- };
141
-
142
- jsi::Value makeShareableClone(
143
- jsi::Runtime &rt,
144
- const jsi::Value &value,
145
- const jsi::Value &shouldRetainRemote,
146
- const jsi::Value &nativeStateSource);
147
-
148
- std::shared_ptr<Shareable> extractShareableOrThrow(
149
- jsi::Runtime &rt,
150
- const jsi::Value &maybeShareableValue,
151
- const std::string &errorMessage =
152
- "[Reanimated] Expecting the object to be of type ShareableJSRef.");
153
-
154
- template <typename T>
155
- std::shared_ptr<T> extractShareableOrThrow(
156
- jsi::Runtime &rt,
157
- const jsi::Value &shareableRef,
158
- const std::string &errorMessage =
159
- "[Reanimated] Provided shareable object is of an incompatible type.") {
160
- auto res = std::dynamic_pointer_cast<T>(
161
- extractShareableOrThrow(rt, shareableRef, errorMessage));
162
- if (!res) {
163
- throw std::runtime_error(errorMessage);
164
- }
165
- return res;
166
- }
167
-
168
- class ShareableArray : public Shareable {
169
- public:
170
- ShareableArray(jsi::Runtime &rt, const jsi::Array &array);
171
-
172
- jsi::Value toJSValue(jsi::Runtime &rt) override;
173
-
174
- protected:
175
- std::vector<std::shared_ptr<Shareable>> data_;
176
- };
177
-
178
- class ShareableObject : public Shareable {
179
- public:
180
- ShareableObject(jsi::Runtime &rt, const jsi::Object &object);
181
-
182
- ShareableObject(
183
- jsi::Runtime &rt,
184
- const jsi::Object &object,
185
- const jsi::Value &nativeStateSource);
186
-
187
- jsi::Value toJSValue(jsi::Runtime &rt) override;
188
-
189
- protected:
190
- std::vector<std::pair<std::string, std::shared_ptr<Shareable>>> data_;
191
- std::shared_ptr<jsi::NativeState> nativeState_;
192
- };
193
-
194
- class ShareableHostObject : public Shareable {
195
- public:
196
- ShareableHostObject(
197
- jsi::Runtime &,
198
- const std::shared_ptr<jsi::HostObject> &hostObject)
199
- : Shareable(HostObjectType), hostObject_(hostObject) {}
200
-
201
- jsi::Value toJSValue(jsi::Runtime &rt) override;
202
-
203
- protected:
204
- const std::shared_ptr<jsi::HostObject> hostObject_;
205
- };
206
-
207
- class ShareableHostFunction : public Shareable {
208
- public:
209
- ShareableHostFunction(jsi::Runtime &rt, jsi::Function function)
210
- : Shareable(HostFunctionType),
211
- hostFunction_(
212
- (assert(function.isHostFunction(rt)),
213
- function.getHostFunction(rt))),
214
- name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
215
- paramCount_(function.getProperty(rt, "length").asNumber()) {}
216
-
217
- jsi::Value toJSValue(jsi::Runtime &rt) override;
218
-
219
- protected:
220
- const jsi::HostFunctionType hostFunction_;
221
- const std::string name_;
222
- const unsigned int paramCount_;
223
- };
224
-
225
- class ShareableArrayBuffer : public Shareable {
226
- public:
227
- ShareableArrayBuffer(jsi::Runtime &rt, const jsi::ArrayBuffer &arrayBuffer)
228
- : Shareable(ArrayBufferType),
229
- data_(
230
- arrayBuffer.data(rt),
231
- arrayBuffer.data(rt) + arrayBuffer.size(rt)) {}
232
-
233
- jsi::Value toJSValue(jsi::Runtime &rt) override;
234
-
235
- protected:
236
- const std::vector<uint8_t> data_;
237
- };
238
-
239
- class ShareableWorklet : public ShareableObject {
240
- public:
241
- ShareableWorklet(jsi::Runtime &rt, const jsi::Object &worklet)
242
- : ShareableObject(rt, worklet) {
243
- valueType_ = WorkletType;
244
- }
245
-
246
- jsi::Value toJSValue(jsi::Runtime &rt) override;
247
- };
248
-
249
- class ShareableRemoteFunction
250
- : public Shareable,
251
- public std::enable_shared_from_this<ShareableRemoteFunction> {
252
- private:
253
- jsi::Runtime *runtime_;
254
- #ifndef NDEBUG
255
- const std::string name_;
256
- #endif
257
- std::unique_ptr<jsi::Value> function_;
258
-
259
- public:
260
- ShareableRemoteFunction(jsi::Runtime &rt, jsi::Function &&function)
261
- : Shareable(RemoteFunctionType),
262
- runtime_(&rt),
263
- #ifndef NDEBUG
264
- name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
265
- #endif
266
- function_(std::make_unique<jsi::Value>(rt, std::move(function))) {
267
- }
268
-
269
- ~ShareableRemoteFunction() {
270
- cleanupIfRuntimeExists(runtime_, function_);
271
- }
272
-
273
- jsi::Value toJSValue(jsi::Runtime &rt) override;
274
- };
275
-
276
- class ShareableHandle : public Shareable {
277
- private:
278
- // We don't release the initializer since the handle can get
279
- // initialized in parallel on multiple threads. However this is not a problem,
280
- // since the final value is taken from a cache on the runtime which guarantees
281
- // sequential access.
282
- std::unique_ptr<ShareableObject> initializer_;
283
- std::unique_ptr<jsi::Value> remoteValue_;
284
- mutable std::mutex initializationMutex_;
285
- jsi::Runtime *remoteRuntime_;
286
-
287
- public:
288
- ShareableHandle(jsi::Runtime &rt, const jsi::Object &initializerObject)
289
- : Shareable(HandleType),
290
- initializer_(std::make_unique<ShareableObject>(rt, initializerObject)) {
291
- }
292
-
293
- ~ShareableHandle() {
294
- cleanupIfRuntimeExists(remoteRuntime_, remoteValue_);
295
- }
296
-
297
- jsi::Value toJSValue(jsi::Runtime &rt) override;
298
- };
299
-
300
- class ShareableString : public Shareable {
301
- public:
302
- explicit ShareableString(const std::string &string)
303
- : Shareable(StringType), data_(string) {}
304
-
305
- jsi::Value toJSValue(jsi::Runtime &rt) override;
306
-
307
- protected:
308
- const std::string data_;
309
- };
310
-
311
- class ShareableBigInt : public Shareable {
312
- public:
313
- explicit ShareableBigInt(jsi::Runtime &rt, const jsi::BigInt &bigint)
314
- : Shareable(BigIntType), string_(bigint.toString(rt).utf8(rt)) {}
315
-
316
- jsi::Value toJSValue(jsi::Runtime &rt) override;
317
-
318
- protected:
319
- const std::string string_;
320
- };
321
-
322
- class ShareableScalar : public Shareable {
323
- public:
324
- explicit ShareableScalar(double number) : Shareable(NumberType) {
325
- data_.number = number;
326
- }
327
- explicit ShareableScalar(bool boolean) : Shareable(BooleanType) {
328
- data_.boolean = boolean;
329
- }
330
- ShareableScalar() : Shareable(UndefinedType) {}
331
- explicit ShareableScalar(std::nullptr_t) : Shareable(NullType) {}
332
-
333
- jsi::Value toJSValue(jsi::Runtime &);
334
-
335
- protected:
336
- union Data {
337
- bool boolean;
338
- double number;
339
- };
340
-
341
- private:
342
- Data data_;
343
- };
344
-
345
- } // namespace worklets
1
+ #pragma once
2
+
3
+ #include <worklets/Registries/WorkletRuntimeRegistry.h>
4
+
5
+ #include <jsi/jsi.h>
6
+
7
+ #include <memory>
8
+ #include <string>
9
+ #include <utility>
10
+ #include <vector>
11
+
12
+ using namespace facebook;
13
+
14
+ namespace worklets {
15
+
16
+ jsi::Function getValueUnpacker(jsi::Runtime &rt);
17
+
18
+ #ifndef NDEBUG
19
+ jsi::Function getCallGuard(jsi::Runtime &rt);
20
+ #endif // NDEBUG
21
+
22
+ // If possible, please use `WorkletRuntime::runGuarded` instead.
23
+ template <typename... Args>
24
+ inline jsi::Value runOnRuntimeGuarded(
25
+ jsi::Runtime &rt,
26
+ const jsi::Value &function,
27
+ Args &&...args) {
28
+ // We only use callGuard in debug mode, otherwise we call the provided
29
+ // function directly. CallGuard provides a way of capturing exceptions in
30
+ // JavaScript and propagating them to the main React Native thread such that
31
+ // they can be presented using RN's LogBox.
32
+ #ifndef NDEBUG
33
+ return getCallGuard(rt).call(rt, function, args...);
34
+ #else
35
+ return function.asObject(rt).asFunction(rt).call(rt, args...);
36
+ #endif
37
+ }
38
+
39
+ inline void cleanupIfRuntimeExists(
40
+ jsi::Runtime *rt,
41
+ std::unique_ptr<jsi::Value> &value) {
42
+ if (rt != nullptr && !WorkletRuntimeRegistry::isRuntimeAlive(rt)) {
43
+ // The below use of unique_ptr.release prevents the smart pointer from
44
+ // calling the destructor of the kept object. This effectively results in
45
+ // leaking some memory. We do this on purpose, as sometimes we would keep
46
+ // references to JSI objects past the lifetime of its runtime (e.g.,
47
+ // shared values references from the RN VM holds reference to JSI objects
48
+ // on the UI runtime). When the UI runtime is terminated, the orphaned JSI
49
+ // objects would crash the app when their destructors are called, because
50
+ // they call into a memory that's managed by the terminated runtime. We
51
+ // accept the tradeoff of leaking memory here, as it has a limited impact.
52
+ // This scenario can only occur when the React instance is torn down which
53
+ // happens in development mode during app reloads, or in production when
54
+ // the app is being shut down gracefully by the system. An alternative
55
+ // solution would require us to keep track of all JSI values that are in
56
+ // use which would require additional data structure and compute spent on
57
+ // bookkeeping that only for the sake of destroying the values in time
58
+ // before the runtime is terminated. Note that the underlying memory that
59
+ // jsi::Value refers to is managed by the VM and gets freed along with the
60
+ // runtime.
61
+ value.release();
62
+ }
63
+ }
64
+
65
+ class Shareable {
66
+ public:
67
+ virtual jsi::Value toJSValue(jsi::Runtime &rt) = 0;
68
+
69
+ virtual ~Shareable();
70
+
71
+ enum ValueType {
72
+ UndefinedType,
73
+ NullType,
74
+ BooleanType,
75
+ NumberType,
76
+ // SymbolType, TODO
77
+ BigIntType,
78
+ StringType,
79
+ ObjectType,
80
+ ArrayType,
81
+ WorkletType,
82
+ RemoteFunctionType,
83
+ HandleType,
84
+ HostObjectType,
85
+ HostFunctionType,
86
+ ArrayBufferType,
87
+ };
88
+
89
+ explicit Shareable(ValueType valueType) : valueType_(valueType) {}
90
+
91
+ inline ValueType valueType() const {
92
+ return valueType_;
93
+ }
94
+
95
+ static std::shared_ptr<Shareable> undefined();
96
+
97
+ protected:
98
+ ValueType valueType_;
99
+ };
100
+
101
+ template <typename BaseClass>
102
+ class RetainingShareable : virtual public BaseClass {
103
+ private:
104
+ jsi::Runtime *primaryRuntime_;
105
+ jsi::Runtime *secondaryRuntime_;
106
+ std::unique_ptr<jsi::Value> secondaryValue_;
107
+
108
+ public:
109
+ template <typename... Args>
110
+ explicit RetainingShareable(jsi::Runtime &rt, Args &&...args)
111
+ : BaseClass(rt, std::forward<Args>(args)...), primaryRuntime_(&rt) {}
112
+
113
+ jsi::Value toJSValue(jsi::Runtime &rt);
114
+
115
+ ~RetainingShareable() {
116
+ cleanupIfRuntimeExists(secondaryRuntime_, secondaryValue_);
117
+ }
118
+ };
119
+
120
+ class ShareableJSRef : public jsi::HostObject {
121
+ private:
122
+ const std::shared_ptr<Shareable> value_;
123
+
124
+ public:
125
+ explicit ShareableJSRef(const std::shared_ptr<Shareable> &value)
126
+ : value_(value) {}
127
+
128
+ virtual ~ShareableJSRef();
129
+
130
+ std::shared_ptr<Shareable> value() const {
131
+ return value_;
132
+ }
133
+
134
+ static jsi::Object newHostObject(
135
+ jsi::Runtime &rt,
136
+ const std::shared_ptr<Shareable> &value) {
137
+ return jsi::Object::createFromHostObject(
138
+ rt, std::make_shared<ShareableJSRef>(value));
139
+ }
140
+ };
141
+
142
+ jsi::Value makeShareableClone(
143
+ jsi::Runtime &rt,
144
+ const jsi::Value &value,
145
+ const jsi::Value &shouldRetainRemote,
146
+ const jsi::Value &nativeStateSource);
147
+
148
+ std::shared_ptr<Shareable> extractShareableOrThrow(
149
+ jsi::Runtime &rt,
150
+ const jsi::Value &maybeShareableValue,
151
+ const std::string &errorMessage =
152
+ "[Reanimated] Expecting the object to be of type ShareableJSRef.");
153
+
154
+ template <typename T>
155
+ std::shared_ptr<T> extractShareableOrThrow(
156
+ jsi::Runtime &rt,
157
+ const jsi::Value &shareableRef,
158
+ const std::string &errorMessage =
159
+ "[Reanimated] Provided shareable object is of an incompatible type.") {
160
+ auto res = std::dynamic_pointer_cast<T>(
161
+ extractShareableOrThrow(rt, shareableRef, errorMessage));
162
+ if (!res) {
163
+ throw std::runtime_error(errorMessage);
164
+ }
165
+ return res;
166
+ }
167
+
168
+ class ShareableArray : public Shareable {
169
+ public:
170
+ ShareableArray(jsi::Runtime &rt, const jsi::Array &array);
171
+
172
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
173
+
174
+ protected:
175
+ std::vector<std::shared_ptr<Shareable>> data_;
176
+ };
177
+
178
+ class ShareableObject : public Shareable {
179
+ public:
180
+ ShareableObject(jsi::Runtime &rt, const jsi::Object &object);
181
+
182
+ ShareableObject(
183
+ jsi::Runtime &rt,
184
+ const jsi::Object &object,
185
+ const jsi::Value &nativeStateSource);
186
+
187
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
188
+
189
+ protected:
190
+ std::vector<std::pair<std::string, std::shared_ptr<Shareable>>> data_;
191
+ std::shared_ptr<jsi::NativeState> nativeState_;
192
+ };
193
+
194
+ class ShareableHostObject : public Shareable {
195
+ public:
196
+ ShareableHostObject(
197
+ jsi::Runtime &,
198
+ const std::shared_ptr<jsi::HostObject> &hostObject)
199
+ : Shareable(HostObjectType), hostObject_(hostObject) {}
200
+
201
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
202
+
203
+ protected:
204
+ const std::shared_ptr<jsi::HostObject> hostObject_;
205
+ };
206
+
207
+ class ShareableHostFunction : public Shareable {
208
+ public:
209
+ ShareableHostFunction(jsi::Runtime &rt, jsi::Function function)
210
+ : Shareable(HostFunctionType),
211
+ hostFunction_(
212
+ (assert(function.isHostFunction(rt)),
213
+ function.getHostFunction(rt))),
214
+ name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
215
+ paramCount_(function.getProperty(rt, "length").asNumber()) {}
216
+
217
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
218
+
219
+ protected:
220
+ const jsi::HostFunctionType hostFunction_;
221
+ const std::string name_;
222
+ const unsigned int paramCount_;
223
+ };
224
+
225
+ class ShareableArrayBuffer : public Shareable {
226
+ public:
227
+ ShareableArrayBuffer(jsi::Runtime &rt, const jsi::ArrayBuffer &arrayBuffer)
228
+ : Shareable(ArrayBufferType),
229
+ data_(
230
+ arrayBuffer.data(rt),
231
+ arrayBuffer.data(rt) + arrayBuffer.size(rt)) {}
232
+
233
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
234
+
235
+ protected:
236
+ const std::vector<uint8_t> data_;
237
+ };
238
+
239
+ class ShareableWorklet : public ShareableObject {
240
+ public:
241
+ ShareableWorklet(jsi::Runtime &rt, const jsi::Object &worklet)
242
+ : ShareableObject(rt, worklet) {
243
+ valueType_ = WorkletType;
244
+ }
245
+
246
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
247
+ };
248
+
249
+ class ShareableRemoteFunction
250
+ : public Shareable,
251
+ public std::enable_shared_from_this<ShareableRemoteFunction> {
252
+ private:
253
+ jsi::Runtime *runtime_;
254
+ #ifndef NDEBUG
255
+ const std::string name_;
256
+ #endif
257
+ std::unique_ptr<jsi::Value> function_;
258
+
259
+ public:
260
+ ShareableRemoteFunction(jsi::Runtime &rt, jsi::Function &&function)
261
+ : Shareable(RemoteFunctionType),
262
+ runtime_(&rt),
263
+ #ifndef NDEBUG
264
+ name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
265
+ #endif
266
+ function_(std::make_unique<jsi::Value>(rt, std::move(function))) {
267
+ }
268
+
269
+ ~ShareableRemoteFunction() {
270
+ cleanupIfRuntimeExists(runtime_, function_);
271
+ }
272
+
273
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
274
+ };
275
+
276
+ class ShareableHandle : public Shareable {
277
+ private:
278
+ // We don't release the initializer since the handle can get
279
+ // initialized in parallel on multiple threads. However this is not a problem,
280
+ // since the final value is taken from a cache on the runtime which guarantees
281
+ // sequential access.
282
+ std::unique_ptr<ShareableObject> initializer_;
283
+ std::unique_ptr<jsi::Value> remoteValue_;
284
+ mutable std::mutex initializationMutex_;
285
+ jsi::Runtime *remoteRuntime_;
286
+
287
+ public:
288
+ ShareableHandle(jsi::Runtime &rt, const jsi::Object &initializerObject)
289
+ : Shareable(HandleType),
290
+ initializer_(std::make_unique<ShareableObject>(rt, initializerObject)) {
291
+ }
292
+
293
+ ~ShareableHandle() {
294
+ cleanupIfRuntimeExists(remoteRuntime_, remoteValue_);
295
+ }
296
+
297
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
298
+ };
299
+
300
+ class ShareableString : public Shareable {
301
+ public:
302
+ explicit ShareableString(const std::string &string)
303
+ : Shareable(StringType), data_(string) {}
304
+
305
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
306
+
307
+ protected:
308
+ const std::string data_;
309
+ };
310
+
311
+ class ShareableBigInt : public Shareable {
312
+ public:
313
+ explicit ShareableBigInt(jsi::Runtime &rt, const jsi::BigInt &bigint)
314
+ : Shareable(BigIntType), string_(bigint.toString(rt).utf8(rt)) {}
315
+
316
+ jsi::Value toJSValue(jsi::Runtime &rt) override;
317
+
318
+ protected:
319
+ const std::string string_;
320
+ };
321
+
322
+ class ShareableScalar : public Shareable {
323
+ public:
324
+ explicit ShareableScalar(double number) : Shareable(NumberType) {
325
+ data_.number = number;
326
+ }
327
+ explicit ShareableScalar(bool boolean) : Shareable(BooleanType) {
328
+ data_.boolean = boolean;
329
+ }
330
+ ShareableScalar() : Shareable(UndefinedType) {}
331
+ explicit ShareableScalar(std::nullptr_t) : Shareable(NullType) {}
332
+
333
+ jsi::Value toJSValue(jsi::Runtime &);
334
+
335
+ protected:
336
+ union Data {
337
+ bool boolean;
338
+ double number;
339
+ };
340
+
341
+ private:
342
+ Data data_;
343
+ };
344
+
345
+ } // namespace worklets