Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
81d51544
Commit
81d51544
authored
Sep 18, 2025
by
Niklas Mischkulnig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
link_shallow
parent
5acdea74
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
160 additions
and
2 deletions
+160
-2
linker.rs
turbopack/crates/turbopack-ecmascript/src/analyzer/linker.rs
+128
-0
mod.rs
turbopack/crates/turbopack-ecmascript/src/references/mod.rs
+32
-2
No files found.
turbopack/crates/turbopack-ecmascript/src/analyzer/linker.rs
View file @
81d51544
...
@@ -4,9 +4,137 @@ use anyhow::Result;
...
@@ -4,9 +4,137 @@ use anyhow::Result;
use
parking_lot
::
Mutex
;
use
parking_lot
::
Mutex
;
use
rustc_hash
::{
FxHashMap
,
FxHashSet
};
use
rustc_hash
::{
FxHashMap
,
FxHashSet
};
use
swc_core
::
ecma
::
ast
::
Id
;
use
swc_core
::
ecma
::
ast
::
Id
;
use
turbo_tasks
::
TryJoinIterExt
;
use
super
::{
JsValue
,
graph
::
VarGraph
};
use
super
::{
JsValue
,
graph
::
VarGraph
};
#[derive(Debug,
Clone,
Copy,
PartialEq,
Eq)]
pub
enum
LinkEarlyAbortCondition
{
// The return value is only inspected for truthiness
Truthy
,
// We are only interested in WellKnownFunctions (and alternatives of them)
WellKnownFunction
,
}
impl
LinkEarlyAbortCondition
{
fn
process
(
&
self
,
val
:
&
JsValue
)
->
Option
<
JsValue
>
{
match
self
{
LinkEarlyAbortCondition
::
Truthy
=>
{
// TODO
if
let
Some
(
_
)
=
val
.is_truthy
()
{
Some
(
val
.clone
())
}
else
{
None
}
}
LinkEarlyAbortCondition
::
WellKnownFunction
=>
match
val
{
JsValue
::
Variable
(
_
)
|
JsValue
::
Argument
(
_
,
_
)
=>
{
// These can still end up as well-known functions
None
}
JsValue
::
WellKnownFunction
(
_
)
=>
Some
(
val
.clone
()),
JsValue
::
Alternatives
{
values
,
..
}
=>
{
let
mapped
=
values
.iter
()
.flat_map
(|
v
|
self
.process
(
v
))
.collect
::
<
Vec
<
_
>>
();
if
mapped
.len
()
==
values
.len
()
{
// We could determine the state of all values
Some
(
JsValue
::
alternatives
(
mapped
))
}
else
{
None
}
}
_
=>
Some
(
JsValue
::
unknown_empty
(
false
,
"early abort non well-known function"
,
)),
},
}
}
}
// Resolves variable references only, doesn't exhaustively evaluate everything inside of the value.
pub
async
fn
link_shallow
<
'a
,
B
,
RB
,
F
,
RF
>
(
graph
:
&
VarGraph
,
mut
current_val
:
JsValue
,
early_visitor
:
&
B
,
visitor
:
&
F
,
fun_args_values
:
&
Mutex
<
FxHashMap
<
u32
,
Vec
<
JsValue
>>>
,
var_cache
:
&
Mutex
<
FxHashMap
<
Id
,
JsValue
>>
,
)
->
Result
<
JsValue
>
where
RB
:
'a
+
Future
<
Output
=
Result
<
(
JsValue
,
bool
)
>>
+
Send
,
B
:
'a
+
Fn
(
JsValue
)
->
RB
+
Sync
,
RF
:
'a
+
Future
<
Output
=
Result
<
(
JsValue
,
bool
)
>>
+
Send
,
F
:
'a
+
Fn
(
JsValue
)
->
RF
+
Sync
,
{
current_val
.normalize
();
if
let
JsValue
::
Alternatives
{
values
,
logical_property
,
..
}
=
current_val
{
let
values
=
values
.into_iter
()
.map
(|
v
|
link_shallow
(
graph
,
v
,
early_visitor
,
visitor
,
fun_args_values
,
var_cache
))
.try_join
()
.await
?
;
return
Ok
(
if
let
Some
(
logical_property
)
=
logical_property
{
JsValue
::
alternatives_with_additional_property
(
values
,
logical_property
)
}
else
{
JsValue
::
alternatives
(
values
)
});
}
// Resolve variable references, don't do anything else.
loop
{
match
current_val
{
JsValue
::
Variable
(
var
)
=>
{
if
let
Some
(
val
)
=
graph
.values
.get
(
&
var
)
{
current_val
=
val
.clone
();
continue
;
}
else
{
current_val
=
JsValue
::
unknown
(
JsValue
::
Variable
(
var
),
false
,
"no value of this variable analysed"
,
);
}
}
JsValue
::
Argument
(
func_ident
,
index
)
=>
{
if
let
Some
(
args
)
=
fun_args_values
.lock
()
.get
(
&
func_ident
)
{
if
let
Some
(
val
)
=
args
.get
(
index
)
{
current_val
=
val
.clone
();
continue
;
}
else
{
current_val
=
JsValue
::
unknown_empty
(
false
,
"unknown function argument (out of bounds)"
,
);
}
}
else
{
current_val
=
JsValue
::
unknown
(
JsValue
::
Argument
(
func_ident
,
index
),
false
,
"function calls are not analysed yet"
,
);
}
}
val
=>
{
current_val
=
val
;
}
};
break
;
}
let
val
=
early_visitor
(
current_val
)
.await
?
.
0
;
let
val
=
visitor
(
val
)
.await
?
.
0
;
Ok
(
val
)
}
pub
async
fn
link
<
'a
,
B
,
RB
,
F
,
RF
>
(
pub
async
fn
link
<
'a
,
B
,
RB
,
F
,
RF
>
(
graph
:
&
VarGraph
,
graph
:
&
VarGraph
,
mut
val
:
JsValue
,
mut
val
:
JsValue
,
...
...
turbopack/crates/turbopack-ecmascript/src/references/mod.rs
View file @
81d51544
...
@@ -136,7 +136,7 @@ use crate::{
...
@@ -136,7 +136,7 @@ use crate::{
builtin
::
early_replace_builtin
,
builtin
::
early_replace_builtin
,
graph
::{
ConditionalKind
,
EffectArg
,
EvalContext
,
VarGraph
},
graph
::{
ConditionalKind
,
EffectArg
,
EvalContext
,
VarGraph
},
imports
::{
ImportAnnotations
,
ImportAttributes
,
ImportedSymbol
,
Reexport
},
imports
::{
ImportAnnotations
,
ImportAttributes
,
ImportedSymbol
,
Reexport
},
linker
::
JsValueTruthy
,
linker
::
link_shallow
,
parse_require_context
,
parse_require_context
,
top_level_await
::
has_top_level_await
,
top_level_await
::
has_top_level_await
,
},
},
...
@@ -469,6 +469,33 @@ impl AnalysisState<'_> {
...
@@ -469,6 +469,33 @@ impl AnalysisState<'_> {
.
0
)
.
0
)
}
}
/// Links a value to the graph, returning the linked value, but without recursing.
async
fn
unsafe_link_value_shallow
(
&
self
,
value
:
JsValue
,
attributes
:
&
ImportAttributes
,
)
->
Result
<
JsValue
>
{
link_shallow
(
self
.var_graph
,
value
,
&
early_value_visitor
,
&
|
value
|
{
value_visitor
(
*
self
.origin
,
value
,
*
self
.compile_time_info
,
&
self
.free_var_references
,
self
.var_graph
,
attributes
,
self
.define_process_cwd
,
)
},
&
self
.fun_args_values
,
&
self
.var_cache
,
)
.await
}
async
fn
link_truthy
(
&
self
,
value
:
JsValue
,
attributes
:
&
ImportAttributes
)
->
Result
<
JsValue
>
{
async
fn
link_truthy
(
&
self
,
value
:
JsValue
,
attributes
:
&
ImportAttributes
)
->
Result
<
JsValue
>
{
self
.link_value
(
value
,
attributes
)
.await
self
.link_value
(
value
,
attributes
)
.await
}
}
...
@@ -478,7 +505,10 @@ impl AnalysisState<'_> {
...
@@ -478,7 +505,10 @@ impl AnalysisState<'_> {
value
:
JsValue
,
value
:
JsValue
,
attributes
:
&
ImportAttributes
,
attributes
:
&
ImportAttributes
,
)
->
Result
<
Vec
<
WellKnownFunctionKind
>>
{
)
->
Result
<
Vec
<
WellKnownFunctionKind
>>
{
let
value
=
self
.link_value
(
value
,
attributes
)
.await
?
;
// We are only interested in well-known functions, the only JsValues that can evaluated to
// that are WellKnownFunction-s, and Variables that point to them. Anything else can be
// short circuited.
let
value
=
self
.unsafe_link_value_shallow
(
value
,
attributes
)
.await
?
;
Ok
(
match
value
{
Ok
(
match
value
{
JsValue
::
WellKnownFunction
(
kind
)
=>
vec!
[
kind
],
JsValue
::
WellKnownFunction
(
kind
)
=>
vec!
[
kind
],
JsValue
::
Alternatives
{
values
,
..
}
=>
values
JsValue
::
Alternatives
{
values
,
..
}
=>
values
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment