[JAEGER] Merge from tracemonkey.
[mozilla-central.git] / js / src / jsnum.cpp
blobde8fae78f49f382f39754dabe3acb07d063769f9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * IBM Corp.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * JS number type and wrapper class.
44 #ifdef XP_OS2
45 #define _PC_53 PC_53
46 #define _MCW_EM MCW_EM
47 #define _MCW_PC MCW_PC
48 #endif
49 #include <locale.h>
50 #include <limits.h>
51 #include <math.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include "jstypes.h"
55 #include "jsstdint.h"
56 #include "jsutil.h" /* Added by JSIFY */
57 #include "jsapi.h"
58 #include "jsatom.h"
59 #include "jsbuiltins.h"
60 #include "jscntxt.h"
61 #include "jsversion.h"
62 #include "jsdtoa.h"
63 #include "jsgc.h"
64 #include "jsinterp.h"
65 #include "jsnum.h"
66 #include "jsobj.h"
67 #include "jsopcode.h"
68 #include "jsprf.h"
69 #include "jsscope.h"
70 #include "jsstr.h"
71 #include "jsvector.h"
73 #include "jsobjinlines.h"
74 #include "jsstrinlines.h"
76 using namespace js;
78 #ifndef JS_HAVE_STDINT_H /* Native support is innocent until proven guilty. */
80 JS_STATIC_ASSERT(uint8_t(-1) == UINT8_MAX);
81 JS_STATIC_ASSERT(uint16_t(-1) == UINT16_MAX);
82 JS_STATIC_ASSERT(uint32_t(-1) == UINT32_MAX);
83 JS_STATIC_ASSERT(uint64_t(-1) == UINT64_MAX);
85 JS_STATIC_ASSERT(INT8_MAX > INT8_MIN);
86 JS_STATIC_ASSERT(uint8_t(INT8_MAX) + uint8_t(1) == uint8_t(INT8_MIN));
87 JS_STATIC_ASSERT(INT16_MAX > INT16_MIN);
88 JS_STATIC_ASSERT(uint16_t(INT16_MAX) + uint16_t(1) == uint16_t(INT16_MIN));
89 JS_STATIC_ASSERT(INT32_MAX > INT32_MIN);
90 JS_STATIC_ASSERT(uint32_t(INT32_MAX) + uint32_t(1) == uint32_t(INT32_MIN));
91 JS_STATIC_ASSERT(INT64_MAX > INT64_MIN);
92 JS_STATIC_ASSERT(uint64_t(INT64_MAX) + uint64_t(1) == uint64_t(INT64_MIN));
94 JS_STATIC_ASSERT(INTPTR_MAX > INTPTR_MIN);
95 JS_STATIC_ASSERT(uintptr_t(INTPTR_MAX) + uintptr_t(1) == uintptr_t(INTPTR_MIN));
96 JS_STATIC_ASSERT(uintptr_t(-1) == UINTPTR_MAX);
97 JS_STATIC_ASSERT(size_t(-1) == SIZE_MAX);
98 JS_STATIC_ASSERT(PTRDIFF_MAX > PTRDIFF_MIN);
99 JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MAX) == PTRDIFF_MAX);
100 JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MIN) == PTRDIFF_MIN);
101 JS_STATIC_ASSERT(uintptr_t(PTRDIFF_MAX) + uintptr_t(1) == uintptr_t(PTRDIFF_MIN));
103 #endif /* JS_HAVE_STDINT_H */
105 static JSBool
106 num_isNaN(JSContext *cx, uintN argc, Value *vp)
108 if (argc == 0) {
109 vp->setBoolean(true);
110 return JS_TRUE;
112 jsdouble x;
113 if (!ValueToNumber(cx, vp[2], &x))
114 return false;
115 vp->setBoolean(JSDOUBLE_IS_NaN(x));
116 return JS_TRUE;
119 static JSBool
120 num_isFinite(JSContext *cx, uintN argc, Value *vp)
122 if (argc == 0) {
123 vp->setBoolean(false);
124 return JS_TRUE;
126 jsdouble x;
127 if (!ValueToNumber(cx, vp[2], &x))
128 return JS_FALSE;
129 vp->setBoolean(JSDOUBLE_IS_FINITE(x));
130 return JS_TRUE;
133 static JSBool
134 num_parseFloat(JSContext *cx, uintN argc, Value *vp)
136 JSString *str;
137 jsdouble d;
138 const jschar *bp, *end, *ep;
140 if (argc == 0) {
141 vp->setDouble(js_NaN);
142 return JS_TRUE;
144 str = js_ValueToString(cx, vp[2]);
145 if (!str)
146 return JS_FALSE;
147 str->getCharsAndEnd(bp, end);
148 if (!js_strtod(cx, bp, end, &ep, &d))
149 return JS_FALSE;
150 if (ep == bp) {
151 vp->setDouble(js_NaN);
152 return JS_TRUE;
154 vp->setNumber(d);
155 return JS_TRUE;
158 #ifdef JS_TRACER
159 static jsdouble FASTCALL
160 ParseFloat(JSContext* cx, JSString* str)
162 const jschar* bp;
163 const jschar* end;
164 const jschar* ep;
165 jsdouble d;
167 str->getCharsAndEnd(bp, end);
168 if (!js_strtod(cx, bp, end, &ep, &d) || ep == bp)
169 return js_NaN;
170 return d;
172 #endif
174 static inline jsdouble
175 DoubleToInteger(jsdouble d)
177 if (!JSDOUBLE_IS_FINITE(d))
178 return js_NaN;
179 if (d > 0)
180 return floor(d);
181 if (d < 0)
182 return -floor(-d);
183 return 0;
186 /* See ECMA 15.1.2.2. */
187 static JSBool
188 num_parseInt(JSContext *cx, uintN argc, Value *vp)
190 JSString *str;
191 jsdouble d;
192 const jschar *bp, *end, *ep;
194 if (argc == 0) {
195 vp->setDouble(js_NaN);
196 return JS_TRUE;
198 int32_t radix;
199 if (argc > 1) {
200 if (!ValueToECMAInt32(cx, vp[3], &radix))
201 return JS_FALSE;
202 } else {
203 radix = 0;
205 if (radix != 0 && (radix < 2 || radix > 36)) {
206 vp->setDouble(js_NaN);
207 return JS_TRUE;
210 if (vp[2].isInt32() && (radix == 0 || radix == 10)) {
211 *vp = vp[2];
212 return JS_TRUE;
215 if (vp[2].isDouble() && (radix == 0 || radix == 10)) {
216 vp->setDouble(DoubleToInteger(vp[2].toDouble()));
217 return JS_TRUE;
220 str = js_ValueToString(cx, vp[2]);
221 if (!str)
222 return JS_FALSE;
223 str->getCharsAndEnd(bp, end);
224 if (!js_strtointeger(cx, bp, end, &ep, radix, &d))
225 return JS_FALSE;
226 if (ep == bp) {
227 vp->setDouble(js_NaN);
228 return JS_TRUE;
230 vp->setNumber(d);
231 return JS_TRUE;
234 #ifdef JS_TRACER
235 static jsdouble FASTCALL
236 ParseInt(JSContext* cx, JSString* str)
238 const jschar* bp;
239 const jschar* end;
240 const jschar* ep;
241 jsdouble d;
243 str->getCharsAndEnd(bp, end);
244 if (!js_strtointeger(cx, bp, end, &ep, 0, &d) || ep == bp)
245 return js_NaN;
246 return d;
249 static jsdouble FASTCALL
250 ParseIntDouble(jsdouble d)
252 return DoubleToInteger(d);
254 #endif
256 const char js_Infinity_str[] = "Infinity";
257 const char js_NaN_str[] = "NaN";
258 const char js_isNaN_str[] = "isNaN";
259 const char js_isFinite_str[] = "isFinite";
260 const char js_parseFloat_str[] = "parseFloat";
261 const char js_parseInt_str[] = "parseInt";
263 #ifdef JS_TRACER
265 JS_DEFINE_TRCINFO_2(num_parseInt,
266 (2, (static, DOUBLE, ParseInt, CONTEXT, STRING, 1, nanojit::ACC_NONE)),
267 (1, (static, DOUBLE, ParseIntDouble, DOUBLE, 1, nanojit::ACC_NONE)))
269 JS_DEFINE_TRCINFO_1(num_parseFloat,
270 (2, (static, DOUBLE, ParseFloat, CONTEXT, STRING, 1, nanojit::ACC_NONE)))
272 #endif /* JS_TRACER */
274 static JSFunctionSpec number_functions[] = {
275 JS_FN(js_isNaN_str, num_isNaN, 1,0),
276 JS_FN(js_isFinite_str, num_isFinite, 1,0),
277 JS_TN(js_parseFloat_str, num_parseFloat, 1,0, &num_parseFloat_trcinfo),
278 JS_TN(js_parseInt_str, num_parseInt, 2,0, &num_parseInt_trcinfo),
279 JS_FS_END
282 Class js_NumberClass = {
283 js_Number_str,
284 JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Number),
285 PropertyStub, PropertyStub, PropertyStub, PropertyStub,
286 EnumerateStub, ResolveStub, ConvertStub, NULL,
287 JSCLASS_NO_OPTIONAL_MEMBERS
290 static JSBool
291 Number(JSContext *cx, JSObject *obj, uintN argc, Value *argv, Value *rval)
293 Value v;
294 if (argc != 0) {
295 if (!ValueToNumber(cx, &argv[0]))
296 return JS_FALSE;
297 } else {
298 argv[0].setInt32(0);
300 if (!JS_IsConstructing(cx))
301 *rval = argv[0];
302 else
303 obj->setPrimitiveThis(argv[0]);
304 return true;
307 #if JS_HAS_TOSOURCE
308 static JSBool
309 num_toSource(JSContext *cx, uintN argc, Value *vp)
311 char numBuf[DTOSTR_STANDARD_BUFFER_SIZE], *numStr;
312 char buf[64];
313 JSString *str;
315 const Value *primp;
316 if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &primp))
317 return JS_FALSE;
318 double d = primp->toNumber();
319 numStr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, numBuf, sizeof numBuf,
320 DTOSTR_STANDARD, 0, d);
321 if (!numStr) {
322 JS_ReportOutOfMemory(cx);
323 return JS_FALSE;
325 JS_snprintf(buf, sizeof buf, "(new %s(%s))", js_NumberClass.name, numStr);
326 str = JS_NewStringCopyZ(cx, buf);
327 if (!str)
328 return JS_FALSE;
329 vp->setString(str);
330 return JS_TRUE;
332 #endif
334 /* The buf must be big enough for MIN_INT to fit including '-' and '\0'. */
335 static char *
336 IntToCString(jsint i, jsint base, char *buf, size_t bufSize)
338 char *cp;
339 jsuint u;
341 u = (i < 0) ? -i : i;
343 cp = buf + bufSize; /* one past last buffer cell */
344 *--cp = '\0'; /* null terminate the string to be */
347 * Build the string from behind. We use multiply and subtraction
348 * instead of modulus because that's much faster.
350 switch (base) {
351 case 10:
352 do {
353 jsuint newu = u / 10;
354 *--cp = (char)(u - newu * 10) + '0';
355 u = newu;
356 } while (u != 0);
357 break;
358 case 16:
359 do {
360 jsuint newu = u / 16;
361 *--cp = "0123456789abcdef"[u - newu * 16];
362 u = newu;
363 } while (u != 0);
364 break;
365 default:
366 JS_ASSERT(base >= 2 && base <= 36);
367 do {
368 jsuint newu = u / base;
369 *--cp = "0123456789abcdefghijklmnopqrstuvwxyz"[u - newu * base];
370 u = newu;
371 } while (u != 0);
372 break;
374 if (i < 0)
375 *--cp = '-';
377 JS_ASSERT(cp >= buf);
378 return cp;
381 static JSString * JS_FASTCALL
382 js_NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base);
384 static JSBool
385 num_toString(JSContext *cx, uintN argc, Value *vp)
387 const Value *primp;
388 if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &primp))
389 return JS_FALSE;
390 double d = primp->toNumber();
391 int32_t base = 10;
392 if (argc != 0 && !vp[2].isUndefined()) {
393 if (!ValueToECMAInt32(cx, vp[2], &base))
394 return JS_FALSE;
396 if (base < 2 || base > 36) {
397 char numBuf[12];
398 char *numStr = IntToCString(base, 10, numBuf, sizeof numBuf);
399 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_RADIX,
400 numStr);
401 return JS_FALSE;
404 JSString *str = js_NumberToStringWithBase(cx, d, base);
405 if (!str) {
406 JS_ReportOutOfMemory(cx);
407 return JS_FALSE;
409 vp->setString(str);
410 return JS_TRUE;
413 static JSBool
414 num_toLocaleString(JSContext *cx, uintN argc, Value *vp)
416 size_t thousandsLength, decimalLength;
417 const char *numGrouping, *tmpGroup;
418 JSRuntime *rt;
419 JSString *str;
420 const char *num, *end, *tmpSrc;
421 char *buf, *tmpDest;
422 const char *nint;
423 int digits, size, remainder, nrepeat;
426 * Create the string, move back to bytes to make string twiddling
427 * a bit easier and so we can insert platform charset seperators.
429 if (!num_toString(cx, 0, vp))
430 return JS_FALSE;
431 JS_ASSERT(vp->isString());
432 num = js_GetStringBytes(cx, vp->toString());
433 if (!num)
434 return JS_FALSE;
437 * Find the first non-integer value, whether it be a letter as in
438 * 'Infinity', a decimal point, or an 'e' from exponential notation.
440 nint = num;
441 if (*nint == '-')
442 nint++;
443 while (*nint >= '0' && *nint <= '9')
444 nint++;
445 digits = nint - num;
446 end = num + digits;
447 if (!digits)
448 return JS_TRUE;
450 rt = cx->runtime;
451 thousandsLength = strlen(rt->thousandsSeparator);
452 decimalLength = strlen(rt->decimalSeparator);
454 /* Figure out how long resulting string will be. */
455 size = digits + (*nint ? strlen(nint + 1) + 1 : 0);
456 if (*nint == '.')
457 size += decimalLength;
459 numGrouping = tmpGroup = rt->numGrouping;
460 remainder = digits;
461 if (*num == '-')
462 remainder--;
464 while (*tmpGroup != CHAR_MAX && *tmpGroup != '\0') {
465 if (*tmpGroup >= remainder)
466 break;
467 size += thousandsLength;
468 remainder -= *tmpGroup;
469 tmpGroup++;
471 if (*tmpGroup == '\0' && *numGrouping != '\0') {
472 nrepeat = (remainder - 1) / tmpGroup[-1];
473 size += thousandsLength * nrepeat;
474 remainder -= nrepeat * tmpGroup[-1];
475 } else {
476 nrepeat = 0;
478 tmpGroup--;
480 buf = (char *)cx->malloc(size + 1);
481 if (!buf)
482 return JS_FALSE;
484 tmpDest = buf;
485 tmpSrc = num;
487 while (*tmpSrc == '-' || remainder--)
488 *tmpDest++ = *tmpSrc++;
489 while (tmpSrc < end) {
490 strcpy(tmpDest, rt->thousandsSeparator);
491 tmpDest += thousandsLength;
492 memcpy(tmpDest, tmpSrc, *tmpGroup);
493 tmpDest += *tmpGroup;
494 tmpSrc += *tmpGroup;
495 if (--nrepeat < 0)
496 tmpGroup--;
499 if (*nint == '.') {
500 strcpy(tmpDest, rt->decimalSeparator);
501 tmpDest += decimalLength;
502 strcpy(tmpDest, nint + 1);
503 } else {
504 strcpy(tmpDest, nint);
507 if (cx->localeCallbacks && cx->localeCallbacks->localeToUnicode)
508 return cx->localeCallbacks->localeToUnicode(cx, buf, Jsvalify(vp));
510 str = JS_NewString(cx, buf, size);
511 if (!str) {
512 cx->free(buf);
513 return JS_FALSE;
516 vp->setString(str);
517 return JS_TRUE;
520 static JSBool
521 num_valueOf(JSContext *cx, uintN argc, Value *vp)
523 if (vp[1].isNumber()) {
524 *vp = vp[1];
525 return JS_TRUE;
527 JSObject *obj = ComputeThisFromVp(cx, vp);
528 if (!InstanceOf(cx, obj, &js_NumberClass, vp + 2))
529 return JS_FALSE;
530 *vp = obj->getPrimitiveThis();
531 return JS_TRUE;
535 #define MAX_PRECISION 100
537 static JSBool
538 num_to(JSContext *cx, JSDToStrMode zeroArgMode, JSDToStrMode oneArgMode,
539 jsint precisionMin, jsint precisionMax, jsint precisionOffset,
540 uintN argc, Value *vp)
542 /* Use MAX_PRECISION+1 because precisionOffset can be 1. */
543 char buf[DTOSTR_VARIABLE_BUFFER_SIZE(MAX_PRECISION+1)];
544 char *numStr;
546 const Value *primp;
547 if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &primp))
548 return JS_FALSE;
549 double d = primp->toNumber();
551 double precision;
552 if (argc == 0) {
553 precision = 0.0;
554 oneArgMode = zeroArgMode;
555 } else {
556 if (!ValueToNumber(cx, vp[2], &precision))
557 return JS_FALSE;
558 precision = js_DoubleToInteger(precision);
559 if (precision < precisionMin || precision > precisionMax) {
560 numStr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, buf, sizeof buf,
561 DTOSTR_STANDARD, 0, precision);
562 if (!numStr)
563 JS_ReportOutOfMemory(cx);
564 else
565 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr);
566 return JS_FALSE;
570 numStr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, buf, sizeof buf,
571 oneArgMode, (jsint)precision + precisionOffset, d);
572 if (!numStr) {
573 JS_ReportOutOfMemory(cx);
574 return JS_FALSE;
576 JSString *str = JS_NewStringCopyZ(cx, numStr);
577 if (!str)
578 return JS_FALSE;
579 vp->setString(str);
580 return JS_TRUE;
584 * In the following three implementations, we allow a larger range of precision
585 * than ECMA requires; this is permitted by ECMA-262.
587 static JSBool
588 num_toFixed(JSContext *cx, uintN argc, Value *vp)
590 return num_to(cx, DTOSTR_FIXED, DTOSTR_FIXED, -20, MAX_PRECISION, 0,
591 argc, vp);
594 static JSBool
595 num_toExponential(JSContext *cx, uintN argc, Value *vp)
597 return num_to(cx, DTOSTR_STANDARD_EXPONENTIAL, DTOSTR_EXPONENTIAL, 0,
598 MAX_PRECISION, 1, argc, vp);
601 static JSBool
602 num_toPrecision(JSContext *cx, uintN argc, Value *vp)
604 if (argc == 0 || vp[2].isUndefined())
605 return num_toString(cx, 0, vp);
606 return num_to(cx, DTOSTR_STANDARD, DTOSTR_PRECISION, 1, MAX_PRECISION, 0,
607 argc, vp);
610 #ifdef JS_TRACER
612 JS_DEFINE_TRCINFO_2(num_toString,
613 (2, (extern, STRING_RETRY, js_NumberToString, CONTEXT, THIS_DOUBLE, 1,
614 nanojit::ACC_NONE)),
615 (3, (static, STRING_RETRY, js_NumberToStringWithBase, CONTEXT, THIS_DOUBLE, INT32, 1,
616 nanojit::ACC_NONE)))
618 #endif /* JS_TRACER */
620 static JSFunctionSpec number_methods[] = {
621 #if JS_HAS_TOSOURCE
622 JS_FN(js_toSource_str, num_toSource, 0,JSFUN_THISP_NUMBER),
623 #endif
624 JS_TN(js_toString_str, num_toString, 1,JSFUN_THISP_NUMBER, &num_toString_trcinfo),
625 JS_FN(js_toLocaleString_str, num_toLocaleString, 0,JSFUN_THISP_NUMBER),
626 JS_FN(js_valueOf_str, num_valueOf, 0,JSFUN_THISP_NUMBER),
627 JS_FN(js_toJSON_str, num_valueOf, 0,JSFUN_THISP_NUMBER),
628 JS_FN("toFixed", num_toFixed, 1,JSFUN_THISP_NUMBER),
629 JS_FN("toExponential", num_toExponential, 1,JSFUN_THISP_NUMBER),
630 JS_FN("toPrecision", num_toPrecision, 1,JSFUN_THISP_NUMBER),
631 JS_FS_END
634 /* NB: Keep this in synch with number_constants[]. */
635 enum nc_slot {
636 NC_NaN,
637 NC_POSITIVE_INFINITY,
638 NC_NEGATIVE_INFINITY,
639 NC_MAX_VALUE,
640 NC_MIN_VALUE,
641 NC_LIMIT
645 * Some to most C compilers forbid spelling these at compile time, or barf
646 * if you try, so all but MAX_VALUE are set up by js_InitRuntimeNumberState
647 * using union jsdpun.
649 static JSConstDoubleSpec number_constants[] = {
650 {0, js_NaN_str, 0,{0,0,0}},
651 {0, "POSITIVE_INFINITY", 0,{0,0,0}},
652 {0, "NEGATIVE_INFINITY", 0,{0,0,0}},
653 {1.7976931348623157E+308, "MAX_VALUE", 0,{0,0,0}},
654 {0, "MIN_VALUE", 0,{0,0,0}},
655 {0,0,0,{0,0,0}}
658 jsdouble js_NaN;
659 jsdouble js_PositiveInfinity;
660 jsdouble js_NegativeInfinity;
662 #if (defined __GNUC__ && defined __i386__) || \
663 (defined __SUNPRO_CC && defined __i386)
666 * Set the exception mask to mask all exceptions and set the FPU precision
667 * to 53 bit mantissa (64 bit doubles).
669 inline void FIX_FPU() {
670 short control;
671 asm("fstcw %0" : "=m" (control) : );
672 control &= ~0x300; // Lower bits 8 and 9 (precision control).
673 control |= 0x2f3; // Raise bits 0-5 (exception masks) and 9 (64-bit precision).
674 asm("fldcw %0" : : "m" (control) );
677 #else
679 #define FIX_FPU() ((void)0)
681 #endif
683 JSBool
684 js_InitRuntimeNumberState(JSContext *cx)
686 JSRuntime *rt = cx->runtime;
688 FIX_FPU();
690 jsdpun u;
691 u.s.hi = JSDOUBLE_HI32_NAN;
692 u.s.lo = JSDOUBLE_LO32_NAN;
693 number_constants[NC_NaN].dval = js_NaN = u.d;
694 rt->NaNValue.setDouble(u.d);
696 u.s.hi = JSDOUBLE_HI32_EXPMASK;
697 u.s.lo = 0x00000000;
698 number_constants[NC_POSITIVE_INFINITY].dval = js_PositiveInfinity = u.d;
699 rt->positiveInfinityValue.setDouble(u.d);
701 u.s.hi = JSDOUBLE_HI32_SIGNBIT | JSDOUBLE_HI32_EXPMASK;
702 u.s.lo = 0x00000000;
703 number_constants[NC_NEGATIVE_INFINITY].dval = js_NegativeInfinity = u.d;
704 rt->negativeInfinityValue.setDouble(u.d);
706 u.s.hi = 0;
707 u.s.lo = 1;
708 number_constants[NC_MIN_VALUE].dval = u.d;
710 #ifndef HAVE_LOCALECONV
711 rt->thousandsSeparator = JS_strdup(cx, "'");
712 rt->decimalSeparator = JS_strdup(cx, ".");
713 rt->numGrouping = JS_strdup(cx, "\3\0");
714 #else
715 struct lconv *locale = localeconv();
716 rt->thousandsSeparator =
717 JS_strdup(cx, locale->thousands_sep ? locale->thousands_sep : "'");
718 rt->decimalSeparator =
719 JS_strdup(cx, locale->decimal_point ? locale->decimal_point : ".");
720 rt->numGrouping =
721 JS_strdup(cx, locale->grouping ? locale->grouping : "\3\0");
722 #endif
724 return rt->thousandsSeparator && rt->decimalSeparator && rt->numGrouping;
727 void
728 js_FinishRuntimeNumberState(JSContext *cx)
730 JSRuntime *rt = cx->runtime;
732 cx->free((void *) rt->thousandsSeparator);
733 cx->free((void *) rt->decimalSeparator);
734 cx->free((void *) rt->numGrouping);
735 rt->thousandsSeparator = rt->decimalSeparator = rt->numGrouping = NULL;
738 JSObject *
739 js_InitNumberClass(JSContext *cx, JSObject *obj)
741 JSObject *proto, *ctor;
742 JSRuntime *rt;
744 /* XXX must do at least once per new thread, so do it per JSContext... */
745 FIX_FPU();
747 if (!JS_DefineFunctions(cx, obj, number_functions))
748 return NULL;
750 proto = js_InitClass(cx, obj, NULL, &js_NumberClass, Number, 1,
751 NULL, number_methods, NULL, NULL);
752 if (!proto || !(ctor = JS_GetConstructor(cx, proto)))
753 return NULL;
754 proto->setPrimitiveThis(Int32Value(0));
755 if (!JS_DefineConstDoubles(cx, ctor, number_constants))
756 return NULL;
758 /* ECMA 15.1.1.1 */
759 rt = cx->runtime;
760 if (!JS_DefineProperty(cx, obj, js_NaN_str, Jsvalify(rt->NaNValue),
761 JS_PropertyStub, JS_PropertyStub,
762 JSPROP_PERMANENT | JSPROP_READONLY)) {
763 return NULL;
766 /* ECMA 15.1.1.2 */
767 if (!JS_DefineProperty(cx, obj, js_Infinity_str, Jsvalify(rt->positiveInfinityValue),
768 JS_PropertyStub, JS_PropertyStub,
769 JSPROP_PERMANENT | JSPROP_READONLY)) {
770 return NULL;
772 return proto;
776 * Convert a number to C string. The buf must be large enough to accommodate
777 * the result, including '-' and '\0', if base == 10 or d is an integer that
778 * fits in 32 bits. The caller must free the resulting pointer if it does not
779 * point into buf.
781 static char *
782 NumberToCString(JSContext *cx, jsdouble d, jsint base, char *buf, size_t bufSize)
784 int32_t i;
785 char *numStr;
787 JS_ASSERT(bufSize >= DTOSTR_STANDARD_BUFFER_SIZE);
788 if (JSDOUBLE_IS_INT32(d, &i)) {
789 numStr = IntToCString(i, base, buf, bufSize);
790 } else {
791 if (base == 10)
792 numStr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, buf, bufSize,
793 DTOSTR_STANDARD, 0, d);
794 else
795 numStr = js_dtobasestr(JS_THREAD_DATA(cx)->dtoaState, base, d);
796 if (!numStr) {
797 JS_ReportOutOfMemory(cx);
798 return NULL;
801 return numStr;
804 JSString * JS_FASTCALL
805 js_IntToString(JSContext *cx, jsint i)
807 if (jsuint(i) < INT_STRING_LIMIT)
808 return JSString::intString(i);
810 char buf[12];
811 return JS_NewStringCopyZ(cx, IntToCString(i, 10, buf, sizeof buf));
814 static JSString * JS_FASTCALL
815 js_NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base)
818 * The longest possible result here that would need to fit in buf is
819 * (-0x80000000).toString(2), which has length 33. (This can produce
820 * longer results, but in those cases buf is not used; see comment at
821 * NumberToCString.)
823 char buf[34];
824 char *numStr;
825 JSString *s;
828 * Caller is responsible for error reporting. When called from trace,
829 * returning NULL here will cause us to fall of trace and then retry
830 * from the interpreter (which will report the error).
832 if (base < 2 || base > 36)
833 return NULL;
835 int32_t i;
836 if (JSDOUBLE_IS_INT32(d, &i)) {
837 if (base == 10 && jsuint(i) < INT_STRING_LIMIT)
838 return JSString::intString(i);
839 if (jsuint(i) < jsuint(base)) {
840 if (i < 10)
841 return JSString::intString(i);
842 return JSString::unitString(jschar('a' + i - 10));
845 JSThreadData *data = JS_THREAD_DATA(cx);
846 if (data->dtoaCache.s && data->dtoaCache.base == base && data->dtoaCache.d == d)
847 return data->dtoaCache.s;
848 numStr = NumberToCString(cx, d, base, buf, sizeof buf);
849 if (!numStr)
850 return NULL;
851 s = JS_NewStringCopyZ(cx, numStr);
852 if (!(numStr >= buf && numStr < buf + sizeof buf))
853 js_free(numStr);
854 data->dtoaCache.base = base;
855 data->dtoaCache.d = d;
856 data->dtoaCache.s = s;
857 return s;
860 JSString * JS_FASTCALL
861 js_NumberToString(JSContext *cx, jsdouble d)
863 return js_NumberToStringWithBase(cx, d, 10);
866 JSBool JS_FASTCALL
867 js_NumberValueToCharBuffer(JSContext *cx, const Value &v, JSCharBuffer &cb)
869 /* Convert to C-string. */
870 static const size_t arrSize = DTOSTR_STANDARD_BUFFER_SIZE;
871 char arr[arrSize];
872 const char *cstr;
873 if (v.isInt32()) {
874 cstr = IntToCString(v.toInt32(), 10, arr, arrSize);
875 } else {
876 cstr = js_dtostr(JS_THREAD_DATA(cx)->dtoaState, arr, arrSize,
877 DTOSTR_STANDARD, 0, v.toDouble());
879 if (!cstr)
880 return JS_FALSE;
883 * Inflate to jschar string. The input C-string characters are < 127, so
884 * even if jschars are UTF-8, all chars should map to one jschar.
886 size_t cstrlen = strlen(cstr);
887 JS_ASSERT(cstrlen < arrSize);
888 size_t sizeBefore = cb.length();
889 if (!cb.growByUninitialized(cstrlen))
890 return JS_FALSE;
891 jschar *appendBegin = cb.begin() + sizeBefore;
892 #ifdef DEBUG
893 size_t oldcstrlen = cstrlen;
894 JSBool ok =
895 #endif
896 js_InflateStringToBuffer(cx, cstr, cstrlen, appendBegin, &cstrlen);
897 JS_ASSERT(ok && cstrlen == oldcstrlen);
898 return JS_TRUE;
901 namespace js {
903 bool
904 ValueToNumberSlow(JSContext *cx, Value v, double *out)
906 JS_ASSERT(!v.isNumber());
907 goto skip_int_double;
908 for (;;) {
909 if (v.isNumber()) {
910 *out = v.toNumber();
911 return true;
913 skip_int_double:
914 if (v.isString()) {
915 jsdouble d = StringToNumberType<jsdouble>(cx, v.toString());
916 if (JSDOUBLE_IS_NaN(d))
917 break;
918 *out = d;
919 return true;
921 if (v.isBoolean()) {
922 if (v.toBoolean()) {
923 *out = 1.0;
924 return true;
926 *out = 0.0;
927 return true;
929 if (v.isNull()) {
930 *out = 0.0;
931 return true;
933 if (v.isUndefined())
934 break;
936 JS_ASSERT(v.isObject());
937 if (!DefaultValue(cx, &v.toObject(), JSTYPE_NUMBER, &v))
938 return false;
939 if (v.isObject())
940 break;
943 *out = js_NaN;
944 return true;
947 bool
948 ValueToECMAInt32Slow(JSContext *cx, const Value &v, int32_t *out)
950 JS_ASSERT(!v.isInt32());
951 jsdouble d;
952 if (v.isDouble()) {
953 d = v.toDouble();
954 } else {
955 if (!ValueToNumberSlow(cx, v, &d))
956 return false;
958 *out = js_DoubleToECMAInt32(d);
959 return true;
962 bool
963 ValueToECMAUint32Slow(JSContext *cx, const Value &v, uint32_t *out)
965 JS_ASSERT(!v.isInt32());
966 jsdouble d;
967 if (v.isDouble()) {
968 d = v.toDouble();
969 } else {
970 if (!ValueToNumberSlow(cx, v, &d))
971 return false;
973 *out = js_DoubleToECMAUint32(d);
974 return true;
977 } /* namespace js */
979 uint32
980 js_DoubleToECMAUint32(jsdouble d)
982 int32 i;
983 JSBool neg;
984 jsdouble two32;
986 if (!JSDOUBLE_IS_FINITE(d))
987 return 0;
990 * We check whether d fits int32, not uint32, as all but the ">>>" bit
991 * manipulation bytecode stores the result as int, not uint. When the
992 * result does not fit int Value, it will be stored as a negative double.
994 i = (int32) d;
995 if ((jsdouble) i == d)
996 return (int32)i;
998 neg = (d < 0);
999 d = floor(neg ? -d : d);
1000 d = neg ? -d : d;
1002 two32 = 4294967296.0;
1003 d = fmod(d, two32);
1005 return (uint32) (d >= 0 ? d : d + two32);
1008 namespace js {
1010 bool
1011 ValueToInt32Slow(JSContext *cx, const Value &v, int32_t *out)
1013 JS_ASSERT(!v.isInt32());
1014 jsdouble d;
1015 if (v.isDouble()) {
1016 d = v.toDouble();
1017 } else if (!ValueToNumberSlow(cx, v, &d)) {
1018 return false;
1021 if (JSDOUBLE_IS_NaN(d) || d <= -2147483649.0 || 2147483648.0 <= d) {
1022 js_ReportValueError(cx, JSMSG_CANT_CONVERT,
1023 JSDVG_SEARCH_STACK, v, NULL);
1024 return false;
1026 *out = (int32) floor(d + 0.5); /* Round to nearest */
1027 return true;
1030 bool
1031 ValueToUint16Slow(JSContext *cx, const Value &v, uint16_t *out)
1033 JS_ASSERT(!v.isInt32());
1034 jsdouble d;
1035 if (v.isDouble()) {
1036 d = v.toDouble();
1037 } else if (!ValueToNumberSlow(cx, v, &d)) {
1038 return false;
1041 if (d == 0 || !JSDOUBLE_IS_FINITE(d)) {
1042 *out = 0;
1043 return true;
1046 uint16 u = (uint16) d;
1047 if ((jsdouble)u == d) {
1048 *out = u;
1049 return true;
1052 bool neg = (d < 0);
1053 d = floor(neg ? -d : d);
1054 d = neg ? -d : d;
1055 jsuint m = JS_BIT(16);
1056 d = fmod(d, (double) m);
1057 if (d < 0)
1058 d += m;
1059 *out = (uint16_t) d;
1060 return true;
1063 } /* namespace js */
1065 JSBool
1066 js_strtod(JSContext *cx, const jschar *s, const jschar *send,
1067 const jschar **ep, jsdouble *dp)
1069 const jschar *s1;
1070 size_t length, i;
1071 char cbuf[32];
1072 char *cstr, *istr, *estr;
1073 JSBool negative;
1074 jsdouble d;
1076 s1 = js_SkipWhiteSpace(s, send);
1077 length = send - s1;
1079 /* Use cbuf to avoid malloc */
1080 if (length >= sizeof cbuf) {
1081 cstr = (char *) cx->malloc(length + 1);
1082 if (!cstr)
1083 return JS_FALSE;
1084 } else {
1085 cstr = cbuf;
1088 for (i = 0; i != length; i++) {
1089 if (s1[i] >> 8)
1090 break;
1091 cstr[i] = (char)s1[i];
1093 cstr[i] = 0;
1095 istr = cstr;
1096 if ((negative = (*istr == '-')) != 0 || *istr == '+')
1097 istr++;
1098 if (*istr == 'I' && !strncmp(istr, js_Infinity_str, sizeof js_Infinity_str - 1)) {
1099 d = negative ? js_NegativeInfinity : js_PositiveInfinity;
1100 estr = istr + 8;
1101 } else {
1102 int err;
1103 d = js_strtod_harder(JS_THREAD_DATA(cx)->dtoaState, cstr, &estr, &err);
1104 if (d == HUGE_VAL)
1105 d = js_PositiveInfinity;
1106 else if (d == -HUGE_VAL)
1107 d = js_NegativeInfinity;
1110 i = estr - cstr;
1111 if (cstr != cbuf)
1112 cx->free(cstr);
1113 *ep = i ? s1 + i : s;
1114 *dp = d;
1115 return JS_TRUE;
1118 struct BinaryDigitReader
1120 uintN base; /* Base of number; must be a power of 2 */
1121 uintN digit; /* Current digit value in radix given by base */
1122 uintN digitMask; /* Mask to extract the next bit from digit */
1123 const jschar *digits; /* Pointer to the remaining digits */
1124 const jschar *end; /* Pointer to first non-digit */
1127 /* Return the next binary digit from the number or -1 if done */
1128 static intN GetNextBinaryDigit(struct BinaryDigitReader *bdr)
1130 intN bit;
1132 if (bdr->digitMask == 0) {
1133 uintN c;
1135 if (bdr->digits == bdr->end)
1136 return -1;
1138 c = *bdr->digits++;
1139 if ('0' <= c && c <= '9')
1140 bdr->digit = c - '0';
1141 else if ('a' <= c && c <= 'z')
1142 bdr->digit = c - 'a' + 10;
1143 else
1144 bdr->digit = c - 'A' + 10;
1145 bdr->digitMask = bdr->base >> 1;
1147 bit = (bdr->digit & bdr->digitMask) != 0;
1148 bdr->digitMask >>= 1;
1149 return bit;
1152 JSBool
1153 js_strtointeger(JSContext *cx, const jschar *s, const jschar *send,
1154 const jschar **ep, jsint base, jsdouble *dp)
1156 const jschar *s1, *start;
1157 JSBool negative;
1158 jsdouble value;
1160 s1 = js_SkipWhiteSpace(s, send);
1161 if (s1 == send)
1162 goto no_digits;
1163 if ((negative = (*s1 == '-')) != 0 || *s1 == '+') {
1164 s1++;
1165 if (s1 == send)
1166 goto no_digits;
1169 if (base == 0) {
1170 /* No base supplied, or some base that evaluated to 0. */
1171 if (*s1 == '0') {
1172 /* It's either hex or octal; only increment char if str isn't '0' */
1173 if (s1 + 1 != send && (s1[1] == 'X' || s1[1] == 'x')) {
1174 base = 16;
1175 s1 += 2;
1176 if (s1 == send)
1177 goto no_digits;
1178 } else {
1179 base = 8;
1181 } else {
1182 base = 10; /* Default to decimal. */
1184 } else if (base == 16) {
1185 /* If base is 16, ignore hex prefix. */
1186 if (*s1 == '0' && s1 + 1 != send && (s1[1] == 'X' || s1[1] == 'x')) {
1187 s1 += 2;
1188 if (s1 == send)
1189 goto no_digits;
1194 * Done with the preliminaries; find some prefix of the string that's
1195 * a number in the given base.
1197 JS_ASSERT(s1 < send);
1198 start = s1;
1199 value = 0.0;
1200 do {
1201 uintN digit;
1202 jschar c = *s1;
1203 if ('0' <= c && c <= '9')
1204 digit = c - '0';
1205 else if ('a' <= c && c <= 'z')
1206 digit = c - 'a' + 10;
1207 else if ('A' <= c && c <= 'Z')
1208 digit = c - 'A' + 10;
1209 else
1210 break;
1211 if (digit >= (uintN)base)
1212 break;
1213 value = value * base + digit;
1214 } while (++s1 != send);
1216 if (value >= 9007199254740992.0) {
1217 if (base == 10) {
1219 * If we're accumulating a decimal number and the number is >=
1220 * 2^53, then the result from the repeated multiply-add above may
1221 * be inaccurate. Call js_strtod_harder to get the correct answer.
1223 size_t i;
1224 size_t length = s1 - start;
1225 char *cstr = (char *) cx->malloc(length + 1);
1226 char *estr;
1227 int err=0;
1229 if (!cstr)
1230 return JS_FALSE;
1231 for (i = 0; i != length; i++)
1232 cstr[i] = (char)start[i];
1233 cstr[length] = 0;
1235 value = js_strtod_harder(JS_THREAD_DATA(cx)->dtoaState, cstr, &estr, &err);
1236 if (err == JS_DTOA_ENOMEM) {
1237 JS_ReportOutOfMemory(cx);
1238 cx->free(cstr);
1239 return JS_FALSE;
1241 if (err == JS_DTOA_ERANGE && value == HUGE_VAL)
1242 value = js_PositiveInfinity;
1243 cx->free(cstr);
1244 } else if ((base & (base - 1)) == 0) {
1246 * The number may also be inaccurate for power-of-two bases. This
1247 * happens if the addition in value * base + digit causes a round-
1248 * down to an even least significant mantissa bit when the first
1249 * dropped bit is a one. If any of the following digits in the
1250 * number (which haven't been added in yet) are nonzero, then the
1251 * correct action would have been to round up instead of down. An
1252 * example occurs when reading the number 0x1000000000000081, which
1253 * rounds to 0x1000000000000000 instead of 0x1000000000000100.
1255 struct BinaryDigitReader bdr;
1256 intN bit, bit2;
1257 intN j;
1259 bdr.base = base;
1260 bdr.digit = 0; // shut GCC up
1261 bdr.digitMask = 0;
1262 bdr.digits = start;
1263 bdr.end = s1;
1264 value = 0.0;
1266 /* Skip leading zeros. */
1267 do {
1268 bit = GetNextBinaryDigit(&bdr);
1269 } while (bit == 0);
1271 if (bit == 1) {
1272 /* Gather the 53 significant bits (including the leading 1) */
1273 value = 1.0;
1274 for (j = 52; j; j--) {
1275 bit = GetNextBinaryDigit(&bdr);
1276 if (bit < 0)
1277 goto done;
1278 value = value*2 + bit;
1280 /* bit2 is the 54th bit (the first dropped from the mantissa) */
1281 bit2 = GetNextBinaryDigit(&bdr);
1282 if (bit2 >= 0) {
1283 jsdouble factor = 2.0;
1284 intN sticky = 0; /* sticky is 1 if any bit beyond the 54th is 1 */
1285 intN bit3;
1287 while ((bit3 = GetNextBinaryDigit(&bdr)) >= 0) {
1288 sticky |= bit3;
1289 factor *= 2;
1291 value += bit2 & (bit | sticky);
1292 value *= factor;
1294 done:;
1298 /* We don't worry about inaccurate numbers for any other base. */
1300 if (s1 == start) {
1301 no_digits:
1302 *dp = 0.0;
1303 *ep = s;
1304 } else {
1305 *dp = negative ? -value : value;
1306 *ep = s1;
1308 return JS_TRUE;