Skip to content

Commit

Permalink
handle hoisted effects
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 31, 2024
1 parent 3e6d472 commit 43fa273
Show file tree
Hide file tree
Showing 24 changed files with 38,384 additions and 37,989 deletions.
37 changes: 29 additions & 8 deletions crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ pub fn create_graph(m: &Program, eval_context: &EvalContext) -> VarGraph {
data: &mut graph,
eval_context,
effects: Default::default(),
hoisted_effects: Default::default(),
early_return_stack: Default::default(),
var_decl_kind: Default::default(),
current_value: Default::default(),
Expand Down Expand Up @@ -690,6 +691,7 @@ struct Analyzer<'a> {
data: &'a mut VarGraph,

effects: Vec<Effect>,
hoisted_effects: Vec<Effect>,
early_return_stack: Vec<EarlyReturn>,

eval_context: &'a EvalContext,
Expand Down Expand Up @@ -1409,6 +1411,7 @@ impl VisitAstPath for Analyzer<'_> {
self.cur_fn_ident = decl.function.span.lo.0;
decl.visit_children_with_path(self, ast_path);
let return_value = self.take_return_values();
self.hoisted_effects.append(&mut self.effects);

self.add_value(
decl.ident.to_id(),
Expand Down Expand Up @@ -1763,6 +1766,7 @@ impl VisitAstPath for Analyzer<'_> {
self.effects = take(&mut self.data.effects);
program.visit_children_with_path(self, ast_path);
self.end_early_return_block();
self.effects.append(&mut self.hoisted_effects);
self.data.effects = take(&mut self.effects);
}

Expand Down Expand Up @@ -1909,8 +1913,7 @@ impl VisitAstPath for Analyzer<'_> {
n: &'ast BlockStmt,
ast_path: &mut swc_core::ecma::visit::AstNodePath<'r>,
) {
n.visit_children_with_path(self, ast_path);
let handle = if ast_path.len() < 2 {
let block_type = if ast_path.len() < 2 {
Some(false)
} else if matches!(
&ast_path[ast_path.len() - 2..],
Expand Down Expand Up @@ -1941,12 +1944,30 @@ impl VisitAstPath for Analyzer<'_> {
} else {
Some(false)
};
if let Some(func) = handle {
if self.end_early_return_block() && !func {
self.early_return_stack.push(EarlyReturn::Always {
prev_effects: take(&mut self.effects),
start_ast_path: as_parent_path(ast_path),
});
match block_type {
Some(true) => {
let early_return_stack = take(&mut self.early_return_stack);
let mut effects = take(&mut self.effects);
let hoisted_effects = take(&mut self.hoisted_effects);
n.visit_children_with_path(self, ast_path);
self.end_early_return_block();
self.effects.append(&mut self.hoisted_effects);
effects.append(&mut self.effects);
self.hoisted_effects = hoisted_effects;
self.effects = effects;
self.early_return_stack = early_return_stack;
}
Some(false) => {
n.visit_children_with_path(self, ast_path);
if self.end_early_return_block() {
self.early_return_stack.push(EarlyReturn::Always {
prev_effects: take(&mut self.effects),
start_ast_path: as_parent_path(ast_path),
});
}
}
None => {
n.visit_children_with_path(self, ast_path);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,112 +1155,7 @@
),
},
else: EffectsBlock {
effects: [
Call {
func: FreeVar(
"import",
),
args: [
Value(
Constant(
Str(
Word(
"c",
),
),
),
),
],
ast_path: [
Program(
Module,
),
Module(
Body(
8,
),
),
ModuleItem(
Stmt,
),
Stmt(
Decl,
),
Decl(
Fn,
),
FnDecl(
Function,
),
Function(
Body,
),
BlockStmt(
Stmts(
1,
),
),
Stmt(
Expr,
),
ExprStmt(
Expr,
),
Expr(
Call,
),
],
span: 438..449#0,
in_try: false,
},
ImportedBinding {
esm_reference_index: 4,
export: Some(
"z",
),
ast_path: [
Program(
Module,
),
Module(
Body(
8,
),
),
ModuleItem(
Stmt,
),
Stmt(
Decl,
),
Decl(
Fn,
),
FnDecl(
Function,
),
Function(
Body,
),
BlockStmt(
Stmts(
2,
),
),
Stmt(
Expr,
),
ExprStmt(
Expr,
),
Expr(
Ident,
),
],
span: 453..454#2,
in_try: false,
},
],
effects: [],
range: StartAfter(
[
Program(
Expand Down Expand Up @@ -1337,4 +1232,108 @@
span: 399..434#0,
in_try: false,
},
Call {
func: FreeVar(
"import",
),
args: [
Value(
Constant(
Str(
Word(
"c",
),
),
),
),
],
ast_path: [
Program(
Module,
),
Module(
Body(
8,
),
),
ModuleItem(
Stmt,
),
Stmt(
Decl,
),
Decl(
Fn,
),
FnDecl(
Function,
),
Function(
Body,
),
BlockStmt(
Stmts(
1,
),
),
Stmt(
Expr,
),
ExprStmt(
Expr,
),
Expr(
Call,
),
],
span: 438..449#0,
in_try: false,
},
ImportedBinding {
esm_reference_index: 4,
export: Some(
"z",
),
ast_path: [
Program(
Module,
),
Module(
Body(
8,
),
),
ModuleItem(
Stmt,
),
Stmt(
Decl,
),
Decl(
Fn,
),
FnDecl(
Function,
),
Function(
Body,
),
BlockStmt(
Stmts(
2,
),
),
Stmt(
Expr,
),
ExprStmt(
Expr,
),
Expr(
Ident,
),
],
span: 453..454#2,
in_try: false,
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
- *0* unreachable
⚠️ This value might have side effects

14 -> 16 call = import*0*("c")
0 -> 16 call = import*0*("c")
- *0* import: The dynamic import() method from the ESM specification: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports
Loading

0 comments on commit 43fa273

Please sign in to comment.