2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JS number type and wrapper class.
|
|
|
|
*/
|
2012-01-23 03:43:16 -08:00
|
|
|
|
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
#include "mozilla/RangedPtr.h"
|
|
|
|
|
|
|
|
#include "double-conversion.h"
|
|
|
|
// Avoid warnings about ASSERT being defined by the assembler as well.
|
|
|
|
#undef ASSERT
|
|
|
|
|
2009-03-30 15:26:51 -07:00
|
|
|
#ifdef XP_OS2
|
|
|
|
#define _PC_53 PC_53
|
|
|
|
#define _MCW_EM MCW_EM
|
|
|
|
#define _MCW_PC MCW_PC
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <locale.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-07-27 17:09:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jstypes.h"
|
2010-10-01 16:46:54 -07:00
|
|
|
#include "jsutil.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsdtoa.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
|
|
|
#include "jsprf.h"
|
2008-03-04 15:40:10 -08:00
|
|
|
#include "jsscope.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsstr.h"
|
2011-06-07 18:41:37 -07:00
|
|
|
#include "jslibmath.h"
|
2010-04-14 16:18:03 -07:00
|
|
|
|
2011-05-02 14:03:47 -07:00
|
|
|
#include "vm/GlobalObject.h"
|
2012-04-19 16:18:24 -07:00
|
|
|
#include "vm/NumericConversions.h"
|
2012-03-14 15:29:29 -07:00
|
|
|
#include "vm/StringBuffer.h"
|
2011-05-02 14:03:47 -07:00
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
#include "jsatominlines.h"
|
2010-10-29 08:05:55 -07:00
|
|
|
#include "jsinferinlines.h"
|
2011-06-10 11:44:16 -07:00
|
|
|
#include "jsnuminlines.h"
|
2010-04-14 18:57:30 -07:00
|
|
|
#include "jsobjinlines.h"
|
|
|
|
|
2011-05-13 14:12:15 -07:00
|
|
|
#include "vm/NumberObject-inl.h"
|
2011-06-20 11:44:20 -07:00
|
|
|
#include "vm/String-inl.h"
|
|
|
|
|
2010-03-09 14:21:32 -08:00
|
|
|
using namespace js;
|
2010-10-29 08:05:55 -07:00
|
|
|
using namespace js::types;
|
2009-07-15 15:59:25 -07:00
|
|
|
|
2010-07-23 00:29:44 -07:00
|
|
|
/*
|
|
|
|
* If we're accumulating a decimal number and the number is >= 2^53, then the
|
|
|
|
* fast result from the loop in GetPrefixInteger may be inaccurate. Call
|
|
|
|
* js_strtod_harder to get the correct answer.
|
|
|
|
*/
|
2010-10-25 16:47:11 -07:00
|
|
|
static bool
|
2012-02-24 14:19:52 -08:00
|
|
|
ComputeAccurateDecimalInteger(JSContext *cx, const jschar *start, const jschar *end, double *dp)
|
2010-07-23 00:29:44 -07:00
|
|
|
{
|
|
|
|
size_t length = end - start;
|
2012-08-31 15:10:10 -07:00
|
|
|
char *cstr = cx->pod_malloc<char>(length + 1);
|
2010-07-23 00:29:44 -07:00
|
|
|
if (!cstr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
char c = char(start[i]);
|
|
|
|
JS_ASSERT(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
|
|
|
|
cstr[i] = c;
|
|
|
|
}
|
|
|
|
cstr[length] = 0;
|
|
|
|
|
|
|
|
char *estr;
|
|
|
|
int err = 0;
|
2011-07-18 14:54:48 -07:00
|
|
|
*dp = js_strtod_harder(cx->runtime->dtoaState, cstr, &estr, &err);
|
2010-07-23 00:29:44 -07:00
|
|
|
if (err == JS_DTOA_ENOMEM) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(cstr);
|
2010-07-23 00:29:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (err == JS_DTOA_ERANGE && *dp == HUGE_VAL)
|
|
|
|
*dp = js_PositiveInfinity;
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(cstr);
|
2010-07-23 00:29:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class BinaryDigitReader
|
|
|
|
{
|
|
|
|
const int base; /* Base of number; must be a power of 2 */
|
|
|
|
int digit; /* Current digit value in radix given by base */
|
|
|
|
int digitMask; /* Mask to extract the next bit from digit */
|
|
|
|
const jschar *start; /* Pointer to the remaining digits */
|
|
|
|
const jschar *end; /* Pointer to first non-digit */
|
|
|
|
|
|
|
|
public:
|
|
|
|
BinaryDigitReader(int base, const jschar *start, const jschar *end)
|
|
|
|
: base(base), digit(0), digitMask(0), start(start), end(end)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the next binary digit from the number, or -1 if done. */
|
|
|
|
int nextDigit() {
|
|
|
|
if (digitMask == 0) {
|
|
|
|
if (start == end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int c = *start++;
|
|
|
|
JS_ASSERT(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
digit = c - '0';
|
|
|
|
else if ('a' <= c && c <= 'z')
|
|
|
|
digit = c - 'a' + 10;
|
|
|
|
else
|
|
|
|
digit = c - 'A' + 10;
|
|
|
|
digitMask = base >> 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bit = (digit & digitMask) != 0;
|
|
|
|
digitMask >>= 1;
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The fast result might also have been inaccurate for power-of-two bases. This
|
|
|
|
* happens if the addition in value * 2 + digit causes a round-down to an even
|
|
|
|
* least significant mantissa bit when the first dropped bit is a one. If any
|
|
|
|
* of the following digits in the number (which haven't been added in yet) are
|
|
|
|
* nonzero, then the correct action would have been to round up instead of
|
|
|
|
* down. An example occurs when reading the number 0x1000000000000081, which
|
|
|
|
* rounds to 0x1000000000000000 instead of 0x1000000000000100.
|
|
|
|
*/
|
2012-02-24 14:19:52 -08:00
|
|
|
static double
|
2010-07-23 00:29:44 -07:00
|
|
|
ComputeAccurateBinaryBaseInteger(JSContext *cx, const jschar *start, const jschar *end, int base)
|
|
|
|
{
|
|
|
|
BinaryDigitReader bdr(base, start, end);
|
|
|
|
|
|
|
|
/* Skip leading zeroes. */
|
|
|
|
int bit;
|
|
|
|
do {
|
|
|
|
bit = bdr.nextDigit();
|
|
|
|
} while (bit == 0);
|
|
|
|
|
|
|
|
JS_ASSERT(bit == 1); // guaranteed by GetPrefixInteger
|
|
|
|
|
|
|
|
/* Gather the 53 significant bits (including the leading 1). */
|
2012-02-24 14:19:52 -08:00
|
|
|
double value = 1.0;
|
2010-07-23 00:29:44 -07:00
|
|
|
for (int j = 52; j > 0; j--) {
|
|
|
|
bit = bdr.nextDigit();
|
|
|
|
if (bit < 0)
|
|
|
|
return value;
|
|
|
|
value = value * 2 + bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bit2 is the 54th bit (the first dropped from the mantissa). */
|
|
|
|
int bit2 = bdr.nextDigit();
|
|
|
|
if (bit2 >= 0) {
|
2012-02-24 14:19:52 -08:00
|
|
|
double factor = 2.0;
|
2010-07-23 00:29:44 -07:00
|
|
|
int sticky = 0; /* sticky is 1 if any bit beyond the 54th is 1 */
|
|
|
|
int bit3;
|
|
|
|
|
|
|
|
while ((bit3 = bdr.nextDigit()) >= 0) {
|
|
|
|
sticky |= bit3;
|
|
|
|
factor *= 2;
|
|
|
|
}
|
|
|
|
value += bit2 & (bit | sticky);
|
|
|
|
value *= factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetPrefixInteger(JSContext *cx, const jschar *start, const jschar *end, int base,
|
2012-02-24 14:19:52 -08:00
|
|
|
const jschar **endp, double *dp)
|
2010-07-23 00:29:44 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(start <= end);
|
|
|
|
JS_ASSERT(2 <= base && base <= 36);
|
|
|
|
|
|
|
|
const jschar *s = start;
|
2012-02-24 14:19:52 -08:00
|
|
|
double d = 0.0;
|
2010-07-23 00:29:44 -07:00
|
|
|
for (; s < end; s++) {
|
|
|
|
int digit;
|
|
|
|
jschar c = *s;
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
digit = c - '0';
|
|
|
|
else if ('a' <= c && c <= 'z')
|
|
|
|
digit = c - 'a' + 10;
|
|
|
|
else if ('A' <= c && c <= 'Z')
|
|
|
|
digit = c - 'A' + 10;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
if (digit >= base)
|
|
|
|
break;
|
|
|
|
d = d * base + digit;
|
|
|
|
}
|
|
|
|
|
|
|
|
*endp = s;
|
|
|
|
*dp = d;
|
|
|
|
|
|
|
|
/* If we haven't reached the limit of integer precision, we're done. */
|
|
|
|
if (d < DOUBLE_INTEGRAL_PRECISION_LIMIT)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise compute the correct integer from the prefix of valid digits
|
|
|
|
* if we're computing for base ten or a power of two. Don't worry about
|
|
|
|
* other bases; see 15.1.2.2 step 13.
|
|
|
|
*/
|
|
|
|
if (base == 10)
|
|
|
|
return ComputeAccurateDecimalInteger(cx, start, s, dp);
|
|
|
|
if ((base & (base - 1)) == 0)
|
|
|
|
*dp = ComputeAccurateBinaryBaseInteger(cx, start, s, base);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace js
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
num_isNaN(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(true);
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2012-02-24 14:19:52 -08:00
|
|
|
double x;
|
2011-06-13 21:49:59 -07:00
|
|
|
if (!ToNumber(cx, vp[2], &x))
|
2010-04-07 13:18:50 -07:00
|
|
|
return false;
|
2012-01-23 03:43:16 -08:00
|
|
|
vp->setBoolean(MOZ_DOUBLE_IS_NaN(x));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
num_isFinite(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(false);
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2012-02-24 14:19:52 -08:00
|
|
|
double x;
|
2011-06-13 21:49:59 -07:00
|
|
|
if (!ToNumber(cx, vp[2], &x))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2012-01-23 03:43:16 -08:00
|
|
|
vp->setBoolean(MOZ_DOUBLE_IS_FINITE(x));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
num_parseFloat(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSString *str;
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2007-08-15 23:23:06 -07:00
|
|
|
const jschar *bp, *end, *ep;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setDouble(js_NaN);
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2011-12-01 19:35:44 -08:00
|
|
|
str = ToString(cx, vp[2]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2010-12-06 10:26:58 -08:00
|
|
|
bp = str->getChars(cx);
|
|
|
|
if (!bp)
|
|
|
|
return JS_FALSE;
|
|
|
|
end = bp + str->length();
|
2007-08-15 23:23:06 -07:00
|
|
|
if (!js_strtod(cx, bp, end, &ep, &d))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (ep == bp) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setDouble(js_NaN);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setNumber(d);
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-10-25 16:47:11 -07:00
|
|
|
static bool
|
2010-08-03 15:47:58 -07:00
|
|
|
ParseIntStringHelper(JSContext *cx, const jschar *ws, const jschar *end, int maybeRadix,
|
2012-02-24 14:19:52 -08:00
|
|
|
bool stripPrefix, double *dp)
|
2010-07-23 00:29:44 -07:00
|
|
|
{
|
2010-07-30 14:39:51 -07:00
|
|
|
JS_ASSERT(maybeRadix == 0 || (2 <= maybeRadix && maybeRadix <= 36));
|
2010-07-23 00:29:44 -07:00
|
|
|
JS_ASSERT(ws <= end);
|
|
|
|
|
2011-07-26 14:10:33 -07:00
|
|
|
const jschar *s = SkipSpace(ws, end);
|
2010-07-23 00:29:44 -07:00
|
|
|
JS_ASSERT(ws <= s);
|
|
|
|
JS_ASSERT(s <= end);
|
|
|
|
|
|
|
|
/* 15.1.2.2 steps 3-4. */
|
|
|
|
bool negative = (s != end && s[0] == '-');
|
|
|
|
|
|
|
|
/* 15.1.2.2 step 5. */
|
|
|
|
if (s != end && (s[0] == '-' || s[0] == '+'))
|
|
|
|
s++;
|
|
|
|
|
2010-07-30 14:39:51 -07:00
|
|
|
/* 15.1.2.2 step 9. */
|
2010-07-23 00:29:44 -07:00
|
|
|
int radix = maybeRadix;
|
2010-07-30 14:39:51 -07:00
|
|
|
if (radix == 0) {
|
|
|
|
if (end - s >= 2 && s[0] == '0' && (s[1] != 'x' && s[1] != 'X')) {
|
|
|
|
/*
|
|
|
|
* Non-standard: ES5 requires that parseInt interpret leading-zero
|
|
|
|
* strings not starting with "0x" or "0X" as decimal (absent an
|
|
|
|
* explicitly specified non-zero radix), but we continue to
|
2010-08-02 18:04:29 -07:00
|
|
|
* interpret such strings as octal, as per ES3 and web practice.
|
2010-07-30 14:39:51 -07:00
|
|
|
*/
|
2010-08-02 18:04:29 -07:00
|
|
|
radix = 8;
|
2010-07-30 14:39:51 -07:00
|
|
|
} else {
|
|
|
|
radix = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 15.1.2.2 step 10. */
|
2010-07-23 00:29:44 -07:00
|
|
|
if (stripPrefix) {
|
2010-07-30 14:39:51 -07:00
|
|
|
if (end - s >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
|
|
|
|
s += 2;
|
|
|
|
radix = 16;
|
2010-07-23 00:29:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 15.1.2.2 steps 11-14. */
|
|
|
|
const jschar *actualEnd;
|
|
|
|
if (!GetPrefixInteger(cx, s, end, radix, &actualEnd, dp))
|
|
|
|
return false;
|
|
|
|
if (s == actualEnd)
|
|
|
|
*dp = js_NaN;
|
|
|
|
else if (negative)
|
|
|
|
*dp = -*dp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* See ECMA 15.1.2.2. */
|
2012-01-04 12:29:54 -08:00
|
|
|
JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
js::num_parseInt(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-12-06 02:31:16 -08:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2010-07-23 00:29:44 -07:00
|
|
|
/* Fast paths and exceptional cases. */
|
2011-12-06 02:31:16 -08:00
|
|
|
if (args.length() == 0) {
|
|
|
|
args.rval().setDouble(js_NaN);
|
2011-05-09 07:12:47 -07:00
|
|
|
return true;
|
2008-08-08 09:02:50 -07:00
|
|
|
}
|
2010-07-23 00:29:44 -07:00
|
|
|
|
2012-05-19 12:56:17 -07:00
|
|
|
if (args.length() == 1 ||
|
2011-12-06 02:31:16 -08:00
|
|
|
(args[1].isInt32() && (args[1].toInt32() == 0 || args[1].toInt32() == 10))) {
|
|
|
|
if (args[0].isInt32()) {
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().set(args[0]);
|
2010-07-23 00:29:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-07 05:31:59 -07:00
|
|
|
/*
|
2011-06-16 02:42:35 -07:00
|
|
|
* Step 1 is |inputString = ToString(string)|. When string >=
|
2011-05-07 05:31:59 -07:00
|
|
|
* 1e21, ToString(string) is in the form "NeM". 'e' marks the end of
|
|
|
|
* the word, which would mean the result of parseInt(string) should be |N|.
|
|
|
|
*
|
|
|
|
* To preserve this behaviour, we can't use the fast-path when string >
|
|
|
|
* 1e21, or else the result would be |NeM|.
|
2012-05-19 12:56:17 -07:00
|
|
|
*
|
2011-12-06 02:31:16 -08:00
|
|
|
* The same goes for values smaller than 1.0e-6, because the string would be in
|
|
|
|
* the form of "Ne-M".
|
2011-05-07 05:31:59 -07:00
|
|
|
*/
|
2011-12-06 02:31:16 -08:00
|
|
|
if (args[0].isDouble()) {
|
|
|
|
double d = args[0].toDouble();
|
|
|
|
if (1.0e-6 < d && d < 1.0e21) {
|
|
|
|
args.rval().setNumber(floor(d));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (-1.0e21 < d && d < -1.0e-6) {
|
|
|
|
args.rval().setNumber(-floor(-d));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (d == 0.0) {
|
|
|
|
args.rval().setInt32(0);
|
|
|
|
return true;
|
|
|
|
}
|
2010-07-23 00:29:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 1. */
|
2012-08-03 09:41:00 -07:00
|
|
|
RootedString inputString(cx, ToString(cx, args[0]));
|
2010-07-23 00:29:44 -07:00
|
|
|
if (!inputString)
|
|
|
|
return false;
|
2011-12-06 02:31:16 -08:00
|
|
|
args[0].setString(inputString);
|
2010-07-23 00:29:44 -07:00
|
|
|
|
2010-07-30 14:39:51 -07:00
|
|
|
/* 15.1.2.2 steps 6-8. */
|
2010-07-23 00:29:44 -07:00
|
|
|
bool stripPrefix = true;
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
int32_t radix = 0;
|
2011-12-06 02:31:16 -08:00
|
|
|
if (args.length() > 1) {
|
2011-12-05 14:10:02 -08:00
|
|
|
if (!ToInt32(cx, args[1], &radix))
|
2010-07-23 00:29:44 -07:00
|
|
|
return false;
|
|
|
|
if (radix != 0) {
|
|
|
|
if (radix < 2 || radix > 36) {
|
2011-12-06 02:31:16 -08:00
|
|
|
args.rval().setDouble(js_NaN);
|
2011-05-09 07:12:47 -07:00
|
|
|
return true;
|
2010-07-23 00:29:44 -07:00
|
|
|
}
|
|
|
|
if (radix != 16)
|
|
|
|
stripPrefix = false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-30 14:39:51 -07:00
|
|
|
/* Steps 2-5, 9-14. */
|
2010-12-06 10:26:58 -08:00
|
|
|
const jschar *ws = inputString->getChars(cx);
|
|
|
|
if (!ws)
|
|
|
|
return false;
|
|
|
|
const jschar *end = ws + inputString->length();
|
2008-03-05 11:11:54 -08:00
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
double number;
|
2010-07-23 00:29:44 -07:00
|
|
|
if (!ParseIntStringHelper(cx, ws, end, radix, stripPrefix, &number))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Step 15. */
|
2011-12-06 02:31:16 -08:00
|
|
|
args.rval().setNumber(number);
|
2010-07-23 00:29:44 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec number_functions[] = {
|
2011-06-10 19:03:57 -07:00
|
|
|
JS_FN(js_isNaN_str, num_isNaN, 1,0),
|
|
|
|
JS_FN(js_isFinite_str, num_isFinite, 1,0),
|
2011-11-22 14:41:42 -08:00
|
|
|
JS_FN(js_parseFloat_str, num_parseFloat, 1,0),
|
|
|
|
JS_FN(js_parseInt_str, num_parseInt, 2,0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-09-02 17:23:26 -07:00
|
|
|
Class js::NumberClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Number_str,
|
2009-08-04 14:06:55 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Number),
|
2011-09-20 11:40:24 -07:00
|
|
|
JS_PropertyStub, /* addProperty */
|
|
|
|
JS_PropertyStub, /* delProperty */
|
|
|
|
JS_PropertyStub, /* getProperty */
|
|
|
|
JS_StrictPropertyStub, /* setProperty */
|
|
|
|
JS_EnumerateStub,
|
|
|
|
JS_ResolveStub,
|
|
|
|
JS_ConvertStub
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
2012-02-28 15:11:11 -08:00
|
|
|
Number(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-16 12:35:04 -07:00
|
|
|
/* Sample JS_CALLEE before clobbering. */
|
|
|
|
bool isConstructing = IsConstructing(vp);
|
|
|
|
|
|
|
|
if (argc > 0) {
|
2011-06-13 21:49:59 -07:00
|
|
|
if (!ToNumber(cx, &vp[2]))
|
2010-08-16 12:35:04 -07:00
|
|
|
return false;
|
|
|
|
vp[0] = vp[2];
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-08-16 12:35:04 -07:00
|
|
|
vp[0].setInt32(0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-16 12:35:04 -07:00
|
|
|
|
|
|
|
if (!isConstructing)
|
|
|
|
return true;
|
2010-10-29 08:05:55 -07:00
|
|
|
|
2012-02-16 19:10:45 -08:00
|
|
|
JSObject *obj = NumberObject::create(cx, vp[0].toNumber());
|
2010-08-16 12:35:04 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
vp->setObject(*obj);
|
2009-08-04 14:06:55 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
IsNumber(const Value &v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-03 17:44:22 -07:00
|
|
|
return v.isNumber() || (v.isObject() && v.toObject().hasClass(&NumberClass));
|
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
inline double
|
|
|
|
Extract(const Value &v)
|
|
|
|
{
|
|
|
|
if (v.isNumber())
|
|
|
|
return v.toNumber();
|
|
|
|
return v.toObject().asNumber().unbox();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toSource_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
|
|
|
double d = Extract(args.thisv());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-06 02:31:00 -08:00
|
|
|
StringBuffer sb(cx);
|
2012-07-03 17:44:22 -07:00
|
|
|
if (!sb.append("(new Number(") ||
|
|
|
|
!NumberValueToStringBuffer(cx, NumberValue(d), sb) ||
|
2011-12-06 02:31:00 -08:00
|
|
|
!sb.append("))"))
|
|
|
|
{
|
2010-10-12 11:50:02 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-10-12 11:50:02 -07:00
|
|
|
|
2011-12-06 02:31:00 -08:00
|
|
|
JSString *str = sb.finishString();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2010-10-12 11:50:02 -07:00
|
|
|
return false;
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setString(str);
|
2010-10-12 11:50:02 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-03 17:44:22 -07:00
|
|
|
|
|
|
|
static JSBool
|
|
|
|
num_toSource(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toSource_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2010-09-13 13:08:25 -07:00
|
|
|
ToCStringBuf::ToCStringBuf() :dbuf(NULL)
|
|
|
|
{
|
|
|
|
JS_STATIC_ASSERT(sbufSize >= DTOSTR_STANDARD_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ToCStringBuf::~ToCStringBuf()
|
|
|
|
{
|
|
|
|
if (dbuf)
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(dbuf);
|
2010-09-13 13:08:25 -07:00
|
|
|
}
|
|
|
|
|
2012-08-28 10:28:19 -07:00
|
|
|
JSFlatString *
|
2012-06-18 09:37:25 -07:00
|
|
|
js::Int32ToString(JSContext *cx, int32_t si)
|
2010-11-08 14:35:27 -08:00
|
|
|
{
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint32_t ui;
|
2010-11-08 14:35:27 -08:00
|
|
|
if (si >= 0) {
|
2011-09-20 14:47:14 -07:00
|
|
|
if (StaticStrings::hasInt(si))
|
|
|
|
return cx->runtime->staticStrings.getInt(si);
|
2010-11-08 14:35:27 -08:00
|
|
|
ui = si;
|
|
|
|
} else {
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
ui = uint32_t(-si);
|
|
|
|
JS_ASSERT_IF(si == INT32_MIN, ui == uint32_t(INT32_MAX) + 1);
|
2010-11-08 14:35:27 -08:00
|
|
|
}
|
|
|
|
|
2011-01-24 16:30:16 -08:00
|
|
|
JSCompartment *c = cx->compartment;
|
2012-08-28 10:28:19 -07:00
|
|
|
if (JSFlatString *str = c->dtoaCache.lookup(10, si))
|
2011-01-24 16:30:16 -08:00
|
|
|
return str;
|
2010-11-08 14:35:27 -08:00
|
|
|
|
|
|
|
JSShortString *str = js_NewGCShortString(cx);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2012-08-22 12:19:07 -07:00
|
|
|
jschar buffer[JSShortString::MAX_SHORT_LENGTH + 1];
|
|
|
|
RangedPtr<jschar> end(buffer + JSShortString::MAX_SHORT_LENGTH,
|
|
|
|
buffer, JSShortString::MAX_SHORT_LENGTH + 1);
|
2011-08-04 19:21:25 -07:00
|
|
|
*end = '\0';
|
|
|
|
RangedPtr<jschar> start = BackfillIndexInCharBuffer(ui, end);
|
2010-11-08 14:35:27 -08:00
|
|
|
if (si < 0)
|
2011-08-04 19:21:25 -07:00
|
|
|
*--start = '-';
|
2010-11-08 14:35:27 -08:00
|
|
|
|
2012-08-22 12:19:07 -07:00
|
|
|
jschar *dst = str->init(end - start);
|
|
|
|
PodCopy(dst, start.get(), end - start + 1);
|
2010-11-08 14:35:27 -08:00
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
c->dtoaCache.cache(10, si, str);
|
|
|
|
return str;
|
2010-11-08 14:35:27 -08:00
|
|
|
}
|
|
|
|
|
2010-09-13 13:08:25 -07:00
|
|
|
/* Returns a non-NULL pointer to inside cbuf. */
|
2009-01-08 15:09:16 -08:00
|
|
|
static char *
|
2012-03-01 18:54:01 -08:00
|
|
|
IntToCString(ToCStringBuf *cbuf, int i, int base = 10)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-03-05 18:43:45 -08:00
|
|
|
unsigned u = (i < 0) ? -i : i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
RangedPtr<char> cp(cbuf->sbuf + cbuf->sbufSize - 1, cbuf->sbuf, cbuf->sbufSize);
|
|
|
|
*cp = '\0';
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
/* Build the string from behind. */
|
2008-10-13 19:07:30 -07:00
|
|
|
switch (base) {
|
|
|
|
case 10:
|
2011-08-04 19:21:25 -07:00
|
|
|
cp = BackfillIndexInCharBuffer(u, cp);
|
2008-10-13 19:07:30 -07:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
do {
|
2012-03-05 18:43:45 -08:00
|
|
|
unsigned newu = u / 16;
|
2008-10-13 19:07:30 -07:00
|
|
|
*--cp = "0123456789abcdef"[u - newu * 16];
|
|
|
|
u = newu;
|
|
|
|
} while (u != 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
JS_ASSERT(base >= 2 && base <= 36);
|
|
|
|
do {
|
2012-03-05 18:43:45 -08:00
|
|
|
unsigned newu = u / base;
|
2008-10-13 19:07:30 -07:00
|
|
|
*--cp = "0123456789abcdefghijklmnopqrstuvwxyz"[u - newu * base];
|
|
|
|
u = newu;
|
|
|
|
} while (u != 0);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i < 0)
|
|
|
|
*--cp = '-';
|
|
|
|
|
2011-08-04 19:21:25 -07:00
|
|
|
return cp.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
static JSString * JS_FASTCALL
|
2012-03-01 18:54:01 -08:00
|
|
|
js_NumberToStringWithBase(JSContext *cx, double d, int base);
|
2009-10-14 13:25:20 -07:00
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toString_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-03 17:44:22 -07:00
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
double d = Extract(args.thisv());
|
2010-10-12 11:50:02 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
int32_t base = 10;
|
2012-03-01 10:48:52 -08:00
|
|
|
if (args.hasDefined(0)) {
|
2012-02-24 14:19:52 -08:00
|
|
|
double d2;
|
2011-09-08 21:02:26 -07:00
|
|
|
if (!ToInteger(cx, args[0], &d2))
|
2011-04-02 11:33:20 -07:00
|
|
|
return false;
|
2010-04-07 13:18:50 -07:00
|
|
|
|
2011-04-02 11:33:20 -07:00
|
|
|
if (d2 < 2 || d2 > 36) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_RADIX);
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-04-02 11:33:20 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
base = int32_t(d2);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
JSString *str = js_NumberToStringWithBase(cx, d, base);
|
2009-10-14 13:25:20 -07:00
|
|
|
if (!str) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2011-10-04 10:48:36 -07:00
|
|
|
return false;
|
2009-10-14 13:25:20 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
args.rval().setString(str);
|
2011-10-04 10:48:36 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 17:45:14 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 17:45:14 -07:00
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toLocaleString_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
|
|
|
|
|
|
|
double d = Extract(args.thisv());
|
2012-07-06 17:45:14 -07:00
|
|
|
|
|
|
|
Rooted<JSString*> str(cx, js_NumberToStringWithBase(cx, d, 10));
|
|
|
|
if (!str) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the string, move back to bytes to make string twiddling
|
|
|
|
* a bit easier and so we can insert platform charset seperators.
|
|
|
|
*/
|
2012-07-06 17:45:14 -07:00
|
|
|
JSAutoByteString numBytes(cx, str);
|
2010-11-11 12:40:29 -08:00
|
|
|
if (!numBytes)
|
2012-07-06 17:30:06 -07:00
|
|
|
return false;
|
|
|
|
const char *num = numBytes.ptr();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!num)
|
2012-07-06 17:30:06 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-24 12:43:40 -07:00
|
|
|
/*
|
|
|
|
* Find the first non-integer value, whether it be a letter as in
|
2008-07-24 12:59:11 -07:00
|
|
|
* 'Infinity', a decimal point, or an 'e' from exponential notation.
|
2008-07-24 12:43:40 -07:00
|
|
|
*/
|
2012-07-06 17:30:06 -07:00
|
|
|
const char *nint = num;
|
2008-07-24 12:43:40 -07:00
|
|
|
if (*nint == '-')
|
|
|
|
nint++;
|
|
|
|
while (*nint >= '0' && *nint <= '9')
|
|
|
|
nint++;
|
2012-07-06 17:30:06 -07:00
|
|
|
int digits = nint - num;
|
|
|
|
const char *end = num + digits;
|
2012-07-06 17:45:14 -07:00
|
|
|
if (!digits) {
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().setString(str);
|
2012-07-06 17:30:06 -07:00
|
|
|
return true;
|
2012-07-06 17:45:14 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 17:30:06 -07:00
|
|
|
JSRuntime *rt = cx->runtime;
|
|
|
|
size_t thousandsLength = strlen(rt->thousandsSeparator);
|
|
|
|
size_t decimalLength = strlen(rt->decimalSeparator);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Figure out how long resulting string will be. */
|
2012-07-06 17:30:06 -07:00
|
|
|
int buflen = strlen(num);
|
2008-07-24 12:43:40 -07:00
|
|
|
if (*nint == '.')
|
2010-12-31 11:30:24 -08:00
|
|
|
buflen += decimalLength - 1; /* -1 to account for existing '.' */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 17:30:06 -07:00
|
|
|
const char *numGrouping;
|
|
|
|
const char *tmpGroup;
|
2007-03-22 10:30:00 -07:00
|
|
|
numGrouping = tmpGroup = rt->numGrouping;
|
2012-07-06 17:30:06 -07:00
|
|
|
int remainder = digits;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*num == '-')
|
|
|
|
remainder--;
|
|
|
|
|
|
|
|
while (*tmpGroup != CHAR_MAX && *tmpGroup != '\0') {
|
|
|
|
if (*tmpGroup >= remainder)
|
|
|
|
break;
|
2010-12-22 19:32:05 -08:00
|
|
|
buflen += thousandsLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
remainder -= *tmpGroup;
|
|
|
|
tmpGroup++;
|
|
|
|
}
|
2012-07-06 17:30:06 -07:00
|
|
|
|
|
|
|
int nrepeat;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (*tmpGroup == '\0' && *numGrouping != '\0') {
|
|
|
|
nrepeat = (remainder - 1) / tmpGroup[-1];
|
2010-12-22 19:32:05 -08:00
|
|
|
buflen += thousandsLength * nrepeat;
|
2007-03-22 10:30:00 -07:00
|
|
|
remainder -= nrepeat * tmpGroup[-1];
|
|
|
|
} else {
|
|
|
|
nrepeat = 0;
|
|
|
|
}
|
|
|
|
tmpGroup--;
|
|
|
|
|
2012-08-31 15:10:10 -07:00
|
|
|
char *buf = cx->pod_malloc<char>(buflen + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!buf)
|
2012-07-06 17:30:06 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 17:30:06 -07:00
|
|
|
char *tmpDest = buf;
|
|
|
|
const char *tmpSrc = num;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-31 11:30:24 -08:00
|
|
|
while (*tmpSrc == '-' || remainder--) {
|
|
|
|
JS_ASSERT(tmpDest - buf < buflen);
|
2007-03-22 10:30:00 -07:00
|
|
|
*tmpDest++ = *tmpSrc++;
|
2010-12-31 11:30:24 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
while (tmpSrc < end) {
|
2010-12-31 11:30:24 -08:00
|
|
|
JS_ASSERT(tmpDest - buf + ptrdiff_t(thousandsLength) <= buflen);
|
2007-03-22 10:30:00 -07:00
|
|
|
strcpy(tmpDest, rt->thousandsSeparator);
|
|
|
|
tmpDest += thousandsLength;
|
2010-12-31 11:30:24 -08:00
|
|
|
JS_ASSERT(tmpDest - buf + *tmpGroup <= buflen);
|
2012-01-17 14:32:36 -08:00
|
|
|
js_memcpy(tmpDest, tmpSrc, *tmpGroup);
|
2007-03-22 10:30:00 -07:00
|
|
|
tmpDest += *tmpGroup;
|
|
|
|
tmpSrc += *tmpGroup;
|
|
|
|
if (--nrepeat < 0)
|
|
|
|
tmpGroup--;
|
|
|
|
}
|
|
|
|
|
2008-07-24 12:43:40 -07:00
|
|
|
if (*nint == '.') {
|
2010-12-31 11:30:24 -08:00
|
|
|
JS_ASSERT(tmpDest - buf + ptrdiff_t(decimalLength) <= buflen);
|
2007-03-22 10:30:00 -07:00
|
|
|
strcpy(tmpDest, rt->decimalSeparator);
|
|
|
|
tmpDest += decimalLength;
|
2010-12-31 11:30:24 -08:00
|
|
|
JS_ASSERT(tmpDest - buf + ptrdiff_t(strlen(nint + 1)) <= buflen);
|
2008-07-24 12:43:40 -07:00
|
|
|
strcpy(tmpDest, nint + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-12-31 11:30:24 -08:00
|
|
|
JS_ASSERT(tmpDest - buf + ptrdiff_t(strlen(nint)) <= buflen);
|
2008-07-24 12:43:40 -07:00
|
|
|
strcpy(tmpDest, nint);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-12-09 13:24:32 -08:00
|
|
|
if (cx->localeCallbacks && cx->localeCallbacks->localeToUnicode) {
|
2012-07-06 17:45:14 -07:00
|
|
|
Rooted<Value> v(cx, StringValue(str));
|
|
|
|
bool ok = !!cx->localeCallbacks->localeToUnicode(cx, buf, v.address());
|
|
|
|
if (ok)
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().set(v);
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(buf);
|
2010-12-09 13:24:32 -08:00
|
|
|
return ok;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-22 19:32:05 -08:00
|
|
|
str = js_NewStringCopyN(cx, buf, buflen);
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(buf);
|
2010-12-09 02:22:15 -08:00
|
|
|
if (!str)
|
2012-07-06 17:30:06 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().setString(str);
|
2012-07-06 17:30:06 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JSBool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toLocaleString(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-08 21:02:26 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toLocaleString_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2011-09-08 21:02:26 -07:00
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_valueOf_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
|
|
|
args.rval().setNumber(Extract(args.thisv()));
|
2010-10-12 11:50:02 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
JSBool
|
|
|
|
js_num_valueOf(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_valueOf_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2012-07-06 17:21:18 -07:00
|
|
|
const unsigned MAX_PRECISION = 100;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 17:21:18 -07:00
|
|
|
static bool
|
|
|
|
ComputePrecisionInRange(JSContext *cx, int minPrecision, int maxPrecision, const Value &v,
|
|
|
|
int *precision)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 17:21:18 -07:00
|
|
|
double prec;
|
|
|
|
if (!ToInteger(cx, v, &prec))
|
|
|
|
return false;
|
|
|
|
if (minPrecision <= prec && prec <= maxPrecision) {
|
|
|
|
*precision = int(prec);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-06 17:21:18 -07:00
|
|
|
ToCStringBuf cbuf;
|
|
|
|
char *numStr = IntToCString(&cbuf, *precision);
|
|
|
|
JS_ASSERT(numStr);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr);
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-06 17:21:18 -07:00
|
|
|
static bool
|
|
|
|
DToStrResult(JSContext *cx, double d, JSDToStrMode mode, int precision, CallArgs args)
|
|
|
|
{
|
|
|
|
char buf[DTOSTR_VARIABLE_BUFFER_SIZE(MAX_PRECISION + 1)];
|
|
|
|
char *numStr = js_dtostr(cx->runtime->dtoaState, buf, sizeof buf, mode, precision, d);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2012-07-06 17:21:18 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-24 20:26:34 -07:00
|
|
|
JSString *str = js_NewStringCopyZ(cx, numStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
2012-07-06 17:21:18 -07:00
|
|
|
return false;
|
2012-07-30 04:19:09 -07:00
|
|
|
args.rval().setString(str);
|
2012-07-06 17:21:18 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
/*
|
|
|
|
* In the following three implementations, we allow a larger range of precision
|
|
|
|
* than ECMA requires; this is permitted by ECMA-262.
|
|
|
|
*/
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toFixed_impl(JSContext *cx, CallArgs args)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-03 17:44:22 -07:00
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
2012-07-06 17:21:18 -07:00
|
|
|
|
|
|
|
int precision;
|
|
|
|
if (args.length() == 0) {
|
|
|
|
precision = 0;
|
|
|
|
} else {
|
|
|
|
if (!ComputePrecisionInRange(cx, -20, MAX_PRECISION, args[0], &precision))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
return DToStrResult(cx, Extract(args.thisv()), DTOSTR_FIXED, precision, args);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JSBool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toFixed(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-06 17:21:18 -07:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toFixed_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 17:21:18 -07:00
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toExponential_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
2012-07-06 17:21:18 -07:00
|
|
|
|
|
|
|
JSDToStrMode mode;
|
|
|
|
int precision;
|
|
|
|
if (args.length() == 0) {
|
|
|
|
mode = DTOSTR_STANDARD_EXPONENTIAL;
|
|
|
|
precision = 0;
|
|
|
|
} else {
|
|
|
|
mode = DTOSTR_EXPONENTIAL;
|
|
|
|
if (!ComputePrecisionInRange(cx, 0, MAX_PRECISION, args[0], &precision))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-03 17:44:22 -07:00
|
|
|
return DToStrResult(cx, Extract(args.thisv()), mode, precision + 1, args);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JSBool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toExponential(JSContext *cx, unsigned argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-03-01 10:48:52 -08:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toExponential_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
2012-07-06 17:21:18 -07:00
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toPrecision_impl(JSContext *cx, CallArgs args)
|
|
|
|
{
|
|
|
|
JS_ASSERT(IsNumber(args.thisv()));
|
|
|
|
|
|
|
|
double d = Extract(args.thisv());
|
2012-07-06 17:21:18 -07:00
|
|
|
|
|
|
|
if (!args.hasDefined(0)) {
|
|
|
|
JSString *str = js_NumberToStringWithBase(cx, d, 10);
|
|
|
|
if (!str) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
args.rval().setString(str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSDToStrMode mode;
|
|
|
|
int precision;
|
|
|
|
if (args.length() == 0) {
|
|
|
|
mode = DTOSTR_STANDARD;
|
|
|
|
precision = 0;
|
|
|
|
} else {
|
|
|
|
mode = DTOSTR_PRECISION;
|
|
|
|
if (!ComputePrecisionInRange(cx, 1, MAX_PRECISION, args[0], &precision))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DToStrResult(cx, d, mode, precision, args);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-16 11:40:05 -07:00
|
|
|
JSBool
|
2012-07-03 17:44:22 -07:00
|
|
|
num_toPrecision(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
2012-08-16 11:40:05 -07:00
|
|
|
return CallNonGenericMethod<IsNumber, num_toPrecision_impl>(cx, args);
|
2012-07-03 17:44:22 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSFunctionSpec number_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2011-06-10 19:03:57 -07:00
|
|
|
JS_FN(js_toSource_str, num_toSource, 0, 0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2011-11-22 14:41:42 -08:00
|
|
|
JS_FN(js_toString_str, num_toString, 1, 0),
|
2011-06-10 19:03:57 -07:00
|
|
|
JS_FN(js_toLocaleString_str, num_toLocaleString, 0, 0),
|
|
|
|
JS_FN(js_valueOf_str, js_num_valueOf, 0, 0),
|
|
|
|
JS_FN("toFixed", num_toFixed, 1, 0),
|
|
|
|
JS_FN("toExponential", num_toExponential, 1, 0),
|
|
|
|
JS_FN("toPrecision", num_toPrecision, 1, 0),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2012-06-02 11:16:24 -07:00
|
|
|
|
|
|
|
// ES6 draft ES6 15.7.3.10
|
|
|
|
static JSBool
|
|
|
|
Number_isNaN(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
if (args.length() < 1 || !args[0].isDouble()) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
args.rval().setBoolean(MOZ_DOUBLE_IS_NaN(args[0].toDouble()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:50:20 -07:00
|
|
|
// ES6 draft ES6 15.7.3.11
|
|
|
|
static JSBool
|
|
|
|
Number_isFinite(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
if (args.length() < 1 || !args[0].isNumber()) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
args.rval().setBoolean(args[0].isInt32() ||
|
|
|
|
MOZ_DOUBLE_IS_FINITE(args[0].toDouble()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:50:21 -07:00
|
|
|
// ES6 draft ES6 15.7.3.12
|
|
|
|
static JSBool
|
|
|
|
Number_isInteger(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
if (args.length() < 1 || !args[0].isNumber()) {
|
|
|
|
args.rval().setBoolean(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Value val = args[0];
|
|
|
|
args.rval().setBoolean(val.isInt32() ||
|
|
|
|
(MOZ_DOUBLE_IS_FINITE(val.toDouble()) &&
|
|
|
|
ToInteger(val.toDouble()) == val.toDouble()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ES6 drafult ES6 15.7.3.13
|
|
|
|
static JSBool
|
|
|
|
Number_toInteger(JSContext *cx, unsigned argc, Value *vp)
|
|
|
|
{
|
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
if (args.length() < 1) {
|
|
|
|
args.rval().setInt32(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
double asint;
|
|
|
|
if (!ToInteger(cx, args[0], &asint))
|
|
|
|
return false;
|
|
|
|
args.rval().setNumber(asint);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-02 11:16:24 -07:00
|
|
|
|
|
|
|
static JSFunctionSpec number_static_methods[] = {
|
2012-06-06 19:50:20 -07:00
|
|
|
JS_FN("isFinite", Number_isFinite, 1, 0),
|
2012-06-06 19:50:21 -07:00
|
|
|
JS_FN("isInteger", Number_isInteger, 1, 0),
|
2012-06-02 11:16:24 -07:00
|
|
|
JS_FN("isNaN", Number_isNaN, 1, 0),
|
2012-06-06 19:50:21 -07:00
|
|
|
JS_FN("toInteger", Number_toInteger, 1, 0),
|
2012-06-02 11:16:24 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* NB: Keep this in synch with number_constants[]. */
|
|
|
|
enum nc_slot {
|
|
|
|
NC_NaN,
|
|
|
|
NC_POSITIVE_INFINITY,
|
|
|
|
NC_NEGATIVE_INFINITY,
|
|
|
|
NC_MAX_VALUE,
|
|
|
|
NC_MIN_VALUE,
|
|
|
|
NC_LIMIT
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some to most C compilers forbid spelling these at compile time, or barf
|
2011-06-16 02:42:35 -07:00
|
|
|
* if you try, so all but MAX_VALUE are set up by InitRuntimeNumberState
|
2007-03-22 10:30:00 -07:00
|
|
|
* using union jsdpun.
|
|
|
|
*/
|
|
|
|
static JSConstDoubleSpec number_constants[] = {
|
2012-03-16 16:15:31 -07:00
|
|
|
{0, "NaN", 0,{0,0,0}},
|
2007-03-22 10:30:00 -07:00
|
|
|
{0, "POSITIVE_INFINITY", 0,{0,0,0}},
|
|
|
|
{0, "NEGATIVE_INFINITY", 0,{0,0,0}},
|
|
|
|
{1.7976931348623157E+308, "MAX_VALUE", 0,{0,0,0}},
|
|
|
|
{0, "MIN_VALUE", 0,{0,0,0}},
|
|
|
|
{0,0,0,{0,0,0}}
|
|
|
|
};
|
|
|
|
|
2012-02-24 14:19:52 -08:00
|
|
|
double js_NaN;
|
|
|
|
double js_PositiveInfinity;
|
|
|
|
double js_NegativeInfinity;
|
2009-08-27 11:34:13 -07:00
|
|
|
|
2010-03-22 23:41:52 -07:00
|
|
|
#if (defined __GNUC__ && defined __i386__) || \
|
|
|
|
(defined __SUNPRO_CC && defined __i386)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the exception mask to mask all exceptions and set the FPU precision
|
2009-08-27 11:34:13 -07:00
|
|
|
* to 53 bit mantissa (64 bit doubles).
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2009-08-27 11:34:13 -07:00
|
|
|
inline void FIX_FPU() {
|
|
|
|
short control;
|
|
|
|
asm("fstcw %0" : "=m" (control) : );
|
|
|
|
control &= ~0x300; // Lower bits 8 and 9 (precision control).
|
|
|
|
control |= 0x2f3; // Raise bits 0-5 (exception masks) and 9 (64-bit precision).
|
|
|
|
asm("fldcw %0" : : "m" (control) );
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define FIX_FPU() ((void)0)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-06-16 02:42:35 -07:00
|
|
|
namespace js {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-16 02:42:35 -07:00
|
|
|
bool
|
|
|
|
InitRuntimeNumberState(JSRuntime *rt)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
FIX_FPU();
|
|
|
|
|
2012-01-23 03:43:16 -08:00
|
|
|
double d;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our NaN must be one particular canonical value, because we rely on NaN
|
|
|
|
* encoding for our value representation. See jsval.h.
|
|
|
|
*/
|
|
|
|
d = MOZ_DOUBLE_SPECIFIC_NaN(0, 0x8000000000000ULL);
|
|
|
|
number_constants[NC_NaN].dval = js_NaN = d;
|
|
|
|
rt->NaNValue.setDouble(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-23 03:43:16 -08:00
|
|
|
d = MOZ_DOUBLE_POSITIVE_INFINITY();
|
|
|
|
number_constants[NC_POSITIVE_INFINITY].dval = js_PositiveInfinity = d;
|
|
|
|
rt->positiveInfinityValue.setDouble(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-23 03:43:16 -08:00
|
|
|
d = MOZ_DOUBLE_NEGATIVE_INFINITY();
|
|
|
|
number_constants[NC_NEGATIVE_INFINITY].dval = js_NegativeInfinity = d;
|
|
|
|
rt->negativeInfinityValue.setDouble(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-23 03:43:16 -08:00
|
|
|
number_constants[NC_MIN_VALUE].dval = MOZ_DOUBLE_MIN_VALUE();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-16 02:42:35 -07:00
|
|
|
/* Copy locale-specific separators into the runtime strings. */
|
|
|
|
const char *thousandsSeparator, *decimalPoint, *grouping;
|
|
|
|
#ifdef HAVE_LOCALECONV
|
2009-10-28 04:57:31 -07:00
|
|
|
struct lconv *locale = localeconv();
|
2011-06-16 02:42:35 -07:00
|
|
|
thousandsSeparator = locale->thousands_sep;
|
|
|
|
decimalPoint = locale->decimal_point;
|
|
|
|
grouping = locale->grouping;
|
|
|
|
#else
|
|
|
|
thousandsSeparator = getenv("LOCALE_THOUSANDS_SEP");
|
|
|
|
decimalPoint = getenv("LOCALE_DECIMAL_POINT");
|
|
|
|
grouping = getenv("LOCALE_GROUPING");
|
2010-04-02 15:09:05 -07:00
|
|
|
#endif
|
2011-06-16 02:42:35 -07:00
|
|
|
if (!thousandsSeparator)
|
|
|
|
thousandsSeparator = "'";
|
|
|
|
if (!decimalPoint)
|
|
|
|
decimalPoint = ".";
|
|
|
|
if (!grouping)
|
|
|
|
grouping = "\3\0";
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-16 02:42:35 -07:00
|
|
|
/*
|
|
|
|
* We use single malloc to get the memory for all separator and grouping
|
|
|
|
* strings.
|
|
|
|
*/
|
|
|
|
size_t thousandsSeparatorSize = strlen(thousandsSeparator) + 1;
|
|
|
|
size_t decimalPointSize = strlen(decimalPoint) + 1;
|
|
|
|
size_t groupingSize = strlen(grouping) + 1;
|
|
|
|
|
2012-08-31 15:10:10 -07:00
|
|
|
char *storage = js_pod_malloc<char>(thousandsSeparatorSize +
|
|
|
|
decimalPointSize +
|
|
|
|
groupingSize);
|
2011-06-16 02:42:35 -07:00
|
|
|
if (!storage)
|
|
|
|
return false;
|
|
|
|
|
2012-01-17 14:32:36 -08:00
|
|
|
js_memcpy(storage, thousandsSeparator, thousandsSeparatorSize);
|
2011-06-16 02:42:35 -07:00
|
|
|
rt->thousandsSeparator = storage;
|
|
|
|
storage += thousandsSeparatorSize;
|
|
|
|
|
2012-01-17 14:32:36 -08:00
|
|
|
js_memcpy(storage, decimalPoint, decimalPointSize);
|
2011-06-16 02:42:35 -07:00
|
|
|
rt->decimalSeparator = storage;
|
|
|
|
storage += decimalPointSize;
|
|
|
|
|
2012-01-17 14:32:36 -08:00
|
|
|
js_memcpy(storage, grouping, groupingSize);
|
2011-06-16 02:42:35 -07:00
|
|
|
rt->numGrouping = grouping;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-16 02:42:35 -07:00
|
|
|
FinishRuntimeNumberState(JSRuntime *rt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-16 02:42:35 -07:00
|
|
|
/*
|
|
|
|
* The free also releases the memory for decimalSeparator and numGrouping
|
|
|
|
* strings.
|
|
|
|
*/
|
|
|
|
char *storage = const_cast<char *>(rt->thousandsSeparator);
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(storage);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-16 02:42:35 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *
|
|
|
|
js_InitNumberClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2011-05-02 14:03:47 -07:00
|
|
|
JS_ASSERT(obj->isNative());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* XXX must do at least once per new thread, so do it per JSContext... */
|
|
|
|
FIX_FPU();
|
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
Rooted<GlobalObject*> global(cx, &obj->asGlobal());
|
2011-05-02 14:03:47 -07:00
|
|
|
|
2012-05-24 16:05:18 -07:00
|
|
|
RootedObject numberProto(cx, global->createBlankPrototype(cx, &NumberClass));
|
2011-05-02 14:03:47 -07:00
|
|
|
if (!numberProto)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2012-01-02 15:02:05 -08:00
|
|
|
numberProto->asNumber().setPrimitiveValue(0);
|
2011-04-03 13:14:38 -07:00
|
|
|
|
2012-09-06 13:48:40 -07:00
|
|
|
RootedFunction ctor(cx);
|
2012-09-11 10:32:33 -07:00
|
|
|
ctor = global->createConstructor(cx, Number, cx->names().Number, 1);
|
2011-05-02 14:03:47 -07:00
|
|
|
if (!ctor)
|
2011-04-03 13:14:38 -07:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-02 14:03:47 -07:00
|
|
|
if (!LinkConstructorAndPrototype(cx, ctor, numberProto))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Add numeric constants (MAX_VALUE, NaN, &c.) to the Number constructor. */
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!JS_DefineConstDoubles(cx, ctor, number_constants))
|
|
|
|
return NULL;
|
|
|
|
|
2012-06-02 11:16:24 -07:00
|
|
|
if (!DefinePropertiesAndBrand(cx, ctor, NULL, number_static_methods))
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-02 14:03:47 -07:00
|
|
|
if (!DefinePropertiesAndBrand(cx, numberProto, NULL, number_methods))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
|
2011-05-02 14:03:47 -07:00
|
|
|
if (!JS_DefineFunctions(cx, global, number_functions))
|
|
|
|
return NULL;
|
|
|
|
|
2012-07-30 04:19:09 -07:00
|
|
|
RootedValue valueNaN(cx, cx->runtime->NaNValue);
|
|
|
|
RootedValue valueInfinity(cx, cx->runtime->positiveInfinityValue);
|
|
|
|
|
2011-05-02 14:03:47 -07:00
|
|
|
/* ES5 15.1.1.1, 15.1.1.2 */
|
2012-09-11 10:32:33 -07:00
|
|
|
if (!DefineNativeProperty(cx, global, cx->names().NaN, valueNaN,
|
2012-07-30 04:19:09 -07:00
|
|
|
JS_PropertyStub, JS_StrictPropertyStub,
|
2011-05-02 14:03:47 -07:00
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY, 0, 0) ||
|
2012-09-11 10:32:33 -07:00
|
|
|
!DefineNativeProperty(cx, global, cx->names().Infinity, valueInfinity,
|
2011-05-02 14:03:47 -07:00
|
|
|
JS_PropertyStub, JS_StrictPropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY, 0, 0))
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-02 14:03:47 -07:00
|
|
|
|
|
|
|
if (!DefineConstructorAndPrototype(cx, global, JSProto_Number, ctor, numberProto))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return numberProto;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-13 13:08:25 -07:00
|
|
|
namespace js {
|
|
|
|
|
2009-01-08 15:09:16 -08:00
|
|
|
static char *
|
2012-03-01 18:54:01 -08:00
|
|
|
FracNumberToCString(JSContext *cx, ToCStringBuf *cbuf, double d, int base = 10)
|
2010-09-13 13:08:25 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2010-09-13 14:52:33 -07:00
|
|
|
int32_t _;
|
2012-01-23 03:43:16 -08:00
|
|
|
JS_ASSERT(!MOZ_DOUBLE_IS_INT32(d, &_));
|
2010-09-13 13:08:25 -07:00
|
|
|
}
|
|
|
|
#endif
|
2010-09-15 11:49:12 -07:00
|
|
|
|
|
|
|
char* numStr;
|
|
|
|
if (base == 10) {
|
|
|
|
/*
|
|
|
|
* This is V8's implementation of the algorithm described in the
|
|
|
|
* following paper:
|
|
|
|
*
|
2011-06-16 02:42:35 -07:00
|
|
|
* Printing floating-point numbers quickly and accurately with integers.
|
2010-09-15 11:49:12 -07:00
|
|
|
* Florian Loitsch, PLDI 2010.
|
|
|
|
*/
|
2012-04-09 09:05:11 -07:00
|
|
|
const double_conversion::DoubleToStringConverter &converter
|
|
|
|
= double_conversion::DoubleToStringConverter::EcmaScriptConverter();
|
|
|
|
double_conversion::StringBuilder builder(cbuf->sbuf, cbuf->sbufSize);
|
|
|
|
converter.ToShortest(d, &builder);
|
|
|
|
numStr = builder.Finalize();
|
2010-09-15 11:49:12 -07:00
|
|
|
} else {
|
2011-07-18 14:54:48 -07:00
|
|
|
numStr = cbuf->dbuf = js_dtobasestr(cx->runtime->dtoaState, base, d);
|
2010-09-15 11:49:12 -07:00
|
|
|
}
|
|
|
|
return numStr;
|
2010-09-13 13:08:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2012-03-01 18:54:01 -08:00
|
|
|
NumberToCString(JSContext *cx, ToCStringBuf *cbuf, double d, int base/* = 10*/)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
int32_t i;
|
2012-01-23 03:43:16 -08:00
|
|
|
return MOZ_DOUBLE_IS_INT32(d, &i)
|
2010-09-13 13:08:25 -07:00
|
|
|
? IntToCString(cbuf, i, base)
|
|
|
|
: FracNumberToCString(cx, cbuf, d, base);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-28 23:09:21 -08:00
|
|
|
}
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
static JSString * JS_FASTCALL
|
2012-03-01 18:54:01 -08:00
|
|
|
js_NumberToStringWithBase(JSContext *cx, double d, int base)
|
2007-11-28 23:09:21 -08:00
|
|
|
{
|
2010-09-13 13:08:25 -07:00
|
|
|
ToCStringBuf cbuf;
|
2007-11-28 23:09:21 -08:00
|
|
|
char *numStr;
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
/*
|
|
|
|
* Caller is responsible for error reporting. When called from trace,
|
|
|
|
* returning NULL here will cause us to fall of trace and then retry
|
|
|
|
* from the interpreter (which will report the error).
|
|
|
|
*/
|
2008-10-13 19:07:30 -07:00
|
|
|
if (base < 2 || base > 36)
|
|
|
|
return NULL;
|
2009-10-14 13:25:20 -07:00
|
|
|
|
2011-01-24 16:30:16 -08:00
|
|
|
JSCompartment *c = cx->compartment;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
int32_t i;
|
2012-01-23 03:43:16 -08:00
|
|
|
if (MOZ_DOUBLE_IS_INT32(d, &i)) {
|
2011-09-20 14:47:14 -07:00
|
|
|
if (base == 10 && StaticStrings::hasInt(i))
|
|
|
|
return cx->runtime->staticStrings.getInt(i);
|
2012-03-05 18:43:45 -08:00
|
|
|
if (unsigned(i) < unsigned(base)) {
|
2009-10-14 13:25:20 -07:00
|
|
|
if (i < 10)
|
2011-09-20 14:47:14 -07:00
|
|
|
return cx->runtime->staticStrings.getInt(i);
|
2011-03-14 13:59:53 -07:00
|
|
|
jschar c = 'a' + i - 10;
|
2011-09-20 14:47:14 -07:00
|
|
|
JS_ASSERT(StaticStrings::hasUnit(c));
|
|
|
|
return cx->runtime->staticStrings.getUnit(c);
|
2009-10-14 13:25:20 -07:00
|
|
|
}
|
2010-09-13 13:08:25 -07:00
|
|
|
|
2011-03-14 13:59:53 -07:00
|
|
|
if (JSFlatString *str = c->dtoaCache.lookup(base, d))
|
2011-01-24 16:30:16 -08:00
|
|
|
return str;
|
2010-09-13 13:08:25 -07:00
|
|
|
|
|
|
|
numStr = IntToCString(&cbuf, i, base);
|
|
|
|
JS_ASSERT(!cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize);
|
|
|
|
} else {
|
2011-03-14 13:59:53 -07:00
|
|
|
if (JSFlatString *str = c->dtoaCache.lookup(base, d))
|
2011-01-24 16:30:16 -08:00
|
|
|
return str;
|
2010-09-13 13:08:25 -07:00
|
|
|
|
|
|
|
numStr = FracNumberToCString(cx, &cbuf, d, base);
|
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
JS_ASSERT_IF(base == 10,
|
|
|
|
!cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize);
|
|
|
|
JS_ASSERT_IF(base != 10,
|
|
|
|
cbuf.dbuf && cbuf.dbuf == numStr);
|
2009-10-14 13:25:20 -07:00
|
|
|
}
|
2010-09-13 13:08:25 -07:00
|
|
|
|
2012-08-28 10:28:19 -07:00
|
|
|
JSFlatString *s = js_NewStringCopyZ(cx, numStr);
|
2011-01-24 16:30:16 -08:00
|
|
|
c->dtoaCache.cache(base, d, s);
|
2009-01-08 15:09:16 -08:00
|
|
|
return s;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 10:28:02 -08:00
|
|
|
JSString *
|
2012-02-24 14:19:52 -08:00
|
|
|
js_NumberToString(JSContext *cx, double d)
|
2008-10-13 19:07:30 -07:00
|
|
|
{
|
2009-10-14 13:25:20 -07:00
|
|
|
return js_NumberToStringWithBase(cx, d, 10);
|
2008-10-13 19:07:30 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 15:28:58 -08:00
|
|
|
namespace js {
|
|
|
|
|
2012-08-28 10:28:19 -07:00
|
|
|
JSFlatString *
|
2012-02-24 14:19:52 -08:00
|
|
|
NumberToString(JSContext *cx, double d)
|
2010-12-06 10:26:58 -08:00
|
|
|
{
|
|
|
|
if (JSString *str = js_NumberToStringWithBase(cx, d, 10))
|
2012-08-28 10:28:19 -07:00
|
|
|
return &str->asFlat();
|
2010-12-06 10:26:58 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-28 10:28:19 -07:00
|
|
|
JSFlatString *
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
IndexToString(JSContext *cx, uint32_t index)
|
2011-07-27 17:09:12 -07:00
|
|
|
{
|
2011-09-20 14:47:14 -07:00
|
|
|
if (StaticStrings::hasUint(index))
|
|
|
|
return cx->runtime->staticStrings.getUint(index);
|
2011-07-27 17:09:12 -07:00
|
|
|
|
|
|
|
JSCompartment *c = cx->compartment;
|
2012-08-28 10:28:19 -07:00
|
|
|
if (JSFlatString *str = c->dtoaCache.lookup(10, index))
|
2011-07-27 17:09:12 -07:00
|
|
|
return str;
|
|
|
|
|
|
|
|
JSShortString *str = js_NewGCShortString(cx);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2012-08-22 12:19:07 -07:00
|
|
|
jschar buffer[JSShortString::MAX_SHORT_LENGTH + 1];
|
|
|
|
RangedPtr<jschar> end(buffer + JSShortString::MAX_SHORT_LENGTH,
|
|
|
|
buffer, JSShortString::MAX_SHORT_LENGTH + 1);
|
2011-08-04 19:21:25 -07:00
|
|
|
*end = '\0';
|
|
|
|
RangedPtr<jschar> start = BackfillIndexInCharBuffer(index, end);
|
2011-07-27 17:09:12 -07:00
|
|
|
|
2012-08-22 12:19:07 -07:00
|
|
|
jschar *dst = str->init(end - start);
|
|
|
|
PodCopy(dst, start.get(), end - start + 1);
|
2011-07-27 17:09:12 -07:00
|
|
|
|
2011-08-03 12:39:55 -07:00
|
|
|
c->dtoaCache.cache(10, index, str);
|
2011-07-27 17:09:12 -07:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-01-12 15:28:58 -08:00
|
|
|
bool JS_FASTCALL
|
|
|
|
NumberValueToStringBuffer(JSContext *cx, const Value &v, StringBuffer &sb)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
|
|
|
/* Convert to C-string. */
|
2010-09-13 13:08:25 -07:00
|
|
|
ToCStringBuf cbuf;
|
2009-06-30 17:19:42 -07:00
|
|
|
const char *cstr;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isInt32()) {
|
2010-09-13 13:08:25 -07:00
|
|
|
cstr = IntToCString(&cbuf, v.toInt32());
|
2009-06-30 17:19:42 -07:00
|
|
|
} else {
|
2010-09-13 13:08:25 -07:00
|
|
|
cstr = NumberToCString(cx, &cbuf, v.toDouble());
|
|
|
|
if (!cstr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inflate to jschar string. The input C-string characters are < 127, so
|
|
|
|
* even if jschars are UTF-8, all chars should map to one jschar.
|
|
|
|
*/
|
|
|
|
size_t cstrlen = strlen(cstr);
|
2010-09-13 13:08:25 -07:00
|
|
|
JS_ASSERT(!cbuf.dbuf && cstrlen < cbuf.sbufSize);
|
2011-01-12 15:28:58 -08:00
|
|
|
return sb.appendInflated(cstr, cstrlen);
|
2009-06-30 17:19:42 -07:00
|
|
|
}
|
|
|
|
|
2012-03-28 16:35:21 -07:00
|
|
|
JS_PUBLIC_API(bool)
|
2011-06-13 21:49:59 -07:00
|
|
|
ToNumberSlow(JSContext *cx, Value v, double *out)
|
2010-04-07 13:18:50 -07:00
|
|
|
{
|
2012-04-30 17:10:30 -07:00
|
|
|
#ifdef DEBUG
|
2012-05-03 19:17:19 -07:00
|
|
|
/*
|
|
|
|
* MSVC bizarrely miscompiles this, complaining about the first brace below
|
|
|
|
* being unmatched (!). The error message points at both this opening brace
|
|
|
|
* and at the corresponding SkipRoot constructor. The error seems to derive
|
|
|
|
* from the presence guard-object macros on the SkipRoot class/constructor,
|
|
|
|
* which seems well in the weeds for an unmatched-brace syntax error.
|
|
|
|
* Otherwise the problem is inscrutable, and I haven't found a workaround.
|
|
|
|
* So for now just disable it when compiling with MSVC -- not ideal, but at
|
|
|
|
* least Windows debug shell builds complete again.
|
|
|
|
*/
|
|
|
|
#ifndef _MSC_VER
|
2012-04-30 17:10:30 -07:00
|
|
|
{
|
|
|
|
SkipRoot skip(cx, &v);
|
|
|
|
MaybeCheckStackRoots(cx);
|
|
|
|
}
|
2012-05-03 19:17:19 -07:00
|
|
|
#endif
|
2012-04-30 17:10:30 -07:00
|
|
|
#endif
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!v.isNumber());
|
2010-04-07 13:18:50 -07:00
|
|
|
goto skip_int_double;
|
2008-03-06 15:24:08 -08:00
|
|
|
for (;;) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isNumber()) {
|
|
|
|
*out = v.toNumber();
|
|
|
|
return true;
|
2010-04-07 13:18:50 -07:00
|
|
|
}
|
|
|
|
skip_int_double:
|
2010-12-06 10:26:58 -08:00
|
|
|
if (v.isString())
|
2012-02-24 14:19:52 -08:00
|
|
|
return StringToNumberType<double>(cx, v.toString(), out);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isBoolean()) {
|
|
|
|
if (v.toBoolean()) {
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = 1.0;
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2008-03-10 12:27:44 -07:00
|
|
|
}
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = 0.0;
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2008-03-10 12:27:44 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isNull()) {
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = 0.0;
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2008-03-06 15:24:08 -08:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isUndefined())
|
2008-03-06 15:24:08 -08:00
|
|
|
break;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(v.isObject());
|
2011-04-01 15:24:21 -07:00
|
|
|
if (!ToPrimitive(cx, JSTYPE_NUMBER, &v))
|
2010-07-14 23:19:36 -07:00
|
|
|
return false;
|
|
|
|
if (v.isObject())
|
2008-03-06 15:24:08 -08:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = js_NaN;
|
2010-07-14 23:19:36 -07:00
|
|
|
return true;
|
2010-04-07 13:18:50 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-03 15:15:04 -07:00
|
|
|
/*
|
|
|
|
* Convert a value to an int64_t, according to the WebIDL rules for long long
|
|
|
|
* conversion. Return converted value in *out on success, false on failure.
|
|
|
|
*/
|
|
|
|
JS_PUBLIC_API(bool)
|
|
|
|
ToInt64Slow(JSContext *cx, const Value &v, int64_t *out)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!v.isInt32());
|
|
|
|
double d;
|
|
|
|
if (v.isDouble()) {
|
|
|
|
d = v.toDouble();
|
|
|
|
} else {
|
|
|
|
if (!ToNumberSlow(cx, v, &d))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out = ToInt64(d);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a value to an uint64_t, according to the WebIDL rules for unsigned long long
|
|
|
|
* conversion. Return converted value in *out on success, false on failure.
|
|
|
|
*/
|
|
|
|
JS_PUBLIC_API(bool)
|
|
|
|
ToUint64Slow(JSContext *cx, const Value &v, uint64_t *out)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!v.isInt32());
|
|
|
|
double d;
|
|
|
|
if (v.isDouble()) {
|
|
|
|
d = v.toDouble();
|
|
|
|
} else {
|
|
|
|
if (!ToNumberSlow(cx, v, &d))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*out = ToUint64(d);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-28 16:35:21 -07:00
|
|
|
JS_PUBLIC_API(bool)
|
2011-12-05 14:10:02 -08:00
|
|
|
ToInt32Slow(JSContext *cx, const Value &v, int32_t *out)
|
2010-04-07 13:18:50 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!v.isInt32());
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isDouble()) {
|
|
|
|
d = v.toDouble();
|
2008-03-06 13:40:43 -08:00
|
|
|
} else {
|
2011-06-13 21:49:59 -07:00
|
|
|
if (!ToNumberSlow(cx, v, &d))
|
2010-04-07 13:18:50 -07:00
|
|
|
return false;
|
2008-03-06 13:40:43 -08:00
|
|
|
}
|
2012-04-19 16:18:24 -07:00
|
|
|
*out = ToInt32(d);
|
2010-04-07 13:18:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-02 12:59:30 -07:00
|
|
|
JS_PUBLIC_API(bool)
|
2011-12-05 14:10:02 -08:00
|
|
|
ToUint32Slow(JSContext *cx, const Value &v, uint32_t *out)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!v.isInt32());
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isDouble()) {
|
|
|
|
d = v.toDouble();
|
2008-03-06 13:40:43 -08:00
|
|
|
} else {
|
2011-06-13 21:49:59 -07:00
|
|
|
if (!ToNumberSlow(cx, v, &d))
|
2010-04-07 13:18:50 -07:00
|
|
|
return false;
|
2008-03-06 13:40:43 -08:00
|
|
|
}
|
2012-04-19 16:18:24 -07:00
|
|
|
*out = ToUint32(d);
|
2010-04-07 13:18:50 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-02 12:59:30 -07:00
|
|
|
JS_PUBLIC_API(bool)
|
|
|
|
ToUint16Slow(JSContext *cx, const Value &v, uint16_t *out)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!v.isInt32());
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isDouble()) {
|
|
|
|
d = v.toDouble();
|
2011-06-13 21:49:59 -07:00
|
|
|
} else if (!ToNumberSlow(cx, v, &d)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
return false;
|
2008-03-10 12:27:44 -07:00
|
|
|
}
|
2010-04-07 13:18:50 -07:00
|
|
|
|
2012-01-23 03:43:16 -08:00
|
|
|
if (d == 0 || !MOZ_DOUBLE_IS_FINITE(d)) {
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
uint16_t u = (uint16_t) d;
|
2012-02-24 14:19:52 -08:00
|
|
|
if ((double)u == d) {
|
2010-04-07 13:18:50 -07:00
|
|
|
*out = u;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool neg = (d < 0);
|
|
|
|
d = floor(neg ? -d : d);
|
|
|
|
d = neg ? -d : d;
|
2012-03-05 18:43:45 -08:00
|
|
|
unsigned m = JS_BIT(16);
|
2010-04-07 13:18:50 -07:00
|
|
|
d = fmod(d, (double) m);
|
|
|
|
if (d < 0)
|
|
|
|
d += m;
|
|
|
|
*out = (uint16_t) d;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-07 13:18:50 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2007-08-04 12:05:16 -07:00
|
|
|
js_strtod(JSContext *cx, const jschar *s, const jschar *send,
|
2012-02-24 14:19:52 -08:00
|
|
|
const jschar **ep, double *dp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-07-26 14:10:33 -07:00
|
|
|
size_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
char cbuf[32];
|
|
|
|
char *cstr, *istr, *estr;
|
|
|
|
JSBool negative;
|
2012-02-24 14:19:52 -08:00
|
|
|
double d;
|
2007-08-04 12:05:16 -07:00
|
|
|
|
2011-07-26 14:10:33 -07:00
|
|
|
const jschar *s1 = SkipSpace(s, send);
|
|
|
|
size_t length = send - s1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Use cbuf to avoid malloc */
|
|
|
|
if (length >= sizeof cbuf) {
|
2011-03-31 01:14:12 -07:00
|
|
|
cstr = (char *) cx->malloc_(length + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cstr)
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
cstr = cbuf;
|
|
|
|
}
|
|
|
|
|
2007-08-04 12:05:16 -07:00
|
|
|
for (i = 0; i != length; i++) {
|
|
|
|
if (s1[i] >> 8)
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
cstr[i] = (char)s1[i];
|
|
|
|
}
|
2007-08-04 12:05:16 -07:00
|
|
|
cstr[i] = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
istr = cstr;
|
|
|
|
if ((negative = (*istr == '-')) != 0 || *istr == '+')
|
|
|
|
istr++;
|
2012-03-16 16:15:31 -07:00
|
|
|
if (*istr == 'I' && !strncmp(istr, "Infinity", 8)) {
|
2009-10-28 04:57:31 -07:00
|
|
|
d = negative ? js_NegativeInfinity : js_PositiveInfinity;
|
2007-03-22 10:30:00 -07:00
|
|
|
estr = istr + 8;
|
|
|
|
} else {
|
|
|
|
int err;
|
2011-07-18 14:54:48 -07:00
|
|
|
d = js_strtod_harder(cx->runtime->dtoaState, cstr, &estr, &err);
|
2008-08-05 11:18:29 -07:00
|
|
|
if (d == HUGE_VAL)
|
2009-10-28 04:57:31 -07:00
|
|
|
d = js_PositiveInfinity;
|
2008-08-05 11:18:29 -07:00
|
|
|
else if (d == -HUGE_VAL)
|
2009-10-28 04:57:31 -07:00
|
|
|
d = js_NegativeInfinity;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
i = estr - cstr;
|
|
|
|
if (cstr != cbuf)
|
2012-08-31 15:01:33 -07:00
|
|
|
js_free(cstr);
|
2007-03-22 10:30:00 -07:00
|
|
|
*ep = i ? s1 + i : s;
|
|
|
|
*dp = d;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|