Unverified Commit 37c97d69 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: refactor codegen (#78685)

Refactor codegen in preparation for scope hoisting

Now, you can easily get the AST of the code-gen-ed module (which I need to merge them together)

Part of PACK-2960
parent e6ea4b75
...@@ -266,6 +266,9 @@ impl<T> ReadRef<T> ...@@ -266,6 +266,9 @@ impl<T> ReadRef<T>
where where
T: VcValueType, T: VcValueType,
{ {
/// Returns the inner value, if this [`ReadRef`] has exactly one strong reference.
///
/// Otherwise, an [`Err`] is returned with the same [`ReadRef`] that was passed in.
pub fn try_unwrap(this: Self) -> Result<VcReadTarget<T>, Self> { pub fn try_unwrap(this: Self) -> Result<VcReadTarget<T>, Self> {
match triomphe::Arc::try_unwrap(this.0) { match triomphe::Arc::try_unwrap(this.0) {
Ok(value) => Ok(T::Read::value_to_target(value)), Ok(value) => Ok(T::Read::value_to_target(value)),
......
...@@ -204,32 +204,31 @@ impl EcmascriptAnalyzable for EcmascriptModuleFacadeModule { ...@@ -204,32 +204,31 @@ impl EcmascriptAnalyzable for EcmascriptModuleFacadeModule {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn module_content( async fn module_content_options(
self: Vc<Self>, self: Vc<Self>,
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
async_module_info: Option<ResolvedVc<AsyncModuleInfo>>, async_module_info: Option<ResolvedVc<AsyncModuleInfo>>,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContentOptions>> {
let (esm_references, part_references) = self.await?.specific_references().await?; let (esm_references, part_references) = self.await?.specific_references().await?;
Ok(EcmascriptModuleContent::new( Ok(EcmascriptModuleContentOptions {
EcmascriptModuleContentOptions { parsed: ParseResult::empty().to_resolved().await?,
parsed: ParseResult::empty().to_resolved().await?, ident: self.ident().to_resolved().await?,
ident: self.ident().to_resolved().await?, specified_module_type: SpecifiedModuleType::EcmaScript,
specified_module_type: SpecifiedModuleType::EcmaScript, module_graph,
module_graph, chunking_context,
chunking_context, references: self.references().to_resolved().await?,
references: self.references().to_resolved().await?, esm_references,
esm_references, part_references,
part_references, code_generation: CodeGens::empty().to_resolved().await?,
code_generation: CodeGens::empty().to_resolved().await?, async_module: ResolvedVc::cell(Some(self.async_module().to_resolved().await?)),
async_module: ResolvedVc::cell(Some(self.async_module().to_resolved().await?)), generate_source_map: false,
generate_source_map: false, original_source_map: OptionStringifiedSourceMap::none().to_resolved().await?,
original_source_map: OptionStringifiedSourceMap::none().to_resolved().await?, exports: self.get_exports().to_resolved().await?,
exports: self.get_exports().to_resolved().await?, async_module_info,
async_module_info, }
}, .cell())
))
} }
} }
......
...@@ -92,12 +92,12 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule { ...@@ -92,12 +92,12 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn module_content( async fn module_content_options(
self: Vc<Self>, self: Vc<Self>,
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
async_module_info: Option<ResolvedVc<AsyncModuleInfo>>, async_module_info: Option<ResolvedVc<AsyncModuleInfo>>,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContentOptions>> {
let exports = self.get_exports().to_resolved().await?; let exports = self.get_exports().to_resolved().await?;
let original_module = self.await?.module; let original_module = self.await?.module;
let parsed = original_module.parse().to_resolved().await?; let parsed = original_module.parse().to_resolved().await?;
...@@ -110,24 +110,23 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule { ...@@ -110,24 +110,23 @@ impl EcmascriptAnalyzable for EcmascriptModuleLocalsModule {
.reference_module_source_maps(Vc::upcast(self)) .reference_module_source_maps(Vc::upcast(self))
.await?; .await?;
Ok(EcmascriptModuleContent::new( Ok(EcmascriptModuleContentOptions {
EcmascriptModuleContentOptions { parsed,
parsed, ident: self.ident().to_resolved().await?,
ident: self.ident().to_resolved().await?, specified_module_type: module_type_result.module_type,
specified_module_type: module_type_result.module_type, module_graph,
module_graph, chunking_context,
chunking_context, references: analyze.local_references().to_resolved().await?,
references: analyze.local_references().to_resolved().await?, esm_references: analyze_result.esm_local_references,
esm_references: analyze_result.esm_local_references, part_references: vec![],
part_references: vec![], code_generation: analyze_result.code_generation,
code_generation: analyze_result.code_generation, async_module: analyze_result.async_module,
async_module: analyze_result.async_module, generate_source_map,
generate_source_map, original_source_map: analyze_result.source_map,
original_source_map: analyze_result.source_map, exports,
exports, async_module_info,
async_module_info, }
}, .cell())
))
} }
} }
......
...@@ -75,12 +75,12 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset { ...@@ -75,12 +75,12 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn module_content( async fn module_content_options(
self: Vc<Self>, self: Vc<Self>,
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
chunking_context: ResolvedVc<Box<dyn ChunkingContext>>, chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
async_module_info: Option<ResolvedVc<AsyncModuleInfo>>, async_module_info: Option<ResolvedVc<AsyncModuleInfo>>,
) -> Result<Vc<EcmascriptModuleContent>> { ) -> Result<Vc<EcmascriptModuleContentOptions>> {
let module = self.await?; let module = self.await?;
let split_data = split_module(*module.full_module); let split_data = split_module(*module.full_module);
...@@ -96,24 +96,23 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset { ...@@ -96,24 +96,23 @@ impl EcmascriptAnalyzable for EcmascriptModulePartAsset {
.reference_module_source_maps(Vc::upcast(self)) .reference_module_source_maps(Vc::upcast(self))
.await?; .await?;
Ok(EcmascriptModuleContent::new( Ok(EcmascriptModuleContentOptions {
EcmascriptModuleContentOptions { parsed,
parsed, ident: self.ident().to_resolved().await?,
ident: self.ident().to_resolved().await?, specified_module_type: module_type_result.module_type,
specified_module_type: module_type_result.module_type, module_graph,
module_graph, chunking_context,
chunking_context, references: analyze.references().to_resolved().await?,
references: analyze.references().to_resolved().await?, esm_references: analyze_ref.esm_references,
esm_references: analyze_ref.esm_references, part_references: vec![],
part_references: vec![], code_generation: analyze_ref.code_generation,
code_generation: analyze_ref.code_generation, async_module: analyze_ref.async_module,
async_module: analyze_ref.async_module, generate_source_map,
generate_source_map, original_source_map: analyze_ref.source_map,
original_source_map: analyze_ref.source_map, exports: analyze_ref.exports,
exports: analyze_ref.exports, async_module_info,
async_module_info, }
}, .cell())
))
} }
} }
......
...@@ -6,5 +6,6 @@ var { g: global, __dirname, m: module, e: exports } = __turbopack_context__; ...@@ -6,5 +6,6 @@ var { g: global, __dirname, m: module, e: exports } = __turbopack_context__;
{ {
const e = new Error("Could not parse module '[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/unreachable/input/index.js'"); const e = new Error("Could not parse module '[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/unreachable/input/index.js'");
e.code = 'MODULE_UNPARSEABLE'; e.code = 'MODULE_UNPARSEABLE';
throw e;}}), throw e;
}}),
}]); }]);
\ No newline at end of file
...@@ -6,5 +6,6 @@ var { g: global, __dirname, m: module, e: exports } = __turbopack_context__; ...@@ -6,5 +6,6 @@ var { g: global, __dirname, m: module, e: exports } = __turbopack_context__;
{ {
const e = new Error("Could not parse module '[project]/turbopack/crates/turbopack-tests/tests/snapshot/mdx/error/input/index.js'"); const e = new Error("Could not parse module '[project]/turbopack/crates/turbopack-tests/tests/snapshot/mdx/error/input/index.js'");
e.code = 'MODULE_UNPARSEABLE'; e.code = 'MODULE_UNPARSEABLE';
throw e;}}), throw e;
}}),
}]); }]);
\ No newline at end of file
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