Skip to content

Commit 7fa5f89

Browse files
committed
fix: stacked tree
1 parent 898d0c6 commit 7fa5f89

File tree

3 files changed

+31
-48
lines changed

3 files changed

+31
-48
lines changed

crates/jsshaker/src/scope/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
pub struct Scoping<'a> {
2727
pub call: Vec<CallScope<'a>>,
2828
pub variable: LinkedTree<'a, VariableScopeId, VariableScope<'a>>,
29-
pub cf: StackedTree<CfScopeId, CfScope<'a>>,
29+
pub cf: StackedTree<'a, CfScopeId, CfScope<'a>>,
3030
pub root_cf_scope: CfScopeId,
3131
pub try_catch_depth: Option<usize>,
3232
pub current_callsite: AstKind2<'a>,
@@ -38,7 +38,8 @@ impl<'a> Scoping<'a> {
3838
let mut variable = LinkedTree::new_in(factory.allocator);
3939
let root_variable_scope =
4040
variable.push(VariableScope::new_in_with_this(factory.allocator, factory.unknown));
41-
let cf = StackedTree::new(CfScope::new(CfScopeKind::Root, factory.vec(), false));
41+
let cf =
42+
StackedTree::new(factory.allocator, CfScope::new(CfScopeKind::Root, factory.vec(), false));
4243
let root_cf_scope = cf.root;
4344
factory.root_cf_scope = Some(root_cf_scope);
4445
Scoping {

crates/jsshaker/src/scope/stacked_tree.rs

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,38 @@
1-
use std::fmt::Debug;
2-
use std::hash::Hash;
1+
use oxc::allocator::Allocator;
32

4-
pub trait Idx: Debug + Clone + Copy + PartialEq + Eq + Hash {
5-
fn new(depth: usize, parent: usize) -> Self;
6-
fn depth(self) -> usize;
7-
fn parent(self) -> usize;
3+
use crate::utils::box_bump::{BoxBump, Idx};
4+
5+
pub struct StackedTreeId<I: Idx> {
6+
depth: usize,
7+
parent: Option<I>,
88
}
99

1010
#[macro_export]
1111
macro_rules! define_stacked_tree_idx {
1212
($v:vis struct $type:ident;) => {
13-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14-
$v struct $type {
15-
depth: u32,
16-
parent: u32,
17-
}
18-
19-
impl $crate::scope::stacked_tree::Idx for $type {
20-
#[inline(always)]
21-
fn new(depth: usize, parent: usize) -> Self {
22-
Self { depth: depth as u32, parent: parent as u32 }
23-
}
24-
#[inline(always)]
25-
fn depth(self) -> usize {
26-
self.depth as usize
27-
}
28-
#[inline(always)]
29-
fn parent(self) -> usize {
30-
self.parent as usize
31-
}
13+
$crate::define_box_bump_idx! {
14+
$v struct $type for $crate::scope::stacked_tree::StackedTreeId<$type>;
3215
}
3316
};
3417
}
3518

3619
struct StackedTreeItem<I: Idx, T> {
3720
id: I,
38-
id_idx: usize,
3921
data: T,
4022
}
4123

42-
pub struct StackedTree<I: Idx, T> {
43-
ids: Vec<I>,
24+
pub struct StackedTree<'a, I: Idx, T> {
25+
ids: BoxBump<'a, I, StackedTreeId<I>>,
4426
stack: Vec<StackedTreeItem<I, T>>,
4527
pub root: I,
4628
}
4729

48-
impl<I: Idx, T> StackedTree<I, T> {
49-
pub fn new(root_data: T) -> Self {
50-
let root_id = I::new(0, 0);
51-
let root_item = StackedTreeItem { id: root_id, id_idx: 0, data: root_data };
52-
StackedTree { ids: vec![root_id], stack: vec![root_item], root: root_id }
30+
impl<'a, I: Idx, T> StackedTree<'a, I, T> {
31+
pub fn new(allocator: &'a Allocator, root_data: T) -> Self {
32+
let ids = BoxBump::new(allocator);
33+
let root_id = ids.alloc(StackedTreeId { depth: 0, parent: None });
34+
let root_item = StackedTreeItem { id: root_id, data: root_data };
35+
StackedTree { ids, stack: vec![root_item], root: root_id }
5336
}
5437

5538
pub fn current_id(&self) -> I {
@@ -89,10 +72,11 @@ impl<I: Idx, T> StackedTree<I, T> {
8972
}
9073

9174
pub fn push(&mut self, data: T) -> I {
92-
let id = I::new(self.stack.len(), self.stack.last().unwrap().id_idx);
93-
let id_idx = self.ids.len();
94-
self.stack.push(StackedTreeItem { id, id_idx, data });
95-
self.ids.push(id);
75+
let id = self.ids.alloc(StackedTreeId {
76+
depth: self.stack.len(),
77+
parent: Some(self.stack.last().unwrap().id),
78+
});
79+
self.stack.push(StackedTreeItem { id, data });
9680
id
9781
}
9882

@@ -104,20 +88,24 @@ impl<I: Idx, T> StackedTree<I, T> {
10488
self.stack[depth].id
10589
}
10690

91+
fn get_depth(&self, id: I) -> usize {
92+
self.ids.get(id).depth
93+
}
94+
10795
fn get_parent(&self, id: I) -> Option<I> {
108-
if id.depth() == 0 { None } else { Some(self.ids[id.parent()]) }
96+
self.ids.get(id).parent
10997
}
11098

11199
pub fn find_lca(&self, another: I) -> (usize, I) {
112100
let current_depth = self.stack.len() - 1;
113-
let another_depth = another.depth();
101+
let another_depth = self.get_depth(another);
114102
let min_depth = current_depth.min(another_depth);
115103

116104
let mut another = another;
117105
for _ in min_depth..another_depth {
118106
another = self.get_parent(another).unwrap();
119107
}
120-
debug_assert_eq!(min_depth, another.depth());
108+
debug_assert_eq!(min_depth, self.get_depth(another));
121109

122110
let mut depth = min_depth;
123111
loop {

crates/jsshaker/src/scope/variable_scope.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ impl<'a> Analyzer<'a> {
8686
decl_node: AstKind2<'a>,
8787
fn_value: Option<Entity<'a>>,
8888
) {
89-
println!(
90-
"Declare {:?} {:?} {:?}",
91-
symbol,
92-
self.scoping.cf.current_id(),
93-
self.scoping.cf.current_data()
94-
);
9589
if let Some(variable) = self.variable(scope, symbol) {
9690
// Here we can't use kind.is_untracked() because this time we are declaring a variable
9791
let old_kind = variable.borrow().kind;

0 commit comments

Comments
 (0)