Skip to content

Commit

Permalink
feat(allocator,ast): add BoxWithDrop<BigInt>
Browse files Browse the repository at this point in the history
relates #1803
  • Loading branch information
Boshen committed Jan 29, 2024
1 parent 8e7c376 commit fb3d877
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ workspace = true
doctest = false

[dependencies]
bumpalo = { workspace = true, features = ["collections"] }
bumpalo = { workspace = true, features = ["collections", "boxed"] }
serde = { workspace = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_allocator/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::{ser::SerializeSeq, Serialize, Serializer};

use crate::Allocator;

/// Bumpalo Box
/// A Box without `Drop`
pub struct Box<'alloc, T: ?Sized>(pub &'alloc mut T);

impl<'alloc, T> Box<'alloc, T> {
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{convert::From, ops::Deref};
mod arena;

pub use arena::{Box, String, Vec};
pub use bumpalo::boxed::Box as BoxWithDrop;
use bumpalo::Bump;

#[derive(Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum Expression<'a> {
BooleanLiteral(Box<'a, BooleanLiteral>),
NullLiteral(Box<'a, NullLiteral>),
NumberLiteral(Box<'a, NumberLiteral<'a>>),
BigintLiteral(Box<'a, BigintLiteral>),
BigintLiteral(Box<'a, BigintLiteral<'a>>),
RegExpLiteral(Box<'a, RegExpLiteral>),
StringLiteral(Box<'a, StringLiteral>),
TemplateLiteral(Box<'a, TemplateLiteral<'a>>),
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_ast/src/ast/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{

use bitflags::bitflags;
use num_bigint::BigInt;
use oxc_allocator::BoxWithDrop;
use oxc_span::{Atom, Span};
use oxc_syntax::{BigintBase, NumberBase};
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -104,13 +105,13 @@ impl<'a> Hash for NumberLiteral<'a> {
}
}

#[derive(Debug, Clone, Hash)]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize), serde(tag = "type"))]
pub struct BigintLiteral {
pub struct BigintLiteral<'a> {
#[cfg_attr(feature = "serde", serde(flatten))]
pub span: Span,
#[cfg_attr(feature = "serde", serde(serialize_with = "crate::serialize::serialize_bigint"))]
pub value: BigInt,
pub value: BoxWithDrop<'a, BigInt>,
#[cfg_attr(feature = "serde", serde(skip))]
pub base: BigintBase,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub enum TSLiteral<'a> {
BooleanLiteral(Box<'a, BooleanLiteral>),
NullLiteral(Box<'a, NullLiteral>),
NumberLiteral(Box<'a, NumberLiteral<'a>>),
BigintLiteral(Box<'a, BigintLiteral>),
BigintLiteral(Box<'a, BigintLiteral<'a>>),
RegExpLiteral(Box<'a, RegExpLiteral>),
StringLiteral(Box<'a, StringLiteral>),
TemplateLiteral(Box<'a, TemplateLiteral<'a>>),
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use num_bigint::BigInt;
use std::mem;

use oxc_allocator::{Allocator, Box, String, Vec};
use oxc_allocator::{Allocator, Box, BoxWithDrop, String, Vec};
use oxc_span::{Atom, GetSpan, SourceType, Span};
use oxc_syntax::{
operator::{
Expand Down Expand Up @@ -136,8 +136,8 @@ impl<'a> AstBuilder<'a> {
BooleanLiteral { span, value }
}

pub fn bigint_literal(&self, span: Span, value: BigInt, base: BigintBase) -> BigintLiteral {
BigintLiteral { span, value, base }
pub fn bigint_literal(&self, span: Span, value: BigInt, base: BigintBase) -> BigintLiteral<'a> {
BigintLiteral { span, value: BoxWithDrop::new_in(value, self.allocator), base }
}

pub fn template_literal(
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'a> AstBuilder<'a> {
Expression::NumberLiteral(self.alloc(literal))
}

pub fn literal_bigint_expression(&self, literal: BigintLiteral) -> Expression<'a> {
pub fn literal_bigint_expression(&self, literal: BigintLiteral<'a>) -> Expression<'a> {
Expression::BigintLiteral(self.alloc(literal))
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub enum AstKind<'a> {
StringLiteral(&'a StringLiteral),
BooleanLiteral(&'a BooleanLiteral),
NullLiteral(&'a NullLiteral),
BigintLiteral(&'a BigintLiteral),
BigintLiteral(&'a BigintLiteral<'a>),
RegExpLiteral(&'a RegExpLiteral),
TemplateLiteral(&'a TemplateLiteral<'a>),

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ pub trait Visit<'a>: Sized {
self.leave_node(kind);
}

fn visit_bigint_literal(&mut self, lit: &BigintLiteral) {
fn visit_bigint_literal(&mut self, lit: &BigintLiteral<'a>) {
let kind = AstKind::BigintLiteral(self.alloc(lit));
self.enter_node(kind);
self.leave_node(kind);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ pub trait VisitMut<'a>: Sized {
self.leave_node(kind);
}

fn visit_bigint_literal(&mut self, lit: &mut BigintLiteral) {
fn visit_bigint_literal(&mut self, lit: &mut BigintLiteral<'a>) {
let kind = AstKind::BigintLiteral(self.alloc(lit));
self.enter_node(kind);
self.leave_node(kind);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ fn print_non_negative_float<const MINIFY: bool>(value: f64, _p: &Codegen<{ MINIF
result
}

impl<const MINIFY: bool> Gen<MINIFY> for BigintLiteral {
impl<'a, const MINIFY: bool> Gen<MINIFY> for BigintLiteral<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, _ctx: Context) {
use num_bigint::Sign;

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl<'a> Parser<'a> {
Ok(NumberLiteral::new(self.end_span(span), value, src, base))
}

pub(crate) fn parse_literal_bigint(&mut self) -> Result<BigintLiteral> {
pub(crate) fn parse_literal_bigint(&mut self) -> Result<BigintLiteral<'a>> {
let span = self.start_span();
let base = match self.cur_kind() {
Kind::Decimal => BigintBase::Decimal,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ impl<'a> Format<'a> for NumberLiteral<'a> {
}
}

impl<'a> Format<'a> for BigintLiteral {
impl<'a> Format<'a> for BigintLiteral<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let text = self.span.source_text(p.source_text);
// Perf: avoid a memory allocation from `to_ascii_lowercase`.
Expand Down

0 comments on commit fb3d877

Please sign in to comment.