Skip to content

Commit

Permalink
Implement From for Unique and Shared shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed May 19, 2023
1 parent f486bc2 commit c3c68d1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
4 changes: 2 additions & 2 deletions boa_engine/src/object/property_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl PropertyMap {
pub fn from_prototype_unique_shape(prototype: JsPrototype) -> Self {
Self {
indexed_properties: IndexedProperties::default(),
shape: Shape::unique(UniqueShape::new(prototype, PropertyTableInner::default())),
shape: UniqueShape::new(prototype, PropertyTableInner::default()).into(),
storage: Vec::default(),
}
}
Expand All @@ -259,7 +259,7 @@ impl PropertyMap {
let shape = root_shape.shape().change_prototype_transition(prototype);
Self {
indexed_properties: IndexedProperties::default(),
shape: Shape::shared(shape),
shape: shape.into(),
storage: Vec::default(),
}
}
Expand Down
55 changes: 29 additions & 26 deletions boa_engine/src/object/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ pub struct Shape {
}

impl Default for Shape {
#[inline]
fn default() -> Self {
Self::unique(UniqueShape::default())
UniqueShape::default().into()
}
}

Expand All @@ -77,20 +78,6 @@ impl Shape {
/// NOTE: This only applies to [`SharedShape`].
const TRANSITION_COUNT_MAX: u16 = 1024;

/// Create a [`Shape`] from a [`SharedShape`].
pub(crate) fn shared(inner: SharedShape) -> Self {
Self {
inner: Inner::Shared(inner),
}
}

/// Create a [`Shape`] from a [`UniqueShape`].
pub(crate) const fn unique(shape: UniqueShape) -> Self {
Self {
inner: Inner::Unique(shape),
}
}

/// Returns `true` if it's a shared shape, `false` otherwise.
#[inline]
pub const fn is_shared(&self) -> bool {
Expand Down Expand Up @@ -118,11 +105,11 @@ impl Shape {
Inner::Shared(shape) => {
let shape = shape.insert_property_transition(key);
if shape.transition_count() >= Self::TRANSITION_COUNT_MAX {
return Self::unique(shape.to_unique());
return shape.to_unique().into();
}
Self::shared(shape)
shape.into()
}
Inner::Unique(shape) => Self::unique(shape.insert_property_transition(key)),
Inner::Unique(shape) => shape.insert_property_transition(key).into(),
}
}

Expand All @@ -139,9 +126,9 @@ impl Shape {
let change_transition = shape.change_attributes_transition(key);
let shape =
if change_transition.shape.transition_count() >= Self::TRANSITION_COUNT_MAX {
Self::unique(change_transition.shape.to_unique())
change_transition.shape.to_unique().into()
} else {
Self::shared(change_transition.shape)
change_transition.shape.into()
};
ChangeTransition {
shape,
Expand All @@ -160,11 +147,11 @@ impl Shape {
Inner::Shared(shape) => {
let shape = shape.remove_property_transition(key);
if shape.transition_count() >= Self::TRANSITION_COUNT_MAX {
return Self::unique(shape.to_unique());
return shape.to_unique().into();
}
Self::shared(shape)
shape.into()
}
Inner::Unique(shape) => Self::unique(shape.remove_property_transition(key)),
Inner::Unique(shape) => shape.remove_property_transition(key).into(),
}
}

Expand All @@ -174,11 +161,11 @@ impl Shape {
Inner::Shared(shape) => {
let shape = shape.change_prototype_transition(prototype);
if shape.transition_count() >= Self::TRANSITION_COUNT_MAX {
return Self::unique(shape.to_unique());
return shape.to_unique().into();
}
Self::shared(shape)
shape.into()
}
Inner::Unique(shape) => Self::unique(shape.change_prototype_transition(prototype)),
Inner::Unique(shape) => shape.change_prototype_transition(prototype).into(),
}
}

Expand Down Expand Up @@ -217,3 +204,19 @@ impl Shape {
}
}
}

impl From<UniqueShape> for Shape {
fn from(shape: UniqueShape) -> Self {
Self {
inner: Inner::Unique(shape),
}
}
}

impl From<SharedShape> for Shape {
fn from(shape: SharedShape) -> Self {
Self {
inner: Inner::Shared(shape),
}
}
}
7 changes: 4 additions & 3 deletions boa_engine/src/object/shape/root_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use boa_macros::{Finalize, Trace};

use super::SharedShape;

/// Represent the root shape that [`SharedShape`] transitions start from.
/// This is a wrapper around [`SharedShape`] that ensures it's root shape.
///
/// This is a wrapper around [`SharedShape`] that ensures that the shape the root shape.
/// Represent the root shape that [`SharedShape`] transitions start from.
#[derive(Debug, Clone, Trace, Finalize)]
pub struct RootShape {
shape: SharedShape,
}

impl Default for RootShape {
#[inline]
fn default() -> Self {
Self {
shape: SharedShape::root(),
Expand All @@ -20,7 +21,7 @@ impl Default for RootShape {

impl RootShape {
/// Gets the inner [`SharedShape`].
pub(crate) const fn shape(&self) -> &SharedShape {
pub const fn shape(&self) -> &SharedShape {
&self.shape
}
}
17 changes: 7 additions & 10 deletions boa_engine/src/object/shape/shared_shape/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use boa_gc::{Finalize, Trace};
use thin_vec::ThinVec;

use crate::{
object::{
shape::{slot::SlotAttributes, Shape},
JsObject, Object, ObjectData, PropertyMap,
},
object::{shape::slot::SlotAttributes, JsObject, Object, ObjectData, PropertyMap},
property::{Attribute, PropertyKey},
JsValue,
};
Expand All @@ -21,15 +18,15 @@ pub(crate) struct ObjectTemplate {

impl ObjectTemplate {
/// Create a new [`ObjectTemplate`]
pub(crate) fn new(root_shape: &SharedShape) -> Self {
pub(crate) fn new(shape: &SharedShape) -> Self {
Self {
shape: root_shape.clone(),
shape: shape.clone(),
}
}

/// Create and [`ObjectTemplate`] with a prototype.
pub(crate) fn with_prototype(root_shape: &SharedShape, prototype: JsObject) -> Self {
let shape = root_shape.change_prototype_transition(Some(prototype));
pub(crate) fn with_prototype(shape: &SharedShape, prototype: JsObject) -> Self {
let shape = shape.change_prototype_transition(Some(prototype));
Self { shape }
}

Expand Down Expand Up @@ -111,7 +108,7 @@ impl ObjectTemplate {
let mut object = Object {
kind: data.kind,
extensible: true,
properties: PropertyMap::new(Shape::shared(self.shape.clone()), ThinVec::default()),
properties: PropertyMap::new(self.shape.clone().into(), ThinVec::default()),
private_elements: ThinVec::new(),
};

Expand All @@ -133,7 +130,7 @@ impl ObjectTemplate {
let mut object = Object {
kind: data.kind,
extensible: true,
properties: PropertyMap::new(Shape::shared(self.shape.clone()), elements),
properties: PropertyMap::new(self.shape.clone().into(), elements),
private_elements: ThinVec::new(),
};

Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/object/shape/unique_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl UniqueShape {
property_table.keys[index].1.attributes = key.attributes;
// TODO: invalidate the pointer.
return ChangeTransition {
shape: Shape::unique(self.clone()),
shape: self.clone().into(),
action: ChangeTransitionAction::Nothing,
};
}
Expand Down Expand Up @@ -211,7 +211,7 @@ impl UniqueShape {
let shape = Self::new(prototype, property_table);

ChangeTransition {
shape: Shape::unique(shape),
shape: shape.into(),
action,
}
}
Expand Down

0 comments on commit c3c68d1

Please sign in to comment.