@lynx-js/react 0.129.0 → 0.129.2

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 (282) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +3 -4
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/globalProps.test.ts +27 -6
  6. package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
  7. package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
  8. package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
  9. package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
  10. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
  11. package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
  12. package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
  13. package/runtime/__test__/element-template/native/mts-destroy.test.ts +88 -0
  14. package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +3 -0
  15. package/runtime/__test__/element-template/native/reload.test.ts +118 -2
  16. package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
  17. package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
  18. package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
  19. package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
  20. package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
  21. package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
  22. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +212 -7
  23. package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
  24. package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
  25. package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
  26. package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
  27. package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
  28. package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
  29. package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
  30. package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
  31. package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
  32. package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
  33. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +309 -26
  34. package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
  35. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
  36. package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
  37. package/runtime/__test__/shared/component-stack.test.ts +211 -0
  38. package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
  39. package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
  40. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
  41. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
  42. package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
  43. package/runtime/__test__/snapshot/preact.test.jsx +0 -3
  44. package/runtime/__test__/snapshot/ref.test.jsx +30 -0
  45. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
  46. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +12 -0
  47. package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
  48. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
  49. package/runtime/lazy/element-template-internal.js +1 -1
  50. package/runtime/lazy/internal.js +1 -1
  51. package/runtime/lazy/jsx-dev-runtime.js +1 -1
  52. package/runtime/lazy/jsx-runtime.js +1 -1
  53. package/runtime/lazy/legacy-react-runtime.js +1 -1
  54. package/runtime/lazy/react-lepus.js +1 -1
  55. package/runtime/lazy/react.js +1 -1
  56. package/runtime/lib/core/capture-main-thread-object.js +2 -1
  57. package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
  58. package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
  59. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  60. package/runtime/lib/core/globalProps.js +1 -1
  61. package/runtime/lib/core/globalProps.js.map +1 -1
  62. package/runtime/lib/core/lynx/lazy-bundle.js +1 -1
  63. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  64. package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
  65. package/runtime/lib/core/lynx-data-processors.js +3 -0
  66. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  67. package/runtime/lib/core/runtime-destroy.js +1 -1
  68. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  69. package/runtime/lib/document.js +0 -5
  70. package/runtime/lib/document.js.map +1 -1
  71. package/runtime/lib/element-template/background/attr-slots.js +5 -2
  72. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  73. package/runtime/lib/element-template/background/commit-hook.js +11 -12
  74. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  75. package/runtime/lib/element-template/background/destroy.js +16 -11
  76. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  77. package/runtime/lib/element-template/background/hydrate.js +23 -0
  78. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  79. package/runtime/lib/element-template/client/root.d.ts +1 -1
  80. package/runtime/lib/element-template/client/root.js.map +1 -1
  81. package/runtime/lib/element-template/debug/alog.d.ts +1 -1
  82. package/runtime/lib/element-template/debug/alog.js +4 -1
  83. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  84. package/runtime/lib/element-template/index.d.ts +2 -2
  85. package/runtime/lib/element-template/index.js.map +1 -1
  86. package/runtime/lib/element-template/internal.d.ts +2 -2
  87. package/runtime/lib/element-template/internal.js +1 -14
  88. package/runtime/lib/element-template/internal.js.map +1 -1
  89. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  90. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
  91. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  92. package/runtime/lib/element-template/native/flush.d.ts +1 -0
  93. package/runtime/lib/element-template/native/flush.js +9 -0
  94. package/runtime/lib/element-template/native/flush.js.map +1 -0
  95. package/runtime/lib/element-template/native/main-thread-api.js +3 -7
  96. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  97. package/runtime/lib/element-template/native/mts-destroy.js +8 -2
  98. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  99. package/runtime/lib/element-template/native/patch-listener.js +57 -50
  100. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  101. package/runtime/lib/element-template/native/reload-background.js +1 -1
  102. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  103. package/runtime/lib/element-template/native/reload-main-thread.js +35 -10
  104. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  105. package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
  106. package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
  107. package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
  108. package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
  109. package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
  110. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  111. package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
  112. package/runtime/lib/element-template/protocol/opcodes.js +1 -0
  113. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  114. package/runtime/lib/element-template/protocol/types.d.ts +7 -1
  115. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  116. package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
  117. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  118. package/runtime/lib/element-template/runtime/patch.js +31 -2
  119. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  120. package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
  121. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  122. package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
  123. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  124. package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
  125. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
  126. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  127. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
  128. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  129. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -1
  130. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +380 -50
  131. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  132. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +3 -2
  133. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
  134. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  135. package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
  136. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  137. package/runtime/lib/internal.d.ts +1 -1
  138. package/runtime/lib/internal.js +1 -1
  139. package/runtime/lib/internal.js.map +1 -1
  140. package/runtime/lib/lynx-api.d.ts +2 -125
  141. package/runtime/lib/lynx-api.js.map +1 -1
  142. package/runtime/lib/shared/component-stack.d.ts +0 -4
  143. package/runtime/lib/shared/component-stack.js +21 -5
  144. package/runtime/lib/shared/component-stack.js.map +1 -1
  145. package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
  146. package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
  147. package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
  148. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  149. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
  150. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
  151. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  152. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
  153. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  154. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
  155. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  156. package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
  157. package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
  158. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  159. package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
  160. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  161. package/runtime/lib/snapshot/lynx/calledByNative.js +11 -8
  162. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  163. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
  164. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  165. package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
  166. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  167. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
  168. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  169. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
  170. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  171. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  172. package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
  173. package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
  174. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  175. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
  176. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
  177. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
  178. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
  179. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +6 -0
  180. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  181. package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
  182. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  183. package/runtime/package.json +2 -3
  184. package/runtime/src/core/capture-main-thread-object.ts +2 -1
  185. package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
  186. package/runtime/src/core/globalProps.ts +1 -1
  187. package/runtime/src/core/lynx/lazy-bundle.ts +1 -1
  188. package/runtime/src/core/lynx-data-processors.ts +124 -1
  189. package/runtime/src/core/runtime-destroy.ts +1 -1
  190. package/runtime/src/document.ts +0 -6
  191. package/runtime/src/element-template/background/attr-slots.ts +5 -1
  192. package/runtime/src/element-template/background/commit-hook.ts +18 -12
  193. package/runtime/src/element-template/background/destroy.ts +16 -13
  194. package/runtime/src/element-template/background/hydrate.ts +23 -0
  195. package/runtime/src/element-template/client/root.ts +1 -3
  196. package/runtime/src/element-template/debug/alog.ts +5 -2
  197. package/runtime/src/element-template/index.ts +2 -2
  198. package/runtime/src/element-template/internal.ts +1 -20
  199. package/runtime/src/element-template/lynx/env.ts +1 -1
  200. package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
  201. package/runtime/src/element-template/native/flush.ts +10 -0
  202. package/runtime/src/element-template/native/main-thread-api.ts +3 -6
  203. package/runtime/src/element-template/native/mts-destroy.ts +7 -2
  204. package/runtime/src/element-template/native/patch-listener.ts +62 -57
  205. package/runtime/src/element-template/native/reload-background.ts +1 -1
  206. package/runtime/src/element-template/native/reload-main-thread.ts +32 -11
  207. package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
  208. package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
  209. package/runtime/src/element-template/protocol/opcodes.ts +1 -0
  210. package/runtime/src/element-template/protocol/types.ts +8 -13
  211. package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
  212. package/runtime/src/element-template/runtime/patch.ts +38 -0
  213. package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
  214. package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
  215. package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
  216. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
  217. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +456 -50
  218. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +14 -4
  219. package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
  220. package/runtime/src/element-template/types.d.ts +6 -0
  221. package/runtime/src/internal.ts +1 -1
  222. package/runtime/src/lynx-api.ts +2 -124
  223. package/runtime/src/shared/component-stack.ts +22 -5
  224. package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
  225. package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
  226. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
  227. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
  228. package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
  229. package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
  230. package/runtime/src/snapshot/lynx/calledByNative.ts +11 -9
  231. package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
  232. package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
  233. package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
  234. package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
  235. package/runtime/src/worklet-runtime/README.md +30 -10
  236. package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
  237. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
  238. package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +6 -0
  239. package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
  240. package/runtime/types/types.d.ts +2 -1
  241. package/runtime/vitest.config.ts +0 -2
  242. package/testing-library/.turbo/turbo-build.log +7 -7
  243. package/testing-library/src/__tests__/act.test.jsx +0 -7
  244. package/testing-library/src/__tests__/alog.test.jsx +12 -54
  245. package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
  246. package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
  247. package/testing-library/src/__tests__/worklet.test.jsx +49 -0
  248. package/transform/.turbo/turbo-build$colon$wasm.log +133 -297
  249. package/transform/__test__/defines.spec.js +30 -0
  250. package/transform/__test__/fixture.spec.js +11 -11
  251. package/transform/__test__/main-thread-object-compat.spec.js +111 -0
  252. package/transform/__test__/object-methods.spec.js +79 -0
  253. package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
  254. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
  255. package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
  256. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +63 -57
  257. package/transform/crates/swc_plugin_worklet/lib.rs +393 -589
  258. package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
  259. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
  260. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
  261. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +11 -6
  262. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +12 -6
  263. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +14 -5
  264. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +17 -6
  265. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
  266. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
  267. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
  268. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
  269. package/transform/dist/wasm.cjs +1 -1
  270. package/transform/package.json +0 -1
  271. package/transform/src/lib.rs +1 -1
  272. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +167 -69
  273. package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
  274. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  275. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +226 -132
  276. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
  277. package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
  278. package/runtime/lib/snapshot/worklet/hmr.js +0 -28
  279. package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
  280. package/runtime/src/snapshot/worklet/hmr.ts +0 -34
  281. /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
  282. /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
@@ -1,7 +1,6 @@
1
1
  #![allow(clippy::boxed_local)]
2
2
  use crate::extract_ident::ExtractingIdentsCollector;
3
3
  use crate::worklet_type::WorkletType;
4
- use crate::TransformMode;
5
4
  use std::collections::HashSet;
6
5
  use std::vec;
7
6
  use swc_core::common::{EqIgnoreSpan, DUMMY_SP};
@@ -37,7 +36,6 @@ impl VisitMut for CaptureRootReplacer {
37
36
  }
38
37
 
39
38
  struct RegisterWorkletParams<'a> {
40
- mode: TransformMode,
41
39
  target: TransformTarget,
42
40
  worklet_type: WorkletType,
43
41
  function_name: Ident,
@@ -46,7 +44,6 @@ struct RegisterWorkletParams<'a> {
46
44
  extracted_js_fns: Vec<(IdentName, Box<Expr>)>,
47
45
  hash: Expr,
48
46
  is_class_member: bool,
49
- named_imports: &'a mut HashSet<String>,
50
47
  requires_main_thread_runtime: &'a mut bool,
51
48
  register_worklet: &'a Ident,
52
49
  }
@@ -54,7 +51,6 @@ struct RegisterWorkletParams<'a> {
54
51
  impl StmtGen {
55
52
  #[allow(clippy::too_many_arguments)]
56
53
  pub fn transform_worklet(
57
- mode: TransformMode,
58
54
  worklet_type: WorkletType,
59
55
  hash: String,
60
56
  target: TransformTarget,
@@ -70,12 +66,16 @@ impl StmtGen {
70
66
  let hash = Expr::Lit(hash.into());
71
67
  let mut extracted_value = ident_collector.take_values();
72
68
  let mut extracted_this_expr = ident_collector.take_this_expr();
73
- if StmtGen::wrap_main_thread_object_candidates(&mut extracted_value, CaptureRoot::Ident)
74
- | StmtGen::wrap_main_thread_object_candidates(
75
- &mut extracted_this_expr,
76
- CaptureRoot::ThisMember,
77
- )
78
- {
69
+ let optional_member_sources = ident_collector.take_optional_member_sources();
70
+ if StmtGen::wrap_main_thread_object_candidates(
71
+ &mut extracted_value,
72
+ CaptureRoot::Ident,
73
+ &optional_member_sources,
74
+ ) | StmtGen::wrap_main_thread_object_candidates(
75
+ &mut extracted_this_expr,
76
+ CaptureRoot::ThisMember,
77
+ &optional_member_sources,
78
+ ) {
79
79
  named_imports.insert("captureMainThreadObject".into());
80
80
  }
81
81
  let extracted_idents = ident_collector.take_idents();
@@ -87,10 +87,8 @@ impl StmtGen {
87
87
  // that could turn an anonymous registration into a second worklet.
88
88
  let mut collected_function = function.clone();
89
89
  collected_function.body.as_mut().unwrap().stmts.remove(0);
90
- let mut collected_imports = HashSet::new();
91
90
  let mut collected_runtime_requirement = false;
92
91
  StmtGen::gen_register_worklet_stmt(RegisterWorkletParams {
93
- mode,
94
92
  target: TransformTarget::LEPUS,
95
93
  worklet_type: worklet_type.clone(),
96
94
  function_name: function_name.clone(),
@@ -99,7 +97,6 @@ impl StmtGen {
99
97
  extracted_js_fns: extracted_js_fns.clone(),
100
98
  hash: hash.clone(),
101
99
  is_class_member,
102
- named_imports: &mut collected_imports,
103
100
  requires_main_thread_runtime: &mut collected_runtime_requirement,
104
101
  register_worklet,
105
102
  })
@@ -119,7 +116,6 @@ impl StmtGen {
119
116
  named_imports,
120
117
  ),
121
118
  StmtGen::gen_register_worklet_stmt(RegisterWorkletParams {
122
- mode,
123
119
  target: if target == TransformTarget::MIXED {
124
120
  TransformTarget::LEPUS
125
121
  } else {
@@ -132,7 +128,6 @@ impl StmtGen {
132
128
  extracted_js_fns,
133
129
  hash,
134
130
  is_class_member,
135
- named_imports,
136
131
  requires_main_thread_runtime,
137
132
  register_worklet,
138
133
  }),
@@ -240,6 +235,7 @@ impl StmtGen {
240
235
  fn wrap_main_thread_object_candidates(
241
236
  extracted_value: &mut Box<Expr>,
242
237
  capture_root: CaptureRoot,
238
+ optional_member_sources: &[Box<Expr>],
243
239
  ) -> bool {
244
240
  let mut wrapped = false;
245
241
  let Some(object) = extracted_value.as_mut_object() else {
@@ -263,13 +259,21 @@ impl StmtGen {
263
259
  let Some(source) = source else {
264
260
  continue;
265
261
  };
266
- wrapped |= StmtGen::wrap_main_thread_object_candidate(&mut key_value.value, source);
262
+ wrapped |= StmtGen::wrap_main_thread_object_candidate(
263
+ &mut key_value.value,
264
+ source,
265
+ optional_member_sources,
266
+ );
267
267
  }
268
268
 
269
269
  wrapped
270
270
  }
271
271
 
272
- fn wrap_main_thread_object_candidate(value: &mut Box<Expr>, source: Expr) -> bool {
272
+ fn wrap_main_thread_object_candidate(
273
+ value: &mut Box<Expr>,
274
+ source: Expr,
275
+ optional_member_sources: &[Box<Expr>],
276
+ ) -> bool {
273
277
  let Some(object) = value.as_mut_object() else {
274
278
  return false;
275
279
  };
@@ -285,9 +289,16 @@ impl StmtGen {
285
289
  continue;
286
290
  }
287
291
  let child_source = StmtGen::append_member(source.clone(), &key_value.key);
288
- StmtGen::wrap_main_thread_object_candidate(&mut key_value.value, child_source);
292
+ StmtGen::wrap_main_thread_object_candidate(
293
+ &mut key_value.value,
294
+ child_source,
295
+ optional_member_sources,
296
+ );
289
297
  }
290
298
 
299
+ let is_optional = optional_member_sources
300
+ .iter()
301
+ .any(|optional_source| optional_source.as_ref().eq_ignore_span(&source));
291
302
  let mut fallback = value.clone();
292
303
  let source_argument = source.clone();
293
304
  let capture_argument = if source.is_ident() {
@@ -300,21 +311,28 @@ impl StmtGen {
300
311
  });
301
312
  Expr::Ident(capture_source)
302
313
  };
303
- let captured = CallExpr {
304
- ctxt: Default::default(),
305
- span: DUMMY_SP,
306
- args: vec![capture_argument.clone().into()],
307
- callee: Callee::Expr(quote_expr!("captureMainThreadObject")),
308
- type_args: None,
309
- }
310
- .into();
314
+ // Older externalized runtimes do not export this helper. Keep ordinary
315
+ // main-thread function captures working by using the narrowed fallback.
316
+ let captured = quote!(
317
+ "captureMainThreadObject?.($source)" as Expr,
318
+ source: Expr = capture_argument.clone(),
319
+ );
311
320
  let captured_or_fallback: Expr = BinExpr {
312
321
  span: DUMMY_SP,
313
322
  op: BinaryOp::NullishCoalescing,
314
- left: captured,
323
+ left: captured.into(),
315
324
  right: fallback,
316
325
  }
317
326
  .into();
327
+ let captured_or_fallback: Expr = if is_optional {
328
+ quote!(
329
+ "$src == null ? $src : $expr" as Expr,
330
+ src: Expr = capture_argument.clone(),
331
+ expr: Expr = captured_or_fallback,
332
+ )
333
+ } else {
334
+ captured_or_fallback
335
+ };
318
336
  *value = if source_argument.is_ident() {
319
337
  captured_or_fallback.into()
320
338
  } else {
@@ -407,7 +425,6 @@ impl StmtGen {
407
425
  */
408
426
  fn gen_register_worklet_stmt(params: RegisterWorkletParams) -> Stmt {
409
427
  let RegisterWorkletParams {
410
- mode,
411
428
  target,
412
429
  worklet_type,
413
430
  function_name,
@@ -416,11 +433,14 @@ impl StmtGen {
416
433
  extracted_js_fns,
417
434
  hash,
418
435
  is_class_member,
419
- named_imports,
420
436
  requires_main_thread_runtime,
421
437
  register_worklet,
422
438
  } = params;
423
439
 
440
+ if target != TransformTarget::LEPUS {
441
+ return EmptyStmt { span: DUMMY_SP }.into();
442
+ }
443
+
424
444
  let function_to_register = Box::new(StmtGen::gen_function_to_register(
425
445
  function,
426
446
  function_name,
@@ -430,35 +450,21 @@ impl StmtGen {
430
450
  is_class_member,
431
451
  ));
432
452
 
433
- if target == TransformTarget::LEPUS {
434
- *requires_main_thread_runtime = true;
435
- let registration = quote_expr!("$register($type_, $hash, $fn_)",
436
- register: Ident = register_worklet.clone(),
437
- type_: Expr = Expr::Lit(worklet_type.type_str().into()),
438
- hash: Expr = hash,
439
- fn_: Expr = Expr::Fn(FnExpr {
440
- ident: None,
441
- function: function_to_register,
442
- }),
443
- );
444
- ExprStmt {
445
- span: DUMMY_SP,
446
- expr: registration,
447
- }
448
- .into()
449
- } else if mode == TransformMode::Development {
450
- named_imports.insert("registerWorkletOnBackground".into());
451
- quote!("registerWorkletOnBackground($type_, $hash, $fn_)" as Stmt,
452
- type_: Expr = Expr::Lit(worklet_type.type_str().into()),
453
- hash: Expr = hash,
454
- fn_: Expr = Expr::Fn(FnExpr {
455
- ident: None,
456
- function: function_to_register,
457
- }),
458
- )
459
- } else {
460
- EmptyStmt { span: DUMMY_SP }.into()
453
+ *requires_main_thread_runtime = true;
454
+ let registration = quote_expr!("$register($type_, $hash, $fn_)",
455
+ register: Ident = register_worklet.clone(),
456
+ type_: Expr = Expr::Lit(worklet_type.type_str().into()),
457
+ hash: Expr = hash,
458
+ fn_: Expr = Expr::Fn(FnExpr {
459
+ ident: None,
460
+ function: function_to_register,
461
+ }),
462
+ );
463
+ ExprStmt {
464
+ span: DUMMY_SP,
465
+ expr: registration,
461
466
  }
467
+ .into()
462
468
  }
463
469
 
464
470
  fn gen_function_to_register(