Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 663a1a1

Browse files
committedJun 21, 2023
Implement serialize and deserialize for primitive types
1 parent 9649396 commit 663a1a1

File tree

5 files changed

+301
-56
lines changed

5 files changed

+301
-56
lines changed
 

‎boa_engine/src/snapshot/deserializer.rs

+127-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use indexmap::IndexSet;
2+
use thin_vec::ThinVec;
23

3-
use super::SnapshotError;
4+
use super::{SnapshotError, SnapshotResult};
45

56
/// TODO: doc
67
pub trait Deserialize: Sized {
78
/// TODO: doc
8-
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> Result<Self, SnapshotError>;
9+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self>;
910
}
1011

1112
/// TODO: doc
@@ -111,3 +112,127 @@ impl SnapshotDeserializer<'_> {
111112
Ok(bytes)
112113
}
113114
}
115+
116+
impl Deserialize for bool {
117+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
118+
d.read_bool()
119+
}
120+
}
121+
122+
impl Deserialize for u8 {
123+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
124+
d.read_u8()
125+
}
126+
}
127+
128+
impl Deserialize for i8 {
129+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
130+
d.read_i8()
131+
}
132+
}
133+
134+
impl Deserialize for u16 {
135+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
136+
d.read_u16()
137+
}
138+
}
139+
140+
impl Deserialize for i16 {
141+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
142+
d.read_i16()
143+
}
144+
}
145+
146+
impl Deserialize for u32 {
147+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
148+
d.read_u32()
149+
}
150+
}
151+
152+
impl Deserialize for i32 {
153+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
154+
d.read_i32()
155+
}
156+
}
157+
158+
impl Deserialize for u64 {
159+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
160+
d.read_u64()
161+
}
162+
}
163+
164+
impl Deserialize for i64 {
165+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
166+
d.read_i64()
167+
}
168+
}
169+
170+
impl Deserialize for usize {
171+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
172+
d.read_usize()
173+
}
174+
}
175+
176+
impl Deserialize for isize {
177+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
178+
d.read_isize()
179+
}
180+
}
181+
182+
impl Deserialize for f32 {
183+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
184+
d.read_f32()
185+
}
186+
}
187+
188+
impl Deserialize for f64 {
189+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
190+
d.read_f64()
191+
}
192+
}
193+
194+
impl<T: Deserialize> Deserialize for Option<T> {
195+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
196+
let is_some = bool::deserialize(d)?;
197+
if is_some {
198+
return Ok(Some(T::deserialize(d)?));
199+
}
200+
201+
Ok(None)
202+
}
203+
}
204+
205+
impl<T: Deserialize, E: Deserialize> Deserialize for Result<T, E> {
206+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
207+
let is_ok = bool::deserialize(d)?;
208+
Ok(if is_ok {
209+
Ok(T::deserialize(d)?)
210+
} else {
211+
Err(E::deserialize(d)?)
212+
})
213+
}
214+
}
215+
216+
impl<T: Deserialize> Deserialize for Vec<T> {
217+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
218+
let len = usize::deserialize(d)?;
219+
let mut values = Vec::with_capacity(len);
220+
for _ in 0..len {
221+
let value = T::deserialize(d)?;
222+
values.push(value);
223+
}
224+
Ok(values)
225+
}
226+
}
227+
228+
impl<T: Deserialize> Deserialize for ThinVec<T> {
229+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
230+
let len = usize::deserialize(d)?;
231+
let mut values = ThinVec::with_capacity(len);
232+
for _ in 0..len {
233+
let value = T::deserialize(d)?;
234+
values.push(value);
235+
}
236+
Ok(values)
237+
}
238+
}

‎boa_engine/src/snapshot/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ impl From<std::io::Error> for SnapshotError {
2323
Self::Io(value)
2424
}
2525
}
26+
27+
/// Type alias for [`Result`] return type snapshot operations.
28+
pub type SnapshotResult<T> = Result<T, SnapshotError>;

‎boa_engine/src/snapshot/header.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::{Deserialize, Serialize, SnapshotDeserializer, SnapshotResult, SnapshotSerializer};
2+
3+
/// TODO: doc
4+
#[derive(Debug, Clone, Copy)]
5+
pub struct Header {
6+
pub(crate) signature: [u8; 4],
7+
pub(crate) version: u32,
8+
// checksum: u64,
9+
}
10+
11+
impl Serialize for Header {
12+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
13+
s.write_bytes(&self.signature)?;
14+
s.write_u32(self.version)?;
15+
Ok(())
16+
}
17+
}
18+
19+
impl Deserialize for Header {
20+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> SnapshotResult<Self> {
21+
let signature = d.read_bytes(4)?;
22+
let signature = [signature[0], signature[1], signature[2], signature[3]];
23+
24+
let version = d.read_u32()?;
25+
26+
Ok(Self { signature, version })
27+
}
28+
}

‎boa_engine/src/snapshot/mod.rs

+2-20
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,16 @@
55

66
mod deserializer;
77
mod error;
8+
mod header;
89
mod serializer;
910

1011
pub use deserializer::*;
1112
pub use error::*;
13+
pub use header::*;
1214
pub use serializer::*;
1315

1416
use crate::Context;
1517
use indexmap::IndexSet;
16-
use std::fmt::Debug;
17-
18-
/// TODO: doc
19-
#[derive(Debug, Clone, Copy)]
20-
pub struct Header {
21-
signature: [u8; 4],
22-
version: u32,
23-
// checksum: u64,
24-
}
25-
26-
impl Deserialize for Header {
27-
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> Result<Self, SnapshotError> {
28-
let signature = d.read_bytes(4)?;
29-
let signature = [signature[0], signature[1], signature[2], signature[3]];
30-
31-
let version = d.read_u32()?;
32-
33-
Ok(Self { signature, version })
34-
}
35-
}
3618

3719
/// TODO: doc
3820
pub struct Snapshot {

‎boa_engine/src/snapshot/serializer.rs

+141-34
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
use indexmap::{IndexMap, IndexSet};
2+
use thin_vec::ThinVec;
23

34
use crate::{Context, JsBigInt, JsObject, JsString, JsSymbol};
45

5-
use super::{Header, Snapshot, SnapshotError};
6+
use super::{Header, Snapshot, SnapshotError, SnapshotResult};
67

78
/// TODO: doc
89
pub trait Serialize {
910
/// Serialize type
10-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError>;
11-
}
12-
13-
impl Serialize for Header {
14-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
15-
s.write_bytes(&self.signature)?;
16-
s.write_u32(self.version)?;
17-
Ok(())
18-
}
11+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()>;
1912
}
2013

2114
/// TODO: doc
@@ -106,85 +99,201 @@ impl SnapshotSerializer {
10699
}
107100

108101
/// TODO: doc
109-
pub fn write_bool(&mut self, v: bool) -> Result<(), SnapshotError> {
102+
pub fn write_bool(&mut self, v: bool) -> SnapshotResult<()> {
110103
Ok(self.write_u8(if v { 1 } else { 0 })?)
111104
}
112105
/// TODO: doc
113-
pub fn write_u8(&mut self, v: u8) -> Result<(), SnapshotError> {
106+
pub fn write_u8(&mut self, v: u8) -> SnapshotResult<()> {
114107
Ok(self.write_bytes(&[v])?)
115108
}
116109
/// TODO: doc
117-
pub fn write_i8(&mut self, v: i8) -> Result<(), SnapshotError> {
110+
pub fn write_i8(&mut self, v: i8) -> SnapshotResult<()> {
118111
Ok(self.write_bytes(&v.to_le_bytes())?)
119112
}
120113

121114
/// TODO: doc
122-
pub fn write_u16(&mut self, v: u16) -> Result<(), SnapshotError> {
115+
pub fn write_u16(&mut self, v: u16) -> SnapshotResult<()> {
123116
Ok(self.write_bytes(&v.to_le_bytes())?)
124117
}
125118
/// TODO: doc
126-
pub fn write_i16(&mut self, v: i16) -> Result<(), SnapshotError> {
119+
pub fn write_i16(&mut self, v: i16) -> SnapshotResult<()> {
127120
Ok(self.write_bytes(&v.to_le_bytes())?)
128121
}
129122

130123
/// TODO: doc
131-
pub fn write_u32(&mut self, v: u32) -> Result<(), SnapshotError> {
124+
pub fn write_u32(&mut self, v: u32) -> SnapshotResult<()> {
132125
Ok(self.write_bytes(&v.to_le_bytes())?)
133126
}
134127
/// TODO: doc
135-
pub fn write_i32(&mut self, v: i32) -> Result<(), SnapshotError> {
128+
pub fn write_i32(&mut self, v: i32) -> SnapshotResult<()> {
136129
Ok(self.write_bytes(&v.to_le_bytes())?)
137130
}
138131

139132
/// TODO: doc
140-
pub fn write_f32(&mut self, v: f32) -> Result<(), SnapshotError> {
133+
pub fn write_f32(&mut self, v: f32) -> SnapshotResult<()> {
141134
Ok(self.write_bytes(&v.to_le_bytes())?)
142135
}
143136
/// TODO: doc
144-
pub fn write_f64(&mut self, v: f64) -> Result<(), SnapshotError> {
137+
pub fn write_f64(&mut self, v: f64) -> SnapshotResult<()> {
145138
Ok(self.write_bytes(&v.to_le_bytes())?)
146139
}
147140

148141
/// TODO: doc
149-
pub fn write_u64(&mut self, v: u64) -> Result<(), SnapshotError> {
142+
pub fn write_u64(&mut self, v: u64) -> SnapshotResult<()> {
150143
Ok(self.write_bytes(&v.to_le_bytes())?)
151144
}
152145
/// TODO: doc
153-
pub fn write_i64(&mut self, v: i64) -> Result<(), SnapshotError> {
146+
pub fn write_i64(&mut self, v: i64) -> SnapshotResult<()> {
154147
Ok(self.write_bytes(&v.to_le_bytes())?)
155148
}
156149
/// TODO: doc
157-
pub fn write_u128(&mut self, v: u128) -> Result<(), SnapshotError> {
150+
pub fn write_u128(&mut self, v: u128) -> SnapshotResult<()> {
158151
Ok(self.write_bytes(&v.to_le_bytes())?)
159152
}
160153
/// TODO: doc
161-
pub fn write_i128(&mut self, v: i128) -> Result<(), SnapshotError> {
154+
pub fn write_i128(&mut self, v: i128) -> SnapshotResult<()> {
162155
Ok(self.write_bytes(&v.to_le_bytes())?)
163156
}
164157
/// TODO: doc
165-
pub fn write_usize(&mut self, v: usize) -> Result<(), SnapshotError> {
158+
pub fn write_usize(&mut self, v: usize) -> SnapshotResult<()> {
166159
Ok(self.write_bytes(&(v as u64).to_le_bytes())?)
167160
}
168161
/// TODO: doc
169-
pub fn write_isize(&mut self, v: isize) -> Result<(), SnapshotError> {
162+
pub fn write_isize(&mut self, v: isize) -> SnapshotResult<()> {
170163
Ok(self.write_bytes(&(v as i64).to_le_bytes())?)
171164
}
172165
/// TODO: doc
173-
pub fn write_string(&mut self, v: &str) -> Result<(), SnapshotError> {
166+
pub fn write_string(&mut self, v: &str) -> SnapshotResult<()> {
174167
let asb = v.as_bytes();
175168
self.write_usize(asb.len())?;
176169
self.bytes.extend_from_slice(asb);
177170
Ok(())
178171
}
179172
/// TODO: doc
180-
pub fn write_bytes(&mut self, v: &[u8]) -> Result<(), SnapshotError> {
173+
pub fn write_bytes(&mut self, v: &[u8]) -> SnapshotResult<()> {
181174
self.bytes.extend_from_slice(v);
182175
Ok(())
183176
}
184177
}
185178

179+
impl Serialize for bool {
180+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
181+
s.write_bool(*self)
182+
}
183+
}
184+
185+
impl Serialize for u8 {
186+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
187+
s.write_u8(*self)
188+
}
189+
}
190+
191+
impl Serialize for i8 {
192+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
193+
s.write_i8(*self)
194+
}
195+
}
196+
197+
impl Serialize for u16 {
198+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
199+
s.write_u16(*self)
200+
}
201+
}
202+
203+
impl Serialize for i16 {
204+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
205+
s.write_i16(*self)
206+
}
207+
}
208+
209+
impl Serialize for u32 {
210+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
211+
s.write_u32(*self)
212+
}
213+
}
214+
215+
impl Serialize for i32 {
216+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
217+
s.write_i32(*self)
218+
}
219+
}
220+
221+
impl Serialize for u64 {
222+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
223+
s.write_u64(*self)
224+
}
225+
}
226+
227+
impl Serialize for i64 {
228+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
229+
s.write_i64(*self)
230+
}
231+
}
232+
233+
impl Serialize for usize {
234+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
235+
s.write_usize(*self)
236+
}
237+
}
238+
239+
impl Serialize for isize {
240+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
241+
s.write_isize(*self)
242+
}
243+
}
244+
245+
impl Serialize for f32 {
246+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
247+
s.write_f32(*self)
248+
}
249+
}
250+
251+
impl Serialize for f64 {
252+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
253+
s.write_f64(*self)
254+
}
255+
}
256+
257+
impl<T: Serialize> Serialize for Option<T> {
258+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
259+
if let Some(value) = self {
260+
s.write_bool(true)?;
261+
value.serialize(s)?
262+
} else {
263+
s.write_bool(false)?;
264+
}
265+
Ok(())
266+
}
267+
}
268+
269+
impl<T: Serialize, E: Serialize> Serialize for Result<T, E> {
270+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
271+
match self {
272+
Ok(value) => {
273+
s.write_bool(true)?;
274+
value.serialize(s)?;
275+
}
276+
Err(err) => {
277+
s.write_bool(false)?;
278+
err.serialize(s)?;
279+
}
280+
}
281+
Ok(())
282+
}
283+
}
284+
186285
impl<T: Serialize> Serialize for Vec<T> {
187-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
286+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
287+
s.write_usize(self.len())?;
288+
for element in self {
289+
element.serialize(s)?;
290+
}
291+
Ok(())
292+
}
293+
}
294+
295+
impl<T: Serialize> Serialize for ThinVec<T> {
296+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
188297
s.write_usize(self.len())?;
189298
for element in self {
190299
element.serialize(s)?;
@@ -194,7 +303,7 @@ impl<T: Serialize> Serialize for Vec<T> {
194303
}
195304

196305
impl Serialize for JsString {
197-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
306+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
198307
let index = s.strings.insert_full(self.ptr.addr(), self.clone()).0;
199308

200309
s.write_u32(index as u32)?;
@@ -203,26 +312,24 @@ impl Serialize for JsString {
203312
}
204313

205314
impl Serialize for JsSymbol {
206-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
315+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
207316
let index = s.symbols.insert_full(self.hash(), self.clone()).0;
208-
209317
s.write_u32(index as u32)?;
210318
Ok(())
211319
}
212320
}
213321

214322
impl Serialize for JsBigInt {
215-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
323+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
216324
let index = s.bigints.insert_full(self.clone()).0;
217325
s.write_u32(index as u32)?;
218326
Ok(())
219327
}
220328
}
221329

222330
impl Serialize for JsObject {
223-
fn serialize(&self, s: &mut SnapshotSerializer) -> Result<(), SnapshotError> {
331+
fn serialize(&self, s: &mut SnapshotSerializer) -> SnapshotResult<()> {
224332
let value = s.objects.insert_full(self.clone()).0;
225-
226333
s.write_u32(value as u32)?;
227334
Ok(())
228335
}

0 commit comments

Comments
 (0)
Please sign in to comment.