@lynx-js/react 0.129.0 → 0.129.1
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.
- package/CHANGELOG.md +14 -0
- package/components/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/refresh/.turbo/turbo-build.log +1 -1
- package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
- package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
- package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
- package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
- package/runtime/__test__/element-template/native/mts-destroy.test.ts +83 -0
- package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +3 -0
- package/runtime/__test__/element-template/native/reload.test.ts +31 -2
- package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
- package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +197 -7
- package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
- package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
- package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
- package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +121 -26
- package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
- package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
- package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
- package/runtime/__test__/shared/component-stack.test.ts +211 -0
- package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
- package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
- package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
- package/runtime/__test__/snapshot/preact.test.jsx +0 -3
- package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
- package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +12 -0
- package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
- package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
- package/runtime/lazy/element-template-internal.js +0 -1
- package/runtime/lazy/internal.js +1 -1
- package/runtime/lazy/jsx-dev-runtime.js +1 -1
- package/runtime/lazy/jsx-runtime.js +1 -1
- package/runtime/lazy/legacy-react-runtime.js +1 -1
- package/runtime/lazy/react-lepus.js +1 -1
- package/runtime/lazy/react.js +1 -1
- package/runtime/lib/core/capture-main-thread-object.js +2 -1
- package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
- package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
- package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
- package/runtime/lib/core/lynx/lazy-bundle.js +1 -1
- package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
- package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
- package/runtime/lib/core/lynx-data-processors.js +3 -0
- package/runtime/lib/core/lynx-data-processors.js.map +1 -1
- package/runtime/lib/core/runtime-destroy.js +1 -1
- package/runtime/lib/core/runtime-destroy.js.map +1 -1
- package/runtime/lib/document.js +0 -5
- package/runtime/lib/document.js.map +1 -1
- package/runtime/lib/element-template/background/commit-hook.js +11 -12
- package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
- package/runtime/lib/element-template/client/root.d.ts +1 -1
- package/runtime/lib/element-template/client/root.js.map +1 -1
- package/runtime/lib/element-template/index.d.ts +2 -2
- package/runtime/lib/element-template/index.js.map +1 -1
- package/runtime/lib/element-template/internal.d.ts +1 -1
- package/runtime/lib/element-template/internal.js +0 -13
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/lynx/env.js.map +1 -1
- package/runtime/lib/element-template/native/flush.d.ts +1 -0
- package/runtime/lib/element-template/native/flush.js +9 -0
- package/runtime/lib/element-template/native/flush.js.map +1 -0
- package/runtime/lib/element-template/native/main-thread-api.js +3 -7
- package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
- package/runtime/lib/element-template/native/mts-destroy.js +8 -2
- package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
- package/runtime/lib/element-template/native/patch-listener.js +57 -50
- package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
- package/runtime/lib/element-template/native/reload-main-thread.js +2 -1
- package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
- package/runtime/lib/element-template/protocol/types.js.map +1 -1
- package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
- package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
- package/runtime/lib/element-template/runtime/patch.js +4 -0
- package/runtime/lib/element-template/runtime/patch.js.map +1 -1
- package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
- package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
- package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
- package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +1 -0
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +41 -21
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
- package/runtime/lib/internal.d.ts +1 -1
- package/runtime/lib/internal.js +1 -1
- package/runtime/lib/internal.js.map +1 -1
- package/runtime/lib/lynx-api.d.ts +2 -125
- package/runtime/lib/lynx-api.js.map +1 -1
- package/runtime/lib/shared/component-stack.d.ts +0 -4
- package/runtime/lib/shared/component-stack.js +21 -5
- package/runtime/lib/shared/component-stack.js.map +1 -1
- package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
- package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
- package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
- package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
- package/runtime/lib/snapshot/lynx/calledByNative.js +11 -8
- package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
- package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
- package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
- package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
- package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
- package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
- package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
- package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +6 -0
- package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
- package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
- package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
- package/runtime/package.json +2 -3
- package/runtime/src/core/capture-main-thread-object.ts +2 -1
- package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
- package/runtime/src/core/lynx/lazy-bundle.ts +1 -1
- package/runtime/src/core/lynx-data-processors.ts +124 -1
- package/runtime/src/core/runtime-destroy.ts +1 -1
- package/runtime/src/document.ts +0 -6
- package/runtime/src/element-template/background/commit-hook.ts +18 -12
- package/runtime/src/element-template/client/root.ts +1 -3
- package/runtime/src/element-template/index.ts +2 -2
- package/runtime/src/element-template/internal.ts +0 -20
- package/runtime/src/element-template/lynx/env.ts +1 -1
- package/runtime/src/element-template/native/flush.ts +10 -0
- package/runtime/src/element-template/native/main-thread-api.ts +3 -6
- package/runtime/src/element-template/native/mts-destroy.ts +7 -2
- package/runtime/src/element-template/native/patch-listener.ts +62 -57
- package/runtime/src/element-template/native/reload-main-thread.ts +2 -1
- package/runtime/src/element-template/protocol/types.ts +0 -13
- package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
- package/runtime/src/element-template/runtime/patch.ts +5 -0
- package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
- package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
- package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
- package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +51 -21
- package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +2 -2
- package/runtime/src/element-template/types.d.ts +6 -0
- package/runtime/src/internal.ts +1 -1
- package/runtime/src/lynx-api.ts +2 -124
- package/runtime/src/shared/component-stack.ts +22 -5
- package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
- package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
- package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
- package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
- package/runtime/src/snapshot/lynx/calledByNative.ts +11 -9
- package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
- package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
- package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
- package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
- package/runtime/src/worklet-runtime/README.md +30 -10
- package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
- package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
- package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +6 -0
- package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
- package/runtime/vitest.config.ts +0 -2
- package/testing-library/.turbo/turbo-build.log +6 -6
- package/testing-library/src/__tests__/act.test.jsx +0 -7
- package/testing-library/src/__tests__/alog.test.jsx +12 -54
- package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
- package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
- package/testing-library/src/__tests__/worklet.test.jsx +49 -0
- package/transform/.turbo/turbo-build$colon$wasm.log +98 -376
- package/transform/__test__/defines.spec.js +30 -0
- package/transform/__test__/object-methods.spec.js +79 -0
- package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
- package/transform/crates/swc_plugin_worklet/gen_stmt.rs +56 -48
- package/transform/crates/swc_plugin_worklet/lib.rs +393 -589
- package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +6 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +9 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +12 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +1 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +1 -1
- package/transform/dist/wasm.cjs +1 -1
- package/transform/package.json +0 -1
- package/transform/src/lib.rs +1 -1
- package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +113 -42
- package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
- package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
- package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +273 -66
- package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
- package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
- package/runtime/lib/snapshot/worklet/hmr.js +0 -28
- package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
- package/runtime/src/snapshot/worklet/hmr.ts +0 -34
- /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
- /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
|
@@ -79,4 +79,83 @@ describe('object method captures', () => {
|
|
|
79
79
|
expect(Object.getOwnPropertyDescriptor(valueType, 'create').get.call(other)._jsFn._jsFn1)
|
|
80
80
|
.toBe(other.callback);
|
|
81
81
|
});
|
|
82
|
+
|
|
83
|
+
it('preserves nullish bases and guards when narrowing optional member captures', async () => {
|
|
84
|
+
const result = await transformReactLynx(
|
|
85
|
+
`
|
|
86
|
+
export function create(enabled, handlers) {
|
|
87
|
+
return () => {
|
|
88
|
+
'main thread';
|
|
89
|
+
if (enabled && handlers?.start) handlers.start();
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function createNested(enabled, handlers) {
|
|
94
|
+
return () => {
|
|
95
|
+
'main thread';
|
|
96
|
+
if (enabled && handlers?.nested?.start) handlers.nested.start();
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
`,
|
|
100
|
+
options('LEPUS'),
|
|
101
|
+
);
|
|
102
|
+
const functions = new Map();
|
|
103
|
+
const { create, createNested } = new Function(
|
|
104
|
+
'__registerWorklet',
|
|
105
|
+
'__captureMainThreadObject',
|
|
106
|
+
`${result.code.replace(/^import .*;$/gm, '').replace(/^export /gm, '')}\nreturn { create, createNested };`,
|
|
107
|
+
)(
|
|
108
|
+
(_type, id, fn) => functions.set(id, fn),
|
|
109
|
+
() => undefined,
|
|
110
|
+
);
|
|
111
|
+
const invoke = worklet => functions.get(worklet._wkltId).call(worklet);
|
|
112
|
+
|
|
113
|
+
const withNullDisabled = create(false, null);
|
|
114
|
+
expect(withNullDisabled._c.handlers).toBeNull();
|
|
115
|
+
expect(() => invoke(withNullDisabled)).not.toThrow();
|
|
116
|
+
|
|
117
|
+
const withUndefinedDisabled = create(false, undefined);
|
|
118
|
+
expect(withUndefinedDisabled._c.handlers).toBeUndefined();
|
|
119
|
+
expect(() => invoke(withUndefinedDisabled)).not.toThrow();
|
|
120
|
+
|
|
121
|
+
const withNullEnabled = create(true, null);
|
|
122
|
+
expect(withNullEnabled._c.handlers).toBeNull();
|
|
123
|
+
expect(() => invoke(withNullEnabled)).not.toThrow();
|
|
124
|
+
|
|
125
|
+
const withUndefinedEnabled = create(true, undefined);
|
|
126
|
+
expect(withUndefinedEnabled._c.handlers).toBeUndefined();
|
|
127
|
+
expect(() => invoke(withUndefinedEnabled)).not.toThrow();
|
|
128
|
+
|
|
129
|
+
let started = 0;
|
|
130
|
+
const handlers = {
|
|
131
|
+
start() {
|
|
132
|
+
started += 1;
|
|
133
|
+
},
|
|
134
|
+
extra: 'unreferenced',
|
|
135
|
+
};
|
|
136
|
+
const withHandlersDisabled = create(false, handlers);
|
|
137
|
+
expect(withHandlersDisabled._c.handlers).toEqual({ start: handlers.start });
|
|
138
|
+
expect('extra' in withHandlersDisabled._c.handlers).toBe(false);
|
|
139
|
+
invoke(withHandlersDisabled);
|
|
140
|
+
expect(started).toBe(0);
|
|
141
|
+
|
|
142
|
+
const withHandlersEnabled = create(true, handlers);
|
|
143
|
+
invoke(withHandlersEnabled);
|
|
144
|
+
expect(started).toBe(1);
|
|
145
|
+
|
|
146
|
+
const withNestedNull = createNested(true, { nested: null, extra: 1 });
|
|
147
|
+
expect(withNestedNull._c.handlers).toEqual({ nested: null });
|
|
148
|
+
expect(() => invoke(withNestedNull)).not.toThrow();
|
|
149
|
+
|
|
150
|
+
const withNestedUndefined = createNested(true, { nested: undefined, extra: 1 });
|
|
151
|
+
expect(withNestedUndefined._c.handlers).toEqual({ nested: undefined });
|
|
152
|
+
expect(() => invoke(withNestedUndefined)).not.toThrow();
|
|
153
|
+
|
|
154
|
+
const withNestedHandlers = createNested(true, { nested: handlers, extra: 1 });
|
|
155
|
+
expect(withNestedHandlers._c.handlers).toEqual({
|
|
156
|
+
nested: { start: handlers.start },
|
|
157
|
+
});
|
|
158
|
+
invoke(withNestedHandlers);
|
|
159
|
+
expect(started).toBe(2);
|
|
160
|
+
});
|
|
82
161
|
});
|
|
@@ -6,7 +6,7 @@ use std::mem::{swap, take};
|
|
|
6
6
|
use std::ops::Deref;
|
|
7
7
|
use std::{borrow::Cow, cmp::max};
|
|
8
8
|
use swc_core::common::util::take::Take;
|
|
9
|
-
use swc_core::common::EqIgnoreSpan;
|
|
9
|
+
use swc_core::common::{EqIgnoreSpan, DUMMY_SP};
|
|
10
10
|
use swc_core::ecma::ast::*;
|
|
11
11
|
use swc_core::ecma::utils::quote_ident;
|
|
12
12
|
use swc_core::ecma::visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
|
|
@@ -43,6 +43,7 @@ pub struct ExtractingIdentsCollector {
|
|
|
43
43
|
member_expr_depth: u32,
|
|
44
44
|
// when `d` in `a.b.c.d` is visited, `member_expr_path` should be [a, b, c].
|
|
45
45
|
member_expr_path: Vec<Box<Expr>>,
|
|
46
|
+
optional_member_sources: Vec<Box<Expr>>,
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
impl ExtractingIdentsCollector {
|
|
@@ -63,9 +64,14 @@ impl ExtractingIdentsCollector {
|
|
|
63
64
|
}],
|
|
64
65
|
member_expr_depth: 0,
|
|
65
66
|
member_expr_path: vec![],
|
|
67
|
+
optional_member_sources: vec![],
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
70
|
|
|
71
|
+
pub fn take_optional_member_sources(&mut self) -> Vec<Box<Expr>> {
|
|
72
|
+
take(&mut self.optional_member_sources)
|
|
73
|
+
}
|
|
74
|
+
|
|
69
75
|
pub fn take_values(&mut self) -> Box<Expr> {
|
|
70
76
|
self.values_extracted.take()
|
|
71
77
|
}
|
|
@@ -116,7 +122,7 @@ impl ExtractingIdentsCollector {
|
|
|
116
122
|
.contains(&String::from(s)))
|
|
117
123
|
}
|
|
118
124
|
|
|
119
|
-
fn add_if_needed(&mut self, name: Box<Expr
|
|
125
|
+
fn add_if_needed(&mut self, name: Box<Expr>) {
|
|
120
126
|
assert!(name.is_ident() || name.is_lit() || name.is_this());
|
|
121
127
|
|
|
122
128
|
if self.member_expr_path.is_empty()
|
|
@@ -143,15 +149,16 @@ impl ExtractingIdentsCollector {
|
|
|
143
149
|
}
|
|
144
150
|
|
|
145
151
|
if !name.is_this() {
|
|
152
|
+
let expr_to_extract = Self::expr_from_path(&self.member_expr_path);
|
|
146
153
|
if !self.member_expr_path.is_empty() && self.member_expr_path[0].is_this() {
|
|
147
154
|
Self::add_inner(
|
|
148
|
-
expr_to_extract,
|
|
155
|
+
&expr_to_extract,
|
|
149
156
|
&mut self.this_expr_to_extract,
|
|
150
157
|
&self.member_expr_path[1..],
|
|
151
158
|
);
|
|
152
159
|
} else {
|
|
153
160
|
Self::add_inner(
|
|
154
|
-
expr_to_extract,
|
|
161
|
+
&expr_to_extract,
|
|
155
162
|
&mut self.values_extracted,
|
|
156
163
|
&self.member_expr_path,
|
|
157
164
|
)
|
|
@@ -247,6 +254,85 @@ impl ExtractingIdentsCollector {
|
|
|
247
254
|
})
|
|
248
255
|
}
|
|
249
256
|
|
|
257
|
+
fn is_extractable_member_obj(expr: &Expr) -> bool {
|
|
258
|
+
match expr {
|
|
259
|
+
Expr::Ident(_) | Expr::Member(_) | Expr::This(_) => true,
|
|
260
|
+
Expr::OptChain(opt_chain) => matches!(&*opt_chain.base, OptChainBase::Member(_)),
|
|
261
|
+
_ => false,
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
fn expr_from_path(path: &[Box<Expr>]) -> Box<Expr> {
|
|
266
|
+
let mut expr = path[0].clone();
|
|
267
|
+
for segment in &path[1..] {
|
|
268
|
+
let prop = match &**segment {
|
|
269
|
+
Expr::Ident(id) => MemberProp::Ident(id.clone().into()),
|
|
270
|
+
Expr::Lit(Lit::Str(_)) => MemberProp::Computed(ComputedPropName {
|
|
271
|
+
span: DUMMY_SP,
|
|
272
|
+
expr: segment.clone(),
|
|
273
|
+
}),
|
|
274
|
+
_ => unreachable!("Unexpected property name type - expected Ident or Str"),
|
|
275
|
+
};
|
|
276
|
+
expr = Expr::Member(MemberExpr {
|
|
277
|
+
span: DUMMY_SP,
|
|
278
|
+
obj: expr,
|
|
279
|
+
prop,
|
|
280
|
+
})
|
|
281
|
+
.into();
|
|
282
|
+
}
|
|
283
|
+
expr
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
fn visit_member_expr(&mut self, n: &mut MemberExpr, optional: bool) {
|
|
287
|
+
let prop_is_computed = n.prop.is_computed();
|
|
288
|
+
let prop_is_computed_lit = prop_is_computed && n.prop.as_computed().unwrap().expr.is_lit();
|
|
289
|
+
let prop_is_computed_lit_str = prop_is_computed_lit
|
|
290
|
+
&& matches!(
|
|
291
|
+
n.prop.as_computed().unwrap().expr.as_lit().unwrap(),
|
|
292
|
+
Lit::Str(_)
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
if (prop_is_computed && !prop_is_computed_lit_str)
|
|
296
|
+
|| (n.prop.is_ident() && n.prop.as_ident().unwrap().sym == "current")
|
|
297
|
+
|| !Self::is_extractable_member_obj(&n.obj)
|
|
298
|
+
{
|
|
299
|
+
self.member_expr_depth = 0;
|
|
300
|
+
n.obj.visit_mut_with(self);
|
|
301
|
+
} else {
|
|
302
|
+
self.member_expr_depth += 1;
|
|
303
|
+
n.obj.visit_mut_with(self);
|
|
304
|
+
self.member_expr_depth = max(self.member_expr_depth, 1) - 1;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let extracted_prop = match &n.prop {
|
|
308
|
+
MemberProp::Computed(comp) if prop_is_computed_lit_str => {
|
|
309
|
+
Some(Expr::Lit(comp.expr.as_lit().unwrap().clone()).into())
|
|
310
|
+
}
|
|
311
|
+
MemberProp::Computed(_) => {
|
|
312
|
+
n.prop.visit_mut_with(self);
|
|
313
|
+
None
|
|
314
|
+
}
|
|
315
|
+
MemberProp::Ident(ident) => Some(Expr::Ident(ident.clone().into()).into()),
|
|
316
|
+
_ => None,
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
if let Some(prop) = extracted_prop {
|
|
320
|
+
if !self.member_expr_path.is_empty() {
|
|
321
|
+
if optional {
|
|
322
|
+
let source = Self::expr_from_path(&self.member_expr_path);
|
|
323
|
+
if !self
|
|
324
|
+
.optional_member_sources
|
|
325
|
+
.iter()
|
|
326
|
+
.any(|existing| existing.eq_ignore_span(&source))
|
|
327
|
+
{
|
|
328
|
+
self.optional_member_sources.push(source);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
self.add_if_needed(prop);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
250
336
|
fn push_fn_scope(&mut self, ids_declared_in_scope: FxHashSet<Id>) {
|
|
251
337
|
let last_scope = self.scope_env.last().unwrap();
|
|
252
338
|
self.scope_env.push(ScopeEnv {
|
|
@@ -275,7 +361,7 @@ impl VisitMut for ExtractingIdentsCollector {
|
|
|
275
361
|
|
|
276
362
|
fn visit_mut_this_expr(&mut self, n: &mut ThisExpr) {
|
|
277
363
|
if self.member_expr_depth > 0 && !self.scope_env.last().unwrap().is_inner_fn_scope {
|
|
278
|
-
self.add_if_needed(Expr::This(*n).into()
|
|
364
|
+
self.add_if_needed(Expr::This(*n).into());
|
|
279
365
|
}
|
|
280
366
|
}
|
|
281
367
|
|
|
@@ -293,57 +379,21 @@ impl VisitMut for ExtractingIdentsCollector {
|
|
|
293
379
|
.iter()
|
|
294
380
|
.any(|env| env.ids_declared_locally.contains(&n.to_id()))
|
|
295
381
|
{
|
|
296
|
-
self.add_if_needed(
|
|
297
|
-
Expr::Ident(n.clone()).into(),
|
|
298
|
-
&Expr::Ident(n.clone()).into(),
|
|
299
|
-
);
|
|
382
|
+
self.add_if_needed(Expr::Ident(n.clone()).into());
|
|
300
383
|
}
|
|
301
384
|
|
|
302
385
|
n.visit_mut_children_with(self);
|
|
303
386
|
}
|
|
304
387
|
|
|
305
|
-
fn
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
&& matches!(
|
|
310
|
-
n.prop.as_computed().unwrap().expr.as_lit().unwrap(),
|
|
311
|
-
Lit::Str(_)
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
if (prop_is_computed && !prop_is_computed_lit_str)
|
|
315
|
-
|| (n.prop.is_ident() && n.prop.as_ident().unwrap().sym == "current")
|
|
316
|
-
|| !(n.obj.is_ident() || n.obj.is_member() || n.obj.is_this())
|
|
317
|
-
{
|
|
318
|
-
self.member_expr_depth = 0;
|
|
319
|
-
n.obj.visit_mut_with(self);
|
|
320
|
-
} else {
|
|
321
|
-
self.member_expr_depth += 1;
|
|
322
|
-
n.obj.visit_mut_with(self);
|
|
323
|
-
self.member_expr_depth = max(self.member_expr_depth, 1) - 1;
|
|
388
|
+
fn visit_mut_opt_chain_expr(&mut self, n: &mut OptChainExpr) {
|
|
389
|
+
match &mut *n.base {
|
|
390
|
+
OptChainBase::Member(member) => self.visit_member_expr(member, n.optional),
|
|
391
|
+
OptChainBase::Call(call) => call.visit_mut_with(self),
|
|
324
392
|
}
|
|
393
|
+
}
|
|
325
394
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if prop_is_computed_lit_str {
|
|
329
|
-
if !self.member_expr_path.is_empty() {
|
|
330
|
-
self.add_if_needed(
|
|
331
|
-
Expr::Lit(comp.expr.as_lit().unwrap().clone()).into(),
|
|
332
|
-
&Expr::Member(n.clone()).into(),
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
} else {
|
|
336
|
-
n.prop.visit_mut_with(self);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
MemberProp::Ident(ident) if !self.member_expr_path.is_empty() => {
|
|
340
|
-
self.add_if_needed(
|
|
341
|
-
Expr::Ident(ident.clone().into()).into(),
|
|
342
|
-
&Expr::Member(n.clone()).into(),
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
_ => {}
|
|
346
|
-
}
|
|
395
|
+
fn visit_mut_member_expr(&mut self, n: &mut MemberExpr) {
|
|
396
|
+
self.visit_member_expr(n, false);
|
|
347
397
|
}
|
|
348
398
|
|
|
349
399
|
/*
|
|
@@ -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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
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() {
|
|
@@ -315,6 +326,15 @@ impl StmtGen {
|
|
|
315
326
|
right: fallback,
|
|
316
327
|
}
|
|
317
328
|
.into();
|
|
329
|
+
let captured_or_fallback: Expr = if is_optional {
|
|
330
|
+
quote!(
|
|
331
|
+
"$src == null ? $src : $expr" as Expr,
|
|
332
|
+
src: Expr = capture_argument.clone(),
|
|
333
|
+
expr: Expr = captured_or_fallback,
|
|
334
|
+
)
|
|
335
|
+
} else {
|
|
336
|
+
captured_or_fallback
|
|
337
|
+
};
|
|
318
338
|
*value = if source_argument.is_ident() {
|
|
319
339
|
captured_or_fallback.into()
|
|
320
340
|
} else {
|
|
@@ -407,7 +427,6 @@ impl StmtGen {
|
|
|
407
427
|
*/
|
|
408
428
|
fn gen_register_worklet_stmt(params: RegisterWorkletParams) -> Stmt {
|
|
409
429
|
let RegisterWorkletParams {
|
|
410
|
-
mode,
|
|
411
430
|
target,
|
|
412
431
|
worklet_type,
|
|
413
432
|
function_name,
|
|
@@ -416,11 +435,14 @@ impl StmtGen {
|
|
|
416
435
|
extracted_js_fns,
|
|
417
436
|
hash,
|
|
418
437
|
is_class_member,
|
|
419
|
-
named_imports,
|
|
420
438
|
requires_main_thread_runtime,
|
|
421
439
|
register_worklet,
|
|
422
440
|
} = params;
|
|
423
441
|
|
|
442
|
+
if target != TransformTarget::LEPUS {
|
|
443
|
+
return EmptyStmt { span: DUMMY_SP }.into();
|
|
444
|
+
}
|
|
445
|
+
|
|
424
446
|
let function_to_register = Box::new(StmtGen::gen_function_to_register(
|
|
425
447
|
function,
|
|
426
448
|
function_name,
|
|
@@ -430,35 +452,21 @@ impl StmtGen {
|
|
|
430
452
|
is_class_member,
|
|
431
453
|
));
|
|
432
454
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
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()
|
|
455
|
+
*requires_main_thread_runtime = true;
|
|
456
|
+
let registration = quote_expr!("$register($type_, $hash, $fn_)",
|
|
457
|
+
register: Ident = register_worklet.clone(),
|
|
458
|
+
type_: Expr = Expr::Lit(worklet_type.type_str().into()),
|
|
459
|
+
hash: Expr = hash,
|
|
460
|
+
fn_: Expr = Expr::Fn(FnExpr {
|
|
461
|
+
ident: None,
|
|
462
|
+
function: function_to_register,
|
|
463
|
+
}),
|
|
464
|
+
);
|
|
465
|
+
ExprStmt {
|
|
466
|
+
span: DUMMY_SP,
|
|
467
|
+
expr: registration,
|
|
461
468
|
}
|
|
469
|
+
.into()
|
|
462
470
|
}
|
|
463
471
|
|
|
464
472
|
fn gen_function_to_register(
|