public Object execIdCall()

in rhino/src/main/java/org/mozilla/javascript/NativeDate.java [269:525]


    public Object execIdCall(
            IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
        if (!f.hasTag(DATE_TAG)) {
            return super.execIdCall(f, cx, scope, thisObj, args);
        }
        int id = f.methodId();
        switch (id) {
            case ConstructorId_now:
                return ScriptRuntime.wrapNumber(now());

            case ConstructorId_parse:
                {
                    String dataStr = ScriptRuntime.toString(args, 0);
                    return ScriptRuntime.wrapNumber(date_parseString(cx, dataStr));
                }

            case ConstructorId_UTC:
                return ScriptRuntime.wrapNumber(jsStaticFunction_UTC(args));

            case Id_constructor:
                {
                    // if called as a function, just return a string
                    // representing the current time.
                    if (thisObj != null) return date_format(cx, now(), Id_toString);
                    return jsConstructor(cx, args);
                }

            case Id_toJSON:
                {
                    final String toISOString = "toISOString";

                    Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
                    Object tv = ScriptRuntime.toPrimitive(o, ScriptRuntime.NumberClass);
                    if (tv instanceof Number) {
                        double d = ((Number) tv).doubleValue();
                        if (Double.isNaN(d) || Double.isInfinite(d)) {
                            return null;
                        }
                    }
                    Object toISO = ScriptableObject.getProperty(o, toISOString);
                    if (toISO == NOT_FOUND) {
                        throw ScriptRuntime.typeErrorById(
                                "msg.function.not.found.in",
                                toISOString,
                                ScriptRuntime.toString(o));
                    }
                    if (!(toISO instanceof Callable)) {
                        throw ScriptRuntime.typeErrorById(
                                "msg.isnt.function.in",
                                toISOString,
                                ScriptRuntime.toString(o),
                                ScriptRuntime.toString(toISO));
                    }
                    Object result = ((Callable) toISO).call(cx, scope, o, ScriptRuntime.emptyArgs);
                    if (!ScriptRuntime.isPrimitive(result)) {
                        throw ScriptRuntime.typeErrorById(
                                "msg.toisostring.must.return.primitive",
                                ScriptRuntime.toString(result));
                    }
                    return result;
                }
            case SymbolId_toPrimitive:
                {
                    Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
                    final Object arg0 = args.length > 0 ? args[0] : Undefined.instance;
                    final String hint = (arg0 instanceof CharSequence) ? arg0.toString() : null;
                    Class<?> typeHint = null;
                    if ("string".equals(hint) || "default".equals(hint)) {
                        typeHint = ScriptRuntime.StringClass;
                    } else if ("number".equals(hint)) {
                        typeHint = ScriptRuntime.NumberClass;
                    }
                    if (typeHint == null) {
                        throw ScriptRuntime.typeErrorById(
                                "msg.invalid.toprimitive.hint", ScriptRuntime.toString(arg0));
                    }

                    return ScriptableObject.getDefaultValue(o, typeHint);
                }
        }

        // The rest of Date.prototype methods require thisObj to be Date
        NativeDate realThis = ensureType(thisObj, NativeDate.class, f);
        double t = realThis.date;

        switch (id) {
            case Id_toString:
            case Id_toTimeString:
            case Id_toDateString:
                if (!Double.isNaN(t)) {
                    return date_format(cx, t, id);
                }
                return js_NaN_date_str;

            case Id_toLocaleString:
            case Id_toLocaleTimeString:
            case Id_toLocaleDateString:
                if (!Double.isNaN(t)) {
                    return toLocale_helper(cx, t, id, args);
                }
                return js_NaN_date_str;

            case Id_toUTCString:
                if (!Double.isNaN(t)) {
                    return js_toUTCString(t);
                }
                return js_NaN_date_str;

            case Id_toSource:
                return "(new Date(" + ScriptRuntime.toString(t) + "))";

            case Id_valueOf:
            case Id_getTime:
                return ScriptRuntime.wrapNumber(t);

            case Id_getYear:
            case Id_getFullYear:
            case Id_getUTCFullYear:
                if (!Double.isNaN(t)) {
                    if (id != Id_getUTCFullYear) t = LocalTime(cx, t);
                    t = YearFromTime(t);
                    if (id == Id_getYear) {
                        if (cx.hasFeature(Context.FEATURE_NON_ECMA_GET_YEAR)) {
                            if (1900 <= t && t < 2000) {
                                t -= 1900;
                            }
                        } else {
                            t -= 1900;
                        }
                    }
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getMonth:
            case Id_getUTCMonth:
                if (!Double.isNaN(t)) {
                    if (id == Id_getMonth) t = LocalTime(cx, t);
                    t = MonthFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getDate:
            case Id_getUTCDate:
                if (!Double.isNaN(t)) {
                    if (id == Id_getDate) t = LocalTime(cx, t);
                    t = DateFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getDay:
            case Id_getUTCDay:
                if (!Double.isNaN(t)) {
                    if (id == Id_getDay) t = LocalTime(cx, t);
                    t = WeekDay(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getHours:
            case Id_getUTCHours:
                if (!Double.isNaN(t)) {
                    if (id == Id_getHours) t = LocalTime(cx, t);
                    t = HourFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getMinutes:
            case Id_getUTCMinutes:
                if (!Double.isNaN(t)) {
                    if (id == Id_getMinutes) t = LocalTime(cx, t);
                    t = MinFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getSeconds:
            case Id_getUTCSeconds:
                if (!Double.isNaN(t)) {
                    if (id == Id_getSeconds) t = LocalTime(cx, t);
                    t = SecFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getMilliseconds:
            case Id_getUTCMilliseconds:
                if (!Double.isNaN(t)) {
                    if (id == Id_getMilliseconds) t = LocalTime(cx, t);
                    t = msFromTime(t);
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_getTimezoneOffset:
                if (!Double.isNaN(t)) {
                    t = (t - LocalTime(cx, t)) / msPerMinute;
                }
                return ScriptRuntime.wrapNumber(t);

            case Id_setTime:
                t = TimeClip(ScriptRuntime.toNumber(args, 0));
                realThis.date = t;
                return ScriptRuntime.wrapNumber(t);

            case Id_setMilliseconds:
            case Id_setUTCMilliseconds:
            case Id_setSeconds:
            case Id_setUTCSeconds:
            case Id_setMinutes:
            case Id_setUTCMinutes:
            case Id_setHours:
            case Id_setUTCHours:
                t = makeTime(cx, t, args, id);
                realThis.date = t;
                return ScriptRuntime.wrapNumber(t);

            case Id_setDate:
            case Id_setUTCDate:
            case Id_setMonth:
            case Id_setUTCMonth:
            case Id_setFullYear:
            case Id_setUTCFullYear:
                t = makeDate(cx, t, args, id);
                realThis.date = t;
                return ScriptRuntime.wrapNumber(t);

            case Id_setYear:
                {
                    double year = ScriptRuntime.toNumber(args, 0);

                    if (Double.isNaN(year) || Double.isInfinite(year)) {
                        t = ScriptRuntime.NaN;
                    } else {
                        if (Double.isNaN(t)) {
                            t = 0;
                        } else {
                            t = LocalTime(cx, t);
                        }

                        if (year >= 0 && year <= 99) year += 1900;

                        double day = MakeDay(year, MonthFromTime(t), DateFromTime(t));
                        t = MakeDate(day, TimeWithinDay(t));
                        t = internalUTC(cx, t);
                        t = TimeClip(t);
                    }
                }
                realThis.date = t;
                return ScriptRuntime.wrapNumber(t);

            case Id_toISOString:
                if (!Double.isNaN(t)) {
                    return js_toISOString(t);
                }
                String msg = ScriptRuntime.getMessageById("msg.invalid.date");
                throw ScriptRuntime.rangeError(msg);

            default:
                throw new IllegalArgumentException(String.valueOf(id));
        }
    }