Skip to content

Commit 58fe815

Browse files
committed
simplify the protocol to reduce the diff and code size
1 parent 6405cd4 commit 58fe815

File tree

90 files changed

+221
-373
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+221
-373
lines changed

turbopack/crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ function createModuleObject(id: ModuleId): Module {
9595
}
9696
}
9797

98-
type BindingTag = 0 | 1 | 2
99-
const BindingTag_Getter = 0 as BindingTag
100-
const BindingTag_GetterSetter = 1 as BindingTag
101-
const BindingTag_Value = 2 as BindingTag
98+
type BindingTag = 0
99+
const BindingTag_Value = 0 as BindingTag
102100

103101
// an arbitrary sequence of bindings as
104102
// - a prop name
@@ -117,30 +115,32 @@ function esm(exports: Exports, bindings: EsmBindings) {
117115
let i = 0
118116
while (i < bindings.length) {
119117
const propName = bindings[i++] as string
120-
const tag = bindings[i++] as BindingTag
121-
switch (tag) {
122-
case BindingTag_Getter:
118+
const tagOrFunction = bindings[i++]
119+
if (typeof tagOrFunction === 'number') {
120+
if (tagOrFunction === BindingTag_Value) {
123121
defineProp(exports, propName, {
124-
get: bindings[i++] as () => unknown,
122+
value: bindings[i++],
125123
enumerable: true,
124+
writable: false,
126125
})
127-
break
128-
case BindingTag_GetterSetter:
126+
} else {
127+
throw new Error(`unexpected tag: ${tagOrFunction}`)
128+
}
129+
} else {
130+
const getterFn = tagOrFunction as () => unknown
131+
if (typeof bindings[i] === 'function') {
132+
const setterFn = bindings[i++] as (v: unknown) => void
129133
defineProp(exports, propName, {
130-
get: bindings[i++] as () => unknown,
131-
set: bindings[i++] as (v: unknown) => void,
134+
get: getterFn,
135+
set: setterFn,
132136
enumerable: true,
133137
})
134-
break
135-
case BindingTag_Value:
138+
} else {
136139
defineProp(exports, propName, {
137-
value: bindings[i++],
140+
get: getterFn,
138141
enumerable: true,
139-
writable: false,
140142
})
141-
break
142-
default:
143-
throw new Error(`unexpected tag: ${tag}`)
143+
}
144144
}
145145
}
146146
Object.seal(exports)
@@ -287,9 +287,9 @@ function interopEsm(
287287
current = getProto(current)
288288
) {
289289
for (const key of Object.getOwnPropertyNames(current)) {
290-
bindings.push(key, BindingTag_Getter, createGetter(raw, key))
290+
bindings.push(key, createGetter(raw, key))
291291
if (defaultLocation === -1 && key === 'default') {
292-
defaultLocation = bindings.length - 2 // point at the tag
292+
defaultLocation = bindings.length - 1
293293
}
294294
}
295295
}
@@ -299,8 +299,8 @@ function interopEsm(
299299
if (!(allowExportDefault && defaultLocation >= 0)) {
300300
// Replace the binding with one for the namespace itself in order to preserve iteration order.
301301
if (defaultLocation >= 0) {
302-
bindings[defaultLocation] = BindingTag_Value
303-
bindings[defaultLocation + 1] = raw
302+
// Replace the getter with the value
303+
bindings.splice(defaultLocation, 1, BindingTag_Value, raw)
304304
} else {
305305
bindings.push('default', BindingTag_Value, raw)
306306
}

turbopack/crates/turbopack-ecmascript/src/references/esm/export.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,9 @@ impl EsmExports {
709709
.map(|ident| {
710710
let expr = ident.as_expr_individual(DUMMY_SP);
711711
let read_expr = expr.map_either(Expr::from, Expr::from).into_inner();
712+
// For imported bindings we could simply export the'import' and have the runtime hook the bindings together.
713+
// This would be cute, slightly smaller codegen but efficiency is an open question. In fact we could even
714+
// perform the 'liveness' check at runtime by querying the property descriptor.
712715
match (liveness, export_usage_info.is_circuit_breaker) {
713716
(Liveness::Constant, false) => ExportBinding::Value(quote!(
714717
"$expr" as Expr, expr: Expr = read_expr
@@ -769,16 +772,14 @@ impl EsmExports {
769772
));
770773
match exprs {
771774
ExportBinding::Getter(getter) => {
772-
getters.push(Some(Expr::Lit(Lit::Num(Number::from(0))).into()));
773775
getters.push(Some(getter.into()));
774776
}
775777
ExportBinding::GetterSetter(getter, setter) => {
776-
getters.push(Some(Expr::Lit(Lit::Num(Number::from(1))).into()));
777778
getters.push(Some(getter.into()));
778779
getters.push(Some(setter.into()));
779780
}
780781
ExportBinding::Value(value) => {
781-
getters.push(Some(Expr::Lit(Lit::Num(Number::from(2))).into()));
782+
getters.push(Some(Expr::Lit(Lit::Num(Number::from(0))).into()));
782783
getters.push(Some(value.into()));
783784
}
784785
ExportBinding::None => {}

turbopack/crates/turbopack-ecmascript/src/references/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,8 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
33673367
f(ident.sym.as_str().into(), Liveness::Constant);
33683368
}
33693369
Decl::Var(var_decl) => {
3370+
// TODO: examine whether the value is ever mutated rather than just checking
3371+
// 'const'
33703372
let liveness = match var_decl.kind {
33713373
VarDeclKind::Var => Liveness::Live,
33723374
VarDeclKind::Let => Liveness::Live,

turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/dynamic-import/output/4c35f_tests_snapshot_basic-tree-shake_dynamic-import_input_lib_b2d8c81e.js

Lines changed: 0 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)