@lynx-js/react 0.126.2 → 0.128.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (425) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/docs/directives.md +90 -0
  4. package/docs/global-events.md +50 -0
  5. package/docs/import-attributes.md +43 -0
  6. package/docs/macros.md +285 -0
  7. package/package.json +7 -14
  8. package/refresh/.turbo/turbo-build.log +2 -2
  9. package/refresh/dist/index.js +26 -6
  10. package/refresh/package.json +1 -1
  11. package/runtime/__test__/core/main-thread-object.test.js +149 -0
  12. package/runtime/__test__/element-template/fixtures/background/main-thread-function/run-on-main-thread/index.tsx +26 -3
  13. package/runtime/__test__/element-template/imports/main-thread-object-api.test.ts +10 -0
  14. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +4 -1
  15. package/runtime/__test__/element-template/runtime/background/event/compiled-fixtures.test.tsx +39 -48
  16. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +11 -9
  17. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +6 -3
  18. package/runtime/__test__/element-template/test-utils/mock/globals.js +6 -0
  19. package/runtime/__test__/element-template/vitest.config.ts +4 -0
  20. package/runtime/__test__/snapshot/ctxNotFound.test.ts +271 -0
  21. package/runtime/__test__/snapshot/event.test.jsx +7 -7
  22. package/runtime/__test__/snapshot/lazy.test.js +73 -1
  23. package/runtime/__test__/snapshot/lynx/portals.test.jsx +75 -37
  24. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +59 -47
  25. package/runtime/__test__/snapshot/spread.test.jsx +10 -10
  26. package/runtime/__test__/snapshot/worklet/main-thread-object.test.jsx +306 -0
  27. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +1 -0
  28. package/runtime/__test__/worklet-runtime/initEntry.test.js +109 -0
  29. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +182 -0
  30. package/runtime/__test__/worklet-runtime/object-methods.test.js +59 -0
  31. package/runtime/__test__/worklet-runtime/workletRef.test.js +362 -2
  32. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +246 -1
  33. package/runtime/lazy/compat.js +2 -0
  34. package/runtime/lazy/element-template-internal.js +1 -1
  35. package/runtime/lazy/element-template.js +2 -0
  36. package/runtime/lazy/internal.js +2 -1
  37. package/runtime/lazy/react.js +2 -0
  38. package/runtime/lib/core/background-function/exec-map.js.map +1 -1
  39. package/runtime/lib/core/background-function/handle.js.map +1 -1
  40. package/runtime/lib/core/background-function/run-on-background.d.ts +16 -0
  41. package/runtime/lib/core/background-function/run-on-background.js +16 -0
  42. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  43. package/runtime/lib/core/capture-main-thread-object.d.ts +1 -0
  44. package/runtime/lib/core/capture-main-thread-object.js +16 -0
  45. package/runtime/lib/core/capture-main-thread-object.js.map +1 -0
  46. package/runtime/lib/core/commit-context.js.map +1 -1
  47. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  48. package/runtime/lib/core/compat/lynxComponent.js.map +1 -1
  49. package/runtime/lib/core/component.js.map +1 -1
  50. package/runtime/lib/core/forceRootRender.js.map +1 -1
  51. package/runtime/lib/core/globalProps.d.ts +2 -0
  52. package/runtime/lib/core/globalProps.js.map +1 -1
  53. package/runtime/lib/core/hooks/mainThread.js.map +1 -1
  54. package/runtime/lib/core/hooks/mainThreadImpl.js.map +1 -1
  55. package/runtime/lib/core/hooks/react.d.ts +4 -0
  56. package/runtime/lib/core/hooks/react.js +4 -0
  57. package/runtime/lib/core/hooks/react.js.map +1 -1
  58. package/runtime/lib/core/hooks/useLynxGlobalEventListener.d.ts +2 -0
  59. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js +2 -0
  60. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js.map +1 -1
  61. package/runtime/lib/core/initData.d.ts +2 -0
  62. package/runtime/lib/core/initData.js +2 -0
  63. package/runtime/lib/core/initData.js.map +1 -1
  64. package/runtime/lib/core/lynx/dynamic-import.js.map +1 -1
  65. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  66. package/runtime/lib/core/lynx/runtime-backend.js.map +1 -1
  67. package/runtime/lib/core/lynx/runtime-version.js.map +1 -1
  68. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  69. package/runtime/lib/core/lynx-page-data.js.map +1 -1
  70. package/runtime/lib/core/lynx-update-data.js.map +1 -1
  71. package/runtime/lib/core/main-thread-function.js.map +1 -1
  72. package/runtime/lib/core/main-thread-object-handle-registry.d.ts +3 -0
  73. package/runtime/lib/core/main-thread-object-handle-registry.js +17 -0
  74. package/runtime/lib/core/main-thread-object-handle-registry.js.map +1 -0
  75. package/runtime/lib/core/main-thread-object.d.ts +77 -0
  76. package/runtime/lib/core/main-thread-object.js +258 -0
  77. package/runtime/lib/core/main-thread-object.js.map +1 -0
  78. package/runtime/lib/core/main-thread-ref-id.d.ts +1 -0
  79. package/runtime/lib/core/main-thread-ref-id.js +14 -0
  80. package/runtime/lib/core/main-thread-ref-id.js.map +1 -0
  81. package/runtime/lib/core/main-thread-ref-init-value.d.ts +1 -1
  82. package/runtime/lib/core/main-thread-ref-init-value.js +4 -2
  83. package/runtime/lib/core/main-thread-ref-init-value.js.map +1 -1
  84. package/runtime/lib/core/main-thread-ref.d.ts +4 -0
  85. package/runtime/lib/core/main-thread-ref.js +5 -8
  86. package/runtime/lib/core/main-thread-ref.js.map +1 -1
  87. package/runtime/lib/core/mts-capability.js.map +1 -1
  88. package/runtime/lib/core/performance.js.map +1 -1
  89. package/runtime/lib/core/ref.js.map +1 -1
  90. package/runtime/lib/core/reload-version.js.map +1 -1
  91. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  92. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  93. package/runtime/lib/core/thread-function-call/return-value.js.map +1 -1
  94. package/runtime/lib/document.js.map +1 -1
  95. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  96. package/runtime/lib/element-template/background/commit-context.js.map +1 -1
  97. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  98. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  99. package/runtime/lib/element-template/background/document.js.map +1 -1
  100. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  101. package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
  102. package/runtime/lib/element-template/background/instance.js.map +1 -1
  103. package/runtime/lib/element-template/background/manager.js.map +1 -1
  104. package/runtime/lib/element-template/background/page-root-hydrate.js.map +1 -1
  105. package/runtime/lib/element-template/background/render-scope.js.map +1 -1
  106. package/runtime/lib/element-template/client/root.js.map +1 -1
  107. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  108. package/runtime/lib/element-template/debug/elementPAPICall.js.map +1 -1
  109. package/runtime/lib/element-template/debug/profile.js.map +1 -1
  110. package/runtime/lib/element-template/hydration-map.js.map +1 -1
  111. package/runtime/lib/element-template/index.d.ts +2 -0
  112. package/runtime/lib/element-template/index.js +1 -0
  113. package/runtime/lib/element-template/index.js.map +1 -1
  114. package/runtime/lib/element-template/internal.d.ts +1 -1
  115. package/runtime/lib/element-template/internal.js +1 -1
  116. package/runtime/lib/element-template/internal.js.map +1 -1
  117. package/runtime/lib/element-template/jsx-dev-runtime/index.js.map +1 -1
  118. package/runtime/lib/element-template/jsx-runtime/index.js.map +1 -1
  119. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  120. package/runtime/lib/element-template/lynx/performance.js.map +1 -1
  121. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  122. package/runtime/lib/element-template/native/index.js.map +1 -1
  123. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  124. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  125. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  126. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  127. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  128. package/runtime/lib/element-template/prop-adapters/event-value.js.map +1 -1
  129. package/runtime/lib/element-template/prop-adapters/event.js.map +1 -1
  130. package/runtime/lib/element-template/prop-adapters/ref.js.map +1 -1
  131. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  132. package/runtime/lib/element-template/protocol/lifecycle-constant.js.map +1 -1
  133. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  134. package/runtime/lib/element-template/protocol/page.js.map +1 -1
  135. package/runtime/lib/element-template/protocol/template-type.js.map +1 -1
  136. package/runtime/lib/element-template/protocol/types.d.ts +1 -1
  137. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  138. package/runtime/lib/element-template/protocol/update-event.js.map +1 -1
  139. package/runtime/lib/element-template/runtime/list/list.js.map +1 -1
  140. package/runtime/lib/element-template/runtime/page/authored-page.js.map +1 -1
  141. package/runtime/lib/element-template/runtime/page/page.js.map +1 -1
  142. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  143. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  144. package/runtime/lib/element-template/runtime/render/create-rendered-host.js.map +1 -1
  145. package/runtime/lib/element-template/runtime/render/host.js.map +1 -1
  146. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  147. package/runtime/lib/element-template/runtime/render/render-direct.js.map +1 -1
  148. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  149. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  150. package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
  151. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  152. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  153. package/runtime/lib/element-template/runtime/template/main-thread-event-ctx.js.map +1 -1
  154. package/runtime/lib/element-template/runtime/template/main-thread-function.js.map +1 -1
  155. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  156. package/runtime/lib/element-template/runtime/template/registry.js.map +1 -1
  157. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  158. package/runtime/lib/element-template/runtime-marker.js.map +1 -1
  159. package/runtime/lib/index.js.map +1 -1
  160. package/runtime/lib/internal.d.ts +1 -1
  161. package/runtime/lib/internal.js +1 -1
  162. package/runtime/lib/internal.js.map +1 -1
  163. package/runtime/lib/lynx-api.d.ts +34 -0
  164. package/runtime/lib/lynx-api.js +21 -0
  165. package/runtime/lib/lynx-api.js.map +1 -1
  166. package/runtime/lib/lynx.js +4 -1
  167. package/runtime/lib/lynx.js.map +1 -1
  168. package/runtime/lib/root.js.map +1 -1
  169. package/runtime/lib/runtime-marker.js.map +1 -1
  170. package/runtime/lib/shared/attribute-name.js.map +1 -1
  171. package/runtime/lib/shared/component-stack.js.map +1 -1
  172. package/runtime/lib/shared/index-map.js.map +1 -1
  173. package/runtime/lib/shared/profile.js.map +1 -1
  174. package/runtime/lib/shared/render-constants.js.map +1 -1
  175. package/runtime/lib/snapshot/alog/elementPAPICall.js.map +1 -1
  176. package/runtime/lib/snapshot/alog/index.js.map +1 -1
  177. package/runtime/lib/snapshot/alog/render.js.map +1 -1
  178. package/runtime/lib/snapshot/compat/componentIs.js.map +1 -1
  179. package/runtime/lib/snapshot/debug/debug.js.map +1 -1
  180. package/runtime/lib/snapshot/debug/describeInvalidValue.js.map +1 -1
  181. package/runtime/lib/snapshot/debug/formatPatch.js.map +1 -1
  182. package/runtime/lib/snapshot/debug/printSnapshot.js.map +1 -1
  183. package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
  184. package/runtime/lib/snapshot/debug/vnodeSource.js.map +1 -1
  185. package/runtime/lib/snapshot/gesture/processGesture.js.map +1 -1
  186. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +1 -1
  187. package/runtime/lib/snapshot/gesture/types.js.map +1 -1
  188. package/runtime/lib/snapshot/index.js.map +1 -1
  189. package/runtime/lib/snapshot/lifecycle/constant.js.map +1 -1
  190. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  191. package/runtime/lib/snapshot/lifecycle/event/delayEvents.js.map +1 -1
  192. package/runtime/lib/snapshot/lifecycle/event/delayLifecycleEvents.js.map +1 -1
  193. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js.map +1 -1
  194. package/runtime/lib/snapshot/lifecycle/isRendering.js.map +1 -1
  195. package/runtime/lib/snapshot/lifecycle/patch/commit.js.map +1 -1
  196. package/runtime/lib/snapshot/lifecycle/patch/error.d.ts +11 -1
  197. package/runtime/lib/snapshot/lifecycle/patch/error.js +24 -16
  198. package/runtime/lib/snapshot/lifecycle/patch/error.js.map +1 -1
  199. package/runtime/lib/snapshot/lifecycle/patch/globalState.js.map +1 -1
  200. package/runtime/lib/snapshot/lifecycle/patch/isMainThreadHydrating.js.map +1 -1
  201. package/runtime/lib/snapshot/lifecycle/patch/nodesRefApply.js.map +1 -1
  202. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  203. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.d.ts +1 -1
  204. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +29 -7
  205. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  206. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +2 -2
  207. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  208. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  209. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  210. package/runtime/lib/snapshot/lifecycle/render.js.map +1 -1
  211. package/runtime/lib/snapshot/list/list.js.map +1 -1
  212. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  213. package/runtime/lib/snapshot/list/pendingListUpdates.js.map +1 -1
  214. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  215. package/runtime/lib/snapshot/lynx/children.d.ts +2 -0
  216. package/runtime/lib/snapshot/lynx/children.js.map +1 -1
  217. package/runtime/lib/snapshot/lynx/element.d.ts +8 -0
  218. package/runtime/lib/snapshot/lynx/element.js +4 -0
  219. package/runtime/lib/snapshot/lynx/element.js.map +1 -1
  220. package/runtime/lib/snapshot/lynx/env.js.map +1 -1
  221. package/runtime/lib/snapshot/lynx/injectLepusMethods.js.map +1 -1
  222. package/runtime/lib/snapshot/lynx/lazyBundleConstants.js.map +1 -1
  223. package/runtime/lib/snapshot/lynx/nodesRef.js.map +1 -1
  224. package/runtime/lib/snapshot/lynx/performance.js.map +1 -1
  225. package/runtime/lib/snapshot/lynx/portals.js.map +1 -1
  226. package/runtime/lib/snapshot/lynx/portalsPending.js.map +1 -1
  227. package/runtime/lib/snapshot/lynx/prepareLazyBundleMTS.js.map +1 -1
  228. package/runtime/lib/snapshot/lynx/runWithForce.js.map +1 -1
  229. package/runtime/lib/snapshot/lynx/suspense.js.map +1 -1
  230. package/runtime/lib/snapshot/lynx/tt.js.map +1 -1
  231. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  232. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  233. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  234. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  235. package/runtime/lib/snapshot/snapshot/constants.js.map +1 -1
  236. package/runtime/lib/snapshot/snapshot/definition.js.map +1 -1
  237. package/runtime/lib/snapshot/snapshot/dynamicPartType.js.map +1 -1
  238. package/runtime/lib/snapshot/snapshot/event.js.map +1 -1
  239. package/runtime/lib/snapshot/snapshot/gesture.js.map +1 -1
  240. package/runtime/lib/snapshot/snapshot/index.js.map +1 -1
  241. package/runtime/lib/snapshot/snapshot/list.js.map +1 -1
  242. package/runtime/lib/snapshot/snapshot/platformInfo.js.map +1 -1
  243. package/runtime/lib/snapshot/snapshot/reconstructInstanceTree.js.map +1 -1
  244. package/runtime/lib/snapshot/snapshot/ref.js.map +1 -1
  245. package/runtime/lib/snapshot/snapshot/snapshot.js.map +1 -1
  246. package/runtime/lib/snapshot/snapshot/snapshotCreatorMap.js.map +1 -1
  247. package/runtime/lib/snapshot/snapshot/snapshotInstanceHydrationMap.js.map +1 -1
  248. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js.map +1 -1
  249. package/runtime/lib/snapshot/snapshot/spread.js.map +1 -1
  250. package/runtime/lib/snapshot/snapshot/types.js.map +1 -1
  251. package/runtime/lib/snapshot/snapshot/utils.js.map +1 -1
  252. package/runtime/lib/snapshot/snapshot/workletEvent.js.map +1 -1
  253. package/runtime/lib/snapshot/snapshot/workletRef.js.map +1 -1
  254. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +2 -0
  255. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +2 -0
  256. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  257. package/runtime/lib/snapshot/worklet/ctx.js.map +1 -1
  258. package/runtime/lib/snapshot/worklet/destroy.js.map +1 -1
  259. package/runtime/lib/snapshot/worklet/functionality.js.map +1 -1
  260. package/runtime/lib/snapshot/worklet/hmr.js.map +1 -1
  261. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  262. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  263. package/runtime/lib/utils.js.map +1 -1
  264. package/runtime/lib/worklet-runtime/api/animation/animation.js.map +1 -1
  265. package/runtime/lib/worklet-runtime/api/animation/effect.js.map +1 -1
  266. package/runtime/lib/worklet-runtime/api/element.js.map +1 -1
  267. package/runtime/lib/worklet-runtime/api/lepusQuerySelector.js.map +1 -1
  268. package/runtime/lib/worklet-runtime/api/lynxApi.js.map +1 -1
  269. package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -3
  270. package/runtime/lib/worklet-runtime/bindings/bindings.js +9 -1
  271. package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
  272. package/runtime/lib/worklet-runtime/bindings/events.js.map +1 -1
  273. package/runtime/lib/worklet-runtime/bindings/index.d.ts +0 -1
  274. package/runtime/lib/worklet-runtime/bindings/index.js +0 -1
  275. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  276. package/runtime/lib/worklet-runtime/bindings/observers.js.map +1 -1
  277. package/runtime/lib/worklet-runtime/bindings/types.d.ts +3 -6
  278. package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
  279. package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +1 -1
  280. package/runtime/lib/worklet-runtime/delayWorkletEvent.js.map +1 -1
  281. package/runtime/lib/worklet-runtime/eomImpl.js.map +1 -1
  282. package/runtime/lib/worklet-runtime/eventPropagation.js.map +1 -1
  283. package/runtime/lib/worklet-runtime/global.js.map +1 -1
  284. package/runtime/lib/worklet-runtime/hydrate.js +26 -7
  285. package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
  286. package/runtime/lib/worklet-runtime/index.js +6 -0
  287. package/runtime/lib/worklet-runtime/index.js.map +1 -1
  288. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  289. package/runtime/lib/worklet-runtime/listeners.js.map +1 -1
  290. package/runtime/lib/worklet-runtime/mainThreadObject.d.ts +8 -0
  291. package/runtime/lib/worklet-runtime/mainThreadObject.js +67 -0
  292. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -0
  293. package/runtime/lib/worklet-runtime/runOnMainThread.js.map +1 -1
  294. package/runtime/lib/worklet-runtime/utils/mainThreadFlushLoopGuard.js.map +1 -1
  295. package/runtime/lib/worklet-runtime/utils/profile.js.map +1 -1
  296. package/runtime/lib/worklet-runtime/utils/version.js.map +1 -1
  297. package/runtime/lib/worklet-runtime/workletRef.d.ts +11 -7
  298. package/runtime/lib/worklet-runtime/workletRef.js +72 -9
  299. package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
  300. package/runtime/lib/worklet-runtime/workletRuntime.js +68 -9
  301. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  302. package/runtime/package.json +2 -3
  303. package/runtime/src/core/background-function/run-on-background.ts +16 -0
  304. package/runtime/src/core/capture-main-thread-object.ts +17 -0
  305. package/runtime/src/core/globalProps.ts +2 -0
  306. package/runtime/src/core/hooks/react.ts +4 -0
  307. package/runtime/src/core/hooks/useLynxGlobalEventListener.ts +2 -0
  308. package/runtime/src/core/initData.ts +2 -0
  309. package/runtime/src/core/main-thread-object-handle-registry.ts +25 -0
  310. package/runtime/src/core/main-thread-object.ts +380 -0
  311. package/runtime/src/core/main-thread-ref-id.ts +16 -0
  312. package/runtime/src/core/main-thread-ref-init-value.ts +12 -4
  313. package/runtime/src/core/main-thread-ref.ts +7 -8
  314. package/runtime/src/element-template/background/commit-hook.ts +1 -1
  315. package/runtime/src/element-template/index.ts +6 -0
  316. package/runtime/src/element-template/internal.ts +2 -1
  317. package/runtime/src/element-template/protocol/types.ts +1 -1
  318. package/runtime/src/internal.ts +1 -1
  319. package/runtime/src/lynx-api.ts +38 -0
  320. package/runtime/src/lynx.ts +5 -1
  321. package/runtime/src/snapshot/lifecycle/patch/error.ts +36 -19
  322. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +29 -7
  323. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +2 -2
  324. package/runtime/src/snapshot/lynx/children.ts +2 -0
  325. package/runtime/src/snapshot/lynx/element.ts +8 -0
  326. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +2 -0
  327. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +1 -1
  328. package/runtime/src/worklet-runtime/bindings/bindings.ts +17 -2
  329. package/runtime/src/worklet-runtime/bindings/index.ts +0 -1
  330. package/runtime/src/worklet-runtime/bindings/types.ts +7 -11
  331. package/runtime/src/worklet-runtime/hydrate.ts +43 -10
  332. package/runtime/src/worklet-runtime/index.ts +7 -0
  333. package/runtime/src/worklet-runtime/mainThreadObject.ts +117 -0
  334. package/runtime/src/worklet-runtime/workletRef.ts +120 -19
  335. package/runtime/src/worklet-runtime/workletRuntime.ts +83 -11
  336. package/runtime/tsconfig.json +2 -0
  337. package/runtime/vitest.config.ts +4 -0
  338. package/testing-library/.turbo/turbo-build.log +11 -11
  339. package/testing-library/dist/env/index.js +3 -15
  340. package/testing-library/dist/index.d.ts +165 -2
  341. package/testing-library/dist/plugins/index.js +10 -0
  342. package/testing-library/dist/rstest-config.js +15 -0
  343. package/testing-library/dist/setupFiles/common/worklet-runtime-init.js +11 -0
  344. package/testing-library/package.json +2 -1
  345. package/testing-library/rstest.config.ts +3 -0
  346. package/testing-library/src/__tests__/lynx.test.jsx +1 -1
  347. package/testing-library/src/__tests__/rstest-config.test.js +34 -1
  348. package/testing-library/src/__tests__/worklet.test.jsx +87 -33
  349. package/testing-library/src/plugins/vitest.ts +5 -1
  350. package/testing-library/src/rstest-config.ts +17 -1
  351. package/testing-library/src/setupFiles/common/worklet-runtime-init.js +22 -0
  352. package/testing-library/tsconfig.docs.json +9 -0
  353. package/testing-library/typedoc.json +22 -0
  354. package/transform/.turbo/turbo-build$colon$wasm.log +135 -73
  355. package/transform/__test__/defines.spec.js +80 -12
  356. package/transform/__test__/fixture.spec.js +231 -71
  357. package/transform/__test__/object-methods.spec.js +82 -0
  358. package/transform/crates/swc_plugin_worklet/decl_collect.rs +10 -0
  359. package/transform/crates/swc_plugin_worklet/extract_ident.rs +14 -0
  360. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +228 -13
  361. package/transform/crates/swc_plugin_worklet/lib.rs +599 -150
  362. package/transform/crates/swc_plugin_worklet/napi.rs +10 -1
  363. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +8 -1
  364. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +12 -7
  365. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_js.js +27 -0
  366. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_lepus.js +43 -0
  367. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_ident_from_this_lepus.js +2 -4
  368. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +16 -14
  369. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +18 -18
  370. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_3_lepus.js +3 -5
  371. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_4_lepus.js +2 -4
  372. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +7 -5
  373. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +9 -9
  374. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_computed_object_method_key_lepus.js +22 -0
  375. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_globals.js +2 -4
  376. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_object_method_local_decls_lepus.js +14 -0
  377. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_type_annotations.js +2 -4
  378. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_destructure_from_closure.js +2 -4
  379. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_extract_catch_clause_params.js +2 -4
  380. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_extract_idents_inside_of_ctx.js +2 -4
  381. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_transform_recursively.js +2 -4
  382. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +42 -0
  383. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +58 -0
  384. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_skip_shared_identifiers_lepus.js +2 -4
  385. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_arrow_function.js +2 -4
  386. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_fn_decl_lepus.js +2 -4
  387. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_function_expr.js +2 -4
  388. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_lepus.js +2 -4
  389. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_property_lepus.js +2 -4
  390. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_static_lepus.js +2 -4
  391. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_static_property_lepus.js +2 -4
  392. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +3 -1
  393. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js_fn_in_run_on_js_lepus.js +2 -4
  394. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +5 -5
  395. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_alias.js +2 -4
  396. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_general.js +2 -4
  397. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_general_mixed.js +2 -4
  398. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_multiple_worklets.js +3 -5
  399. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_multiple_worklets_in_func.js +3 -5
  400. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_js.js +9 -0
  401. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_lepus.js +14 -0
  402. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_ui_worklet_lepus.js +2 -4
  403. package/transform/dist/wasm.cjs +1 -1
  404. package/transform/index.d.ts +25 -1
  405. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  406. package/transform/src/lib.rs +193 -23
  407. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +63 -467
  408. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  409. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +340 -28
  410. package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
  411. package/transform/vitest.config.ts +5 -1
  412. package/types/react.docs.d.ts +130 -11
  413. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +0 -8
  414. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +0 -23
  415. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +0 -1
  416. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +0 -25
  417. package/runtime/worklet-runtime/dev.js +0 -638
  418. package/runtime/worklet-runtime/main.js +0 -1
  419. package/testing-library/node_modules/.bin/rsbuild +0 -68
  420. package/testing-library/node_modules/.bin/rspeedy +0 -68
  421. package/testing-library/node_modules/.bin/rstest +0 -68
  422. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_add_worklet_runtime_ident_with_inner_ident.js +0 -13
  423. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_add_worklet_runtime_ident_with_outer_ident.js +0 -13
  424. package/transform/node_modules/.bin/esbuild +0 -59
  425. package/transform/node_modules/.bin/napi +0 -68
@@ -10,15 +10,16 @@ use gen_stmt::StmtGen;
10
10
  use hash::WorkletHash;
11
11
  use rustc_hash::FxHashSet;
12
12
  use serde::Deserialize;
13
+ use std::cell::Cell;
13
14
  use std::collections::HashSet;
15
+ use std::rc::Rc;
14
16
  use std::vec;
15
17
  use swc_core::common::util::take::Take;
16
18
  use swc_core::common::{errors::HANDLER, Span, Spanned, DUMMY_SP};
17
19
  use swc_core::ecma::ast::*;
18
20
  use swc_core::ecma::utils::{prepend_stmts, private_ident};
19
21
  use swc_core::ecma::visit::VisitMutWith;
20
- use swc_core::ecma::visit::{noop_visit_mut_type, VisitMut};
21
- use swc_core::quote;
22
+ use swc_core::ecma::visit::{noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitWith};
22
23
  use worklet_type::WorkletType;
23
24
 
24
25
  use swc_plugins_shared::{
@@ -53,7 +54,7 @@ impl Default for WorkletVisitorConfig {
53
54
  filename: "index.js".into(),
54
55
  target: TransformTarget::LEPUS,
55
56
  custom_global_ident_names: None,
56
- runtime_pkg: "NoDiff".into(),
57
+ runtime_pkg: "@lynx-js/react/internal".into(),
57
58
  }
58
59
  }
59
60
  }
@@ -66,8 +67,9 @@ pub struct WorkletVisitor {
66
67
  named_imports: HashSet<String>,
67
68
  hasher: WorkletHash,
68
69
  shared_identifiers: FxHashSet<Id>,
69
- worklet_runtime_loaded: bool,
70
- worklet_runtime_loaded_ident: Ident,
70
+ requires_main_thread_runtime: bool,
71
+ register_worklet: Ident,
72
+ main_thread_programmability: Option<Rc<Cell<bool>>>,
71
73
  defines_collector: Option<DefinesCollector>,
72
74
  }
73
75
 
@@ -80,6 +82,98 @@ impl Default for WorkletVisitor {
80
82
  impl VisitMut for WorkletVisitor {
81
83
  noop_visit_mut_type!();
82
84
 
85
+ fn visit_mut_prop(&mut self, n: &mut Prop) {
86
+ let Prop::Method(method) = n else {
87
+ n.visit_mut_children_with(self);
88
+ return;
89
+ };
90
+ let Some(body) = method.function.body.as_mut() else {
91
+ n.visit_mut_children_with(self);
92
+ return;
93
+ };
94
+ let Some(worklet_type) = self.check_is_worklet_block(body) else {
95
+ n.visit_mut_children_with(self);
96
+ return;
97
+ };
98
+
99
+ if contains_super_property(&method.function) {
100
+ emit_unsupported_object_method_super(method.span());
101
+ return;
102
+ }
103
+
104
+ let mut collector = ExtractingIdentsCollector::new(ExtractingIdentsCollectorConfig {
105
+ custom_global_ident_names: self.cfg.custom_global_ident_names.clone(),
106
+ shared_identifiers: Some(self.shared_identifiers.clone()),
107
+ });
108
+ method.visit_mut_with(&mut collector);
109
+
110
+ let should_use_getter =
111
+ collector.has_extracted_this_props() || collector.has_extracted_js_fns();
112
+
113
+ let hash = self.hasher.gen(&self.cfg.filename, &self.content_hash);
114
+ let collect_main_thread = self.defines_collector.is_some();
115
+ let collected_hash = collect_main_thread.then(|| hash.clone());
116
+ let (worklet_object_expr, register_worklet_stmt, main_thread_stmt) = StmtGen::transform_worklet(
117
+ self.mode,
118
+ worklet_type,
119
+ hash,
120
+ self.cfg.target,
121
+ Ident::dummy(),
122
+ method.function.clone(),
123
+ &mut collector,
124
+ false,
125
+ &mut self.named_imports,
126
+ &mut self.requires_main_thread_runtime,
127
+ &self.register_worklet,
128
+ collect_main_thread,
129
+ );
130
+
131
+ // Extracted `this.xxx` captures are spread into the ctx object. In an object literal,
132
+ // `this` in a plain key-value initializer refers to the enclosing lexical scope
133
+ // (`undefined` at ESM module top level), not the object being defined. Emitting a
134
+ // getter keeps `this` bound to the receiver on property read, matching the original
135
+ // method-call semantics (`obj.create()` has `this === obj`).
136
+ if should_use_getter {
137
+ *n = Prop::Getter(GetterProp {
138
+ span: method.span(),
139
+ key: method.key.clone(),
140
+ function: Function {
141
+ ctxt: Default::default(),
142
+ span: DUMMY_SP,
143
+ params: vec![],
144
+ decorators: vec![],
145
+ this_param: None,
146
+ body: Some(FunctionBody {
147
+ span: DUMMY_SP,
148
+ stmts: vec![ReturnStmt {
149
+ span: DUMMY_SP,
150
+ arg: Some(worklet_object_expr),
151
+ }
152
+ .into()],
153
+ }),
154
+ is_generator: false,
155
+ is_async: false,
156
+ type_params: None,
157
+ return_type: None,
158
+ }
159
+ .into(),
160
+ });
161
+ } else {
162
+ *n = Prop::KeyValue(KeyValueProp {
163
+ key: method.key.clone(),
164
+ value: worklet_object_expr,
165
+ });
166
+ }
167
+ self.collect_worklet_define(
168
+ collected_hash,
169
+ main_thread_stmt,
170
+ collector.saw_shared_identifiers(),
171
+ );
172
+ self
173
+ .stmts_to_insert_at_top_level
174
+ .push(register_worklet_stmt);
175
+ }
176
+
83
177
  fn visit_mut_class_member(&mut self, n: &mut ClassMember) {
84
178
  match n {
85
179
  ClassMember::Method(_) => {
@@ -129,7 +223,8 @@ impl VisitMut for WorkletVisitor {
129
223
  &mut collector,
130
224
  true,
131
225
  &mut self.named_imports,
132
- self.worklet_runtime_loaded_ident.clone(),
226
+ &mut self.requires_main_thread_runtime,
227
+ &self.register_worklet,
133
228
  collect_main_thread,
134
229
  );
135
230
 
@@ -264,7 +359,8 @@ impl VisitMut for WorkletVisitor {
264
359
  &mut collector,
265
360
  true,
266
361
  &mut self.named_imports,
267
- self.worklet_runtime_loaded_ident.clone(),
362
+ &mut self.requires_main_thread_runtime,
363
+ &self.register_worklet,
268
364
  collect_main_thread,
269
365
  );
270
366
 
@@ -353,7 +449,8 @@ impl VisitMut for WorkletVisitor {
353
449
  &mut collector,
354
450
  false,
355
451
  &mut self.named_imports,
356
- self.worklet_runtime_loaded_ident.clone(),
452
+ &mut self.requires_main_thread_runtime,
453
+ &self.register_worklet,
357
454
  collect_main_thread,
358
455
  );
359
456
 
@@ -439,7 +536,8 @@ impl VisitMut for WorkletVisitor {
439
536
  &mut collector,
440
537
  false,
441
538
  &mut self.named_imports,
442
- self.worklet_runtime_loaded_ident.clone(),
539
+ &mut self.requires_main_thread_runtime,
540
+ &self.register_worklet,
443
541
  collect_main_thread,
444
542
  );
445
543
 
@@ -481,7 +579,8 @@ impl VisitMut for WorkletVisitor {
481
579
  &mut collector,
482
580
  false,
483
581
  &mut self.named_imports,
484
- self.worklet_runtime_loaded_ident.clone(),
582
+ &mut self.requires_main_thread_runtime,
583
+ &self.register_worklet,
485
584
  collect_main_thread,
486
585
  );
487
586
 
@@ -568,7 +667,8 @@ impl VisitMut for WorkletVisitor {
568
667
  &mut collector,
569
668
  false,
570
669
  &mut self.named_imports,
571
- self.worklet_runtime_loaded_ident.clone(),
670
+ &mut self.requires_main_thread_runtime,
671
+ &self.register_worklet,
572
672
  collect_main_thread,
573
673
  );
574
674
 
@@ -591,7 +691,7 @@ impl VisitMut for WorkletVisitor {
591
691
  }
592
692
 
593
693
  fn visit_mut_module(&mut self, n: &mut Module) {
594
- // First process imports to detect shared-runtime modules
694
+ // First process imports to detect shared-runtime modules.
595
695
  for item in &n.body {
596
696
  if let ModuleItem::ModuleDecl(ModuleDecl::Import(import_decl)) = item {
597
697
  if is_shared_runtime_import(import_decl) {
@@ -616,87 +716,89 @@ impl VisitMut for WorkletVisitor {
616
716
 
617
717
  n.visit_mut_children_with(self);
618
718
 
619
- // Add global loadWorkletRuntime call if needed
620
- if self.named_imports.contains("loadWorkletRuntime") && !self.worklet_runtime_loaded {
621
- self.stmts_to_insert_at_top_level.insert(
622
- 0,
623
- quote!("const $loaded = loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry)" as Stmt, loaded = self.worklet_runtime_loaded_ident.clone()),
624
- );
625
- self.worklet_runtime_loaded = true;
719
+ if self.requires_main_thread_runtime {
720
+ if let Some(result) = &self.main_thread_programmability {
721
+ result.set(true);
722
+ }
626
723
  }
627
724
 
628
725
  let mut specifiers = self.named_imports.iter().collect::<Vec<_>>();
726
+ let mut prepended_items = vec![];
727
+
728
+ if self.requires_main_thread_runtime {
729
+ // The generated call and its import share an SWC binding, so hygiene
730
+ // keeps authored names and compiler-owned registrations independent.
731
+ prepended_items.push(self.registration_import());
732
+ }
629
733
 
630
734
  if !specifiers.is_empty() {
631
735
  // Sort to keep the output consistent
632
736
  specifiers.sort();
633
737
 
634
- prepend_stmts(
635
- &mut n.body,
636
- vec![
637
- ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
638
- span: DUMMY_SP,
639
- phase: ImportPhase::Evaluation,
640
- specifiers: specifiers
641
- .iter()
642
- .map(|imported| {
643
- ImportSpecifier::Named(ImportNamedSpecifier {
644
- span: DUMMY_SP,
645
- is_type_only: false,
646
- local: Ident {
647
- ctxt: Default::default(),
648
- span: DUMMY_SP,
649
- sym: format!("__{imported}").into(),
650
- optional: false,
651
- },
652
- imported: Some(ModuleExportName::Ident(Ident {
653
- ctxt: Default::default(),
654
- span: DUMMY_SP,
655
- sym: imported.as_str().into(),
656
- optional: false,
657
- })),
658
- })
659
- })
660
- .collect::<Vec<_>>(),
661
- src: Box::new(Str {
738
+ prepended_items.push(ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
739
+ span: DUMMY_SP,
740
+ phase: ImportPhase::Evaluation,
741
+ specifiers: specifiers
742
+ .iter()
743
+ .map(|imported| {
744
+ ImportSpecifier::Named(ImportNamedSpecifier {
662
745
  span: DUMMY_SP,
663
- raw: None,
664
- value: self.cfg.runtime_pkg.clone().into(),
665
- }),
666
- type_only: Default::default(),
667
- with: Default::default(),
668
- })),
669
- ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
670
- ctxt: Default::default(),
746
+ is_type_only: false,
747
+ local: Ident {
748
+ ctxt: Default::default(),
749
+ span: DUMMY_SP,
750
+ sym: format!("__{imported}").into(),
751
+ optional: false,
752
+ },
753
+ imported: Some(ModuleExportName::Ident(Ident {
754
+ ctxt: Default::default(),
755
+ span: DUMMY_SP,
756
+ sym: imported.as_str().into(),
757
+ optional: false,
758
+ })),
759
+ })
760
+ })
761
+ .collect::<Vec<_>>(),
762
+ src: Box::new(Str {
763
+ span: DUMMY_SP,
764
+ raw: None,
765
+ value: self.cfg.runtime_pkg.clone().into(),
766
+ }),
767
+ type_only: Default::default(),
768
+ with: Default::default(),
769
+ })));
770
+ prepended_items.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
771
+ ctxt: Default::default(),
772
+ span: DUMMY_SP,
773
+ kind: VarDeclKind::Var,
774
+ declare: false,
775
+ decls: specifiers
776
+ .into_iter()
777
+ .map(|name| VarDeclarator {
671
778
  span: DUMMY_SP,
672
- kind: VarDeclKind::Var,
673
- declare: false,
674
- decls: specifiers
675
- .into_iter()
676
- .map(|name| VarDeclarator {
779
+ name: Pat::Ident(
780
+ Ident {
781
+ ctxt: Default::default(),
677
782
  span: DUMMY_SP,
678
- name: Pat::Ident(
679
- Ident {
680
- ctxt: Default::default(),
681
- span: DUMMY_SP,
682
- sym: name.as_str().into(),
683
- optional: false,
684
- }
685
- .into(),
686
- ),
687
- init: Some(Box::new(Expr::Ident(Ident {
688
- ctxt: Default::default(),
689
- span: DUMMY_SP,
690
- sym: format!("__{name}").into(),
691
- optional: false,
692
- }))),
693
- definite: false,
694
- })
695
- .collect(),
696
- })))),
697
- ]
698
- .into_iter(),
699
- );
783
+ sym: name.as_str().into(),
784
+ optional: false,
785
+ }
786
+ .into(),
787
+ ),
788
+ init: Some(Box::new(Expr::Ident(Ident {
789
+ ctxt: Default::default(),
790
+ span: DUMMY_SP,
791
+ sym: format!("__{name}").into(),
792
+ optional: false,
793
+ }))),
794
+ definite: false,
795
+ })
796
+ .collect(),
797
+ })))));
798
+ }
799
+
800
+ if !prepended_items.is_empty() {
801
+ prepend_stmts(&mut n.body, prepended_items.into_iter());
700
802
  }
701
803
  // Add statements to insert at top level after processing all items
702
804
  n.body.extend(
@@ -710,6 +812,8 @@ impl VisitMut for WorkletVisitor {
710
812
  }
711
813
 
712
814
  const INVALID_RUNTIME_MSG: &str = "Invalid runtime value. Only 'shared' is supported.";
815
+ const UNSUPPORTED_OBJECT_METHOD_SUPER_MSG: &str =
816
+ "`super` is not supported in Main Thread object methods.";
713
817
 
714
818
  fn emit_invalid_runtime_error(span: Span) {
715
819
  HANDLER.with(|handler| {
@@ -717,6 +821,79 @@ fn emit_invalid_runtime_error(span: Span) {
717
821
  });
718
822
  }
719
823
 
824
+ fn emit_unsupported_object_method_super(span: Span) {
825
+ HANDLER.with(|handler| {
826
+ handler
827
+ .struct_span_err(span, UNSUPPORTED_OBJECT_METHOD_SUPER_MSG)
828
+ .emit();
829
+ });
830
+ }
831
+
832
+ #[derive(Default)]
833
+ struct SuperPropertyDetector {
834
+ found: bool,
835
+ }
836
+
837
+ impl Visit for SuperPropertyDetector {
838
+ noop_visit_type!();
839
+
840
+ fn visit_super_prop_expr(&mut self, _node: &SuperPropExpr) {
841
+ self.found = true;
842
+ }
843
+ // A nested object method has its own [[HomeObject]], so its `super` binding
844
+ // does not refer to the Main Thread object method being checked. Computed
845
+ // property names are evaluated in the enclosing scope and must still be
846
+ // visited because they can use the enclosing method's `super` binding.
847
+ fn visit_method_prop(&mut self, node: &MethodProp) {
848
+ if matches!(&node.key, PropName::Computed(_)) {
849
+ node.key.visit_with(self);
850
+ }
851
+ }
852
+
853
+ fn visit_getter_prop(&mut self, node: &GetterProp) {
854
+ if matches!(&node.key, PropName::Computed(_)) {
855
+ node.key.visit_with(self);
856
+ }
857
+ }
858
+
859
+ fn visit_setter_prop(&mut self, node: &SetterProp) {
860
+ if matches!(&node.key, PropName::Computed(_)) {
861
+ node.key.visit_with(self);
862
+ }
863
+ }
864
+
865
+ fn visit_class_method(&mut self, node: &ClassMethod) {
866
+ if matches!(&node.key, PropName::Computed(_)) {
867
+ node.key.visit_with(self);
868
+ }
869
+ }
870
+
871
+ fn visit_private_method(&mut self, _node: &PrivateMethod) {}
872
+
873
+ fn visit_class_prop(&mut self, node: &ClassProp) {
874
+ if matches!(&node.key, PropName::Computed(_)) {
875
+ node.key.visit_with(self);
876
+ }
877
+ }
878
+
879
+ fn visit_private_prop(&mut self, _node: &PrivateProp) {}
880
+
881
+ fn visit_constructor(&mut self, _node: &Constructor) {}
882
+
883
+ fn visit_static_block(&mut self, _node: &StaticBlock) {}
884
+
885
+ fn visit_auto_accessor(&mut self, node: &AutoAccessor) {
886
+ if let Key::Public(PropName::Computed(_)) = &node.key {
887
+ node.key.visit_with(self);
888
+ }
889
+ }
890
+ }
891
+
892
+ fn contains_super_property(function: &Function) -> bool {
893
+ let mut detector = SuperPropertyDetector::default();
894
+ function.visit_with(&mut detector);
895
+ detector.found
896
+ }
720
897
  fn validate_runtime_value(expr: &Expr) -> bool {
721
898
  match expr {
722
899
  Expr::Lit(Lit::Str(value)) => {
@@ -762,6 +939,11 @@ impl WorkletVisitor {
762
939
  self
763
940
  }
764
941
 
942
+ pub fn with_main_thread_programmability(mut self, result: Rc<Cell<bool>>) -> Self {
943
+ self.main_thread_programmability = Some(result);
944
+ self
945
+ }
946
+
765
947
  pub fn new(mode: TransformMode, cfg: WorkletVisitorConfig) -> Self {
766
948
  WorkletVisitor {
767
949
  mode,
@@ -771,8 +953,9 @@ impl WorkletVisitor {
771
953
  hasher: WorkletHash::new(),
772
954
  named_imports: HashSet::default(),
773
955
  shared_identifiers: FxHashSet::default(),
774
- worklet_runtime_loaded: false,
775
- worklet_runtime_loaded_ident: private_ident!("__workletRuntimeLoaded"),
956
+ requires_main_thread_runtime: false,
957
+ register_worklet: private_ident!("__registerWorklet"),
958
+ main_thread_programmability: None,
776
959
  defines_collector: None,
777
960
  }
778
961
  }
@@ -782,6 +965,36 @@ impl WorkletVisitor {
782
965
  self
783
966
  }
784
967
 
968
+ fn registration_import(&self) -> ModuleItem {
969
+ let runtime_pkg = self
970
+ .cfg
971
+ .runtime_pkg
972
+ .strip_suffix("/internal")
973
+ // A scoped package named `internal` is a package root, not an entry.
974
+ .filter(|pkg| !pkg.starts_with('@') || pkg.contains('/'))
975
+ .unwrap_or(&self.cfg.runtime_pkg);
976
+ ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
977
+ span: DUMMY_SP,
978
+ phase: ImportPhase::Evaluation,
979
+ specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
980
+ span: DUMMY_SP,
981
+ is_type_only: false,
982
+ local: self.register_worklet.clone(),
983
+ imported: Some(ModuleExportName::Ident(Ident::new_no_ctxt(
984
+ "registerWorklet".into(),
985
+ DUMMY_SP,
986
+ ))),
987
+ })],
988
+ src: Box::new(Str {
989
+ span: DUMMY_SP,
990
+ raw: None,
991
+ value: format!("{runtime_pkg}/worklet-runtime/init").into(),
992
+ }),
993
+ type_only: false,
994
+ with: None,
995
+ }))
996
+ }
997
+
785
998
  fn collect_worklet_define(
786
999
  &mut self,
787
1000
  hash: Option<String>,
@@ -795,15 +1008,11 @@ impl WorkletVisitor {
795
1008
  collect_unmergeable_define(&self.defines_collector, DefineKind::Worklet, hash);
796
1009
  return;
797
1010
  }
798
- let guard = quote!(
799
- "const $loaded = loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry)" as Stmt,
800
- loaded = self.worklet_runtime_loaded_ident.clone(),
801
- );
802
1011
  collect_define(
803
1012
  &self.defines_collector,
804
1013
  DefineKind::Worklet,
805
1014
  hash,
806
- vec![ModuleItem::Stmt(guard), ModuleItem::Stmt(stmt)],
1015
+ vec![self.registration_import(), ModuleItem::Stmt(stmt)],
807
1016
  );
808
1017
  }
809
1018
 
@@ -867,6 +1076,145 @@ function worklet(event: Event) {
867
1076
  "#
868
1077
  );
869
1078
 
1079
+ test!(
1080
+ module,
1081
+ Syntax::Typescript(TsSyntax {
1082
+ ..Default::default()
1083
+ }),
1084
+ |_| (
1085
+ resolver(Mark::new(), Mark::new(), true),
1086
+ visit_mut_pass(WorkletVisitor::new(
1087
+ TransformMode::Test,
1088
+ WorkletVisitorConfig {
1089
+ filename: "index.js".into(),
1090
+ target: TransformTarget::LEPUS,
1091
+ custom_global_ident_names: None,
1092
+ runtime_pkg: "@lynx-js/react".into(),
1093
+ }
1094
+ )),
1095
+ hygiene()
1096
+ ),
1097
+ should_not_capture_object_method_local_decls_lepus,
1098
+ r#"
1099
+ const valueType = defineMainThreadObjectType({
1100
+ type: '@test/value',
1101
+ create(initialValue) {
1102
+ "main thread";
1103
+ const state = { value: initialValue };
1104
+ return state;
1105
+ },
1106
+ });
1107
+ "#
1108
+ );
1109
+
1110
+ test!(
1111
+ module,
1112
+ Syntax::Typescript(TsSyntax {
1113
+ ..Default::default()
1114
+ }),
1115
+ |_| (
1116
+ resolver(Mark::new(), Mark::new(), true),
1117
+ visit_mut_pass(WorkletVisitor::new(
1118
+ TransformMode::Test,
1119
+ WorkletVisitorConfig {
1120
+ filename: "index.js".into(),
1121
+ target: TransformTarget::LEPUS,
1122
+ custom_global_ident_names: None,
1123
+ runtime_pkg: "@lynx-js/react".into(),
1124
+ }
1125
+ )),
1126
+ hygiene()
1127
+ ),
1128
+ should_not_capture_computed_object_method_key_lepus,
1129
+ r#"
1130
+ const name = 'create';
1131
+ const nestedName = 'read';
1132
+ const valueType = defineMainThreadObjectType({
1133
+ type: '@test/value',
1134
+ [name](initialValue) {
1135
+ "main thread";
1136
+ return { value: initialValue, [nestedName](nestedName) { return nestedName; } };
1137
+ },
1138
+ });
1139
+ "#
1140
+ );
1141
+
1142
+ test!(
1143
+ module,
1144
+ Syntax::Typescript(TsSyntax {
1145
+ ..Default::default()
1146
+ }),
1147
+ |_| (
1148
+ resolver(Mark::new(), Mark::new(), true),
1149
+ visit_mut_pass(WorkletVisitor::new(
1150
+ TransformMode::Test,
1151
+ WorkletVisitorConfig {
1152
+ filename: "index.js".into(),
1153
+ target: TransformTarget::LEPUS,
1154
+ custom_global_ident_names: None,
1155
+ runtime_pkg: "@lynx-js/react".into(),
1156
+ }
1157
+ )),
1158
+ hygiene()
1159
+ ),
1160
+ should_expose_object_method_capture_channels_lepus,
1161
+ r#"
1162
+ const callback = () => {};
1163
+ const valueType = defineMainThreadObjectType({
1164
+ type: '@test/capturing-value',
1165
+ helper: 1,
1166
+ callback,
1167
+ callbackOnly() {
1168
+ "main thread";
1169
+ runOnBackground(this.callback)();
1170
+ },
1171
+ create(initialValue: number) {
1172
+ "main thread";
1173
+ runOnBackground(callback)();
1174
+ return { value: initialValue + this.helper };
1175
+ },
1176
+ });
1177
+ "#
1178
+ );
1179
+
1180
+ test!(
1181
+ module,
1182
+ Syntax::Typescript(TsSyntax {
1183
+ ..Default::default()
1184
+ }),
1185
+ |_| (
1186
+ resolver(Mark::new(), Mark::new(), true),
1187
+ visit_mut_pass(WorkletVisitor::new(
1188
+ TransformMode::Test,
1189
+ WorkletVisitorConfig {
1190
+ filename: "index.js".into(),
1191
+ target: TransformTarget::JS,
1192
+ custom_global_ident_names: None,
1193
+ runtime_pkg: "@lynx-js/react".into(),
1194
+ }
1195
+ )),
1196
+ hygiene()
1197
+ ),
1198
+ should_expose_object_method_capture_channels_js,
1199
+ r#"
1200
+ const callback = () => {};
1201
+ const valueType = defineMainThreadObjectType({
1202
+ type: '@test/capturing-value',
1203
+ helper: 1,
1204
+ callback,
1205
+ callbackOnly() {
1206
+ "main thread";
1207
+ runOnBackground(this.callback)();
1208
+ },
1209
+ create(initialValue: number) {
1210
+ "main thread";
1211
+ runOnBackground(callback)();
1212
+ return { value: initialValue + this.helper };
1213
+ },
1214
+ });
1215
+ "#
1216
+ );
1217
+
870
1218
  test!(
871
1219
  module,
872
1220
  Syntax::Typescript(TsSyntax {
@@ -993,6 +1341,102 @@ function X(event) {
993
1341
  "#
994
1342
  );
995
1343
 
1344
+ test!(
1345
+ module,
1346
+ Syntax::Typescript(TsSyntax {
1347
+ ..Default::default()
1348
+ }),
1349
+ |_| (
1350
+ resolver(Mark::new(), Mark::new(), true),
1351
+ visit_mut_pass(WorkletVisitor::new(
1352
+ TransformMode::Test,
1353
+ WorkletVisitorConfig {
1354
+ filename: "index.ts".into(),
1355
+ target: TransformTarget::JS,
1356
+ custom_global_ident_names: None,
1357
+ runtime_pkg: "@lynx-js/react".into(),
1358
+ }
1359
+ )),
1360
+ hygiene()
1361
+ ),
1362
+ should_preserve_main_thread_objects_in_class_js,
1363
+ r#"
1364
+ class App extends Component {
1365
+ value: MotionValue<number>;
1366
+ ref: MainThreadRef<number>;
1367
+ static value: MotionValue<number>;
1368
+
1369
+ onTap() {
1370
+ "main thread";
1371
+ this.value.get();
1372
+ this.props.value.get();
1373
+ this.value.set(1);
1374
+ this.value?.get();
1375
+ this.value["get"]();
1376
+ return this.ref.current;
1377
+ }
1378
+
1379
+ onMove = () => {
1380
+ "main thread";
1381
+ return this.value.get();
1382
+ };
1383
+
1384
+ static onStatic() {
1385
+ "main thread";
1386
+ return this.value.get();
1387
+ }
1388
+ }
1389
+ "#
1390
+ );
1391
+
1392
+ test!(
1393
+ module,
1394
+ Syntax::Typescript(TsSyntax {
1395
+ ..Default::default()
1396
+ }),
1397
+ |_| (
1398
+ resolver(Mark::new(), Mark::new(), true),
1399
+ visit_mut_pass(WorkletVisitor::new(
1400
+ TransformMode::Test,
1401
+ WorkletVisitorConfig {
1402
+ filename: "index.ts".into(),
1403
+ target: TransformTarget::LEPUS,
1404
+ custom_global_ident_names: None,
1405
+ runtime_pkg: "@lynx-js/react".into(),
1406
+ }
1407
+ )),
1408
+ hygiene()
1409
+ ),
1410
+ should_preserve_main_thread_objects_in_class_lepus,
1411
+ r#"
1412
+ class App extends Component {
1413
+ value: MotionValue<number>;
1414
+ ref: MainThreadRef<number>;
1415
+ static value: MotionValue<number>;
1416
+
1417
+ onTap() {
1418
+ "main thread";
1419
+ this.value.get();
1420
+ this.props.value.get();
1421
+ this.value.set(1);
1422
+ this.value?.get();
1423
+ this.value["get"]();
1424
+ return this.ref.current;
1425
+ }
1426
+
1427
+ onMove = () => {
1428
+ "main thread";
1429
+ return this.value.get();
1430
+ };
1431
+
1432
+ static onStatic() {
1433
+ "main thread";
1434
+ return this.value.get();
1435
+ }
1436
+ }
1437
+ "#
1438
+ );
1439
+
996
1440
  test!(
997
1441
  module,
998
1442
  Syntax::Es(EsSyntax {
@@ -1354,6 +1798,70 @@ let X = function (event) {
1354
1798
  "#
1355
1799
  );
1356
1800
 
1801
+ test!(
1802
+ module,
1803
+ Syntax::Typescript(TsSyntax {
1804
+ ..Default::default()
1805
+ }),
1806
+ |_| (
1807
+ resolver(Mark::new(), Mark::new(), true),
1808
+ visit_mut_pass(WorkletVisitor::new(
1809
+ TransformMode::Test,
1810
+ WorkletVisitorConfig {
1811
+ filename: "index.js".into(),
1812
+ target: TransformTarget::LEPUS,
1813
+ custom_global_ident_names: None,
1814
+ runtime_pkg: "@lynx-js/react".into(),
1815
+ }
1816
+ )),
1817
+ hygiene()
1818
+ ),
1819
+ should_transform_object_methods_lepus,
1820
+ r#"
1821
+ import { create } from './shared.js' with { runtime: "shared" };
1822
+
1823
+ const valueType = defineMainThreadObjectType({
1824
+ type: '@test/value',
1825
+ create(initialValue: number) {
1826
+ "main thread";
1827
+ return create(initialValue);
1828
+ },
1829
+ });
1830
+ "#
1831
+ );
1832
+
1833
+ test!(
1834
+ module,
1835
+ Syntax::Typescript(TsSyntax {
1836
+ ..Default::default()
1837
+ }),
1838
+ |_| (
1839
+ resolver(Mark::new(), Mark::new(), true),
1840
+ visit_mut_pass(WorkletVisitor::new(
1841
+ TransformMode::Test,
1842
+ WorkletVisitorConfig {
1843
+ filename: "index.js".into(),
1844
+ target: TransformTarget::JS,
1845
+ custom_global_ident_names: None,
1846
+ runtime_pkg: "@lynx-js/react".into(),
1847
+ }
1848
+ )),
1849
+ hygiene()
1850
+ ),
1851
+ should_transform_object_methods_js,
1852
+ r#"
1853
+ import { create } from './shared.js' with { runtime: "shared" };
1854
+
1855
+ const valueType = defineMainThreadObjectType({
1856
+ type: '@test/value',
1857
+ create(initialValue: number) {
1858
+ "main thread";
1859
+ return create(initialValue);
1860
+ },
1861
+ });
1862
+ "#
1863
+ );
1864
+
1357
1865
  test!(
1358
1866
  module,
1359
1867
  Syntax::Es(EsSyntax {
@@ -1846,7 +2354,7 @@ class App extends Component {
1846
2354
  let a = 123;
1847
2355
  const b = [ a, ...y1];
1848
2356
  const c = { a, y2, ...y3, ...{ d: 233, e: y4 } };
1849
- return y5.r;
2357
+ return y5.r + props.value.get();
1850
2358
  }
1851
2359
  "#
1852
2360
  );
@@ -1876,7 +2384,7 @@ class App extends Component {
1876
2384
  let a = 123;
1877
2385
  const b = [ a, ...y1];
1878
2386
  const c = { a, y2, ...y3, ...{ d: 233, e: y4 } };
1879
- return y5.r;
2387
+ return y5.r + props.value.get();
1880
2388
  }
1881
2389
  "#
1882
2390
  );
@@ -2807,63 +3315,4 @@ class App extends Component {
2807
3315
  }
2808
3316
  "#
2809
3317
  );
2810
-
2811
- test!(
2812
- module,
2813
- Syntax::Typescript(TsSyntax {
2814
- ..Default::default()
2815
- }),
2816
- |_| (
2817
- resolver(Mark::new(), Mark::new(), true),
2818
- visit_mut_pass(WorkletVisitor::new(
2819
- TransformMode::Test,
2820
- WorkletVisitorConfig {
2821
- filename: "index.ts".into(),
2822
- target: TransformTarget::LEPUS,
2823
- custom_global_ident_names: None,
2824
- runtime_pkg: "@lynx-js/react".into(),
2825
- }
2826
- )),
2827
- hygiene()
2828
- ),
2829
- should_add_worklet_runtime_ident_with_outer_ident,
2830
- r#"
2831
- const __workletRuntimeLoaded = false;
2832
- console.log(__workletRuntimeLoaded);
2833
-
2834
- function foo() {
2835
- "main thread";
2836
- return 1;
2837
- }
2838
- "#
2839
- );
2840
-
2841
- test!(
2842
- module,
2843
- Syntax::Typescript(TsSyntax {
2844
- ..Default::default()
2845
- }),
2846
- |_| (
2847
- resolver(Mark::new(), Mark::new(), true),
2848
- visit_mut_pass(WorkletVisitor::new(
2849
- TransformMode::Test,
2850
- WorkletVisitorConfig {
2851
- filename: "index.ts".into(),
2852
- target: TransformTarget::LEPUS,
2853
- custom_global_ident_names: None,
2854
- runtime_pkg: "@lynx-js/react".into(),
2855
- }
2856
- )),
2857
- hygiene()
2858
- ),
2859
- should_add_worklet_runtime_ident_with_inner_ident,
2860
- r#"
2861
- function foo() {
2862
- "main thread";
2863
- const __workletRuntimeLoaded = false;
2864
- console.log(__workletRuntimeLoaded);
2865
- return 1;
2866
- }
2867
- "#
2868
- );
2869
3318
  }