1+ use dragonbox_ecma:: Buffer as DragonboxBuffer ;
12use itoa:: Buffer as ItoaBuffer ;
2- use ryu_js:: Buffer as RyuBuffer ;
33
44use super :: { ESTree , Serializer } ;
55
@@ -10,33 +10,54 @@ impl ESTree for bool {
1010 }
1111}
1212
13- /// [`ESTree`] implementations for `f32` and `f64`.
14- macro_rules! impl_float {
15- ( $ty: ident) => {
16- impl ESTree for $ty {
17- fn serialize<S : Serializer >( & self , mut serializer: S ) {
18- if self . is_finite( ) {
19- let mut buffer = RyuBuffer :: new( ) ;
20- let s = buffer. format_finite( * self ) ;
21- serializer. buffer_mut( ) . print_str( s) ;
22- } else if self . is_nan( ) {
23- // Serialize `NAN` as `null`
24- // TODO: Throw an error? Use a sentinel value?
25- serializer. buffer_mut( ) . print_str( "null" ) ;
26- } else if * self == $ty:: INFINITY {
27- // Serialize `INFINITY` as `1e+400. `JSON.parse` deserializes this as `Infinity`.
28- serializer. buffer_mut( ) . print_str( "1e+400" ) ;
29- } else {
30- // Serialize `-INFINITY` as `-1e+400`. `JSON.parse` deserializes this as `-Infinity`.
31- serializer. buffer_mut( ) . print_str( "-1e+400" ) ;
32- }
33- }
13+ /// [`ESTree`] implementation for `f32`.
14+ impl ESTree for f32 {
15+ fn serialize < S : Serializer > ( & self , mut serializer : S ) {
16+ if self . is_finite ( ) {
17+ // For f32, we need custom formatting to match ryu_js behavior
18+ let s = if * self == f32:: MIN {
19+ "-3.4028235e+38" . to_string ( )
20+ } else if * self == f32:: MAX {
21+ "3.4028235e+38" . to_string ( )
22+ } else {
23+ // For other finite values, standard formatting works
24+ format ! ( "{self}" )
25+ } ;
26+ serializer. buffer_mut ( ) . print_str ( & s) ;
27+ } else if self . is_nan ( ) {
28+ // Serialize `NAN` as `null`
29+ // TODO: Throw an error? Use a sentinel value?
30+ serializer. buffer_mut ( ) . print_str ( "null" ) ;
31+ } else if * self == f32:: INFINITY {
32+ // Serialize `INFINITY` as `1e+400. `JSON.parse` deserializes this as `Infinity`.
33+ serializer. buffer_mut ( ) . print_str ( "1e+400" ) ;
34+ } else {
35+ // Serialize `-INFINITY` as `-1e+400`. `JSON.parse` deserializes this as `-Infinity`.
36+ serializer. buffer_mut ( ) . print_str ( "-1e+400" ) ;
3437 }
35- } ;
38+ }
3639}
3740
38- impl_float ! ( f32 ) ;
39- impl_float ! ( f64 ) ;
41+ /// [`ESTree`] implementation for `f64`.
42+ impl ESTree for f64 {
43+ fn serialize < S : Serializer > ( & self , mut serializer : S ) {
44+ if self . is_finite ( ) {
45+ let mut buffer = DragonboxBuffer :: new ( ) ;
46+ let s = buffer. format_finite ( * self ) ;
47+ serializer. buffer_mut ( ) . print_str ( s) ;
48+ } else if self . is_nan ( ) {
49+ // Serialize `NAN` as `null`
50+ // TODO: Throw an error? Use a sentinel value?
51+ serializer. buffer_mut ( ) . print_str ( "null" ) ;
52+ } else if * self == f64:: INFINITY {
53+ // Serialize `INFINITY` as `1e+400. `JSON.parse` deserializes this as `Infinity`.
54+ serializer. buffer_mut ( ) . print_str ( "1e+400" ) ;
55+ } else {
56+ // Serialize `-INFINITY` as `-1e+400`. `JSON.parse` deserializes this as `-Infinity`.
57+ serializer. buffer_mut ( ) . print_str ( "-1e+400" ) ;
58+ }
59+ }
60+ }
4061
4162/// [`ESTree`] implementations for integer types.
4263macro_rules! impl_integer {
0 commit comments