Skip to content

Commit 25356f6

Browse files
committed
Separate into files
1 parent 16e7d61 commit 25356f6

File tree

6 files changed

+403
-360
lines changed

6 files changed

+403
-360
lines changed

boa_engine/src/context/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ impl crate::snapshot::Serialize for Context<'_> {
992992
s: &mut crate::snapshot::SnapshotSerializer,
993993
) -> Result<(), crate::snapshot::SnapshotError> {
994994
s.write_bool(self.strict)?;
995+
self.optimizer_options.serialize(s)?;
995996
self.realm.serialize(s)?;
996997
Ok(())
997998
}
@@ -1002,9 +1003,11 @@ impl crate::snapshot::Deserialize for Context<'_> {
10021003
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
10031004
) -> Result<Self, crate::snapshot::SnapshotError> {
10041005
let strict = d.read_bool()?;
1006+
let optimizer_options = OptimizerOptions::deserialize(d)?;
10051007
let mut context = Context::default();
10061008

10071009
context.strict(strict);
1010+
context.set_optimizer_options(optimizer_options);
10081011

10091012
Ok(context)
10101013
}

boa_engine/src/optimizer/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ pub(crate) mod pass;
44
pub(crate) mod walker;
55

66
use self::{pass::ConstantFolding, walker::Walker};
7-
use crate::Context;
7+
use crate::{
8+
snapshot::{Deserialize, Serialize},
9+
Context,
10+
};
811
use bitflags::bitflags;
912
use boa_ast::{visitor::VisitorMut, Expression, StatementList};
1013
use std::{fmt, ops::ControlFlow};
@@ -24,6 +27,26 @@ bitflags! {
2427
}
2528
}
2629

30+
impl Serialize for OptimizerOptions {
31+
fn serialize(
32+
&self,
33+
s: &mut crate::snapshot::SnapshotSerializer,
34+
) -> Result<(), crate::snapshot::SnapshotError> {
35+
s.write_u8(self.bits())?;
36+
Ok(())
37+
}
38+
}
39+
impl Deserialize for OptimizerOptions {
40+
fn deserialize(
41+
d: &mut crate::snapshot::SnapshotDeserializer<'_>,
42+
) -> Result<Self, crate::snapshot::SnapshotError> {
43+
let bits = d.read_u8()?;
44+
45+
// TODO: handle error.
46+
Ok(OptimizerOptions::from_bits(bits).unwrap())
47+
}
48+
}
49+
2750
/// The action to be performed after an optimization step.
2851
#[derive(Debug)]
2952
pub(crate) enum PassAction<T> {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use indexmap::IndexSet;
2+
3+
use super::SnapshotError;
4+
5+
/// TODO: doc
6+
pub trait Deserialize: Sized {
7+
/// TODO: doc
8+
fn deserialize(d: &mut SnapshotDeserializer<'_>) -> Result<Self, SnapshotError>;
9+
}
10+
11+
/// TODO: doc
12+
pub struct SnapshotDeserializer<'snapshot> {
13+
pub(super) bytes: &'snapshot [u8],
14+
pub(super) index: usize,
15+
pub(super) external_references: &'snapshot IndexSet<usize>,
16+
}
17+
18+
impl SnapshotDeserializer<'_> {
19+
/// TODO: doc
20+
pub fn read_bool(&mut self) -> Result<bool, SnapshotError> {
21+
let byte = self.read_u8()?;
22+
assert!(byte == 0 || byte == 1);
23+
Ok(byte == 1)
24+
}
25+
/// TODO: doc
26+
pub fn read_u8(&mut self) -> Result<u8, SnapshotError> {
27+
let byte = self.bytes[self.index];
28+
self.index += 1;
29+
Ok(byte)
30+
}
31+
/// TODO: doc
32+
pub fn read_i8(&mut self) -> Result<i8, SnapshotError> {
33+
let byte = self.bytes[self.index];
34+
self.index += 1;
35+
Ok(byte as i8)
36+
}
37+
38+
/// TODO: doc
39+
pub fn read_u16(&mut self) -> Result<u16, SnapshotError> {
40+
let bytes = self.read_bytes(std::mem::size_of::<u16>())?;
41+
let value = u16::from_le_bytes([bytes[0], bytes[1]]);
42+
Ok(value)
43+
}
44+
/// TODO: doc
45+
pub fn read_i16(&mut self) -> Result<i16, SnapshotError> {
46+
let value = self.read_u16()?;
47+
Ok(value as i16)
48+
}
49+
50+
/// TODO: doc
51+
pub fn read_u32(&mut self) -> Result<u32, SnapshotError> {
52+
let bytes = self.read_bytes(4)?;
53+
let value = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
54+
Ok(value)
55+
}
56+
/// TODO: doc
57+
pub fn read_i32(&mut self) -> Result<i32, SnapshotError> {
58+
let value = self.read_u32()?;
59+
Ok(value as i32)
60+
}
61+
62+
/// TODO: doc
63+
pub fn read_f32(&mut self) -> Result<f32, SnapshotError> {
64+
let value = self.read_u32()?;
65+
Ok(f32::from_bits(value))
66+
}
67+
/// TODO: doc
68+
pub fn read_f64(&mut self) -> Result<f64, SnapshotError> {
69+
let value = self.read_u64()?;
70+
Ok(f64::from_bits(value))
71+
}
72+
73+
/// TODO: doc
74+
pub fn read_u64(&mut self) -> Result<u64, SnapshotError> {
75+
let bytes = self.read_bytes(std::mem::size_of::<u64>())?;
76+
let value = u64::from_le_bytes([
77+
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
78+
]);
79+
Ok(value)
80+
}
81+
/// TODO: doc
82+
pub fn read_i64(&mut self) -> Result<i64, SnapshotError> {
83+
let value = self.read_u64()?;
84+
Ok(value as i64)
85+
}
86+
87+
/// TODO: doc
88+
pub fn read_usize(&mut self) -> Result<usize, SnapshotError> {
89+
let value = self.read_u64()?;
90+
// TODO: handle error.
91+
Ok(usize::try_from(value).unwrap())
92+
}
93+
/// TODO: doc
94+
pub fn read_isize(&mut self) -> Result<isize, SnapshotError> {
95+
let value = self.read_usize()?;
96+
Ok(value as isize)
97+
}
98+
/// TODO: doc
99+
pub fn read_string(&mut self) -> Result<&str, SnapshotError> {
100+
let len = self.read_usize()?;
101+
let bytes = self.read_bytes(len)?;
102+
// TODO: handle error
103+
Ok(std::str::from_utf8(bytes).unwrap())
104+
}
105+
/// TODO: doc
106+
pub fn read_bytes(&mut self, count: usize) -> Result<&[u8], SnapshotError> {
107+
let index = self.index;
108+
self.index += count;
109+
// TODO: use .get() so we can handle the error.
110+
let bytes = &self.bytes[index..(index + count)];
111+
Ok(bytes)
112+
}
113+
}

boa_engine/src/snapshot/error.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::fmt::{Debug, Display};
2+
3+
/// TODO: doc
4+
#[derive(Debug)]
5+
pub enum SnapshotError {
6+
/// Input/output error.
7+
///
8+
/// See: [`std::io::Error`].
9+
Io(std::io::Error),
10+
}
11+
12+
impl Display for SnapshotError {
13+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14+
// FIXME: implement better formatting
15+
<Self as Debug>::fmt(self, f)
16+
}
17+
}
18+
19+
impl std::error::Error for SnapshotError {}
20+
21+
impl From<std::io::Error> for SnapshotError {
22+
fn from(value: std::io::Error) -> Self {
23+
Self::Io(value)
24+
}
25+
}

0 commit comments

Comments
 (0)