@lynx-js/react 0.126.2 → 0.127.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 (190) hide show
  1. package/CHANGELOG.md +18 -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 +3 -2
  8. package/refresh/.turbo/turbo-build.log +1 -1
  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 +3 -0
  15. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +4 -0
  16. package/runtime/__test__/snapshot/ctxNotFound.test.ts +271 -0
  17. package/runtime/__test__/snapshot/event.test.jsx +7 -7
  18. package/runtime/__test__/snapshot/lazy.test.js +73 -1
  19. package/runtime/__test__/snapshot/lynx/portals.test.jsx +75 -37
  20. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +59 -47
  21. package/runtime/__test__/snapshot/spread.test.jsx +10 -10
  22. package/runtime/__test__/snapshot/worklet/main-thread-object.test.jsx +306 -0
  23. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +1 -0
  24. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +182 -0
  25. package/runtime/__test__/worklet-runtime/object-methods.test.js +60 -0
  26. package/runtime/__test__/worklet-runtime/workletRef.test.js +362 -2
  27. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +246 -1
  28. package/runtime/lazy/compat.js +2 -0
  29. package/runtime/lazy/element-template-internal.js +1 -0
  30. package/runtime/lazy/element-template.js +2 -0
  31. package/runtime/lazy/internal.js +2 -0
  32. package/runtime/lazy/react.js +2 -0
  33. package/runtime/lib/core/background-function/run-on-background.d.ts +16 -0
  34. package/runtime/lib/core/background-function/run-on-background.js +16 -0
  35. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  36. package/runtime/lib/core/capture-main-thread-object.d.ts +1 -0
  37. package/runtime/lib/core/capture-main-thread-object.js +16 -0
  38. package/runtime/lib/core/capture-main-thread-object.js.map +1 -0
  39. package/runtime/lib/core/globalProps.d.ts +2 -0
  40. package/runtime/lib/core/globalProps.js.map +1 -1
  41. package/runtime/lib/core/hooks/react.d.ts +4 -0
  42. package/runtime/lib/core/hooks/react.js +4 -0
  43. package/runtime/lib/core/hooks/react.js.map +1 -1
  44. package/runtime/lib/core/hooks/useLynxGlobalEventListener.d.ts +2 -0
  45. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js +2 -0
  46. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js.map +1 -1
  47. package/runtime/lib/core/initData.d.ts +2 -0
  48. package/runtime/lib/core/initData.js +2 -0
  49. package/runtime/lib/core/initData.js.map +1 -1
  50. package/runtime/lib/core/main-thread-object-handle-registry.d.ts +3 -0
  51. package/runtime/lib/core/main-thread-object-handle-registry.js +17 -0
  52. package/runtime/lib/core/main-thread-object-handle-registry.js.map +1 -0
  53. package/runtime/lib/core/main-thread-object.d.ts +77 -0
  54. package/runtime/lib/core/main-thread-object.js +261 -0
  55. package/runtime/lib/core/main-thread-object.js.map +1 -0
  56. package/runtime/lib/core/main-thread-ref-id.d.ts +1 -0
  57. package/runtime/lib/core/main-thread-ref-id.js +14 -0
  58. package/runtime/lib/core/main-thread-ref-id.js.map +1 -0
  59. package/runtime/lib/core/main-thread-ref-init-value.d.ts +1 -1
  60. package/runtime/lib/core/main-thread-ref-init-value.js +4 -2
  61. package/runtime/lib/core/main-thread-ref-init-value.js.map +1 -1
  62. package/runtime/lib/core/main-thread-ref.d.ts +4 -0
  63. package/runtime/lib/core/main-thread-ref.js +5 -8
  64. package/runtime/lib/core/main-thread-ref.js.map +1 -1
  65. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  66. package/runtime/lib/element-template/index.d.ts +2 -0
  67. package/runtime/lib/element-template/index.js +1 -0
  68. package/runtime/lib/element-template/index.js.map +1 -1
  69. package/runtime/lib/element-template/internal.d.ts +1 -0
  70. package/runtime/lib/element-template/internal.js +1 -0
  71. package/runtime/lib/element-template/internal.js.map +1 -1
  72. package/runtime/lib/element-template/protocol/types.d.ts +1 -1
  73. package/runtime/lib/internal.d.ts +1 -0
  74. package/runtime/lib/internal.js +1 -0
  75. package/runtime/lib/internal.js.map +1 -1
  76. package/runtime/lib/lynx-api.d.ts +34 -0
  77. package/runtime/lib/lynx-api.js +21 -0
  78. package/runtime/lib/lynx-api.js.map +1 -1
  79. package/runtime/lib/lynx.js +4 -1
  80. package/runtime/lib/lynx.js.map +1 -1
  81. package/runtime/lib/snapshot/lifecycle/patch/error.d.ts +11 -1
  82. package/runtime/lib/snapshot/lifecycle/patch/error.js +24 -16
  83. package/runtime/lib/snapshot/lifecycle/patch/error.js.map +1 -1
  84. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.d.ts +1 -1
  85. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +29 -7
  86. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  87. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +2 -2
  88. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  89. package/runtime/lib/snapshot/lynx/children.d.ts +2 -0
  90. package/runtime/lib/snapshot/lynx/children.js.map +1 -1
  91. package/runtime/lib/snapshot/lynx/element.d.ts +8 -0
  92. package/runtime/lib/snapshot/lynx/element.js +4 -0
  93. package/runtime/lib/snapshot/lynx/element.js.map +1 -1
  94. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +2 -0
  95. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +2 -0
  96. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  97. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  98. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  99. package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -3
  100. package/runtime/lib/worklet-runtime/bindings/bindings.js +9 -1
  101. package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
  102. package/runtime/lib/worklet-runtime/bindings/types.d.ts +3 -6
  103. package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
  104. package/runtime/lib/worklet-runtime/hydrate.js +26 -7
  105. package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
  106. package/runtime/lib/worklet-runtime/mainThreadObject.d.ts +8 -0
  107. package/runtime/lib/worklet-runtime/mainThreadObject.js +67 -0
  108. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -0
  109. package/runtime/lib/worklet-runtime/workletRef.d.ts +11 -7
  110. package/runtime/lib/worklet-runtime/workletRef.js +72 -9
  111. package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
  112. package/runtime/lib/worklet-runtime/workletRuntime.js +68 -9
  113. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  114. package/runtime/package.json +2 -3
  115. package/runtime/src/core/background-function/run-on-background.ts +16 -0
  116. package/runtime/src/core/capture-main-thread-object.ts +17 -0
  117. package/runtime/src/core/globalProps.ts +2 -0
  118. package/runtime/src/core/hooks/react.ts +4 -0
  119. package/runtime/src/core/hooks/useLynxGlobalEventListener.ts +2 -0
  120. package/runtime/src/core/initData.ts +2 -0
  121. package/runtime/src/core/main-thread-object-handle-registry.ts +25 -0
  122. package/runtime/src/core/main-thread-object.ts +387 -0
  123. package/runtime/src/core/main-thread-ref-id.ts +16 -0
  124. package/runtime/src/core/main-thread-ref-init-value.ts +12 -4
  125. package/runtime/src/core/main-thread-ref.ts +7 -8
  126. package/runtime/src/element-template/background/commit-hook.ts +1 -1
  127. package/runtime/src/element-template/index.ts +6 -0
  128. package/runtime/src/element-template/internal.ts +2 -0
  129. package/runtime/src/element-template/protocol/types.ts +1 -1
  130. package/runtime/src/internal.ts +1 -0
  131. package/runtime/src/lynx-api.ts +38 -0
  132. package/runtime/src/lynx.ts +5 -1
  133. package/runtime/src/snapshot/lifecycle/patch/error.ts +36 -19
  134. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +29 -7
  135. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +2 -2
  136. package/runtime/src/snapshot/lynx/children.ts +2 -0
  137. package/runtime/src/snapshot/lynx/element.ts +8 -0
  138. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +2 -0
  139. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +1 -1
  140. package/runtime/src/worklet-runtime/bindings/bindings.ts +17 -2
  141. package/runtime/src/worklet-runtime/bindings/types.ts +7 -11
  142. package/runtime/src/worklet-runtime/hydrate.ts +43 -10
  143. package/runtime/src/worklet-runtime/mainThreadObject.ts +117 -0
  144. package/runtime/src/worklet-runtime/workletRef.ts +120 -19
  145. package/runtime/src/worklet-runtime/workletRuntime.ts +83 -11
  146. package/runtime/worklet-runtime/dev.js +154 -17
  147. package/runtime/worklet-runtime/main.js +1 -1
  148. package/testing-library/.turbo/turbo-build.log +6 -6
  149. package/testing-library/dist/index.d.ts +165 -2
  150. package/testing-library/package.json +2 -1
  151. package/testing-library/src/__tests__/lynx.test.jsx +1 -1
  152. package/testing-library/src/__tests__/worklet.test.jsx +46 -1
  153. package/testing-library/tsconfig.docs.json +9 -0
  154. package/testing-library/typedoc.json +22 -0
  155. package/transform/.turbo/turbo-build$colon$wasm.log +150 -78
  156. package/transform/__test__/fixture.spec.js +72 -30
  157. package/transform/__test__/object-methods.spec.js +84 -0
  158. package/transform/crates/swc_plugin_worklet/decl_collect.rs +10 -0
  159. package/transform/crates/swc_plugin_worklet/extract_ident.rs +14 -0
  160. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +202 -3
  161. package/transform/crates/swc_plugin_worklet/lib.rs +468 -3
  162. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +8 -1
  163. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +10 -5
  164. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_js.js +27 -0
  165. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_lepus.js +45 -0
  166. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +16 -14
  167. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +16 -16
  168. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +7 -5
  169. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +7 -7
  170. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_computed_object_method_key_lepus.js +24 -0
  171. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_object_method_local_decls_lepus.js +16 -0
  172. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +42 -0
  173. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +58 -0
  174. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +3 -1
  175. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +3 -3
  176. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_js.js +9 -0
  177. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_lepus.js +16 -0
  178. package/transform/dist/wasm.cjs +1 -1
  179. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  180. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +66 -469
  181. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  182. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +364 -30
  183. package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
  184. package/transform/vitest.config.ts +5 -1
  185. package/types/react.docs.d.ts +130 -11
  186. package/testing-library/node_modules/.bin/rsbuild +0 -68
  187. package/testing-library/node_modules/.bin/rspeedy +0 -68
  188. package/testing-library/node_modules/.bin/rstest +0 -68
  189. package/transform/node_modules/.bin/esbuild +0 -59
  190. package/transform/node_modules/.bin/napi +0 -68
@@ -17,7 +17,7 @@ use swc_core::common::{errors::HANDLER, Span, Spanned, DUMMY_SP};
17
17
  use swc_core::ecma::ast::*;
18
18
  use swc_core::ecma::utils::{prepend_stmts, private_ident};
19
19
  use swc_core::ecma::visit::VisitMutWith;
20
- use swc_core::ecma::visit::{noop_visit_mut_type, VisitMut};
20
+ use swc_core::ecma::visit::{noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitWith};
21
21
  use swc_core::quote;
22
22
  use worklet_type::WorkletType;
23
23
 
@@ -80,6 +80,97 @@ impl Default for WorkletVisitor {
80
80
  impl VisitMut for WorkletVisitor {
81
81
  noop_visit_mut_type!();
82
82
 
83
+ fn visit_mut_prop(&mut self, n: &mut Prop) {
84
+ let Prop::Method(method) = n else {
85
+ n.visit_mut_children_with(self);
86
+ return;
87
+ };
88
+ let Some(body) = method.function.body.as_mut() else {
89
+ n.visit_mut_children_with(self);
90
+ return;
91
+ };
92
+ let Some(worklet_type) = self.check_is_worklet_block(body) else {
93
+ n.visit_mut_children_with(self);
94
+ return;
95
+ };
96
+
97
+ if contains_super_property(&method.function) {
98
+ emit_unsupported_object_method_super(method.span());
99
+ return;
100
+ }
101
+
102
+ let mut collector = ExtractingIdentsCollector::new(ExtractingIdentsCollectorConfig {
103
+ custom_global_ident_names: self.cfg.custom_global_ident_names.clone(),
104
+ shared_identifiers: Some(self.shared_identifiers.clone()),
105
+ });
106
+ method.visit_mut_with(&mut collector);
107
+
108
+ let should_use_getter =
109
+ collector.has_extracted_this_props() || collector.has_extracted_js_fns();
110
+
111
+ let hash = self.hasher.gen(&self.cfg.filename, &self.content_hash);
112
+ let collect_main_thread = self.defines_collector.is_some();
113
+ let collected_hash = collect_main_thread.then(|| hash.clone());
114
+ let (worklet_object_expr, register_worklet_stmt, main_thread_stmt) = StmtGen::transform_worklet(
115
+ self.mode,
116
+ worklet_type,
117
+ hash,
118
+ self.cfg.target,
119
+ Ident::dummy(),
120
+ method.function.clone(),
121
+ &mut collector,
122
+ false,
123
+ &mut self.named_imports,
124
+ self.worklet_runtime_loaded_ident.clone(),
125
+ collect_main_thread,
126
+ );
127
+
128
+ // Extracted `this.xxx` captures are spread into the ctx object. In an object literal,
129
+ // `this` in a plain key-value initializer refers to the enclosing lexical scope
130
+ // (`undefined` at ESM module top level), not the object being defined. Emitting a
131
+ // getter keeps `this` bound to the receiver on property read, matching the original
132
+ // method-call semantics (`obj.create()` has `this === obj`).
133
+ if should_use_getter {
134
+ *n = Prop::Getter(GetterProp {
135
+ span: method.span(),
136
+ key: method.key.clone(),
137
+ function: Function {
138
+ ctxt: Default::default(),
139
+ span: DUMMY_SP,
140
+ params: vec![],
141
+ decorators: vec![],
142
+ this_param: None,
143
+ body: Some(FunctionBody {
144
+ span: DUMMY_SP,
145
+ stmts: vec![ReturnStmt {
146
+ span: DUMMY_SP,
147
+ arg: Some(worklet_object_expr),
148
+ }
149
+ .into()],
150
+ }),
151
+ is_generator: false,
152
+ is_async: false,
153
+ type_params: None,
154
+ return_type: None,
155
+ }
156
+ .into(),
157
+ });
158
+ } else {
159
+ *n = Prop::KeyValue(KeyValueProp {
160
+ key: method.key.clone(),
161
+ value: worklet_object_expr,
162
+ });
163
+ }
164
+ self.collect_worklet_define(
165
+ collected_hash,
166
+ main_thread_stmt,
167
+ collector.saw_shared_identifiers(),
168
+ );
169
+ self
170
+ .stmts_to_insert_at_top_level
171
+ .push(register_worklet_stmt);
172
+ }
173
+
83
174
  fn visit_mut_class_member(&mut self, n: &mut ClassMember) {
84
175
  match n {
85
176
  ClassMember::Method(_) => {
@@ -710,6 +801,8 @@ impl VisitMut for WorkletVisitor {
710
801
  }
711
802
 
712
803
  const INVALID_RUNTIME_MSG: &str = "Invalid runtime value. Only 'shared' is supported.";
804
+ const UNSUPPORTED_OBJECT_METHOD_SUPER_MSG: &str =
805
+ "`super` is not supported in Main Thread object methods.";
713
806
 
714
807
  fn emit_invalid_runtime_error(span: Span) {
715
808
  HANDLER.with(|handler| {
@@ -717,6 +810,79 @@ fn emit_invalid_runtime_error(span: Span) {
717
810
  });
718
811
  }
719
812
 
813
+ fn emit_unsupported_object_method_super(span: Span) {
814
+ HANDLER.with(|handler| {
815
+ handler
816
+ .struct_span_err(span, UNSUPPORTED_OBJECT_METHOD_SUPER_MSG)
817
+ .emit();
818
+ });
819
+ }
820
+
821
+ #[derive(Default)]
822
+ struct SuperPropertyDetector {
823
+ found: bool,
824
+ }
825
+
826
+ impl Visit for SuperPropertyDetector {
827
+ noop_visit_type!();
828
+
829
+ fn visit_super_prop_expr(&mut self, _node: &SuperPropExpr) {
830
+ self.found = true;
831
+ }
832
+ // A nested object method has its own [[HomeObject]], so its `super` binding
833
+ // does not refer to the Main Thread object method being checked. Computed
834
+ // property names are evaluated in the enclosing scope and must still be
835
+ // visited because they can use the enclosing method's `super` binding.
836
+ fn visit_method_prop(&mut self, node: &MethodProp) {
837
+ if matches!(&node.key, PropName::Computed(_)) {
838
+ node.key.visit_with(self);
839
+ }
840
+ }
841
+
842
+ fn visit_getter_prop(&mut self, node: &GetterProp) {
843
+ if matches!(&node.key, PropName::Computed(_)) {
844
+ node.key.visit_with(self);
845
+ }
846
+ }
847
+
848
+ fn visit_setter_prop(&mut self, node: &SetterProp) {
849
+ if matches!(&node.key, PropName::Computed(_)) {
850
+ node.key.visit_with(self);
851
+ }
852
+ }
853
+
854
+ fn visit_class_method(&mut self, node: &ClassMethod) {
855
+ if matches!(&node.key, PropName::Computed(_)) {
856
+ node.key.visit_with(self);
857
+ }
858
+ }
859
+
860
+ fn visit_private_method(&mut self, _node: &PrivateMethod) {}
861
+
862
+ fn visit_class_prop(&mut self, node: &ClassProp) {
863
+ if matches!(&node.key, PropName::Computed(_)) {
864
+ node.key.visit_with(self);
865
+ }
866
+ }
867
+
868
+ fn visit_private_prop(&mut self, _node: &PrivateProp) {}
869
+
870
+ fn visit_constructor(&mut self, _node: &Constructor) {}
871
+
872
+ fn visit_static_block(&mut self, _node: &StaticBlock) {}
873
+
874
+ fn visit_auto_accessor(&mut self, node: &AutoAccessor) {
875
+ if let Key::Public(PropName::Computed(_)) = &node.key {
876
+ node.key.visit_with(self);
877
+ }
878
+ }
879
+ }
880
+
881
+ fn contains_super_property(function: &Function) -> bool {
882
+ let mut detector = SuperPropertyDetector::default();
883
+ function.visit_with(&mut detector);
884
+ detector.found
885
+ }
720
886
  fn validate_runtime_value(expr: &Expr) -> bool {
721
887
  match expr {
722
888
  Expr::Lit(Lit::Str(value)) => {
@@ -867,6 +1033,145 @@ function worklet(event: Event) {
867
1033
  "#
868
1034
  );
869
1035
 
1036
+ test!(
1037
+ module,
1038
+ Syntax::Typescript(TsSyntax {
1039
+ ..Default::default()
1040
+ }),
1041
+ |_| (
1042
+ resolver(Mark::new(), Mark::new(), true),
1043
+ visit_mut_pass(WorkletVisitor::new(
1044
+ TransformMode::Test,
1045
+ WorkletVisitorConfig {
1046
+ filename: "index.js".into(),
1047
+ target: TransformTarget::LEPUS,
1048
+ custom_global_ident_names: None,
1049
+ runtime_pkg: "@lynx-js/react".into(),
1050
+ }
1051
+ )),
1052
+ hygiene()
1053
+ ),
1054
+ should_not_capture_object_method_local_decls_lepus,
1055
+ r#"
1056
+ const valueType = defineMainThreadObjectType({
1057
+ type: '@test/value',
1058
+ create(initialValue) {
1059
+ "main thread";
1060
+ const state = { value: initialValue };
1061
+ return state;
1062
+ },
1063
+ });
1064
+ "#
1065
+ );
1066
+
1067
+ test!(
1068
+ module,
1069
+ Syntax::Typescript(TsSyntax {
1070
+ ..Default::default()
1071
+ }),
1072
+ |_| (
1073
+ resolver(Mark::new(), Mark::new(), true),
1074
+ visit_mut_pass(WorkletVisitor::new(
1075
+ TransformMode::Test,
1076
+ WorkletVisitorConfig {
1077
+ filename: "index.js".into(),
1078
+ target: TransformTarget::LEPUS,
1079
+ custom_global_ident_names: None,
1080
+ runtime_pkg: "@lynx-js/react".into(),
1081
+ }
1082
+ )),
1083
+ hygiene()
1084
+ ),
1085
+ should_not_capture_computed_object_method_key_lepus,
1086
+ r#"
1087
+ const name = 'create';
1088
+ const nestedName = 'read';
1089
+ const valueType = defineMainThreadObjectType({
1090
+ type: '@test/value',
1091
+ [name](initialValue) {
1092
+ "main thread";
1093
+ return { value: initialValue, [nestedName](nestedName) { return nestedName; } };
1094
+ },
1095
+ });
1096
+ "#
1097
+ );
1098
+
1099
+ test!(
1100
+ module,
1101
+ Syntax::Typescript(TsSyntax {
1102
+ ..Default::default()
1103
+ }),
1104
+ |_| (
1105
+ resolver(Mark::new(), Mark::new(), true),
1106
+ visit_mut_pass(WorkletVisitor::new(
1107
+ TransformMode::Test,
1108
+ WorkletVisitorConfig {
1109
+ filename: "index.js".into(),
1110
+ target: TransformTarget::LEPUS,
1111
+ custom_global_ident_names: None,
1112
+ runtime_pkg: "@lynx-js/react".into(),
1113
+ }
1114
+ )),
1115
+ hygiene()
1116
+ ),
1117
+ should_expose_object_method_capture_channels_lepus,
1118
+ r#"
1119
+ const callback = () => {};
1120
+ const valueType = defineMainThreadObjectType({
1121
+ type: '@test/capturing-value',
1122
+ helper: 1,
1123
+ callback,
1124
+ callbackOnly() {
1125
+ "main thread";
1126
+ runOnBackground(this.callback)();
1127
+ },
1128
+ create(initialValue: number) {
1129
+ "main thread";
1130
+ runOnBackground(callback)();
1131
+ return { value: initialValue + this.helper };
1132
+ },
1133
+ });
1134
+ "#
1135
+ );
1136
+
1137
+ test!(
1138
+ module,
1139
+ Syntax::Typescript(TsSyntax {
1140
+ ..Default::default()
1141
+ }),
1142
+ |_| (
1143
+ resolver(Mark::new(), Mark::new(), true),
1144
+ visit_mut_pass(WorkletVisitor::new(
1145
+ TransformMode::Test,
1146
+ WorkletVisitorConfig {
1147
+ filename: "index.js".into(),
1148
+ target: TransformTarget::JS,
1149
+ custom_global_ident_names: None,
1150
+ runtime_pkg: "@lynx-js/react".into(),
1151
+ }
1152
+ )),
1153
+ hygiene()
1154
+ ),
1155
+ should_expose_object_method_capture_channels_js,
1156
+ r#"
1157
+ const callback = () => {};
1158
+ const valueType = defineMainThreadObjectType({
1159
+ type: '@test/capturing-value',
1160
+ helper: 1,
1161
+ callback,
1162
+ callbackOnly() {
1163
+ "main thread";
1164
+ runOnBackground(this.callback)();
1165
+ },
1166
+ create(initialValue: number) {
1167
+ "main thread";
1168
+ runOnBackground(callback)();
1169
+ return { value: initialValue + this.helper };
1170
+ },
1171
+ });
1172
+ "#
1173
+ );
1174
+
870
1175
  test!(
871
1176
  module,
872
1177
  Syntax::Typescript(TsSyntax {
@@ -993,6 +1298,102 @@ function X(event) {
993
1298
  "#
994
1299
  );
995
1300
 
1301
+ test!(
1302
+ module,
1303
+ Syntax::Typescript(TsSyntax {
1304
+ ..Default::default()
1305
+ }),
1306
+ |_| (
1307
+ resolver(Mark::new(), Mark::new(), true),
1308
+ visit_mut_pass(WorkletVisitor::new(
1309
+ TransformMode::Test,
1310
+ WorkletVisitorConfig {
1311
+ filename: "index.ts".into(),
1312
+ target: TransformTarget::JS,
1313
+ custom_global_ident_names: None,
1314
+ runtime_pkg: "@lynx-js/react".into(),
1315
+ }
1316
+ )),
1317
+ hygiene()
1318
+ ),
1319
+ should_preserve_main_thread_objects_in_class_js,
1320
+ r#"
1321
+ class App extends Component {
1322
+ value: MotionValue<number>;
1323
+ ref: MainThreadRef<number>;
1324
+ static value: MotionValue<number>;
1325
+
1326
+ onTap() {
1327
+ "main thread";
1328
+ this.value.get();
1329
+ this.props.value.get();
1330
+ this.value.set(1);
1331
+ this.value?.get();
1332
+ this.value["get"]();
1333
+ return this.ref.current;
1334
+ }
1335
+
1336
+ onMove = () => {
1337
+ "main thread";
1338
+ return this.value.get();
1339
+ };
1340
+
1341
+ static onStatic() {
1342
+ "main thread";
1343
+ return this.value.get();
1344
+ }
1345
+ }
1346
+ "#
1347
+ );
1348
+
1349
+ test!(
1350
+ module,
1351
+ Syntax::Typescript(TsSyntax {
1352
+ ..Default::default()
1353
+ }),
1354
+ |_| (
1355
+ resolver(Mark::new(), Mark::new(), true),
1356
+ visit_mut_pass(WorkletVisitor::new(
1357
+ TransformMode::Test,
1358
+ WorkletVisitorConfig {
1359
+ filename: "index.ts".into(),
1360
+ target: TransformTarget::LEPUS,
1361
+ custom_global_ident_names: None,
1362
+ runtime_pkg: "@lynx-js/react".into(),
1363
+ }
1364
+ )),
1365
+ hygiene()
1366
+ ),
1367
+ should_preserve_main_thread_objects_in_class_lepus,
1368
+ r#"
1369
+ class App extends Component {
1370
+ value: MotionValue<number>;
1371
+ ref: MainThreadRef<number>;
1372
+ static value: MotionValue<number>;
1373
+
1374
+ onTap() {
1375
+ "main thread";
1376
+ this.value.get();
1377
+ this.props.value.get();
1378
+ this.value.set(1);
1379
+ this.value?.get();
1380
+ this.value["get"]();
1381
+ return this.ref.current;
1382
+ }
1383
+
1384
+ onMove = () => {
1385
+ "main thread";
1386
+ return this.value.get();
1387
+ };
1388
+
1389
+ static onStatic() {
1390
+ "main thread";
1391
+ return this.value.get();
1392
+ }
1393
+ }
1394
+ "#
1395
+ );
1396
+
996
1397
  test!(
997
1398
  module,
998
1399
  Syntax::Es(EsSyntax {
@@ -1354,6 +1755,70 @@ let X = function (event) {
1354
1755
  "#
1355
1756
  );
1356
1757
 
1758
+ test!(
1759
+ module,
1760
+ Syntax::Typescript(TsSyntax {
1761
+ ..Default::default()
1762
+ }),
1763
+ |_| (
1764
+ resolver(Mark::new(), Mark::new(), true),
1765
+ visit_mut_pass(WorkletVisitor::new(
1766
+ TransformMode::Test,
1767
+ WorkletVisitorConfig {
1768
+ filename: "index.js".into(),
1769
+ target: TransformTarget::LEPUS,
1770
+ custom_global_ident_names: None,
1771
+ runtime_pkg: "@lynx-js/react".into(),
1772
+ }
1773
+ )),
1774
+ hygiene()
1775
+ ),
1776
+ should_transform_object_methods_lepus,
1777
+ r#"
1778
+ import { create } from './shared.js' with { runtime: "shared" };
1779
+
1780
+ const valueType = defineMainThreadObjectType({
1781
+ type: '@test/value',
1782
+ create(initialValue: number) {
1783
+ "main thread";
1784
+ return create(initialValue);
1785
+ },
1786
+ });
1787
+ "#
1788
+ );
1789
+
1790
+ test!(
1791
+ module,
1792
+ Syntax::Typescript(TsSyntax {
1793
+ ..Default::default()
1794
+ }),
1795
+ |_| (
1796
+ resolver(Mark::new(), Mark::new(), true),
1797
+ visit_mut_pass(WorkletVisitor::new(
1798
+ TransformMode::Test,
1799
+ WorkletVisitorConfig {
1800
+ filename: "index.js".into(),
1801
+ target: TransformTarget::JS,
1802
+ custom_global_ident_names: None,
1803
+ runtime_pkg: "@lynx-js/react".into(),
1804
+ }
1805
+ )),
1806
+ hygiene()
1807
+ ),
1808
+ should_transform_object_methods_js,
1809
+ r#"
1810
+ import { create } from './shared.js' with { runtime: "shared" };
1811
+
1812
+ const valueType = defineMainThreadObjectType({
1813
+ type: '@test/value',
1814
+ create(initialValue: number) {
1815
+ "main thread";
1816
+ return create(initialValue);
1817
+ },
1818
+ });
1819
+ "#
1820
+ );
1821
+
1357
1822
  test!(
1358
1823
  module,
1359
1824
  Syntax::Es(EsSyntax {
@@ -1846,7 +2311,7 @@ class App extends Component {
1846
2311
  let a = 123;
1847
2312
  const b = [ a, ...y1];
1848
2313
  const c = { a, y2, ...y3, ...{ d: 233, e: y4 } };
1849
- return y5.r;
2314
+ return y5.r + props.value.get();
1850
2315
  }
1851
2316
  "#
1852
2317
  );
@@ -1876,7 +2341,7 @@ class App extends Component {
1876
2341
  let a = 123;
1877
2342
  const b = [ a, ...y1];
1878
2343
  const c = { a, y2, ...y3, ...{ d: 233, e: y4 } };
1879
- return y5.r;
2344
+ return y5.r + props.value.get();
1880
2345
  }
1881
2346
  "#
1882
2347
  );
@@ -1,11 +1,18 @@
1
+ import { captureMainThreadObject as __captureMainThreadObject } from "@lynx-js/react";
2
+ var captureMainThreadObject = __captureMainThreadObject;
1
3
  let Y = {
2
4
  _c: {
3
5
  y1,
4
6
  y2,
5
7
  y3,
6
8
  y4,
7
- y5: {
9
+ y5: captureMainThreadObject(y5) ?? {
8
10
  r: y5.r
11
+ },
12
+ props: captureMainThreadObject(props) ?? {
13
+ value: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
14
+ get: __mainThreadObjectSource.get
15
+ })(props.value)
9
16
  }
10
17
  },
11
18
  _wkltId: "a77b:test:1"
@@ -1,13 +1,18 @@
1
- import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react";
2
- var loadWorkletRuntime = __loadWorkletRuntime;
1
+ import { captureMainThreadObject as __captureMainThreadObject, loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react";
2
+ var captureMainThreadObject = __captureMainThreadObject, loadWorkletRuntime = __loadWorkletRuntime;
3
3
  let Y = {
4
4
  _c: {
5
5
  y1,
6
6
  y2,
7
7
  y3,
8
8
  y4,
9
- y5: {
9
+ y5: captureMainThreadObject(y5) ?? {
10
10
  r: y5.r
11
+ },
12
+ props: captureMainThreadObject(props) ?? {
13
+ value: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
14
+ get: __mainThreadObjectSource.get
15
+ })(props.value)
11
16
  }
12
17
  },
13
18
  _wkltId: "a77b:test:1"
@@ -15,7 +20,7 @@ let Y = {
15
20
  const __workletRuntimeLoaded = loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry);
16
21
  __workletRuntimeLoaded && registerWorkletInternal("main-thread", "a77b:test:1", function() {
17
22
  const Y = lynxWorkletImpl._workletMap["a77b:test:1"].bind(this);
18
- let { y1, y2, y3, y4, y5 } = this["_c"];
23
+ let { y1, y2, y3, y4, y5, props } = this["_c"];
19
24
  "main thread";
20
25
  let a = 123;
21
26
  const b = [
@@ -31,5 +36,5 @@ __workletRuntimeLoaded && registerWorkletInternal("main-thread", "a77b:test:1",
31
36
  e: y4
32
37
  }
33
38
  };
34
- return y5.r;
39
+ return y5.r + props.value.get();
35
40
  });
@@ -0,0 +1,27 @@
1
+ import { transformToWorklet as __transformToWorklet } from "@lynx-js/react";
2
+ var transformToWorklet = __transformToWorklet;
3
+ const callback = ()=>{};
4
+ const valueType = defineMainThreadObjectType({
5
+ type: '@test/capturing-value',
6
+ helper: 1,
7
+ callback,
8
+ get callbackOnly () {
9
+ return {
10
+ _wkltId: "a77b:test:1",
11
+ _jsFn: {
12
+ _jsFn1: transformToWorklet(this.callback)
13
+ }
14
+ };
15
+ },
16
+ get create () {
17
+ return {
18
+ _wkltId: "a77b:test:2",
19
+ _jsFn: {
20
+ _jsFn1: transformToWorklet(callback)
21
+ },
22
+ ...{
23
+ helper: this.helper
24
+ }
25
+ };
26
+ }
27
+ });
@@ -0,0 +1,45 @@
1
+ import { loadWorkletRuntime as __loadWorkletRuntime } from "@lynx-js/react";
2
+ var loadWorkletRuntime = __loadWorkletRuntime;
3
+ const callback = ()=>{};
4
+ const valueType = defineMainThreadObjectType({
5
+ type: '@test/capturing-value',
6
+ helper: 1,
7
+ callback,
8
+ get callbackOnly () {
9
+ return {
10
+ _wkltId: "a77b:test:1",
11
+ _jsFn: {
12
+ _jsFn1: {
13
+ _isFirstScreen: true
14
+ }
15
+ }
16
+ };
17
+ },
18
+ get create () {
19
+ return {
20
+ _wkltId: "a77b:test:2",
21
+ _jsFn: {
22
+ _jsFn1: {
23
+ _isFirstScreen: true
24
+ }
25
+ },
26
+ ...{
27
+ helper: this.helper
28
+ }
29
+ };
30
+ }
31
+ });
32
+ const __workletRuntimeLoaded = loadWorkletRuntime(typeof globDynamicComponentEntry === 'undefined' ? undefined : globDynamicComponentEntry);
33
+ __workletRuntimeLoaded && registerWorkletInternal("main-thread", "a77b:test:1", function() {
34
+ let { _jsFn1 } = this["_jsFn"];
35
+ "main thread";
36
+ runOnBackground(_jsFn1)();
37
+ });
38
+ __workletRuntimeLoaded && registerWorkletInternal("main-thread", "a77b:test:2", function(initialValue: number) {
39
+ let { _jsFn1 } = this["_jsFn"];
40
+ "main thread";
41
+ runOnBackground(_jsFn1)();
42
+ return {
43
+ value: initialValue + this.helper
44
+ };
45
+ });
@@ -1,3 +1,5 @@
1
+ import { captureMainThreadObject as __captureMainThreadObject } from "@lynx-js/react";
2
+ var captureMainThreadObject = __captureMainThreadObject;
1
3
  let onTapLepus = {
2
4
  _c: {
3
5
  aaaa,
@@ -8,22 +10,22 @@ let onTapLepus = {
8
10
  _wkltId: "a123:test:1",
9
11
  ...{
10
12
  aaaa: this.aaaa,
11
- bbbb: {
12
- cccc: {
13
- dddd: this.bbbb.cccc.dddd
14
- }
15
- },
13
+ bbbb: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
14
+ cccc: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
15
+ dddd: __mainThreadObjectSource.dddd
16
+ })(__mainThreadObjectSource.cccc)
17
+ })(this.bbbb),
16
18
  eeee: this.eeee,
17
19
  ffff: this.ffff,
18
- hhhh: {
19
- 'iiii': this.hhhh['iiii'],
20
- kkkk: this.hhhh.kkkk
21
- },
20
+ hhhh: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
21
+ 'iiii': __mainThreadObjectSource['iiii'],
22
+ kkkk: __mainThreadObjectSource.kkkk
23
+ })(this.hhhh),
22
24
  llll: this.llll,
23
- mmmm: {
24
- nnnn: {
25
- 'oooo': this.mmmm.nnnn['oooo']
26
- }
27
- }
25
+ mmmm: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
26
+ nnnn: ((__mainThreadObjectSource)=>captureMainThreadObject(__mainThreadObjectSource) ?? {
27
+ 'oooo': __mainThreadObjectSource['oooo']
28
+ })(__mainThreadObjectSource.nnnn)
29
+ })(this.mmmm)
28
30
  }
29
31
  };