// Convert the given value to a boolean value. public static bool ToBoolean(object value) { double d; if (value == null || value is JUndefinedObject) return false; else if (value is bool) return (bool)value; else if (PrimSupport.AsNumber(value, out d)) return (d != 0 && !Double.IsNaN(d)); else if (value is string) return ((string)value).Length > 0; else return true; } // ToBoolean
} // ToString // Convert the given value to a string value. public static object ToObject(object value) { double d; if (value is string) return new JStringObject((string)value); else if (PrimSupport.AsNumber(value, out d)) return new JNumberObject(d); else if (value is bool) return new JBooleanObject((bool)value); else if (value == null) throw new TypeError("attempt to convert null to an object"); else if (value is JUndefinedObject) throw new TypeError("attempt to convert undefined to an object"); else return value; } // ToObject
} // ToUInt16 // Convert the given value to a string value. public static string ToString(object value) { double d; if (value is string) return (string)value; else if (PrimSupport.AsNumber(value, out d)) { // HACK snewman 7/26/01: format according to grammar in 9.8.1 on p. 47 return System.Convert.ToString(d); } else if (value == null) return "null"; else if (value is JUndefinedObject) return "undefined"; else if (value is bool) return (bool)value ? "true" : "false"; JObject asObj = value as JObject; if (asObj != null) return ToString(asObj.DefaultValue(JObject.ValueHintType.String)); else return value.ToString(); } // ToString
} // ToBoolean // Convert the given value to a numeric value. public static double ToNumber(object value) { double d; if (PrimSupport.AsNumber(value, out d)) return d; else if (value == null) return 0; else if (value is JUndefinedObject) return Double.NaN; else if (value is bool) return (bool)value ? 1 : 0; else if (value is string) { // HACK snewman 7/26/01: parse according to grammar in 9.3.1 on p. 43 return System.Convert.ToDouble((string)value); } JObject asObj = value as JObject; if (asObj != null) return ToNumber(asObj.DefaultValue(JObject.ValueHintType.Number)); else throw new TypeError("attempt to convert non-JavaScript object to number"); } // ToNumber