44#![ allow( dead_code) ]
55
66use std:: {
7+ collections:: hash_map:: Entry ,
78 fmt:: { Debug , Display } ,
89 io:: Write ,
910} ;
1011
11- use crate :: Context ;
12+ use rustc_hash:: FxHashMap ;
13+
14+ use crate :: { Context , JsObject } ;
1215
1316/// TODO: doc
1417#[ derive( Debug , Clone , Copy ) ]
@@ -21,27 +24,40 @@ pub struct Header {
2124/// TODO: doc
2225pub trait Serialize {
2326 /// Serialize type
24- fn serialize ( & self , s : & mut Serializer < ' _ > ) -> Result < ( ) , SnapshotError > ;
27+ fn serialize ( & self , s : & mut SnapshotSerializer < ' _ > ) -> Result < ( ) , SnapshotError > ;
2528}
2629
2730impl Serialize for Header {
28- fn serialize ( & self , s : & mut Serializer < ' _ > ) -> Result < ( ) , SnapshotError > {
31+ fn serialize ( & self , s : & mut SnapshotSerializer < ' _ > ) -> Result < ( ) , SnapshotError > {
2932 s. write_bytes ( & self . signature ) ?;
3033 s. write_u32 ( self . version ) ?;
3134 Ok ( ( ) )
3235 }
3336}
3437
38+ impl Serialize for JsObject {
39+ fn serialize ( & self , s : & mut SnapshotSerializer < ' _ > ) -> Result < ( ) , SnapshotError > {
40+ let len = s. objects . len ( ) ;
41+ let value = match s. objects . entry ( self . clone ( ) ) {
42+ Entry :: Occupied ( entry) => * entry. get ( ) ,
43+ Entry :: Vacant ( entry) => * entry. insert ( len as u32 ) ,
44+ } ;
45+
46+ s. write_u32 ( value) ?;
47+ Ok ( ( ) )
48+ }
49+ }
50+
3551/// TODO: doc
3652pub struct Snapshot {
3753 header : Header ,
3854 bytes : Vec < u8 > ,
3955}
4056
4157/// TODO: doc
42- pub struct Serializer < ' writer > {
58+ pub struct SnapshotSerializer < ' writer > {
4359 writer : & ' writer mut dyn Write ,
44- // ... other state ...
60+ objects : FxHashMap < usize , u32 > ,
4561}
4662
4763/// TODO: doc
@@ -68,15 +84,27 @@ impl From<std::io::Error> for SnapshotError {
6884 }
6985}
7086
71- impl < ' writer > Serializer < ' writer > {
87+ impl < ' writer > SnapshotSerializer < ' writer > {
7288 /// TODO: doc
7389 pub fn new ( writer : & ' writer mut dyn Write ) -> Self {
74- Self { writer }
90+ Self {
91+ writer,
92+ objects : FxHashMap :: default ( ) ,
93+ }
7594 }
7695
7796 /// Serialize the given [`Context`].
7897 pub fn serialize ( & mut self , context : & mut Context < ' _ > ) -> Result < ( ) , SnapshotError > {
79- let header = Header { signature : * b".boa" , version : 256 } ;
98+ boa_gc:: force_collect ( ) ;
99+
100+ // boa_gc::walk_gc_alloc_pointers(|address| {
101+
102+ // });
103+
104+ let header = Header {
105+ signature : * b".boa" ,
106+ version : 256 ,
107+ } ;
80108
81109 header. serialize ( self ) ?;
82110 context. serialize ( self )
@@ -97,59 +125,59 @@ impl<'writer> Serializer<'writer> {
97125 }
98126 /// Writes a binary i8 to the dyn Write
99127 pub fn write_i8 ( & mut self , v : i8 ) -> Result < ( ) , SnapshotError > {
100- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
128+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
101129 }
102130
103131 /// Writes a binary little endian u16 to the dyn Write
104132 pub fn write_u16 ( & mut self , v : u16 ) -> Result < ( ) , SnapshotError > {
105- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
133+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
106134 }
107135 /// Writes a binary little endian i16 to the dyn Write
108136 pub fn write_i16 ( & mut self , v : i16 ) -> Result < ( ) , SnapshotError > {
109- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
137+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
110138 }
111139
112140 /// Writes a binary little endian u32 to the dyn Write
113141 pub fn write_u32 ( & mut self , v : u32 ) -> Result < ( ) , SnapshotError > {
114- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
142+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
115143 }
116144 /// Writes a binary little endian i32 to the dyn Write
117145 pub fn write_i32 ( & mut self , v : i32 ) -> Result < ( ) , SnapshotError > {
118- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
146+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
119147 }
120148
121149 /// Writes a binary little endian f32 to the dyn Write
122150 pub fn write_f32 ( & mut self , v : f32 ) -> Result < ( ) , SnapshotError > {
123- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
151+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
124152 }
125153 /// Writes a binary little endian f64 to the dyn Write
126154 pub fn write_f64 ( & mut self , v : f64 ) -> Result < ( ) , SnapshotError > {
127- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
155+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
128156 }
129157
130158 /// Writes a binary little endian u64 to the dyn Write
131159 pub fn write_u64 ( & mut self , v : u64 ) -> Result < ( ) , SnapshotError > {
132- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
160+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
133161 }
134162 /// Writes a binary little endian i64 to the dyn Write
135163 pub fn write_i64 ( & mut self , v : i64 ) -> Result < ( ) , SnapshotError > {
136- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
164+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
137165 }
138166 /// Writes a binary little endian u128 to the dyn Write
139167 pub fn write_u128 ( & mut self , v : u128 ) -> Result < ( ) , SnapshotError > {
140- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
168+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
141169 }
142170 /// Writes a binary little endian i128 to the dyn Write
143171 pub fn write_i128 ( & mut self , v : i128 ) -> Result < ( ) , SnapshotError > {
144- Ok ( self . write_bytes ( & v. to_ne_bytes ( ) ) ?)
172+ Ok ( self . write_bytes ( & v. to_le_bytes ( ) ) ?)
145173 }
146174 /// Writes a binary little endian usize as u64 to the dyn Write
147175 pub fn write_usize ( & mut self , v : usize ) -> Result < ( ) , SnapshotError > {
148- Ok ( self . write_bytes ( & ( v as u64 ) . to_ne_bytes ( ) ) ?)
176+ Ok ( self . write_bytes ( & ( v as u64 ) . to_le_bytes ( ) ) ?)
149177 }
150178 /// Writes a binary little endian isize as i64 to the dyn Write
151179 pub fn write_isize ( & mut self , v : isize ) -> Result < ( ) , SnapshotError > {
152- Ok ( self . write_bytes ( & ( v as i64 ) . to_ne_bytes ( ) ) ?)
180+ Ok ( self . write_bytes ( & ( v as i64 ) . to_le_bytes ( ) ) ?)
153181 }
154182 /// Writes a binary u8 array to the dyn Write
155183 pub fn write_buf ( & mut self , v : & [ u8 ] ) -> Result < ( ) , SnapshotError > {
0 commit comments