public static bool JSEqualsExact(JSValue a, JSValue b) { JSType t1 = a.ObjectType; JSType t2 = b.ObjectType; if (t1 != t2) { return(false); } switch (t1) { case JSType.Number: return(a.NumberValue() == b.NumberValue()); case JSType.String: return(a.StringValue() == b.StringValue()); case JSType.Boolean: return(a.BoolValue() == b.BoolValue()); case JSType.Null: return(true); case JSType.Undefined: return(true); } return(a == b); }
private bool GreaterThanOrNull(JSValue b) { if ((this.DataType == JSType.String) && (b.DataType == JSType.String)) { return(string.Compare(this.StringValue(), b.StringValue(), StringComparison.Ordinal) > 0); } return(!(this.NumberValue() <= b.NumberValue())); }
public JSDate(JSValue Prototype, JSValue Constructor, JSArgs args) : base(Prototype, Constructor) { if (args.Count == 0) { this.CLRDate = DateTime.Now; } else if (args.Count == 1) { JSValue v = args[0].ToPrimitive(); if (v is JSString) { this.CLRDate = DateTime.Parse(v.StringValue()); } else { this.CLRDate = FromNumber(TimeClip(v.NumberValue())); } } else { this.CLRDate = UTC(args); } }
public static bool JSEquals(JSValue a, JSValue b) { JSType t1 = a.ObjectType; JSType t2 = b.ObjectType; if (t1 != t2) { switch (t2) { case JSType.Boolean: return(JSEquals(a, b.ToJSNumber())); case JSType.Object: return(JSEquals(a, b.ToPrimitive())); } switch (t1) { case JSType.Number: if (t2 != JSType.String) { goto Label_016F; } return(a.NumberValue() == b.NumberValue()); case JSType.String: if (t2 != JSType.Number) { goto Label_016F; } return(a.NumberValue() == b.NumberValue()); case JSType.Boolean: return(JSEquals(a.ToJSNumber(), b)); case JSType.Object: return(JSEquals(a.ToPrimitive(), b)); case JSType.Array: case JSType.Function: goto Label_016F; case JSType.Null: return(t2 == JSType.Undefined); case JSType.Undefined: return(t2 == JSType.Null); } } else { switch (t1) { case JSType.Number: return(a.NumberValue() == b.NumberValue()); case JSType.String: return(a.StringValue() == b.StringValue()); case JSType.Boolean: return(a.BoolValue() == b.BoolValue()); case JSType.Null: return(true); case JSType.Undefined: return(true); } return(a == b); } Label_016F: return(false); }
private void AddFunction() { this.FunctionCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { string argNames; string sBody; if (args.Count > 1) { string[] ar = new string[args.Count - 1]; for (int i = 0; i < (args.Count - 1); i++) { ar[i] = args[i].StringValue(); } argNames = string.Join(",", ar); } else { argNames = ""; } if (args.Count == 0) { sBody = ""; } else { sBody = args[args.Count - 1].StringValue(); } JSFunctionObject r = CompiledScript.Compile("(function (" + argNames + ") {" + sBody + "})", false).Run() as JSFunctionObject; r.Scope = JSContext.CurrentGlobalContext.LexicalEnv; return(r); }, 1); this.FunctionPrototype = new JSFunctionBase(); this.FunctionPrototype.SetDataProp("constructor", this.FunctionCtor, true, false, true); this.FunctionPrototype.SetDataProp("call", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { JSArgs newArgs; if (AsConstructor) { throw new JSRuntimeException("TypeError", "Function.call called as constructor"); } JSFunctionBase fnc = InternalUtilities.JSFunctionCast(ThisObj); if (args.Count == 0) { newArgs = args; } else { JSValue[] newAr = new JSValue[args.Count - 1]; Array.Copy(args.ArgValues, 1, newAr, 0, args.Count - 1); newArgs = new JSArgs(ThisObj, newAr); } return(fnc.Call(Scope, args[0], newArgs)); }, 1), true, false, true); this.FunctionPrototype.SetDataProp("apply", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (AsConstructor) { throw new JSRuntimeException("TypeError", "Function.apply called as constructor"); } JSFunctionBase fnc = InternalUtilities.JSFunctionCast(ThisObj); JSValue argArray = args[1]; if ((argArray is JSUndefined) || (argArray is JSNull)) { return(fnc.Call(Scope, args[0], new JSArgs(ThisObj, new JSValue[0]))); } if (!(argArray is JSObject)) { throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply"); } JSValue olen = argArray["length"]; if (!olen.CheckCoercible()) { throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply"); } double nlen = olen.NumberValue(); uint len = nlen.JSToUInt32(); if (len != nlen) { throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply"); } JSValue[] newAr = new JSValue[len]; for (int i = 0; i < len; i++) { newAr[i] = argArray[i.ToString()]; } return(fnc.Call(Scope, args[0], new JSArgs(ThisObj, newAr))); }, 2), true, false, true); this.FunctionPrototype.SetDataProp("toString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { return(InternalUtilities.JSFunctionCast(ThisObj).GenerateString()); }), true, false, true); this.FunctionPrototype.SetDataProp("length", 0.0, true, false, true); this.FunctionCtor.SetDataProp("prototype", this.FunctionPrototype, false, false, false); }
private void AddDateProperties() { this.Global.SetDataProp("Date", this.DateCtor, false, false, false); this.DateCtor.SetDataProp("parse", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { return(new JSDate(DateTime.Parse(args[0].StringValue()))); }), true, false, true); this.DateCtor.SetDataProp("UTC", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { return(new JSDate(JSDate.UTC(args))); }), true, false, true); this.DateCtor.SetDataProp("now", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { return(new JSDate(DateTime.UtcNow)); }), true, false, true); this.DatePrototype.SetDataProp("toString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("F")); }), true, false, true); this.DatePrototype.SetDataProp("toDateString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toDateString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("D")); }), true, false, true); this.DatePrototype.SetDataProp("toTimeString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toTimeString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("T")); }), true, false, true); this.DatePrototype.SetDataProp("toLocaleString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toLocaleString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("F") + " GMT" + v.ToString("zzz") + " (" + (TimeZone.CurrentTimeZone.IsDaylightSavingTime(v) ? TimeZone.CurrentTimeZone.DaylightName : TimeZone.CurrentTimeZone.StandardName) + ")"); }), true, false, true); this.DatePrototype.SetDataProp("toLocaleDateString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toLocaleDateString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("D")); }), true, false, true); this.DatePrototype.SetDataProp("toLocaleTimeString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toLocaleTimeString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToLocalTime().ToString("T") + " GMT" + v.ToString("zzz") + " (" + (TimeZone.CurrentTimeZone.IsDaylightSavingTime(v) ? TimeZone.CurrentTimeZone.DaylightName : TimeZone.CurrentTimeZone.StandardName) + ")"); }), true, false, true); this.DatePrototype.SetDataProp("valueOf", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.valueOf called on non-date type"); } return(((JSDate)ThisObj).ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("getTime", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getTime called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return(((double)(v.Ticks - 0x89f7ff5f7b58000L)) / 10000.0); }), true, false, true); this.DatePrototype.SetDataProp("getFullYear", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getFullYear called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Year); }), true, false, true); this.DatePrototype.SetDataProp("getUTCFullYear", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCFullYear called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Year); }), true, false, true); this.DatePrototype.SetDataProp("getMonth", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getMonth called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)(v.ToLocalTime().Month - 1)); }), true, false, true); this.DatePrototype.SetDataProp("getUTCMonth", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCMonth called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)(v.Month - 1)); }), true, false, true); this.DatePrototype.SetDataProp("getDate", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getDate called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Day); }), true, false, true); this.DatePrototype.SetDataProp("getUTCDate", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCDate called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Day); }), true, false, true); this.DatePrototype.SetDataProp("getDay", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getDay called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((double)v.ToLocalTime().DayOfWeek); }), true, false, true); this.DatePrototype.SetDataProp("getUTCDay", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCDay called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((double)v.DayOfWeek); }), true, false, true); this.DatePrototype.SetDataProp("getHours", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getHours called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Hour); }), true, false, true); this.DatePrototype.SetDataProp("getUTCHours", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCHours called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Hour); }), true, false, true); this.DatePrototype.SetDataProp("getMinutes", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getMinutes called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Minute); }), true, false, true); this.DatePrototype.SetDataProp("getUTCMinutes", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCMinutes called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Minute); }), true, false, true); this.DatePrototype.SetDataProp("getSeconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getSeconds called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Second); }), true, false, true); this.DatePrototype.SetDataProp("getUTCSeconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCSeconds called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Second); }), true, false, true); this.DatePrototype.SetDataProp("getMilliseconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getMilliseconds called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.ToLocalTime().Millisecond); }), true, false, true); this.DatePrototype.SetDataProp("getUTCMilliseconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getUTCMilliseconds called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)v.Millisecond); }), true, false, true); this.DatePrototype.SetDataProp("getTimezoneOffset", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.getTimezoneOffset called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return(((v - v.ToLocalTime())).TotalMinutes); }), true, false, true); this.DatePrototype.SetDataProp("setTime", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setTime called on non-date type"); } JSDate d = (JSDate)ThisObj; double v = JSDate.TimeClip(args[0].NumberValue()); d.CLRDate = JSDate.FromNumber(v); return(v); }), true, false, true); this.DatePrototype.SetDataProp("setMilliseconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMilliseconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMilliseconds(ms - d.CLRDate.Millisecond); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCMilliseconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setUTCMilliseconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMilliseconds(ms - d.CLRDate.Millisecond); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setSeconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setSeconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromSeconds(ms - d.CLRDate.Second); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCSeconds", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setUTCSeconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromSeconds(ms - d.CLRDate.Second); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setMinutes", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMinutes called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMinutes(ms - d.CLRDate.Minute); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCMinutes", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setUTCMinutes called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMinutes(ms - d.CLRDate.Minute); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setHours", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setHours called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromHours(ms - d.CLRDate.Hour); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCHours", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setUTCHours called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromHours(ms - d.CLRDate.Hour); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setDate", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setDate called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromDays(ms - d.CLRDate.Day); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCDate", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setDate called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromDays(ms - d.CLRDate.Day); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setMonth", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMonths called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); DateTime x = new DateTime(d.CLRDate.Year, (int)ms, 0); x += TimeSpan.FromDays((double)d.CLRDate.Day); x += d.CLRDate.TimeOfDay; d.CLRDate = x; return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCMonth", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMilliseconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMilliseconds(ms - d.CLRDate.Millisecond); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setFullYear", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMilliseconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMilliseconds(ms - d.CLRDate.Millisecond); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("setUTCFullYear", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.setMilliseconds called on non-date type"); } JSDate d = (JSDate)ThisObj; double ms = args[0].NumberValue().JSToInteger(); d.CLRDate += TimeSpan.FromMilliseconds(ms - d.CLRDate.Millisecond); return(d.ToDouble()); }), true, false, true); this.DatePrototype.SetDataProp("toUTCString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toUTCString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToString("F") + " GMT"); }), true, false, true); this.DatePrototype.SetDataProp("toISOString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toISOString called on non-date type"); } DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return("Invalid Date"); } return(v.ToString("yyyy-MM-ddTHH:mm:ss.FFFZ")); }), true, false, true); this.DatePrototype.SetDataProp("toJSON", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!(ThisObj is JSDate)) { throw new JSRuntimeException("TypeError", "Date.toJSON called on non-date type"); } JSObject O = ThisObj.ToJSObject(); JSValue p = O.ToPrimitive(false); if (!(!(p is JSNumber) || p.NumberValue().IsFinite())) { return(JSNull.Instance); } JSFunctionBase toISO = O["toISOString"] as JSFunctionBase; if (toISO == null) { throw new JSRuntimeException("TypeError", "Date.toJSON called on object without toISOString"); } return(toISO.Call(Scope, O, new JSArgs(toISO, new JSValue[0]))); }), true, false, true); this.DatePrototype.SetDataProp("getYear", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { DateTime v = ((JSDate)ThisObj).CLRDate; if (v == DateTime.MinValue) { return(JSNumber.NaN); } return((JSValue)(v.ToLocalTime().Year - 0x76c)); }), true, false, true); }
private bool GreaterThanOrNull(JSValue b) { if ((this.DataType == JSType.String) && (b.DataType == JSType.String)) { return (string.Compare(this.StringValue(), b.StringValue(), StringComparison.Ordinal) > 0); } return !(this.NumberValue() <= b.NumberValue()); }
public static bool JSEqualsExact(JSValue a, JSValue b) { JSType t1 = a.ObjectType; JSType t2 = b.ObjectType; if (t1 != t2) { return false; } switch (t1) { case JSType.Number: return (a.NumberValue() == b.NumberValue()); case JSType.String: return (a.StringValue() == b.StringValue()); case JSType.Boolean: return (a.BoolValue() == b.BoolValue()); case JSType.Null: return true; case JSType.Undefined: return true; } return (a == b); }
public static bool JSEquals(JSValue a, JSValue b) { JSType t1 = a.ObjectType; JSType t2 = b.ObjectType; if (t1 != t2) { switch (t2) { case JSType.Boolean: return JSEquals(a, b.ToJSNumber()); case JSType.Object: return JSEquals(a, b.ToPrimitive()); } switch (t1) { case JSType.Number: if (t2 != JSType.String) { goto Label_016F; } return (a.NumberValue() == b.NumberValue()); case JSType.String: if (t2 != JSType.Number) { goto Label_016F; } return (a.NumberValue() == b.NumberValue()); case JSType.Boolean: return JSEquals(a.ToJSNumber(), b); case JSType.Object: return JSEquals(a.ToPrimitive(), b); case JSType.Array: case JSType.Function: goto Label_016F; case JSType.Null: return (t2 == JSType.Undefined); case JSType.Undefined: return (t2 == JSType.Null); } } else { switch (t1) { case JSType.Number: return (a.NumberValue() == b.NumberValue()); case JSType.String: return (a.StringValue() == b.StringValue()); case JSType.Boolean: return (a.BoolValue() == b.BoolValue()); case JSType.Null: return true; case JSType.Undefined: return true; } return (a == b); } Label_016F: return false; }