Skip to content

Commit fd9f5b3

Browse files
committed
Implement serialize and deserialize for primitive types
1 parent 25356f6 commit fd9f5b3

File tree

30 files changed

+1274
-127
lines changed

30 files changed

+1274
-127
lines changed

boa_engine/src/builtins/iterable/mod.rs

Lines changed: 23 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 63 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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/environments/compile.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ struct CompileTimeBinding {
1919
strict: bool,
2020
}
2121

22+
impl crate::snapshot::Serialize for CompileTimeBinding {
23+
fn serialize(
24+
&self,
25+
s: &mut crate::snapshot::SnapshotSerializer,
26+
) -> crate::snapshot::SnapshotResult<()> {
27+
self.index.serialize(s)?;
28+
self.mutable.serialize(s)?;
29+
self.lex.serialize(s)?;
30+
self.strict.serialize(s)?;
31+
Ok(())
32+
}
33+
}
34+
2235
/// A compile time environment maps bound identifiers to their binding positions.
2336
///
2437
/// A compile time environment also indicates, if it is a function environment.
@@ -30,6 +43,29 @@ pub(crate) struct CompileTimeEnvironment {
3043
function_scope: bool,
3144
}
3245

46+
impl crate::snapshot::Serialize for Identifier {
47+
fn serialize(
48+
&self,
49+
s: &mut crate::snapshot::SnapshotSerializer,
50+
) -> crate::snapshot::SnapshotResult<()> {
51+
self.sym().get().serialize(s)?;
52+
Ok(())
53+
}
54+
}
55+
56+
impl crate::snapshot::Serialize for CompileTimeEnvironment {
57+
fn serialize(
58+
&self,
59+
s: &mut crate::snapshot::SnapshotSerializer,
60+
) -> crate::snapshot::SnapshotResult<()> {
61+
self.outer.serialize(s)?;
62+
self.environment_index.serialize(s)?;
63+
self.bindings.serialize(s)?;
64+
self.function_scope.serialize(s)?;
65+
Ok(())
66+
}
67+
}
68+
3369
// Safety: Nothing in this struct needs tracing, so this is safe.
3470
unsafe impl Trace for CompileTimeEnvironment {
3571
empty_trace!();

boa_engine/src/environments/runtime/declarative/function.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ pub(crate) struct FunctionEnvironment {
1010
slots: FunctionSlots,
1111
}
1212

13+
impl crate::snapshot::Serialize for FunctionEnvironment {
14+
fn serialize(
15+
&self,
16+
s: &mut crate::snapshot::SnapshotSerializer,
17+
) -> crate::snapshot::SnapshotResult<()> {
18+
self.inner.serialize(s)?;
19+
self.slots.serialize(s)?;
20+
Ok(())
21+
}
22+
}
23+
1324
impl FunctionEnvironment {
1425
/// Creates a new `FunctionEnvironment`.
1526
pub(crate) fn new(bindings: u32, poisoned: bool, with: bool, slots: FunctionSlots) -> Self {
@@ -160,6 +171,22 @@ pub(crate) enum ThisBindingStatus {
160171
Initialized(JsValue),
161172
}
162173

174+
impl crate::snapshot::Serialize for ThisBindingStatus {
175+
fn serialize(
176+
&self,
177+
s: &mut crate::snapshot::SnapshotSerializer,
178+
) -> crate::snapshot::SnapshotResult<()> {
179+
match self {
180+
ThisBindingStatus::Lexical => b'L'.serialize(s),
181+
ThisBindingStatus::Uninitialized => b'U'.serialize(s),
182+
ThisBindingStatus::Initialized(v) => {
183+
b'I'.serialize(s)?;
184+
v.serialize(s)
185+
}
186+
}
187+
}
188+
}
189+
163190
unsafe impl Trace for ThisBindingStatus {
164191
custom_trace!(this, {
165192
match this {
@@ -182,6 +209,18 @@ pub(crate) struct FunctionSlots {
182209
new_target: Option<JsObject>,
183210
}
184211

212+
impl crate::snapshot::Serialize for FunctionSlots {
213+
fn serialize(
214+
&self,
215+
s: &mut crate::snapshot::SnapshotSerializer,
216+
) -> crate::snapshot::SnapshotResult<()> {
217+
self.this.borrow().serialize(s)?;
218+
self.function_object.serialize(s)?;
219+
self.new_target.serialize(s)?;
220+
Ok(())
221+
}
222+
}
223+
185224
impl FunctionSlots {
186225
/// Creates a new `FunctionSluts`.
187226
pub(crate) fn new(

boa_engine/src/environments/runtime/declarative/global.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ pub(crate) struct GlobalEnvironment {
1010
global_this: JsObject,
1111
}
1212

13+
impl crate::snapshot::Serialize for GlobalEnvironment {
14+
fn serialize(
15+
&self,
16+
s: &mut crate::snapshot::SnapshotSerializer,
17+
) -> crate::snapshot::SnapshotResult<()> {
18+
self.inner.serialize(s)?;
19+
self.global_this.serialize(s)?;
20+
Ok(())
21+
}
22+
}
23+
1324
impl GlobalEnvironment {
1425
/// Creates a new `GlobalEnvironment`.
1526
pub(crate) fn new(global_this: JsObject) -> Self {

boa_engine/src/environments/runtime/declarative/lexical.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ pub(crate) struct LexicalEnvironment {
99
inner: PoisonableEnvironment,
1010
}
1111

12+
impl crate::snapshot::Serialize for LexicalEnvironment {
13+
fn serialize(
14+
&self,
15+
s: &mut crate::snapshot::SnapshotSerializer,
16+
) -> crate::snapshot::SnapshotResult<()> {
17+
self.inner.serialize(s)
18+
}
19+
}
20+
1221
impl LexicalEnvironment {
1322
/// Creates a new `LexicalEnvironment`.
1423
pub(crate) fn new(bindings: u32, poisoned: bool, with: bool) -> Self {

boa_engine/src/environments/runtime/declarative/mod.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ pub(crate) struct DeclarativeEnvironment {
4545
compile: Rc<RefCell<CompileTimeEnvironment>>,
4646
}
4747

48+
impl crate::snapshot::Serialize for DeclarativeEnvironment {
49+
fn serialize(
50+
&self,
51+
s: &mut crate::snapshot::SnapshotSerializer,
52+
) -> crate::snapshot::SnapshotResult<()> {
53+
self.kind.serialize(s)?;
54+
self.compile.serialize(s)?;
55+
Ok(())
56+
}
57+
}
58+
4859
impl DeclarativeEnvironment {
4960
/// Creates a new global `DeclarativeEnvironment`.
5061
pub(crate) fn global(global_this: JsObject) -> Self {
@@ -145,6 +156,34 @@ pub(crate) enum DeclarativeEnvironmentKind {
145156
Module(ModuleEnvironment),
146157
}
147158

159+
impl crate::snapshot::Serialize for DeclarativeEnvironmentKind {
160+
fn serialize(
161+
&self,
162+
s: &mut crate::snapshot::SnapshotSerializer,
163+
) -> crate::snapshot::SnapshotResult<()> {
164+
match self {
165+
DeclarativeEnvironmentKind::Lexical(env) => {
166+
s.write_u8(b'L')?;
167+
env.serialize(s)?;
168+
}
169+
DeclarativeEnvironmentKind::Global(env) => {
170+
s.write_u8(b'G')?;
171+
env.serialize(s)?;
172+
}
173+
DeclarativeEnvironmentKind::Function(env) => {
174+
s.write_u8(b'F')?;
175+
env.serialize(s)?;
176+
}
177+
DeclarativeEnvironmentKind::Module(env) => {
178+
s.write_u8(b'M')?;
179+
env.serialize(s)?;
180+
}
181+
}
182+
183+
Ok(())
184+
}
185+
}
186+
148187
impl DeclarativeEnvironmentKind {
149188
/// Unwraps the inner function environment if possible. Returns `None` otherwise.
150189
pub(crate) const fn as_function(&self) -> Option<&FunctionEnvironment> {
@@ -278,6 +317,18 @@ pub(crate) struct PoisonableEnvironment {
278317
with: Cell<bool>,
279318
}
280319

320+
impl crate::snapshot::Serialize for PoisonableEnvironment {
321+
fn serialize(
322+
&self,
323+
s: &mut crate::snapshot::SnapshotSerializer,
324+
) -> crate::snapshot::SnapshotResult<()> {
325+
self.bindings.borrow().serialize(s)?;
326+
self.poisoned.serialize(s)?;
327+
self.with.serialize(s)?;
328+
Ok(())
329+
}
330+
}
331+
281332
impl PoisonableEnvironment {
282333
/// Creates a new `PoisonableEnvironment`.
283334
pub(crate) fn new(bindings_count: u32, poisoned: bool, with: bool) -> Self {

0 commit comments

Comments
 (0)