Skip to content

Commit 03390ad

Browse files
committed
feat(allocator): TakeIn trait with AllocatorAccessor (#11201)
closes #10314
1 parent 61451fc commit 03390ad

Some content is hidden

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

50 files changed

+363
-388
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::Allocator;
2+
3+
/// Accessor for getting the underlying allocator.
4+
pub trait AllocatorAccessor<'a> {
5+
/// Get the underlying allocator.
6+
fn allocator(self) -> &'a Allocator;
7+
}
8+
9+
impl<'a> AllocatorAccessor<'a> for &'a Allocator {
10+
#[inline]
11+
fn allocator(self) -> &'a Allocator {
12+
self
13+
}
14+
}

crates/oxc_allocator/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
#![warn(missing_docs)]
2323

24+
mod accessor;
2425
mod address;
2526
mod alloc;
2627
mod allocator;
@@ -36,6 +37,7 @@ mod take_in;
3637
mod vec;
3738
mod vec2;
3839

40+
pub use accessor::AllocatorAccessor;
3941
pub use address::{Address, GetAddress};
4042
pub use allocator::Allocator;
4143
pub use boxed::Box;

crates/oxc_allocator/src/take_in.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::{cell::Cell, mem, num};
22

3-
use crate::{Allocator, Box, Vec};
3+
use crate::{Allocator, AllocatorAccessor, Box, Vec};
44

55
/// A trait to replace an existing AST node with a dummy.
66
pub trait TakeIn<'a>: Dummy<'a> {
77
/// Replace node with a dummy.
88
#[must_use]
9-
fn take_in(&mut self, allocator: &'a Allocator) -> Self {
9+
fn take_in<A: AllocatorAccessor<'a>>(&mut self, allocator_accessor: A) -> Self {
10+
let allocator = allocator_accessor.allocator();
1011
let dummy = Dummy::dummy(allocator);
1112
mem::replace(self, dummy)
1213
}

crates/oxc_ast/src/ast_builder_impl.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::borrow::Cow;
44

5-
use oxc_allocator::{Allocator, Box, FromIn, IntoIn, String, Vec};
5+
use oxc_allocator::{Allocator, AllocatorAccessor, Box, FromIn, IntoIn, String, Vec};
66
use oxc_span::{Atom, SPAN, Span};
77
use oxc_syntax::{number::NumberBase, operator::UnaryOperator, scope::ScopeId};
88

@@ -19,6 +19,13 @@ impl<'a, T> FromIn<'a, NONE> for Option<Box<'a, T>> {
1919
}
2020
}
2121

22+
impl<'a> AllocatorAccessor<'a> for AstBuilder<'a> {
23+
#[inline]
24+
fn allocator(self) -> &'a Allocator {
25+
self.allocator
26+
}
27+
}
28+
2229
impl<'a> AstBuilder<'a> {
2330
/// Create a new AST builder that will allocate nodes in the given allocator.
2431
#[inline]

crates/oxc_isolated_declarations/src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'a> IsolatedDeclarations<'a> {
187187
stmts.iter_mut().for_each(|stmt| {
188188
if let Statement::ExportNamedDeclaration(decl) = stmt {
189189
if let Some(declaration) = &mut decl.declaration {
190-
*stmt = Statement::from(declaration.take_in(self.ast.allocator));
190+
*stmt = Statement::from(declaration.take_in(self.ast));
191191
}
192192
}
193193
});

crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a> LatePeepholeOptimizations {
2222
*expr =
2323
MemberExpression::StaticMemberExpression(ctx.ast.alloc_static_member_expression(
2424
e.span,
25-
e.object.take_in(ctx.ast.allocator),
25+
e.object.take_in(ctx.ast),
2626
property,
2727
e.optional,
2828
));

crates/oxc_minifier/src/peephole/fold_constants.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a> PeepholeOptimizations {
132132
// (TRUE || x) => TRUE (also, (3 || x) => 3)
133133
// (FALSE && x) => FALSE
134134
if if lval { op.is_or() } else { op.is_and() } {
135-
return Some(logical_expr.left.take_in(ctx.ast.allocator));
135+
return Some(logical_expr.left.take_in(ctx.ast));
136136
} else if !left.may_have_side_effects(&ctx) {
137137
let should_keep_indirect_access =
138138
Self::should_keep_indirect_access(&logical_expr.right, ctx);
@@ -147,19 +147,19 @@ impl<'a> PeepholeOptimizations {
147147
None,
148148
NumberBase::Decimal,
149149
),
150-
logical_expr.right.take_in(ctx.ast.allocator),
150+
logical_expr.right.take_in(ctx.ast),
151151
]),
152152
));
153153
}
154154
// (FALSE || x) => x
155155
// (TRUE && x) => x
156-
return Some(logical_expr.right.take_in(ctx.ast.allocator));
156+
return Some(logical_expr.right.take_in(ctx.ast));
157157
}
158158
// Left side may have side effects, but we know its boolean value.
159159
// e.g. true_with_sideeffects || foo() => true_with_sideeffects, foo()
160160
// or: false_with_sideeffects && foo() => false_with_sideeffects, foo()
161-
let left = logical_expr.left.take_in(ctx.ast.allocator);
162-
let right = logical_expr.right.take_in(ctx.ast.allocator);
161+
let left = logical_expr.left.take_in(ctx.ast);
162+
let right = logical_expr.right.take_in(ctx.ast);
163163
let vec = ctx.ast.vec_from_array([left, right]);
164164
let sequence_expr = ctx.ast.expression_sequence(logical_expr.span, vec);
165165
return Some(sequence_expr);
@@ -174,8 +174,8 @@ impl<'a> PeepholeOptimizations {
174174
if !right_boolean && left_child_op.is_or()
175175
|| right_boolean && left_child_op.is_and()
176176
{
177-
let left = left_child.left.take_in(ctx.ast.allocator);
178-
let right = logical_expr.right.take_in(ctx.ast.allocator);
177+
let left = left_child.left.take_in(ctx.ast);
178+
let right = logical_expr.right.take_in(ctx.ast);
179179
let logic_expr = ctx.ast.expression_logical(
180180
logical_expr.span,
181181
left,
@@ -204,8 +204,8 @@ impl<'a> PeepholeOptimizations {
204204
Some(if left.may_have_side_effects(&ctx) {
205205
// e.g. `(a(), null) ?? 1` => `(a(), null, 1)`
206206
let expressions = ctx.ast.vec_from_array([
207-
logical_expr.left.take_in(ctx.ast.allocator),
208-
logical_expr.right.take_in(ctx.ast.allocator),
207+
logical_expr.left.take_in(ctx.ast),
208+
logical_expr.right.take_in(ctx.ast),
209209
]);
210210
ctx.ast.expression_sequence(logical_expr.span, expressions)
211211
} else {
@@ -222,12 +222,12 @@ impl<'a> PeepholeOptimizations {
222222
None,
223223
NumberBase::Decimal,
224224
),
225-
logical_expr.right.take_in(ctx.ast.allocator),
225+
logical_expr.right.take_in(ctx.ast),
226226
]),
227227
));
228228
}
229229
// nullish condition => this expression evaluates to the right side.
230-
logical_expr.right.take_in(ctx.ast.allocator)
230+
logical_expr.right.take_in(ctx.ast)
231231
})
232232
}
233233
ValueType::Number
@@ -248,12 +248,12 @@ impl<'a> PeepholeOptimizations {
248248
None,
249249
NumberBase::Decimal,
250250
),
251-
logical_expr.left.take_in(ctx.ast.allocator),
251+
logical_expr.left.take_in(ctx.ast),
252252
]),
253253
));
254254
}
255255
// non-nullish condition => this expression evaluates to the left side.
256-
Some(logical_expr.left.take_in(ctx.ast.allocator))
256+
Some(logical_expr.left.take_in(ctx.ast))
257257
}
258258
ValueType::Undetermined => None,
259259
}
@@ -391,14 +391,14 @@ impl<'a> PeepholeOptimizations {
391391
let span = Span::new(left_binary_expr.right.span().start, e.right.span().end);
392392
let value = ctx.ast.atom_from_strs_array([&left_str, &right_str]);
393393
let right = ctx.ast.expression_string_literal(span, value, None);
394-
let left = left_binary_expr.left.take_in(ctx.ast.allocator);
394+
let left = left_binary_expr.left.take_in(ctx.ast);
395395
return Some(ctx.ast.expression_binary(e.span, left, e.operator, right));
396396
}
397397

398398
if let Some(new_right) =
399399
Self::try_fold_add_op(&mut left_binary_expr.right, &mut e.right, ctx)
400400
{
401-
let left = left_binary_expr.left.take_in(ctx.ast.allocator);
401+
let left = left_binary_expr.left.take_in(ctx.ast);
402402
return Some(ctx.ast.expression_binary(e.span, left, e.operator, new_right));
403403
}
404404
}
@@ -437,7 +437,7 @@ impl<'a> PeepholeOptimizations {
437437
}
438438
left.quasis.extend(right.quasis.drain(1..)); // first quasi is already handled
439439
left.expressions.extend(right.expressions.drain(..));
440-
return Some(left_expr.take_in(ctx.ast.allocator));
440+
return Some(left_expr.take_in(ctx.ast));
441441
}
442442

443443
// "`${x}y` + 'z'" => "`${x}yz`"
@@ -453,7 +453,7 @@ impl<'a> PeepholeOptimizations {
453453
.cooked
454454
.map(|cooked| ctx.ast.atom(&(cooked.as_str().to_string() + &right_str)));
455455
last_quasi.value.cooked = new_cooked;
456-
return Some(left_expr.take_in(ctx.ast.allocator));
456+
return Some(left_expr.take_in(ctx.ast));
457457
}
458458
} else if let Expression::TemplateLiteral(right) = right_expr {
459459
// "'x' + `y${z}`" => "`xy${z}`"
@@ -471,17 +471,17 @@ impl<'a> PeepholeOptimizations {
471471
.cooked
472472
.map(|cooked| ctx.ast.atom(&(left_str.into_owned() + cooked.as_str())));
473473
first_quasi.value.cooked = new_cooked;
474-
return Some(right_expr.take_in(ctx.ast.allocator));
474+
return Some(right_expr.take_in(ctx.ast));
475475
}
476476
}
477477

478478
// remove useless `+ ""` (e.g. `typeof foo + ""` -> `typeof foo`)
479479
if Self::evaluates_to_empty_string(left_expr) && right_expr.value_type(&ctx).is_string() {
480-
return Some(right_expr.take_in(ctx.ast.allocator));
480+
return Some(right_expr.take_in(ctx.ast));
481481
} else if Self::evaluates_to_empty_string(right_expr)
482482
&& left_expr.value_type(&ctx).is_string()
483483
{
484-
return Some(left_expr.take_in(ctx.ast.allocator));
484+
return Some(left_expr.take_in(ctx.ast));
485485
}
486486

487487
None
@@ -523,7 +523,7 @@ impl<'a> PeepholeOptimizations {
523523

524524
Some(ctx.ast.expression_binary(
525525
e.span,
526-
expr_to_move.take_in(ctx.ast.allocator),
526+
expr_to_move.take_in(ctx.ast),
527527
op,
528528
ctx.value_to_expr(Span::new(left.right.span().start, e.right.span().end), v),
529529
))

0 commit comments

Comments
 (0)