Skip to content

Commit 0a931ee

Browse files
committed
fix!!!!!!!!!!!!
1 parent 9904f9b commit 0a931ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+521
-424
lines changed

crates/tree_shaker/src/analyzer.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
Scoping,
1111
conditional::ConditionalDataMap,
1212
exhaustive::{ExhaustiveCallback, ExhaustiveDepId},
13-
r#loop::LoopDataMap,
13+
// r#loop::LoopDataMap,
1414
},
1515
utils::ExtraData,
1616
vfs::Vfs,
@@ -23,7 +23,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
2323
use std::{collections::BTreeSet, mem};
2424

2525
pub struct Analyzer<'a> {
26-
pub vfs: &'a dyn Vfs,
26+
pub vfs: Box<dyn Vfs>,
2727
pub config: &'a TreeShakeConfig,
2828
pub allocator: &'a Allocator,
2929
pub factory: &'a EntityFactory<'a>,
@@ -39,21 +39,17 @@ pub struct Analyzer<'a> {
3939
pub exhaustive_callbacks: FxHashMap<ExhaustiveDepId, FxHashSet<ExhaustiveCallback<'a>>>,
4040
pub referred_deps: ReferredDeps,
4141
pub conditional_data: ConditionalDataMap<'a>,
42-
pub loop_data: LoopDataMap<'a>,
42+
// pub loop_data: LoopDataMap<'a>,
4343
pub folder: ConstantFolder<'a>,
4444
pub mangler: Mangler<'a>,
4545
pub pending_deps: FxHashSet<ExhaustiveCallback<'a>>,
4646
pub diagnostics: BTreeSet<String>,
4747
}
4848

4949
impl<'a> Analyzer<'a> {
50-
pub fn new_in(
51-
vfs: &'a dyn Vfs,
52-
config: &'a TreeShakeConfig,
53-
allocator: &'a Allocator,
54-
) -> &'a mut Self {
50+
pub fn new_in(vfs: Box<dyn Vfs>, config: &'a TreeShakeConfig, allocator: &'a Allocator) -> Self {
5551
let factory = &*allocator.alloc(EntityFactory::new(allocator, config));
56-
allocator.alloc(Analyzer {
52+
Analyzer {
5753
vfs,
5854
config,
5955
allocator,
@@ -70,12 +66,12 @@ impl<'a> Analyzer<'a> {
7066
exhaustive_callbacks: Default::default(),
7167
referred_deps: Default::default(),
7268
conditional_data: Default::default(),
73-
loop_data: Default::default(),
69+
// loop_data: Default::default(),
7470
folder: Default::default(),
7571
mangler: Mangler::new(config.mangling.is_some(), allocator),
7672
pending_deps: Default::default(),
7773
diagnostics: Default::default(),
78-
})
74+
}
7975
}
8076

8177
pub fn finalize(&mut self) {
@@ -94,7 +90,7 @@ impl<'a> Analyzer<'a> {
9490
dirty |= self.consume_top_level_uncaught();
9591
dirty |= self.call_exhaustive_callbacks();
9692
dirty |= self.post_analyze_handle_conditional();
97-
dirty |= self.post_analyze_handle_loops();
93+
// dirty |= self.post_analyze_handle_loops();
9894
dirty |= self.post_analyze_handle_folding();
9995
if !dirty {
10096
break;
@@ -108,11 +104,12 @@ impl<'a> Analyzer<'a> {
108104
}
109105

110106
fn consume_top_level_uncaught(&mut self) -> bool {
107+
let factory = self.factory;
111108
let thrown_values = &mut self.call_scope_mut().try_scopes.last_mut().unwrap().thrown_values;
112109
if thrown_values.is_empty() {
113110
false
114111
} else {
115-
let values = mem::take(thrown_values);
112+
let values = mem::replace(thrown_values, factory.vec());
116113
self.consume(values);
117114
true
118115
}

crates/tree_shaker/src/builtins/globals/object_constructor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ impl<'a> Builtins<'a> {
1414
ObjectPrototype::Builtin(&self.prototypes.function),
1515
false,
1616
);
17-
object.init_rest(ObjectPropertyValue::Field(factory.immutable_unknown, true));
17+
object.init_rest(factory, ObjectPropertyValue::Field(factory.immutable_unknown, true));
1818

19-
init_namespace!(object, {
19+
init_namespace!(object, factory, {
2020
"prototype" => factory.immutable_unknown,
2121
"assign" => self.create_object_assign_impl(),
2222
"keys" => self.create_object_keys_impl(),

crates/tree_shaker/src/builtins/globals/symbol_constructor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ impl Builtins<'_> {
1414
ObjectPrototype::Builtin(&self.prototypes.function),
1515
false,
1616
);
17-
object.init_rest(ObjectPropertyValue::Field(factory.immutable_unknown, true));
17+
object.init_rest(factory, ObjectPropertyValue::Field(factory.immutable_unknown, true));
1818

19-
init_namespace!(object, {
19+
init_namespace!(object, factory, {
2020
"prototype" => factory.immutable_unknown,
2121
// "asyncIterator" => factory.string("__#asyncIterator__"),
2222
// "hasInstance" => factory.string("__#hasInstance__"),

crates/tree_shaker/src/builtins/import_meta.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use super::{Builtins, constants::IMPORT_META_OBJECT_ID, prototypes::BuiltinPrototypes};
2-
use crate::entity::{Entity, EntityFactory, ObjectProperty, ObjectPropertyValue, ObjectPrototype};
2+
use crate::{
3+
consumable::ConsumableCollector,
4+
entity::{Entity, EntityFactory, ObjectProperty, ObjectPropertyValue, ObjectPrototype},
5+
};
36

47
impl<'a> Builtins<'a> {
58
pub fn create_import_meta(
@@ -8,24 +11,27 @@ impl<'a> Builtins<'a> {
811
) -> Entity<'a> {
912
let object =
1013
factory.builtin_object(IMPORT_META_OBJECT_ID, ObjectPrototype::ImplicitOrNull, true);
11-
object.init_rest(ObjectPropertyValue::Property(
12-
Some(factory.immutable_unknown),
13-
Some(factory.immutable_unknown),
14-
));
14+
object.init_rest(
15+
factory,
16+
ObjectPropertyValue::Property(
17+
Some(factory.immutable_unknown),
18+
Some(factory.immutable_unknown),
19+
),
20+
);
1521

1622
// import.meta.url
1723
object.string_keyed.borrow_mut().insert(
1824
"url",
1925
ObjectProperty {
2026
definite: true,
2127
enumerable: true,
22-
possible_values: vec![ObjectPropertyValue::Property(
28+
possible_values: factory.vec1(ObjectPropertyValue::Property(
2329
Some(factory.implemented_builtin_fn("import.meta.url", |analyzer, _, _, _| {
2430
analyzer.factory.unknown_string
2531
})),
2632
None,
27-
)],
28-
non_existent: Default::default(),
33+
)),
34+
non_existent: ConsumableCollector::new(factory.vec()),
2935
key: None,
3036
mangling: None,
3137
},

crates/tree_shaker/src/builtins/prototypes/mod.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ use crate::{
1818
consumable::Consumable,
1919
entity::{Entity, EntityFactory, LiteralEntity},
2020
};
21-
use oxc::semantic::SymbolId;
22-
use rustc_hash::FxHashMap;
21+
use oxc::{allocator, semantic::SymbolId};
2322

2423
use super::Builtins;
2524

26-
#[derive(Default)]
2725
pub struct BuiltinPrototype<'a> {
2826
name: &'static str,
29-
string_keyed: FxHashMap<&'static str, Entity<'a>>,
30-
symbol_keyed: FxHashMap<SymbolId, Entity<'a>>,
27+
string_keyed: allocator::HashMap<'a, &'static str, Entity<'a>>,
28+
symbol_keyed: allocator::HashMap<'a, SymbolId, Entity<'a>>,
3129
}
3230

3331
impl fmt::Debug for BuiltinPrototype<'_> {
@@ -37,6 +35,14 @@ impl fmt::Debug for BuiltinPrototype<'_> {
3735
}
3836

3937
impl<'a> BuiltinPrototype<'a> {
38+
pub fn new_in(factory: &EntityFactory<'a>) -> Self {
39+
Self {
40+
name: "",
41+
string_keyed: allocator::HashMap::new_in(factory.allocator),
42+
symbol_keyed: allocator::HashMap::new_in(factory.allocator),
43+
}
44+
}
45+
4046
pub fn with_name(mut self, name: &'static str) -> Self {
4147
self.name = name;
4248
self
@@ -75,7 +81,7 @@ impl<'a> BuiltinPrototype<'a> {
7581
) -> Entity<'a> {
7682
let dep = (dep, target, key);
7783
if let Some(key_literals) = key.get_to_literals(analyzer) {
78-
let mut values = vec![];
84+
let mut values = analyzer.factory.vec();
7985
for key_literal in key_literals {
8086
if let Some(property) = self.get_literal_keyed(key_literal) {
8187
values.push(property);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::BuiltinPrototype;
22
use crate::entity::EntityFactory;
33

4-
pub fn create_null_prototype<'a>(_factory: &EntityFactory<'a>) -> BuiltinPrototype<'a> {
5-
BuiltinPrototype::default().with_name("null")
4+
pub fn create_null_prototype<'a>(factory: &EntityFactory<'a>) -> BuiltinPrototype<'a> {
5+
BuiltinPrototype::new_in(factory).with_name("null")
66
}

crates/tree_shaker/src/builtins/react/class_names.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pub fn create_class_names_namespace<'a>(
1010
factory.implemented_builtin_fn("classnames::default", |analyzer, dep, _this, args| {
1111
let (class_names, rest, iterate_dep) = args.iterate(analyzer, dep);
1212

13-
let mut deps_1 = vec![];
14-
let mut deps_2 = vec![iterate_dep];
13+
let mut deps_1 = factory.vec();
14+
let mut deps_2 = factory.vec1(iterate_dep);
1515
for class_name in class_names {
1616
if TypeofResult::Object.contains(class_name.test_typeof()) {
1717
// This may be an array. However, this makes no difference in this logic.

crates/tree_shaker/src/builtins/react/context.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn create_react_create_context_impl<'a>(factory: &'a EntityFactory<'a>) -> E
5353
dep,
5454
});
5555

56-
init_object!(context, {
56+
init_object!(context, factory, {
5757
"__#internal__consumed_hook" => analyzer.factory.computed_unknown(context_id),
5858
"__#internal__context_id" => analyzer.serialize_internal_id(context_id),
5959
"Provider" => create_react_context_provider_impl(analyzer, context_id),
@@ -69,11 +69,13 @@ impl<'a> ConsumableTrait<'a> for ContextId {
6969
let data = &mut analyzer.builtins.react_data.contexts[*self];
7070
data.consumed = true;
7171
let default_value = data.default_value;
72-
let stack = mem::take(&mut data.stack);
7372
let dep = data.dep;
73+
let stack = mem::take(&mut data.stack);
7474
analyzer.consume(default_value);
75-
analyzer.consume(stack);
7675
analyzer.consume(dep);
76+
for value in stack {
77+
analyzer.consume(value);
78+
}
7779
}
7880
}
7981

crates/tree_shaker/src/builtins/react/dependencies.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use oxc::span::Span;
99
use rustc_hash::FxHashMap;
1010
use std::{cell::RefCell, rc::Rc, vec};
1111

12-
#[derive(Debug, Default)]
12+
#[derive(Debug)]
1313
pub struct ReactDependenciesData<'a> {
1414
pub collectors: Vec<ConsumableCollector<'a, Entity<'a>>>,
1515
pub rest_collector: ConsumableCollector<'a, Entity<'a>>,
@@ -33,11 +33,24 @@ pub fn check_dependencies<'a>(
3333
let (elements, rest, iterate_dep) = current.iterate(analyzer, dep);
3434

3535
let span = (analyzer.current_module(), analyzer.current_span());
36-
let data = analyzer.builtins.react_data.dependencies.entry(span).or_default().clone();
36+
let data = analyzer
37+
.builtins
38+
.react_data
39+
.dependencies
40+
.entry(span)
41+
.or_insert_with(|| {
42+
Rc::new(RefCell::new(ReactDependenciesData {
43+
collectors: vec![],
44+
rest_collector: ConsumableCollector::new(factory.vec()),
45+
extra_collector: ConsumableCollector::new(factory.vec()),
46+
previous: None,
47+
}))
48+
})
49+
.clone();
3750
let mut data = data.borrow_mut();
3851

3952
if data.collectors.len() <= elements.len() {
40-
data.collectors.resize_with(elements.len(), ConsumableCollector::default);
53+
data.collectors.resize_with(elements.len(), || ConsumableCollector::new(factory.vec()));
4154
}
4255
for (index, element) in elements.iter().enumerate() {
4356
data.collectors[index].push(*element);
@@ -83,7 +96,7 @@ pub fn check_dependencies<'a>(
8396
(false, extra_collector.collect(factory))
8497
}
8598
} else {
86-
let mut deps = vec![];
99+
let mut deps = analyzer.factory.vec();
87100
for index in &changed {
88101
deps.push(collectors[*index].collect(factory));
89102
if let Some(previous) = previous.get_mut(*index) {
@@ -106,7 +119,7 @@ pub fn check_dependencies<'a>(
106119
} else {
107120
require_rerun = true;
108121
for element in &elements {
109-
collectors.push(ConsumableCollector::new(vec![*element]));
122+
collectors.push(ConsumableCollector::new(analyzer.factory.vec1(*element)));
110123
}
111124
if let Some(rest) = rest {
112125
rest_collector.push(rest);

crates/tree_shaker/src/builtins/react/forward_ref.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use oxc::allocator;
2+
13
use crate::entity::{Entity, EntityFactory};
24

35
pub fn create_react_forward_ref_impl<'a>(factory: &'a EntityFactory<'a>) -> Entity<'a> {
@@ -15,7 +17,10 @@ pub fn create_react_forward_ref_impl<'a>(factory: &'a EntityFactory<'a>) -> Enti
1517
analyzer,
1618
dep,
1719
this,
18-
analyzer.factory.arguments(vec![(false, props), (false, r#ref)]),
20+
analyzer.factory.arguments(allocator::Vec::from_array_in(
21+
[(false, props), (false, r#ref)],
22+
analyzer.allocator,
23+
)),
1924
)
2025
},
2126
)

crates/tree_shaker/src/builtins/react/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub fn create_react_namespace<'a>(
4040
) -> Entity<'a> {
4141
let namespace =
4242
factory.builtin_object(REACT_NAMESPACE_OBJECT_ID, ObjectPrototype::ImplicitOrNull, false);
43-
namespace.init_rest(ObjectPropertyValue::Field(factory.immutable_unknown, true));
43+
namespace.init_rest(factory, ObjectPropertyValue::Field(factory.immutable_unknown, true));
4444

45-
init_namespace!(namespace, {
45+
init_namespace!(namespace, factory, {
4646
"forwardRef" => create_react_forward_ref_impl(factory),
4747
"memo" => create_react_memo_impl(factory),
4848
"createElement" => create_react_create_element_impl(factory),
@@ -63,9 +63,9 @@ pub fn create_react_jsx_runtime_namespace<'a>(
6363
ObjectPrototype::ImplicitOrNull,
6464
false,
6565
);
66-
object.init_rest(ObjectPropertyValue::Field(factory.immutable_unknown, true));
66+
object.init_rest(factory, ObjectPropertyValue::Field(factory.immutable_unknown, true));
6767

68-
init_namespace!(object, {
68+
init_namespace!(object, factory, {
6969
"jsx" => create_react_jsx_impl(factory),
7070
"jsxs" => create_react_jsxs_impl(factory),
7171
});

crates/tree_shaker/src/builtins/utils.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#[macro_export]
22
macro_rules! init_namespace {
3-
($ns:expr, { $($k:expr => $v:expr,)* }) => {
3+
($ns:expr, $factory:expr, { $($k:expr => $v:expr,)* }) => {
44
{
55
use $crate::entity::{ObjectProperty, ObjectPropertyValue};
6+
use $crate::consumable::ConsumableCollector;
67
let mut string_keyed = $ns.string_keyed.borrow_mut();
78
$(string_keyed.insert(
89
$k,
910
ObjectProperty {
1011
definite: true,
1112
enumerable: false,
12-
possible_values: vec![ObjectPropertyValue::Field($v, true)],
13-
non_existent: Default::default(),
13+
possible_values: $factory.vec1(ObjectPropertyValue::Field($v, true)),
14+
non_existent: ConsumableCollector::new($factory.vec()),
1415
key: None,
1516
mangling: None,
1617
},
@@ -21,17 +22,18 @@ macro_rules! init_namespace {
2122

2223
#[macro_export]
2324
macro_rules! init_object {
24-
($ns:expr, { $($k:expr => $v:expr,)* }) => {
25+
($ns:expr, $factory:expr, { $($k:expr => $v:expr,)* }) => {
2526
{
2627
use $crate::entity::{ObjectProperty, ObjectPropertyValue};
28+
use $crate::consumable::ConsumableCollector;
2729
let mut string_keyed = $ns.string_keyed.borrow_mut();
2830
$(string_keyed.insert(
2931
$k,
3032
ObjectProperty {
3133
definite: true,
3234
enumerable: true,
33-
possible_values: vec![ObjectPropertyValue::Field($v, false)],
34-
non_existent: Default::default(),
35+
possible_values: $factory.vec1(ObjectPropertyValue::Field($v, false)),
36+
non_existent: ConsumableCollector::new($factory.vec()),
3537
key: None,
3638
mangling: None,
3739
},

0 commit comments

Comments
 (0)