@amazon-devices/react-native-worklets 1.0.0-rn-83

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 (348) 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/JSIWorkletsModuleProxy.cpp +712 -0
  4. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +88 -0
  5. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +89 -0
  6. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +80 -0
  7. package/Common/cpp/worklets/Public/AsyncQueue.h +14 -0
  8. package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
  9. package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +51 -0
  10. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
  11. package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +40 -0
  12. package/Common/cpp/worklets/Resources/SynchronizableUnpacker.cpp +49 -0
  13. package/Common/cpp/worklets/Resources/Unpackers.h +6 -0
  14. package/Common/cpp/worklets/Resources/ValueUnpacker.cpp +53 -0
  15. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +62 -0
  16. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +46 -0
  17. package/Common/cpp/worklets/RunLoop/EventLoop.cpp +112 -0
  18. package/Common/cpp/worklets/RunLoop/EventLoop.h +52 -0
  19. package/Common/cpp/worklets/SharedItems/Serializable.cpp +545 -0
  20. package/Common/cpp/worklets/SharedItems/Serializable.h +473 -0
  21. package/Common/cpp/worklets/SharedItems/Synchronizable.cpp +67 -0
  22. package/Common/cpp/worklets/SharedItems/Synchronizable.h +59 -0
  23. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.cpp +81 -0
  24. package/Common/cpp/worklets/SharedItems/SynchronizableAccess.h +45 -0
  25. package/Common/cpp/worklets/Tools/Defs.h +10 -0
  26. package/Common/cpp/worklets/Tools/FeatureFlags.cpp +15 -0
  27. package/Common/cpp/worklets/Tools/FeatureFlags.h +44 -0
  28. package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
  29. package/Common/cpp/worklets/Tools/JSISerializer.h +51 -0
  30. package/Common/cpp/worklets/Tools/JSLogger.cpp +68 -0
  31. package/Common/cpp/worklets/Tools/JSLogger.h +37 -0
  32. package/Common/cpp/worklets/Tools/JSScheduler.cpp +24 -0
  33. package/Common/cpp/worklets/Tools/JSScheduler.h +39 -0
  34. package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
  35. package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +82 -0
  36. package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
  37. package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
  38. package/Common/cpp/worklets/Tools/UIScheduler.h +26 -0
  39. package/Common/cpp/worklets/Tools/VersionUtils.cpp +75 -0
  40. package/Common/cpp/worklets/Tools/VersionUtils.h +28 -0
  41. package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
  42. package/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
  43. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +46 -0
  44. package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +208 -0
  45. package/Common/cpp/worklets/Tools/WorkletsSystraceSection.h +161 -0
  46. package/Common/cpp/worklets/Tools/WorkletsVersion.cpp +54 -0
  47. package/Common/cpp/worklets/Tools/WorkletsVersion.h +20 -0
  48. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +38 -0
  49. package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +21 -0
  50. package/Common/cpp/worklets/WorkletRuntime/RuntimeData.cpp +3 -0
  51. package/Common/cpp/worklets/WorkletRuntime/RuntimeData.h +12 -0
  52. package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
  53. package/Common/cpp/worklets/WorkletRuntime/RuntimeKind.h +16 -0
  54. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.cpp +105 -0
  55. package/Common/cpp/worklets/WorkletRuntime/RuntimeManager.h +63 -0
  56. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +23 -0
  57. package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
  58. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +119 -0
  59. package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +134 -0
  60. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +244 -0
  61. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +102 -0
  62. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
  63. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +308 -0
  64. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +26 -0
  65. package/LICENSE +7 -0
  66. package/README.md +31 -0
  67. package/RNWorklets.podspec +94 -0
  68. package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.bundle +10512 -0
  69. package/SystemBundles/cc12deac6e91c435b76698c8445aed85612f2f88e733218d14f88a35d06fb0f4.map +3771 -0
  70. package/SystemModules/amzn__react-native-worklets-1.modules.txt +39 -0
  71. package/SystemModules/amzn__react-native-worklets-1.private.modules.txt +61 -0
  72. package/SystemModules/keplerscript-system-bundle-config.json +7 -0
  73. package/android/CMakeLists.txt +124 -0
  74. package/android/build.gradle +350 -0
  75. package/android/gradle.properties +5 -0
  76. package/android/proguard-rules.pro +3 -0
  77. package/android/spotless.gradle +9 -0
  78. package/android/src/experimentalBundling/com/swmansion/worklets/WorkletsModule.java +149 -0
  79. package/android/src/legacyBundling/com/swmansion/worklets/WorkletsModule.java +122 -0
  80. package/android/src/main/AndroidManifest.xml +2 -0
  81. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
  82. package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +43 -0
  83. package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +34 -0
  84. package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
  85. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +115 -0
  86. package/android/src/main/cpp/worklets/android/WorkletsModule.h +78 -0
  87. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +13 -0
  88. package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.java +60 -0
  89. package/android/src/main/java/com/swmansion/worklets/JSCallInvokerResolver.java +27 -0
  90. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThread.java +16 -0
  91. package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThreadBase.java +73 -0
  92. package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +48 -0
  93. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameCallback.java +19 -0
  94. package/android/src/main/java/com/swmansion/worklets/runloop/AnimationFrameQueue.java +113 -0
  95. package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
  96. package/apple/worklets/apple/AnimationFrameQueue.mm +135 -0
  97. package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
  98. package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
  99. package/apple/worklets/apple/IOSUIScheduler.h +15 -0
  100. package/apple/worklets/apple/IOSUIScheduler.mm +29 -0
  101. package/apple/worklets/apple/PlatformLogger.mm +31 -0
  102. package/apple/worklets/apple/SlowAnimations.h +8 -0
  103. package/apple/worklets/apple/SlowAnimations.mm +45 -0
  104. package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
  105. package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
  106. package/apple/worklets/apple/WorkletsMessageThread.mm +31 -0
  107. package/apple/worklets/apple/WorkletsModule.h +28 -0
  108. package/apple/worklets/apple/WorkletsModule.mm +121 -0
  109. package/bundleMode/index.d.ts +3 -0
  110. package/bundleMode/index.js +59 -0
  111. package/compatibility.json +11 -0
  112. package/kepler-compatibility.json +5 -0
  113. package/kepler-transformed-package-info.json +3 -0
  114. package/lib/module/PlatformChecker/PlatformChecker.js +8 -0
  115. package/lib/module/PlatformChecker/PlatformChecker.js.map +1 -0
  116. package/lib/module/PlatformChecker/index.js +18 -0
  117. package/lib/module/PlatformChecker/index.js.map +1 -0
  118. package/lib/module/WorkletsError.js +29 -0
  119. package/lib/module/WorkletsError.js.map +1 -0
  120. package/lib/module/WorkletsModule/JSWorklets.js +111 -0
  121. package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
  122. package/lib/module/WorkletsModule/NativeWorklets.js +126 -0
  123. package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
  124. package/lib/module/WorkletsModule/index.js +4 -0
  125. package/lib/module/WorkletsModule/index.js.map +1 -0
  126. package/lib/module/WorkletsModule/workletsModuleInstance.js +7 -0
  127. package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -0
  128. package/lib/module/WorkletsModule/workletsModuleInstance.web.js +5 -0
  129. package/lib/module/WorkletsModule/workletsModuleInstance.web.js.map +1 -0
  130. package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
  131. package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
  132. package/lib/module/bundleUnpacker.js +47 -0
  133. package/lib/module/bundleUnpacker.js.map +1 -0
  134. package/lib/module/callGuard.js +30 -0
  135. package/lib/module/callGuard.js.map +1 -0
  136. package/lib/module/deprecated.js +20 -0
  137. package/lib/module/deprecated.js.map +1 -0
  138. package/lib/module/errors.js +69 -0
  139. package/lib/module/errors.js.map +1 -0
  140. package/lib/module/featureFlags/index.js +48 -0
  141. package/lib/module/featureFlags/index.js.map +1 -0
  142. package/lib/module/featureFlags/staticFlags.json +4 -0
  143. package/lib/module/index.js +26 -0
  144. package/lib/module/index.js.map +1 -0
  145. package/lib/module/initializers.js +221 -0
  146. package/lib/module/initializers.js.map +1 -0
  147. package/lib/module/isSynchronizable.js +6 -0
  148. package/lib/module/isSynchronizable.js.map +1 -0
  149. package/lib/module/logger.js +15 -0
  150. package/lib/module/logger.js.map +1 -0
  151. package/lib/module/privateGlobals.d.js +7 -0
  152. package/lib/module/privateGlobals.d.js.map +1 -0
  153. package/lib/module/publicGlobals.js +6 -0
  154. package/lib/module/publicGlobals.js.map +1 -0
  155. package/lib/module/runLoop/common/setImmediatePolyfill.js +15 -0
  156. package/lib/module/runLoop/common/setImmediatePolyfill.js.map +1 -0
  157. package/lib/module/runLoop/common/setIntervalPolyfill.js +26 -0
  158. package/lib/module/runLoop/common/setIntervalPolyfill.js.map +1 -0
  159. package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js +10 -0
  160. package/lib/module/runLoop/uiRuntime/mockedRequestAnimationFrame.js.map +1 -0
  161. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js +52 -0
  162. package/lib/module/runLoop/uiRuntime/requestAnimationFrame.js.map +1 -0
  163. package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js +32 -0
  164. package/lib/module/runLoop/uiRuntime/setTimeoutPolyfill.js.map +1 -0
  165. package/lib/module/runLoop/workletRuntime/index.js +19 -0
  166. package/lib/module/runLoop/workletRuntime/index.js.map +1 -0
  167. package/lib/module/runLoop/workletRuntime/queueMicrotask.js +11 -0
  168. package/lib/module/runLoop/workletRuntime/queueMicrotask.js.map +1 -0
  169. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js +48 -0
  170. package/lib/module/runLoop/workletRuntime/requestAnimationFramePolyfill.js.map +1 -0
  171. package/lib/module/runLoop/workletRuntime/setTimeout.js +28 -0
  172. package/lib/module/runLoop/workletRuntime/setTimeout.js.map +1 -0
  173. package/lib/module/runLoop/workletRuntime/taskQueue.js +37 -0
  174. package/lib/module/runLoop/workletRuntime/taskQueue.js.map +1 -0
  175. package/lib/module/runtimeKind.js +39 -0
  176. package/lib/module/runtimeKind.js.map +1 -0
  177. package/lib/module/runtimes.js +100 -0
  178. package/lib/module/runtimes.js.map +1 -0
  179. package/lib/module/serializable.js +568 -0
  180. package/lib/module/serializable.js.map +1 -0
  181. package/lib/module/serializableMappingCache.js +37 -0
  182. package/lib/module/serializableMappingCache.js.map +1 -0
  183. package/lib/module/specs/NativeWorkletsModule.js +5 -0
  184. package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
  185. package/lib/module/specs/NativeWorkletsModule.kepler.js +7 -0
  186. package/lib/module/specs/NativeWorkletsModule.kepler.js.map +1 -0
  187. package/lib/module/specs/index.js +10 -0
  188. package/lib/module/specs/index.js.map +1 -0
  189. package/lib/module/synchronizable.js +9 -0
  190. package/lib/module/synchronizable.js.map +1 -0
  191. package/lib/module/synchronizableUnpacker.js +42 -0
  192. package/lib/module/synchronizableUnpacker.js.map +1 -0
  193. package/lib/module/threads.js +347 -0
  194. package/lib/module/threads.js.map +1 -0
  195. package/lib/module/utils/checkCppVersion.js +30 -0
  196. package/lib/module/utils/checkCppVersion.js.map +1 -0
  197. package/lib/module/utils/jsVersion.js +9 -0
  198. package/lib/module/utils/jsVersion.js.map +1 -0
  199. package/lib/module/valueUnpacker.js +60 -0
  200. package/lib/module/valueUnpacker.js.map +1 -0
  201. package/lib/module/workletFunction.js +37 -0
  202. package/lib/module/workletFunction.js.map +1 -0
  203. package/lib/module/workletRuntimeEntry.js +32 -0
  204. package/lib/module/workletRuntimeEntry.js.map +1 -0
  205. package/lib/module/workletTypes.js +12 -0
  206. package/lib/module/workletTypes.js.map +1 -0
  207. package/lib/typescript/PlatformChecker/PlatformChecker.d.ts +5 -0
  208. package/lib/typescript/PlatformChecker/PlatformChecker.d.ts.map +1 -0
  209. package/lib/typescript/PlatformChecker/index.d.ts +10 -0
  210. package/lib/typescript/PlatformChecker/index.d.ts.map +1 -0
  211. package/lib/typescript/WorkletsError.d.ts +15 -0
  212. package/lib/typescript/WorkletsError.d.ts.map +1 -0
  213. package/lib/typescript/WorkletsModule/JSWorklets.d.ts +3 -0
  214. package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -0
  215. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +3 -0
  216. package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
  217. package/lib/typescript/WorkletsModule/index.d.ts +3 -0
  218. package/lib/typescript/WorkletsModule/index.d.ts.map +1 -0
  219. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +2 -0
  220. package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -0
  221. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +2 -0
  222. package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -0
  223. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +37 -0
  224. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
  225. package/lib/typescript/bundleUnpacker.d.ts +7 -0
  226. package/lib/typescript/bundleUnpacker.d.ts.map +1 -0
  227. package/lib/typescript/callGuard.d.ts +4 -0
  228. package/lib/typescript/callGuard.d.ts.map +1 -0
  229. package/lib/typescript/deprecated.d.ts +20 -0
  230. package/lib/typescript/deprecated.d.ts.map +1 -0
  231. package/lib/typescript/errors.d.ts +16 -0
  232. package/lib/typescript/errors.d.ts.map +1 -0
  233. package/lib/typescript/featureFlags/index.d.ts +23 -0
  234. package/lib/typescript/featureFlags/index.d.ts.map +1 -0
  235. package/lib/typescript/index.d.ts +17 -0
  236. package/lib/typescript/index.d.ts.map +1 -0
  237. package/lib/typescript/initializers.d.ts +17 -0
  238. package/lib/typescript/initializers.d.ts.map +1 -0
  239. package/lib/typescript/isSynchronizable.d.ts +3 -0
  240. package/lib/typescript/isSynchronizable.d.ts.map +1 -0
  241. package/lib/typescript/logger.d.ts +5 -0
  242. package/lib/typescript/logger.d.ts.map +1 -0
  243. package/lib/typescript/publicGlobals.d.ts +31 -0
  244. package/lib/typescript/publicGlobals.d.ts.map +1 -0
  245. package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts +2 -0
  246. package/lib/typescript/runLoop/common/setImmediatePolyfill.d.ts.map +1 -0
  247. package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts +2 -0
  248. package/lib/typescript/runLoop/common/setIntervalPolyfill.d.ts.map +1 -0
  249. package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts +6 -0
  250. package/lib/typescript/runLoop/uiRuntime/mockedRequestAnimationFrame.d.ts.map +1 -0
  251. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts +2 -0
  252. package/lib/typescript/runLoop/uiRuntime/requestAnimationFrame.d.ts.map +1 -0
  253. package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts +2 -0
  254. package/lib/typescript/runLoop/uiRuntime/setTimeoutPolyfill.d.ts.map +1 -0
  255. package/lib/typescript/runLoop/workletRuntime/index.d.ts +2 -0
  256. package/lib/typescript/runLoop/workletRuntime/index.d.ts.map +1 -0
  257. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts +2 -0
  258. package/lib/typescript/runLoop/workletRuntime/queueMicrotask.d.ts.map +1 -0
  259. package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts +2 -0
  260. package/lib/typescript/runLoop/workletRuntime/requestAnimationFramePolyfill.d.ts.map +1 -0
  261. package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts +2 -0
  262. package/lib/typescript/runLoop/workletRuntime/setTimeout.d.ts.map +1 -0
  263. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts +10 -0
  264. package/lib/typescript/runLoop/workletRuntime/taskQueue.d.ts.map +1 -0
  265. package/lib/typescript/runtimeKind.d.ts +25 -0
  266. package/lib/typescript/runtimeKind.d.ts.map +1 -0
  267. package/lib/typescript/runtimes.d.ts +77 -0
  268. package/lib/typescript/runtimes.d.ts.map +1 -0
  269. package/lib/typescript/serializable.d.ts +20 -0
  270. package/lib/typescript/serializable.d.ts.map +1 -0
  271. package/lib/typescript/serializableMappingCache.d.ts +16 -0
  272. package/lib/typescript/serializableMappingCache.d.ts.map +1 -0
  273. package/lib/typescript/specs/NativeWorkletsModule.d.ts +7 -0
  274. package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
  275. package/lib/typescript/specs/NativeWorkletsModule.kepler.d.ts +7 -0
  276. package/lib/typescript/specs/NativeWorkletsModule.kepler.d.ts.map +1 -0
  277. package/lib/typescript/specs/index.d.ts +3 -0
  278. package/lib/typescript/specs/index.d.ts.map +1 -0
  279. package/lib/typescript/synchronizable.d.ts +15 -0
  280. package/lib/typescript/synchronizable.d.ts.map +1 -0
  281. package/lib/typescript/synchronizableUnpacker.d.ts +4 -0
  282. package/lib/typescript/synchronizableUnpacker.d.ts.map +1 -0
  283. package/lib/typescript/threads.d.ts +138 -0
  284. package/lib/typescript/threads.d.ts.map +1 -0
  285. package/lib/typescript/utils/checkCppVersion.d.ts +3 -0
  286. package/lib/typescript/utils/checkCppVersion.d.ts.map +1 -0
  287. package/lib/typescript/utils/jsVersion.d.ts +7 -0
  288. package/lib/typescript/utils/jsVersion.d.ts.map +1 -0
  289. package/lib/typescript/valueUnpacker.d.ts +6 -0
  290. package/lib/typescript/valueUnpacker.d.ts.map +1 -0
  291. package/lib/typescript/workletFunction.d.ts +27 -0
  292. package/lib/typescript/workletFunction.d.ts.map +1 -0
  293. package/lib/typescript/workletRuntimeEntry.d.ts +14 -0
  294. package/lib/typescript/workletRuntimeEntry.d.ts.map +1 -0
  295. package/lib/typescript/workletTypes.d.ts +61 -0
  296. package/lib/typescript/workletTypes.d.ts.map +1 -0
  297. package/package.json +153 -0
  298. package/plugin/index.js +1761 -0
  299. package/react-native.config.js +21 -0
  300. package/scripts/validate-react-native-version.js +43 -0
  301. package/scripts/worklets_utils.rb +94 -0
  302. package/src/PlatformChecker/PlatformChecker.ts +7 -0
  303. package/src/PlatformChecker/index.ts +30 -0
  304. package/src/WorkletsError.ts +36 -0
  305. package/src/WorkletsModule/JSWorklets.ts +200 -0
  306. package/src/WorkletsModule/NativeWorklets.ts +256 -0
  307. package/src/WorkletsModule/index.ts +7 -0
  308. package/src/WorkletsModule/workletsModuleInstance.ts +9 -0
  309. package/src/WorkletsModule/workletsModuleInstance.web.ts +5 -0
  310. package/src/WorkletsModule/workletsModuleProxy.ts +126 -0
  311. package/src/bundleUnpacker.ts +75 -0
  312. package/src/callGuard.ts +33 -0
  313. package/src/deprecated.ts +32 -0
  314. package/src/errors.ts +80 -0
  315. package/src/featureFlags/index.ts +72 -0
  316. package/src/featureFlags/staticFlags.json +4 -0
  317. package/src/index.ts +53 -0
  318. package/src/initializers.ts +262 -0
  319. package/src/isSynchronizable.ts +13 -0
  320. package/src/logger.ts +16 -0
  321. package/src/privateGlobals.d.ts +79 -0
  322. package/src/publicGlobals.ts +39 -0
  323. package/src/runLoop/common/setImmediatePolyfill.ts +20 -0
  324. package/src/runLoop/common/setIntervalPolyfill.ts +38 -0
  325. package/src/runLoop/uiRuntime/mockedRequestAnimationFrame.ts +11 -0
  326. package/src/runLoop/uiRuntime/requestAnimationFrame.ts +66 -0
  327. package/src/runLoop/uiRuntime/setTimeoutPolyfill.ts +40 -0
  328. package/src/runLoop/workletRuntime/index.ts +18 -0
  329. package/src/runLoop/workletRuntime/queueMicrotask.ts +13 -0
  330. package/src/runLoop/workletRuntime/requestAnimationFramePolyfill.ts +61 -0
  331. package/src/runLoop/workletRuntime/setTimeout.ts +37 -0
  332. package/src/runLoop/workletRuntime/taskQueue.ts +44 -0
  333. package/src/runtimeKind.ts +37 -0
  334. package/src/runtimes.ts +193 -0
  335. package/src/serializable.ts +801 -0
  336. package/src/serializableMappingCache.ts +42 -0
  337. package/src/specs/NativeWorkletsModule.kepler.ts +11 -0
  338. package/src/specs/NativeWorkletsModule.ts +9 -0
  339. package/src/specs/index.ts +13 -0
  340. package/src/synchronizable.ts +32 -0
  341. package/src/synchronizableUnpacker.ts +67 -0
  342. package/src/threads.ts +452 -0
  343. package/src/utils/checkCppVersion.ts +34 -0
  344. package/src/utils/jsVersion.ts +7 -0
  345. package/src/valueUnpacker.ts +90 -0
  346. package/src/workletFunction.ts +42 -0
  347. package/src/workletRuntimeEntry.ts +35 -0
  348. package/src/workletTypes.ts +81 -0
@@ -0,0 +1,63 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <worklets/Tools/UIScheduler.h>
5
+ #include <worklets/WorkletRuntime/RuntimeData.h>
6
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
7
+
8
+ #include <atomic>
9
+ #include <cstdint>
10
+ #include <map>
11
+ #include <memory>
12
+ #include <shared_mutex>
13
+ #include <string>
14
+ #include <vector>
15
+
16
+ namespace worklets {
17
+
18
+ /**
19
+ * Forward declaration to avoid circular dependencies.
20
+ */
21
+ class JSIWorkletsModuleProxy;
22
+
23
+ class RuntimeManager {
24
+ public:
25
+ std::shared_ptr<WorkletRuntime> getRuntime(uint64_t runtimeId);
26
+ std::shared_ptr<WorkletRuntime> getRuntime(const std::string &name);
27
+ #ifdef WORKLETS_BUNDLE_MODE
28
+ std::shared_ptr<WorkletRuntime> getRuntime(jsi::Runtime *runtime);
29
+ #endif // WORKLETS_BUNDLE_MODE
30
+
31
+ std::vector<std::shared_ptr<WorkletRuntime>> getAllRuntimes();
32
+
33
+ std::shared_ptr<WorkletRuntime> getUIRuntime();
34
+
35
+ std::shared_ptr<WorkletRuntime> createWorkletRuntime(
36
+ std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsModuleProxy,
37
+ const std::string &name,
38
+ std::shared_ptr<SerializableWorklet> initializer = nullptr,
39
+ const std::shared_ptr<AsyncQueue> &queue = nullptr,
40
+ bool enableEventLoop = true);
41
+
42
+ std::shared_ptr<WorkletRuntime> createUninitializedUIRuntime(
43
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
44
+ const std::shared_ptr<AsyncQueue> &uiAsyncQueue);
45
+
46
+ private:
47
+ uint64_t getNextRuntimeId();
48
+
49
+ void registerRuntime(
50
+ const uint64_t runtimeId,
51
+ const std::string &name,
52
+ const std::shared_ptr<WorkletRuntime> &workletRuntime);
53
+
54
+ std::atomic_uint64_t nextRuntimeId_{uiRuntimeId + 1};
55
+ std::map<uint64_t, std::weak_ptr<WorkletRuntime>> weakRuntimes_;
56
+ std::shared_mutex weakRuntimesMutex_;
57
+ std::map<std::string, uint64_t> nameToRuntimeId_;
58
+ #ifdef WORKLETS_BUNDLE_MODE
59
+ std::map<jsi::Runtime *, uint64_t> runtimeAddressToRuntimeId_;
60
+ #endif // WORKLETS_BUNDLE_MODE
61
+ };
62
+
63
+ } // namespace worklets
@@ -0,0 +1,23 @@
1
+ #include <worklets/Tools/WorkletsJSIUtils.h>
2
+ #include <worklets/WorkletRuntime/RuntimeKind.h>
3
+ #include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
4
+
5
+ #include <utility>
6
+
7
+ namespace worklets {
8
+
9
+ void UIRuntimeDecorator::decorate(
10
+ facebook::jsi::Runtime &uiRuntime,
11
+ std::function<
12
+ void(facebook::jsi::Runtime &rt, const facebook::jsi::Value &callback)>
13
+ &&requestAnimationFrame) {
14
+ uiRuntime.global().setProperty(
15
+ uiRuntime, runtimeKindBindingName, static_cast<int>(RuntimeKind::UI));
16
+
17
+ uiRuntime.global().setProperty(uiRuntime, "_UI", true);
18
+
19
+ jsi_utils::installJsiFunction(
20
+ uiRuntime, "requestAnimationFrame", std::move(requestAnimationFrame));
21
+ }
22
+
23
+ } // namespace worklets
@@ -0,0 +1,16 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ namespace worklets {
6
+
7
+ class UIRuntimeDecorator {
8
+ public:
9
+ static void decorate(
10
+ facebook::jsi::Runtime &uiRuntime,
11
+ std::function<void(
12
+ facebook::jsi::Runtime &rt,
13
+ const facebook::jsi::Value &callback)> &&requestAnimationFrame);
14
+ };
15
+
16
+ } // namespace worklets
@@ -0,0 +1,119 @@
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
+ // amznmod_react - cherry-pick f312abe
20
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
21
+ using namespace facebook::hermes::inspector_modern;
22
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
23
+
24
+ #if HERMES_ENABLE_DEBUGGER
25
+
26
+ // amznmod_react - cherry-pick f312abe
27
+ class HermesExecutorRuntimeAdapter
28
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
29
+ : public RuntimeAdapter
30
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
31
+ {
32
+ public:
33
+ explicit HermesExecutorRuntimeAdapter(
34
+ facebook::hermes::HermesRuntime &hermesRuntime,
35
+ const std::shared_ptr<MessageQueueThread> &thread)
36
+ : hermesRuntime_(hermesRuntime), thread_(std::move(thread)) {}
37
+
38
+ virtual ~HermesExecutorRuntimeAdapter() {
39
+ // This is required by iOS, because there is an assertion in the destructor
40
+ // that the thread was indeed `quit` before
41
+ thread_->quitSynchronous();
42
+ }
43
+
44
+ // amznmod_react - cherry-pick f312abe
45
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
46
+ facebook::hermes::HermesRuntime &getRuntime() override {
47
+ return hermesRuntime_;
48
+ }
49
+
50
+ // This is not empty in the original implementation, but we decided to tickle
51
+ // the runtime by running a small piece of code on every frame as using this
52
+ // required us to hold a refernce to the runtime inside this adapter which
53
+ // caused issues while reloading the app.
54
+ void tickleJs() override {}
55
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
56
+
57
+ public:
58
+ facebook::hermes::HermesRuntime &hermesRuntime_;
59
+ std::shared_ptr<MessageQueueThread> thread_;
60
+ };
61
+
62
+ #endif // HERMES_ENABLE_DEBUGGER
63
+
64
+ WorkletHermesRuntime::WorkletHermesRuntime(
65
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime,
66
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
67
+ const std::string &name)
68
+ : jsi::WithRuntimeDecorator<WorkletsReentrancyCheck>(
69
+ *runtime,
70
+ reentrancyCheck_),
71
+ runtime_(std::move(runtime)) {
72
+ // amznmod_react - cherry-pick f312abe
73
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
74
+ auto adapter =
75
+ std::make_unique<HermesExecutorRuntimeAdapter>(*runtime_, jsQueue);
76
+ debugToken_ = chrome::enableDebugging(std::move(adapter), name);
77
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
78
+
79
+ #ifndef NDEBUG
80
+ facebook::hermes::HermesRuntime *wrappedRuntime = runtime_.get();
81
+ jsi::Value evalWithSourceMap = jsi::Function::createFromHostFunction(
82
+ *runtime_,
83
+ jsi::PropNameID::forAscii(*runtime_, "evalWithSourceMap"),
84
+ 3,
85
+ [wrappedRuntime](
86
+ jsi::Runtime &rt,
87
+ const jsi::Value &thisValue,
88
+ const jsi::Value *args,
89
+ size_t count) -> jsi::Value {
90
+ auto code = std::make_shared<const jsi::StringBuffer>(
91
+ args[0].asString(rt).utf8(rt));
92
+ std::string sourceURL;
93
+ if (count > 1 && args[1].isString()) {
94
+ sourceURL = args[1].asString(rt).utf8(rt);
95
+ }
96
+ std::shared_ptr<const jsi::Buffer> sourceMap;
97
+ if (count > 2 && args[2].isString()) {
98
+ sourceMap = std::make_shared<const jsi::StringBuffer>(
99
+ args[2].asString(rt).utf8(rt));
100
+ }
101
+ return wrappedRuntime->evaluateJavaScriptWithSourceMap(
102
+ code, sourceMap, sourceURL);
103
+ });
104
+ runtime_->global().setProperty(
105
+ *runtime_, "evalWithSourceMap", evalWithSourceMap);
106
+ #endif // NDEBUG
107
+ }
108
+
109
+ WorkletHermesRuntime::~WorkletHermesRuntime() {
110
+ // amznmod_react - cherry-pick f312abe
111
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
112
+ // We have to disable debugging before the runtime is destroyed.
113
+ chrome::disableDebugging(debugToken_);
114
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
115
+ }
116
+
117
+ } // namespace worklets
118
+
119
+ #endif // JS_RUNTIME_HERMES
@@ -0,0 +1,134 @@
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
+ #include <react/debug/react_native_assert.h>
14
+
15
+ #include <atomic>
16
+ #include <memory>
17
+ #include <string>
18
+ #include <thread>
19
+
20
+ // amznmod_react - cherry-pick f312abe
21
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
22
+ #include <hermes/inspector-modern/chrome/Registration.h>
23
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
24
+
25
+ namespace worklets {
26
+
27
+ using namespace facebook;
28
+ using namespace react;
29
+ // amznmod_react - cherry-pick f312abe
30
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
31
+ using namespace facebook::hermes::inspector_modern;
32
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
33
+
34
+ // ReentrancyCheck is copied from React Native
35
+ // from ReactCommon/hermes/executor/HermesExecutorFactory.cpp
36
+ // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp
37
+ struct WorkletsReentrancyCheck {
38
+ // This is effectively a very subtle and complex assert, so only
39
+ // include it in builds which would include asserts.
40
+ #ifndef NDEBUG
41
+ WorkletsReentrancyCheck() : tid(std::thread::id()), depth(0) {}
42
+
43
+ void before() {
44
+ std::thread::id this_id = std::this_thread::get_id();
45
+ std::thread::id expected = std::thread::id();
46
+
47
+ // A note on memory ordering: the main purpose of these checks is
48
+ // to observe a before/before race, without an intervening after.
49
+ // This will be detected by the compare_exchange_strong atomicity
50
+ // properties, regardless of memory order.
51
+ //
52
+ // For everything else, it is easiest to think of 'depth' as a
53
+ // proxy for any access made inside the VM. If access to depth
54
+ // are reordered incorrectly, the same could be true of any other
55
+ // operation made by the VM. In fact, using acquire/release
56
+ // memory ordering could create barriers which mask a programmer
57
+ // error. So, we use relaxed memory order, to avoid masking
58
+ // actual ordering errors. Although, in practice, ordering errors
59
+ // of this sort would be surprising, because the decorator would
60
+ // need to call after() without before().
61
+
62
+ if (tid.compare_exchange_strong(
63
+ expected, this_id, std::memory_order_relaxed)) {
64
+ // Returns true if tid and expected were the same. If they
65
+ // were, then the stored tid referred to no thread, and we
66
+ // atomically saved this thread's tid. Now increment depth.
67
+ react_native_assert(
68
+ depth == 0 && "[Worklets] No thread id, but depth != 0");
69
+ ++depth;
70
+ } else if (expected == this_id) {
71
+ // If the stored tid referred to a thread, expected was set to
72
+ // that value. If that value is this thread's tid, that's ok,
73
+ // just increment depth again.
74
+ react_native_assert(
75
+ depth != 0 && "[Worklets] Thread id was set, but depth == 0");
76
+ ++depth;
77
+ } else {
78
+ // The stored tid was some other thread. This indicates a bad
79
+ // programmer error, where VM methods were called on two
80
+ // different threads unsafely. Fail fast (and hard) so the
81
+ // crash can be analyzed.
82
+ __builtin_trap();
83
+ }
84
+ }
85
+
86
+ void after() {
87
+ react_native_assert(
88
+ tid.load(std::memory_order_relaxed) == std::this_thread::get_id() &&
89
+ "[Worklets] No thread id in after()");
90
+ if (--depth == 0) {
91
+ // If we decremented depth to zero, store no-thread into tid.
92
+ std::thread::id expected = std::this_thread::get_id();
93
+ bool didWrite = tid.compare_exchange_strong(
94
+ expected, std::thread::id(), std::memory_order_relaxed);
95
+ react_native_assert(
96
+ didWrite && "[Worklets] Decremented to zero, but no tid write");
97
+ }
98
+ }
99
+
100
+ std::atomic<std::thread::id> tid;
101
+ // This is not atomic, as it is only written or read from the owning
102
+ // thread.
103
+ unsigned int depth;
104
+ #endif // NDEBUG
105
+ };
106
+
107
+ // This is in fact a subclass of jsi::Runtime! WithRuntimeDecorator is a
108
+ // template class that is a subclass of DecoratedRuntime which is also a
109
+ // template class that then inherits its template, which in this case is
110
+ // jsi::Runtime. So the inheritance is: WorkletHermesRuntime ->
111
+ // WithRuntimeDecorator -> DecoratedRuntime -> jsi::Runtime You can find out
112
+ // more about this in ReactCommon/jsi/jsi/Decorator.h or by following this link:
113
+ // https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/jsi/jsi/decorator.h
114
+ class WorkletHermesRuntime
115
+ : public jsi::WithRuntimeDecorator<WorkletsReentrancyCheck> {
116
+ public:
117
+ WorkletHermesRuntime(
118
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime,
119
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
120
+ const std::string &name);
121
+ ~WorkletHermesRuntime();
122
+
123
+ private:
124
+ std::unique_ptr<facebook::hermes::HermesRuntime> runtime_;
125
+ WorkletsReentrancyCheck reentrancyCheck_;
126
+ // amznmod_react - cherry-pick f312abe
127
+ #if HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
128
+ chrome::DebugSessionToken debugToken_;
129
+ #endif // HERMES_ENABLE_DEBUGGER && !defined(HERMES_V1_ENABLED)
130
+ };
131
+
132
+ } // namespace worklets
133
+
134
+ #endif // JS_RUNTIME_HERMES
@@ -0,0 +1,244 @@
1
+ #include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
2
+ #include <worklets/Resources/Unpackers.h>
3
+ #include <worklets/Tools/Defs.h>
4
+ #include <worklets/Tools/JSISerializer.h>
5
+ #include <worklets/Tools/JSLogger.h>
6
+ #include <worklets/Tools/WorkletsJSIUtils.h>
7
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
8
+ #include <worklets/WorkletRuntime/WorkletRuntimeCollector.h>
9
+ #include <worklets/WorkletRuntime/WorkletRuntimeDecorator.h>
10
+
11
+ #include <cxxreact/MessageQueueThread.h>
12
+ #include <jsi/decorator.h>
13
+ #include <jsi/jsi.h>
14
+
15
+ #include <memory>
16
+ #include <utility>
17
+
18
+ #if JS_RUNTIME_HERMES
19
+ #include <worklets/WorkletRuntime/WorkletHermesRuntime.h>
20
+ #else
21
+ #include <jsc/JSCRuntime.h>
22
+ #endif // JS_RUNTIME
23
+
24
+ namespace worklets {
25
+
26
+ class AroundLock {
27
+ const std::shared_ptr<std::recursive_mutex> mutex_;
28
+
29
+ public:
30
+ explicit AroundLock(const std::shared_ptr<std::recursive_mutex> &mutex)
31
+ : mutex_(mutex) {}
32
+
33
+ void before() const {
34
+ mutex_->lock();
35
+ }
36
+
37
+ void after() const {
38
+ mutex_->unlock();
39
+ }
40
+ };
41
+
42
+ class LockableRuntime : public jsi::WithRuntimeDecorator<AroundLock> {
43
+ AroundLock aroundLock_;
44
+ std::shared_ptr<jsi::Runtime> runtime_;
45
+
46
+ public:
47
+ explicit LockableRuntime(
48
+ std::shared_ptr<jsi::Runtime> &runtime,
49
+ const std::shared_ptr<std::recursive_mutex> &runtimeMutex)
50
+ : jsi::WithRuntimeDecorator<AroundLock>(*runtime, aroundLock_),
51
+ aroundLock_(runtimeMutex),
52
+ runtime_(std::move(runtime)) {}
53
+ };
54
+
55
+ static std::shared_ptr<jsi::Runtime> makeRuntime(
56
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
57
+ const std::string &name,
58
+ const std::shared_ptr<std::recursive_mutex> &runtimeMutex) {
59
+ std::shared_ptr<jsi::Runtime> jsiRuntime;
60
+ #if JS_RUNTIME_HERMES
61
+ auto hermesRuntime = facebook::hermes::makeHermesRuntime();
62
+ jsiRuntime = std::make_shared<WorkletHermesRuntime>(
63
+ std::move(hermesRuntime), jsQueue, name);
64
+ #else
65
+ jsiRuntime = facebook::jsc::makeJSCRuntime();
66
+ #endif
67
+
68
+ return std::make_shared<LockableRuntime>(jsiRuntime, runtimeMutex);
69
+ }
70
+
71
+ WorkletRuntime::WorkletRuntime(
72
+ uint64_t runtimeId,
73
+ const std::shared_ptr<MessageQueueThread> &jsQueue,
74
+ const std::string &name,
75
+ const std::shared_ptr<AsyncQueue> &queue,
76
+ bool enableEventLoop)
77
+ : runtimeId_(runtimeId),
78
+ runtimeMutex_(std::make_shared<std::recursive_mutex>()),
79
+ runtime_(makeRuntime(jsQueue, name, runtimeMutex_)),
80
+ name_(name),
81
+ queue_(queue) {
82
+ jsi::Runtime &rt = *runtime_;
83
+ WorkletRuntimeCollector::install(rt);
84
+ if (enableEventLoop && name != uiRuntimeName) {
85
+ eventLoop_ = std::make_shared<EventLoop>(name_, runtime_, queue_);
86
+ eventLoop_->run();
87
+ }
88
+ }
89
+
90
+ void WorkletRuntime::init(
91
+ std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsModuleProxy) {
92
+ jsi::Runtime &rt = *runtime_;
93
+ const auto jsScheduler = jsiWorkletsModuleProxy->getJSScheduler();
94
+ const auto isDevBundle = jsiWorkletsModuleProxy->isDevBundle();
95
+ #ifdef WORKLETS_BUNDLE_MODE
96
+ auto script = jsiWorkletsModuleProxy->getScript();
97
+ const auto &sourceUrl = jsiWorkletsModuleProxy->getSourceUrl();
98
+ #endif // WORKLETS_BUNDLE_MODE
99
+
100
+ auto optimizedJsiWorkletsModuleProxy =
101
+ jsi_utils::optimizedFromHostObject(rt, std::move(jsiWorkletsModuleProxy));
102
+
103
+ WorkletRuntimeDecorator::decorate(
104
+ rt,
105
+ name_,
106
+ jsScheduler,
107
+ isDevBundle,
108
+ std::move(optimizedJsiWorkletsModuleProxy),
109
+ eventLoop_);
110
+
111
+ #ifdef WORKLETS_BUNDLE_MODE
112
+ if (!script) {
113
+ throw std::runtime_error(
114
+ "[Worklets] Expected to receive the bundle, but got nullptr instead.");
115
+ }
116
+
117
+ try {
118
+ rt.evaluateJavaScript(script, sourceUrl);
119
+ } catch (facebook::jsi::JSError error) {
120
+ const auto &message = error.getMessage();
121
+ const auto &stack = error.getStack();
122
+ if (!message.starts_with("[Worklets] Worklets initialized successfully")) {
123
+ const auto newMessage =
124
+ "[Worklets] Failed to initialize runtime. Reason: " + message;
125
+ JSLogger::reportFatalErrorOnJS(
126
+ jsScheduler,
127
+ {.message = newMessage,
128
+ .stack = stack,
129
+ .name = "WorkletsError",
130
+ .jsEngine = "Worklets"});
131
+ }
132
+ }
133
+ #else
134
+ // Legacy behavior
135
+ auto valueUnpackerBuffer =
136
+ std::make_shared<const jsi::StringBuffer>(ValueUnpackerCode);
137
+ rt.evaluateJavaScript(valueUnpackerBuffer, "valueUnpacker");
138
+
139
+ auto synchronizableUnpackerBuffer =
140
+ std::make_shared<const jsi::StringBuffer>(SynchronizableUnpackerCode);
141
+ rt.evaluateJavaScript(synchronizableUnpackerBuffer, "synchronizableUnpacker");
142
+ #endif // WORKLETS_BUNDLE_MODE
143
+ }
144
+
145
+ void WorkletRuntime::runAsyncGuarded(
146
+ const std::shared_ptr<SerializableWorklet> &worklet) {
147
+ react_native_assert(
148
+ "[Worklets] Tried to invoke `runAsyncGuarded` on a Worklet Runtime but "
149
+ "the async queue is not set. Recreate the runtime with a valid async queue.");
150
+
151
+ queue_->push([worklet, weakThis = weak_from_this()] {
152
+ auto strongThis = weakThis.lock();
153
+ if (!strongThis) {
154
+ return;
155
+ }
156
+
157
+ strongThis->runGuarded(worklet);
158
+ });
159
+ }
160
+
161
+ jsi::Value WorkletRuntime::executeSync(
162
+ jsi::Runtime &rt,
163
+ const jsi::Value &worklet) const {
164
+ auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
165
+ rt,
166
+ worklet,
167
+ "[Worklets] Only worklets can be executed synchronously on UI runtime.");
168
+ auto lock = std::unique_lock<std::recursive_mutex>(*runtimeMutex_);
169
+ jsi::Runtime &uiRuntime = getJSIRuntime();
170
+ auto result = runGuarded(serializableWorklet);
171
+ auto serializableResult = extractSerializableOrThrow(uiRuntime, result);
172
+ lock.unlock();
173
+ return serializableResult->toJSValue(rt);
174
+ }
175
+
176
+ #ifdef WORKLETS_BUNDLE_MODE
177
+ jsi::Value WorkletRuntime::executeSync(
178
+ std::function<jsi::Value(jsi::Runtime &)> &&job) const {
179
+ auto lock = std::unique_lock<std::recursive_mutex>(*runtimeMutex_);
180
+ jsi::Runtime &uiRuntime = getJSIRuntime();
181
+ return job(uiRuntime);
182
+ }
183
+
184
+ jsi::Value WorkletRuntime::executeSync(
185
+ const std::function<jsi::Value(jsi::Runtime &)> &job) const {
186
+ auto lock = std::unique_lock<std::recursive_mutex>(*runtimeMutex_);
187
+ jsi::Runtime &uiRuntime = getJSIRuntime();
188
+ return job(uiRuntime);
189
+ }
190
+ #endif // WORKLETS_BUNDLE_MODE
191
+
192
+ jsi::Value WorkletRuntime::get(
193
+ jsi::Runtime &rt,
194
+ const jsi::PropNameID &propName) {
195
+ auto name = propName.utf8(rt);
196
+ if (name == "toString") {
197
+ return jsi::Function::createFromHostFunction(
198
+ rt,
199
+ propName,
200
+ 0,
201
+ [weakThis = weak_from_this()](
202
+ jsi::Runtime &rt, const jsi::Value &, const jsi::Value *, size_t)
203
+ -> jsi::Value {
204
+ auto strongThis = weakThis.lock();
205
+ if (!strongThis) {
206
+ return jsi::String::createFromUtf8(rt, "");
207
+ }
208
+
209
+ return jsi::String::createFromUtf8(rt, strongThis->toString());
210
+ });
211
+ }
212
+ if (name == "name") {
213
+ return jsi::String::createFromUtf8(rt, name_);
214
+ }
215
+ return jsi::Value::undefined();
216
+ }
217
+
218
+ std::vector<jsi::PropNameID> WorkletRuntime::getPropertyNames(
219
+ jsi::Runtime &rt) {
220
+ std::vector<jsi::PropNameID> result;
221
+ result.push_back(jsi::PropNameID::forUtf8(rt, "toString"));
222
+ result.push_back(jsi::PropNameID::forUtf8(rt, "name"));
223
+ return result;
224
+ }
225
+
226
+ std::shared_ptr<WorkletRuntime> extractWorkletRuntime(
227
+ jsi::Runtime &rt,
228
+ const jsi::Value &value) {
229
+ return value.getObject(rt).getHostObject<WorkletRuntime>(rt);
230
+ }
231
+
232
+ void scheduleOnRuntime(
233
+ jsi::Runtime &rt,
234
+ const jsi::Value &workletRuntimeValue,
235
+ const jsi::Value &serializableWorkletValue) {
236
+ auto workletRuntime = extractWorkletRuntime(rt, workletRuntimeValue);
237
+ auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
238
+ rt,
239
+ serializableWorkletValue,
240
+ "[Worklets] Function passed to `_scheduleOnRuntime` is not a serializable worklet.");
241
+ workletRuntime->runAsyncGuarded(serializableWorklet);
242
+ }
243
+
244
+ } // namespace worklets