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 swc_core::ecma::{
ast::Stmt,
visit::{AstParentKind, VisitMut},
};
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};
/// impl of code generation inferred from a ModuleReference.
......@@ -106,13 +107,31 @@ pub trait CodeGenerateableWithAsyncModuleInfo {
) -> Vc<CodeGeneration>;
}
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs, ValueDebugFormat)]
pub enum CodeGen {
pub enum UnresolvedCodeGen {
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>>),
}
#[turbo_tasks::value(transparent, local)]
#[turbo_tasks::value(transparent)]
pub struct CodeGenerateables(Vec<CodeGen>);
pub fn path_to(
......
......@@ -126,7 +126,9 @@ use crate::{
ConstantNumber, ConstantString, JsValueUrlKind, RequireContextValue,
},
chunk::EcmascriptExports,
code_gen::{CodeGen, CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables},
code_gen::{
CodeGenerateable, CodeGenerateableWithAsyncModuleInfo, CodeGenerateables, UnresolvedCodeGen,
},
magic_identifier,
parse::parse,
references::{
......@@ -167,7 +169,9 @@ pub struct AnalyzeEcmascriptModuleResultBuilder {
local_references: FxIndexSet<ResolvedVc<Box<dyn ModuleReference>>>,
reexport_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,
async_module: ResolvedVc<OptionAsyncModule>,
successful: bool,
......@@ -235,18 +239,18 @@ impl AnalyzeEcmascriptModuleResultBuilder {
C: Upcast<Box<dyn CodeGenerateable>>,
{
self.code_gens
.push(CodeGen::CodeGenerateable(Vc::upcast(code_gen)));
.push(UnresolvedCodeGen::CodeGenerateable(Vc::upcast(code_gen)));
}
/// Adds a codegen to the analysis result.
#[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
C: Upcast<Box<dyn CodeGenerateableWithAsyncModuleInfo>>,
{
self.code_gens
.push(CodeGen::CodeGenerateableWithAsyncModuleInfo(
ResolvedVc::upcast(code_gen),
.push(UnresolvedCodeGen::CodeGenerateableWithAsyncModuleInfo(
Vc::upcast(code_gen),
));
}
......@@ -274,8 +278,7 @@ impl AnalyzeEcmascriptModuleResultBuilder {
self.successful = successful;
}
/// Builds the final analysis result. Resolves internal Vcs for performance
/// in using them.
/// Builds the final analysis result. Resolves internal Vcs.
pub async fn build(
mut self,
track_reexport_references: bool,
......@@ -301,14 +304,6 @@ impl AnalyzeEcmascriptModuleResultBuilder {
.into_iter()
.flatten()
.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 {
source_map
......@@ -321,7 +316,13 @@ impl AnalyzeEcmascriptModuleResultBuilder {
local_references: ResolvedVc::cell(local_references),
reexport_references: ResolvedVc::cell(reexport_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(),
async_module: self.async_module,
successful: self.successful,
......@@ -2976,7 +2977,6 @@ impl VisitAstPath for ModuleReferencesVisitor<'_> {
export: &'ast NamedExport,
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
let is_fake_esm = export
.with
......@@ -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));
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