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

Diff of /trunk/js/jsnum.cpp

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

revision 459 by siliconforks, Tue Dec 9 03:37:47 2008 UTC revision 460 by siliconforks, Sat Sep 26 23:15:22 2009 UTC
# Line 45  Line 45 
45  #if defined(XP_WIN) || defined(XP_OS2)  #if defined(XP_WIN) || defined(XP_OS2)
46  #include <float.h>  #include <float.h>
47  #endif  #endif
48    #ifdef XP_OS2
49    #define _PC_53  PC_53
50    #define _MCW_EM MCW_EM
51    #define _MCW_PC MCW_PC
52    #endif
53  #include <locale.h>  #include <locale.h>
54  #include <limits.h>  #include <limits.h>
55  #include <math.h>  #include <math.h>
# Line 261  Line 266 
266      } else {      } else {
267          v = JSVAL_ZERO;          v = JSVAL_ZERO;
268      }      }
269      if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {      if (!JS_IsConstructing(cx)) {
270          *rval = v;          *rval = v;
271          return JS_TRUE;          return JS_TRUE;
272      }      }
# Line 298  Line 303 
303  #endif  #endif
304    
305  /* The buf must be big enough for MIN_INT to fit including '-' and '\0'. */  /* The buf must be big enough for MIN_INT to fit including '-' and '\0'. */
306  char *  static char *
307  js_IntToCString(jsint i, jsint base, char *buf, size_t bufSize)  IntToCString(jsint i, jsint base, char *buf, size_t bufSize)
308  {  {
309      char *cp;      char *cp;
310      jsuint u;      jsuint u;
# Line 363  Line 368 
368              return JS_FALSE;              return JS_FALSE;
369          if (base < 2 || base > 36) {          if (base < 2 || base > 36) {
370              char numBuf[12];              char numBuf[12];
371              char *numStr = js_IntToCString(base, 10, numBuf, sizeof numBuf);              char *numStr = IntToCString(base, 10, numBuf, sizeof numBuf);
372              JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_RADIX,              JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_RADIX,
373                                   numStr);                                   numStr);
374              return JS_FALSE;              return JS_FALSE;
# Line 787  Line 792 
792      return js_NewDoubleInRootedValue(cx, d, vp);      return js_NewDoubleInRootedValue(cx, d, vp);
793  }  }
794    
795  char *  JSBool
796  js_NumberToCString(JSContext *cx, jsdouble d, jsint base, char *buf, size_t bufSize)  js_NewWeaklyRootedNumber(JSContext *cx, jsdouble d, jsval *rval)
797    {
798        jsint i;
799        if (JSDOUBLE_IS_INT(d, i) && INT_FITS_IN_JSVAL(i)) {
800            *rval = INT_TO_JSVAL(i);
801            return JS_TRUE;
802        }
803        return JS_NewDoubleValue(cx, d, rval);
804    }
805    
806    /*
807     * Convert a number to C string. The buf must be large enough to accommodate
808     * the result, including '-' and '\0', if base == 10 or d is an integer that
809     * fits in 32 bits. The caller must free the resulting pointer if it does not
810     * point into buf.
811     */
812    static char *
813    NumberToCString(JSContext *cx, jsdouble d, jsint base, char *buf, size_t bufSize)
814  {  {
815      jsint i;      jsint i;
816      char *numStr;      char *numStr;
817    
818      JS_ASSERT(bufSize >= DTOSTR_STANDARD_BUFFER_SIZE);      JS_ASSERT(bufSize >= DTOSTR_STANDARD_BUFFER_SIZE);
819      if (JSDOUBLE_IS_INT(d, i)) {      if (JSDOUBLE_IS_INT(d, i)) {
820          numStr = js_IntToCString(i, base, buf, bufSize);          numStr = IntToCString(i, base, buf, bufSize);
821      } else {      } else {
822          if (base == 10)          if (base == 10)
823              numStr = JS_dtostr(buf, bufSize, DTOSTR_STANDARD, 0, d);              numStr = JS_dtostr(buf, bufSize, DTOSTR_STANDARD, 0, d);
# Line 812  Line 834 
834  static JSString * JS_FASTCALL  static JSString * JS_FASTCALL
835  NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base)  NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base)
836  {  {
837      char buf[DTOSTR_STANDARD_BUFFER_SIZE];      /*
838         * The longest possible result here that would need to fit in buf is
839         * (-0x80000000).toString(2), which has length 33.  (This can produce
840         * longer results, but in those cases buf is not used; see comment at
841         * NumberToCString.)
842         */
843        char buf[34];
844      char *numStr;      char *numStr;
845        JSString *s;
846    
847      if (base < 2 || base > 36)      if (base < 2 || base > 36)
848          return NULL;          return NULL;
849      numStr = js_NumberToCString(cx, d, base, buf, sizeof buf);      numStr = NumberToCString(cx, d, base, buf, sizeof buf);
850      if (!numStr)      if (!numStr)
851          return NULL;          return NULL;
852      return JS_NewStringCopyZ(cx, numStr);      s = JS_NewStringCopyZ(cx, numStr);
853        if (!(numStr >= buf && numStr < buf + sizeof buf))
854            free(numStr);
855        return s;
856  }  }
857    
858  JSString * JS_FASTCALL  JSString * JS_FASTCALL

Legend:
Removed from v.459  
changed lines
  Added in v.460

  ViewVC Help
Powered by ViewVC 1.1.24