react-native-worklets 0.3.0 → 0.4.0-bundle-mode-preview-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 (202) hide show
  1. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +532 -0
  2. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +88 -0
  3. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +40 -122
  4. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +16 -40
  5. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +2 -1
  6. package/Common/cpp/worklets/Resources/ValueUnpacker.cpp +1 -1
  7. package/Common/cpp/worklets/SharedItems/Shareables.cpp +200 -24
  8. package/Common/cpp/worklets/SharedItems/Shareables.h +108 -7
  9. package/Common/cpp/worklets/Tools/JSLogger.cpp +56 -4
  10. package/Common/cpp/worklets/Tools/JSLogger.h +17 -0
  11. package/Common/cpp/worklets/Tools/JSScheduler.cpp +12 -0
  12. package/Common/cpp/worklets/Tools/JSScheduler.h +10 -2
  13. package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +3 -1
  14. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +19 -1
  15. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +12 -3
  16. package/Common/cpp/worklets/Tools/WorkletsSystraceSection.h +136 -0
  17. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +4 -4
  18. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +1 -1
  19. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +85 -0
  20. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +55 -0
  21. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +8 -4
  22. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +70 -24
  23. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +24 -4
  24. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +53 -1
  25. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +2 -1
  26. package/RNWorklets.podspec +9 -4
  27. package/android/CMakeLists.txt +14 -36
  28. package/android/build.gradle +16 -33
  29. package/android/src/experimentalBundling/com/swmansion/worklets/WorkletsModule.java +149 -0
  30. package/android/src/{main/java → legacyBundling}/com/swmansion/worklets/WorkletsModule.java +17 -2
  31. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +49 -8
  32. package/android/src/main/cpp/worklets/android/WorkletsModule.h +17 -2
  33. package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +1 -1
  34. package/apple/worklets/apple/WorkletsMessageThread.mm +4 -0
  35. package/apple/worklets/apple/WorkletsModule.h +16 -1
  36. package/apple/worklets/apple/WorkletsModule.mm +29 -2
  37. package/bundleMode/index.d.ts +3 -0
  38. package/bundleMode/index.js +55 -0
  39. package/lib/module/PlatformChecker/PlatformChecker.js +8 -0
  40. package/lib/module/PlatformChecker/PlatformChecker.js.map +1 -0
  41. package/lib/module/PlatformChecker/index.js +17 -0
  42. package/lib/module/PlatformChecker/index.js.map +1 -0
  43. package/lib/module/WorkletsError.js +2 -1
  44. package/lib/module/WorkletsError.js.map +1 -1
  45. package/lib/module/WorkletsModule/JSWorklets.js +36 -4
  46. package/lib/module/WorkletsModule/JSWorklets.js.map +1 -1
  47. package/lib/module/WorkletsModule/NativeWorklets.js +35 -15
  48. package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -1
  49. package/lib/module/WorkletsModule/workletsModuleInstance.js +2 -2
  50. package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -1
  51. package/lib/module/bundleUnpacker.js +47 -0
  52. package/lib/module/bundleUnpacker.js.map +1 -0
  53. package/lib/module/callGuard.js +30 -0
  54. package/lib/module/callGuard.js.map +1 -0
  55. package/lib/module/errors.js +30 -11
  56. package/lib/module/errors.js.map +1 -1
  57. package/lib/module/index.js +10 -7
  58. package/lib/module/index.js.map +1 -1
  59. package/lib/module/initializers.js +123 -103
  60. package/lib/module/initializers.js.map +1 -1
  61. package/lib/module/logger.js +15 -0
  62. package/lib/module/logger.js.map +1 -0
  63. package/lib/module/privateGlobals.d.js +0 -1
  64. package/lib/module/privateGlobals.d.js.map +1 -1
  65. package/lib/module/publicGlobals.js +5 -0
  66. package/lib/module/publicGlobals.js.map +1 -0
  67. package/lib/module/runLoop/mockedRequestAnimationFrame.js.map +1 -0
  68. package/lib/module/runLoop/requestAnimationFrame.js +50 -0
  69. package/lib/module/runLoop/requestAnimationFrame.js.map +1 -0
  70. package/lib/module/runLoop/setImmediatePolyfill.js +15 -0
  71. package/lib/module/runLoop/setImmediatePolyfill.js.map +1 -0
  72. package/lib/module/runLoop/setIntervalPolyfill.js +26 -0
  73. package/lib/module/runLoop/setIntervalPolyfill.js.map +1 -0
  74. package/lib/module/runLoop/setTimeoutPolyfill.js +32 -0
  75. package/lib/module/runLoop/setTimeoutPolyfill.js.map +1 -0
  76. package/lib/module/runtimes.js +6 -10
  77. package/lib/module/runtimes.js.map +1 -1
  78. package/lib/module/shareableMappingCache.js +1 -3
  79. package/lib/module/shareableMappingCache.js.map +1 -1
  80. package/lib/module/shareables.js +116 -34
  81. package/lib/module/shareables.js.map +1 -1
  82. package/lib/module/specs/index.js +2 -2
  83. package/lib/module/specs/index.js.map +1 -1
  84. package/lib/module/threads.js +49 -54
  85. package/lib/module/threads.js.map +1 -1
  86. package/lib/module/valueUnpacker.js +3 -3
  87. package/lib/module/valueUnpacker.js.map +1 -1
  88. package/lib/module/workletRuntimeEntry.js +30 -0
  89. package/lib/module/workletRuntimeEntry.js.map +1 -0
  90. package/lib/typescript/PlatformChecker/PlatformChecker.d.ts +5 -0
  91. package/lib/typescript/PlatformChecker/PlatformChecker.d.ts.map +1 -0
  92. package/lib/typescript/PlatformChecker/index.d.ts +10 -0
  93. package/lib/typescript/PlatformChecker/index.d.ts.map +1 -0
  94. package/lib/typescript/WorkletsError.d.ts.map +1 -1
  95. package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -1
  96. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +1 -3
  97. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -1
  98. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +1 -1
  99. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -1
  100. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +1 -1
  101. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -1
  102. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +12 -2
  103. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  104. package/lib/typescript/bundleUnpacker.d.ts +7 -0
  105. package/lib/typescript/bundleUnpacker.d.ts.map +1 -0
  106. package/lib/typescript/callGuard.d.ts +4 -0
  107. package/lib/typescript/callGuard.d.ts.map +1 -0
  108. package/lib/typescript/errors.d.ts +13 -5
  109. package/lib/typescript/errors.d.ts.map +1 -1
  110. package/lib/typescript/index.d.ts +1 -2
  111. package/lib/typescript/index.d.ts.map +1 -1
  112. package/lib/typescript/initializers.d.ts +16 -5
  113. package/lib/typescript/initializers.d.ts.map +1 -1
  114. package/lib/typescript/logger.d.ts +5 -0
  115. package/lib/typescript/logger.d.ts.map +1 -0
  116. package/lib/typescript/publicGlobals.d.ts +22 -0
  117. package/lib/typescript/publicGlobals.d.ts.map +1 -0
  118. package/lib/typescript/runLoop/mockedRequestAnimationFrame.d.ts.map +1 -0
  119. package/lib/typescript/runLoop/requestAnimationFrame.d.ts.map +1 -0
  120. package/lib/typescript/runLoop/setImmediatePolyfill.d.ts +2 -0
  121. package/lib/typescript/runLoop/setImmediatePolyfill.d.ts.map +1 -0
  122. package/lib/typescript/runLoop/setIntervalPolyfill.d.ts +2 -0
  123. package/lib/typescript/runLoop/setIntervalPolyfill.d.ts.map +1 -0
  124. package/lib/typescript/runLoop/setTimeoutPolyfill.d.ts +2 -0
  125. package/lib/typescript/runLoop/setTimeoutPolyfill.d.ts.map +1 -0
  126. package/lib/typescript/runtimes.d.ts.map +1 -1
  127. package/lib/typescript/shareableMappingCache.d.ts.map +1 -1
  128. package/lib/typescript/shareables.d.ts +3 -2
  129. package/lib/typescript/shareables.d.ts.map +1 -1
  130. package/lib/typescript/specs/NativeWorkletsModule.d.ts +1 -1
  131. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -1
  132. package/lib/typescript/specs/index.d.ts +2 -2
  133. package/lib/typescript/specs/index.d.ts.map +1 -1
  134. package/lib/typescript/threads.d.ts.map +1 -1
  135. package/lib/typescript/workletRuntimeEntry.d.ts +14 -0
  136. package/lib/typescript/workletRuntimeEntry.d.ts.map +1 -0
  137. package/lib/typescript/workletTypes.d.ts +14 -3
  138. package/lib/typescript/workletTypes.d.ts.map +1 -1
  139. package/package.json +17 -8
  140. package/plugin/index.js +145 -52
  141. package/scripts/worklets_utils.rb +9 -0
  142. package/src/PlatformChecker/PlatformChecker.ts +7 -0
  143. package/src/PlatformChecker/index.ts +29 -0
  144. package/src/WorkletsError.ts +2 -1
  145. package/src/WorkletsModule/JSWorklets.ts +71 -4
  146. package/src/WorkletsModule/NativeWorklets.ts +83 -21
  147. package/src/WorkletsModule/workletsModuleInstance.ts +2 -2
  148. package/src/WorkletsModule/workletsModuleProxy.ts +49 -1
  149. package/src/bundleUnpacker.ts +75 -0
  150. package/src/callGuard.ts +33 -0
  151. package/src/errors.ts +35 -18
  152. package/src/index.ts +12 -12
  153. package/src/initializers.ts +143 -113
  154. package/src/logger.ts +16 -0
  155. package/src/privateGlobals.d.ts +22 -6
  156. package/src/publicGlobals.ts +26 -0
  157. package/src/runLoop/requestAnimationFrame.ts +67 -0
  158. package/src/runLoop/setImmediatePolyfill.ts +20 -0
  159. package/src/runLoop/setIntervalPolyfill.ts +38 -0
  160. package/src/runLoop/setTimeoutPolyfill.ts +40 -0
  161. package/src/runtimes.ts +6 -11
  162. package/src/shareableMappingCache.ts +1 -3
  163. package/src/shareables.ts +179 -65
  164. package/src/specs/NativeWorkletsModule.ts +1 -1
  165. package/src/specs/index.ts +5 -2
  166. package/src/threads.ts +75 -65
  167. package/src/valueUnpacker.ts +3 -3
  168. package/src/workletRuntimeEntry.ts +30 -0
  169. package/src/workletTypes.ts +22 -5
  170. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +0 -139
  171. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +0 -61
  172. package/android/src/paper/com/swmansion/worklets/NativeWorkletsModuleSpec.java +0 -26
  173. package/lib/module/PlatformChecker.js +0 -26
  174. package/lib/module/PlatformChecker.js.map +0 -1
  175. package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js.map +0 -1
  176. package/lib/module/animationFrameQueue/requestAnimationFrame.js +0 -36
  177. package/lib/module/animationFrameQueue/requestAnimationFrame.js.map +0 -1
  178. package/lib/module/logger/LogBox.js +0 -15
  179. package/lib/module/logger/LogBox.js.map +0 -1
  180. package/lib/module/logger/index.js +0 -5
  181. package/lib/module/logger/index.js.map +0 -1
  182. package/lib/module/logger/logger.js +0 -137
  183. package/lib/module/logger/logger.js.map +0 -1
  184. package/lib/typescript/PlatformChecker.d.ts +0 -6
  185. package/lib/typescript/PlatformChecker.d.ts.map +0 -1
  186. package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts.map +0 -1
  187. package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts.map +0 -1
  188. package/lib/typescript/logger/LogBox.d.ts +0 -32
  189. package/lib/typescript/logger/LogBox.d.ts.map +0 -1
  190. package/lib/typescript/logger/index.d.ts +0 -3
  191. package/lib/typescript/logger/index.d.ts.map +0 -1
  192. package/lib/typescript/logger/logger.d.ts +0 -52
  193. package/lib/typescript/logger/logger.d.ts.map +0 -1
  194. package/src/PlatformChecker.ts +0 -30
  195. package/src/animationFrameQueue/requestAnimationFrame.ts +0 -41
  196. package/src/logger/LogBox.ts +0 -55
  197. package/src/logger/index.ts +0 -3
  198. package/src/logger/logger.ts +0 -155
  199. /package/lib/module/{animationFrameQueue → runLoop}/mockedRequestAnimationFrame.js +0 -0
  200. /package/lib/typescript/{animationFrameQueue → runLoop}/mockedRequestAnimationFrame.d.ts +0 -0
  201. /package/lib/typescript/{animationFrameQueue → runLoop}/requestAnimationFrame.d.ts +0 -0
  202. /package/src/{animationFrameQueue → runLoop}/mockedRequestAnimationFrame.ts +0 -0
@@ -0,0 +1,532 @@
1
+ #include <react/renderer/uimanager/UIManagerBinding.h>
2
+ #include <react/renderer/uimanager/primitives.h>
3
+
4
+ #include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
5
+ #include <worklets/NativeModules/WorkletsModuleProxy.h>
6
+ #include <worklets/SharedItems/Shareables.h>
7
+ #include <worklets/Tools/Defs.h>
8
+ #include <worklets/Tools/JSLogger.h>
9
+ #include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
10
+
11
+ #ifdef __ANDROID__
12
+ #include <fbjni/fbjni.h>
13
+ #endif // __ANDROID__
14
+
15
+ using namespace facebook;
16
+
17
+ namespace worklets {
18
+
19
+ inline void scheduleOnUI(
20
+ const std::shared_ptr<UIScheduler> &uiScheduler,
21
+ const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime,
22
+ jsi::Runtime &rt,
23
+ const jsi::Value &worklet) {
24
+ auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
25
+ rt, worklet, "[Worklets] Only worklets can be scheduled to run on UI.");
26
+ uiScheduler->scheduleOnUI([shareableWorklet, weakUIWorkletRuntime]() {
27
+ // This callback can outlive the WorkletsModuleProxy object during the
28
+ // invalidation of React Native. This happens when WorkletsModuleProxy
29
+ // destructor is called on the JS thread and the UI thread is
30
+ // executing callbacks from the `scheduleOnUI` queue. Therefore, we
31
+ // need to make sure it's still alive before we try to access it.
32
+ auto uiWorkletRuntime = weakUIWorkletRuntime.lock();
33
+ if (!uiWorkletRuntime) {
34
+ return;
35
+ }
36
+
37
+ #if JS_RUNTIME_HERMES
38
+ // JSI's scope defined here allows for JSI-objects to be cleared up
39
+ // after each runtime loop. Within these loops we typically create
40
+ // some temporary JSI objects and hence it allows for such objects to
41
+ // be garbage collected much sooner. Apparently the scope API is only
42
+ // supported on Hermes at the moment.
43
+ const auto scope = jsi::Scope(uiWorkletRuntime->getJSIRuntime());
44
+ #endif // JS_RUNTIME_HERMES
45
+
46
+ uiWorkletRuntime->runGuarded(shareableWorklet);
47
+ });
48
+ }
49
+
50
+ inline jsi::Value executeOnUIRuntimeSync(
51
+ const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime,
52
+ jsi::Runtime &rt,
53
+ const jsi::Value &worklet) {
54
+ if (auto uiWorkletRuntime = weakUIWorkletRuntime.lock()) {
55
+ return uiWorkletRuntime->executeSync(rt, worklet);
56
+ }
57
+ return jsi::Value::undefined();
58
+ }
59
+
60
+ inline jsi::Value createWorkletRuntime(
61
+ jsi::Runtime &originRuntime,
62
+ const std::shared_ptr<RuntimeManager> &runtimeManager,
63
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
64
+ std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsModuleProxy,
65
+ const std::string &name,
66
+ std::shared_ptr<ShareableWorklet> &initializer) {
67
+ const auto workletRuntime = runtimeManager->createWorkletRuntime(
68
+ jsiWorkletsModuleProxy, true /* supportsLocking */, name, initializer);
69
+ return jsi::Object::createFromHostObject(originRuntime, workletRuntime);
70
+ }
71
+
72
+ #ifdef WORKLETS_BUNDLE_MODE
73
+ inline jsi::Value propagateModuleUpdate(
74
+ const std::shared_ptr<RuntimeManager> &runtimeManager,
75
+ const std::string &code,
76
+ const std::string &sourceUrl) {
77
+ const auto runtimes = runtimeManager->getAllRuntimes();
78
+
79
+ for (auto runtime : runtimes) {
80
+ runtime->executeSync([code, sourceUrl](jsi::Runtime &rt) {
81
+ const auto buffer = std::make_shared<jsi::StringBuffer>(code);
82
+ rt.evaluateJavaScript(buffer, sourceUrl);
83
+ return jsi::Value::undefined();
84
+ });
85
+ }
86
+ return jsi::Value::undefined();
87
+ }
88
+ #endif // WORKLETS_BUNDLE_MODE
89
+
90
+ inline jsi::Value reportFatalErrorOnJS(
91
+ const std::shared_ptr<JSScheduler> &jsScheduler,
92
+ const std::string &message,
93
+ const std::string &stack,
94
+ const std::string &name,
95
+ const std::string &jsEngine) {
96
+ JSLogger::reportFatalErrorOnJS(
97
+ jsScheduler,
98
+ JSErrorData{
99
+ .message = message,
100
+ .stack = stack,
101
+ .name = name,
102
+ .jsEngine = jsEngine});
103
+ return jsi::Value::undefined();
104
+ }
105
+
106
+ JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(
107
+ const bool isDevBundle,
108
+ const std::shared_ptr<const BigStringBuffer> &script,
109
+ const std::string &sourceUrl,
110
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
111
+ const std::shared_ptr<JSScheduler> &jsScheduler,
112
+ const std::shared_ptr<UIScheduler> &uiScheduler,
113
+ const std::shared_ptr<RuntimeManager> &runtimeManager,
114
+ const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime)
115
+ : jsi::HostObject(),
116
+ isDevBundle_(isDevBundle),
117
+ script_(script),
118
+ sourceUrl_(sourceUrl),
119
+ jsQueue_(jsQueue),
120
+ jsScheduler_(jsScheduler),
121
+ uiScheduler_(uiScheduler),
122
+ runtimeManager_(runtimeManager),
123
+ uiWorkletRuntime_(uiWorkletRuntime) {}
124
+
125
+ JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(
126
+ const JSIWorkletsModuleProxy &other)
127
+ : jsi::HostObject(),
128
+ isDevBundle_(other.isDevBundle_),
129
+ script_(other.script_),
130
+ sourceUrl_(other.sourceUrl_),
131
+ jsQueue_(other.jsQueue_),
132
+ jsScheduler_(other.jsScheduler_),
133
+ uiScheduler_(other.uiScheduler_),
134
+ runtimeManager_(other.runtimeManager_),
135
+ uiWorkletRuntime_(other.uiWorkletRuntime_) {}
136
+
137
+ JSIWorkletsModuleProxy::~JSIWorkletsModuleProxy() = default;
138
+
139
+ std::vector<jsi::PropNameID> JSIWorkletsModuleProxy::getPropertyNames(
140
+ jsi::Runtime &rt) {
141
+ std::vector<jsi::PropNameID> propertyNames;
142
+
143
+ propertyNames.emplace_back(
144
+ jsi::PropNameID::forAscii(rt, "makeShareableClone"));
145
+ propertyNames.emplace_back(
146
+ jsi::PropNameID::forAscii(rt, "makeShareableBigInt"));
147
+ propertyNames.emplace_back(
148
+ jsi::PropNameID::forAscii(rt, "makeShareableBoolean"));
149
+ propertyNames.emplace_back(
150
+ jsi::PropNameID::forAscii(rt, "makeShareableImport"));
151
+ propertyNames.emplace_back(
152
+ jsi::PropNameID::forAscii(rt, "makeShareableNull"));
153
+ propertyNames.emplace_back(
154
+ jsi::PropNameID::forAscii(rt, "makeShareableNumber"));
155
+ propertyNames.emplace_back(
156
+ jsi::PropNameID::forAscii(rt, "makeShareableString"));
157
+ propertyNames.emplace_back(
158
+ jsi::PropNameID::forAscii(rt, "makeShareableUndefined"));
159
+ propertyNames.emplace_back(
160
+ jsi::PropNameID::forAscii(rt, "makeShareableHostObject"));
161
+ propertyNames.emplace_back(
162
+ jsi::PropNameID::forAscii(rt, "makeShareableInitializer"));
163
+ propertyNames.emplace_back(
164
+ jsi::PropNameID::forAscii(rt, "makeShareableArray"));
165
+ propertyNames.emplace_back(
166
+ jsi::PropNameID::forAscii(rt, "makeShareableFunction"));
167
+ propertyNames.emplace_back(
168
+ jsi::PropNameID::forAscii(rt, "makeShareableTurboModuleLike"));
169
+ propertyNames.emplace_back(
170
+ jsi::PropNameID::forAscii(rt, "makeShareableObject"));
171
+ propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "makeShareableMap"));
172
+ propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "makeShareableSet"));
173
+ propertyNames.emplace_back(
174
+ jsi::PropNameID::forAscii(rt, "makeShareableWorklet"));
175
+
176
+ propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnUI"));
177
+ propertyNames.emplace_back(
178
+ jsi::PropNameID::forAscii(rt, "executeOnUIRuntimeSync"));
179
+ propertyNames.emplace_back(
180
+ jsi::PropNameID::forAscii(rt, "createWorkletRuntime"));
181
+ propertyNames.emplace_back(
182
+ jsi::PropNameID::forAscii(rt, "scheduleOnRuntime"));
183
+ propertyNames.emplace_back(
184
+ jsi::PropNameID::forAscii(rt, "reportFatalErrorOnJS"));
185
+
186
+ #ifdef WORKLETS_BUNDLE_MODE
187
+ propertyNames.emplace_back(
188
+ jsi::PropNameID::forAscii(rt, "propagateModuleUpdate"));
189
+ #endif // WORKLETS_BUNDLE_MODE
190
+
191
+ return propertyNames;
192
+ }
193
+
194
+ jsi::Value JSIWorkletsModuleProxy::get(
195
+ jsi::Runtime &rt,
196
+ const jsi::PropNameID &propName) {
197
+ const auto name = propName.utf8(rt);
198
+
199
+ if (name == "makeShareableClone") {
200
+ return jsi::Function::createFromHostFunction(
201
+ rt,
202
+ propName,
203
+ 3,
204
+ [](jsi::Runtime &rt,
205
+ const jsi::Value &thisValue,
206
+ const jsi::Value *args,
207
+ size_t count) {
208
+ return makeShareableClone(rt, args[0], args[1], args[2]);
209
+ });
210
+ }
211
+
212
+ if (name == "makeShareableBigInt") {
213
+ return jsi::Function::createFromHostFunction(
214
+ rt,
215
+ propName,
216
+ 1,
217
+ [](jsi::Runtime &rt,
218
+ const jsi::Value &thisValue,
219
+ const jsi::Value *args,
220
+ size_t count) {
221
+ return makeShareableBigInt(rt, args[0].asBigInt(rt));
222
+ });
223
+ }
224
+
225
+ if (name == "makeShareableBoolean") {
226
+ return jsi::Function::createFromHostFunction(
227
+ rt,
228
+ propName,
229
+ 1,
230
+ [](jsi::Runtime &rt,
231
+ const jsi::Value &thisValue,
232
+ const jsi::Value *args,
233
+ size_t count) {
234
+ return makeShareableBoolean(rt, args[0].asBool());
235
+ });
236
+ }
237
+
238
+ if (name == "makeShareableImport") {
239
+ return jsi::Function::createFromHostFunction(
240
+ rt,
241
+ propName,
242
+ 2,
243
+ [](jsi::Runtime &rt,
244
+ const jsi::Value &thisValue,
245
+ const jsi::Value *args,
246
+ size_t count) {
247
+ return makeShareableImport(
248
+ rt, args[0].asNumber(), args[1].asString(rt));
249
+ });
250
+ }
251
+
252
+ if (name == "makeShareableNumber") {
253
+ return jsi::Function::createFromHostFunction(
254
+ rt,
255
+ propName,
256
+ 1,
257
+ [](jsi::Runtime &rt,
258
+ const jsi::Value &thisValue,
259
+ const jsi::Value *args,
260
+ size_t count) {
261
+ return makeShareableNumber(rt, args[0].asNumber());
262
+ });
263
+ }
264
+
265
+ if (name == "makeShareableNull") {
266
+ return jsi::Function::createFromHostFunction(
267
+ rt,
268
+ propName,
269
+ 0,
270
+ [](jsi::Runtime &rt,
271
+ const jsi::Value &thisValue,
272
+ const jsi::Value *args,
273
+ size_t count) { return makeShareableNull(rt); });
274
+ }
275
+
276
+ if (name == "makeShareableString") {
277
+ return jsi::Function::createFromHostFunction(
278
+ rt,
279
+ propName,
280
+ 1,
281
+ [](jsi::Runtime &rt,
282
+ const jsi::Value &thisValue,
283
+ const jsi::Value *args,
284
+ size_t count) {
285
+ return makeShareableString(rt, args[0].asString(rt));
286
+ });
287
+ }
288
+
289
+ if (name == "makeShareableUndefined") {
290
+ return jsi::Function::createFromHostFunction(
291
+ rt,
292
+ propName,
293
+ 0,
294
+ [](jsi::Runtime &rt,
295
+ const jsi::Value &thisValue,
296
+ const jsi::Value *args,
297
+ size_t count) { return makeShareableUndefined(rt); });
298
+ }
299
+
300
+ if (name == "makeShareableInitializer") {
301
+ return jsi::Function::createFromHostFunction(
302
+ rt,
303
+ propName,
304
+ 1,
305
+ [](jsi::Runtime &rt,
306
+ const jsi::Value &thisValue,
307
+ const jsi::Value *args,
308
+ size_t count) {
309
+ return makeShareableInitializer(rt, args[0].asObject(rt));
310
+ });
311
+ }
312
+
313
+ if (name == "makeShareableArray") {
314
+ return jsi::Function::createFromHostFunction(
315
+ rt,
316
+ propName,
317
+ 2,
318
+ [](jsi::Runtime &rt,
319
+ const jsi::Value &thisValue,
320
+ const jsi::Value *args,
321
+ size_t count) {
322
+ return makeShareableArray(
323
+ rt, args[0].asObject(rt).asArray(rt), args[1]);
324
+ });
325
+ }
326
+
327
+ if (name == "makeShareableMap") {
328
+ return jsi::Function::createFromHostFunction(
329
+ rt,
330
+ propName,
331
+ 2,
332
+ [](jsi::Runtime &rt,
333
+ const jsi::Value &thisValue,
334
+ const jsi::Value *args,
335
+ size_t count) {
336
+ return makeShareableMap(
337
+ rt,
338
+ args[0].asObject(rt).asArray(rt),
339
+ args[1].asObject(rt).asArray(rt));
340
+ });
341
+ }
342
+
343
+ if (name == "makeShareableSet") {
344
+ return jsi::Function::createFromHostFunction(
345
+ rt,
346
+ propName,
347
+ 1,
348
+ [](jsi::Runtime &rt,
349
+ const jsi::Value &thisValue,
350
+ const jsi::Value *args,
351
+ size_t count) {
352
+ return makeShareableSet(rt, args[0].asObject(rt).asArray(rt));
353
+ });
354
+ }
355
+
356
+ if (name == "makeShareableHostObject") {
357
+ return jsi::Function::createFromHostFunction(
358
+ rt,
359
+ propName,
360
+ 1,
361
+ [](jsi::Runtime &rt,
362
+ const jsi::Value &thisValue,
363
+ const jsi::Value *args,
364
+ size_t count) {
365
+ return makeShareableHostObject(
366
+ rt, args[0].asObject(rt).asHostObject(rt));
367
+ });
368
+ }
369
+
370
+ if (name == "makeShareableFunction") {
371
+ return jsi::Function::createFromHostFunction(
372
+ rt,
373
+ propName,
374
+ 1,
375
+ [](jsi::Runtime &rt,
376
+ const jsi::Value &thisValue,
377
+ const jsi::Value *args,
378
+ size_t count) {
379
+ return makeShareableFunction(rt, args[0].asObject(rt).asFunction(rt));
380
+ });
381
+ }
382
+
383
+ if (name == "makeShareableTurboModuleLike") {
384
+ return jsi::Function::createFromHostFunction(
385
+ rt,
386
+ propName,
387
+ 2,
388
+ [](jsi::Runtime &rt,
389
+ const jsi::Value &thisValue,
390
+ const jsi::Value *args,
391
+ size_t count) {
392
+ return makeShareableTurboModuleLike(
393
+ rt, args[0].asObject(rt), args[1].asObject(rt).asHostObject(rt));
394
+ });
395
+ }
396
+
397
+ if (name == "makeShareableObject") {
398
+ return jsi::Function::createFromHostFunction(
399
+ rt,
400
+ propName,
401
+ 1,
402
+ [](jsi::Runtime &rt,
403
+ const jsi::Value &thisValue,
404
+ const jsi::Value *args,
405
+ size_t count) {
406
+ return makeShareableObject(
407
+ rt, args[0].getObject(rt), args[1].getBool(), args[2]);
408
+ });
409
+ }
410
+
411
+ if (name == "makeShareableWorklet") {
412
+ return jsi::Function::createFromHostFunction(
413
+ rt,
414
+ propName,
415
+ 2,
416
+ [](jsi::Runtime &rt,
417
+ const jsi::Value &thisValue,
418
+ const jsi::Value *args,
419
+ size_t count) {
420
+ return makeShareableWorklet(
421
+ rt, args[0].getObject(rt), args[1].getBool());
422
+ });
423
+ }
424
+
425
+ if (name == "scheduleOnUI") {
426
+ return jsi::Function::createFromHostFunction(
427
+ rt,
428
+ propName,
429
+ 1,
430
+ [uiScheduler = uiScheduler_, uiWorkletRuntime = uiWorkletRuntime_](
431
+ jsi::Runtime &rt,
432
+ const jsi::Value &thisValue,
433
+ const jsi::Value *args,
434
+ size_t count) {
435
+ scheduleOnUI(uiScheduler, uiWorkletRuntime, rt, args[0]);
436
+ return jsi::Value::undefined();
437
+ });
438
+ }
439
+
440
+ if (name == "executeOnUIRuntimeSync") {
441
+ return jsi::Function::createFromHostFunction(
442
+ rt,
443
+ propName,
444
+ 1,
445
+ [uiWorkletRuntime = uiWorkletRuntime_](
446
+ jsi::Runtime &rt,
447
+ const jsi::Value &thisValue,
448
+ const jsi::Value *args,
449
+ size_t count) {
450
+ return executeOnUIRuntimeSync(uiWorkletRuntime, rt, args[0]);
451
+ });
452
+ }
453
+
454
+ if (name == "createWorkletRuntime") {
455
+ return jsi::Function::createFromHostFunction(
456
+ rt,
457
+ propName,
458
+ 2,
459
+ [clone = std::make_shared<JSIWorkletsModuleProxy>(*this)](
460
+ jsi::Runtime &rt,
461
+ const jsi::Value &thisValue,
462
+ const jsi::Value *args,
463
+ size_t count) {
464
+ auto shareableInitializer = extractShareableOrThrow<ShareableWorklet>(
465
+ rt, args[1], "[Worklets] Initializer must be a worklet.");
466
+
467
+ return createWorkletRuntime(
468
+ rt,
469
+ clone->getRuntimeManager(),
470
+ clone->getJSQueue(),
471
+ clone,
472
+ args[0].asString(rt).utf8(rt),
473
+ shareableInitializer);
474
+ });
475
+ }
476
+
477
+ if (name == "scheduleOnRuntime") {
478
+ return jsi::Function::createFromHostFunction(
479
+ rt,
480
+ propName,
481
+ 2,
482
+ [](jsi::Runtime &rt,
483
+ const jsi ::Value &thisValue,
484
+ const jsi::Value *args,
485
+ size_t count) {
486
+ worklets::scheduleOnRuntime(rt, args[0], args[1]);
487
+ return jsi::Value::undefined();
488
+ });
489
+ }
490
+
491
+ if (name == "reportFatalErrorOnJS") {
492
+ return jsi::Function::createFromHostFunction(
493
+ rt,
494
+ propName,
495
+ 4,
496
+ [jsScheduler = jsScheduler_](
497
+ jsi::Runtime &rt,
498
+ const jsi::Value &thisValue,
499
+ const jsi::Value *args,
500
+ size_t count) {
501
+ return reportFatalErrorOnJS(
502
+ jsScheduler,
503
+ /* message */ args[0].asString(rt).utf8(rt),
504
+ /* stack */ args[1].asString(rt).utf8(rt),
505
+ /* name */ args[2].asString(rt).utf8(rt),
506
+ /* jsEngine */ args[3].asString(rt).utf8(rt));
507
+ });
508
+ }
509
+
510
+ #ifdef WORKLETS_BUNDLE_MODE
511
+ if (name == "propagateModuleUpdate") {
512
+ return jsi::Function::createFromHostFunction(
513
+ rt,
514
+ propName,
515
+ 2,
516
+ [runtimeManager = runtimeManager_](
517
+ jsi::Runtime &rt,
518
+ const jsi::Value &thisValue,
519
+ const jsi::Value *args,
520
+ size_t count) {
521
+ return propagateModuleUpdate(
522
+ runtimeManager,
523
+ args[0].asString(rt).utf8(rt),
524
+ args[1].asString(rt).utf8(rt));
525
+ });
526
+ }
527
+ #endif // WORKLETS_BUNDLE_MODE
528
+
529
+ return jsi::Value::undefined();
530
+ }
531
+
532
+ } // namespace worklets
@@ -0,0 +1,88 @@
1
+
2
+ #pragma once
3
+
4
+ #include <cxxreact/MessageQueueThread.h>
5
+ #include <react/renderer/uimanager/UIManagerBinding.h>
6
+ #include <react/renderer/uimanager/primitives.h>
7
+
8
+ #include <worklets/SharedItems/Shareables.h>
9
+ #include <worklets/Tools/Defs.h>
10
+ #include <worklets/WorkletRuntime/RuntimeManager.h>
11
+ #include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
12
+
13
+ #ifdef __ANDROID__
14
+ #include <fbjni/fbjni.h>
15
+ #endif // __ANDROID__
16
+
17
+ #include <jsi/jsi.h>
18
+
19
+ #include <memory>
20
+ #include <string>
21
+ #include <vector>
22
+
23
+ using namespace facebook;
24
+
25
+ namespace worklets {
26
+
27
+ class WorkletRuntime;
28
+
29
+ class JSIWorkletsModuleProxy : public jsi::HostObject {
30
+ public:
31
+ explicit JSIWorkletsModuleProxy(
32
+ const bool isDevBundle,
33
+ const std::shared_ptr<const BigStringBuffer> &script,
34
+ const std::string &sourceUrl,
35
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
36
+ const std::shared_ptr<JSScheduler> &jsScheduler,
37
+ const std::shared_ptr<UIScheduler> &uiScheduler,
38
+ const std::shared_ptr<RuntimeManager> &runtimeManager,
39
+ const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime);
40
+
41
+ JSIWorkletsModuleProxy(const JSIWorkletsModuleProxy &other);
42
+
43
+ ~JSIWorkletsModuleProxy() override;
44
+
45
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
46
+
47
+ jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override;
48
+
49
+ [[nodiscard]] std::shared_ptr<MessageQueueThread> getJSQueue() const {
50
+ return jsQueue_;
51
+ }
52
+
53
+ [[nodiscard]] std::shared_ptr<JSScheduler> getJSScheduler() const {
54
+ return jsScheduler_;
55
+ }
56
+
57
+ [[nodiscard]] std::shared_ptr<UIScheduler> getUIScheduler() const {
58
+ return uiScheduler_;
59
+ }
60
+
61
+ [[nodiscard]] bool isDevBundle() const {
62
+ return isDevBundle_;
63
+ }
64
+
65
+ [[nodiscard]] std::shared_ptr<const BigStringBuffer> getScript() const {
66
+ return script_;
67
+ }
68
+
69
+ [[nodiscard]] std::string getSourceUrl() const {
70
+ return sourceUrl_;
71
+ }
72
+
73
+ [[nodiscard]] std::shared_ptr<RuntimeManager> getRuntimeManager() const {
74
+ return runtimeManager_;
75
+ }
76
+
77
+ private:
78
+ const bool isDevBundle_;
79
+ const std::shared_ptr<const BigStringBuffer> script_;
80
+ const std::string sourceUrl_;
81
+ const std::shared_ptr<MessageQueueThread> jsQueue_;
82
+ const std::shared_ptr<JSScheduler> jsScheduler_;
83
+ const std::shared_ptr<UIScheduler> uiScheduler_;
84
+ const std::shared_ptr<RuntimeManager> runtimeManager_;
85
+ const std::weak_ptr<WorkletRuntime> uiWorkletRuntime_;
86
+ };
87
+
88
+ } // namespace worklets