/[jscoverage]/trunk/js/jsdate.cpp
ViewVC logotype

Diff of /trunk/js/jsdate.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 332 by siliconforks, Thu Oct 23 19:03:33 2008 UTC revision 399 by siliconforks, Tue Dec 9 03:37:47 2008 UTC
# Line 63  Line 63 
63  #include "jsutil.h" /* Added by JSIFY */  #include "jsutil.h" /* Added by JSIFY */
64  #include "jsapi.h"  #include "jsapi.h"
65  #include "jsversion.h"  #include "jsversion.h"
66    #include "jsbuiltins.h"
67  #include "jscntxt.h"  #include "jscntxt.h"
68  #include "jsdate.h"  #include "jsdate.h"
69  #include "jsinterp.h"  #include "jsinterp.h"
# Line 912  Line 913 
913      return js_NewNumberInRootedValue(cx, result, vp);      return js_NewNumberInRootedValue(cx, result, vp);
914  }  }
915    
916  JSBool  static JSBool
917  js_date_now(JSContext *cx, uintN argc, jsval *vp)  date_now(JSContext *cx, uintN argc, jsval *vp)
918  {  {
919      return js_NewDoubleInRootedValue(cx, PRMJ_Now() / PRMJ_USEC_PER_MSEC, vp);      return js_NewDoubleInRootedValue(cx, PRMJ_Now() / PRMJ_USEC_PER_MSEC, vp);
920  }  }
921    
922    #ifdef JS_TRACER
923    static jsdouble FASTCALL
924    date_now_tn(JSContext*)
925    {
926        return PRMJ_Now() / PRMJ_USEC_PER_MSEC;
927    }
928    #endif
929    
930  /*  /*
931   * Get UTC time from the date object. Returns false if the object is not   * Get UTC time from the date object. Returns false if the object is not
932   * Date type.   * Date type.
# Line 1579  Line 1588 
1588     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1589  };  };
1590    
1591    
1592    // Avoid dependence on PRMJ_FormatTimeUSEnglish, because it
1593    // requires a PRMJTime... which only has 16-bit years.  Sub-ECMA.
1594    static void
1595    print_gmt_string(char* buf, size_t size, jsdouble utctime)
1596    {
1597        JS_snprintf(buf, size, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
1598                    days[WeekDay(utctime)],
1599                    DateFromTime(utctime),
1600                    months[MonthFromTime(utctime)],
1601                    YearFromTime(utctime),
1602                    HourFromTime(utctime),
1603                    MinFromTime(utctime),
1604                    SecFromTime(utctime));
1605    }
1606    
1607    static void
1608    print_iso_string(char* buf, size_t size, jsdouble utctime)
1609    {
1610        JS_snprintf(buf, size, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
1611                    YearFromTime(utctime),
1612                    MonthFromTime(utctime) + 1,
1613                    DateFromTime(utctime),
1614                    HourFromTime(utctime),
1615                    MinFromTime(utctime),
1616                    SecFromTime(utctime),
1617                    msFromTime(utctime));
1618    }
1619    
1620  static JSBool  static JSBool
1621  date_toGMTString(JSContext *cx, uintN argc, jsval *vp)  date_utc_format(JSContext *cx, jsval *vp,
1622                    void (*printFunc)(char*, size_t, jsdouble))
1623  {  {
1624      char buf[100];      char buf[100];
1625      JSString *str;      JSString *str;
# Line 1592  Line 1631 
1631      if (!JSDOUBLE_IS_FINITE(utctime)) {      if (!JSDOUBLE_IS_FINITE(utctime)) {
1632          JS_snprintf(buf, sizeof buf, js_NaN_date_str);          JS_snprintf(buf, sizeof buf, js_NaN_date_str);
1633      } else {      } else {
1634          /* Avoid dependence on PRMJ_FormatTimeUSEnglish, because it          (*printFunc)(buf, sizeof buf, utctime);
          * requires a PRMJTime... which only has 16-bit years.  Sub-ECMA.  
          */  
         JS_snprintf(buf, sizeof buf, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",  
                     days[WeekDay(utctime)],  
                     DateFromTime(utctime),  
                     months[MonthFromTime(utctime)],  
                     YearFromTime(utctime),  
                     HourFromTime(utctime),  
                     MinFromTime(utctime),  
                     SecFromTime(utctime));  
1635      }      }
1636      str = JS_NewStringCopyZ(cx, buf);      str = JS_NewStringCopyZ(cx, buf);
1637      if (!str)      if (!str)
# Line 1611  Line 1640 
1640      return JS_TRUE;      return JS_TRUE;
1641  }  }
1642    
1643    static JSBool
1644    date_toGMTString(JSContext *cx, uintN argc, jsval *vp)
1645    {
1646        return date_utc_format(cx, vp, print_gmt_string);
1647    }
1648    
1649    static JSBool
1650    date_toISOString(JSContext *cx, uintN argc, jsval *vp)
1651    {
1652        return date_utc_format(cx, vp, print_iso_string);
1653    }
1654    
1655  /* for Date.toLocaleString; interface to PRMJTime date struct.  /* for Date.toLocaleString; interface to PRMJTime date struct.
1656   */   */
1657  static void  static void
# Line 1927  Line 1968 
1968      return date_format(cx, utctime, FORMATSPEC_FULL, vp);      return date_format(cx, utctime, FORMATSPEC_FULL, vp);
1969  }  }
1970    
1971    #ifdef JS_TRACER
1972    static jsval FASTCALL
1973    date_valueOf_tn(JSContext* cx, JSObject* obj, JSString* str)
1974    {
1975        JS_ASSERT(JS_InstanceOf(cx, obj, &js_DateClass, NULL));
1976        jsdouble t = *JSVAL_TO_DOUBLE(obj->fslots[JSSLOT_UTC_TIME]);
1977    
1978        JSString* number_str = ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_NUMBER]);
1979        jsval v;
1980        if (js_EqualStrings(str, number_str)) {
1981            if (!js_NewNumberInRootedValue(cx, t, &v))
1982                return JSVAL_ERROR_COOKIE;
1983        } else {
1984            if (!date_format(cx, t, FORMATSPEC_FULL, &v))
1985                return JSVAL_ERROR_COOKIE;
1986        }
1987        return v;
1988    }
1989    #endif
1990    
1991  static JSBool  static JSBool
1992  date_valueOf(JSContext *cx, uintN argc, jsval *vp)  date_valueOf(JSContext *cx, uintN argc, jsval *vp)
1993  {  {
1994      JSString *str, *str2;      JSString *str, *number_str;
1995    
1996      /* It is an error to call date_valueOf on a non-date object, but we don't      /* It is an error to call date_valueOf on a non-date object, but we don't
1997       * need to check for that explicitly here because every path calls       * need to check for that explicitly here because every path calls
# Line 1945  Line 2006 
2006      str = js_ValueToString(cx, vp[2]);      str = js_ValueToString(cx, vp[2]);
2007      if (!str)      if (!str)
2008          return JS_FALSE;          return JS_FALSE;
2009      str2 = ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_NUMBER]);      number_str = ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[JSTYPE_NUMBER]);
2010      if (js_EqualStrings(str, str2))      if (js_EqualStrings(str, number_str))
2011          return date_getTime(cx, argc, vp);          return date_getTime(cx, argc, vp);
2012      return date_toString(cx, argc, vp);      return date_toString(cx, argc, vp);
2013  }  }
2014    
2015  /*  JS_DEFINE_CALLINFO_2(extern, OBJECT, js_FastNewDate, CONTEXT, OBJECT, 0, 0)
2016   * creation and destruction  
2017   */  // Don't really need an argument here, but we don't support arg-less builtins
2018    JS_DEFINE_TRCINFO_1(date_now,
2019        (1, (static, DOUBLE, date_now_tn, CONTEXT, 0, 0)))
2020    
2021  static JSFunctionSpec date_static_methods[] = {  static JSFunctionSpec date_static_methods[] = {
2022      JS_FN("UTC",                 date_UTC,                MAXARGS,0),      JS_FN("UTC",                 date_UTC,                MAXARGS,0),
2023      JS_FN("parse",               date_parse,              1,0),      JS_FN("parse",               date_parse,              1,0),
2024      JS_FN("now",                 js_date_now,             0,0),      JS_TN("now",                 date_now,                0,0, date_now_trcinfo),
2025      JS_FS_END      JS_FS_END
2026  };  };
2027    
2028    JS_DEFINE_TRCINFO_1(date_valueOf,
2029        (3, (static, JSVAL_FAIL, date_valueOf_tn, CONTEXT, THIS, STRING, 0, 0)))
2030    
2031  static JSFunctionSpec date_methods[] = {  static JSFunctionSpec date_methods[] = {
2032      JS_FN("getTime",             date_getTime,            0,0),      JS_FN("getTime",             date_getTime,            0,0),
2033      JS_FN("getTimezoneOffset",   date_getTimezoneOffset,  0,0),      JS_FN("getTimezoneOffset",   date_getTimezoneOffset,  0,0),
# Line 2005  Line 2071 
2071      JS_FN("toLocaleFormat",      date_toLocaleFormat,     0,0),      JS_FN("toLocaleFormat",      date_toLocaleFormat,     0,0),
2072      JS_FN("toDateString",        date_toDateString,       0,0),      JS_FN("toDateString",        date_toDateString,       0,0),
2073      JS_FN("toTimeString",        date_toTimeString,       0,0),      JS_FN("toTimeString",        date_toTimeString,       0,0),
2074        JS_FN("toISOString",         date_toISOString,        0,0),
2075        JS_FN(js_toJSON_str,         date_toISOString,        0,0),
2076    
2077  #if JS_HAS_TOSOURCE  #if JS_HAS_TOSOURCE
2078      JS_FN(js_toSource_str,       date_toSource,           0,0),      JS_FN(js_toSource_str,       date_toSource,           0,0),
2079  #endif  #endif
2080      JS_FN(js_toString_str,       date_toString,           0,0),      JS_FN(js_toString_str,       date_toString,           0,0),
2081      JS_FN(js_valueOf_str,        date_valueOf,            0,0),      JS_TN(js_valueOf_str,        date_valueOf,            0,0, date_valueOf_trcinfo),
2082      JS_FS_END      JS_FS_END
2083  };  };
2084    
# Line 2027  Line 2096 
2096      return date;      return date;
2097  }  }
2098    
2099  static JSBool  JSBool
2100  Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)  js_Date(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
2101  {  {
2102      jsdouble *date;      jsdouble *date;
2103      JSString *str;      JSString *str;
# Line 2091  Line 2160 
2160      return JS_TRUE;      return JS_TRUE;
2161  }  }
2162    
2163    JS_STATIC_ASSERT(JSSLOT_PRIVATE == JSSLOT_UTC_TIME);
2164    JS_STATIC_ASSERT(JSSLOT_UTC_TIME + 1 == JSSLOT_LOCAL_TIME);
2165    
2166    #ifdef JS_TRACER
2167    JSObject* FASTCALL
2168    js_FastNewDate(JSContext* cx, JSObject* proto)
2169    {
2170        JS_ASSERT(JS_ON_TRACE(cx));
2171        JSObject* obj = (JSObject*) js_NewGCThing(cx, GCX_OBJECT, sizeof(JSObject));
2172        if (!obj)
2173            return NULL;
2174    
2175        JSClass* clasp = &js_DateClass;
2176        obj->classword = jsuword(clasp);
2177    
2178        obj->fslots[JSSLOT_PROTO] = OBJECT_TO_JSVAL(proto);
2179        obj->fslots[JSSLOT_PARENT] = proto->fslots[JSSLOT_PARENT];
2180    
2181        jsdouble* date = js_NewWeaklyRootedDouble(cx, 0.0);
2182        if (!date)
2183            return NULL;
2184        *date = date_now_tn(cx);
2185        obj->fslots[JSSLOT_UTC_TIME] = DOUBLE_TO_JSVAL(date);
2186        obj->fslots[JSSLOT_LOCAL_TIME] = DOUBLE_TO_JSVAL(cx->runtime->jsNaN);
2187        for (unsigned i = JSSLOT_LOCAL_TIME + 1; i != JS_INITIAL_NSLOTS; ++i)
2188            obj->fslots[i] = JSVAL_VOID;
2189        
2190        JS_ASSERT(!clasp->getObjectOps);
2191        JS_ASSERT(proto->map->ops == &js_ObjectOps);
2192        obj->map = js_HoldObjectMap(cx, proto->map);
2193        obj->dslots = NULL;
2194        return obj;    
2195    }
2196    #endif
2197    
2198  JSObject *  JSObject *
2199  js_InitDateClass(JSContext *cx, JSObject *obj)  js_InitDateClass(JSContext *cx, JSObject *obj)
2200  {  {
# Line 2099  Line 2203 
2203    
2204      /* set static LocalTZA */      /* set static LocalTZA */
2205      LocalTZA = -(PRMJ_LocalGMTDifference() * msPerSecond);      LocalTZA = -(PRMJ_LocalGMTDifference() * msPerSecond);
2206      proto = JS_InitClass(cx, obj, NULL, &js_DateClass, Date, MAXARGS,      proto = JS_InitClass(cx, obj, NULL, &js_DateClass, js_Date, MAXARGS,
2207                           NULL, date_methods, NULL, date_static_methods);                           NULL, date_methods, NULL, date_static_methods);
2208      if (!proto)      if (!proto)
2209          return NULL;          return NULL;

Legend:
Removed from v.332  
changed lines
  Added in v.399

  ViewVC Help
Powered by ViewVC 1.1.24