@react-native-ohos/react-native-reanimated 3.18.2 → 3.18.3

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 (99) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +406 -10
  4. package/harmony/reanimated/BuildProfile.ets +1 -1
  5. package/harmony/reanimated/build-profile.json5 +11 -11
  6. package/harmony/reanimated/oh-package-lock.json5 +29 -0
  7. package/harmony/reanimated/oh-package.json5 +11 -11
  8. package/harmony/reanimated/src/main/cpp/CMakeLists.txt +2 -0
  9. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp +86 -86
  10. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h +47 -47
  11. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.cpp +68 -68
  12. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.h +73 -68
  13. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp +101 -101
  14. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h +51 -51
  15. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitShadowNode.h +49 -49
  16. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpp +96 -96
  17. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.h +33 -33
  18. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.cpp +95 -95
  19. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.h +29 -29
  20. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationType.h +9 -9
  21. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.cpp +232 -232
  22. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h +90 -90
  23. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp +870 -870
  24. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h +147 -147
  25. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.cpp +91 -91
  26. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.h +176 -176
  27. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +971 -971
  28. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +252 -254
  29. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.cpp +231 -231
  30. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.h +109 -109
  31. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.cpp +56 -56
  32. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.h +20 -20
  33. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.cpp +70 -70
  34. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.h +35 -35
  35. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/CollectionUtils.h +14 -14
  36. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.cpp +5 -5
  37. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.h +19 -19
  38. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/PlatformDepMethodsHolder.h +98 -98
  39. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/ReanimatedSystraceSection.h +135 -135
  40. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +47 -47
  41. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +59 -59
  42. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +27 -27
  43. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +25 -25
  44. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -94
  45. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -49
  46. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -8
  47. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -39
  48. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -326
  49. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.h +345 -345
  50. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -52
  51. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.h +35 -35
  52. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/Defs.h +10 -10
  53. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -342
  54. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.h +47 -47
  55. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.cpp +16 -16
  56. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.h +20 -20
  57. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.cpp +20 -20
  58. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.h +29 -29
  59. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/PlatformLogger.h +16 -16
  60. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.cpp +26 -26
  61. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.h +199 -199
  62. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.cpp +95 -95
  63. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.h +19 -19
  64. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -72
  65. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -49
  66. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.cpp +20 -20
  67. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.h +22 -22
  68. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -29
  69. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -41
  70. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +16 -16
  71. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -19
  72. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.cpp +112 -112
  73. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.h +127 -127
  74. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.cpp +53 -53
  75. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.h +22 -22
  76. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -191
  77. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +173 -173
  78. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -90
  79. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -36
  80. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +189 -189
  81. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -22
  82. package/harmony/reanimated/src/main/cpp/ReanimatedModule.cpp +29 -19
  83. package/harmony/reanimated/src/main/cpp/ReanimatedSensor.cpp +248 -0
  84. package/harmony/reanimated/src/main/cpp/ReanimatedSensor.h +36 -0
  85. package/harmony/reanimated/src/main/cpp/ReanimatedSensorContainer.cpp +36 -0
  86. package/harmony/reanimated/src/main/cpp/ReanimatedSensorContainer.h +23 -0
  87. package/harmony/reanimated/src/main/cpp/WorkletsModule.cpp +60 -60
  88. package/harmony/reanimated/src/main/cpp/patches/AnimatedSensor/AnimatedSensorModule.cpp +89 -89
  89. package/harmony/reanimated/src/main/cpp/patches/NativeModules/ReanimatedModuleProxy.cpp +53 -4
  90. package/harmony/reanimated/src/main/ets/ReanimatedModule.ts +115 -15
  91. package/harmony/reanimated/src/main/module.json5 +12 -12
  92. package/harmony/reanimated/src/main/resources/base/element/color.json +7 -7
  93. package/harmony/reanimated/src/main/resources/base/element/string.json +15 -15
  94. package/harmony/reanimated/src/main/resources/base/profile/main_pages.json +5 -5
  95. package/harmony/reanimated/src/main/resources/en_US/element/string.json +15 -15
  96. package/harmony/reanimated/src/main/resources/zh_CN/element/string.json +15 -15
  97. package/harmony/reanimated/ts.ts +6 -6
  98. package/harmony/reanimated.har +0 -0
  99. package/package.json +51 -50
@@ -1,342 +1,342 @@
1
- #include <worklets/Tools/JSISerializer.h>
2
-
3
- #include <cxxabi.h>
4
- #include <iostream>
5
- #include <sstream>
6
-
7
- namespace worklets {
8
-
9
- const std::vector<std::string> SUPPORTED_ERROR_TYPES = {
10
- "Error",
11
- "AggregateError",
12
- "EvalError",
13
- "RangeError",
14
- "ReferenceError",
15
- "SyntaxError",
16
- "TypeError",
17
- "URIError",
18
- "InternalError"};
19
-
20
- const std::vector<std::string> SUPPORTED_INDEXED_COLLECTION_TYPES = {
21
- "Int8Array",
22
- "Uint8Array",
23
- "Uint8ClampedArray",
24
- "Int16Array",
25
- "Uint16Array",
26
- "Int32Array",
27
- "Uint32Array",
28
- "BigInt64Array",
29
- "BigUint64Array",
30
- "Float32Array",
31
- "Float64Array",
32
- };
33
-
34
- const std::vector<std::string> SUPPORTED_STRUCTURED_DATA_TYPES = {
35
- "ArrayBuffer",
36
- "SharedArrayBuffer",
37
- "DataView",
38
- "Atomics",
39
- "JSON",
40
- };
41
-
42
- const std::vector<std::string> SUPPORTED_MANAGING_MEMORY_TYPES = {
43
- "WeakRef",
44
- "FinalizationRegistry",
45
- };
46
-
47
- const std::vector<std::string> SUPPORTED_ABSTRACTION_OBJECT_TYPES = {
48
- "Iterator",
49
- "AsyncIterator",
50
- "Promise",
51
- "GeneratorFunction",
52
- "AsyncGeneratorFunction",
53
- "Generator",
54
- "AsyncGenerator",
55
- "AsyncFunction",
56
- };
57
-
58
- const std::vector<std::string> SUPPORTED_REFLECTION_TYPES = {
59
- "Reflect",
60
- "Proxy",
61
- };
62
-
63
- static inline std::string getObjectTypeName(
64
- jsi::Runtime &rt,
65
- const jsi::Object &object) {
66
- return object.getPropertyAsObject(rt, "constructor")
67
- .getProperty(rt, "name")
68
- .toString(rt)
69
- .utf8(rt);
70
- }
71
-
72
- static inline bool isInstanceOf(
73
- jsi::Runtime &rt,
74
- const jsi::Object &object,
75
- const std::string &type) {
76
- return getObjectTypeName(rt, object) == type;
77
- }
78
-
79
- static inline bool isInstanceOfAny(
80
- jsi::Runtime &rt,
81
- const jsi::Object &object,
82
- const std::vector<std::string> &supportedTypes) {
83
- auto instanceType = getObjectTypeName(rt, object);
84
-
85
- return std::find(
86
- supportedTypes.begin(), supportedTypes.end(), instanceType) !=
87
- supportedTypes.end();
88
- }
89
-
90
- JSISerializer::JSISerializer(jsi::Runtime &rt)
91
- : rt_(rt),
92
- visitedNodes_(rt_.global()
93
- .getPropertyAsFunction(rt_, "Set")
94
- .callAsConstructor(rt_)
95
- .asObject(rt_)) {}
96
-
97
- std::string JSISerializer::stringifyWithName(const jsi::Object &object) {
98
- std::stringstream ss;
99
- ss << '[' << getObjectTypeName(rt_, object) << ']';
100
-
101
- return ss.str();
102
- }
103
-
104
- std::string JSISerializer::stringifyArray(const jsi::Array &arr) {
105
- std::stringstream ss;
106
- ss << '[';
107
-
108
- for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
109
- jsi::Value element = arr.getValueAtIndex(rt_, i);
110
- ss << stringifyJSIValueRecursively(element);
111
- if (i != length - 1) {
112
- ss << ", ";
113
- }
114
- }
115
-
116
- ss << ']';
117
-
118
- return ss.str();
119
- }
120
-
121
- std::string JSISerializer::stringifyFunction(const jsi::Function &func) {
122
- std::stringstream ss;
123
- auto kind = (func.isHostFunction(rt_) ? "jsi::HostFunction" : "Function");
124
- auto name = func.getProperty(rt_, "name").toString(rt_).utf8(rt_);
125
- name = name.empty() ? "anonymous" : name;
126
-
127
- ss << '[' << kind << ' ' << name << ']';
128
- return ss.str();
129
- }
130
-
131
- std::string JSISerializer::stringifyHostObject(jsi::HostObject &hostObject) {
132
- int status = -1;
133
- char *hostObjClassName =
134
- abi::__cxa_demangle(typeid(hostObject).name(), NULL, NULL, &status);
135
- if (status != 0) {
136
- return "[jsi::HostObject]";
137
- }
138
-
139
- std::stringstream ss;
140
- ss << "[jsi::HostObject(" << hostObjClassName << ")";
141
- std::free(hostObjClassName);
142
-
143
- auto props = hostObject.getPropertyNames(rt_);
144
- auto propsCount = props.size();
145
-
146
- if (propsCount > 0) {
147
- ss << " {";
148
- auto lastKey = props.back().utf8(rt_);
149
- for (const auto &key : props) {
150
- auto formattedKey = key.utf8(rt_);
151
- auto value = hostObject.get(rt_, key);
152
- ss << '"' << formattedKey << '"' << ": "
153
- << stringifyJSIValueRecursively(value);
154
- if (formattedKey != lastKey) {
155
- ss << ", ";
156
- }
157
- }
158
- ss << '}';
159
- }
160
- ss << ']';
161
-
162
- return ss.str();
163
- }
164
-
165
- std::string JSISerializer::stringifyObject(const jsi::Object &object) {
166
- std::stringstream ss;
167
- ss << '{';
168
-
169
- auto props = object.getPropertyNames(rt_);
170
-
171
- for (size_t i = 0, propsCount = props.size(rt_); i < propsCount; i++) {
172
- jsi::String propName = props.getValueAtIndex(rt_, i).toString(rt_);
173
- ss << '"' << propName.utf8(rt_) << '"' << ": "
174
- << stringifyJSIValueRecursively(object.getProperty(rt_, propName));
175
- if (i != propsCount - 1) {
176
- ss << ", ";
177
- }
178
- }
179
-
180
- ss << '}';
181
-
182
- return ss.str();
183
- }
184
-
185
- std::string JSISerializer::stringifyError(const jsi::Object &object) {
186
- std::stringstream ss;
187
- ss << '[' << object.getProperty(rt_, "name").toString(rt_).utf8(rt_) << ": "
188
- << object.getProperty(rt_, "message").toString(rt_).utf8(rt_) << ']';
189
- return ss.str();
190
- }
191
-
192
- std::string JSISerializer::stringifySet(const jsi::Object &object) {
193
- std::stringstream ss;
194
- jsi::Function arrayFrom = rt_.global()
195
- .getPropertyAsObject(rt_, "Array")
196
- .getPropertyAsFunction(rt_, "from");
197
- jsi::Object result = arrayFrom.call(rt_, object).asObject(rt_);
198
-
199
- if (!result.isArray(rt_)) {
200
- return "[Set]";
201
- }
202
-
203
- auto arr = result.asArray(rt_);
204
- ss << "Set {";
205
-
206
- for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
207
- ss << stringifyJSIValueRecursively(arr.getValueAtIndex(rt_, i));
208
- if (i != length - 1) {
209
- ss << ", ";
210
- }
211
- }
212
-
213
- ss << '}';
214
-
215
- return ss.str();
216
- }
217
-
218
- std::string JSISerializer::stringifyMap(const jsi::Object &object) {
219
- std::stringstream ss;
220
- jsi::Function arrayFrom = rt_.global()
221
- .getPropertyAsObject(rt_, "Array")
222
- .getPropertyAsFunction(rt_, "from");
223
- jsi::Object result = arrayFrom.call(rt_, object).asObject(rt_);
224
-
225
- if (!result.isArray(rt_)) {
226
- return "[Map]";
227
- }
228
-
229
- auto arr = result.asArray(rt_);
230
- ss << "Map {";
231
-
232
- for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
233
- auto pair = arr.getValueAtIndex(rt_, i).asObject(rt_).getArray(rt_);
234
- auto key = pair.getValueAtIndex(rt_, 0);
235
- auto value = pair.getValueAtIndex(rt_, 1);
236
- ss << stringifyJSIValueRecursively(key) << ": "
237
- << stringifyJSIValueRecursively(value);
238
- if (i != length - 1) {
239
- ss << ", ";
240
- }
241
- }
242
-
243
- ss << '}';
244
-
245
- return ss.str();
246
- }
247
-
248
- std::string JSISerializer::stringifyRecursiveType(const jsi::Object &object) {
249
- auto type = getObjectTypeName(rt_, object);
250
-
251
- if (type == "Array") {
252
- return "[...]";
253
- }
254
- if (type == "Object") {
255
- return "{...}";
256
- }
257
- return "...";
258
- }
259
-
260
- std::string JSISerializer::stringifyWithToString(const jsi::Object &object) {
261
- return object.getPropertyAsFunction(rt_, "toString")
262
- .callWithThis(rt_, object)
263
- .toString(rt_)
264
- .utf8(rt_);
265
- }
266
-
267
- std::string JSISerializer::stringifyJSIValueRecursively(
268
- const jsi::Value &value,
269
- bool isTopLevel) {
270
- if (value.isBool() || value.isNumber()) {
271
- return value.toString(rt_).utf8(rt_);
272
- }
273
- if (value.isString()) {
274
- return isTopLevel ? value.getString(rt_).utf8(rt_)
275
- : '"' + value.getString(rt_).utf8(rt_) + '"';
276
- }
277
- if (value.isSymbol()) {
278
- return value.getSymbol(rt_).toString(rt_);
279
- }
280
- if (value.isBigInt()) {
281
- return value.getBigInt(rt_).toString(rt_).utf8(rt_) + 'n';
282
- }
283
- if (value.isUndefined()) {
284
- return "undefined";
285
- }
286
- if (value.isNull()) {
287
- return "null";
288
- }
289
- if (value.isObject()) {
290
- jsi::Object object = value.asObject(rt_);
291
-
292
- if (hasBeenVisited(object)) {
293
- return stringifyRecursiveType(object);
294
- }
295
- markAsVisited(object);
296
-
297
- if (object.isArray(rt_)) {
298
- return stringifyArray(object.getArray(rt_));
299
- }
300
- if (object.isFunction(rt_)) {
301
- return stringifyFunction(object.getFunction(rt_));
302
- }
303
- if (object.isHostObject(rt_)) {
304
- return stringifyHostObject(*object.getHostObject(rt_));
305
- }
306
- if (isInstanceOfAny(rt_, object, SUPPORTED_ERROR_TYPES)) {
307
- return stringifyError(object);
308
- }
309
- if (isInstanceOfAny(rt_, object, SUPPORTED_INDEXED_COLLECTION_TYPES) ||
310
- isInstanceOfAny(rt_, object, SUPPORTED_STRUCTURED_DATA_TYPES) ||
311
- isInstanceOfAny(rt_, object, SUPPORTED_MANAGING_MEMORY_TYPES) ||
312
- isInstanceOfAny(rt_, object, SUPPORTED_ABSTRACTION_OBJECT_TYPES) ||
313
- isInstanceOfAny(rt_, object, SUPPORTED_REFLECTION_TYPES) ||
314
- isInstanceOf(rt_, object, "Intl") ||
315
- isInstanceOf(rt_, object, "WeakMap") ||
316
- isInstanceOf(rt_, object, "WeakSet")) {
317
- // TODO: Consider extending this log info
318
- return stringifyWithName(object);
319
- }
320
- if (isInstanceOf(rt_, object, "Date") ||
321
- isInstanceOf(rt_, object, "RegExp")) {
322
- return stringifyWithToString(object);
323
- }
324
- if (isInstanceOf(rt_, object, "Map")) {
325
- return stringifyMap(object);
326
- }
327
- if (isInstanceOf(rt_, object, "Set")) {
328
- return stringifySet(object);
329
- }
330
- return stringifyObject(object);
331
- }
332
-
333
- throw std::runtime_error("[Reanimated] Unsupported value type.");
334
- }
335
-
336
- std::string stringifyJSIValue(jsi::Runtime &rt, const jsi::Value &value) {
337
- JSISerializer serializer(rt);
338
-
339
- return serializer.stringifyJSIValueRecursively(value, true);
340
- }
341
-
342
- } // namespace worklets
1
+ #include <worklets/Tools/JSISerializer.h>
2
+
3
+ #include <cxxabi.h>
4
+ #include <iostream>
5
+ #include <sstream>
6
+
7
+ namespace worklets {
8
+
9
+ const std::vector<std::string> SUPPORTED_ERROR_TYPES = {
10
+ "Error",
11
+ "AggregateError",
12
+ "EvalError",
13
+ "RangeError",
14
+ "ReferenceError",
15
+ "SyntaxError",
16
+ "TypeError",
17
+ "URIError",
18
+ "InternalError"};
19
+
20
+ const std::vector<std::string> SUPPORTED_INDEXED_COLLECTION_TYPES = {
21
+ "Int8Array",
22
+ "Uint8Array",
23
+ "Uint8ClampedArray",
24
+ "Int16Array",
25
+ "Uint16Array",
26
+ "Int32Array",
27
+ "Uint32Array",
28
+ "BigInt64Array",
29
+ "BigUint64Array",
30
+ "Float32Array",
31
+ "Float64Array",
32
+ };
33
+
34
+ const std::vector<std::string> SUPPORTED_STRUCTURED_DATA_TYPES = {
35
+ "ArrayBuffer",
36
+ "SharedArrayBuffer",
37
+ "DataView",
38
+ "Atomics",
39
+ "JSON",
40
+ };
41
+
42
+ const std::vector<std::string> SUPPORTED_MANAGING_MEMORY_TYPES = {
43
+ "WeakRef",
44
+ "FinalizationRegistry",
45
+ };
46
+
47
+ const std::vector<std::string> SUPPORTED_ABSTRACTION_OBJECT_TYPES = {
48
+ "Iterator",
49
+ "AsyncIterator",
50
+ "Promise",
51
+ "GeneratorFunction",
52
+ "AsyncGeneratorFunction",
53
+ "Generator",
54
+ "AsyncGenerator",
55
+ "AsyncFunction",
56
+ };
57
+
58
+ const std::vector<std::string> SUPPORTED_REFLECTION_TYPES = {
59
+ "Reflect",
60
+ "Proxy",
61
+ };
62
+
63
+ static inline std::string getObjectTypeName(
64
+ jsi::Runtime &rt,
65
+ const jsi::Object &object) {
66
+ return object.getPropertyAsObject(rt, "constructor")
67
+ .getProperty(rt, "name")
68
+ .toString(rt)
69
+ .utf8(rt);
70
+ }
71
+
72
+ static inline bool isInstanceOf(
73
+ jsi::Runtime &rt,
74
+ const jsi::Object &object,
75
+ const std::string &type) {
76
+ return getObjectTypeName(rt, object) == type;
77
+ }
78
+
79
+ static inline bool isInstanceOfAny(
80
+ jsi::Runtime &rt,
81
+ const jsi::Object &object,
82
+ const std::vector<std::string> &supportedTypes) {
83
+ auto instanceType = getObjectTypeName(rt, object);
84
+
85
+ return std::find(
86
+ supportedTypes.begin(), supportedTypes.end(), instanceType) !=
87
+ supportedTypes.end();
88
+ }
89
+
90
+ JSISerializer::JSISerializer(jsi::Runtime &rt)
91
+ : rt_(rt),
92
+ visitedNodes_(rt_.global()
93
+ .getPropertyAsFunction(rt_, "Set")
94
+ .callAsConstructor(rt_)
95
+ .asObject(rt_)) {}
96
+
97
+ std::string JSISerializer::stringifyWithName(const jsi::Object &object) {
98
+ std::stringstream ss;
99
+ ss << '[' << getObjectTypeName(rt_, object) << ']';
100
+
101
+ return ss.str();
102
+ }
103
+
104
+ std::string JSISerializer::stringifyArray(const jsi::Array &arr) {
105
+ std::stringstream ss;
106
+ ss << '[';
107
+
108
+ for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
109
+ jsi::Value element = arr.getValueAtIndex(rt_, i);
110
+ ss << stringifyJSIValueRecursively(element);
111
+ if (i != length - 1) {
112
+ ss << ", ";
113
+ }
114
+ }
115
+
116
+ ss << ']';
117
+
118
+ return ss.str();
119
+ }
120
+
121
+ std::string JSISerializer::stringifyFunction(const jsi::Function &func) {
122
+ std::stringstream ss;
123
+ auto kind = (func.isHostFunction(rt_) ? "jsi::HostFunction" : "Function");
124
+ auto name = func.getProperty(rt_, "name").toString(rt_).utf8(rt_);
125
+ name = name.empty() ? "anonymous" : name;
126
+
127
+ ss << '[' << kind << ' ' << name << ']';
128
+ return ss.str();
129
+ }
130
+
131
+ std::string JSISerializer::stringifyHostObject(jsi::HostObject &hostObject) {
132
+ int status = -1;
133
+ char *hostObjClassName =
134
+ abi::__cxa_demangle(typeid(hostObject).name(), NULL, NULL, &status);
135
+ if (status != 0) {
136
+ return "[jsi::HostObject]";
137
+ }
138
+
139
+ std::stringstream ss;
140
+ ss << "[jsi::HostObject(" << hostObjClassName << ")";
141
+ std::free(hostObjClassName);
142
+
143
+ auto props = hostObject.getPropertyNames(rt_);
144
+ auto propsCount = props.size();
145
+
146
+ if (propsCount > 0) {
147
+ ss << " {";
148
+ auto lastKey = props.back().utf8(rt_);
149
+ for (const auto &key : props) {
150
+ auto formattedKey = key.utf8(rt_);
151
+ auto value = hostObject.get(rt_, key);
152
+ ss << '"' << formattedKey << '"' << ": "
153
+ << stringifyJSIValueRecursively(value);
154
+ if (formattedKey != lastKey) {
155
+ ss << ", ";
156
+ }
157
+ }
158
+ ss << '}';
159
+ }
160
+ ss << ']';
161
+
162
+ return ss.str();
163
+ }
164
+
165
+ std::string JSISerializer::stringifyObject(const jsi::Object &object) {
166
+ std::stringstream ss;
167
+ ss << '{';
168
+
169
+ auto props = object.getPropertyNames(rt_);
170
+
171
+ for (size_t i = 0, propsCount = props.size(rt_); i < propsCount; i++) {
172
+ jsi::String propName = props.getValueAtIndex(rt_, i).toString(rt_);
173
+ ss << '"' << propName.utf8(rt_) << '"' << ": "
174
+ << stringifyJSIValueRecursively(object.getProperty(rt_, propName));
175
+ if (i != propsCount - 1) {
176
+ ss << ", ";
177
+ }
178
+ }
179
+
180
+ ss << '}';
181
+
182
+ return ss.str();
183
+ }
184
+
185
+ std::string JSISerializer::stringifyError(const jsi::Object &object) {
186
+ std::stringstream ss;
187
+ ss << '[' << object.getProperty(rt_, "name").toString(rt_).utf8(rt_) << ": "
188
+ << object.getProperty(rt_, "message").toString(rt_).utf8(rt_) << ']';
189
+ return ss.str();
190
+ }
191
+
192
+ std::string JSISerializer::stringifySet(const jsi::Object &object) {
193
+ std::stringstream ss;
194
+ jsi::Function arrayFrom = rt_.global()
195
+ .getPropertyAsObject(rt_, "Array")
196
+ .getPropertyAsFunction(rt_, "from");
197
+ jsi::Object result = arrayFrom.call(rt_, object).asObject(rt_);
198
+
199
+ if (!result.isArray(rt_)) {
200
+ return "[Set]";
201
+ }
202
+
203
+ auto arr = result.asArray(rt_);
204
+ ss << "Set {";
205
+
206
+ for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
207
+ ss << stringifyJSIValueRecursively(arr.getValueAtIndex(rt_, i));
208
+ if (i != length - 1) {
209
+ ss << ", ";
210
+ }
211
+ }
212
+
213
+ ss << '}';
214
+
215
+ return ss.str();
216
+ }
217
+
218
+ std::string JSISerializer::stringifyMap(const jsi::Object &object) {
219
+ std::stringstream ss;
220
+ jsi::Function arrayFrom = rt_.global()
221
+ .getPropertyAsObject(rt_, "Array")
222
+ .getPropertyAsFunction(rt_, "from");
223
+ jsi::Object result = arrayFrom.call(rt_, object).asObject(rt_);
224
+
225
+ if (!result.isArray(rt_)) {
226
+ return "[Map]";
227
+ }
228
+
229
+ auto arr = result.asArray(rt_);
230
+ ss << "Map {";
231
+
232
+ for (size_t i = 0, length = arr.size(rt_); i < length; i++) {
233
+ auto pair = arr.getValueAtIndex(rt_, i).asObject(rt_).getArray(rt_);
234
+ auto key = pair.getValueAtIndex(rt_, 0);
235
+ auto value = pair.getValueAtIndex(rt_, 1);
236
+ ss << stringifyJSIValueRecursively(key) << ": "
237
+ << stringifyJSIValueRecursively(value);
238
+ if (i != length - 1) {
239
+ ss << ", ";
240
+ }
241
+ }
242
+
243
+ ss << '}';
244
+
245
+ return ss.str();
246
+ }
247
+
248
+ std::string JSISerializer::stringifyRecursiveType(const jsi::Object &object) {
249
+ auto type = getObjectTypeName(rt_, object);
250
+
251
+ if (type == "Array") {
252
+ return "[...]";
253
+ }
254
+ if (type == "Object") {
255
+ return "{...}";
256
+ }
257
+ return "...";
258
+ }
259
+
260
+ std::string JSISerializer::stringifyWithToString(const jsi::Object &object) {
261
+ return object.getPropertyAsFunction(rt_, "toString")
262
+ .callWithThis(rt_, object)
263
+ .toString(rt_)
264
+ .utf8(rt_);
265
+ }
266
+
267
+ std::string JSISerializer::stringifyJSIValueRecursively(
268
+ const jsi::Value &value,
269
+ bool isTopLevel) {
270
+ if (value.isBool() || value.isNumber()) {
271
+ return value.toString(rt_).utf8(rt_);
272
+ }
273
+ if (value.isString()) {
274
+ return isTopLevel ? value.getString(rt_).utf8(rt_)
275
+ : '"' + value.getString(rt_).utf8(rt_) + '"';
276
+ }
277
+ if (value.isSymbol()) {
278
+ return value.getSymbol(rt_).toString(rt_);
279
+ }
280
+ if (value.isBigInt()) {
281
+ return value.getBigInt(rt_).toString(rt_).utf8(rt_) + 'n';
282
+ }
283
+ if (value.isUndefined()) {
284
+ return "undefined";
285
+ }
286
+ if (value.isNull()) {
287
+ return "null";
288
+ }
289
+ if (value.isObject()) {
290
+ jsi::Object object = value.asObject(rt_);
291
+
292
+ if (hasBeenVisited(object)) {
293
+ return stringifyRecursiveType(object);
294
+ }
295
+ markAsVisited(object);
296
+
297
+ if (object.isArray(rt_)) {
298
+ return stringifyArray(object.getArray(rt_));
299
+ }
300
+ if (object.isFunction(rt_)) {
301
+ return stringifyFunction(object.getFunction(rt_));
302
+ }
303
+ if (object.isHostObject(rt_)) {
304
+ return stringifyHostObject(*object.getHostObject(rt_));
305
+ }
306
+ if (isInstanceOfAny(rt_, object, SUPPORTED_ERROR_TYPES)) {
307
+ return stringifyError(object);
308
+ }
309
+ if (isInstanceOfAny(rt_, object, SUPPORTED_INDEXED_COLLECTION_TYPES) ||
310
+ isInstanceOfAny(rt_, object, SUPPORTED_STRUCTURED_DATA_TYPES) ||
311
+ isInstanceOfAny(rt_, object, SUPPORTED_MANAGING_MEMORY_TYPES) ||
312
+ isInstanceOfAny(rt_, object, SUPPORTED_ABSTRACTION_OBJECT_TYPES) ||
313
+ isInstanceOfAny(rt_, object, SUPPORTED_REFLECTION_TYPES) ||
314
+ isInstanceOf(rt_, object, "Intl") ||
315
+ isInstanceOf(rt_, object, "WeakMap") ||
316
+ isInstanceOf(rt_, object, "WeakSet")) {
317
+ // TODO: Consider extending this log info
318
+ return stringifyWithName(object);
319
+ }
320
+ if (isInstanceOf(rt_, object, "Date") ||
321
+ isInstanceOf(rt_, object, "RegExp")) {
322
+ return stringifyWithToString(object);
323
+ }
324
+ if (isInstanceOf(rt_, object, "Map")) {
325
+ return stringifyMap(object);
326
+ }
327
+ if (isInstanceOf(rt_, object, "Set")) {
328
+ return stringifySet(object);
329
+ }
330
+ return stringifyObject(object);
331
+ }
332
+
333
+ throw std::runtime_error("[Reanimated] Unsupported value type.");
334
+ }
335
+
336
+ std::string stringifyJSIValue(jsi::Runtime &rt, const jsi::Value &value) {
337
+ JSISerializer serializer(rt);
338
+
339
+ return serializer.stringifyJSIValueRecursively(value, true);
340
+ }
341
+
342
+ } // namespace worklets