@react-native-ohos/react-native-reanimated 3.18.1-rc.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.
Files changed (113) hide show
  1. package/harmony/reanimated/BuildProfile.ets +17 -0
  2. package/harmony/reanimated/build-profile.json5 +12 -0
  3. package/harmony/reanimated/hvigorfile.ts +5 -0
  4. package/harmony/reanimated/index.ets +5 -0
  5. package/harmony/reanimated/oh-package.json5 +12 -0
  6. package/harmony/reanimated/src/main/cpp/CMakeLists.txt +72 -0
  7. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp +86 -0
  8. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h +47 -0
  9. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.cpp +68 -0
  10. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/PropsRegistry.h +68 -0
  11. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp +101 -0
  12. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h +51 -0
  13. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedCommitShadowNode.h +49 -0
  14. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.cpp +87 -0
  15. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ReanimatedMountHook.h +33 -0
  16. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.cpp +95 -0
  17. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Fabric/ShadowTreeCloner.h +29 -0
  18. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationType.h +9 -0
  19. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.cpp +232 -0
  20. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h +90 -0
  21. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.cpp +870 -0
  22. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxy.h +147 -0
  23. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.cpp +91 -0
  24. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsUtils.h +176 -0
  25. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp +971 -0
  26. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h +252 -0
  27. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.cpp +231 -0
  28. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxySpec.h +109 -0
  29. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.cpp +56 -0
  30. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/RNRuntimeDecorator.h +20 -0
  31. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.cpp +70 -0
  32. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/RuntimeDecorators/UIRuntimeDecorator.h +35 -0
  33. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/CollectionUtils.h +14 -0
  34. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.cpp +5 -0
  35. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/FeaturesConfig.h +19 -0
  36. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/PlatformDepMethodsHolder.h +98 -0
  37. package/harmony/reanimated/src/main/cpp/Common/cpp/reanimated/Tools/ReanimatedSystraceSection.h +135 -0
  38. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +47 -0
  39. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +59 -0
  40. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +27 -0
  41. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +25 -0
  42. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
  43. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -0
  44. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
  45. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -0
  46. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -0
  47. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/SharedItems/Shareables.h +345 -0
  48. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -0
  49. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/AsyncQueue.h +35 -0
  50. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/Defs.h +10 -0
  51. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
  52. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSISerializer.h +47 -0
  53. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.cpp +16 -0
  54. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSLogger.h +20 -0
  55. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.cpp +20 -0
  56. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/JSScheduler.h +29 -0
  57. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
  58. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.cpp +26 -0
  59. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedJSIUtils.h +199 -0
  60. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.cpp +95 -0
  61. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ReanimatedVersion.h +19 -0
  62. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -0
  63. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
  64. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.cpp +20 -0
  65. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
  66. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
  67. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
  68. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +16 -0
  69. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -0
  70. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.cpp +112 -0
  71. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedHermesRuntime.h +127 -0
  72. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.cpp +53 -0
  73. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/ReanimatedRuntime.h +22 -0
  74. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
  75. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +155 -0
  76. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -0
  77. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
  78. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +182 -0
  79. package/harmony/reanimated/src/main/cpp/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -0
  80. package/harmony/reanimated/src/main/cpp/ReanimatedLogger.cpp +33 -0
  81. package/harmony/reanimated/src/main/cpp/ReanimatedModule.cpp +222 -0
  82. package/harmony/reanimated/src/main/cpp/ReanimatedModule.h +24 -0
  83. package/harmony/reanimated/src/main/cpp/ReanimatedNodesManager.cpp +56 -0
  84. package/harmony/reanimated/src/main/cpp/ReanimatedNodesManager.h +31 -0
  85. package/harmony/reanimated/src/main/cpp/ReanimatedPackage.cpp +47 -0
  86. package/harmony/reanimated/src/main/cpp/ReanimatedPackage.h +16 -0
  87. package/harmony/reanimated/src/main/cpp/ReanimatedUIScheduler.cpp +23 -0
  88. package/harmony/reanimated/src/main/cpp/ReanimatedUIScheduler.h +20 -0
  89. package/harmony/reanimated/src/main/cpp/TransformParser.h +94 -0
  90. package/harmony/reanimated/src/main/cpp/WorkletsModule.cpp +58 -0
  91. package/harmony/reanimated/src/main/cpp/WorkletsModule.h +22 -0
  92. package/harmony/reanimated/src/main/cpp/patches/AnimatedSensor/AnimatedSensorModule.cpp +89 -0
  93. package/harmony/reanimated/src/main/cpp/patches/Fabric/ShadowTreeCloner.cpp +95 -0
  94. package/harmony/reanimated/src/main/cpp/patches/NativeModules/ReanimatedModuleProxy.cpp +961 -0
  95. package/harmony/reanimated/src/main/ets/AnimationManager.ts +61 -0
  96. package/harmony/reanimated/src/main/ets/ReanimatedModule.ts +41 -0
  97. package/harmony/reanimated/src/main/ets/ReanimatedPackage.ts +29 -0
  98. package/harmony/reanimated/src/main/ets/WorkletsModule.ts +15 -0
  99. package/harmony/reanimated/src/main/module.json5 +13 -0
  100. package/harmony/reanimated/src/main/resources/base/element/color.json +8 -0
  101. package/harmony/reanimated/src/main/resources/base/element/string.json +16 -0
  102. package/harmony/reanimated/src/main/resources/base/media/icon.png +0 -0
  103. package/harmony/reanimated/src/main/resources/base/media/startIcon.png +0 -0
  104. package/harmony/reanimated/src/main/resources/base/profile/main_pages.json +5 -0
  105. package/harmony/reanimated/src/main/resources/en_US/element/string.json +16 -0
  106. package/harmony/reanimated/src/main/resources/zh_CN/element/string.json +16 -0
  107. package/harmony/reanimated/ts.ts +6 -0
  108. package/harmony/reanimated.har +0 -0
  109. package/lib/index.d.ts +4 -0
  110. package/lib/index.d.ts.map +1 -0
  111. package/lib/index.js +7 -0
  112. package/package.json +50 -0
  113. package/src/index.ts +10 -0
@@ -0,0 +1,199 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <sstream>
5
+ #include <string>
6
+ #include <tuple>
7
+ #include <utility>
8
+
9
+ using namespace facebook;
10
+
11
+ namespace worklets::jsi_utils {
12
+
13
+ // `get` functions take a pointer to `jsi::Value` and
14
+ // call an appropriate method to cast to the native type
15
+ template <typename T>
16
+ inline T get(jsi::Runtime &rt, const jsi::Value *value);
17
+
18
+ template <>
19
+ inline double get<double>(jsi::Runtime &, const jsi::Value *value) {
20
+ return value->asNumber();
21
+ }
22
+
23
+ template <>
24
+ inline int get<int>(jsi::Runtime &, const jsi::Value *value) {
25
+ return value->asNumber();
26
+ }
27
+
28
+ template <>
29
+ inline bool get<bool>(jsi::Runtime &, const jsi::Value *value) {
30
+ if (!value->isBool()) {
31
+ throw jsi::JSINativeException("[Reanimated] Expected a boolean.");
32
+ }
33
+ return value->getBool();
34
+ }
35
+
36
+ template <>
37
+ inline jsi::Object get<jsi::Object>(jsi::Runtime &rt, const jsi::Value *value) {
38
+ return value->asObject(rt);
39
+ }
40
+
41
+ template <>
42
+ inline jsi::Value const &get<jsi::Value const &>(
43
+ jsi::Runtime &,
44
+ const jsi::Value *value) {
45
+ return *value;
46
+ }
47
+
48
+ // `convertArgs` functions take a variadic template parameter of target (C++)
49
+ // argument types `Targs` and a `jsi::Value` array `args`, and converts `args`
50
+ // to a tuple of typed C++ arguments to be passed to the native implementation.
51
+ // This is accomplished by dispatching (at compile time) to the correct
52
+ // implementation based on the first type of `Targs`, using SFINAE to select the
53
+ // correct specialization, and concatenating with the result of recursion on the
54
+ // rest of `Targs`
55
+
56
+ // BEGIN implementations for `convertArgs` specializations.
57
+ // specialization for empty `Targs` - returns an empty tuple
58
+ template <typename... Args>
59
+ inline std::enable_if_t<(sizeof...(Args) == 0), std::tuple<>> convertArgs(
60
+ jsi::Runtime &,
61
+ const jsi::Value *) {
62
+ return std::make_tuple();
63
+ }
64
+
65
+ // calls `get<First>` on the first argument to retrieve the native type,
66
+ // then calls recursively on the rest of `args`
67
+ // and returns the concatenation of results
68
+ template <typename T, typename... Rest>
69
+ inline std::tuple<T, Rest...> convertArgs(
70
+ jsi::Runtime &rt,
71
+ const jsi::Value *args) {
72
+ auto arg = std::tuple<T>(get<T>(rt, args));
73
+ auto rest = convertArgs<Rest...>(rt, std::next(args));
74
+ return std::tuple_cat(std::move(arg), std::move(rest));
75
+ }
76
+ // END implementations for `convertArgs` specializations.
77
+
78
+ // returns a tuple with the result of casting `args` to appropriate
79
+ // native C++ types needed to call `function`
80
+ template <typename Ret, typename... Args>
81
+ std::tuple<Args...> getArgsForFunction(
82
+ std::function<Ret(Args...)>,
83
+ jsi::Runtime &rt,
84
+ const jsi::Value *args,
85
+ const size_t count) {
86
+ assert(sizeof...(Args) == count);
87
+ return convertArgs<Args...>(rt, args);
88
+ }
89
+
90
+ // returns a tuple with the result of casting `args` to appropriate
91
+ // native C++ types needed to call `function`,
92
+ // passing `rt` as the first argument
93
+ template <typename Ret, typename... Args>
94
+ std::tuple<jsi::Runtime &, Args...> getArgsForFunction(
95
+ std::function<Ret(jsi::Runtime &, Args...)>,
96
+ jsi::Runtime &rt,
97
+ const jsi::Value *args,
98
+ const size_t count) {
99
+ assert(sizeof...(Args) == count);
100
+ return std::tuple_cat(std::tie(rt), convertArgs<Args...>(rt, args));
101
+ }
102
+
103
+ // calls `function` with `args`
104
+ template <typename Ret, typename... Args>
105
+ inline jsi::Value apply(
106
+ std::function<Ret(Args...)> function,
107
+ std::tuple<Args...> args) {
108
+ return std::apply(function, std::move(args));
109
+ }
110
+
111
+ // calls string-returning `function` with `args`,
112
+ // and returns the string
113
+ template <typename... Args>
114
+ inline jsi::Value apply(
115
+ jsi::Runtime &rt,
116
+ std::function<std::string(Args...)> function,
117
+ std::tuple<Args...> args) {
118
+ return jsi::String::createFromUtf8(rt, std::apply(function, std::move(args)));
119
+ }
120
+
121
+ // calls void-returning `function` with `args`,
122
+ // and returns `undefined`
123
+ template <typename... Args>
124
+ inline jsi::Value apply(
125
+ std::function<void(Args...)> function,
126
+ std::tuple<Args...> args) {
127
+ std::apply(function, std::move(args));
128
+ return jsi::Value::undefined();
129
+ }
130
+
131
+ // returns a function with JSI calling convention
132
+ // from a native function `function`
133
+ template <typename Fun>
134
+ jsi::HostFunctionType createHostFunction(Fun function) {
135
+ return [function](
136
+ jsi::Runtime &rt,
137
+ const jsi::Value &,
138
+ const jsi::Value *args,
139
+ const size_t count) {
140
+ auto argz = getArgsForFunction(function, rt, args, count);
141
+ return apply(function, std::move(argz));
142
+ };
143
+ }
144
+
145
+ // returns a function with JSI calling convention
146
+ // from a native function `function` returning a string
147
+ template <typename... Args>
148
+ jsi::HostFunctionType createHostFunction(
149
+ std::function<std::string(Args...)> function) {
150
+ return [function](
151
+ jsi::Runtime &rt,
152
+ const jsi::Value &,
153
+ const jsi::Value *args,
154
+ const size_t count) {
155
+ auto argz = getArgsForFunction(function, rt, args, count);
156
+ return apply(rt, function, std::move(argz));
157
+ };
158
+ }
159
+
160
+ // used to determine if `function<Ret(Args...)>`
161
+ // takes `Runtime &` as its first argument
162
+ template <typename... Args>
163
+ struct takes_runtime {
164
+ static constexpr size_t value = 0;
165
+ };
166
+
167
+ // specialization for `function<Ret(Runtime &, Rest...)`
168
+ template <typename... Rest>
169
+ struct takes_runtime<jsi::Runtime &, Rest...> {
170
+ static constexpr size_t value = 1;
171
+ };
172
+
173
+ // creates a JSI compatible function from `function`
174
+ // and installs it as a global function named `name`
175
+ // in the `rt` JS runtime
176
+ template <typename Ret, typename... Args>
177
+ void installJsiFunction(
178
+ jsi::Runtime &rt,
179
+ std::string_view name,
180
+ std::function<Ret(Args...)> function) {
181
+ auto clb = createHostFunction(function);
182
+ auto argsCount = sizeof...(Args) - takes_runtime<Args...>::value;
183
+ jsi::Value jsiFunction = jsi::Function::createFromHostFunction(
184
+ rt, jsi::PropNameID::forAscii(rt, name.data()), argsCount, clb);
185
+ rt.global().setProperty(rt, name.data(), jsiFunction);
186
+ }
187
+
188
+ // this should take care of passing types convertible to `function`
189
+ template <typename Fun>
190
+ void installJsiFunction(jsi::Runtime &rt, std::string_view name, Fun function) {
191
+ installJsiFunction(rt, name, std::function(std::forward<Fun>(function)));
192
+ }
193
+
194
+ jsi::Array convertStringToArray(
195
+ jsi::Runtime &rt,
196
+ const std::string &value,
197
+ const unsigned int expectedSize);
198
+
199
+ } // namespace worklets::jsi_utils
@@ -0,0 +1,95 @@
1
+ #include <worklets/Tools/JSLogger.h>
2
+ #include <worklets/Tools/ReanimatedVersion.h>
3
+
4
+ #include <memory>
5
+ #include <regex>
6
+ #include <string>
7
+
8
+ #ifdef REANIMATED_VERSION
9
+ #define STRINGIZE(x) #x
10
+ #define STRINGIZE2(x) STRINGIZE(x)
11
+ #define REANIMATED_VERSION_STRING STRINGIZE2(REANIMATED_VERSION)
12
+ #endif // REANIMATED_VERSION
13
+
14
+ using namespace facebook;
15
+
16
+ namespace worklets {
17
+
18
+ std::string getReanimatedCppVersion() {
19
+ return std::string(REANIMATED_VERSION_STRING);
20
+ }
21
+
22
+ void injectReanimatedCppVersion(jsi::Runtime &rnRuntime) {
23
+ auto version = getReanimatedCppVersion();
24
+ rnRuntime.global().setProperty(
25
+ rnRuntime,
26
+ "_REANIMATED_VERSION_CPP",
27
+ jsi::String::createFromUtf8(rnRuntime, version));
28
+ }
29
+
30
+ #ifndef NDEBUG
31
+ // This function is pretty much a copy of
32
+ // `src/reanimated2/platform-specific/checkVersion.ts`.
33
+ bool matchVersion(const std::string &version1, const std::string &version2) {
34
+ std::regex pattern("^\\d+\\.\\d+\\.\\d+$");
35
+ if (std::regex_match(version1, pattern) &&
36
+ std::regex_match(version2, pattern)) {
37
+ auto majorPattern = std::regex("^\\d+");
38
+ auto major1 = std::regex_search(version1, majorPattern);
39
+ auto major2 = std::regex_search(version2, majorPattern);
40
+ if (major1 != major2) {
41
+ return false;
42
+ }
43
+ auto minorPattern = std::regex("\\.\\d+\\.");
44
+ auto minor1 = std::regex_search(version1, minorPattern);
45
+ auto minor2 = std::regex_search(version2, minorPattern);
46
+ if (minor1 != minor2) {
47
+ return false;
48
+ }
49
+ return true;
50
+ } else {
51
+ return version1 == version2;
52
+ }
53
+ }
54
+
55
+ void checkJSVersion(
56
+ jsi::Runtime &rnRuntime,
57
+ const std::shared_ptr<JSLogger> &jsLogger) {
58
+ auto cppVersion = getReanimatedCppVersion();
59
+
60
+ auto maybeJSVersion =
61
+ rnRuntime.global().getProperty(rnRuntime, "_REANIMATED_VERSION_JS");
62
+ if (maybeJSVersion.isUndefined()) {
63
+ jsLogger->warnOnJS(
64
+ std::string(
65
+ "[Reanimated] C++ side failed to resolve JavaScript code version\n") +
66
+ "See `https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#c-side-failed-to-resolve-javascript-code-version` for more details.");
67
+ return;
68
+ }
69
+
70
+ auto jsVersion = maybeJSVersion.asString(rnRuntime).utf8(rnRuntime);
71
+
72
+ if (!matchVersion(cppVersion, jsVersion)) {
73
+ jsLogger->warnOnJS(
74
+ std::string(
75
+ "[Reanimated] Mismatch between C++ code version and JavaScript code version (") +
76
+ cppVersion + " vs. " + jsVersion + " respectively).\n" +
77
+ "See `https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-c-code-version-and-javascript-code-version` for more details.");
78
+ return;
79
+ }
80
+ }
81
+ #else
82
+ void checkJSVersion(
83
+ jsi::Runtime &rnRuntime,
84
+ const std::shared_ptr<worklets::JSLogger> &jsLogger) {
85
+ // In release builds we don't check the version, hence
86
+ // this function is a NOOP.
87
+ }
88
+
89
+ bool matchVersion(const std::string &version1, const std::string &version2) {
90
+ // Stub implementation for release builds.
91
+ return true;
92
+ }
93
+ #endif // NDEBUG
94
+
95
+ }; // namespace worklets
@@ -0,0 +1,19 @@
1
+ #pragma once
2
+
3
+ #include <worklets/Tools/JSLogger.h>
4
+
5
+ #include <jsi/jsi.h>
6
+
7
+ #include <memory>
8
+ #include <string>
9
+
10
+ using namespace facebook;
11
+
12
+ namespace worklets {
13
+
14
+ std::string getReanimatedCppVersion();
15
+ void injectReanimatedCppVersion(jsi::Runtime &);
16
+ bool matchVersion(const std::string &, const std::string &);
17
+ void checkJSVersion(jsi::Runtime &, const std::shared_ptr<JSLogger> &);
18
+
19
+ }; // namespace worklets
@@ -0,0 +1,72 @@
1
+ #pragma once
2
+
3
+ #ifndef NDEBUG
4
+
5
+ #include <cxxabi.h>
6
+
7
+ #include <atomic>
8
+ #include <cassert>
9
+ #include <iostream>
10
+ #include <string>
11
+
12
+ #ifdef ANDROID
13
+ #include <android/log.h>
14
+ #endif // ANDROID
15
+
16
+ namespace worklets {
17
+
18
+ // This is a class that counts how many instances of a different class there
19
+ // are. It is meant only to be used with classes that should only have one
20
+ // instance.
21
+
22
+ template <class T>
23
+ class SingleInstanceChecker {
24
+ public:
25
+ SingleInstanceChecker();
26
+ ~SingleInstanceChecker();
27
+
28
+ private:
29
+ void assertWithMessage(bool condition, std::string message) {
30
+ if (!condition) {
31
+ #ifdef ANDROID
32
+ __android_log_print(
33
+ ANDROID_LOG_WARN, "Reanimated", "%s", message.c_str());
34
+ #else
35
+ std::cerr << "[Reanimated] " << message << std::endl;
36
+ #endif
37
+
38
+ #ifdef IS_REANIMATED_EXAMPLE_APP
39
+ assert(false);
40
+ #endif
41
+ }
42
+ }
43
+
44
+ // A static field will exist separately for every class template.
45
+ // This has to be inline for automatic initialization.
46
+ inline static std::atomic<int> instanceCount_;
47
+ };
48
+
49
+ template <class T>
50
+ SingleInstanceChecker<T>::SingleInstanceChecker() {
51
+ int status = 0;
52
+ std::string className =
53
+ __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
54
+
55
+ // React Native can spawn up to two instances of a Native Module at the same
56
+ // time. This happens during a reload when a new instance of React Native is
57
+ // created while simultaneously the old one is torn down.
58
+ instanceCount_++;
59
+ assertWithMessage(
60
+ instanceCount_ <= 2,
61
+ "[Reanimated] More than two instances of " + className +
62
+ " present. This may indicate a memory leak due to a retain cycle.");
63
+ }
64
+
65
+ template <class T>
66
+ SingleInstanceChecker<T>::~SingleInstanceChecker() {
67
+ instanceCount_--;
68
+ }
69
+
70
+ } // namespace worklets
71
+
72
+ #endif // NDEBUG
@@ -0,0 +1,49 @@
1
+ #pragma once
2
+
3
+ #include <condition_variable>
4
+ #include <mutex>
5
+ #include <queue>
6
+ #include <utility>
7
+
8
+ namespace worklets {
9
+
10
+ //
11
+ // Copyright (c) 2013 Juan Palacios juan.palacios.puyana@gmail.com
12
+ // Subject to the BSD 2-Clause License
13
+ // - see < https://opensource.org/license/bsd-2-clause/ >
14
+ //
15
+ template <typename T>
16
+ class ThreadSafeQueue {
17
+ public:
18
+ T pop() {
19
+ std::unique_lock<std::mutex> mlock(mutex_);
20
+ while (queue_.empty()) {
21
+ cond_.wait(mlock);
22
+ }
23
+ const auto item = queue_.front();
24
+ queue_.pop();
25
+ return item;
26
+ }
27
+
28
+ void push(T &&item) {
29
+ std::unique_lock<std::mutex> mlock(mutex_);
30
+ queue_.push(std::move(item));
31
+ mlock.unlock();
32
+ cond_.notify_one();
33
+ }
34
+
35
+ bool empty() const {
36
+ std::unique_lock<std::mutex> mlock(mutex_);
37
+ const auto res = queue_.empty();
38
+ mlock.unlock();
39
+ cond_.notify_one();
40
+ return res;
41
+ }
42
+
43
+ private:
44
+ std::queue<T> queue_;
45
+ mutable std::mutex mutex_;
46
+ mutable std::condition_variable cond_;
47
+ };
48
+
49
+ } // namespace worklets
@@ -0,0 +1,20 @@
1
+ #include <worklets/Tools/UIScheduler.h>
2
+ #include <worklets/WorkletRuntime/ReanimatedRuntime.h>
3
+
4
+ #include <utility>
5
+
6
+ namespace worklets {
7
+
8
+ void UIScheduler::scheduleOnUI(std::function<void()> job) {
9
+ uiJobs_.push(std::move(job));
10
+ }
11
+
12
+ void UIScheduler::triggerUI() {
13
+ scheduledOnUI_ = false;
14
+ while (!uiJobs_.empty()) {
15
+ const auto job = uiJobs_.pop();
16
+ job();
17
+ }
18
+ }
19
+
20
+ } // namespace worklets
@@ -0,0 +1,22 @@
1
+ #pragma once
2
+
3
+ #include <worklets/Tools/ThreadSafeQueue.h>
4
+
5
+ #include <ReactCommon/CallInvoker.h>
6
+
7
+ #include <atomic>
8
+
9
+ namespace worklets {
10
+
11
+ class UIScheduler {
12
+ public:
13
+ virtual void scheduleOnUI(std::function<void()> job);
14
+ virtual void triggerUI();
15
+ virtual ~UIScheduler() = default;
16
+
17
+ protected:
18
+ std::atomic<bool> scheduledOnUI_{false};
19
+ ThreadSafeQueue<std::function<void()>> uiJobs_;
20
+ };
21
+
22
+ } // namespace worklets
@@ -0,0 +1,29 @@
1
+ #include <worklets/Tools/WorkletEventHandler.h>
2
+
3
+ namespace worklets {
4
+
5
+ void WorkletEventHandler::process(
6
+ const std::shared_ptr<WorkletRuntime> &workletRuntime,
7
+ const double eventTimestamp,
8
+ const jsi::Value &eventValue) const {
9
+ workletRuntime->runGuarded(
10
+ handlerFunction_, jsi::Value(eventTimestamp), eventValue);
11
+ }
12
+
13
+ uint64_t WorkletEventHandler::getHandlerId() const {
14
+ return handlerId_;
15
+ }
16
+
17
+ const std::string &WorkletEventHandler::getEventName() const {
18
+ return eventName_;
19
+ }
20
+
21
+ uint64_t WorkletEventHandler::getEmitterReactTag() const {
22
+ return emitterReactTag_;
23
+ }
24
+
25
+ bool WorkletEventHandler::shouldIgnoreEmitterReactTag() const {
26
+ return emitterReactTag_ == -1;
27
+ }
28
+
29
+ } // namespace worklets
@@ -0,0 +1,41 @@
1
+ #pragma once
2
+
3
+ #include <worklets/SharedItems/Shareables.h>
4
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
5
+
6
+ #include <jsi/jsi.h>
7
+
8
+ #include <memory>
9
+ #include <string>
10
+
11
+ using namespace facebook;
12
+
13
+ namespace worklets {
14
+
15
+ class WorkletEventHandler {
16
+ const uint64_t handlerId_;
17
+ const uint64_t emitterReactTag_;
18
+ const std::string eventName_;
19
+ const std::shared_ptr<ShareableWorklet> handlerFunction_;
20
+
21
+ public:
22
+ WorkletEventHandler(
23
+ const uint64_t handlerId,
24
+ const std::string &eventName,
25
+ const uint64_t emitterReactTag,
26
+ const std::shared_ptr<ShareableWorklet> &handlerFunction)
27
+ : handlerId_(handlerId),
28
+ emitterReactTag_(emitterReactTag),
29
+ eventName_(eventName),
30
+ handlerFunction_(handlerFunction) {}
31
+ void process(
32
+ const std::shared_ptr<WorkletRuntime> &workletRuntime,
33
+ double eventTimestamp,
34
+ const jsi::Value &eventValue) const;
35
+ uint64_t getHandlerId() const;
36
+ const std::string &getEventName() const;
37
+ uint64_t getEmitterReactTag() const;
38
+ bool shouldIgnoreEmitterReactTag() const;
39
+ };
40
+
41
+ } // namespace worklets
@@ -0,0 +1,16 @@
1
+ #include <worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h>
2
+
3
+ namespace worklets {
4
+
5
+ void RNRuntimeWorkletDecorator::decorate(
6
+ jsi::Runtime &rnRuntime,
7
+ const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy) {
8
+ rnRuntime.global().setProperty(rnRuntime, "_WORKLET", false);
9
+
10
+ rnRuntime.global().setProperty(
11
+ rnRuntime,
12
+ "__workletsModuleProxy",
13
+ jsi::Object::createFromHostObject(rnRuntime, workletsModuleProxy));
14
+ }
15
+
16
+ } // namespace worklets
@@ -0,0 +1,19 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <worklets/NativeModules/WorkletsModuleProxy.h>
5
+ #include <memory>
6
+
7
+ using namespace facebook;
8
+
9
+ namespace worklets {
10
+
11
+ class RNRuntimeWorkletDecorator {
12
+ // TODO: Rename to `RNRuntimeWorkletsDecorator` or something more suitable.
13
+ public:
14
+ static void decorate(
15
+ jsi::Runtime &rnRuntime,
16
+ const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy);
17
+ };
18
+
19
+ } // namespace worklets