Skip to content

Commit

Permalink
Implement serialize and deserialize for primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 30, 2023
1 parent 25356f6 commit dcafd65
Show file tree
Hide file tree
Showing 18 changed files with 789 additions and 81 deletions.
23 changes: 23 additions & 0 deletions boa_engine/src/builtins/iterable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ pub struct IteratorPrototypes {
segment: JsObject,
}

impl crate::snapshot::Serialize for IteratorPrototypes {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.iterator.serialize(s)?;
self.async_iterator.serialize(s)?;
self.async_from_sync_iterator.serialize(s)?;
self.array.serialize(s)?;
self.set.serialize(s)?;
self.string.serialize(s)?;
self.regexp_string.serialize(s)?;
self.map.serialize(s)?;
self.for_in.serialize(s)?;
#[cfg(feature = "intl")]
{
self.segment.serialize(s)?;
}

Ok(())
}
}

impl IteratorPrototypes {
/// Returns the `ArrayIteratorPrototype` object.
#[inline]
Expand Down
21 changes: 21 additions & 0 deletions boa_engine/src/builtins/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ pub struct UriFunctions {
encode_uri_component: JsFunction,
}

impl crate::snapshot::Serialize for UriFunctions {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.decode_uri.serialize(s)?;
self.decode_uri_component.serialize(s)?;
self.encode_uri.serialize(s)?;
self.encode_uri_component.serialize(s)?;
Ok(())
}
}

impl crate::snapshot::Deserialize for UriFunctions {
fn deserialize(
_d: &mut crate::snapshot::SnapshotDeserializer<'_>,
) -> crate::snapshot::SnapshotResult<Self> {
todo!()
}
}

impl Default for UriFunctions {
fn default() -> Self {
Self {
Expand Down
63 changes: 63 additions & 0 deletions boa_engine/src/context/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl crate::snapshot::Serialize for Intrinsics {
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.constructors.serialize(s)?;
self.objects.serialize(s)?;
self.templates.serialize(s)?;
Ok(())
}
}
Expand Down Expand Up @@ -896,6 +898,40 @@ pub struct IntrinsicObjects {
segments_prototype: JsObject,
}

impl crate::snapshot::Serialize for IntrinsicObjects {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.reflect.serialize(s)?;
self.math.serialize(s)?;
self.json.serialize(s)?;
self.throw_type_error.serialize(s)?;
self.array_prototype_values.serialize(s)?;
self.iterator_prototypes.serialize(s)?;
self.generator.serialize(s)?;
self.async_generator.serialize(s)?;
self.eval.serialize(s)?;
self.uri_functions.serialize(s)?;
self.is_finite.serialize(s)?;
self.is_nan.serialize(s)?;
self.parse_float.serialize(s)?;
self.parse_int.serialize(s)?;
#[cfg(feature = "annex-b")]
{
self.escape.serialize(s)?;
self.unescape.serialize(s)?;
}
#[cfg(feature = "intl")]
{
self.intl.serialize(s)?;
self.segments_prototype.serialize(s)?;
}

Ok(())
}
}

impl Default for IntrinsicObjects {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -1085,6 +1121,33 @@ pub(crate) struct ObjectTemplates {
namespace: ObjectTemplate,
}

impl crate::snapshot::Serialize for ObjectTemplates {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.iterator_result.serialize(s)?;
self.ordinary_object.serialize(s)?;
self.array.serialize(s)?;
self.number.serialize(s)?;
self.string.serialize(s)?;
self.symbol.serialize(s)?;
self.bigint.serialize(s)?;
self.boolean.serialize(s)?;

self.unmapped_arguments.serialize(s)?;
self.mapped_arguments.serialize(s)?;
self.function_with_prototype.serialize(s)?;
self.function_prototype.serialize(s)?;
self.function.serialize(s)?;
self.async_function.serialize(s)?;
self.function_without_proto.serialize(s)?;
self.function_with_prototype_without_proto.serialize(s)?;
self.namespace.serialize(s)?;
Ok(())
}
}

impl ObjectTemplates {
pub(crate) fn new(root_shape: &RootShape, constructors: &StandardConstructors) -> Self {
let root_shape = root_shape.shape();
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,7 @@ impl crate::snapshot::Deserialize for Context<'_> {
) -> Result<Self, crate::snapshot::SnapshotError> {
let strict = d.read_bool()?;
let optimizer_options = OptimizerOptions::deserialize(d)?;
// let realm = Realm::deserialize(d)?;
let mut context = Context::default();

context.strict(strict);
Expand Down
24 changes: 24 additions & 0 deletions boa_engine/src/object/shape/property_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ pub(crate) struct PropertyTableInner {
pub(crate) keys: Vec<(PropertyKey, Slot)>,
}

impl crate::snapshot::Serialize for PropertyTableInner {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.map.serialize(s)?;
self.keys.serialize(s)?;
Ok(())
}
}

impl PropertyTableInner {
/// Returns all the keys, in insertion order.
pub(crate) fn keys(&self) -> Vec<PropertyKey> {
Expand Down Expand Up @@ -73,6 +84,19 @@ pub(crate) struct PropertyTable {
pub(super) inner: Rc<RefCell<PropertyTableInner>>,
}

impl crate::snapshot::Serialize for PropertyTable {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
let ptr = self.inner.as_ptr() as usize;
s.reference_or(ptr, |s| {
self.inner.borrow().serialize(s)?;
Ok(())
})
}
}

impl PropertyTable {
/// Returns the inner representation of a [`PropertyTable`].
pub(super) fn inner(&self) -> &RefCell<PropertyTableInner> {
Expand Down
39 changes: 39 additions & 0 deletions boa_engine/src/object/shape/shared_shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ bitflags! {
}
}

impl crate::snapshot::Serialize for ShapeFlags {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.bits().serialize(s)?;
Ok(())
}
}

impl Default for ShapeFlags {
fn default() -> Self {
Self::empty()
Expand Down Expand Up @@ -113,12 +123,41 @@ struct Inner {
flags: ShapeFlags,
}

impl crate::snapshot::Serialize for Inner {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.property_count.serialize(s)?;
self.prototype.serialize(s)?;
self.property_table.serialize(s)?;
self.previous.serialize(s)?;
self.transition_count.serialize(s)?;
self.flags.serialize(s)?;
Ok(())
}
}

/// Represents a shared object shape.
#[derive(Debug, Trace, Finalize, Clone)]
pub struct SharedShape {
inner: Gc<Inner>,
}

impl crate::snapshot::Serialize for SharedShape {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
let ptr: *const _ = self.inner.as_ref();

s.reference_or(ptr as usize, |s| {
self.inner.as_ref().serialize(s)?;
Ok(())
})
}
}

impl SharedShape {
fn property_table(&self) -> &PropertyTable {
&self.inner.property_table
Expand Down
10 changes: 10 additions & 0 deletions boa_engine/src/object/shape/shared_shape/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ pub(crate) struct ObjectTemplate {
shape: SharedShape,
}

impl crate::snapshot::Serialize for ObjectTemplate {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.shape.serialize(s)?;
Ok(())
}
}

impl ObjectTemplate {
/// Create a new [`ObjectTemplate`]
pub(crate) fn new(shape: &SharedShape) -> Self {
Expand Down
21 changes: 21 additions & 0 deletions boa_engine/src/object/shape/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ bitflags! {
}
}

impl crate::snapshot::Serialize for SlotAttributes {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.bits().serialize(s)?;
Ok(())
}
}

impl SlotAttributes {
pub(crate) const fn is_accessor_descriptor(self) -> bool {
self.contains(Self::GET) || self.contains(Self::SET)
Expand Down Expand Up @@ -48,6 +58,17 @@ pub(crate) struct Slot {
pub(crate) attributes: SlotAttributes,
}

impl crate::snapshot::Serialize for Slot {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.index.serialize(s)?;
self.attributes.serialize(s)?;
Ok(())
}
}

impl Slot {
/// Get the width of the slot.
pub(crate) fn width(self) -> u32 {
Expand Down
40 changes: 40 additions & 0 deletions boa_engine/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,46 @@ pub enum PropertyKey {
Index(u32),
}

impl crate::snapshot::Serialize for PropertyKey {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
match self {
PropertyKey::String(v) => {
s.write_u8(0)?;
v.serialize(s)?
}
PropertyKey::Symbol(v) => {
s.write_u8(1)?;
v.serialize(s)?
}
PropertyKey::Index(v) => {
s.write_u8(2)?;
v.serialize(s)?
}
}

Ok(())
}
}

impl crate::snapshot::Deserialize for PropertyKey {
fn deserialize(
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
) -> crate::snapshot::SnapshotResult<Self> {
let typ = u8::deserialize(d)?;
let result = match typ {
0 => Self::String(JsString::deserialize(d)?),
1 => Self::Symbol(JsSymbol::deserialize(d)?),
2 => Self::Index(u32::deserialize(d)?),
_ => unreachable!("corrupted snapshot!"),
};

Ok(result)
}
}

/// Utility function for parsing [`PropertyKey`].
fn parse_u32_index<I, T>(mut input: I) -> Option<u32>
where
Expand Down
25 changes: 25 additions & 0 deletions boa_engine/src/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ impl crate::snapshot::Serialize for Realm {
}
}

impl crate::snapshot::Deserialize for Realm {
fn deserialize(
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
) -> Result<Self, crate::snapshot::SnapshotError> {
let inner = Inner::deserialize(d)?;
Ok(Realm {
inner: Gc::new(inner),
})
}
}

impl Eq for Realm {}

impl PartialEq for Realm {
Expand Down Expand Up @@ -70,10 +81,24 @@ impl crate::snapshot::Serialize for Inner {
s: &mut crate::snapshot::SnapshotSerializer,
) -> Result<(), crate::snapshot::SnapshotError> {
self.intrinsics.serialize(s)?;
self.global_object.serialize(s)?;
self.global_this.serialize(s)?;
self.template_map.borrow().serialize(s)?;
Ok(())
}
}

impl crate::snapshot::Deserialize for Inner {
fn deserialize(
_d: &mut crate::snapshot::SnapshotDeserializer<'_>,
) -> Result<Self, crate::snapshot::SnapshotError> {
// let intrinsics = Intrinsics::deserialize(d)?;

// Ok(Inner::)
todo!()
}
}

impl Realm {
/// Create a new Realm.
#[inline]
Expand Down
Loading

0 comments on commit dcafd65

Please sign in to comment.