public static double min(Object x, Object y, params Object [] args) { ArrayList values = new ArrayList(args); if (x != null) { values.Add(x); } if (y != null) { values.Add(y); } double val; double result = Double.PositiveInfinity; foreach (object value in values) { val = Convert.ToNumber(value); if (Double.IsNaN(val)) { return(Double.NaN); } else if (val < result) { result = val; } } return(result); }
public static Double min(params Object[] args) { Double dx, dy; if (args == null || args.Length < 1) { return(Double.NegativeInfinity); } else { dx = args[0] is Double ? (Double)args[0] : Convert.ToNumber(args[0]); } if (args.Length < 2) { return(dx); } else { dy = dx = args[1] is Double ? (Double)args[1] : Convert.ToNumber(args[1]); } Double min = dx.CompareTo(dy) < 0 ? dx : dy; if (args.Length > 2) { foreach (Object o in args) { min = MathObject.min(min, o); } } return(min); }
public static double setMonth(object thisObj, double dmonth, object date) { SemanticAnalyser.assert_type(thisObj, typeof(DateObject)); DateObject dt = (DateObject)thisObj; double t = DateConstructor.LocalTime(dt.ms); double new_date; if (date == null) { new_date = DateConstructor.DateFromTime(t); } else { new_date = Convert.ToNumber(date); } double day = DateConstructor.MakeDay((double)DateConstructor.YearFromTime(t), dmonth, new_date); double new_val = DateConstructor.ToUTC(DateConstructor.MakeDate(day, t % DateConstructor.MS_PER_DAY)); dt.ms = DateConstructor.TimeClip(new_val); return(dt.ms); }
// Evaluate a numeric binary operator on two values. public static Object DoOp(Object v1, Object v2, JSToken operatorTok) { double n1 = Convert.ToNumber(v1); double n2 = Convert.ToNumber(v2); switch (operatorTok) { case JSToken.Minus: { return(n1 - n2); } // Not reached. case JSToken.Multiply: { return(n1 * n2); } // Not reached. case JSToken.Divide: { return(n1 / n2); } // Not reached. case JSToken.Modulo: { return(n1 % n2); } // Not reached. } throw new JScriptException(JSError.InternalError); }
public static double setUTCSeconds(object thisObj, double dsec, object msec) { SemanticAnalyser.assert_type(thisObj, typeof(DateObject)); DateObject date = (DateObject)thisObj; double t = date.ms; double new_ms; if (msec == null) { new_ms = DateConstructor.msFromTime(t); } else { new_ms = Convert.ToNumber(msec); } double time = DateConstructor.MakeTime(DateConstructor.HourFromTime(t), DateConstructor.MinFromTime(t), dsec, new_ms); double day = Math.Floor(t / DateConstructor.MS_PER_DAY); double new_val = DateConstructor.MakeDate(day, time); date.ms = DateConstructor.TimeClip(new_val); return(date.ms); }
private static Object DoOp(Object v1, Object v2, IConvertible ic1, IConvertible ic2, JSToken operatorTok) { if (operatorTok == JSToken.Minus) { IConvertible ic1a = ic1; Object p1 = Convert.ToPrimitive(v1, PreferredType.Either, ref ic1a); TypeCode t1 = Convert.GetTypeCode(p1, ic1a); if (t1 == TypeCode.Char) { IConvertible ic2a = ic2; Object p2 = Convert.ToPrimitive(v2, PreferredType.Either, ref ic2a); TypeCode t2 = Convert.GetTypeCode(p2, ic2a); if (t2 == TypeCode.String) { String str2 = ic2a.ToString(null); if (str2.Length == 1) { t2 = TypeCode.Char; p2 = str2[0]; ic2a = Convert.GetIConvertible(p2); } } Object result = NumericBinary.DoOp(Convert.ToNumber(p1, ic1a), Convert.ToNumber(p2, ic2a), operatorTok); if (t2 != TypeCode.Char) { result = Convert.Coerce2(result, TypeCode.Char, false); } return(result); } } return(NumericBinary.DoOp(Convert.ToNumber(v1, ic1), Convert.ToNumber(v2, ic2), operatorTok)); }
public static double min(Object x, Object y, params Object[] args) { if (x is Missing) { return(Double.PositiveInfinity); } double dx = Convert.ToNumber(x); if (y is Missing) { return(dx); } double dy = Convert.ToNumber(y); double result = MathObject.Compare(dx, dy); if (result != result) { return(result); } double lhMin = dx; if (result > 0) { lhMin = dy; } if (args.Length == 0) { return(lhMin); } return(MathObject.minv(lhMin, args, 0)); }
public int Compare(Object x, Object y) { if (x == null || x is Missing) { if (y == null || y is Missing) { return(0); } else { return(1); } } else if (y == null || y is Missing) { return(-1); } if (this.compareFn != null) { double result = Convert.ToNumber(this.compareFn.Call(new Object[] { x, y }, null)); if (result != result) { throw new JScriptException(JSError.NumberExpected); } return((int)result); } else { return(String.CompareOrdinal(Convert.ToString(x), Convert.ToString(y))); } }
// Evaluate a numeric unary operator on a value. public Object EvaluateUnary(Object v) { switch (operatorTok) { case JSToken.BitwiseNot: { return(~(Convert.ToInt32(v))); } // Not reached. case JSToken.LogicalNot: { return(!(Convert.ToBoolean(v))); } // Not reached. case JSToken.Minus: { return(-(Convert.ToNumber(v))); } // Not reached. case JSToken.Plus: { return(Convert.ToNumber(v)); } // Not reached. } throw new JScriptException(JSError.InternalError); }
internal ErrorObject(ErrorPrototype parent, Object[] args) : base(parent) { this.exception = null; this.description = ""; this.number = 0; if (args.Length == 1) { if (args[0] == null || Convert.IsPrimitiveNumericType(args[0].GetType())) { this.number = Convert.ToNumber(args[0]); } else { this.description = Convert.ToString(args[0]); } } else if (args.Length > 1) { this.number = Convert.ToNumber(args[0]); this.description = Convert.ToString(args[1]); } this.message = this.description; this.noExpando = false; }
public new ArrayObject CreateInstance(params Object[] args) { ArrayObject arrayObj = new ArrayObject(this.originalPrototype, typeof(ArrayObject)); if (args.Length != 0) { if (args.Length == 1) { Object arg0 = args[0]; IConvertible ic = Convert.GetIConvertible(arg0); switch (Convert.GetTypeCode(arg0, ic)) { case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: double d = Convert.ToNumber(arg0, ic); uint len = Convert.ToUint32(arg0, ic); if (d != (double)len) { throw new JScriptException(JSError.ArrayLengthConstructIncorrect); } arrayObj.length = len; return(arrayObj); } } if (args.Length == 1 && args[0] is Array) { Array array = (Array)args[0]; if (array.Rank != 1) { throw new JScriptException(JSError.TypeMismatch); } arrayObj.length = array.Length; for (int i = 0; i < array.Length; i++) { arrayObj.SetValueAtIndex((uint)i, array.GetValue(i)); } } else { arrayObj.length = args.Length; for (int i = 0; i < args.Length; i++) { arrayObj.SetValueAtIndex((uint)i, args[i]); } } } return(arrayObj); }
//"x in y" should return true whenever "for (var z in y) if (z === x) return true" returns true public static bool JScriptIn(Object v1, Object v2) { bool result = false; if (v2 is ScriptObject) { return(!(((ScriptObject)v2).GetMemberValue(Convert.ToString(v1)) is Missing)); } else if (v2 is Array) { Array arr = (Array)v2; double d = Convert.ToNumber(v1); int i = (int)d; return(d == i && arr.GetLowerBound(0) <= i && i <= arr.GetUpperBound(0)); } else if (v2 is IEnumerable) { if (v1 == null) { return(false); } //Do not enumerate when a direct lookup is available if (v2 is IDictionary) { return(((IDictionary)v2).Contains(v1)); } if (v2 is IExpando) { MemberInfo[] members = ((IReflect)v2).GetMember(Convert.ToString(v1), BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public); return(members.Length > 0); } IEnumerator enu = ((IEnumerable)v2).GetEnumerator(); while (!result && enu.MoveNext()) { if (v1.Equals(enu.Current)) { return(true); } } } else if (v2 is IEnumerator) { if (v1 == null) { return(false); } IEnumerator enu = (IEnumerator)v2; while (!result && enu.MoveNext()) { if (v1.Equals(enu.Current)) { return(true); } } } throw new JScriptException(JSError.ObjectExpected); }
public static object valueOf(object thisObj) { if (!Convert.IsNumber(thisObj)) { throw new JScriptException(JSError.NumberExpected); } else { return(Convert.ToNumber(thisObj)); } }
public new NumberObject CreateInstance(params Object [] args) { double value = 0; if (args.Length > 0) { value = Convert.ToNumber(args [0]); } return(new NumberObject(value)); }
public static string toLocaleString(object thisObj) { if (!Convert.IsNumber(thisObj)) { throw new JScriptException(JSError.NumberExpected); } else { return(Convert.ToNumber(thisObj).ToString("N")); } }
internal override Object Call(Object[] args, Object thisob) { if (args.Length == 0) { return(0); } else { return(Convert.ToNumber(args[0])); } }
public new NumberObject CreateInstance(params Object[] args) { if (args.Length == 0) { return(new NumberObject(this.originalPrototype, 0.0, false)); } else { return(new NumberObject(this.originalPrototype, Convert.ToNumber(args[0]), false)); } }
// Perform a call on this object. internal override Object Call (VsaEngine engine, Object thisob, Object[] args) { if (args.Length == 0) { return(Double.NaN); } else { return(Convert.ToNumber(args[0])); } }
public static double UTC(Object year, Object month, Object date, Object hours, Object minutes, Object seconds, Object ms) { double _year = 0, _month = 0, _date = 1, _hours = 0, _minutes = 0, _seconds = 0, _ms = 0; if (year == null && month == null) { DateTime now = DateTime.Now; _year = now.Year; _month = now.Month - 1; _date = now.Day; _hours = now.Hour; _minutes = now.Minute; _seconds = now.Second; _ms = now.Millisecond; _ms -= TimeZone.CurrentTimeZone.GetUtcOffset(now).TotalMilliseconds; goto done; } _year = Convert.ToNumber(year); if (month != null) { _month = Convert.ToNumber(month); } if (date != null) { _date = Convert.ToNumber(date); } if (hours != null) { _hours = Convert.ToNumber(hours); } if (minutes != null) { _minutes = Convert.ToNumber(minutes); } if (seconds != null) { _seconds = Convert.ToNumber(seconds); } if (ms != null) { _ms = Convert.ToNumber(ms); } if (!Double.IsNaN(_year) && _year >= 0 && _year <= 99) { _year += 1900; } done: return(TimeClip(msec_from_date(_year, _month, _date, _hours, _minutes, _seconds, _ms))); }
// Evaluate a plus operator on two values. public static Object DoOp(Object v1, Object v2) { v1 = Convert.ToPrimitive(v1, DefaultValueHint.None); v2 = Convert.ToPrimitive(v2, DefaultValueHint.None); if (v1 is String || v2 is String) { return(((String)v1) + ((String)v2)); } else { return(Convert.ToNumber(v1) + Convert.ToNumber(v2)); } }
// Compare two values. public static double JScriptCompare(Object v1, Object v2) { v1 = Convert.ToPrimitive(v1, DefaultValueHint.None); v2 = Convert.ToPrimitive(v2, DefaultValueHint.None); if (v1 is String && v2 is String) { return(String.CompareOrdinal((String)v1, (String)v2)); } else { return(Convert.ToNumber(v1) - Convert.ToNumber(v2)); } }
internal static uint ToUint32(object value) { double val = Convert.ToNumber(value); if (Double.IsInfinity(val) || double.IsNaN(val)) { return(0); } else { return((uint)(val % 4294967296)); } }
public object EvaluatePlus(object v1, object v2) { object val1 = Convert.ToPrimitive(v1, null); object val2 = Convert.ToPrimitive(v2, null); if (Convert.IsString(val1) || Convert.IsString(val2)) { return(Convert.ToString(val1) + Convert.ToString(val2)); } else { return(Convert.ToNumber(val1) + Convert.ToNumber(val2)); } }
public object EvaluatePostOrPrefix(ref object v) { double value = Convert.ToNumber(v); v = value; int oper = (int)this.oper; if (oper % 2 == 1) /* prefix? */ { return(value + 1); } else { return(value - 1); } }
public static double setHours(object thisObj, double dhour, object min, object sec, object msec) { SemanticAnalyser.assert_type(thisObj, typeof(DateObject)); DateObject date = (DateObject)thisObj; double t = DateConstructor.LocalTime(date.ms); double new_min; if (min == null) { new_min = DateConstructor.MinFromTime(t); } else { new_min = Convert.ToNumber(min); } double new_sec; if (sec == null) { new_sec = DateConstructor.SecFromTime(t); } else { new_sec = Convert.ToNumber(sec); } double new_ms; if (msec == null) { new_ms = DateConstructor.msFromTime(t); } else { new_ms = Convert.ToNumber(msec); } double time = DateConstructor.MakeTime(dhour, new_min, new_sec, new_ms); double day = Math.Floor(t / DateConstructor.MS_PER_DAY); double new_val = DateConstructor.ToUTC(DateConstructor.MakeDate(day, time)); date.ms = DateConstructor.TimeClip(new_val); return(date.ms); }
public double Invoke(object arg) { if (arg is Object []) { Object [] args = (Object [])arg; if (args.Length == 0) { return(0); } else { return(Convert.ToNumber(args [0])); } } return(Convert.ToNumber(arg)); }
public static string toFixed(object thisObj, double fractionDigits) { if (!Convert.IsNumber(thisObj)) { throw new JScriptException(JSError.NumberExpected); } double value = Convert.ToNumber(thisObj); int prec = Convert.ToInt32(fractionDigits); if (prec < 0 || prec > 21) { throw new JScriptException(JSError.PrecisionOutOfRange); } return(value.ToString("F" + prec, CultureInfo.InvariantCulture)); }
public new DateObject CreateInstance(params Object[] args) { if (args.Length == 0) { return(new DateObject(this.originalPrototype, DateTime.Now.ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970)); } if (args.Length == 1) { Object arg0 = args[0]; IConvertible ic = Convert.GetIConvertible(arg0); TypeCode tcode = Convert.GetTypeCode(arg0, ic); if (tcode == TypeCode.DateTime) { return(new DateObject(this.originalPrototype, ic.ToDateTime(null).ToUniversalTime().Ticks / DatePrototype.ticksPerMillisecond - DatePrototype.msTo1970)); } Object yv = Convert.ToPrimitive(arg0, PreferredType.Either, ref ic); if (Convert.GetTypeCode(yv, ic) == TypeCode.String) { return(new DateObject(this.originalPrototype, parse(ic.ToString(null)))); } double d = Convert.ToNumber(yv, ic); if (DatePrototype.minDate <= d && d <= DatePrototype.maxDate) { return(new DateObject(this.originalPrototype, d)); } return(new DateObject(this.originalPrototype, Double.NaN)); } double dyear = Convert.ToNumber(args[0]); double dmonth = Convert.ToNumber(args[1]); double ddate = (args.Length > 2) ? Convert.ToNumber(args[2]) : 1; double dhours = (args.Length > 3) ? Convert.ToNumber(args[3]) : 0; double dminutes = (args.Length > 4) ? Convert.ToNumber(args[4]) : 0; double dseconds = (args.Length > 5) ? Convert.ToNumber(args[5]) : 0; double dms = (args.Length > 6) ? Convert.ToNumber(args[6]) : 0; int y = (int)dyear; if (!Double.IsNaN(dyear) && 0 <= y && y <= 99) { dyear = y + 1900; } double day = DatePrototype.MakeDay(dyear, dmonth, ddate); double time = DatePrototype.MakeTime(dhours, dminutes, dseconds, dms); return(new DateObject(this.originalPrototype, DatePrototype.TimeClip(DatePrototype.UTC(DatePrototype.MakeDate(day, time))))); }
// Put a property to this object. internal override void Put(String name, Object value) { if (name == "length") { length = value; } else if (CanPut(name)) { double num = Convert.ToNumber(name); uint inum = Convert.ToUInt32(name); if (num != (double)inum) { // Force an array index exception. inum = (uint)(array.Length); } array.SetValue(value, (int)inum); } }
public static double JScriptCompare(object v1, object v2) { object p1 = Convert.ToPrimitive(v1, null); object p2 = Convert.ToPrimitive(v2, null); if (Convert.IsString(p1) && Convert.IsString(p2)) { string s1 = Convert.ToString(p1); string s2 = Convert.ToString(p2); return(s1.CompareTo(s2)); } else { double n1 = Convert.ToNumber(p1); double n2 = Convert.ToNumber(p2); return(n1 - n2); } }