Unverified Commit 07e10789 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbopack-ecmascript): Use ResolvedVc in CodeGen type (#74709)

parent b7d4e14a
use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use swc_core::ecma::{ use swc_core::ecma::{
ast::Stmt, ast::Stmt,
visit::{AstParentKind, VisitMut}, visit::{AstParentKind, VisitMut},
}; };
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, ResolvedVc, Vc}; use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, NonLocalValue, ResolvedVc, Vc};
use turbopack_core::chunk::{AsyncModuleInfo, ChunkingContext}; use turbopack_core::chunk::{AsyncModuleInfo, ChunkingContext};
/// impl of code generation inferred from a ModuleReference. /// impl of code generation inferred from a ModuleReference.
...@@ -106,13 +107,31 @@ pub trait CodeGenerateableWithAsyncModuleInfo { ...@@ -106,13 +107,31 @@ pub trait CodeGenerateableWithAsyncModuleInfo {
) -> Vc<CodeGeneration>; ) -> Vc<CodeGeneration>;
} }
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat)] pub enum UnresolvedCodeGen {
pub enum CodeGen {
CodeGenerateable(Vc<Box<dyn CodeGenerateable>>), CodeGenerateable(Vc<Box<dyn CodeGenerateable>>),
CodeGenerateableWithAsyncModuleInfo(Vc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
}
impl UnresolvedCodeGen {
pub async fn to_resolved(&self) -> Result<CodeGen> {
Ok(match self {
Self::CodeGenerateable(vc) => CodeGen::CodeGenerateable(vc.to_resolved().await?),
Self::CodeGenerateableWithAsyncModuleInfo(vc) => {
CodeGen::CodeGenerateableWithAsyncModuleInfo(vc.to_resolved().await?)
}
})
}
}
#[derive(
Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat, NonLocalValue,
)]
pub enum CodeGen {
CodeGenerateable(ResolvedVc<Box<dyn CodeGenerateable>>),
CodeGenerateableWithAsyncModuleInfo(ResolvedVc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>), CodeGenerateableWithAsyncModuleInfo(ResolvedVc<Box<dyn CodeGenerateableWithAsyncModuleInfo>>),
} }
#[turbo_tasks::value(transparent, local)] #[turbo_tasks::value(transparent)]
pub struct CodeGenerateables(Vec<CodeGen>); pub struct CodeGenerateables(Vec<CodeGen>);
pub fn path_to( pub fn path_to(
......
...@@ -126,7 +126,9 @@ use crate::{ ...@@ -126,7 +126,9 @@ use crate::{
ConstantNumber, ConstantString, JsValueUrlKind, RequireContextValue, ConstantNumber, ConstantString, JsValueUrlKind, RequireContextValue,
}, },
chunk::EcmascriptExports, chunk::EcmascriptExports,
code_gen::{CodeGen, CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables}, code_gen::{
CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables, UnresolvedCodeGen,
},
magic_identifier, magic_identifier,
parse::parse, parse::parse,
references::{ references::{
...@@ -167,7 +169,9 @@ pub struct AnalyzeEcmascriptModuleResultBuilder { ...@@ -167,7 +169,9 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
local_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>, local_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
reexport_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>, reexport_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
evaluation_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>, evaluation_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
code_gens: Vec<CodeGen>, // Many of these `code_gens` are accumulated inside of a synchronous SWC visitor, so we don't
// resolve them until `build()`.
code_gens: Vec<UnresolvedCodeGen>,
exports: EcmascriptExports, exports: EcmascriptExports,
async_module: ResolvedVc<OptionAsyncModule>, async_module: ResolvedVc<OptionAsyncModule>,
successful: bool, successful: bool,
...@@ -235,18 +239,18 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -235,18 +239,18 @@ impl AnalyzeEcmascriptModuleResultBuilder {
C: Upcast<Box<dyn CodeGenerateable>>, C: Upcast<Box<dyn CodeGenerateable>>,
{ {
self.code_gens self.code_gens
.push(CodeGen::CodeGenerateable(Vc::upcast(code_gen))); .push(UnresolvedCodeGen::CodeGenerateable(Vc::upcast(code_gen)));
} }
/// Adds a codegen to the analysis result. /// Adds a codegen to the analysis result.
#[allow(dead_code)] #[allow(dead_code)]
pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: ResolvedVc<C>) pub fn add_code_gen_with_availability_info<C>(&mut self, code_gen: Vc<C>)
where where
C: Upcast<Box<dyn CodeGenerateableWithAsyncModuleInfo>>, C: Upcast<Box<dyn CodeGenerateableWithAsyncModuleInfo>>,
{ {
self.code_gens self.code_gens
.push(CodeGen::CodeGenerateableWithAsyncModuleInfo( .push(UnresolvedCodeGen::CodeGenerateableWithAsyncModuleInfo(
ResolvedVc::upcast(code_gen), Vc::upcast(code_gen),
)); ));
} }
...@@ -274,8 +278,7 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -274,8 +278,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.successful = successful; self.successful = successful;
} }
/// Builds the final analysis result. Resolves internal Vcs for performance /// Builds the final analysis result. Resolves internal Vcs.
/// in using them.
pub async fn build( pub async fn build(
mut self, mut self,
track_reexport_references: bool, track_reexport_references: bool,
...@@ -301,14 +304,6 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -301,14 +304,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
.into_iter() .into_iter()
.flatten() .flatten()
.collect(); .collect();
for c in self.code_gens.iter_mut() {
match c {
CodeGen::CodeGenerateable(c) => {
*c = c.resolve().await?;
}
CodeGen::CodeGenerateableWithAsyncModuleInfo(..) => {}
}
}
let source_map = if let Some(source_map) = self.source_map { let source_map = if let Some(source_map) = self.source_map {
source_map source_map
...@@ -321,7 +316,13 @@ impl AnalyzeEcmascriptModuleResultBuilder { ...@@ -321,7 +316,13 @@ impl AnalyzeEcmascriptModuleResultBuilder {
local_references: ResolvedVc::cell(local_references), local_references: ResolvedVc::cell(local_references),
reexport_references: ResolvedVc::cell(reexport_references), reexport_references: ResolvedVc::cell(reexport_references),
evaluation_references: ResolvedVc::cell(evaluation_references), evaluation_references: ResolvedVc::cell(evaluation_references),
code_generation: ResolvedVc::cell(self.code_gens), code_generation: ResolvedVc::cell(
self.code_gens
.iter()
.map(UnresolvedCodeGen::to_resolved)
.try_join()
.await?,
),
exports: self.exports.resolved_cell(), exports: self.exports.resolved_cell(),
async_module: self.async_module, async_module: self.async_module,
successful: self.successful, successful: self.successful,
...@@ -2976,7 +2977,6 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> { ...@@ -2976,7 +2977,6 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
export: &'ast NamedExport, export: &'ast NamedExport,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>, ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) { ) {
let path = Vc::cell(as_parent_path(ast_path));
// We create mutable exports for fake ESMs generated by module splitting // We create mutable exports for fake ESMs generated by module splitting
let is_fake_esm = export let is_fake_esm = export
.with .with
...@@ -3032,6 +3032,7 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> { ...@@ -3032,6 +3032,7 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
} }
} }
let path = Vc::cell(as_parent_path(ast_path));
self.analysis.add_code_gen(EsmModuleItem::new(path)); self.analysis.add_code_gen(EsmModuleItem::new(path));
export.visit_children_with_ast_path(self, ast_path); export.visit_children_with_ast_path(self, ast_path);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment