Example #1
0
        /// <summary>
        /// Converts any JavaScript value to a primitive string value.
        /// </summary>
        /// <param name="value"> The value to convert. </param>
        /// <returns> A primitive string value. </returns>
        public static string ToString(object value)
        {
            if (value == null || value == Undefined.Value)
            {
                return("undefined");
            }
            if (value == Null.Value)
            {
                return("null");
            }
            if (value is bool)
            {
                return((bool)value ? "true" : "false");
            }
            if (value is int)
            {
                return(((int)value).ToString());
            }
            if (value is uint)
            {
                return(((uint)value).ToString());
            }
            if (value is double)
            {
                // Check if the value is in the cache.
                double doubleValue = (double)value;
                lock (cacheLock)
                {
                    if (doubleValue == cacheValue)
                    {
                        return(cacheResult);
                    }
                }

                // Convert the number to a string.
                var result = NumberFormatter.ToString(doubleValue, 10, NumberFormatter.Style.Regular);

                // Cache the result.
                lock (cacheLock)
                {
                    cacheValue  = doubleValue;
                    cacheResult = result;
                }

                return(result);
            }
            if (value is string)
            {
                return((string)value);
            }
            if (value is ConcatenatedString)
            {
                return(value.ToString());
            }
            if (value is ObjectInstance)
            {
                return(ToString(ToPrimitive(value, PrimitiveTypeHint.String)));
            }
            throw new ArgumentException(string.Format("Cannot convert object of type '{0}' to a string.", value.GetType()), "value");
        }
Example #2
0
        /// <summary>
        /// Converts any JavaScript value to a primitive string value.
        /// </summary>
        /// <param name="value"> The value to convert. </param>
        /// <returns> A primitive string value. </returns>
        public static string ToString(object value)
        {
            if (value == null || value == Undefined.Value)
            {
                return("undefined");
            }
            if (value == Null.Value)
            {
                return("null");
            }
            if (value is bool)
            {
                return((bool)value ? "true" : "false");
            }
            if (value is int)
            {
                return(((int)value).ToString());
            }
            if (value is uint)
            {
                return(((uint)value).ToString());
            }
            if (value is double)
            {
                // Check if the value is in the cache.
                double doubleValue = (double)value;
                var    cache       = numberToStringCache;
                if (doubleValue == cache.Value)
                {
                    return(cache.Result);
                }

                // Convert the number to a string.
                var result = NumberFormatter.ToString((double)value, 10, NumberFormatter.Style.Regular);

                // Cache the result.
                // This is thread-safe on Intel but not architectures with weak write ordering.
                numberToStringCache = new NumberToStringCache()
                {
                    Value = doubleValue, Result = result
                };

                return(result);
            }
            if (value is string)
            {
                return((string)value);
            }
            if (value is ConcatenatedString)
            {
                return(value.ToString());
            }
            if (value is ObjectInstance)
            {
                return(ToString(ToPrimitive(value, PrimitiveTypeHint.String)));
            }
            throw new ArgumentException(string.Format("Cannot convert object of type '{0}' to a string.", value.GetType()), "value");
        }