@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,801 @@
1
+ 'use strict';
2
+ import { registerWorkletStackDetails } from './errors';
3
+ import { isSynchronizable } from './isSynchronizable';
4
+ import { logger } from './logger';
5
+ import { SHOULD_BE_USE_WEB } from './PlatformChecker';
6
+ import {
7
+ serializableMappingCache,
8
+ serializableMappingFlag,
9
+ } from './serializableMappingCache';
10
+ import type { Synchronizable } from './synchronizable';
11
+ import { jsVersion } from './utils/jsVersion';
12
+ import { isWorkletFunction } from './workletFunction';
13
+ import { WorkletsError } from './WorkletsError';
14
+ import { WorkletsModule } from './WorkletsModule';
15
+ import type {
16
+ FlatSerializableRef,
17
+ SerializableRef,
18
+ WorkletFunction,
19
+ WorkletImport,
20
+ } from './workletTypes';
21
+
22
+ // for web and jest environments this file provides a stub implementation
23
+ // where no serializable references are used. Instead, the objects themselves are used
24
+ // instead of serializable references, because of the fact that we don't have to deal with
25
+ // running the code on separate VMs.
26
+
27
+ const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';
28
+
29
+ function isHostObject(value: NonNullable<object>) {
30
+ 'worklet';
31
+ // We could use JSI to determine whether an object is a host object, however
32
+ // the below workaround works well and is way faster than an additional JSI call.
33
+ // We use the fact that host objects have broken implementation of `hasOwnProperty`
34
+ // and hence return true for all `in` checks regardless of the key we ask for.
35
+ return MAGIC_KEY in value;
36
+ }
37
+
38
+ export function isSerializableRef(value: unknown): value is SerializableRef {
39
+ return (
40
+ typeof value === 'object' && value !== null && '__serializableRef' in value
41
+ );
42
+ }
43
+
44
+ function isPlainJSObject(object: object): object is Record<string, unknown> {
45
+ 'worklet';
46
+ return Object.getPrototypeOf(object) === Object.prototype;
47
+ }
48
+
49
+ function isTurboModuleLike(object: object): object is Record<string, unknown> {
50
+ return isHostObject(Object.getPrototypeOf(object));
51
+ }
52
+
53
+ function getFromCache(value: object) {
54
+ const cached = serializableMappingCache.get(value);
55
+ if (cached === serializableMappingFlag) {
56
+ // This means that `value` was already a clone and we should return it as is.
57
+ return value;
58
+ }
59
+ return cached;
60
+ }
61
+
62
+ // The below object is used as a replacement for objects that cannot be transferred
63
+ // as serializable values. In createSerializable we detect if an object is of
64
+ // a plain Object.prototype and only allow such objects to be transferred. This lets
65
+ // us avoid all sorts of react internals from leaking into the UI runtime. To make it
66
+ // possible to catch errors when someone actually tries to access such object on the UI
67
+ // runtime, we use the below Proxy object which is instantiated on the UI runtime and
68
+ // throws whenever someone tries to access its fields.
69
+ const INACCESSIBLE_OBJECT = {
70
+ __init: () => {
71
+ 'worklet';
72
+ return new Proxy(
73
+ {},
74
+ {
75
+ get: (_: unknown, prop: string | symbol) => {
76
+ if (
77
+ prop === '_isReanimatedSharedValue' ||
78
+ prop === '__remoteFunction'
79
+ ) {
80
+ // not very happy about this check here, but we need to allow for
81
+ // "inaccessible" objects to be tested with isSerializableRef check
82
+ // as it is being used in the mappers when extracting inputs recursively
83
+ // as well as with isRemoteFunction when cloning objects recursively.
84
+ // Apparently we can't check if a key exists there as HostObjects always
85
+ // return true for such tests, so the only possibility for us is to
86
+ // actually access that key and see if it is set to true. We therefore
87
+ // need to allow for this key to be accessed here.
88
+ return false;
89
+ }
90
+ throw new WorkletsError(
91
+ `Trying to access property \`${String(
92
+ prop
93
+ )}\` of an object which cannot be sent to the UI runtime.`
94
+ );
95
+ },
96
+ set: () => {
97
+ throw new WorkletsError(
98
+ 'Trying to write to an object which cannot be sent to the UI runtime.'
99
+ );
100
+ },
101
+ }
102
+ );
103
+ },
104
+ };
105
+
106
+ const VALID_ARRAY_VIEWS_NAMES = [
107
+ 'Int8Array',
108
+ 'Uint8Array',
109
+ 'Uint8ClampedArray',
110
+ 'Int16Array',
111
+ 'Uint16Array',
112
+ 'Int32Array',
113
+ 'Uint32Array',
114
+ 'Float32Array',
115
+ 'Float64Array',
116
+ 'BigInt64Array',
117
+ 'BigUint64Array',
118
+ 'DataView',
119
+ ];
120
+
121
+ const DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;
122
+ // Below variable stores object that we process in createSerializable at the specified depth.
123
+ // We use it to check if later on the function reenters with the same object
124
+ let processedObjectAtThresholdDepth: unknown;
125
+
126
+ function createSerializableWeb<T>(value: T): SerializableRef<T> {
127
+ return value as SerializableRef<T>;
128
+ }
129
+
130
+ function createSerializableNative<T>(
131
+ value: T,
132
+ shouldPersistRemote = false,
133
+ depth = 0
134
+ ): SerializableRef<T> {
135
+ detectCyclicObject(value, depth);
136
+
137
+ const isObject = typeof value === 'object';
138
+ const isFunction = typeof value === 'function';
139
+
140
+ if (typeof value === 'string') {
141
+ return cloneString(value) as SerializableRef<T>;
142
+ }
143
+
144
+ if (typeof value === 'number') {
145
+ return cloneNumber(value) as SerializableRef<T>;
146
+ }
147
+
148
+ if (typeof value === 'boolean') {
149
+ return cloneBoolean(value) as SerializableRef<T>;
150
+ }
151
+
152
+ if (typeof value === 'bigint') {
153
+ return cloneBigInt(value) as SerializableRef<T>;
154
+ }
155
+
156
+ if (value === undefined) {
157
+ return cloneUndefined() as SerializableRef<T>;
158
+ }
159
+
160
+ if (value === null) {
161
+ return cloneNull() as SerializableRef<T>;
162
+ }
163
+
164
+ if ((!isObject && !isFunction) || value === null) {
165
+ return clonePrimitive(value, shouldPersistRemote);
166
+ }
167
+
168
+ const cached = getFromCache(value);
169
+ if (cached !== undefined) {
170
+ return cached as SerializableRef<T>;
171
+ }
172
+
173
+ if (Array.isArray(value)) {
174
+ return cloneArray(value, shouldPersistRemote, depth);
175
+ }
176
+ if (
177
+ globalThis._WORKLETS_BUNDLE_MODE &&
178
+ isFunction &&
179
+ (value as WorkletImport).__bundleData
180
+ ) {
181
+ return cloneImport(value as WorkletImport) as SerializableRef<T>;
182
+ }
183
+ if (isFunction && !isWorkletFunction(value)) {
184
+ return cloneRemoteFunction(value);
185
+ }
186
+ // RN has introduced a new representation of TurboModules as a JS object whose prototype is the host object
187
+ // More details: https://github.com/facebook/react-native/blob/main/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp#L182
188
+ if (isTurboModuleLike(value)) {
189
+ return cloneTurboModuleLike(value, shouldPersistRemote, depth);
190
+ }
191
+ if (isHostObject(value)) {
192
+ return cloneHostObject(value);
193
+ }
194
+ if (isPlainJSObject(value) && value.__init) {
195
+ return cloneInitializer(
196
+ value,
197
+ shouldPersistRemote,
198
+ depth
199
+ ) as SerializableRef<T>;
200
+ }
201
+ if (isPlainJSObject(value) && value.__workletContextObjectFactory) {
202
+ return cloneContextObject(value);
203
+ }
204
+ if ((isPlainJSObject(value) || isFunction) && isWorkletFunction(value)) {
205
+ return cloneWorklet(value, shouldPersistRemote, depth);
206
+ }
207
+ if (isSynchronizable(value)) {
208
+ return cloneSynchronizable(value) as SerializableRef<T>;
209
+ }
210
+ if (isPlainJSObject(value) || isFunction) {
211
+ return clonePlainJSObject(value, shouldPersistRemote, depth);
212
+ }
213
+ if (value instanceof Set) {
214
+ return cloneSet(value);
215
+ }
216
+ if (value instanceof Map) {
217
+ return cloneMap(value);
218
+ }
219
+ if (value instanceof RegExp) {
220
+ return cloneRegExp(value);
221
+ }
222
+ if (value instanceof Error) {
223
+ return cloneError(value);
224
+ }
225
+ if (value instanceof ArrayBuffer) {
226
+ return cloneArrayBuffer(value, shouldPersistRemote);
227
+ }
228
+ if (ArrayBuffer.isView(value)) {
229
+ // typed array (e.g. Int32Array, Uint8ClampedArray) or DataView
230
+ return cloneArrayBufferView(value);
231
+ }
232
+ return inaccessibleObject(value);
233
+ }
234
+
235
+ if (globalThis._WORKLETS_BUNDLE_MODE) {
236
+ // TODO: Do it programatically.
237
+ createSerializableNative.__bundleData = {
238
+ imported: 'createSerializable',
239
+ // @ts-expect-error resolveWeak is defined by Metro
240
+ source: require.resolveWeak('./index'),
241
+ };
242
+ }
243
+
244
+ interface CreateSerializable {
245
+ <T>(value: T): SerializableRef<T>;
246
+ <T>(
247
+ value: T,
248
+ shouldPersistRemote: boolean,
249
+ depth: number
250
+ ): SerializableRef<T>;
251
+ }
252
+
253
+ export const createSerializable: CreateSerializable = SHOULD_BE_USE_WEB
254
+ ? createSerializableWeb
255
+ : createSerializableNative;
256
+
257
+ function detectCyclicObject(value: unknown, depth: number) {
258
+ if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
259
+ // if we reach certain recursion depth we suspect that we are dealing with a cyclic object.
260
+ // this type of objects are not supported and cannot be transferred as serializable, so we
261
+ // implement a simple detection mechanism that remembers the value at a given depth and
262
+ // tests whether we try reenter this method later on with the same value. If that happens
263
+ // we throw an appropriate error.
264
+ if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
265
+ processedObjectAtThresholdDepth = value;
266
+ } else if (value === processedObjectAtThresholdDepth) {
267
+ throw new WorkletsError(
268
+ 'Trying to convert a cyclic object to a serializable. This is not supported.'
269
+ );
270
+ }
271
+ } else {
272
+ processedObjectAtThresholdDepth = undefined;
273
+ }
274
+ }
275
+
276
+ function clonePrimitive<T>(
277
+ value: T,
278
+ shouldPersistRemote: boolean
279
+ ): SerializableRef<T> {
280
+ return WorkletsModule.createSerializable(value, shouldPersistRemote);
281
+ }
282
+
283
+ function cloneString(value: string): SerializableRef<string> {
284
+ return WorkletsModule.createSerializableString(value);
285
+ }
286
+
287
+ function cloneNumber(value: number): SerializableRef<number> {
288
+ return WorkletsModule.createSerializableNumber(value);
289
+ }
290
+
291
+ function cloneBoolean(value: boolean): SerializableRef<boolean> {
292
+ return WorkletsModule.createSerializableBoolean(value);
293
+ }
294
+
295
+ function cloneBigInt(value: bigint): SerializableRef<bigint> {
296
+ return WorkletsModule.createSerializableBigInt(value);
297
+ }
298
+
299
+ function cloneUndefined(): SerializableRef<undefined> {
300
+ return WorkletsModule.createSerializableUndefined();
301
+ }
302
+
303
+ function cloneNull(): SerializableRef<null> {
304
+ return WorkletsModule.createSerializableNull();
305
+ }
306
+
307
+ function cloneObjectProperties<T extends object>(
308
+ value: T,
309
+ shouldPersistRemote: boolean,
310
+ depth: number
311
+ ): Record<string, unknown> {
312
+ const clonedProps: Record<string, unknown> = {};
313
+ for (const [key, element] of Object.entries(value)) {
314
+ // We don't need to clone __initData field as it contains long strings
315
+ // representing the worklet code, source map, and location, and we will
316
+ // serialize/deserialize it once.
317
+ if (key === '__initData' && clonedProps.__initData !== undefined) {
318
+ continue;
319
+ }
320
+ clonedProps[key] = createSerializable(
321
+ element,
322
+ shouldPersistRemote,
323
+ depth + 1
324
+ );
325
+ }
326
+ return clonedProps;
327
+ }
328
+
329
+ function cloneInitializer(
330
+ value: object,
331
+ shouldPersistRemote = false,
332
+ depth = 0
333
+ ): SerializableRef<object> {
334
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
335
+ value,
336
+ shouldPersistRemote,
337
+ depth
338
+ );
339
+ return WorkletsModule.createSerializableInitializer(clonedProps);
340
+ }
341
+
342
+ function cloneArray<T extends unknown[]>(
343
+ value: T,
344
+ shouldPersistRemote: boolean,
345
+ depth: number
346
+ ): SerializableRef<T> {
347
+ const clonedElements = value.map((element) =>
348
+ createSerializable(element, shouldPersistRemote, depth + 1)
349
+ );
350
+ const clone = WorkletsModule.createSerializableArray(
351
+ clonedElements,
352
+ shouldPersistRemote
353
+ ) as SerializableRef<T>;
354
+ serializableMappingCache.set(value, clone);
355
+ serializableMappingCache.set(clone);
356
+
357
+ freezeObjectInDev(value);
358
+ return clone;
359
+ }
360
+
361
+ function cloneRemoteFunction<TArgs extends unknown[], TReturn>(
362
+ value: (...args: TArgs) => TReturn
363
+ ): SerializableRef<TReturn> {
364
+ const clone = WorkletsModule.createSerializableFunction(value);
365
+ serializableMappingCache.set(value, clone);
366
+ serializableMappingCache.set(clone);
367
+
368
+ freezeObjectInDev(value);
369
+ return clone;
370
+ }
371
+
372
+ function cloneHostObject<T extends object>(value: T): SerializableRef<T> {
373
+ // for host objects we pass the reference to the object as serializable and
374
+ // then recreate new host object wrapping the same instance on the UI thread.
375
+ // there is no point of iterating over keys as we do for regular objects.
376
+ const clone = WorkletsModule.createSerializableHostObject(value);
377
+ serializableMappingCache.set(value, clone);
378
+ serializableMappingCache.set(clone);
379
+
380
+ return clone;
381
+ }
382
+
383
+ function cloneWorklet<T extends WorkletFunction>(
384
+ value: T,
385
+ shouldPersistRemote: boolean,
386
+ depth: number
387
+ ): SerializableRef<T> {
388
+ if (__DEV__) {
389
+ const babelVersion = (value as WorkletFunction).__pluginVersion;
390
+ if (babelVersion !== undefined && babelVersion !== jsVersion) {
391
+ throw new WorkletsError(
392
+ `Mismatch between JavaScript code version and Worklets Babel plugin version (${jsVersion} vs. ${babelVersion}).
393
+ See \`https://docs.swmansion.com/react-native-worklets/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-worklets-babel-plugin-version\` for more details.
394
+ Offending code was: \`${getWorkletCode(value)}\``
395
+ );
396
+ }
397
+ registerWorkletStackDetails(
398
+ value.__workletHash,
399
+ (value as WorkletFunction).__stackDetails!
400
+ );
401
+ }
402
+ if ((value as WorkletFunction).__stackDetails) {
403
+ // `Error` type of value cannot be copied to the UI thread, so we
404
+ // remove it after we handled it in dev mode or delete it to ignore it in production mode.
405
+ // Not removing this would cause an infinite loop in production mode and it just
406
+ // seems more elegant to handle it this way.
407
+ delete (value as WorkletFunction).__stackDetails;
408
+ }
409
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
410
+ value,
411
+ true,
412
+ depth
413
+ );
414
+ // to save on transferring static __initData field of worklet structure
415
+ // we request serializable value to persist its UI counterpart. This means
416
+ // that the __initData field that contains long strings representing the
417
+ // worklet code, source map, and location, will always be
418
+ // serialized/deserialized once.
419
+ clonedProps.__initData = createSerializable(
420
+ value.__initData,
421
+ true,
422
+ depth + 1
423
+ );
424
+
425
+ const clone = WorkletsModule.createSerializableWorklet(
426
+ clonedProps,
427
+ // TODO: Check after refactor if we can remove shouldPersistRemote parameter (imho it's redundant here since worklets are always persistent)
428
+ // retain all worklets
429
+ true
430
+ ) as SerializableRef<T>;
431
+ serializableMappingCache.set(value, clone);
432
+ serializableMappingCache.set(clone);
433
+
434
+ freezeObjectInDev(value);
435
+ return clone;
436
+ }
437
+
438
+ /**
439
+ * TurboModuleLike objects are JS objects that have a TurboModule as their
440
+ * prototype.
441
+ */
442
+ function cloneTurboModuleLike<T extends object>(
443
+ value: T,
444
+ shouldPersistRemote: boolean,
445
+ depth: number
446
+ ): SerializableRef<T> {
447
+ const proto = Object.getPrototypeOf(value);
448
+ const clonedProps = cloneObjectProperties(value, shouldPersistRemote, depth);
449
+ const clone = WorkletsModule.createSerializableTurboModuleLike(
450
+ clonedProps,
451
+ proto
452
+ ) as SerializableRef<T>;
453
+ return clone;
454
+ }
455
+
456
+ function cloneContextObject<T extends object>(value: T): SerializableRef<T> {
457
+ const workletContextObjectFactory = (value as Record<string, unknown>)
458
+ .__workletContextObjectFactory as () => T;
459
+ const handle = cloneInitializer({
460
+ __init: () => {
461
+ 'worklet';
462
+ return workletContextObjectFactory();
463
+ },
464
+ });
465
+ serializableMappingCache.set(value, handle);
466
+ return handle as SerializableRef<T>;
467
+ }
468
+
469
+ function clonePlainJSObject<T extends object>(
470
+ value: T,
471
+ shouldPersistRemote: boolean,
472
+ depth: number
473
+ ): SerializableRef<T> {
474
+ const clonedProps: Record<string, unknown> = cloneObjectProperties(
475
+ value,
476
+ shouldPersistRemote,
477
+ depth
478
+ );
479
+ const clone = WorkletsModule.createSerializableObject(
480
+ clonedProps,
481
+ shouldPersistRemote,
482
+ value
483
+ ) as SerializableRef<T>;
484
+ serializableMappingCache.set(value, clone);
485
+ serializableMappingCache.set(clone);
486
+
487
+ freezeObjectInDev(value);
488
+ return clone;
489
+ }
490
+
491
+ function cloneMap<T extends Map<unknown, unknown>>(
492
+ value: T
493
+ ): SerializableRef<T> {
494
+ const clonedKeys: unknown[] = [];
495
+ const clonedValues: unknown[] = [];
496
+ for (const [key, element] of value.entries()) {
497
+ clonedKeys.push(createSerializable(key));
498
+ clonedValues.push(createSerializable(element));
499
+ }
500
+ const clone = WorkletsModule.createSerializableMap(
501
+ clonedKeys,
502
+ clonedValues
503
+ ) as SerializableRef<T>;
504
+ serializableMappingCache.set(value, clone);
505
+ serializableMappingCache.set(clone);
506
+
507
+ freezeObjectInDev(value);
508
+ return clone;
509
+ }
510
+
511
+ function cloneSet<T extends Set<unknown>>(value: T): SerializableRef<T> {
512
+ const clonedElements: unknown[] = [];
513
+ for (const element of value) {
514
+ clonedElements.push(createSerializable(element));
515
+ }
516
+ const clone = WorkletsModule.createSerializableSet(
517
+ clonedElements
518
+ ) as SerializableRef<T>;
519
+ serializableMappingCache.set(value, clone);
520
+ serializableMappingCache.set(clone);
521
+
522
+ freezeObjectInDev(value);
523
+ return clone;
524
+ }
525
+
526
+ function cloneRegExp<T extends RegExp>(value: T): SerializableRef<T> {
527
+ const pattern = value.source;
528
+ const flags = value.flags;
529
+ const handle = cloneInitializer({
530
+ __init: () => {
531
+ 'worklet';
532
+ return new RegExp(pattern, flags);
533
+ },
534
+ }) as unknown as SerializableRef<T>;
535
+ serializableMappingCache.set(value, handle);
536
+
537
+ return handle;
538
+ }
539
+
540
+ function cloneError<T extends Error>(value: T): SerializableRef<T> {
541
+ const { name, message, stack } = value;
542
+ const handle = cloneInitializer({
543
+ __init: () => {
544
+ 'worklet';
545
+ // eslint-disable-next-line reanimated/use-worklets-error
546
+ const error = new Error();
547
+ error.name = name;
548
+ error.message = message;
549
+ error.stack = stack;
550
+ return error;
551
+ },
552
+ });
553
+ serializableMappingCache.set(value, handle);
554
+ return handle as unknown as SerializableRef<T>;
555
+ }
556
+
557
+ function cloneArrayBuffer<T extends ArrayBuffer>(
558
+ value: T,
559
+ shouldPersistRemote: boolean
560
+ ): SerializableRef<T> {
561
+ const clone = WorkletsModule.createSerializable(
562
+ value,
563
+ shouldPersistRemote,
564
+ value
565
+ );
566
+ serializableMappingCache.set(value, clone);
567
+ serializableMappingCache.set(clone);
568
+
569
+ return clone;
570
+ }
571
+
572
+ function cloneArrayBufferView<T extends ArrayBufferView>(
573
+ value: T
574
+ ): SerializableRef<T> {
575
+ const buffer = value.buffer;
576
+ const typeName = value.constructor.name;
577
+ const handle = cloneInitializer({
578
+ __init: () => {
579
+ 'worklet';
580
+ if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
581
+ throw new WorkletsError(`Invalid array view name \`${typeName}\`.`);
582
+ }
583
+ const constructor = global[typeName as keyof typeof global];
584
+ if (constructor === undefined) {
585
+ throw new WorkletsError(`Constructor for \`${typeName}\` not found.`);
586
+ }
587
+ return new constructor(buffer);
588
+ },
589
+ }) as unknown as SerializableRef<T>;
590
+ serializableMappingCache.set(value, handle);
591
+
592
+ return handle;
593
+ }
594
+
595
+ function cloneSynchronizable<TValue>(
596
+ value: Synchronizable<TValue>
597
+ ): SerializableRef<TValue> {
598
+ serializableMappingCache.set(value);
599
+ return value;
600
+ }
601
+
602
+ function cloneImport<TValue extends WorkletImport>(
603
+ value: TValue
604
+ ): SerializableRef<TValue> {
605
+ const { source, imported } = value.__bundleData;
606
+ const clone = WorkletsModule.createSerializableImport(source, imported);
607
+
608
+ serializableMappingCache.set(value, clone);
609
+ serializableMappingCache.set(clone);
610
+
611
+ return clone as SerializableRef<TValue>;
612
+ }
613
+
614
+ function inaccessibleObject<T extends object>(value: T): SerializableRef<T> {
615
+ // This is reached for object types that are not of plain Object.prototype.
616
+ // We don't support such objects from being transferred as serializables to
617
+ // the UI runtime and hence we replace them with "inaccessible object"
618
+ // which is implemented as a Proxy object that throws on any attempt
619
+ // of accessing its fields. We argue that such objects can sometimes leak
620
+ // as attributes of objects being captured by worklets but should never
621
+ // be used on the UI runtime regardless. If they are being accessed, the user
622
+ // will get an appropriate error message.
623
+ const clone = createSerializable<T>(INACCESSIBLE_OBJECT as T);
624
+ serializableMappingCache.set(value, clone);
625
+ return clone;
626
+ }
627
+
628
+ const WORKLET_CODE_THRESHOLD = 255;
629
+
630
+ function getWorkletCode(value: WorkletFunction) {
631
+ const code = value?.__initData?.code;
632
+ if (!code) {
633
+ return 'unknown';
634
+ }
635
+ if (code.length > WORKLET_CODE_THRESHOLD) {
636
+ return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;
637
+ }
638
+ return code;
639
+ }
640
+
641
+ type RemoteFunction<T> = {
642
+ __remoteFunction: FlatSerializableRef<T>;
643
+ };
644
+
645
+ function isRemoteFunction<T>(value: {
646
+ __remoteFunction?: unknown;
647
+ }): value is RemoteFunction<T> {
648
+ 'worklet';
649
+ return !!value.__remoteFunction;
650
+ }
651
+
652
+ /**
653
+ * We freeze
654
+ *
655
+ * - Arrays,
656
+ * - Remote functions,
657
+ * - Plain JS objects,
658
+ *
659
+ * That are transformed to a serializable with a meaningful warning. This should
660
+ * help detect issues when someone modifies data after it's been converted.
661
+ * Meaning that they may be doing a faulty assumption in their code expecting
662
+ * that the updates are going to automatically propagate to the object sent to
663
+ * the UI thread. If the user really wants some objects to be mutable they
664
+ * should use shared values instead.
665
+ */
666
+ function freezeObjectInDev<T extends object>(value: T) {
667
+ if (!__DEV__) {
668
+ return;
669
+ }
670
+ Object.entries(value).forEach(([key, element]) => {
671
+ const descriptor = Object.getOwnPropertyDescriptor(value, key)!;
672
+ if (!descriptor.configurable) {
673
+ return;
674
+ }
675
+ Object.defineProperty(value, key, {
676
+ get() {
677
+ return element;
678
+ },
679
+ set() {
680
+ logger.warn(
681
+ `Tried to modify key \`${key}\` of an object which has been already passed to a worklet. See
682
+ https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-modify-key-of-an-object-which-has-been-converted-to-a-serializable
683
+ for more details.`
684
+ );
685
+ },
686
+ });
687
+ });
688
+ Object.preventExtensions(value);
689
+ }
690
+
691
+ function makeShareableCloneOnUIRecursiveLEGACY<T>(
692
+ value: T
693
+ ): FlatSerializableRef<T> {
694
+ 'worklet';
695
+ if (SHOULD_BE_USE_WEB) {
696
+ // @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
697
+ // see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
698
+ return value;
699
+ }
700
+ // eslint-disable-next-line @typescript-eslint/no-shadow
701
+ function cloneRecursive(value: T): FlatSerializableRef<T> {
702
+ if (
703
+ (typeof value === 'object' && value !== null) ||
704
+ typeof value === 'function'
705
+ ) {
706
+ if (isHostObject(value)) {
707
+ // We call `_createSerializableClone` to wrap the provided HostObject
708
+ // inside SerializableJSRef.
709
+ return global._createSerializableHostObject(
710
+ value
711
+ ) as FlatSerializableRef<T>;
712
+ }
713
+ if (isRemoteFunction<T>(value)) {
714
+ // RemoteFunctions are created by us therefore they are
715
+ // a Serializable out of the box and there is no need to
716
+ // call `_createSerializableClone`.
717
+ return value.__remoteFunction;
718
+ }
719
+ if (Array.isArray(value)) {
720
+ return global._createSerializableArray(
721
+ value.map(cloneRecursive)
722
+ ) as FlatSerializableRef<T>;
723
+ }
724
+ if ((value as Record<string, unknown>).__synchronizableRef) {
725
+ return global._createSerializableSynchronizable(
726
+ value
727
+ ) as FlatSerializableRef<T>;
728
+ }
729
+ const toAdapt: Record<string, FlatSerializableRef<T>> = {};
730
+ for (const [key, element] of Object.entries(value)) {
731
+ toAdapt[key] = cloneRecursive(element);
732
+ }
733
+ return global._createSerializable(
734
+ toAdapt,
735
+ value
736
+ ) as FlatSerializableRef<T>;
737
+ }
738
+
739
+ if (typeof value === 'string') {
740
+ return global._createSerializableString(value);
741
+ }
742
+
743
+ if (typeof value === 'number') {
744
+ return global._createSerializableNumber(value);
745
+ }
746
+
747
+ if (typeof value === 'boolean') {
748
+ return global._createSerializableBoolean(value);
749
+ }
750
+
751
+ if (typeof value === 'bigint') {
752
+ return global._createSerializableBigInt(value);
753
+ }
754
+
755
+ if (value === undefined) {
756
+ return global._createSerializableUndefined();
757
+ }
758
+
759
+ if (value === null) {
760
+ return global._createSerializableNull();
761
+ }
762
+
763
+ return global._createSerializable(value, undefined);
764
+ }
765
+ return cloneRecursive(value);
766
+ }
767
+
768
+ /** @deprecated This function is no longer supported. */
769
+ export const makeShareableCloneOnUIRecursive = (
770
+ globalThis._WORKLETS_BUNDLE_MODE
771
+ ? createSerializable
772
+ : makeShareableCloneOnUIRecursiveLEGACY
773
+ ) as typeof makeShareableCloneOnUIRecursiveLEGACY;
774
+
775
+ function makeShareableJS<T>(value: T): T {
776
+ return value;
777
+ }
778
+
779
+ function makeShareableNative<T extends object>(value: T): T {
780
+ if (serializableMappingCache.get(value)) {
781
+ return value;
782
+ }
783
+ const handle = createSerializable({
784
+ __init: () => {
785
+ 'worklet';
786
+ return value;
787
+ },
788
+ });
789
+ serializableMappingCache.set(value, handle);
790
+ return value;
791
+ }
792
+
793
+ /**
794
+ * This function creates a value on UI with persistent state - changes to it on
795
+ * the UI thread will be seen by all worklets. Use it when you want to create a
796
+ * value that is read and written only on the UI thread.
797
+ */
798
+ /** @deprecated This function is no longer supported. */
799
+ export const makeShareable = SHOULD_BE_USE_WEB
800
+ ? makeShareableJS
801
+ : makeShareableNative;