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 Jul 1, 2023
1 parent 25356f6 commit fd9f5b3
Show file tree
Hide file tree
Showing 30 changed files with 1,274 additions and 127 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
36 changes: 36 additions & 0 deletions boa_engine/src/environments/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ struct CompileTimeBinding {
strict: bool,
}

impl crate::snapshot::Serialize for CompileTimeBinding {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.index.serialize(s)?;
self.mutable.serialize(s)?;
self.lex.serialize(s)?;
self.strict.serialize(s)?;
Ok(())
}
}

/// A compile time environment maps bound identifiers to their binding positions.
///
/// A compile time environment also indicates, if it is a function environment.
Expand All @@ -30,6 +43,29 @@ pub(crate) struct CompileTimeEnvironment {
function_scope: bool,
}

impl crate::snapshot::Serialize for Identifier {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.sym().get().serialize(s)?;
Ok(())
}
}

impl crate::snapshot::Serialize for CompileTimeEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.outer.serialize(s)?;
self.environment_index.serialize(s)?;
self.bindings.serialize(s)?;
self.function_scope.serialize(s)?;
Ok(())
}
}

// Safety: Nothing in this struct needs tracing, so this is safe.
unsafe impl Trace for CompileTimeEnvironment {
empty_trace!();
Expand Down
39 changes: 39 additions & 0 deletions boa_engine/src/environments/runtime/declarative/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ pub(crate) struct FunctionEnvironment {
slots: FunctionSlots,
}

impl crate::snapshot::Serialize for FunctionEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.inner.serialize(s)?;
self.slots.serialize(s)?;
Ok(())
}
}

impl FunctionEnvironment {
/// Creates a new `FunctionEnvironment`.
pub(crate) fn new(bindings: u32, poisoned: bool, with: bool, slots: FunctionSlots) -> Self {
Expand Down Expand Up @@ -160,6 +171,22 @@ pub(crate) enum ThisBindingStatus {
Initialized(JsValue),
}

impl crate::snapshot::Serialize for ThisBindingStatus {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
match self {
ThisBindingStatus::Lexical => b'L'.serialize(s),
ThisBindingStatus::Uninitialized => b'U'.serialize(s),
ThisBindingStatus::Initialized(v) => {
b'I'.serialize(s)?;
v.serialize(s)
}
}
}
}

unsafe impl Trace for ThisBindingStatus {
custom_trace!(this, {
match this {
Expand All @@ -182,6 +209,18 @@ pub(crate) struct FunctionSlots {
new_target: Option<JsObject>,
}

impl crate::snapshot::Serialize for FunctionSlots {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.this.borrow().serialize(s)?;
self.function_object.serialize(s)?;
self.new_target.serialize(s)?;
Ok(())
}
}

impl FunctionSlots {
/// Creates a new `FunctionSluts`.
pub(crate) fn new(
Expand Down
11 changes: 11 additions & 0 deletions boa_engine/src/environments/runtime/declarative/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ pub(crate) struct GlobalEnvironment {
global_this: JsObject,
}

impl crate::snapshot::Serialize for GlobalEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.inner.serialize(s)?;
self.global_this.serialize(s)?;
Ok(())
}
}

impl GlobalEnvironment {
/// Creates a new `GlobalEnvironment`.
pub(crate) fn new(global_this: JsObject) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions boa_engine/src/environments/runtime/declarative/lexical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ pub(crate) struct LexicalEnvironment {
inner: PoisonableEnvironment,
}

impl crate::snapshot::Serialize for LexicalEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.inner.serialize(s)
}
}

impl LexicalEnvironment {
/// Creates a new `LexicalEnvironment`.
pub(crate) fn new(bindings: u32, poisoned: bool, with: bool) -> Self {
Expand Down
51 changes: 51 additions & 0 deletions boa_engine/src/environments/runtime/declarative/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ pub(crate) struct DeclarativeEnvironment {
compile: Rc<RefCell<CompileTimeEnvironment>>,
}

impl crate::snapshot::Serialize for DeclarativeEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.kind.serialize(s)?;
self.compile.serialize(s)?;
Ok(())
}
}

impl DeclarativeEnvironment {
/// Creates a new global `DeclarativeEnvironment`.
pub(crate) fn global(global_this: JsObject) -> Self {
Expand Down Expand Up @@ -145,6 +156,34 @@ pub(crate) enum DeclarativeEnvironmentKind {
Module(ModuleEnvironment),
}

impl crate::snapshot::Serialize for DeclarativeEnvironmentKind {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
match self {
DeclarativeEnvironmentKind::Lexical(env) => {
s.write_u8(b'L')?;
env.serialize(s)?;
}
DeclarativeEnvironmentKind::Global(env) => {
s.write_u8(b'G')?;
env.serialize(s)?;
}
DeclarativeEnvironmentKind::Function(env) => {
s.write_u8(b'F')?;
env.serialize(s)?;
}
DeclarativeEnvironmentKind::Module(env) => {
s.write_u8(b'M')?;
env.serialize(s)?;
}
}

Ok(())
}
}

impl DeclarativeEnvironmentKind {
/// Unwraps the inner function environment if possible. Returns `None` otherwise.
pub(crate) const fn as_function(&self) -> Option<&FunctionEnvironment> {
Expand Down Expand Up @@ -278,6 +317,18 @@ pub(crate) struct PoisonableEnvironment {
with: Cell<bool>,
}

impl crate::snapshot::Serialize for PoisonableEnvironment {
fn serialize(
&self,
s: &mut crate::snapshot::SnapshotSerializer,
) -> crate::snapshot::SnapshotResult<()> {
self.bindings.borrow().serialize(s)?;
self.poisoned.serialize(s)?;
self.with.serialize(s)?;
Ok(())
}
}

impl PoisonableEnvironment {
/// Creates a new `PoisonableEnvironment`.
pub(crate) fn new(bindings_count: u32, poisoned: bool, with: bool) -> Self {
Expand Down
Loading

0 comments on commit fd9f5b3

Please sign in to comment.