@@ -15,7 +15,7 @@ mod tests;
1515use super :: function:: { make_builtin_fn, make_constructor_fn} ;
1616use crate :: {
1717 builtins:: {
18- object:: { internal_methods_trait :: ObjectInternalMethods , ObjectKind } ,
18+ object:: ObjectData ,
1919 value:: { ResultValue , Value , ValueData } ,
2020 } ,
2121 exec:: Interpreter ,
@@ -35,19 +35,11 @@ impl Boolean {
3535 args : & [ Value ] ,
3636 _: & mut Interpreter ,
3737 ) -> ResultValue {
38- this. set_kind ( ObjectKind :: Boolean ) ;
39-
4038 // Get the argument, if any
41- if let Some ( ref value) = args. get ( 0 ) {
42- this. set_internal_slot ( "BooleanData" , Self :: to_boolean ( value) ) ;
43- } else {
44- this. set_internal_slot ( "BooleanData" , Self :: to_boolean ( & Value :: from ( false ) ) ) ;
45- }
39+ let data = args. get ( 0 ) . map ( |x| x. to_boolean ( ) ) . unwrap_or ( false ) ;
40+ this. set_data ( ObjectData :: Boolean ( data) ) ;
4641
47- match args. get ( 0 ) {
48- Some ( ref value) => Ok ( Self :: to_boolean ( value) ) ,
49- None => Ok ( Self :: to_boolean ( & Value :: from ( false ) ) ) ,
50- }
42+ Ok ( Value :: from ( data) )
5143 }
5244
5345 /// The `toString()` method returns a string representing the specified `Boolean` object.
@@ -73,22 +65,7 @@ impl Boolean {
7365 /// [spec]: https://tc39.es/ecma262/#sec-boolean.prototype.valueof
7466 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/valueOf
7567 pub ( crate ) fn value_of ( this : & mut Value , _: & [ Value ] , _: & mut Interpreter ) -> ResultValue {
76- Ok ( Self :: this_boolean_value ( this) )
77- }
78-
79- // === Utility Functions ===
80- /// [toBoolean](https://tc39.es/ecma262/#sec-toboolean)
81- /// Creates a new boolean value from the input
82- #[ allow( clippy:: wrong_self_convention) ]
83- pub ( crate ) fn to_boolean ( value : & Value ) -> Value {
84- match * value. deref ( ) . borrow ( ) {
85- ValueData :: Object ( _) => Value :: from ( true ) ,
86- ValueData :: String ( ref s) if !s. is_empty ( ) => Value :: from ( true ) ,
87- ValueData :: Rational ( n) if n != 0.0 && !n. is_nan ( ) => Value :: from ( true ) ,
88- ValueData :: Integer ( n) if n != 0 => Value :: from ( true ) ,
89- ValueData :: Boolean ( v) => Value :: from ( v) ,
90- _ => Value :: from ( false ) ,
91- }
68+ Ok ( Value :: from ( Self :: this_boolean_value ( this) ) )
9269 }
9370
9471 /// An Utility function used to get the internal BooleanData.
@@ -97,11 +74,14 @@ impl Boolean {
9774 /// - [ECMAScript reference][spec]
9875 ///
9976 /// [spec]: https://tc39.es/ecma262/#sec-thisbooleanvalue
100- pub ( crate ) fn this_boolean_value ( value : & Value ) -> Value {
77+ pub ( crate ) fn this_boolean_value ( value : & Value ) -> bool {
10178 match * value. deref ( ) . borrow ( ) {
102- ValueData :: Boolean ( v) => Value :: from ( v) ,
103- ValueData :: Object ( ref v) => ( v) . deref ( ) . borrow ( ) . get_internal_slot ( "BooleanData" ) ,
104- _ => Value :: from ( false ) ,
79+ ValueData :: Boolean ( v) => v,
80+ ValueData :: Object ( ref v) => match v. deref ( ) . borrow ( ) . data {
81+ ObjectData :: Boolean ( boolean) => boolean,
82+ _ => unreachable ! ( ) ,
83+ } ,
84+ _ => false ,
10585 }
10686 }
10787
@@ -110,7 +90,6 @@ impl Boolean {
11090 // Create Prototype
11191 // https://tc39.es/ecma262/#sec-properties-of-the-boolean-prototype-object
11292 let prototype = Value :: new_object ( Some ( global) ) ;
113- prototype. set_internal_slot ( "BooleanData" , Self :: to_boolean ( & Value :: from ( false ) ) ) ;
11493
11594 make_builtin_fn ( Self :: to_string, "toString" , & prototype, 0 ) ;
11695 make_builtin_fn ( Self :: value_of, "valueOf" , & prototype, 0 ) ;
0 commit comments