Skip to content

Commit 38ade77

Browse files
committed
Implement serialize and deserialize for primitive types
1 parent de3e7c5 commit 38ade77

File tree

19 files changed

+790
-82
lines changed

19 files changed

+790
-82
lines changed

boa_engine/src/builtins/iterable/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,29 @@ pub struct IteratorPrototypes {
7474
segment: JsObject,
7575
}
7676

77+
impl crate::snapshot::Serialize for IteratorPrototypes {
78+
fn serialize(
79+
&self,
80+
s: &mut crate::snapshot::SnapshotSerializer,
81+
) -> Result<(), crate::snapshot::SnapshotError> {
82+
self.iterator.serialize(s)?;
83+
self.async_iterator.serialize(s)?;
84+
self.async_from_sync_iterator.serialize(s)?;
85+
self.array.serialize(s)?;
86+
self.set.serialize(s)?;
87+
self.string.serialize(s)?;
88+
self.regexp_string.serialize(s)?;
89+
self.map.serialize(s)?;
90+
self.for_in.serialize(s)?;
91+
#[cfg(feature = "intl")]
92+
{
93+
self.segment.serialize(s)?;
94+
}
95+
96+
Ok(())
97+
}
98+
}
99+
77100
impl IteratorPrototypes {
78101
/// Returns the `ArrayIteratorPrototype` object.
79102
#[inline]

boa_engine/src/builtins/uri/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,27 @@ pub struct UriFunctions {
4747
encode_uri_component: JsFunction,
4848
}
4949

50+
impl crate::snapshot::Serialize for UriFunctions {
51+
fn serialize(
52+
&self,
53+
s: &mut crate::snapshot::SnapshotSerializer,
54+
) -> Result<(), crate::snapshot::SnapshotError> {
55+
self.decode_uri.serialize(s)?;
56+
self.decode_uri_component.serialize(s)?;
57+
self.encode_uri.serialize(s)?;
58+
self.encode_uri_component.serialize(s)?;
59+
Ok(())
60+
}
61+
}
62+
63+
impl crate::snapshot::Deserialize for UriFunctions {
64+
fn deserialize(
65+
_d: &mut crate::snapshot::SnapshotDeserializer<'_>,
66+
) -> crate::snapshot::SnapshotResult<Self> {
67+
todo!()
68+
}
69+
}
70+
5071
impl Default for UriFunctions {
5172
fn default() -> Self {
5273
Self {

boa_engine/src/context/intrinsics.rs

+63
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl crate::snapshot::Serialize for Intrinsics {
3232
s: &mut crate::snapshot::SnapshotSerializer,
3333
) -> Result<(), crate::snapshot::SnapshotError> {
3434
self.constructors.serialize(s)?;
35+
self.objects.serialize(s)?;
36+
self.templates.serialize(s)?;
3537
Ok(())
3638
}
3739
}
@@ -896,6 +898,40 @@ pub struct IntrinsicObjects {
896898
segments_prototype: JsObject,
897899
}
898900

901+
impl crate::snapshot::Serialize for IntrinsicObjects {
902+
fn serialize(
903+
&self,
904+
s: &mut crate::snapshot::SnapshotSerializer,
905+
) -> Result<(), crate::snapshot::SnapshotError> {
906+
self.reflect.serialize(s)?;
907+
self.math.serialize(s)?;
908+
self.json.serialize(s)?;
909+
self.throw_type_error.serialize(s)?;
910+
self.array_prototype_values.serialize(s)?;
911+
self.iterator_prototypes.serialize(s)?;
912+
self.generator.serialize(s)?;
913+
self.async_generator.serialize(s)?;
914+
self.eval.serialize(s)?;
915+
self.uri_functions.serialize(s)?;
916+
self.is_finite.serialize(s)?;
917+
self.is_nan.serialize(s)?;
918+
self.parse_float.serialize(s)?;
919+
self.parse_int.serialize(s)?;
920+
#[cfg(feature = "annex-b")]
921+
{
922+
self.escape.serialize(s)?;
923+
self.unescape.serialize(s)?;
924+
}
925+
#[cfg(feature = "intl")]
926+
{
927+
self.intl.serialize(s)?;
928+
self.segments_prototype.serialize(s)?;
929+
}
930+
931+
Ok(())
932+
}
933+
}
934+
899935
impl Default for IntrinsicObjects {
900936
fn default() -> Self {
901937
Self {
@@ -1085,6 +1121,33 @@ pub(crate) struct ObjectTemplates {
10851121
namespace: ObjectTemplate,
10861122
}
10871123

1124+
impl crate::snapshot::Serialize for ObjectTemplates {
1125+
fn serialize(
1126+
&self,
1127+
s: &mut crate::snapshot::SnapshotSerializer,
1128+
) -> Result<(), crate::snapshot::SnapshotError> {
1129+
self.iterator_result.serialize(s)?;
1130+
self.ordinary_object.serialize(s)?;
1131+
self.array.serialize(s)?;
1132+
self.number.serialize(s)?;
1133+
self.string.serialize(s)?;
1134+
self.symbol.serialize(s)?;
1135+
self.bigint.serialize(s)?;
1136+
self.boolean.serialize(s)?;
1137+
1138+
self.unmapped_arguments.serialize(s)?;
1139+
self.mapped_arguments.serialize(s)?;
1140+
self.function_with_prototype.serialize(s)?;
1141+
self.function_prototype.serialize(s)?;
1142+
self.function.serialize(s)?;
1143+
self.async_function.serialize(s)?;
1144+
self.function_without_proto.serialize(s)?;
1145+
self.function_with_prototype_without_proto.serialize(s)?;
1146+
self.namespace.serialize(s)?;
1147+
Ok(())
1148+
}
1149+
}
1150+
10881151
impl ObjectTemplates {
10891152
pub(crate) fn new(root_shape: &RootShape, constructors: &StandardConstructors) -> Self {
10901153
let root_shape = root_shape.shape();

boa_engine/src/context/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ impl crate::snapshot::Deserialize for Context<'_> {
10041004
) -> Result<Self, crate::snapshot::SnapshotError> {
10051005
let strict = d.read_bool()?;
10061006
let optimizer_options = OptimizerOptions::deserialize(d)?;
1007+
// let realm = Realm::deserialize(d)?;
10071008
let mut context = Context::default();
10081009

10091010
context.strict(strict);

boa_engine/src/object/shape/property_table.rs

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ pub(crate) struct PropertyTableInner {
1414
pub(crate) keys: Vec<(PropertyKey, Slot)>,
1515
}
1616

17+
impl crate::snapshot::Serialize for PropertyTableInner {
18+
fn serialize(
19+
&self,
20+
s: &mut crate::snapshot::SnapshotSerializer,
21+
) -> Result<(), crate::snapshot::SnapshotError> {
22+
self.map.serialize(s)?;
23+
self.keys.serialize(s)?;
24+
Ok(())
25+
}
26+
}
27+
1728
impl PropertyTableInner {
1829
/// Returns all the keys, in insertion order.
1930
pub(crate) fn keys(&self) -> Vec<PropertyKey> {
@@ -73,6 +84,19 @@ pub(crate) struct PropertyTable {
7384
pub(super) inner: Rc<RefCell<PropertyTableInner>>,
7485
}
7586

87+
impl crate::snapshot::Serialize for PropertyTable {
88+
fn serialize(
89+
&self,
90+
s: &mut crate::snapshot::SnapshotSerializer,
91+
) -> Result<(), crate::snapshot::SnapshotError> {
92+
let ptr = self.inner.as_ptr() as usize;
93+
s.reference_or(ptr, |s| {
94+
self.inner.borrow().serialize(s)?;
95+
Ok(())
96+
})
97+
}
98+
}
99+
76100
impl PropertyTable {
77101
/// Returns the inner representation of a [`PropertyTable`].
78102
pub(super) fn inner(&self) -> &RefCell<PropertyTableInner> {

boa_engine/src/object/shape/shared_shape/mod.rs

+39
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ bitflags! {
4949
}
5050
}
5151

52+
impl crate::snapshot::Serialize for ShapeFlags {
53+
fn serialize(
54+
&self,
55+
s: &mut crate::snapshot::SnapshotSerializer,
56+
) -> Result<(), crate::snapshot::SnapshotError> {
57+
self.bits().serialize(s)?;
58+
Ok(())
59+
}
60+
}
61+
5262
impl Default for ShapeFlags {
5363
fn default() -> Self {
5464
Self::empty()
@@ -113,12 +123,41 @@ struct Inner {
113123
flags: ShapeFlags,
114124
}
115125

126+
impl crate::snapshot::Serialize for Inner {
127+
fn serialize(
128+
&self,
129+
s: &mut crate::snapshot::SnapshotSerializer,
130+
) -> Result<(), crate::snapshot::SnapshotError> {
131+
self.property_count.serialize(s)?;
132+
self.prototype.serialize(s)?;
133+
self.property_table.serialize(s)?;
134+
self.previous.serialize(s)?;
135+
self.transition_count.serialize(s)?;
136+
self.flags.serialize(s)?;
137+
Ok(())
138+
}
139+
}
140+
116141
/// Represents a shared object shape.
117142
#[derive(Debug, Trace, Finalize, Clone)]
118143
pub struct SharedShape {
119144
inner: Gc<Inner>,
120145
}
121146

147+
impl crate::snapshot::Serialize for SharedShape {
148+
fn serialize(
149+
&self,
150+
s: &mut crate::snapshot::SnapshotSerializer,
151+
) -> Result<(), crate::snapshot::SnapshotError> {
152+
let ptr: *const _ = self.inner.as_ref();
153+
154+
s.reference_or(ptr as usize, |s| {
155+
self.inner.as_ref().serialize(s)?;
156+
Ok(())
157+
})
158+
}
159+
}
160+
122161
impl SharedShape {
123162
fn property_table(&self) -> &PropertyTable {
124163
&self.inner.property_table

boa_engine/src/object/shape/shared_shape/template.rs

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ pub(crate) struct ObjectTemplate {
1616
shape: SharedShape,
1717
}
1818

19+
impl crate::snapshot::Serialize for ObjectTemplate {
20+
fn serialize(
21+
&self,
22+
s: &mut crate::snapshot::SnapshotSerializer,
23+
) -> Result<(), crate::snapshot::SnapshotError> {
24+
self.shape.serialize(s)?;
25+
Ok(())
26+
}
27+
}
28+
1929
impl ObjectTemplate {
2030
/// Create a new [`ObjectTemplate`]
2131
pub(crate) fn new(shape: &SharedShape) -> Self {

boa_engine/src/object/shape/slot.rs

+21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ bitflags! {
1313
}
1414
}
1515

16+
impl crate::snapshot::Serialize for SlotAttributes {
17+
fn serialize(
18+
&self,
19+
s: &mut crate::snapshot::SnapshotSerializer,
20+
) -> Result<(), crate::snapshot::SnapshotError> {
21+
self.bits().serialize(s)?;
22+
Ok(())
23+
}
24+
}
25+
1626
impl SlotAttributes {
1727
pub(crate) const fn is_accessor_descriptor(self) -> bool {
1828
self.contains(Self::GET) || self.contains(Self::SET)
@@ -48,6 +58,17 @@ pub(crate) struct Slot {
4858
pub(crate) attributes: SlotAttributes,
4959
}
5060

61+
impl crate::snapshot::Serialize for Slot {
62+
fn serialize(
63+
&self,
64+
s: &mut crate::snapshot::SnapshotSerializer,
65+
) -> Result<(), crate::snapshot::SnapshotError> {
66+
self.index.serialize(s)?;
67+
self.attributes.serialize(s)?;
68+
Ok(())
69+
}
70+
}
71+
5172
impl Slot {
5273
/// Get the width of the slot.
5374
pub(crate) fn width(self) -> u32 {

boa_engine/src/property/mod.rs

+40
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,46 @@ pub enum PropertyKey {
582582
Index(u32),
583583
}
584584

585+
impl crate::snapshot::Serialize for PropertyKey {
586+
fn serialize(
587+
&self,
588+
s: &mut crate::snapshot::SnapshotSerializer,
589+
) -> Result<(), crate::snapshot::SnapshotError> {
590+
match self {
591+
PropertyKey::String(v) => {
592+
s.write_u8(0)?;
593+
v.serialize(s)?
594+
}
595+
PropertyKey::Symbol(v) => {
596+
s.write_u8(1)?;
597+
v.serialize(s)?
598+
}
599+
PropertyKey::Index(v) => {
600+
s.write_u8(2)?;
601+
v.serialize(s)?
602+
}
603+
}
604+
605+
Ok(())
606+
}
607+
}
608+
609+
impl crate::snapshot::Deserialize for PropertyKey {
610+
fn deserialize(
611+
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
612+
) -> crate::snapshot::SnapshotResult<Self> {
613+
let typ = u8::deserialize(d)?;
614+
let result = match typ {
615+
0 => Self::String(JsString::deserialize(d)?),
616+
1 => Self::Symbol(JsSymbol::deserialize(d)?),
617+
2 => Self::Index(u32::deserialize(d)?),
618+
_ => unreachable!("corrupted snapshot!"),
619+
};
620+
621+
Ok(result)
622+
}
623+
}
624+
585625
/// Utility function for parsing [`PropertyKey`].
586626
fn parse_u32_index<I, T>(mut input: I) -> Option<u32>
587627
where

boa_engine/src/realm.rs

+25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ impl crate::snapshot::Serialize for Realm {
3535
}
3636
}
3737

38+
impl crate::snapshot::Deserialize for Realm {
39+
fn deserialize(
40+
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
41+
) -> Result<Self, crate::snapshot::SnapshotError> {
42+
let inner = Inner::deserialize(d)?;
43+
Ok(Realm {
44+
inner: Gc::new(inner),
45+
})
46+
}
47+
}
48+
3849
impl Eq for Realm {}
3950

4051
impl PartialEq for Realm {
@@ -70,10 +81,24 @@ impl crate::snapshot::Serialize for Inner {
7081
s: &mut crate::snapshot::SnapshotSerializer,
7182
) -> Result<(), crate::snapshot::SnapshotError> {
7283
self.intrinsics.serialize(s)?;
84+
self.global_object.serialize(s)?;
85+
self.global_this.serialize(s)?;
86+
self.template_map.borrow().serialize(s)?;
7387
Ok(())
7488
}
7589
}
7690

91+
impl crate::snapshot::Deserialize for Inner {
92+
fn deserialize(
93+
_d: &mut crate::snapshot::SnapshotDeserializer<'_>,
94+
) -> Result<Self, crate::snapshot::SnapshotError> {
95+
// let intrinsics = Intrinsics::deserialize(d)?;
96+
97+
// Ok(Inner::)
98+
todo!()
99+
}
100+
}
101+
77102
impl Realm {
78103
/// Create a new Realm.
79104
#[inline]

0 commit comments

Comments
 (0)