pub fn to_primitive(
        &self,
        context: &mut Context,
        preferred_type: PreferredType,
    ) -> JsResult<JsValue> {
        // 1. Assert: input is an ECMAScript language value. (always a value not need to check)
        // 2. If Type(input) is Object, then
        if let JsValue::Object(obj) = self {
            if let Some(exotic_to_prim) =
                obj.get_method(context, WellKnownSymbols::to_primitive())?
            {
                let hint = match preferred_type {
                    PreferredType::String => "string",
                    PreferredType::Number => "number",
                    PreferredType::Default => "default",
                }
                .into();
                let result = exotic_to_prim.call(self, &[hint], context)?;
                return if result.is_object() {
                    Err(context.construct_type_error("Symbol.toPrimitive cannot return an object"))
                } else {
                    Ok(result)
                };
            }

            let mut hint = preferred_type;

            if hint == PreferredType::Default {
                hint = PreferredType::Number;
            };

            // g. Return ? OrdinaryToPrimitive(input, hint).
            obj.ordinary_to_primitive(context, hint)
        } else {
            // 3. Return input.
            Ok(self.clone())
        }
    }
/// The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType.
///
/// <https://tc39.es/ecma262/#sec-toprimitive>
The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType.
The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType .
pub fn to_primitive(
        &self,
        context: &mut Context,
        preferred_type: PreferredType,
    ) -> JsResult<JsValue> {
        // 1. Assert: input is an ECMAScript language value. (always a value not need to check)
        // 2. If Type(input) is Object, then
        if let JsValue::Object(obj) = self {
            if let Some(exotic_to_prim) =
                obj.get_method(context, WellKnownSymbols::to_primitive())?
            {
                let hint = match preferred_type {
                    PreferredType::String => "string",
                    PreferredType::Number => "number",
                    PreferredType::Default => "default",
                }
                .into();
                let result = exotic_to_prim.call(self, &[hint], context)?;
                return if result.is_object() {
                    Err(context.construct_type_error("Symbol.toPrimitive cannot return an object"))
                } else {
                    Ok(result)
                };
            }
            let mut hint = preferred_type;
            if hint == PreferredType::Default {
                hint = PreferredType::Number;
            };
            // g. Return ? OrdinaryToPrimitive(input, hint).
            obj.ordinary_to_primitive(context, hint)
        } else {
            // 3. Return input.
            Ok(self.clone())
        }
    }
pub fn to_primitive ( & self , context : & mut Context , preferred_type : PreferredType , ) -> JsResult < JsValue > { // 1. Assert: input is an ECMAScript language value. (always a value not need to check) // 2. If Type(input) is Object, then if let JsValue :: Object ( obj ) = self { if let Some ( exotic_to_prim ) = obj . get_method ( context , WellKnownSymbols :: to_primitive ( ) ) ? { let hint = match preferred_type { PreferredType :: String => " " , PreferredType :: Number => " " , PreferredType :: Default => " " , } . into ( ) ; let result = exotic_to_prim . call ( self , & [ hint ] , context ) ? ; return if result . is_object ( ) { Err ( context . construct_type_error ( " " ) ) } else { Ok ( result ) } ; } let mut hint = preferred_type ; if hint == PreferredType :: Default { hint = PreferredType :: Number ; } ; // g. Return ? OrdinaryToPrimitive(input, hint). obj . ordinary_to_primitive ( context , hint ) } else { // 3. Return input. Ok ( self . clone ( ) ) } }
The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType.
The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType .
&self
context
&mut Context
preferred_type
PreferredType
&self
context
&mut Context
preferred_type
PreferredType