22
33use super :: de:: intern_field_name;
44use super :: error:: { exception_already_thrown, ExcResult , ExceptionThrown , RangeError , Throwable , TypeError } ;
5- use super :: key_cache :: { get_or_create_key_cache , KeyCache } ;
5+ use super :: string_const :: { TAG , VALUE } ;
66use super :: syscall:: FnRet ;
77use super :: to_value:: ToValue ;
88use derive_more:: From ;
@@ -15,32 +15,20 @@ use v8::{Array, ArrayBuffer, IntegrityLevel, Local, Object, PinScope, Uint8Array
1515
1616/// Serializes `value` into a V8 into `scope`.
1717pub ( super ) fn serialize_to_js < ' scope > ( scope : & PinScope < ' scope , ' _ > , value : & impl Serialize ) -> FnRet < ' scope > {
18- let key_cache = get_or_create_key_cache ( scope) ;
19- let key_cache = & mut * key_cache. borrow_mut ( ) ;
20- value
21- . serialize ( Serializer :: new ( scope, key_cache) )
22- . map_err ( |e| e. throw ( scope) )
18+ value. serialize ( Serializer :: new ( scope) ) . map_err ( |e| e. throw ( scope) )
2319}
2420
2521/// Deserializes to V8 values.
22+ #[ derive( Copy , Clone ) ]
2623struct Serializer < ' this , ' scope , ' isolate > {
2724 /// The scope to serialize values into.
2825 scope : & ' this PinScope < ' scope , ' isolate > ,
29- /// A cache for frequently used strings.
30- key_cache : & ' this mut KeyCache ,
3126}
3227
3328impl < ' this , ' scope , ' isolate > Serializer < ' this , ' scope , ' isolate > {
3429 /// Creates a new serializer into `scope`.
35- pub fn new ( scope : & ' this PinScope < ' scope , ' isolate > , key_cache : & ' this mut KeyCache ) -> Self {
36- Self { scope, key_cache }
37- }
38-
39- fn reborrow ( & mut self ) -> Serializer < ' _ , ' scope , ' isolate > {
40- Serializer {
41- scope : self . scope ,
42- key_cache : self . key_cache ,
43- }
30+ pub fn new ( scope : & ' this PinScope < ' scope , ' isolate > ) -> Self {
31+ Self { scope }
4432 }
4533}
4634
@@ -158,22 +146,19 @@ impl<'this, 'scope, 'isolate> ser::Serializer for Serializer<'this, 'scope, 'iso
158146 }
159147
160148 fn serialize_variant < T : Serialize + ?Sized > (
161- mut self ,
149+ self ,
162150 tag : u8 ,
163151 var_name : Option < & str > ,
164152 value : & T ,
165153 ) -> Result < Self :: Ok , Self :: Error > {
166154 // Serialize the payload.
167- let value_value: Local < ' scope , Value > = value. serialize ( self . reborrow ( ) ) ?;
155+ let value_value: Local < ' scope , Value > = value. serialize ( self ) ?;
168156 // Figure out the tag.
169157 let tag_value: Local < ' scope , Value > = intern_field_name ( self . scope , var_name, tag as usize ) . into ( ) ;
170158 let values = [ tag_value, value_value] ;
171159
172160 // The property keys are always `"tag"` an `"value"`.
173- let names = [
174- self . key_cache . tag ( self . scope ) . into ( ) ,
175- self . key_cache . value ( self . scope ) . into ( ) ,
176- ] ;
161+ let names = [ TAG . string ( self . scope ) . into ( ) , VALUE . string ( self . scope ) . into ( ) ] ;
177162
178163 // Stitch together the object.
179164 let prototype = v8:: null ( self . scope ) . into ( ) ;
@@ -196,7 +181,7 @@ impl<'scope> ser::SerializeArray for SerializeArray<'_, 'scope, '_> {
196181
197182 fn serialize_element < T : Serialize + ?Sized > ( & mut self , elem : & T ) -> Result < ( ) , Self :: Error > {
198183 // Serialize the current `elem`ent.
199- let value = elem. serialize ( self . inner . reborrow ( ) ) ?;
184+ let value = elem. serialize ( self . inner ) ?;
200185
201186 // Set the value to the array slot at `index`.
202187 let index = self . next_index ;
@@ -243,7 +228,7 @@ impl<'scope> ser::SerializeNamedProduct for SerializeNamedProduct<'_, 'scope, '_
243228 elem : & T ,
244229 ) -> Result < ( ) , Self :: Error > {
245230 // Serialize the field value.
246- let value = elem. serialize ( self . inner . reborrow ( ) ) ?;
231+ let value = elem. serialize ( self . inner ) ?;
247232
248233 // Figure out the object property to use.
249234 let scope = self . inner . scope ;
0 commit comments