Skip to content

Commit

Permalink
Changes to allow cheap cloning of JsValues, still maintaining `Statem…
Browse files Browse the repository at this point in the history
…entList` as `Send`
  • Loading branch information
Razican committed Feb 27, 2022
1 parent 49b9c46 commit 4584be7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
40 changes: 25 additions & 15 deletions boa_engine/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use boa_gc::{unsafe_empty_trace, Finalize, Trace};
use num_integer::Integer;
use num_traits::pow::Pow;
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use std::rc::Rc;
use std::{
fmt::{self, Display},
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
Expand All @@ -20,7 +21,7 @@ use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Debug, Finalize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct JsBigInt {
inner: Box<RawBigInt>,
inner: Rc<RawBigInt>,
}

// Safety: BigInt does not contain any objects which needs to be traced,
Expand All @@ -40,7 +41,7 @@ impl JsBigInt {
#[inline]
pub fn zero() -> Self {
Self {
inner: Box::new(RawBigInt::zero()),
inner: Rc::new(RawBigInt::zero()),
}
}

Expand All @@ -54,7 +55,7 @@ impl JsBigInt {
#[inline]
pub fn one() -> Self {
Self {
inner: Box::new(RawBigInt::one()),
inner: Rc::new(RawBigInt::one()),
}
}

Expand Down Expand Up @@ -82,7 +83,7 @@ impl JsBigInt {
#[inline]
pub fn from_string_radix(buf: &str, radix: u32) -> Option<Self> {
Some(Self {
inner: Box::new(RawBigInt::parse_bytes(buf.as_bytes(), radix)?),
inner: Rc::new(RawBigInt::parse_bytes(buf.as_bytes(), radix)?),
})
}

Expand Down Expand Up @@ -284,7 +285,16 @@ impl From<RawBigInt> for JsBigInt {
#[inline]
fn from(value: RawBigInt) -> Self {
Self {
inner: Box::new(value),
inner: Rc::new(value),
}
}
}

impl From<Box<RawBigInt>> for JsBigInt {
#[inline]
fn from(value: Box<RawBigInt>) -> Self {
Self {
inner: value.into(),
}
}
}
Expand All @@ -293,7 +303,7 @@ impl From<i8> for JsBigInt {
#[inline]
fn from(value: i8) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -302,7 +312,7 @@ impl From<u8> for JsBigInt {
#[inline]
fn from(value: u8) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -311,7 +321,7 @@ impl From<i16> for JsBigInt {
#[inline]
fn from(value: i16) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -320,7 +330,7 @@ impl From<u16> for JsBigInt {
#[inline]
fn from(value: u16) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -329,7 +339,7 @@ impl From<i32> for JsBigInt {
#[inline]
fn from(value: i32) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -338,7 +348,7 @@ impl From<u32> for JsBigInt {
#[inline]
fn from(value: u32) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -347,7 +357,7 @@ impl From<i64> for JsBigInt {
#[inline]
fn from(value: i64) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -356,7 +366,7 @@ impl From<u64> for JsBigInt {
#[inline]
fn from(value: u64) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -365,7 +375,7 @@ impl From<isize> for JsBigInt {
#[inline]
fn from(value: isize) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -374,7 +384,7 @@ impl From<usize> for JsBigInt {
#[inline]
fn from(value: usize) -> Self {
Self {
inner: Box::new(RawBigInt::from(value)),
inner: Rc::new(RawBigInt::from(value)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ impl<'b> ByteCompiler<'b> {
)),
Const::Int(v) => self.emit_push_integer(*v),
Const::Num(v) => self.emit_push_rational(*v),
Const::BigInt(v) => self.emit_push_literal(Literal::BigInt(v.clone())),
Const::BigInt(v) => self.emit_push_literal(Literal::BigInt(v.clone().into())),
Const::Bool(true) => self.emit(Opcode::PushTrue, &[]),
Const::Bool(false) => self.emit(Opcode::PushFalse, &[]),
Const::Null => self.emit(Opcode::PushNull, &[]),
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
marker::PhantomData,
ops::Deref,
ptr::{copy_nonoverlapping, NonNull},
rc::Rc,
};

const CONSTANTS_ARRAY: [&str; 127] = [
Expand Down Expand Up @@ -314,7 +315,7 @@ impl Inner {
#[derive(Finalize)]
pub struct JsString {
inner: NonNull<Inner>,
_marker: PhantomData<Box<str>>,
_marker: PhantomData<Rc<str>>,
}

impl Default for JsString {
Expand Down
7 changes: 4 additions & 3 deletions boa_engine/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
cell::Cell,
fmt::{self, Display},
hash::{Hash, Hasher},
rc::Rc,
};

/// A structure that contains the JavaScript well known symbols.
Expand Down Expand Up @@ -249,7 +250,7 @@ struct Inner {
/// This represents a JavaScript symbol primitive.
#[derive(Debug, Clone)]
pub struct JsSymbol {
inner: Box<Inner>,
inner: Rc<Inner>,
}

impl JsSymbol {
Expand All @@ -263,15 +264,15 @@ impl JsSymbol {
});

Self {
inner: Box::new(Inner { hash, description }),
inner: Rc::new(Inner { hash, description }),
}
}

/// Create a new symbol with a specified hash and description.
#[inline]
fn with_hash(hash: u64, description: Option<JsString>) -> Self {
Self {
inner: Box::new(Inner { hash, description }),
inner: Rc::new(Inner { hash, description }),
}
}

Expand Down
25 changes: 18 additions & 7 deletions boa_engine/src/syntax/ast/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
//! [spec]: https://tc39.es/ecma262/#sec-primary-expression-literals
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals

use crate::JsBigInt;
use boa_gc::{Finalize, Trace};
use boa_gc::{unsafe_empty_trace, Finalize, Trace};
use boa_interner::{Interner, Sym, ToInternedString};

use num_bigint::BigInt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

Expand All @@ -25,7 +24,7 @@ use serde::{Deserialize, Serialize};
/// [spec]: https://tc39.es/ecma262/#sec-primary-expression-literals
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
#[derive(Clone, Debug, Finalize, PartialEq)]
pub enum Const {
/// A string literal is zero or more characters enclosed in double (`"`) or single (`'`) quotation marks.
///
Expand Down Expand Up @@ -74,7 +73,7 @@ pub enum Const {
///
/// [spec]: https://tc39.es/ecma262/#sec-terms-and-definitions-bigint-value
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Numeric_literals
BigInt(JsBigInt),
BigInt(Box<BigInt>),

/// The Boolean type has two literal values: `true` and `false`.
///
Expand Down Expand Up @@ -113,6 +112,12 @@ pub enum Const {
Undefined,
}

// Safety: Const does not contain any objects which needs to be traced,
// so this is safe.
unsafe impl Trace for Const {
unsafe_empty_trace!();
}

impl From<Sym> for Const {
fn from(string: Sym) -> Self {
Self::String(string)
Expand All @@ -131,8 +136,14 @@ impl From<i32> for Const {
}
}

impl From<JsBigInt> for Const {
fn from(i: JsBigInt) -> Self {
impl From<BigInt> for Const {
fn from(i: BigInt) -> Self {
Self::BigInt(Box::new(i))
}
}

impl From<Box<BigInt>> for Const {
fn from(i: Box<BigInt>) -> Self {
Self::BigInt(i)
}
}
Expand Down
6 changes: 4 additions & 2 deletions boa_engine/src/syntax/lexer/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{
};
use boa_interner::Interner;
use boa_profiler::Profiler;
use num_bigint::BigInt;
use num_traits::Zero;
use std::{io::Read, str};

/// Number literal lexing.
Expand Down Expand Up @@ -251,7 +253,7 @@ impl<R> Tokenizer<R> for NumberLiteral {

// DecimalBigIntegerLiteral '0n'
return Ok(Token::new(
TokenKind::NumericLiteral(Numeric::BigInt(0.into())),
TokenKind::NumericLiteral(Numeric::BigInt(BigInt::zero().into())),
Span::new(start_pos, cursor.pos()),
));
}
Expand Down Expand Up @@ -380,7 +382,7 @@ impl<R> Tokenizer<R> for NumberLiteral {
let num = match kind {
NumericKind::BigInt(base) => {
Numeric::BigInt(
JsBigInt::from_string_radix(num_str, base).expect("Could not convert to BigInt")
BigInt::parse_bytes(num_str.as_bytes(), base).expect("Could not convert to BigInt").into()
)
}
NumericKind::Rational /* base: 10 */ => {
Expand Down
10 changes: 5 additions & 5 deletions boa_engine/src/syntax/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use crate::{
syntax::ast::{Keyword, Punctuator, Span},
syntax::lexer::template::TemplateString,
JsBigInt,
};
use boa_interner::{Interner, Sym};

use num_bigint::BigInt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -71,7 +71,7 @@ pub enum Numeric {
Integer(i32),

// A BigInt
BigInt(JsBigInt),
BigInt(Box<BigInt>),
}

impl From<f64> for Numeric {
Expand All @@ -88,10 +88,10 @@ impl From<i32> for Numeric {
}
}

impl From<JsBigInt> for Numeric {
impl From<BigInt> for Numeric {
#[inline]
fn from(n: JsBigInt) -> Self {
Self::BigInt(n)
fn from(n: BigInt) -> Self {
Self::BigInt(Box::new(n))
}
}

Expand Down

0 comments on commit 4584be7

Please sign in to comment.