Skip to content

Commit 3f802db

Browse files
authored
refactor: entity and value (#46)
1 parent 6882d82 commit 3f802db

Some content is hidden

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

66 files changed

+560
-662
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<'a> Builtins<'a> {
2626
"defineProperty" => self.create_object_define_property_impl(),
2727
});
2828

29-
self.globals.borrow_mut().insert("Object", object);
29+
self.globals.borrow_mut().insert("Object", object.into());
3030
}
3131

3232
fn create_object_assign_impl(&self) -> Entity<'a> {
@@ -76,7 +76,7 @@ impl<'a> Builtins<'a> {
7676
}
7777
}
7878

79-
analyzer.factory.computed(array, deps)
79+
analyzer.factory.computed(array.into(), deps)
8080
})
8181
}
8282

@@ -91,7 +91,7 @@ impl<'a> Builtins<'a> {
9191
array.init_rest(value);
9292
}
9393

94-
analyzer.factory.computed(array, deps)
94+
analyzer.factory.computed(array.into(), deps)
9595
})
9696
}
9797

@@ -106,10 +106,10 @@ impl<'a> Builtins<'a> {
106106
let entry = analyzer.new_empty_array();
107107
entry.push_element(key.get_to_string(analyzer));
108108
entry.push_element(value);
109-
array.init_rest(entry);
109+
array.init_rest(entry.into());
110110
}
111111

112-
analyzer.factory.computed(array, deps)
112+
analyzer.factory.computed(array.into(), deps)
113113
})
114114
}
115115

@@ -168,8 +168,7 @@ impl<'a> Builtins<'a> {
168168
return object;
169169
}
170170

171-
let dep = analyzer.factory.consumable((dep, key, descriptor));
172-
object.unknown_mutate(analyzer, dep);
171+
object.unknown_mutate(analyzer, (dep, key, descriptor));
173172
object
174173
})
175174
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ impl<'a> Builtins<'a> {
3434
// "toString" => factory.string("__#toString__"),
3535
});
3636

37-
self.globals.borrow_mut().insert("Symbol", object);
37+
self.globals.borrow_mut().insert("Symbol", object.into());
3838
}
3939
}

crates/tree_shaker/src/builtins/import_meta.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ impl<'a> Builtins<'a> {
3131
},
3232
);
3333

34-
object
34+
object.into()
3535
}
3636
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{object::create_object_prototype, BuiltinPrototype};
22
use crate::{
3-
entity::{Entity, EntityFactory, ObjectId},
3+
entity::{EntityFactory, ObjectId},
44
init_prototype,
55
};
66

@@ -14,11 +14,11 @@ pub fn create_function_prototype<'a>(factory: &EntityFactory<'a>) -> BuiltinProt
1414
// This can be any value
1515
let arguments_object_id = ObjectId::from_usize(0);
1616
match arg.test_is_undefined() {
17-
Some(true) => analyzer.factory.array(cf_scope, arguments_object_id),
17+
Some(true) => analyzer.factory.array(cf_scope, arguments_object_id).into(),
1818
Some(false) => arg,
1919
None => analyzer.factory.union((
2020
arg,
21-
analyzer.factory.array(cf_scope, arguments_object_id) as Entity<'a>,
21+
analyzer.factory.array(cf_scope, arguments_object_id).into(),
2222
)),
2323
}
2424
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a> BuiltinPrototype<'a> {
7373
key: Entity<'a>,
7474
dep: Consumable<'a>,
7575
) -> Entity<'a> {
76-
let dep = analyzer.consumable((dep, target, key));
76+
let dep = (dep, target, key);
7777
if let Some(key_literals) = key.get_to_literals(analyzer) {
7878
let mut values = vec![];
7979
for key_literal in key_literals {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ pub fn create_class_names_namespace<'a>(
2828
}
2929
}
3030

31-
analyzer.factory.computed_unknown_string(analyzer.consumable((deps_1, deps_2, rest)))
31+
analyzer.factory.computed_unknown_string((deps_1, deps_2, rest))
3232
})
3333
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn create_react_create_context_impl<'a>(factory: &'a EntityFactory<'a>) -> E
6060
"Consumer" => create_react_context_consumer_impl(analyzer, context_id),
6161
});
6262

63-
context
63+
context.into()
6464
})
6565
}
6666

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ pub fn create_react_create_element_impl<'a>(factory: &'a EntityFactory<'a>) -> E
66
let [tag, props] = args[..] else { unreachable!() };
77
let props = match props.test_nullish() {
88
Some(true) => analyzer
9-
.new_empty_object(ObjectPrototype::Builtin(&analyzer.builtins.prototypes.object), None),
9+
.new_empty_object(ObjectPrototype::Builtin(&analyzer.builtins.prototypes.object), None)
10+
.into(),
1011
Some(false) => props,
1112
None => analyzer.factory.union((
1213
props,
1314
analyzer
1415
.new_empty_object(ObjectPrototype::Builtin(&analyzer.builtins.prototypes.object), None)
15-
as Entity<'a>,
16+
.into(),
1617
)),
1718
};
1819

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn create_react_namespace<'a>(
5151
"useMemo" => create_react_use_memo_impl(factory),
5252
});
5353

54-
namespace
54+
namespace.into()
5555
}
5656

5757
pub fn create_react_jsx_runtime_namespace<'a>(
@@ -70,5 +70,5 @@ pub fn create_react_jsx_runtime_namespace<'a>(
7070
"jsxs" => create_react_jsxs_impl(factory),
7171
});
7272

73-
object
73+
object.into()
7474
}

crates/tree_shaker/src/consumable/collector.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use super::{Consumable, ConsumableTrait};
1+
use super::{Consumable, ConsumeTrait};
22
use crate::{analyzer::Analyzer, entity::EntityFactory};
33
use std::mem;
44

55
#[derive(Debug)]
6-
pub struct ConsumableCollector<'a, T: ConsumableTrait<'a> + 'a = Consumable<'a>> {
6+
pub struct ConsumableCollector<'a, T: ConsumeTrait<'a> + 'a = Consumable<'a>> {
77
pub current: Vec<T>,
88
pub node: Option<Consumable<'a>>,
99
}
1010

11-
impl<'a, T: ConsumableTrait<'a> + 'a> Default for ConsumableCollector<'a, T> {
11+
impl<'a, T: ConsumeTrait<'a> + 'a> Default for ConsumableCollector<'a, T> {
1212
fn default() -> Self {
1313
Self { current: Vec::new(), node: None }
1414
}
1515
}
1616

17-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableCollector<'a, T> {
17+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableCollector<'a, T> {
1818
pub fn new(current: Vec<T>) -> Self {
1919
Self { current, node: None }
2020
}

crates/tree_shaker/src/consumable/impls.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
1-
use super::{Consumable, ConsumableTrait};
1+
use super::{ConsumableTrait, ConsumeTrait};
22
use crate::analyzer::Analyzer;
33
use std::{cell::RefCell, rc::Rc};
44

55
impl<'a> ConsumableTrait<'a> for () {
66
fn consume(&self, _: &mut Analyzer<'a>) {}
77
}
88

9-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Box<T> {
9+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Box<T> {
1010
fn consume(&self, analyzer: &mut Analyzer<'a>) {
1111
self.as_ref().consume(analyzer)
1212
}
1313
}
1414

15-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Option<T> {
15+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Option<T> {
1616
fn consume(&self, analyzer: &mut Analyzer<'a>) {
1717
if let Some(value) = self {
1818
value.consume(analyzer)
1919
}
2020
}
2121
}
2222

23-
impl<'a, T: Default + ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for &'a RefCell<T> {
23+
impl<'a, T: Default + ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for &'a RefCell<T> {
2424
fn consume(&self, analyzer: &mut Analyzer<'a>) {
2525
self.take().consume(analyzer)
2626
}
2727
}
2828

29-
impl<'a, T: Default + ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Rc<T> {
29+
impl<'a, T: Default + ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Rc<T> {
3030
fn consume(&self, analyzer: &mut Analyzer<'a>) {
3131
self.as_ref().consume(analyzer)
3232
}
3333
}
3434

35-
impl<'a> ConsumableTrait<'a> for Consumable<'a> {
36-
fn consume(&self, analyzer: &mut Analyzer<'a>) {
37-
self.0.consume(analyzer)
38-
}
39-
}
40-
41-
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumableTrait<'a> for Vec<T> {
35+
impl<'a, T: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for Vec<T> {
4236
fn consume(&self, analyzer: &mut Analyzer<'a>) {
4337
for item in self {
4438
item.consume(analyzer)
4539
}
4640
}
4741
}
4842

49-
impl<'a, T1: ConsumableTrait<'a> + 'a, T2: ConsumableTrait<'a> + 'a> ConsumableTrait<'a>
50-
for (T1, T2)
51-
{
43+
impl<'a, T1: ConsumeTrait<'a> + 'a, T2: ConsumeTrait<'a> + 'a> ConsumableTrait<'a> for (T1, T2) {
5244
fn consume(&self, analyzer: &mut Analyzer<'a>) {
5345
self.0.consume(analyzer);
5446
self.1.consume(analyzer)
5547
}
5648
}
5749

58-
impl<
59-
'a,
60-
T1: ConsumableTrait<'a> + 'a,
61-
T2: ConsumableTrait<'a> + 'a,
62-
T3: ConsumableTrait<'a> + 'a,
63-
> ConsumableTrait<'a> for (T1, T2, T3)
50+
impl<'a, T1: ConsumeTrait<'a> + 'a, T2: ConsumeTrait<'a> + 'a, T3: ConsumeTrait<'a> + 'a>
51+
ConsumableTrait<'a> for (T1, T2, T3)
6452
{
6553
fn consume(&self, analyzer: &mut Analyzer<'a>) {
6654
self.0.consume(analyzer);
@@ -71,10 +59,10 @@ impl<
7159

7260
impl<
7361
'a,
74-
T1: ConsumableTrait<'a> + 'a,
75-
T2: ConsumableTrait<'a> + 'a,
76-
T3: ConsumableTrait<'a> + 'a,
77-
T4: ConsumableTrait<'a> + 'a,
62+
T1: ConsumeTrait<'a> + 'a,
63+
T2: ConsumeTrait<'a> + 'a,
64+
T3: ConsumeTrait<'a> + 'a,
65+
T4: ConsumeTrait<'a> + 'a,
7866
> ConsumableTrait<'a> for (T1, T2, T3, T4)
7967
{
8068
fn consume(&self, analyzer: &mut Analyzer<'a>) {

crates/tree_shaker/src/consumable/mod.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,44 @@ use crate::analyzer::Analyzer;
77
pub use collector::*;
88
pub use lazy::*;
99
pub use once::*;
10+
use oxc::allocator::Allocator;
1011
use std::fmt::Debug;
1112

1213
pub trait ConsumableTrait<'a>: Debug {
1314
fn consume(&self, analyzer: &mut Analyzer<'a>);
1415
}
1516

17+
pub trait ConsumeTrait<'a>: Debug {
18+
fn consume(&self, analyzer: &mut Analyzer<'a>);
19+
fn uniform(self, allocator: &'a Allocator) -> Consumable<'a>;
20+
}
21+
22+
impl<'a, T: ConsumableTrait<'a> + 'a> ConsumeTrait<'a> for T {
23+
fn consume(&self, analyzer: &mut Analyzer<'a>) {
24+
self.consume(analyzer);
25+
}
26+
fn uniform(self, allocator: &'a Allocator) -> Consumable<'a> {
27+
Consumable(allocator.alloc(self))
28+
}
29+
}
30+
1631
#[derive(Debug, Clone, Copy)]
1732
pub struct Consumable<'a>(pub &'a (dyn ConsumableTrait<'a> + 'a));
1833

34+
impl<'a> ConsumeTrait<'a> for Consumable<'a> {
35+
fn consume(&self, analyzer: &mut Analyzer<'a>) {
36+
self.0.consume(analyzer);
37+
}
38+
fn uniform(self, _: &'a Allocator) -> Consumable<'a> {
39+
self
40+
}
41+
}
42+
1943
pub type ConsumableVec<'a> = Vec<Consumable<'a>>;
2044

2145
impl<'a> Analyzer<'a> {
2246
#[inline]
23-
pub fn consume(&mut self, dep: impl ConsumableTrait<'a> + 'a) {
47+
pub fn consume(&mut self, dep: impl ConsumeTrait<'a> + 'a) {
2448
dep.consume(self);
2549
}
2650

crates/tree_shaker/src/entity/arguments.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
consumed_object, Entity, EntityTrait, EnumeratedProperties, IteratedElements, ObjectPrototype,
3-
TypeofResult,
2+
consumed_object, Entity, EnumeratedProperties, IteratedElements, ObjectPrototype, TypeofResult,
3+
ValueTrait,
44
};
55
use crate::{analyzer::Analyzer, consumable::Consumable, use_consumed_flag};
66
use std::cell::Cell;
@@ -11,7 +11,7 @@ pub struct ArgumentsEntity<'a> {
1111
pub arguments: Vec<(bool, Entity<'a>)>,
1212
}
1313

14-
impl<'a> EntityTrait<'a> for ArgumentsEntity<'a> {
14+
impl<'a> ValueTrait<'a> for ArgumentsEntity<'a> {
1515
fn consume(&'a self, analyzer: &mut Analyzer<'a>) {
1616
use_consumed_flag!(self);
1717

crates/tree_shaker/src/entity/array.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{
2-
consumed_object, Entity, EntityTrait, EnumeratedProperties, IteratedElements, LiteralEntity,
3-
ObjectId, TypeofResult,
2+
consumed_object, Entity, EnumeratedProperties, IteratedElements, LiteralEntity, ObjectId,
3+
TypeofResult, ValueTrait,
44
};
55
use crate::{
66
analyzer::Analyzer,
@@ -33,7 +33,7 @@ impl<'a> fmt::Debug for ArrayEntity<'a> {
3333
}
3434
}
3535

36-
impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
36+
impl<'a> ValueTrait<'a> for ArrayEntity<'a> {
3737
fn consume(&'a self, analyzer: &mut Analyzer<'a>) {
3838
use_consumed_flag!(self);
3939

@@ -95,7 +95,7 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
9595
result.push(self.get_length().map_or_else(
9696
|| {
9797
let dep = self.rest.borrow().clone();
98-
analyzer.factory.computed_unknown_number(analyzer.consumable(dep))
98+
analyzer.factory.computed_unknown_number(dep)
9999
},
100100
|length| analyzer.factory.number(length as f64, None),
101101
));
@@ -112,10 +112,10 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
112112
}
113113
analyzer.factory.computed_union(result, dep)
114114
} else {
115-
analyzer.factory.computed_unknown(analyzer.consumable((
115+
analyzer.factory.computed_unknown((
116116
self.elements.borrow().iter().chain(self.rest.borrow().iter()).cloned().collect::<Vec<_>>(),
117117
dep,
118-
)))
118+
))
119119
}
120120
}
121121

@@ -290,7 +290,7 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
290290
if self.consumed.get() {
291291
return consumed_object::r#await(analyzer, dep);
292292
}
293-
analyzer.factory.computed(self, dep)
293+
analyzer.factory.computed(self.into(), dep)
294294
}
295295

296296
fn iterate(&'a self, analyzer: &mut Analyzer<'a>, dep: Consumable<'a>) -> IteratedElements<'a> {
@@ -342,7 +342,7 @@ impl<'a> EntityTrait<'a> for ArrayEntity<'a> {
342342
}
343343

344344
fn get_to_jsx_child(&'a self, _analyzer: &Analyzer<'a>) -> Entity<'a> {
345-
self
345+
self.into()
346346
}
347347

348348
fn test_typeof(&self) -> TypeofResult {

0 commit comments

Comments
 (0)