Skip to content

Commit

Permalink
Removed reference counted pointers from JsValue variants
Browse files Browse the repository at this point in the history
This makes `JsBigInt` and `JsSymbol` implement `Send`.
It also modifies the `JsObject` variant so that it has a similar structure
to other variants, where the internal structure is private.
  • Loading branch information
Razican committed Feb 23, 2022
1 parent 2c19c6a commit c325122
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
31 changes: 15 additions & 16 deletions boa_engine/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use std::{
fmt::{self, Display},
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
rc::Rc,
};

/// The raw bigint type.
Expand All @@ -21,7 +20,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: Rc<RawBigInt>,
inner: Box<RawBigInt>,
}

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

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

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

Expand Down Expand Up @@ -285,7 +284,7 @@ impl From<RawBigInt> for JsBigInt {
#[inline]
fn from(value: RawBigInt) -> Self {
Self {
inner: Rc::new(value),
inner: Box::new(value),
}
}
}
Expand All @@ -294,7 +293,7 @@ impl From<i8> for JsBigInt {
#[inline]
fn from(value: i8) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -303,7 +302,7 @@ impl From<u8> for JsBigInt {
#[inline]
fn from(value: u8) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -312,7 +311,7 @@ impl From<i16> for JsBigInt {
#[inline]
fn from(value: i16) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -321,7 +320,7 @@ impl From<u16> for JsBigInt {
#[inline]
fn from(value: u16) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -330,7 +329,7 @@ impl From<i32> for JsBigInt {
#[inline]
fn from(value: i32) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -339,7 +338,7 @@ impl From<u32> for JsBigInt {
#[inline]
fn from(value: u32) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -348,7 +347,7 @@ impl From<i64> for JsBigInt {
#[inline]
fn from(value: i64) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -357,7 +356,7 @@ impl From<u64> for JsBigInt {
#[inline]
fn from(value: u64) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -366,7 +365,7 @@ impl From<isize> for JsBigInt {
#[inline]
fn from(value: isize) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand All @@ -375,7 +374,7 @@ impl From<usize> for JsBigInt {
#[inline]
fn from(value: usize) -> Self {
Self {
inner: Rc::new(RawBigInt::from(value)),
inner: Box::new(RawBigInt::from(value)),
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions boa_engine/src/object/jsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ pub type RefMut<'a, T, U> = boa_gc::RefMut<'a, T, U>;

/// Garbage collected `Object`.
#[derive(Trace, Finalize, Clone, Default)]
pub struct JsObject(Gc<boa_gc::Cell<Object>>);
pub struct JsObject {
inner: Gc<boa_gc::Cell<Object>>,
}

impl JsObject {
/// Create a new `JsObject` from an internal `Object`.
#[inline]
fn from_object(object: Object) -> Self {
Self(Gc::new(boa_gc::Cell::new(object)))
Self {
inner: Gc::new(boa_gc::Cell::new(object)),
}
}

/// Create a new empty `JsObject`, with `prototype` set to `JsValue::Null`
Expand Down Expand Up @@ -90,7 +94,7 @@ impl JsObject {
/// This is the non-panicking variant of [`borrow`](#method.borrow).
#[inline]
pub fn try_borrow(&self) -> StdResult<Ref<'_, Object>, BorrowError> {
self.0.try_borrow().map_err(|_| BorrowError)
self.inner.try_borrow().map_err(|_| BorrowError)
}

/// Mutably borrows the object, returning an error if the value is currently borrowed.
Expand All @@ -101,7 +105,7 @@ impl JsObject {
/// This is the non-panicking variant of [`borrow_mut`](#method.borrow_mut).
#[inline]
pub fn try_borrow_mut(&self) -> StdResult<RefMut<'_, Object, Object>, BorrowMutError> {
self.0.try_borrow_mut().map_err(|_| BorrowMutError)
self.inner.try_borrow_mut().map_err(|_| BorrowMutError)
}

/// Checks if the garbage collected memory is the same.
Expand Down Expand Up @@ -658,7 +662,7 @@ Cannot both specify accessors and a value or writable attribute",
impl AsRef<boa_gc::Cell<Object>> for JsObject {
#[inline]
fn as_ref(&self) -> &boa_gc::Cell<Object> {
&*self.0
&*self.inner
}
}

Expand Down Expand Up @@ -787,7 +791,7 @@ impl Debug for JsObject {
// Instead, we check if the object has appeared before in the entire graph. This means that objects will appear
// at most once, hopefully making things a bit clearer.
if !limiter.visited && !limiter.live {
f.debug_tuple("JsObject").field(&self.0).finish()
f.debug_tuple("JsObject").field(&self.inner).finish()
} else {
f.write_str("{ ... }")
}
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ impl Inner {
#[derive(Finalize)]
pub struct JsString {
inner: NonNull<Inner>,
_marker: PhantomData<std::rc::Rc<str>>,
_marker: PhantomData<Box<str>>,
}

impl Default for JsString {
Expand Down
7 changes: 3 additions & 4 deletions boa_engine/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ 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 @@ -250,7 +249,7 @@ struct Inner {
/// This represents a JavaScript symbol primitive.
#[derive(Debug, Clone)]
pub struct JsSymbol {
inner: Rc<Inner>,
inner: Box<Inner>,
}

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

Self {
inner: Rc::new(Inner { hash, description }),
inner: Box::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: Rc::new(Inner { hash, description }),
inner: Box::new(Inner { hash, description }),
}
}

Expand Down

0 comments on commit c325122

Please sign in to comment.