react-native-worklets 0.0.1-alpha → 0.1.0

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 (246) hide show
  1. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +71 -0
  2. package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +38 -0
  3. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +131 -0
  4. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +82 -0
  5. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +72 -0
  6. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +44 -0
  7. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
  8. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -0
  9. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
  10. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -0
  11. package/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -0
  12. package/Common/cpp/worklets/SharedItems/Shareables.h +345 -0
  13. package/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -0
  14. package/Common/cpp/worklets/Tools/AsyncQueue.h +35 -0
  15. package/Common/cpp/worklets/Tools/Defs.h +10 -0
  16. package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
  17. package/Common/cpp/worklets/Tools/JSISerializer.h +47 -0
  18. package/Common/cpp/worklets/Tools/JSLogger.cpp +16 -0
  19. package/Common/cpp/worklets/Tools/JSLogger.h +20 -0
  20. package/Common/cpp/worklets/Tools/JSScheduler.cpp +10 -0
  21. package/Common/cpp/worklets/Tools/JSScheduler.h +29 -0
  22. package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
  23. package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -0
  24. package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
  25. package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
  26. package/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
  27. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
  28. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
  29. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +26 -0
  30. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +199 -0
  31. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +20 -0
  32. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -0
  33. package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
  34. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +19 -0
  35. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
  36. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +108 -0
  37. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +127 -0
  38. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +183 -0
  39. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -0
  40. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
  41. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +179 -0
  42. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -0
  43. package/LICENSE +20 -0
  44. package/README.md +27 -0
  45. package/RNWorklets.podspec +70 -0
  46. package/android/CMakeLists.txt +56 -0
  47. package/android/build.gradle +313 -0
  48. package/android/gradle.properties +5 -0
  49. package/android/proguard-rules.pro +3 -0
  50. package/android/spotless.gradle +9 -0
  51. package/android/src/main/AndroidManifest.xml +2 -0
  52. package/android/src/main/cpp/worklets/CMakeLists.txt +85 -0
  53. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
  54. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +41 -0
  55. package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +32 -0
  56. package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
  57. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +83 -0
  58. package/android/src/main/cpp/worklets/android/WorkletsModule.h +63 -0
  59. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +13 -0
  60. package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.java +60 -0
  61. package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameCallback.java +20 -0
  62. package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameQueue.java +113 -0
  63. package/android/src/main/java/com/swmansion/worklets/JSCallInvokerResolver.java +27 -0
  64. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThread.java +16 -0
  65. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThreadBase.java +72 -0
  66. package/android/src/main/java/com/swmansion/worklets/WorkletsModule.java +106 -0
  67. package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +49 -0
  68. package/android/src/paper/com/swmansion/worklets/NativeWorkletsModuleSpec.java +26 -0
  69. package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
  70. package/apple/worklets/apple/AnimationFrameQueue.mm +81 -0
  71. package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
  72. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
  73. package/apple/worklets/apple/IOSUIScheduler.h +14 -0
  74. package/apple/worklets/apple/IOSUIScheduler.mm +24 -0
  75. package/apple/worklets/apple/PlatformLogger.mm +31 -0
  76. package/apple/worklets/apple/SlowAnimations.h +8 -0
  77. package/apple/worklets/apple/SlowAnimations.mm +47 -0
  78. package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
  79. package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
  80. package/apple/worklets/apple/WorkletsMessageThread.mm +32 -0
  81. package/apple/worklets/apple/WorkletsModule.h +10 -0
  82. package/apple/worklets/apple/WorkletsModule.mm +85 -0
  83. package/lib/module/PlatformChecker.js +35 -0
  84. package/lib/module/PlatformChecker.js.map +1 -0
  85. package/lib/module/WorkletsError.js +13 -0
  86. package/lib/module/WorkletsError.js.map +1 -0
  87. package/lib/module/WorkletsModule/JSWorklets.js +36 -0
  88. package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
  89. package/lib/module/WorkletsModule/NativeWorklets.js +39 -0
  90. package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
  91. package/lib/module/WorkletsModule/index.js +4 -0
  92. package/lib/module/WorkletsModule/index.js.map +1 -0
  93. package/lib/module/WorkletsModule/workletsModuleInstance.js +7 -0
  94. package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -0
  95. package/lib/module/WorkletsModule/workletsModuleInstance.web.js +5 -0
  96. package/lib/module/WorkletsModule/workletsModuleInstance.web.js.map +1 -0
  97. package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
  98. package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
  99. package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js +10 -0
  100. package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js.map +1 -0
  101. package/lib/module/animationFrameQueue/requestAnimationFrame.js +36 -0
  102. package/lib/module/animationFrameQueue/requestAnimationFrame.js.map +1 -0
  103. package/lib/module/errors.js +78 -0
  104. package/lib/module/errors.js.map +1 -0
  105. package/lib/module/index.js +17 -0
  106. package/lib/module/index.js.map +1 -0
  107. package/lib/module/initializers.js +158 -0
  108. package/lib/module/initializers.js.map +1 -0
  109. package/lib/module/logger/LogBox.js +15 -0
  110. package/lib/module/logger/LogBox.js.map +1 -0
  111. package/lib/module/logger/index.js +5 -0
  112. package/lib/module/logger/index.js.map +1 -0
  113. package/lib/module/logger/logger.js +137 -0
  114. package/lib/module/logger/logger.js.map +1 -0
  115. package/lib/module/privateGlobals.d.js +8 -0
  116. package/lib/module/privateGlobals.d.js.map +1 -0
  117. package/lib/module/runtimes.js +63 -0
  118. package/lib/module/runtimes.js.map +1 -0
  119. package/lib/module/shareableMappingCache.js +39 -0
  120. package/lib/module/shareableMappingCache.js.map +1 -0
  121. package/lib/module/shareables.js +417 -0
  122. package/lib/module/shareables.js.map +1 -0
  123. package/lib/module/specs/NativeWorkletsModule.js +5 -0
  124. package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
  125. package/lib/module/specs/index.js +5 -0
  126. package/lib/module/specs/index.js.map +1 -0
  127. package/lib/module/threads.js +204 -0
  128. package/lib/module/threads.js.map +1 -0
  129. package/lib/module/valueUnpacker.js +83 -0
  130. package/lib/module/valueUnpacker.js.map +1 -0
  131. package/lib/module/workletFunction.js +37 -0
  132. package/lib/module/workletFunction.js.map +1 -0
  133. package/lib/module/workletTypes.js +12 -0
  134. package/lib/module/workletTypes.js.map +1 -0
  135. package/lib/typescript/PlatformChecker.d.ts +7 -0
  136. package/lib/typescript/PlatformChecker.d.ts.map +1 -0
  137. package/lib/typescript/WorkletsError.d.ts +3 -0
  138. package/lib/typescript/WorkletsError.d.ts.map +1 -0
  139. package/lib/typescript/WorkletsModule/JSWorklets.d.ts +3 -0
  140. package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -0
  141. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +5 -0
  142. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
  143. package/lib/typescript/WorkletsModule/index.d.ts +3 -0
  144. package/lib/typescript/WorkletsModule/index.d.ts.map +1 -0
  145. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +2 -0
  146. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -0
  147. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +2 -0
  148. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -0
  149. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +12 -0
  150. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
  151. package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts +6 -0
  152. package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts.map +1 -0
  153. package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts +2 -0
  154. package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts.map +1 -0
  155. package/lib/typescript/errors.d.ts +19 -0
  156. package/lib/typescript/errors.d.ts.map +1 -0
  157. package/lib/typescript/index.d.ts +13 -0
  158. package/lib/typescript/index.d.ts.map +1 -0
  159. package/lib/typescript/initializers.d.ts +6 -0
  160. package/lib/typescript/initializers.d.ts.map +1 -0
  161. package/lib/typescript/logger/LogBox.d.ts +32 -0
  162. package/lib/typescript/logger/LogBox.d.ts.map +1 -0
  163. package/lib/typescript/logger/index.d.ts +3 -0
  164. package/lib/typescript/logger/index.d.ts.map +1 -0
  165. package/lib/typescript/logger/logger.d.ts +52 -0
  166. package/lib/typescript/logger/logger.d.ts.map +1 -0
  167. package/lib/typescript/runtimes.d.ts +16 -0
  168. package/lib/typescript/runtimes.d.ts.map +1 -0
  169. package/lib/typescript/shareableMappingCache.d.ts +16 -0
  170. package/lib/typescript/shareableMappingCache.d.ts.map +1 -0
  171. package/lib/typescript/shareables.d.ts +15 -0
  172. package/lib/typescript/shareables.d.ts.map +1 -0
  173. package/lib/typescript/specs/NativeWorkletsModule.d.ts +7 -0
  174. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
  175. package/lib/typescript/specs/index.d.ts +3 -0
  176. package/lib/typescript/specs/index.d.ts.map +1 -0
  177. package/lib/typescript/threads.d.ts +49 -0
  178. package/lib/typescript/threads.d.ts.map +1 -0
  179. package/lib/typescript/valueUnpacker.d.ts +2 -0
  180. package/lib/typescript/valueUnpacker.d.ts.map +1 -0
  181. package/lib/typescript/workletFunction.d.ts +27 -0
  182. package/lib/typescript/workletFunction.d.ts.map +1 -0
  183. package/lib/typescript/workletTypes.d.ts +49 -0
  184. package/lib/typescript/workletTypes.d.ts.map +1 -0
  185. package/package.json +106 -8
  186. package/plugin/index.js +3 -0
  187. package/scripts/worklets_utils.rb +53 -0
  188. package/src/PlatformChecker.ts +43 -0
  189. package/src/WorkletsError.ts +13 -0
  190. package/src/WorkletsModule/JSWorklets.ts +57 -0
  191. package/src/WorkletsModule/NativeWorklets.ts +68 -0
  192. package/src/WorkletsModule/index.ts +7 -0
  193. package/src/WorkletsModule/workletsModuleInstance.ts +9 -0
  194. package/src/WorkletsModule/workletsModuleInstance.web.ts +5 -0
  195. package/src/WorkletsModule/workletsModuleProxy.ts +30 -0
  196. package/src/animationFrameQueue/mockedRequestAnimationFrame.ts +11 -0
  197. package/src/animationFrameQueue/requestAnimationFrame.ts +41 -0
  198. package/src/errors.ts +103 -0
  199. package/src/index.ts +42 -0
  200. package/src/initializers.ts +175 -0
  201. package/src/logger/LogBox.ts +55 -0
  202. package/src/logger/index.ts +3 -0
  203. package/src/logger/logger.ts +155 -0
  204. package/src/privateGlobals.d.ts +41 -0
  205. package/src/runtimes.ts +92 -0
  206. package/src/shareableMappingCache.ts +44 -0
  207. package/src/shareables.ts +577 -0
  208. package/src/specs/NativeWorkletsModule.ts +9 -0
  209. package/src/specs/index.ts +5 -0
  210. package/src/threads.ts +275 -0
  211. package/src/valueUnpacker.ts +110 -0
  212. package/src/workletFunction.ts +47 -0
  213. package/src/workletTypes.ts +76 -0
  214. package/Animated.js +0 -13
  215. package/AnimatedEvent.js +0 -167
  216. package/AnimatedImplementation.js +0 -666
  217. package/CoreAnimated.js +0 -43
  218. package/Easing.js +0 -236
  219. package/NativeAnimatedHelper.js +0 -226
  220. package/SpringConfig.js +0 -79
  221. package/animations/Animation.js +0 -36
  222. package/animations/DecayAnimation.js +0 -70
  223. package/animations/SpringAnimation.js +0 -125
  224. package/animations/TimingAnimation.js +0 -70
  225. package/bezier.js +0 -128
  226. package/createAnimatedComponent.js +0 -188
  227. package/nodes/AnimatedBlock.js +0 -19
  228. package/nodes/AnimatedClock.js +0 -76
  229. package/nodes/AnimatedCond.js +0 -23
  230. package/nodes/AnimatedDetach.js +0 -15
  231. package/nodes/AnimatedInterpolation.js +0 -338
  232. package/nodes/AnimatedNode.js +0 -97
  233. package/nodes/AnimatedOnChange.js +0 -28
  234. package/nodes/AnimatedOp.js +0 -17
  235. package/nodes/AnimatedProps.js +0 -154
  236. package/nodes/AnimatedSet.js +0 -19
  237. package/nodes/AnimatedStartClock.js +0 -21
  238. package/nodes/AnimatedStopClock.js +0 -21
  239. package/nodes/AnimatedStyle.js +0 -89
  240. package/nodes/AnimatedTracking.js +0 -36
  241. package/nodes/AnimatedTransform.js +0 -93
  242. package/nodes/AnimatedValue.js +0 -271
  243. package/nodes/AnimatedWithInput.js +0 -21
  244. package/nodes/SpringNode.js +0 -106
  245. package/nodes/TimingStep.js +0 -44
  246. package/utils.js +0 -28
@@ -0,0 +1,108 @@
1
+ #include <worklets/WorkletRuntime/WorkletHermesRuntime.h>
2
+
3
+ // Only include this file in Hermes-enabled builds as some platforms (like tvOS)
4
+ // don't support hermes and it causes the compilation to fail.
5
+ #if JS_RUNTIME_HERMES
6
+
7
+ #include <cxxreact/MessageQueueThread.h>
8
+ #include <jsi/decorator.h>
9
+ #include <jsi/jsi.h>
10
+
11
+ #include <memory>
12
+ #include <string>
13
+ #include <utility>
14
+
15
+ namespace worklets {
16
+
17
+ using namespace facebook;
18
+ using namespace react;
19
+ #if HERMES_ENABLE_DEBUGGER
20
+ using namespace facebook::hermes::inspector_modern;
21
+ #endif // HERMES_ENABLE_DEBUGGER
22
+
23
+ #if HERMES_ENABLE_DEBUGGER
24
+
25
+ class HermesExecutorRuntimeAdapter : public RuntimeAdapter {
26
+ public:
27
+ explicit HermesExecutorRuntimeAdapter(
28
+ facebook::hermes::HermesRuntime &hermesRuntime,
29
+ const std::shared_ptr<MessageQueueThread> &thread)
30
+ : hermesRuntime_(hermesRuntime), thread_(std::move(thread)) {}
31
+
32
+ virtual ~HermesExecutorRuntimeAdapter() {
33
+ // This is required by iOS, because there is an assertion in the destructor
34
+ // that the thread was indeed `quit` before
35
+ thread_->quitSynchronous();
36
+ }
37
+
38
+ facebook::hermes::HermesRuntime &getRuntime() override {
39
+ return hermesRuntime_;
40
+ }
41
+
42
+ // This is not empty in the original implementation, but we decided to tickle
43
+ // the runtime by running a small piece of code on every frame as using this
44
+ // required us to hold a refernce to the runtime inside this adapter which
45
+ // caused issues while reloading the app.
46
+ void tickleJs() override {}
47
+
48
+ public:
49
+ facebook::hermes::HermesRuntime &hermesRuntime_;
50
+ std::shared_ptr<MessageQueueThread> thread_;
51
+ };
52
+
53
+ #endif // HERMES_ENABLE_DEBUGGER
54
+
55
+ WorkletHermesRuntime::WorkletHermesRuntime(
56
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime,
57
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
58
+ const std::string &name)
59
+ : jsi::WithRuntimeDecorator<WorkletsReentrancyCheck>(
60
+ *runtime,
61
+ reentrancyCheck_),
62
+ runtime_(std::move(runtime)) {
63
+ #if HERMES_ENABLE_DEBUGGER
64
+ auto adapter =
65
+ std::make_unique<HermesExecutorRuntimeAdapter>(*runtime_, jsQueue);
66
+ debugToken_ = chrome::enableDebugging(std::move(adapter), name);
67
+ #endif // HERMES_ENABLE_DEBUGGER
68
+
69
+ #ifndef NDEBUG
70
+ facebook::hermes::HermesRuntime *wrappedRuntime = runtime_.get();
71
+ jsi::Value evalWithSourceMap = jsi::Function::createFromHostFunction(
72
+ *runtime_,
73
+ jsi::PropNameID::forAscii(*runtime_, "evalWithSourceMap"),
74
+ 3,
75
+ [wrappedRuntime](
76
+ jsi::Runtime &rt,
77
+ const jsi::Value &thisValue,
78
+ const jsi::Value *args,
79
+ size_t count) -> jsi::Value {
80
+ auto code = std::make_shared<const jsi::StringBuffer>(
81
+ args[0].asString(rt).utf8(rt));
82
+ std::string sourceURL;
83
+ if (count > 1 && args[1].isString()) {
84
+ sourceURL = args[1].asString(rt).utf8(rt);
85
+ }
86
+ std::shared_ptr<const jsi::Buffer> sourceMap;
87
+ if (count > 2 && args[2].isString()) {
88
+ sourceMap = std::make_shared<const jsi::StringBuffer>(
89
+ args[2].asString(rt).utf8(rt));
90
+ }
91
+ return wrappedRuntime->evaluateJavaScriptWithSourceMap(
92
+ code, sourceMap, sourceURL);
93
+ });
94
+ runtime_->global().setProperty(
95
+ *runtime_, "evalWithSourceMap", evalWithSourceMap);
96
+ #endif // NDEBUG
97
+ }
98
+
99
+ WorkletHermesRuntime::~WorkletHermesRuntime() {
100
+ #if HERMES_ENABLE_DEBUGGER
101
+ // We have to disable debugging before the runtime is destroyed.
102
+ chrome::disableDebugging(debugToken_);
103
+ #endif // HERMES_ENABLE_DEBUGGER
104
+ }
105
+
106
+ } // namespace worklets
107
+
108
+ #endif // JS_RUNTIME_HERMES
@@ -0,0 +1,127 @@
1
+ #pragma once
2
+
3
+ #include <worklets/Tools/Defs.h>
4
+
5
+ // Only include this file in Hermes-enabled builds as some platforms (like tvOS)
6
+ // don't support hermes and it causes the compilation to fail.
7
+ #if JS_RUNTIME_HERMES
8
+
9
+ #include <cxxreact/MessageQueueThread.h>
10
+ #include <hermes/hermes.h>
11
+ #include <jsi/decorator.h>
12
+ #include <jsi/jsi.h>
13
+
14
+ #include <atomic>
15
+ #include <memory>
16
+ #include <string>
17
+ #include <thread>
18
+
19
+ #if HERMES_ENABLE_DEBUGGER
20
+ #include <hermes/inspector-modern/chrome/Registration.h>
21
+ #endif // HERMES_ENABLE_DEBUGGER
22
+
23
+ namespace worklets {
24
+
25
+ using namespace facebook;
26
+ using namespace react;
27
+ #if HERMES_ENABLE_DEBUGGER
28
+ using namespace facebook::hermes::inspector_modern;
29
+ #endif // HERMES_ENABLE_DEBUGGER
30
+
31
+ // ReentrancyCheck is copied from React Native
32
+ // from ReactCommon/hermes/executor/HermesExecutorFactory.cpp
33
+ // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp
34
+ struct WorkletsReentrancyCheck {
35
+ // This is effectively a very subtle and complex assert, so only
36
+ // include it in builds which would include asserts.
37
+ #ifndef NDEBUG
38
+ WorkletsReentrancyCheck() : tid(std::thread::id()), depth(0) {}
39
+
40
+ void before() {
41
+ std::thread::id this_id = std::this_thread::get_id();
42
+ std::thread::id expected = std::thread::id();
43
+
44
+ // A note on memory ordering: the main purpose of these checks is
45
+ // to observe a before/before race, without an intervening after.
46
+ // This will be detected by the compare_exchange_strong atomicity
47
+ // properties, regardless of memory order.
48
+ //
49
+ // For everything else, it is easiest to think of 'depth' as a
50
+ // proxy for any access made inside the VM. If access to depth
51
+ // are reordered incorrectly, the same could be true of any other
52
+ // operation made by the VM. In fact, using acquire/release
53
+ // memory ordering could create barriers which mask a programmer
54
+ // error. So, we use relaxed memory order, to avoid masking
55
+ // actual ordering errors. Although, in practice, ordering errors
56
+ // of this sort would be surprising, because the decorator would
57
+ // need to call after() without before().
58
+
59
+ if (tid.compare_exchange_strong(
60
+ expected, this_id, std::memory_order_relaxed)) {
61
+ // Returns true if tid and expected were the same. If they
62
+ // were, then the stored tid referred to no thread, and we
63
+ // atomically saved this thread's tid. Now increment depth.
64
+ assert(depth == 0 && "[Worklets] No thread id, but depth != 0");
65
+ ++depth;
66
+ } else if (expected == this_id) {
67
+ // If the stored tid referred to a thread, expected was set to
68
+ // that value. If that value is this thread's tid, that's ok,
69
+ // just increment depth again.
70
+ assert(depth != 0 && "[Worklets] Thread id was set, but depth == 0");
71
+ ++depth;
72
+ } else {
73
+ // The stored tid was some other thread. This indicates a bad
74
+ // programmer error, where VM methods were called on two
75
+ // different threads unsafely. Fail fast (and hard) so the
76
+ // crash can be analyzed.
77
+ __builtin_trap();
78
+ }
79
+ }
80
+
81
+ void after() {
82
+ assert(
83
+ tid.load(std::memory_order_relaxed) == std::this_thread::get_id() &&
84
+ "[Worklets] No thread id in after()");
85
+ if (--depth == 0) {
86
+ // If we decremented depth to zero, store no-thread into tid.
87
+ std::thread::id expected = std::this_thread::get_id();
88
+ bool didWrite = tid.compare_exchange_strong(
89
+ expected, std::thread::id(), std::memory_order_relaxed);
90
+ assert(didWrite && "[Worklets] Decremented to zero, but no tid write");
91
+ }
92
+ }
93
+
94
+ std::atomic<std::thread::id> tid;
95
+ // This is not atomic, as it is only written or read from the owning
96
+ // thread.
97
+ unsigned int depth;
98
+ #endif // NDEBUG
99
+ };
100
+
101
+ // This is in fact a subclass of jsi::Runtime! WithRuntimeDecorator is a
102
+ // template class that is a subclass of DecoratedRuntime which is also a
103
+ // template class that then inherits its template, which in this case is
104
+ // jsi::Runtime. So the inheritance is: WorkletHermesRuntime ->
105
+ // WithRuntimeDecorator -> DecoratedRuntime -> jsi::Runtime You can find out
106
+ // more about this in ReactCommon/jsi/jsi/Decorator.h or by following this link:
107
+ // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/jsi/jsi/decorator.h
108
+ class WorkletHermesRuntime
109
+ : public jsi::WithRuntimeDecorator<WorkletsReentrancyCheck> {
110
+ public:
111
+ WorkletHermesRuntime(
112
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime,
113
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
114
+ const std::string &name);
115
+ ~WorkletHermesRuntime();
116
+
117
+ private:
118
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime_;
119
+ WorkletsReentrancyCheck reentrancyCheck_;
120
+ #if HERMES_ENABLE_DEBUGGER
121
+ chrome::DebugSessionToken debugToken_;
122
+ #endif // HERMES_ENABLE_DEBUGGER
123
+ };
124
+
125
+ } // namespace worklets
126
+
127
+ #endif // JS_RUNTIME_HERMES
@@ -0,0 +1,183 @@
1
+ #include <worklets/Tools/Defs.h>
2
+ #include <worklets/Tools/JSISerializer.h>
3
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
4
+ #include <worklets/WorkletRuntime/WorkletRuntimeCollector.h>
5
+ #include <worklets/WorkletRuntime/WorkletRuntimeDecorator.h>
6
+
7
+ #include <cxxreact/MessageQueueThread.h>
8
+ #include <jsi/decorator.h>
9
+ #include <jsi/jsi.h>
10
+
11
+ #include <memory>
12
+ #include <utility>
13
+
14
+ #if JS_RUNTIME_HERMES
15
+ #include <worklets/WorkletRuntime/WorkletHermesRuntime.h>
16
+ #elif JS_RUNTIME_V8
17
+ #include <v8runtime/V8RuntimeFactory.h>
18
+ #else
19
+ #include <jsc/JSCRuntime.h>
20
+ #endif // JS_RUNTIME
21
+
22
+ namespace worklets {
23
+
24
+ class AroundLock {
25
+ const std::shared_ptr<std::recursive_mutex> mutex_;
26
+
27
+ public:
28
+ explicit AroundLock(const std::shared_ptr<std::recursive_mutex> &mutex)
29
+ : mutex_(mutex) {}
30
+
31
+ void before() const {
32
+ mutex_->lock();
33
+ }
34
+
35
+ void after() const {
36
+ mutex_->unlock();
37
+ }
38
+ };
39
+
40
+ class LockableRuntime : public jsi::WithRuntimeDecorator<AroundLock> {
41
+ AroundLock aroundLock_;
42
+ std::shared_ptr<jsi::Runtime> runtime_;
43
+
44
+ public:
45
+ explicit LockableRuntime(
46
+ std::shared_ptr<jsi::Runtime> &runtime,
47
+ const std::shared_ptr<std::recursive_mutex> &runtimeMutex)
48
+ : jsi::WithRuntimeDecorator<AroundLock>(*runtime, aroundLock_),
49
+ aroundLock_(runtimeMutex),
50
+ runtime_(std::move(runtime)) {}
51
+ };
52
+
53
+ static std::shared_ptr<jsi::Runtime> makeRuntime(
54
+ jsi::Runtime &rnRuntime,
55
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
56
+ const std::string &name,
57
+ const bool supportsLocking,
58
+ const std::shared_ptr<std::recursive_mutex> &runtimeMutex) {
59
+ std::shared_ptr<jsi::Runtime> jsiRuntime;
60
+ #if JS_RUNTIME_HERMES
61
+ (void)rnRuntime; // used only by V8
62
+ auto hermesRuntime = facebook::hermes::makeHermesRuntime();
63
+ jsiRuntime = std::make_shared<WorkletHermesRuntime>(
64
+ std::move(hermesRuntime), jsQueue, name);
65
+ #elif JS_RUNTIME_V8
66
+ auto config = std::make_unique<rnv8::V8RuntimeConfig>();
67
+ config->enableInspector = false;
68
+ config->appName = name;
69
+ jsiRuntime = rnv8::createSharedV8Runtime(&rnRuntime, std::move(config));
70
+ #else
71
+ (void)rnRuntime; // used only by V8
72
+ jsiRuntime = facebook::jsc::makeJSCRuntime();
73
+ #endif
74
+
75
+ if (supportsLocking) {
76
+ return std::make_shared<LockableRuntime>(jsiRuntime, runtimeMutex);
77
+ } else {
78
+ return jsiRuntime;
79
+ }
80
+ }
81
+
82
+ WorkletRuntime::WorkletRuntime(
83
+ jsi::Runtime &rnRuntime,
84
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
85
+ const std::shared_ptr<JSScheduler> &jsScheduler,
86
+ const std::string &name,
87
+ const bool supportsLocking,
88
+ const std::string &valueUnpackerCode)
89
+ : runtimeMutex_(std::make_shared<std::recursive_mutex>()),
90
+ runtime_(makeRuntime(
91
+ rnRuntime,
92
+ jsQueue,
93
+ name,
94
+ supportsLocking,
95
+ runtimeMutex_)),
96
+ #ifndef NDEBUG
97
+ supportsLocking_(supportsLocking),
98
+ #endif
99
+ name_(name) {
100
+ jsi::Runtime &rt = *runtime_;
101
+ WorkletRuntimeCollector::install(rt);
102
+ WorkletRuntimeDecorator::decorate(rt, name, jsScheduler);
103
+
104
+ auto codeBuffer = std::make_shared<const jsi::StringBuffer>(
105
+ "(" + valueUnpackerCode + "\n)");
106
+ auto valueUnpacker = rt.evaluateJavaScript(codeBuffer, "valueUnpacker")
107
+ .asObject(rt)
108
+ .asFunction(rt);
109
+ rt.global().setProperty(rt, "__valueUnpacker", valueUnpacker);
110
+ }
111
+
112
+ jsi::Value WorkletRuntime::executeSync(
113
+ jsi::Runtime &rt,
114
+ const jsi::Value &worklet) const {
115
+ assert(
116
+ supportsLocking_ &&
117
+ ("[Worklets] Runtime \"" + name_ + "\" doesn't support locking.")
118
+ .c_str());
119
+ auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
120
+ rt,
121
+ worklet,
122
+ "[Worklets] Only worklets can be executed synchronously on UI runtime.");
123
+ auto lock = std::unique_lock<std::recursive_mutex>(*runtimeMutex_);
124
+ jsi::Runtime &uiRuntime = getJSIRuntime();
125
+ auto result = runGuarded(shareableWorklet);
126
+ auto shareableResult = extractShareableOrThrow(uiRuntime, result);
127
+ lock.unlock();
128
+ return shareableResult->toJSValue(rt);
129
+ }
130
+
131
+ jsi::Value WorkletRuntime::get(
132
+ jsi::Runtime &rt,
133
+ const jsi::PropNameID &propName) {
134
+ auto name = propName.utf8(rt);
135
+ if (name == "toString") {
136
+ return jsi::Function::createFromHostFunction(
137
+ rt,
138
+ propName,
139
+ 0,
140
+ [weakThis = weak_from_this()](
141
+ jsi::Runtime &rt, const jsi::Value &, const jsi::Value *, size_t)
142
+ -> jsi::Value {
143
+ auto strongThis = weakThis.lock();
144
+ if (!strongThis) {
145
+ return jsi::String::createFromUtf8(rt, "");
146
+ }
147
+
148
+ return jsi::String::createFromUtf8(rt, strongThis->toString());
149
+ });
150
+ }
151
+ if (name == "name") {
152
+ return jsi::String::createFromUtf8(rt, name_);
153
+ }
154
+ return jsi::Value::undefined();
155
+ }
156
+
157
+ std::vector<jsi::PropNameID> WorkletRuntime::getPropertyNames(
158
+ jsi::Runtime &rt) {
159
+ std::vector<jsi::PropNameID> result;
160
+ result.push_back(jsi::PropNameID::forUtf8(rt, "toString"));
161
+ result.push_back(jsi::PropNameID::forUtf8(rt, "name"));
162
+ return result;
163
+ }
164
+
165
+ std::shared_ptr<WorkletRuntime> extractWorkletRuntime(
166
+ jsi::Runtime &rt,
167
+ const jsi::Value &value) {
168
+ return value.getObject(rt).getHostObject<WorkletRuntime>(rt);
169
+ }
170
+
171
+ void scheduleOnRuntime(
172
+ jsi::Runtime &rt,
173
+ const jsi::Value &workletRuntimeValue,
174
+ const jsi::Value &shareableWorkletValue) {
175
+ auto workletRuntime = extractWorkletRuntime(rt, workletRuntimeValue);
176
+ auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
177
+ rt,
178
+ shareableWorkletValue,
179
+ "[Worklets] Function passed to `_scheduleOnRuntime` is not a shareable worklet. Please make sure that `processNestedWorklets` option in Reanimated Babel plugin is enabled.");
180
+ workletRuntime->runAsyncGuarded(shareableWorklet);
181
+ }
182
+
183
+ } // namespace worklets
@@ -0,0 +1,90 @@
1
+ #pragma once
2
+
3
+ #include <cxxreact/MessageQueueThread.h>
4
+ #include <jsi/jsi.h>
5
+
6
+ #include <worklets/SharedItems/Shareables.h>
7
+ #include <worklets/Tools/AsyncQueue.h>
8
+ #include <worklets/Tools/JSScheduler.h>
9
+
10
+ #include <memory>
11
+ #include <string>
12
+ #include <utility>
13
+ #include <vector>
14
+
15
+ using namespace facebook;
16
+ using namespace react;
17
+
18
+ namespace worklets {
19
+
20
+ class WorkletRuntime : public jsi::HostObject,
21
+ public std::enable_shared_from_this<WorkletRuntime> {
22
+ public:
23
+ explicit WorkletRuntime(
24
+ jsi::Runtime &rnRuntime,
25
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
26
+ const std::shared_ptr<JSScheduler> &jsScheduler,
27
+ const std::string &name,
28
+ const bool supportsLocking,
29
+ const std::string &valueUnpackerCode);
30
+
31
+ jsi::Runtime &getJSIRuntime() const {
32
+ return *runtime_;
33
+ }
34
+
35
+ template <typename... Args>
36
+ inline jsi::Value runGuarded(
37
+ const std::shared_ptr<ShareableWorklet> &shareableWorklet,
38
+ Args &&...args) const {
39
+ jsi::Runtime &rt = *runtime_;
40
+ return runOnRuntimeGuarded(
41
+ rt, shareableWorklet->toJSValue(rt), std::forward<Args>(args)...);
42
+ }
43
+
44
+ void runAsyncGuarded(
45
+ const std::shared_ptr<ShareableWorklet> &shareableWorklet) {
46
+ if (queue_ == nullptr) {
47
+ queue_ = std::make_shared<AsyncQueue>(name_);
48
+ }
49
+ queue_->push([=, weakThis = weak_from_this()] {
50
+ auto strongThis = weakThis.lock();
51
+ if (!strongThis) {
52
+ return;
53
+ }
54
+
55
+ strongThis->runGuarded(shareableWorklet);
56
+ });
57
+ }
58
+
59
+ jsi::Value executeSync(jsi::Runtime &rt, const jsi::Value &worklet) const;
60
+
61
+ std::string toString() const {
62
+ return "[WorkletRuntime \"" + name_ + "\"]";
63
+ }
64
+
65
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override;
66
+
67
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
68
+
69
+ private:
70
+ const std::shared_ptr<std::recursive_mutex> runtimeMutex_;
71
+ const std::shared_ptr<jsi::Runtime> runtime_;
72
+ #ifndef NDEBUG
73
+ const bool supportsLocking_;
74
+ #endif
75
+ const std::string name_;
76
+ std::shared_ptr<AsyncQueue> queue_;
77
+ };
78
+
79
+ // This function needs to be non-inline to avoid problems with dynamic_cast on
80
+ // Android
81
+ std::shared_ptr<WorkletRuntime> extractWorkletRuntime(
82
+ jsi::Runtime &rt,
83
+ const jsi::Value &value);
84
+
85
+ void scheduleOnRuntime(
86
+ jsi::Runtime &rt,
87
+ const jsi::Value &workletRuntimeValue,
88
+ const jsi::Value &shareableWorkletValue);
89
+
90
+ } // namespace worklets
@@ -0,0 +1,36 @@
1
+ #pragma once
2
+
3
+ #include <worklets/Registries/WorkletRuntimeRegistry.h>
4
+
5
+ #include <jsi/jsi.h>
6
+
7
+ #include <memory>
8
+
9
+ namespace worklets {
10
+
11
+ class WorkletRuntimeCollector : public jsi::HostObject {
12
+ // When worklet runtime is created, we inject an instance of this class as a
13
+ // `jsi::HostObject` into the global object. When worklet runtime is
14
+ // terminated, the object is garbage-collected, which runs the C++ destructor.
15
+ // In the destructor, we unregister the worklet runtime from the registry.
16
+
17
+ public:
18
+ explicit WorkletRuntimeCollector(jsi::Runtime &runtime) : runtime_(runtime) {
19
+ WorkletRuntimeRegistry::registerRuntime(runtime_);
20
+ }
21
+
22
+ ~WorkletRuntimeCollector() {
23
+ WorkletRuntimeRegistry::unregisterRuntime(runtime_);
24
+ }
25
+
26
+ static void install(jsi::Runtime &rt) {
27
+ auto collector = std::make_shared<WorkletRuntimeCollector>(rt);
28
+ auto object = jsi::Object::createFromHostObject(rt, collector);
29
+ rt.global().setProperty(rt, "__workletRuntimeCollector", object);
30
+ }
31
+
32
+ private:
33
+ jsi::Runtime &runtime_;
34
+ };
35
+
36
+ } // namespace worklets