Jurassic.Library.ObjectInstance.GetPrimitiveValue C# (CSharp) Method

GetPrimitiveValue() private method

Returns a primitive value that represents the current object. Used by the addition and equality operators.
private GetPrimitiveValue ( PrimitiveTypeHint typeHint ) : object
typeHint PrimitiveTypeHint Indicates the preferred type of the result.
return object
        internal object GetPrimitiveValue(PrimitiveTypeHint typeHint)
        {
            // The first step is to try calling the @@toPrimitive symbol.
            string hintStr;
            switch (typeHint)
            {
                case PrimitiveTypeHint.None:
                    hintStr = "default";
                    break;
                case PrimitiveTypeHint.Number:
                    hintStr = "number";
                    break;
                case PrimitiveTypeHint.String:
                    hintStr = "string";
                    break;
                default:
                    throw new InvalidOperationException($"Unsupported PrimitiveTypeHint value '{typeHint}'.");
            }
            object toPrimitiveResult;
            if (TryCallMemberFunction(out toPrimitiveResult, Engine.Symbol.ToPrimitive, hintStr) == true)
            {
                // Return value must be primitive.
                if (TypeUtilities.IsPrimitive(toPrimitiveResult) == false)
                    throw new JavaScriptException(Engine, ErrorType.TypeError, "Cannot convert object to primitive value.");
                return toPrimitiveResult;
            }

            // If that didn't work.
            return GetPrimitiveValuePreES6(typeHint);
        }