@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,112 @@
1
+ #include <worklets/RunLoop/EventLoop.h>
2
+
3
+ #include <thread>
4
+ #include <utility>
5
+ #include <vector>
6
+
7
+ namespace worklets {
8
+
9
+ EventLoop::EventLoop(
10
+ const std::string &name,
11
+ const std::shared_ptr<jsi::Runtime> runtime,
12
+ const std::shared_ptr<AsyncQueue> &queue)
13
+ : runtime_(runtime),
14
+ queue_(queue),
15
+ timeoutsQueueState_(std::make_shared<TimeoutsQueueState>()),
16
+ name_(name) {}
17
+
18
+ EventLoop::~EventLoop() {
19
+ {
20
+ std::unique_lock<std::mutex> lock(timeoutsQueueState_->mutex);
21
+ timeoutsQueueState_->running = false;
22
+ timeoutsQueueState_->queue = {};
23
+ }
24
+ timeoutsQueueState_->cv.notify_all();
25
+ }
26
+
27
+ void EventLoop::run() {
28
+ const auto threadName = name_ + "(timeout)";
29
+ auto thread = std::thread(
30
+ [threadName, state = timeoutsQueueState_, weakThis = weak_from_this()] {
31
+ // amzn_mod - include check for VOLTA_DARWIN
32
+ #if __APPLE__ || defined(VOLTA_DARWIN)
33
+ pthread_setname_np(threadName.c_str());
34
+ #endif // __APPLE__ || VOLTA_DARWIN
35
+ while (state->running) {
36
+ std::unique_lock<std::mutex> lock(state->mutex);
37
+
38
+ if (state->queue.empty()) {
39
+ state->cv.wait(lock);
40
+ } else {
41
+ const auto &nextTimeout = state->queue[0];
42
+ const auto timeToWait =
43
+ nextTimeout.targetTime - getCurrentTimeInMs();
44
+ state->cv.wait_for(lock, std::chrono::milliseconds(timeToWait));
45
+ }
46
+
47
+ // Early return if the Event Loop got destroyed already.
48
+ if (!state->running) {
49
+ return;
50
+ }
51
+
52
+ const auto currentTime = getCurrentTimeInMs();
53
+ std::vector<std::function<void(jsi::Runtime & rt)>> jobs;
54
+ auto &timeouts = state->queue;
55
+ timeouts.erase(
56
+ std::remove_if(
57
+ timeouts.begin(),
58
+ timeouts.end(),
59
+ [currentTime, &jobs](const Timeout &timeout) {
60
+ if (currentTime >= timeout.targetTime) {
61
+ jobs.emplace_back(timeout.callback);
62
+ return true;
63
+ }
64
+ return false;
65
+ }),
66
+ timeouts.end());
67
+ lock.unlock();
68
+
69
+ if (auto strongThis = weakThis.lock()) {
70
+ for (auto &job : jobs) {
71
+ strongThis->pushTask(std::move(job));
72
+ }
73
+ }
74
+ }
75
+ });
76
+ // amzn_mod - exclude VOLTA_DARWIN
77
+ #if defined(ANDROID) && not defined(VOLTA_DARWIN)
78
+ pthread_setname_np(thread.native_handle(), threadName.c_str());
79
+ #endif // ANDROID && !VOLTA_DARWIN
80
+ thread.detach();
81
+ }
82
+
83
+ void EventLoop::pushTask(std::function<void(jsi::Runtime &rt)> &&job) {
84
+ queue_->push([weakRuntime = std::weak_ptr<jsi::Runtime>{runtime_},
85
+ job = std::move(job)] {
86
+ if (auto runtime = weakRuntime.lock()) {
87
+ job(*runtime);
88
+ }
89
+ });
90
+ }
91
+
92
+ void EventLoop::pushTimeout(
93
+ std::function<void(jsi::Runtime &rt)> &&job,
94
+ int64_t delay) {
95
+ {
96
+ std::unique_lock<std::mutex> lock(timeoutsQueueState_->mutex);
97
+ const auto targetTime = getCurrentTimeInMs() + delay;
98
+ const auto timeout = Timeout{std::move(job), targetTime};
99
+ auto &queue = timeoutsQueueState_->queue;
100
+ auto it = std::upper_bound(queue.begin(), queue.end(), timeout);
101
+ queue.insert(it, timeout);
102
+ }
103
+ timeoutsQueueState_->cv.notify_one();
104
+ }
105
+
106
+ int64_t EventLoop::getCurrentTimeInMs() {
107
+ const auto currentTime = std::chrono::system_clock::now().time_since_epoch();
108
+ return std::chrono::duration_cast<std::chrono::milliseconds>(currentTime)
109
+ .count();
110
+ }
111
+
112
+ } // namespace worklets
@@ -0,0 +1,52 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+ #include <worklets/Public/AsyncQueue.h>
5
+
6
+ #include <atomic>
7
+ #include <condition_variable>
8
+ #include <memory>
9
+ #include <queue>
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ using namespace facebook;
14
+
15
+ namespace worklets {
16
+
17
+ struct Timeout {
18
+ std::function<void(jsi::Runtime &rt)> callback;
19
+ int64_t targetTime;
20
+ bool operator<(const Timeout &v) const {
21
+ return targetTime < v.targetTime;
22
+ }
23
+ };
24
+
25
+ struct TimeoutsQueueState {
26
+ std::atomic_bool running{true};
27
+ std::mutex mutex;
28
+ std::condition_variable cv;
29
+ std::vector<Timeout> queue;
30
+ };
31
+
32
+ class EventLoop : public std::enable_shared_from_this<EventLoop> {
33
+ public:
34
+ EventLoop(
35
+ const std::string &name,
36
+ const std::shared_ptr<jsi::Runtime> runtime,
37
+ const std::shared_ptr<AsyncQueue> &queue);
38
+ ~EventLoop();
39
+ void run();
40
+ void pushTask(std::function<void(jsi::Runtime &rt)> &&job);
41
+ void pushTimeout(std::function<void(jsi::Runtime &rt)> &&job, int64_t delay);
42
+
43
+ private:
44
+ const std::shared_ptr<jsi::Runtime> runtime_;
45
+ const std::shared_ptr<AsyncQueue> queue_;
46
+ const std::shared_ptr<TimeoutsQueueState> timeoutsQueueState_;
47
+ const std::string name_;
48
+
49
+ static int64_t getCurrentTimeInMs();
50
+ };
51
+
52
+ } // namespace worklets
@@ -0,0 +1,545 @@
1
+ #include <jsi/jsi.h>
2
+ #include <worklets/SharedItems/Serializable.h>
3
+
4
+ using namespace facebook;
5
+
6
+ namespace worklets {
7
+
8
+ jsi::Function getValueUnpacker(jsi::Runtime &rt) {
9
+ auto valueUnpacker = rt.global().getProperty(rt, "__valueUnpacker");
10
+ react_native_assert(valueUnpacker.isObject() && "valueUnpacker not found");
11
+ return valueUnpacker.asObject(rt).asFunction(rt);
12
+ }
13
+
14
+ #ifndef NDEBUG
15
+
16
+ static const auto callGuardLambda = [](facebook::jsi::Runtime &rt,
17
+ const facebook::jsi::Value &thisVal,
18
+ const facebook::jsi::Value *args,
19
+ size_t count) {
20
+ return args[0].asObject(rt).asFunction(rt).call(rt, args + 1, count - 1);
21
+ };
22
+
23
+ jsi::Function getCallGuard(jsi::Runtime &rt) {
24
+ auto callGuard = rt.global().getProperty(rt, "__callGuardDEV");
25
+ if (callGuard.isObject()) {
26
+ // Use JS implementation if `__callGuardDEV` has already been installed.
27
+ // This is the desired behavior.
28
+ return callGuard.asObject(rt).asFunction(rt);
29
+ }
30
+
31
+ // Otherwise, fallback to C++ JSI implementation. This is necessary so that we
32
+ // can install `__callGuardDEV` itself and should happen only once. Note that
33
+ // the C++ implementation doesn't intercept errors and simply throws them as
34
+ // C++ exceptions which crashes the app. We assume that installing the guard
35
+ // doesn't throw any errors.
36
+ return jsi::Function::createFromHostFunction(
37
+ rt, jsi::PropNameID::forAscii(rt, "callGuard"), 1, callGuardLambda);
38
+ }
39
+
40
+ #endif // NDEBUG
41
+
42
+ jsi::Value makeSerializableClone(
43
+ jsi::Runtime &rt,
44
+ const jsi::Value &value,
45
+ const jsi::Value &shouldRetainRemote,
46
+ const jsi::Value &nativeStateSource) {
47
+ std::shared_ptr<Serializable> serializable;
48
+ if (value.isObject()) {
49
+ auto object = value.asObject(rt);
50
+ if (!object.getProperty(rt, "__workletHash").isUndefined()) {
51
+ // We pass `false` because this function is invoked only
52
+ // by `makeSerializableCloneOnUIRecursive` which doesn't
53
+ // make Retaining Serializables.
54
+ return makeSerializableWorklet(rt, object, false);
55
+ } else if (!object.getProperty(rt, "__init").isUndefined()) {
56
+ return makeSerializableInitializer(rt, object);
57
+ } else if (object.isFunction(rt)) {
58
+ return makeSerializableFunction(rt, object.asFunction(rt));
59
+ } else if (object.isArray(rt)) {
60
+ if (shouldRetainRemote.isBool() && shouldRetainRemote.getBool()) {
61
+ serializable =
62
+ std::make_shared<RetainingSerializable<SerializableArray>>(
63
+ rt, object.asArray(rt));
64
+ } else {
65
+ serializable =
66
+ std::make_shared<SerializableArray>(rt, object.asArray(rt));
67
+ }
68
+ } else if (object.isArrayBuffer(rt)) {
69
+ serializable = std::make_shared<SerializableArrayBuffer>(
70
+ rt, object.getArrayBuffer(rt));
71
+ } else if (object.isHostObject(rt)) {
72
+ if (object.isHostObject<SerializableJSRef>(rt)) {
73
+ return object;
74
+ }
75
+ serializable = std::make_shared<SerializableHostObject>(
76
+ rt, object.getHostObject(rt));
77
+ } else {
78
+ if (shouldRetainRemote.isBool() && shouldRetainRemote.getBool()) {
79
+ serializable =
80
+ std::make_shared<RetainingSerializable<SerializableObject>>(
81
+ rt, object, nativeStateSource);
82
+ } else {
83
+ serializable =
84
+ std::make_shared<SerializableObject>(rt, object, nativeStateSource);
85
+ }
86
+ }
87
+ } else if (value.isString()) {
88
+ serializable =
89
+ std::make_shared<SerializableString>(value.asString(rt).utf8(rt));
90
+ } else if (value.isUndefined()) {
91
+ serializable = std::make_shared<SerializableScalar>();
92
+ } else if (value.isNull()) {
93
+ serializable = std::make_shared<SerializableScalar>(nullptr);
94
+ } else if (value.isBool()) {
95
+ serializable = std::make_shared<SerializableScalar>(value.getBool());
96
+ } else if (value.isNumber()) {
97
+ serializable = std::make_shared<SerializableScalar>(value.getNumber());
98
+ } else if (value.isBigInt()) {
99
+ serializable =
100
+ std::make_shared<SerializableBigInt>(rt, value.getBigInt(rt));
101
+ } else if (value.isSymbol()) {
102
+ // TODO: this is only a placeholder implementation, here we replace symbols
103
+ // with strings in order to make certain objects to be captured. There isn't
104
+ // yet any usecase for using symbols on the UI runtime so it is fine to keep
105
+ // it like this for now.
106
+ serializable =
107
+ std::make_shared<SerializableString>(value.getSymbol(rt).toString(rt));
108
+ } else {
109
+ throw std::runtime_error(
110
+ "[Worklets] Attempted to convert an unsupported value type.");
111
+ }
112
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
113
+ }
114
+
115
+ jsi::Value makeSerializableString(jsi::Runtime &rt, const jsi::String &string) {
116
+ const auto serializable =
117
+ std::make_shared<SerializableString>(string.utf8(rt));
118
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
119
+ }
120
+
121
+ jsi::Value makeSerializableNumber(jsi::Runtime &rt, double number) {
122
+ const auto serializable = std::make_shared<SerializableScalar>(number);
123
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
124
+ }
125
+
126
+ jsi::Value makeSerializableBoolean(jsi::Runtime &rt, bool boolean) {
127
+ const auto serializable = std::make_shared<SerializableScalar>(boolean);
128
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
129
+ }
130
+
131
+ jsi::Value makeSerializableBigInt(jsi::Runtime &rt, const jsi::BigInt &bigint) {
132
+ const auto serializable = std::make_shared<SerializableBigInt>(rt, bigint);
133
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
134
+ }
135
+
136
+ jsi::Value makeSerializableUndefined(jsi::Runtime &rt) {
137
+ const auto serializable = std::make_shared<SerializableScalar>();
138
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
139
+ }
140
+
141
+ jsi::Value makeSerializableNull(jsi::Runtime &rt) {
142
+ const auto serializable = std::make_shared<SerializableScalar>(nullptr);
143
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
144
+ }
145
+
146
+ jsi::Value makeSerializableWorklet(
147
+ jsi::Runtime &rt,
148
+ const jsi::Object &object,
149
+ const bool &shouldRetainRemote) {
150
+ std::shared_ptr<Serializable> serializable;
151
+ if (shouldRetainRemote) {
152
+ serializable = std::make_shared<RetainingSerializable<SerializableWorklet>>(
153
+ rt, object);
154
+ } else {
155
+ serializable = std::make_shared<SerializableWorklet>(rt, object);
156
+ }
157
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
158
+ }
159
+
160
+ jsi::Value makeSerializableInitializer(
161
+ jsi::Runtime &rt,
162
+ const jsi::Object &initializerObject) {
163
+ const auto serializable =
164
+ std::make_shared<SerializableInitializer>(rt, initializerObject);
165
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
166
+ }
167
+
168
+ jsi::Value makeSerializableFunction(jsi::Runtime &rt, jsi::Function function) {
169
+ std::shared_ptr<Serializable> serializable;
170
+ if (function.isHostFunction(rt)) {
171
+ serializable =
172
+ std::make_shared<SerializableHostFunction>(rt, std::move(function));
173
+ } else {
174
+ serializable =
175
+ std::make_shared<SerializableRemoteFunction>(rt, std::move(function));
176
+ }
177
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
178
+ }
179
+
180
+ jsi::Value makeSerializableArray(
181
+ jsi::Runtime &rt,
182
+ const jsi::Array &array,
183
+ const jsi::Value &shouldRetainRemote) {
184
+ std::shared_ptr<Serializable> serializable;
185
+ if (shouldRetainRemote.isBool() && shouldRetainRemote.getBool()) {
186
+ serializable =
187
+ std::make_shared<RetainingSerializable<SerializableArray>>(rt, array);
188
+ } else {
189
+ serializable = std::make_shared<SerializableArray>(rt, array);
190
+ }
191
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
192
+ }
193
+
194
+ jsi::Value makeSerializableMap(
195
+ jsi::Runtime &rt,
196
+ const jsi::Array &keys,
197
+ const jsi::Array &values) {
198
+ auto serializable = std::make_shared<SerializableMap>(rt, keys, values);
199
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
200
+ }
201
+
202
+ jsi::Value makeSerializableSet(jsi::Runtime &rt, const jsi::Array &values) {
203
+ auto serializable = std::make_shared<SerializableSet>(rt, values);
204
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
205
+ }
206
+
207
+ jsi::Value makeSerializableHostObject(
208
+ jsi::Runtime &rt,
209
+ const std::shared_ptr<jsi::HostObject> &value) {
210
+ const auto serializable = std::make_shared<SerializableHostObject>(rt, value);
211
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
212
+ }
213
+
214
+ jsi::Value makeSerializableTurboModuleLike(
215
+ jsi::Runtime &rt,
216
+ const jsi::Object &object,
217
+ const std::shared_ptr<jsi::HostObject> &proto) {
218
+ const auto serializable =
219
+ std::make_shared<SerializableTurboModuleLike>(rt, object, proto);
220
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
221
+ }
222
+
223
+ jsi::Value makeSerializableImport(
224
+ jsi::Runtime &rt,
225
+ const double source,
226
+ const jsi::String &imported) {
227
+ auto serializable =
228
+ std::make_shared<SerializableImport>(rt, source, imported);
229
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
230
+ }
231
+
232
+ jsi::Value makeSerializableObject(
233
+ jsi::Runtime &rt,
234
+ jsi::Object object,
235
+ bool shouldRetainRemote,
236
+ const jsi::Value &nativeStateSource) {
237
+ std::shared_ptr<Serializable> serializable;
238
+ if (shouldRetainRemote) {
239
+ serializable = std::make_shared<RetainingSerializable<SerializableObject>>(
240
+ rt, object, nativeStateSource);
241
+ } else {
242
+ serializable =
243
+ std::make_shared<SerializableObject>(rt, object, nativeStateSource);
244
+ }
245
+ return SerializableJSRef::newNativeStateObject(rt, serializable);
246
+ }
247
+
248
+ std::shared_ptr<Serializable> extractSerializableOrThrow(
249
+ jsi::Runtime &rt,
250
+ const jsi::Value &maybeSerializableValue,
251
+ const std::string &errorMessage) {
252
+ if (maybeSerializableValue.isObject()) {
253
+ auto object = maybeSerializableValue.asObject(rt);
254
+ if (object.hasNativeState(rt)) {
255
+ auto nativeState = object.getNativeState(rt);
256
+ return std::dynamic_pointer_cast<SerializableJSRef>(nativeState)->value();
257
+ }
258
+ throw std::runtime_error(
259
+ "[Worklets] Attempted to extract from a Object that wasn't converted to a Serializable.");
260
+ } else if (maybeSerializableValue.isUndefined()) {
261
+ return Serializable::undefined();
262
+ }
263
+ throw std::runtime_error(errorMessage);
264
+ }
265
+
266
+ Serializable::~Serializable() {}
267
+
268
+ std::shared_ptr<Serializable> Serializable::undefined() {
269
+ static auto undefined = std::make_shared<SerializableScalar>();
270
+ return undefined;
271
+ }
272
+
273
+ template <typename BaseClass>
274
+ jsi::Value RetainingSerializable<BaseClass>::toJSValue(jsi::Runtime &rt) {
275
+ if (&rt == primaryRuntime_) {
276
+ // TODO: it is suboptimal to generate new object every time getJS is
277
+ // called on host runtime – the objects we are generating already exists
278
+ // and we should possibly just grab a hold of such object and use it here
279
+ // instead of creating a new JS representation. As far as I understand the
280
+ // only case where it can be realistically called this way is when a
281
+ // shared value is created and then accessed on the same runtime
282
+ return BaseClass::toJSValue(rt);
283
+ }
284
+ if (secondaryValue_ == nullptr) {
285
+ auto value = BaseClass::toJSValue(rt);
286
+ secondaryValue_ = std::make_unique<jsi::Value>(rt, value);
287
+ secondaryRuntime_ = &rt;
288
+ return value;
289
+ }
290
+ if (&rt == secondaryRuntime_) {
291
+ return jsi::Value(rt, *secondaryValue_);
292
+ }
293
+ return BaseClass::toJSValue(rt);
294
+ }
295
+
296
+ SerializableJSRef::~SerializableJSRef() {}
297
+
298
+ SerializableArray::SerializableArray(jsi::Runtime &rt, const jsi::Array &array)
299
+ : Serializable(ArrayType) {
300
+ auto size = array.size(rt);
301
+ data_.reserve(size);
302
+ for (size_t i = 0; i < size; i++) {
303
+ data_.push_back(
304
+ extractSerializableOrThrow(rt, array.getValueAtIndex(rt, i)));
305
+ }
306
+ }
307
+
308
+ jsi::Value SerializableArray::toJSValue(jsi::Runtime &rt) {
309
+ auto size = data_.size();
310
+ auto ary = jsi::Array(rt, size);
311
+ for (size_t i = 0; i < size; i++) {
312
+ ary.setValueAtIndex(rt, i, data_[i]->toJSValue(rt));
313
+ }
314
+ return ary;
315
+ }
316
+
317
+ jsi::Value SerializableArrayBuffer::toJSValue(jsi::Runtime &rt) {
318
+ auto size = static_cast<int>(data_.size());
319
+ auto arrayBuffer = rt.global()
320
+ .getPropertyAsFunction(rt, "ArrayBuffer")
321
+ .callAsConstructor(rt, size)
322
+ .getObject(rt)
323
+ .getArrayBuffer(rt);
324
+ memcpy(arrayBuffer.data(rt), data_.data(), size);
325
+ return arrayBuffer;
326
+ }
327
+
328
+ SerializableObject::SerializableObject(
329
+ jsi::Runtime &rt,
330
+ const jsi::Object &object)
331
+ : Serializable(ObjectType) {
332
+ auto propertyNames = object.getPropertyNames(rt);
333
+ auto size = propertyNames.size(rt);
334
+ data_.reserve(size);
335
+ for (size_t i = 0; i < size; i++) {
336
+ auto key = propertyNames.getValueAtIndex(rt, i).asString(rt);
337
+ auto value = extractSerializableOrThrow(rt, object.getProperty(rt, key));
338
+ data_.emplace_back(key.utf8(rt), value);
339
+ }
340
+ if (object.hasNativeState(rt)) {
341
+ nativeState_ = object.getNativeState(rt);
342
+ }
343
+ }
344
+
345
+ SerializableObject::SerializableObject(
346
+ jsi::Runtime &rt,
347
+ const jsi::Object &object,
348
+ const jsi::Value &nativeStateSource)
349
+ : SerializableObject(rt, object) {
350
+ if (nativeStateSource.isObject() &&
351
+ nativeStateSource.asObject(rt).hasNativeState(rt)) {
352
+ nativeState_ = nativeStateSource.asObject(rt).getNativeState(rt);
353
+ }
354
+ }
355
+
356
+ jsi::Value SerializableObject::toJSValue(jsi::Runtime &rt) {
357
+ auto obj = jsi::Object(rt);
358
+ for (size_t i = 0, size = data_.size(); i < size; i++) {
359
+ obj.setProperty(
360
+ rt,
361
+ jsi::String::createFromUtf8(rt, data_[i].first),
362
+ data_[i].second->toJSValue(rt));
363
+ }
364
+ if (nativeState_ != nullptr) {
365
+ obj.setNativeState(rt, nativeState_);
366
+ }
367
+ return obj;
368
+ }
369
+
370
+ SerializableMap::SerializableMap(
371
+ jsi::Runtime &rt,
372
+ const jsi::Array &keys,
373
+ const jsi::Array &values)
374
+ : Serializable(MapType) {
375
+ auto size = keys.size(rt);
376
+ react_native_assert(
377
+ size == values.size(rt) &&
378
+ "Keys and values arrays must have the same size.");
379
+ data_.reserve(size);
380
+ for (size_t i = 0; i < size; i++) {
381
+ auto key = extractSerializableOrThrow(rt, keys.getValueAtIndex(rt, i));
382
+ auto value = extractSerializableOrThrow(rt, values.getValueAtIndex(rt, i));
383
+ data_.emplace_back(key, value);
384
+ }
385
+ }
386
+
387
+ jsi::Value SerializableMap::toJSValue(jsi::Runtime &rt) {
388
+ const auto keyValues = jsi::Array(rt, data_.size());
389
+ for (size_t i = 0, size = data_.size(); i < size; i++) {
390
+ const auto pair = jsi::Array(rt, 2);
391
+ pair.setValueAtIndex(rt, 0, data_[i].first->toJSValue(rt));
392
+ pair.setValueAtIndex(rt, 1, data_[i].second->toJSValue(rt));
393
+ keyValues.setValueAtIndex(rt, i, std::move(pair));
394
+ }
395
+
396
+ const auto &global = rt.global();
397
+ auto map = global.getPropertyAsFunction(rt, "Map").callAsConstructor(
398
+ rt, std::move(keyValues));
399
+
400
+ return map;
401
+ }
402
+
403
+ SerializableSet::SerializableSet(jsi::Runtime &rt, const jsi::Array &values)
404
+ : Serializable(SetType) {
405
+ auto size = values.size(rt);
406
+ data_.reserve(size);
407
+ for (size_t i = 0; i < size; i++) {
408
+ data_.push_back(
409
+ extractSerializableOrThrow(rt, values.getValueAtIndex(rt, i)));
410
+ }
411
+ }
412
+
413
+ jsi::Value SerializableSet::toJSValue(jsi::Runtime &rt) {
414
+ const auto values = jsi::Array(rt, data_.size());
415
+ for (size_t i = 0, size = data_.size(); i < size; i++) {
416
+ values.setValueAtIndex(rt, i, data_[i]->toJSValue(rt));
417
+ }
418
+
419
+ const auto &global = rt.global();
420
+ auto set = global.getPropertyAsFunction(rt, "Set").callAsConstructor(
421
+ rt, std::move(values));
422
+
423
+ return set;
424
+ }
425
+
426
+ jsi::Value SerializableHostObject::toJSValue(jsi::Runtime &rt) {
427
+ return jsi::Object::createFromHostObject(rt, hostObject_);
428
+ }
429
+
430
+ jsi::Value SerializableHostFunction::toJSValue(jsi::Runtime &rt) {
431
+ return jsi::Function::createFromHostFunction(
432
+ rt, jsi::PropNameID::forUtf8(rt, name_), paramCount_, hostFunction_);
433
+ }
434
+
435
+ jsi::Value SerializableWorklet::toJSValue(jsi::Runtime &rt) {
436
+ react_native_assert(
437
+ std::any_of(
438
+ data_.cbegin(),
439
+ data_.cend(),
440
+ [](const auto &item) { return item.first == "__workletHash"; }) &&
441
+ "SerializableWorklet doesn't have `__workletHash` property");
442
+ jsi::Value obj = SerializableObject::toJSValue(rt);
443
+ return getValueUnpacker(rt).call(
444
+ rt, obj, jsi::String::createFromAscii(rt, "Worklet"));
445
+ }
446
+
447
+ jsi::Value SerializableImport::toJSValue(jsi::Runtime &rt) {
448
+ /**
449
+ * The only way to obtain a module in runtime is to use the Metro's require
450
+ * method implementation, which is injected into the global object as `__r`.
451
+ */
452
+ const auto metroRequire = rt.global().getProperty(rt, "__r");
453
+ if (metroRequire.isUndefined()) {
454
+ return jsi::Value::undefined();
455
+ }
456
+
457
+ const auto imported = jsi::String::createFromUtf8(rt, imported_);
458
+ return metroRequire.asObject(rt)
459
+ .asFunction(rt)
460
+ .call(rt, source_)
461
+ .asObject(rt)
462
+ .getProperty(rt, imported);
463
+ }
464
+
465
+ jsi::Value SerializableRemoteFunction::toJSValue(jsi::Runtime &rt) {
466
+ if (&rt == runtime_) {
467
+ return jsi::Value(rt, *function_);
468
+ } else {
469
+ #ifndef NDEBUG
470
+ return getValueUnpacker(rt).call(
471
+ rt,
472
+ SerializableJSRef::newNativeStateObject(rt, shared_from_this()),
473
+ jsi::String::createFromAscii(rt, "RemoteFunction"),
474
+ jsi::String::createFromUtf8(rt, name_));
475
+ #else
476
+ return SerializableJSRef::newNativeStateObject(rt, shared_from_this());
477
+ #endif
478
+ }
479
+ }
480
+
481
+ jsi::Value SerializableInitializer::toJSValue(jsi::Runtime &rt) {
482
+ if (remoteValue_ == nullptr) {
483
+ auto initObj = initializer_->toJSValue(rt);
484
+ auto value = std::make_unique<jsi::Value>(getValueUnpacker(rt).call(
485
+ rt, initObj, jsi::String::createFromAscii(rt, "Handle")));
486
+
487
+ // We are locking the initialization here since the thread that is
488
+ // initalizing can be pre-empted on runtime lock. E.g.
489
+ // UI thread can be pre-empted on initialization of a shared value and then
490
+ // JS thread can try to access the shared value, locking the whole runtime.
491
+ // If we put the lock on `getValueUnpacker` part (basically any part that
492
+ // requires runtime) we would get a deadlock since UI thread would never
493
+ // release it.
494
+ std::unique_lock<std::mutex> lock(initializationMutex_);
495
+ if (remoteValue_ == nullptr) {
496
+ remoteValue_ = std::move(value);
497
+ remoteRuntime_ = &rt;
498
+ }
499
+ }
500
+ if (&rt == remoteRuntime_) {
501
+ return jsi::Value(rt, *remoteValue_);
502
+ }
503
+ auto initObj = initializer_->toJSValue(rt);
504
+ return getValueUnpacker(rt).call(
505
+ rt, initObj, jsi::String::createFromAscii(rt, "Handle"));
506
+ }
507
+
508
+ jsi::Value SerializableString::toJSValue(jsi::Runtime &rt) {
509
+ return jsi::String::createFromUtf8(rt, data_);
510
+ }
511
+
512
+ jsi::Value SerializableBigInt::toJSValue(jsi::Runtime &rt) {
513
+ return rt.global()
514
+ .getPropertyAsFunction(rt, "BigInt")
515
+ .call(rt, jsi::String::createFromUtf8(rt, string_));
516
+ }
517
+
518
+ jsi::Value SerializableScalar::toJSValue(jsi::Runtime &) {
519
+ switch (valueType_) {
520
+ case Serializable::UndefinedType:
521
+ return jsi::Value();
522
+ case Serializable::NullType:
523
+ return jsi::Value(nullptr);
524
+ case Serializable::BooleanType:
525
+ return jsi::Value(data_.boolean);
526
+ case Serializable::NumberType:
527
+ return jsi::Value(data_.number);
528
+ default:
529
+ throw std::runtime_error(
530
+ "[Worklets] Attempted to convert object that's not of a scalar type.");
531
+ }
532
+ }
533
+
534
+ jsi::Value SerializableTurboModuleLike::toJSValue(jsi::Runtime &rt) {
535
+ auto obj = properties_->toJSValue(rt).asObject(rt);
536
+ const auto prototype = proto_->toJSValue(rt);
537
+ rt.global()
538
+ .getPropertyAsObject(rt, "Object")
539
+ .getPropertyAsFunction(rt, "setPrototypeOf")
540
+ .call(rt, obj, prototype);
541
+
542
+ return obj;
543
+ }
544
+
545
+ } // namespace worklets