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
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* IBM Corp.
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2009-07-15 15:59:25 -07:00
|
|
|
#define __STDC_LIMIT_MACROS
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* JS number type and wrapper class.
|
|
|
|
*/
|
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>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsatom.h"
|
2008-10-08 15:08:33 -07:00
|
|
|
#include "jsbuiltins.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#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"
|
2009-09-05 21:48:30 -07:00
|
|
|
#include "jsstrinlines.h"
|
2009-06-30 17:19:42 -07:00
|
|
|
#include "jsvector.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-15 15:59:25 -07:00
|
|
|
|
2009-09-14 23:00:15 -07:00
|
|
|
#ifndef JS_HAVE_STDINT_H /* Native support is innocent until proven guilty. */
|
|
|
|
|
2009-07-15 15:59:25 -07:00
|
|
|
JS_STATIC_ASSERT(uint8_t(-1) == UINT8_MAX);
|
|
|
|
JS_STATIC_ASSERT(uint16_t(-1) == UINT16_MAX);
|
|
|
|
JS_STATIC_ASSERT(uint32_t(-1) == UINT32_MAX);
|
|
|
|
JS_STATIC_ASSERT(uint64_t(-1) == UINT64_MAX);
|
|
|
|
|
|
|
|
JS_STATIC_ASSERT(INT8_MAX > INT8_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uint8_t(INT8_MAX) + uint8_t(1) == uint8_t(INT8_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
JS_STATIC_ASSERT(INT16_MAX > INT16_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uint16_t(INT16_MAX) + uint16_t(1) == uint16_t(INT16_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
JS_STATIC_ASSERT(INT32_MAX > INT32_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uint32_t(INT32_MAX) + uint32_t(1) == uint32_t(INT32_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
JS_STATIC_ASSERT(INT64_MAX > INT64_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uint64_t(INT64_MAX) + uint64_t(1) == uint64_t(INT64_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
|
|
|
|
JS_STATIC_ASSERT(INTPTR_MAX > INTPTR_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uintptr_t(INTPTR_MAX) + uintptr_t(1) == uintptr_t(INTPTR_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
JS_STATIC_ASSERT(uintptr_t(-1) == UINTPTR_MAX);
|
|
|
|
JS_STATIC_ASSERT(size_t(-1) == SIZE_MAX);
|
|
|
|
JS_STATIC_ASSERT(PTRDIFF_MAX > PTRDIFF_MIN);
|
|
|
|
JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MAX) == PTRDIFF_MAX);
|
|
|
|
JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MIN) == PTRDIFF_MIN);
|
2009-09-15 12:15:10 -07:00
|
|
|
JS_STATIC_ASSERT(uintptr_t(PTRDIFF_MAX) + uintptr_t(1) == uintptr_t(PTRDIFF_MIN));
|
2009-07-15 15:59:25 -07:00
|
|
|
|
2009-09-14 23:00:15 -07:00
|
|
|
#endif /* JS_HAVE_STDINT_H */
|
2009-07-15 15:59:25 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_isNaN(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsdouble x;
|
|
|
|
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
*vp = JSVAL_TRUE;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
x = js_ValueToNumber(cx, &vp[2]);
|
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = BOOLEAN_TO_JSVAL(JSDOUBLE_IS_NaN(x));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_isFinite(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsdouble x;
|
|
|
|
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
*vp = JSVAL_FALSE;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
x = js_ValueToNumber(cx, &vp[2]);
|
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = BOOLEAN_TO_JSVAL(JSDOUBLE_IS_FINITE(x));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
|
|
|
num_parseFloat(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
jsdouble 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) {
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
str = js_ValueToString(cx, vp[2]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(bp, end);
|
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) {
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-03-10 12:27:44 -07:00
|
|
|
return js_NewNumberInRootedValue(cx, d, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
#ifdef JS_TRACER
|
2008-10-16 12:24:10 -07:00
|
|
|
static jsdouble FASTCALL
|
|
|
|
ParseFloat(JSContext* cx, JSString* str)
|
2008-10-08 15:08:33 -07:00
|
|
|
{
|
|
|
|
const jschar* bp;
|
|
|
|
const jschar* end;
|
|
|
|
const jschar* ep;
|
|
|
|
jsdouble d;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(bp, end);
|
2008-10-08 15:08:33 -07:00
|
|
|
if (!js_strtod(cx, bp, end, &ep, &d) || ep == bp)
|
|
|
|
return js_NaN;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* See ECMA 15.1.2.2. */
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
|
|
|
num_parseInt(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsint radix;
|
|
|
|
JSString *str;
|
|
|
|
jsdouble 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) {
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc > 1) {
|
2008-03-06 13:40:43 -08:00
|
|
|
radix = js_ValueToECMAInt32(cx, &vp[3]);
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(vp[3]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
radix = 0;
|
|
|
|
}
|
|
|
|
if (radix != 0 && (radix < 2 || radix > 36)) {
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-05 11:11:54 -08:00
|
|
|
if (JSVAL_IS_INT(vp[2]) && (radix == 0 || radix == 10)) {
|
|
|
|
*vp = vp[2];
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
str = js_ValueToString(cx, vp[2]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(bp, end);
|
2007-08-15 23:23:06 -07:00
|
|
|
if (!js_strtointeger(cx, bp, end, &ep, radix, &d))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (ep == bp) {
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-03-10 12:27:44 -07:00
|
|
|
return js_NewNumberInRootedValue(cx, d, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
#ifdef JS_TRACER
|
2008-10-16 12:24:10 -07:00
|
|
|
static jsdouble FASTCALL
|
|
|
|
ParseInt(JSContext* cx, JSString* str)
|
2008-10-08 15:08:33 -07:00
|
|
|
{
|
|
|
|
const jschar* bp;
|
|
|
|
const jschar* end;
|
|
|
|
const jschar* ep;
|
|
|
|
jsdouble d;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(bp, end);
|
2008-10-08 15:08:33 -07:00
|
|
|
if (!js_strtointeger(cx, bp, end, &ep, 0, &d) || ep == bp)
|
|
|
|
return js_NaN;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2008-10-16 12:24:10 -07:00
|
|
|
static jsdouble FASTCALL
|
|
|
|
ParseIntDouble(jsdouble d)
|
2008-10-08 15:08:33 -07:00
|
|
|
{
|
|
|
|
if (!JSDOUBLE_IS_FINITE(d))
|
|
|
|
return js_NaN;
|
2009-08-10 18:09:51 -07:00
|
|
|
if (d > 0)
|
|
|
|
return floor(d);
|
2009-08-11 12:54:55 -07:00
|
|
|
if (d < 0)
|
|
|
|
return -floor(-d);
|
|
|
|
return 0;
|
2008-10-08 15:08:33 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
const char js_Infinity_str[] = "Infinity";
|
|
|
|
const char js_NaN_str[] = "NaN";
|
|
|
|
const char js_isNaN_str[] = "isNaN";
|
|
|
|
const char js_isFinite_str[] = "isFinite";
|
|
|
|
const char js_parseFloat_str[] = "parseFloat";
|
|
|
|
const char js_parseInt_str[] = "parseInt";
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
|
2008-10-21 15:58:06 -07:00
|
|
|
JS_DEFINE_TRCINFO_2(num_parseInt,
|
2010-03-01 18:47:17 -08:00
|
|
|
(2, (static, DOUBLE, ParseInt, CONTEXT, STRING, 1, nanojit::ACC_NONE)),
|
|
|
|
(1, (static, DOUBLE, ParseIntDouble, DOUBLE, 1, nanojit::ACC_NONE)))
|
2008-10-08 15:08:33 -07:00
|
|
|
|
2008-10-21 15:58:06 -07:00
|
|
|
JS_DEFINE_TRCINFO_1(num_parseFloat,
|
2010-03-01 18:47:17 -08:00
|
|
|
(2, (static, DOUBLE, ParseFloat, CONTEXT, STRING, 1, nanojit::ACC_NONE)))
|
2008-10-08 15:08:33 -07:00
|
|
|
|
|
|
|
#endif /* JS_TRACER */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSFunctionSpec number_functions[] = {
|
2008-10-21 15:58:06 -07:00
|
|
|
JS_FN(js_isNaN_str, num_isNaN, 1,0),
|
|
|
|
JS_FN(js_isFinite_str, num_isFinite, 1,0),
|
2009-09-04 13:44:31 -07:00
|
|
|
JS_TN(js_parseFloat_str, num_parseFloat, 1,0, &num_parseFloat_trcinfo),
|
|
|
|
JS_TN(js_parseInt_str, num_parseInt, 2,0, &num_parseInt_trcinfo),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSClass js_NumberClass = {
|
|
|
|
js_Number_str,
|
2009-08-04 14:06:55 -07:00
|
|
|
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Number),
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
2009-07-22 09:23:21 -07:00
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
|
2007-03-22 10:30:00 -07:00
|
|
|
JSCLASS_NO_OPTIONAL_MEMBERS
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Number(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
2008-02-03 19:41:31 -08:00
|
|
|
jsval v;
|
2008-03-10 12:27:44 -07:00
|
|
|
jsdouble d;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (argc != 0) {
|
2008-03-06 15:24:08 -08:00
|
|
|
d = js_ValueToNumber(cx, &argv[0]);
|
2008-03-10 12:27:44 -07:00
|
|
|
v = argv[0];
|
|
|
|
if (JSVAL_IS_NULL(v))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2008-03-10 12:27:44 -07:00
|
|
|
if (v != JSVAL_TRUE) {
|
|
|
|
JS_ASSERT(JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v));
|
|
|
|
} else {
|
|
|
|
if (!js_NewNumberInRootedValue(cx, d, &argv[0]))
|
|
|
|
return JS_FALSE;
|
|
|
|
v = argv[0];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-03-10 12:27:44 -07:00
|
|
|
v = JSVAL_ZERO;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-08-04 14:06:55 -07:00
|
|
|
if (!JS_IsConstructing(cx))
|
2008-02-03 19:41:31 -08:00
|
|
|
*rval = v;
|
2009-08-04 14:06:55 -07:00
|
|
|
else
|
2009-09-05 08:59:11 -07:00
|
|
|
obj->fslots[JSSLOT_PRIMITIVE_THIS] = v;
|
2009-08-04 14:06:55 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_toSource(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
jsdouble d;
|
|
|
|
char numBuf[DTOSTR_STANDARD_BUFFER_SIZE], *numStr;
|
|
|
|
char buf[64];
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(JSVAL_IS_NUMBER(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
d = JSVAL_IS_INT(v) ? (jsdouble)JSVAL_TO_INT(v) : *JSVAL_TO_DOUBLE(v);
|
|
|
|
numStr = JS_dtostr(numBuf, sizeof numBuf, DTOSTR_STANDARD, 0, d);
|
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
JS_snprintf(buf, sizeof buf, "(new %s(%s))", js_NumberClass.name, numStr);
|
|
|
|
str = JS_NewStringCopyZ(cx, buf);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The buf must be big enough for MIN_INT to fit including '-' and '\0'. */
|
2009-01-08 15:09:16 -08:00
|
|
|
static char *
|
|
|
|
IntToCString(jsint i, jsint base, char *buf, size_t bufSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
jsuint u;
|
|
|
|
|
|
|
|
u = (i < 0) ? -i : i;
|
|
|
|
|
|
|
|
cp = buf + bufSize; /* one past last buffer cell */
|
|
|
|
*--cp = '\0'; /* null terminate the string to be */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build the string from behind. We use multiply and subtraction
|
|
|
|
* instead of modulus because that's much faster.
|
|
|
|
*/
|
2008-10-13 19:07:30 -07:00
|
|
|
switch (base) {
|
|
|
|
case 10:
|
|
|
|
do {
|
|
|
|
jsuint newu = u / 10;
|
|
|
|
*--cp = (char)(u - newu * 10) + '0';
|
|
|
|
u = newu;
|
|
|
|
} while (u != 0);
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
do {
|
|
|
|
jsuint newu = u / 16;
|
|
|
|
*--cp = "0123456789abcdef"[u - newu * 16];
|
|
|
|
u = newu;
|
|
|
|
} while (u != 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
JS_ASSERT(base >= 2 && base <= 36);
|
|
|
|
do {
|
|
|
|
jsuint newu = u / base;
|
|
|
|
*--cp = "0123456789abcdefghijklmnopqrstuvwxyz"[u - newu * base];
|
|
|
|
u = newu;
|
|
|
|
} while (u != 0);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i < 0)
|
|
|
|
*--cp = '-';
|
|
|
|
|
2007-11-28 23:09:21 -08:00
|
|
|
JS_ASSERT(cp >= buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
static JSString * JS_FASTCALL
|
|
|
|
js_NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base);
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
|
|
|
num_toString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
jsdouble d;
|
|
|
|
jsint base;
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(JSVAL_IS_NUMBER(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
d = JSVAL_IS_INT(v) ? (jsdouble)JSVAL_TO_INT(v) : *JSVAL_TO_DOUBLE(v);
|
|
|
|
base = 10;
|
2008-01-14 20:16:08 -08:00
|
|
|
if (argc != 0 && !JSVAL_IS_VOID(vp[2])) {
|
2008-03-06 13:40:43 -08:00
|
|
|
base = js_ValueToECMAInt32(cx, &vp[2]);
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (base < 2 || base > 36) {
|
|
|
|
char numBuf[12];
|
2009-01-08 15:09:16 -08:00
|
|
|
char *numStr = IntToCString(base, 10, numBuf, sizeof numBuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_RADIX,
|
|
|
|
numStr);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2009-10-14 13:25:20 -07:00
|
|
|
str = js_NumberToStringWithBase(cx, d, base);
|
|
|
|
if (!str) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-10-14 13:25:20 -07:00
|
|
|
}
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_toLocaleString(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
char thousandsLength, decimalLength;
|
|
|
|
const char *numGrouping, *tmpGroup;
|
|
|
|
JSRuntime *rt;
|
|
|
|
JSString *numStr, *str;
|
|
|
|
const char *num, *end, *tmpSrc;
|
2007-09-20 11:58:01 -07:00
|
|
|
char *buf, *tmpDest;
|
2008-07-24 12:43:40 -07:00
|
|
|
const char *nint;
|
2007-03-22 10:30:00 -07:00
|
|
|
int digits, size, remainder, nrepeat;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the string, move back to bytes to make string twiddling
|
|
|
|
* a bit easier and so we can insert platform charset seperators.
|
|
|
|
*/
|
2008-10-08 15:08:33 -07:00
|
|
|
if (!num_toString(cx, 0, vp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_ASSERT(JSVAL_IS_STRING(*vp));
|
|
|
|
numStr = JSVAL_TO_STRING(*vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
num = js_GetStringBytes(cx, numStr);
|
|
|
|
if (!num)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
nint = num;
|
|
|
|
if (*nint == '-')
|
|
|
|
nint++;
|
|
|
|
while (*nint >= '0' && *nint <= '9')
|
|
|
|
nint++;
|
|
|
|
digits = nint - num;
|
2007-03-22 10:30:00 -07:00
|
|
|
end = num + digits;
|
2008-07-24 12:43:40 -07:00
|
|
|
if (!digits)
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rt = cx->runtime;
|
|
|
|
thousandsLength = strlen(rt->thousandsSeparator);
|
|
|
|
decimalLength = strlen(rt->decimalSeparator);
|
|
|
|
|
|
|
|
/* Figure out how long resulting string will be. */
|
2008-07-24 12:43:40 -07:00
|
|
|
size = digits + (*nint ? strlen(nint + 1) + 1 : 0);
|
|
|
|
if (*nint == '.')
|
|
|
|
size += decimalLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
numGrouping = tmpGroup = rt->numGrouping;
|
|
|
|
remainder = digits;
|
|
|
|
if (*num == '-')
|
|
|
|
remainder--;
|
|
|
|
|
|
|
|
while (*tmpGroup != CHAR_MAX && *tmpGroup != '\0') {
|
|
|
|
if (*tmpGroup >= remainder)
|
|
|
|
break;
|
|
|
|
size += thousandsLength;
|
|
|
|
remainder -= *tmpGroup;
|
|
|
|
tmpGroup++;
|
|
|
|
}
|
|
|
|
if (*tmpGroup == '\0' && *numGrouping != '\0') {
|
|
|
|
nrepeat = (remainder - 1) / tmpGroup[-1];
|
|
|
|
size += thousandsLength * nrepeat;
|
|
|
|
remainder -= nrepeat * tmpGroup[-1];
|
|
|
|
} else {
|
|
|
|
nrepeat = 0;
|
|
|
|
}
|
|
|
|
tmpGroup--;
|
|
|
|
|
2009-07-27 21:10:12 -07:00
|
|
|
buf = (char *)cx->malloc(size + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!buf)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
tmpDest = buf;
|
|
|
|
tmpSrc = num;
|
|
|
|
|
|
|
|
while (*tmpSrc == '-' || remainder--)
|
|
|
|
*tmpDest++ = *tmpSrc++;
|
|
|
|
while (tmpSrc < end) {
|
|
|
|
strcpy(tmpDest, rt->thousandsSeparator);
|
|
|
|
tmpDest += thousandsLength;
|
|
|
|
memcpy(tmpDest, tmpSrc, *tmpGroup);
|
|
|
|
tmpDest += *tmpGroup;
|
|
|
|
tmpSrc += *tmpGroup;
|
|
|
|
if (--nrepeat < 0)
|
|
|
|
tmpGroup--;
|
|
|
|
}
|
|
|
|
|
2008-07-24 12:43:40 -07:00
|
|
|
if (*nint == '.') {
|
2007-03-22 10:30:00 -07:00
|
|
|
strcpy(tmpDest, rt->decimalSeparator);
|
|
|
|
tmpDest += decimalLength;
|
2008-07-24 12:43:40 -07:00
|
|
|
strcpy(tmpDest, nint + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-07-24 12:43:40 -07:00
|
|
|
strcpy(tmpDest, nint);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cx->localeCallbacks && cx->localeCallbacks->localeToUnicode)
|
2007-08-01 21:33:52 -07:00
|
|
|
return cx->localeCallbacks->localeToUnicode(cx, buf, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
str = JS_NewString(cx, buf, size);
|
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_valueOf(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
jsval v;
|
|
|
|
JSObject *obj;
|
|
|
|
|
|
|
|
v = vp[1];
|
|
|
|
if (JSVAL_IS_NUMBER(v)) {
|
|
|
|
*vp = v;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2008-03-01 14:15:21 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_NumberClass, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-09-05 08:59:11 -07:00
|
|
|
*vp = obj->fslots[JSSLOT_PRIMITIVE_THIS];
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_PRECISION 100
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_to(JSContext *cx, JSDToStrMode zeroArgMode, JSDToStrMode oneArgMode,
|
|
|
|
jsint precisionMin, jsint precisionMax, jsint precisionOffset,
|
|
|
|
uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
jsdouble d, precision;
|
|
|
|
JSString *str;
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
/* Use MAX_PRECISION+1 because precisionOffset can be 1. */
|
|
|
|
char buf[DTOSTR_VARIABLE_BUFFER_SIZE(MAX_PRECISION+1)];
|
|
|
|
char *numStr;
|
|
|
|
|
|
|
|
if (!js_GetPrimitiveThis(cx, vp, &js_NumberClass, &v))
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ASSERT(JSVAL_IS_NUMBER(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
d = JSVAL_IS_INT(v) ? (jsdouble)JSVAL_TO_INT(v) : *JSVAL_TO_DOUBLE(v);
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
if (argc == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
precision = 0.0;
|
|
|
|
oneArgMode = zeroArgMode;
|
|
|
|
} else {
|
2008-03-06 15:24:08 -08:00
|
|
|
precision = js_ValueToNumber(cx, &vp[2]);
|
|
|
|
if (JSVAL_IS_NULL(vp[2]))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
precision = js_DoubleToInteger(precision);
|
|
|
|
if (precision < precisionMin || precision > precisionMax) {
|
|
|
|
numStr = JS_dtostr(buf, sizeof buf, DTOSTR_STANDARD, 0, precision);
|
|
|
|
if (!numStr)
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
else
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_PRECISION_RANGE, numStr);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numStr = JS_dtostr(buf, sizeof buf, oneArgMode, (jsint)precision + precisionOffset, d);
|
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
str = JS_NewStringCopyZ(cx, numStr);
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
2007-08-01 21:33:52 -07:00
|
|
|
*vp = STRING_TO_JSVAL(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_toFixed(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return num_to(cx, DTOSTR_FIXED, DTOSTR_FIXED, -20, MAX_PRECISION, 0,
|
|
|
|
argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_toExponential(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
return num_to(cx, DTOSTR_STANDARD_EXPONENTIAL, DTOSTR_EXPONENTIAL, 0,
|
|
|
|
MAX_PRECISION, 1, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
num_toPrecision(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0 || JSVAL_IS_VOID(vp[2]))
|
2008-10-08 15:08:33 -07:00
|
|
|
return num_toString(cx, 0, vp);
|
2007-08-01 21:33:52 -07:00
|
|
|
return num_to(cx, DTOSTR_STANDARD, DTOSTR_PRECISION, 1, MAX_PRECISION, 0,
|
|
|
|
argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
JS_DEFINE_TRCINFO_2(num_toString,
|
2010-03-01 18:47:17 -08:00
|
|
|
(2, (extern, STRING_RETRY, js_NumberToString, CONTEXT, THIS_DOUBLE, 1,
|
|
|
|
nanojit::ACC_NONE)),
|
|
|
|
(3, (static, STRING_RETRY, js_NumberToStringWithBase, CONTEXT, THIS_DOUBLE, INT32, 1,
|
|
|
|
nanojit::ACC_NONE)))
|
2008-10-08 15:08:33 -07:00
|
|
|
|
|
|
|
#endif /* JS_TRACER */
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSFunctionSpec number_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-10-08 15:08:33 -07:00
|
|
|
JS_FN(js_toSource_str, num_toSource, 0,JSFUN_THISP_NUMBER),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2009-09-04 13:44:31 -07:00
|
|
|
JS_TN(js_toString_str, num_toString, 1,JSFUN_THISP_NUMBER, &num_toString_trcinfo),
|
2008-10-08 15:08:33 -07:00
|
|
|
JS_FN(js_toLocaleString_str, num_toLocaleString, 0,JSFUN_THISP_NUMBER),
|
|
|
|
JS_FN(js_valueOf_str, num_valueOf, 0,JSFUN_THISP_NUMBER),
|
2008-10-17 11:57:34 -07:00
|
|
|
JS_FN(js_toJSON_str, num_valueOf, 0,JSFUN_THISP_NUMBER),
|
2008-10-08 15:08:33 -07:00
|
|
|
JS_FN("toFixed", num_toFixed, 1,JSFUN_THISP_NUMBER),
|
|
|
|
JS_FN("toExponential", num_toExponential, 1,JSFUN_THISP_NUMBER),
|
|
|
|
JS_FN("toPrecision", num_toPrecision, 1,JSFUN_THISP_NUMBER),
|
2007-08-01 21:33:52 -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
|
|
|
|
* if you try, so all but MAX_VALUE are set up by js_InitRuntimeNumberState
|
|
|
|
* using union jsdpun.
|
|
|
|
*/
|
|
|
|
static JSConstDoubleSpec number_constants[] = {
|
|
|
|
{0, js_NaN_str, 0,{0,0,0}},
|
|
|
|
{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}}
|
|
|
|
};
|
|
|
|
|
2008-08-21 00:13:40 -07:00
|
|
|
jsdouble js_NaN;
|
2009-10-28 04:57:31 -07:00
|
|
|
jsdouble js_PositiveInfinity;
|
|
|
|
jsdouble js_NegativeInfinity;
|
2009-08-27 11:34:13 -07:00
|
|
|
|
2009-09-09 20:09:50 -07:00
|
|
|
#if (defined __GNUC__ && 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
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_InitRuntimeNumberState(JSContext *cx)
|
|
|
|
{
|
2009-10-28 04:57:31 -07:00
|
|
|
JS_STATIC_ASSERT(JSVAL_NULL == jsval(0));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
JSRuntime *rt = cx->runtime;
|
|
|
|
JS_ASSERT(JSVAL_IS_NULL(rt->NaNValue));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
FIX_FPU();
|
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
jsdpun u;
|
2007-03-22 10:30:00 -07:00
|
|
|
u.s.hi = JSDOUBLE_HI32_EXPMASK | JSDOUBLE_HI32_MANTMASK;
|
|
|
|
u.s.lo = 0xffffffff;
|
2008-08-21 00:13:40 -07:00
|
|
|
number_constants[NC_NaN].dval = js_NaN = u.d;
|
2009-10-28 04:57:31 -07:00
|
|
|
if (!js_NewDoubleInRootedValue(cx, u.d, &rt->NaNValue))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
u.s.hi = JSDOUBLE_HI32_EXPMASK;
|
|
|
|
u.s.lo = 0x00000000;
|
2009-10-28 04:57:31 -07:00
|
|
|
number_constants[NC_POSITIVE_INFINITY].dval = js_PositiveInfinity = u.d;
|
|
|
|
if (!js_NewDoubleInRootedValue(cx, u.d, &rt->positiveInfinityValue))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
u.s.hi = JSDOUBLE_HI32_SIGNBIT | JSDOUBLE_HI32_EXPMASK;
|
|
|
|
u.s.lo = 0x00000000;
|
2009-10-28 04:57:31 -07:00
|
|
|
number_constants[NC_NEGATIVE_INFINITY].dval = js_NegativeInfinity = u.d;
|
|
|
|
if (!js_NewDoubleInRootedValue(cx, u.d, &rt->negativeInfinityValue))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
u.s.hi = 0;
|
|
|
|
u.s.lo = 1;
|
|
|
|
number_constants[NC_MIN_VALUE].dval = u.d;
|
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
struct lconv *locale = localeconv();
|
2007-03-22 10:30:00 -07:00
|
|
|
rt->thousandsSeparator =
|
|
|
|
JS_strdup(cx, locale->thousands_sep ? locale->thousands_sep : "'");
|
|
|
|
rt->decimalSeparator =
|
|
|
|
JS_strdup(cx, locale->decimal_point ? locale->decimal_point : ".");
|
|
|
|
rt->numGrouping =
|
|
|
|
JS_strdup(cx, locale->grouping ? locale->grouping : "\3\0");
|
|
|
|
|
|
|
|
return rt->thousandsSeparator && rt->decimalSeparator && rt->numGrouping;
|
|
|
|
}
|
|
|
|
|
2008-02-26 13:01:42 -08:00
|
|
|
void
|
|
|
|
js_TraceRuntimeNumberState(JSTracer *trc)
|
|
|
|
{
|
2009-10-28 04:57:31 -07:00
|
|
|
JSRuntime *rt = trc->context->runtime;
|
2008-02-26 13:01:42 -08:00
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
if (!JSVAL_IS_NULL(rt->NaNValue))
|
|
|
|
JS_CALL_DOUBLE_TRACER(trc, JSVAL_TO_DOUBLE(rt->NaNValue), "NaN");
|
|
|
|
if (!JSVAL_IS_NULL(rt->positiveInfinityValue)) {
|
|
|
|
JS_CALL_DOUBLE_TRACER(trc, JSVAL_TO_DOUBLE(rt->positiveInfinityValue),
|
|
|
|
"+Infinity");
|
|
|
|
}
|
|
|
|
if (!JSVAL_IS_NULL(rt->negativeInfinityValue)) {
|
|
|
|
JS_CALL_DOUBLE_TRACER(trc, JSVAL_TO_DOUBLE(rt->negativeInfinityValue),
|
|
|
|
"-Infinity");
|
|
|
|
}
|
2008-02-26 13:01:42 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
js_FinishRuntimeNumberState(JSContext *cx)
|
|
|
|
{
|
|
|
|
JSRuntime *rt = cx->runtime;
|
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
rt->NaNValue = JSVAL_NULL;
|
|
|
|
rt->negativeInfinityValue = JSVAL_NULL;
|
|
|
|
rt->positiveInfinityValue = JSVAL_NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
cx->free((void *) rt->thousandsSeparator);
|
|
|
|
cx->free((void *) rt->decimalSeparator);
|
|
|
|
cx->free((void *) rt->numGrouping);
|
2007-03-22 10:30:00 -07:00
|
|
|
rt->thousandsSeparator = rt->decimalSeparator = rt->numGrouping = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitNumberClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *proto, *ctor;
|
|
|
|
JSRuntime *rt;
|
|
|
|
|
|
|
|
/* XXX must do at least once per new thread, so do it per JSContext... */
|
|
|
|
FIX_FPU();
|
|
|
|
|
|
|
|
if (!JS_DefineFunctions(cx, obj, number_functions))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_NumberClass, Number, 1,
|
|
|
|
NULL, number_methods, NULL, NULL);
|
|
|
|
if (!proto || !(ctor = JS_GetConstructor(cx, proto)))
|
|
|
|
return NULL;
|
2009-09-05 08:59:11 -07:00
|
|
|
proto->fslots[JSSLOT_PRIMITIVE_THIS] = JSVAL_ZERO;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!JS_DefineConstDoubles(cx, ctor, number_constants))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* ECMA 15.1.1.1 */
|
|
|
|
rt = cx->runtime;
|
2010-01-04 21:10:36 -08:00
|
|
|
if (!JS_DefineProperty(cx, obj, js_NaN_str, rt->NaNValue, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ECMA 15.1.1.2 */
|
2009-10-28 04:57:31 -07:00
|
|
|
if (!JS_DefineProperty(cx, obj, js_Infinity_str, rt->positiveInfinityValue,
|
2010-01-04 21:10:36 -08:00
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JSPROP_PERMANENT | JSPROP_READONLY)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return proto;
|
|
|
|
}
|
|
|
|
|
2008-02-03 19:41:31 -08:00
|
|
|
JSBool
|
2008-03-10 12:27:44 -07:00
|
|
|
js_NewNumberInRootedValue(JSContext *cx, jsdouble d, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsint i;
|
|
|
|
|
2008-02-03 19:41:31 -08:00
|
|
|
if (JSDOUBLE_IS_INT(d, i) && INT_FITS_IN_JSVAL(i)) {
|
2008-03-10 12:27:44 -07:00
|
|
|
*vp = INT_TO_JSVAL(i);
|
|
|
|
return JS_TRUE;
|
2008-02-03 19:41:31 -08:00
|
|
|
}
|
2008-03-10 12:27:44 -07:00
|
|
|
return js_NewDoubleInRootedValue(cx, d, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-01-12 13:07:48 -08:00
|
|
|
JSBool
|
|
|
|
js_NewWeaklyRootedNumber(JSContext *cx, jsdouble d, jsval *rval)
|
|
|
|
{
|
|
|
|
jsint i;
|
|
|
|
if (JSDOUBLE_IS_INT(d, i) && INT_FITS_IN_JSVAL(i)) {
|
|
|
|
*rval = INT_TO_JSVAL(i);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
return JS_NewDoubleValue(cx, d, rval);
|
|
|
|
}
|
|
|
|
|
2009-01-08 15:09:16 -08:00
|
|
|
/*
|
|
|
|
* Convert a number to C string. The buf must be large enough to accommodate
|
|
|
|
* the result, including '-' and '\0', if base == 10 or d is an integer that
|
|
|
|
* fits in 32 bits. The caller must free the resulting pointer if it does not
|
|
|
|
* point into buf.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
NumberToCString(JSContext *cx, jsdouble d, jsint base, char *buf, size_t bufSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsint i;
|
|
|
|
char *numStr;
|
|
|
|
|
2007-11-28 23:09:21 -08:00
|
|
|
JS_ASSERT(bufSize >= DTOSTR_STANDARD_BUFFER_SIZE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (JSDOUBLE_IS_INT(d, i)) {
|
2009-01-08 15:09:16 -08:00
|
|
|
numStr = IntToCString(i, base, buf, bufSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-10-13 19:07:30 -07:00
|
|
|
if (base == 10)
|
|
|
|
numStr = JS_dtostr(buf, bufSize, DTOSTR_STANDARD, 0, d);
|
|
|
|
else
|
|
|
|
numStr = JS_dtobasestr(base, d);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!numStr) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-11-28 23:09:21 -08:00
|
|
|
return numStr;
|
|
|
|
}
|
|
|
|
|
2009-10-14 13:25:20 -07:00
|
|
|
static JSString * JS_FASTCALL
|
|
|
|
js_NumberToStringWithBase(JSContext *cx, jsdouble d, jsint base)
|
2007-11-28 23:09:21 -08:00
|
|
|
{
|
2009-01-08 15:09:16 -08:00
|
|
|
/*
|
|
|
|
* The longest possible result here that would need to fit in buf is
|
|
|
|
* (-0x80000000).toString(2), which has length 33. (This can produce
|
|
|
|
* longer results, but in those cases buf is not used; see comment at
|
|
|
|
* NumberToCString.)
|
|
|
|
*/
|
|
|
|
char buf[34];
|
2007-11-28 23:09:21 -08:00
|
|
|
char *numStr;
|
2009-01-08 15:09:16 -08:00
|
|
|
JSString *s;
|
2007-11-28 23:09:21 -08:00
|
|
|
|
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
|
|
|
|
|
|
|
jsint i;
|
|
|
|
if (JSDOUBLE_IS_INT(d, i)) {
|
|
|
|
if (base == 10 && jsuint(i) < INT_STRING_LIMIT)
|
|
|
|
return JSString::intString(i);
|
|
|
|
if (jsuint(i) < jsuint(base)) {
|
|
|
|
if (i < 10)
|
|
|
|
return JSString::intString(i);
|
|
|
|
return JSString::unitString('a' + i - 10);
|
|
|
|
}
|
|
|
|
}
|
2009-01-08 15:09:16 -08:00
|
|
|
numStr = NumberToCString(cx, d, base, buf, sizeof buf);
|
2007-11-28 23:09:21 -08:00
|
|
|
if (!numStr)
|
|
|
|
return NULL;
|
2009-01-08 15:09:16 -08:00
|
|
|
s = JS_NewStringCopyZ(cx, numStr);
|
|
|
|
if (!(numStr >= buf && numStr < buf + sizeof buf))
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(numStr);
|
2009-01-08 15:09:16 -08:00
|
|
|
return s;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-13 19:07:30 -07:00
|
|
|
JSString * JS_FASTCALL
|
|
|
|
js_NumberToString(JSContext *cx, jsdouble d)
|
|
|
|
{
|
2009-10-14 13:25:20 -07:00
|
|
|
return js_NumberToStringWithBase(cx, d, 10);
|
2008-10-13 19:07:30 -07:00
|
|
|
}
|
|
|
|
|
2009-06-30 17:19:42 -07:00
|
|
|
JSBool JS_FASTCALL
|
2009-08-14 16:10:59 -07:00
|
|
|
js_NumberValueToCharBuffer(JSContext *cx, jsval v, JSCharBuffer &cb)
|
2009-06-30 17:19:42 -07:00
|
|
|
{
|
|
|
|
/* Convert to C-string. */
|
|
|
|
static const size_t arrSize = DTOSTR_STANDARD_BUFFER_SIZE;
|
|
|
|
char arr[arrSize];
|
|
|
|
const char *cstr;
|
|
|
|
if (JSVAL_IS_INT(v)) {
|
|
|
|
cstr = IntToCString(JSVAL_TO_INT(v), 10, arr, arrSize);
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(JSVAL_IS_DOUBLE(v));
|
|
|
|
cstr = JS_dtostr(arr, arrSize, DTOSTR_STANDARD, 0, *JSVAL_TO_DOUBLE(v));
|
|
|
|
}
|
|
|
|
if (!cstr)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
JS_ASSERT(cstrlen < arrSize);
|
2009-08-14 16:10:59 -07:00
|
|
|
size_t sizeBefore = cb.length();
|
2009-08-14 16:10:59 -07:00
|
|
|
if (!cb.growBy(cstrlen))
|
2009-06-30 17:19:42 -07:00
|
|
|
return JS_FALSE;
|
2009-08-14 16:10:59 -07:00
|
|
|
jschar *appendBegin = cb.begin() + sizeBefore;
|
2009-06-30 17:19:42 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
size_t oldcstrlen = cstrlen;
|
|
|
|
JSBool ok =
|
|
|
|
#endif
|
|
|
|
js_InflateStringToBuffer(cx, cstr, cstrlen, appendBegin, &cstrlen);
|
|
|
|
JS_ASSERT(ok && cstrlen == oldcstrlen);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
jsdouble
|
|
|
|
js_ValueToNumber(JSContext *cx, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-06 15:24:08 -08:00
|
|
|
jsval v;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *str;
|
2007-08-04 12:05:16 -07:00
|
|
|
const jschar *bp, *end, *ep;
|
2009-10-28 04:57:31 -07:00
|
|
|
jsdouble d;
|
2008-03-06 15:24:08 -08:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
v = *vp;
|
|
|
|
for (;;) {
|
|
|
|
if (JSVAL_IS_INT(v))
|
|
|
|
return (jsdouble) JSVAL_TO_INT(v);
|
|
|
|
if (JSVAL_IS_DOUBLE(v))
|
|
|
|
return *JSVAL_TO_DOUBLE(v);
|
|
|
|
if (JSVAL_IS_STRING(v)) {
|
|
|
|
str = JSVAL_TO_STRING(v);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that ECMA doesn't treat a string beginning with a '0' as
|
|
|
|
* an octal number here. This works because all such numbers will
|
|
|
|
* be interpreted as decimal by js_strtod and will never get
|
|
|
|
* passed to js_strtointeger (which would interpret them as
|
|
|
|
* octal).
|
|
|
|
*/
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndEnd(bp, end);
|
2009-08-14 13:03:10 -07:00
|
|
|
|
|
|
|
/* ECMA doesn't allow signed hex numbers (bug 273467). */
|
|
|
|
bp = js_SkipWhiteSpace(bp, end);
|
|
|
|
if (bp + 2 < end && (*bp == '-' || *bp == '+') &&
|
|
|
|
bp[1] == '0' && (bp[2] == 'X' || bp[2] == 'x')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
if ((!js_strtod(cx, bp, end, &ep, &d) ||
|
|
|
|
js_SkipWhiteSpace(ep, end) != end) &&
|
|
|
|
(!js_strtointeger(cx, bp, end, &ep, 0, &d) ||
|
2008-03-12 00:05:30 -07:00
|
|
|
js_SkipWhiteSpace(ep, end) != end)) {
|
2008-03-06 15:24:08 -08:00
|
|
|
break;
|
|
|
|
}
|
2008-03-10 12:27:44 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* JSVAL_TRUE indicates that double jsval was never constructed
|
|
|
|
* for the result.
|
|
|
|
*/
|
|
|
|
*vp = JSVAL_TRUE;
|
2008-03-06 15:24:08 -08:00
|
|
|
return d;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-10 12:27:44 -07:00
|
|
|
if (JSVAL_IS_BOOLEAN(v)) {
|
|
|
|
if (JSVAL_TO_BOOLEAN(v)) {
|
|
|
|
*vp = JSVAL_ONE;
|
|
|
|
return 1.0;
|
|
|
|
} else {
|
|
|
|
*vp = JSVAL_ZERO;
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
if (JSVAL_IS_NULL(v)) {
|
|
|
|
*vp = JSVAL_ZERO;
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
if (JSVAL_IS_VOID(v))
|
|
|
|
break;
|
|
|
|
|
|
|
|
JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
|
|
|
|
obj = JSVAL_TO_OBJECT(v);
|
2007-08-04 12:05:16 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2008-03-06 15:24:08 -08:00
|
|
|
* vp roots obj so we cannot use it as an extra root for
|
2009-08-11 13:05:44 -07:00
|
|
|
* obj->defaultValue result when calling the hook.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2009-09-25 05:30:11 -07:00
|
|
|
JSAutoTempValueRooter tvr(cx, v);
|
|
|
|
if (!obj->defaultValue(cx, JSTYPE_NUMBER, tvr.addr()))
|
2008-03-06 15:24:08 -08:00
|
|
|
obj = NULL;
|
|
|
|
else
|
2009-09-25 05:30:11 -07:00
|
|
|
v = *vp = tvr.value();
|
2008-03-06 15:24:08 -08:00
|
|
|
if (!obj) {
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return 0.0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(v))
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
|
2009-10-28 04:57:31 -07:00
|
|
|
*vp = cx->runtime->NaNValue;
|
|
|
|
return js_NaN;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
int32
|
|
|
|
js_ValueToECMAInt32(JSContext *cx, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-06 13:40:43 -08:00
|
|
|
jsval v;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsdouble d;
|
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
v = *vp;
|
|
|
|
if (JSVAL_IS_INT(v))
|
|
|
|
return JSVAL_TO_INT(v);
|
|
|
|
if (JSVAL_IS_DOUBLE(v)) {
|
|
|
|
d = *JSVAL_TO_DOUBLE(v);
|
2008-03-10 12:27:44 -07:00
|
|
|
*vp = JSVAL_TRUE;
|
2008-03-06 13:40:43 -08:00
|
|
|
} else {
|
2008-03-06 15:24:08 -08:00
|
|
|
d = js_ValueToNumber(cx, vp);
|
|
|
|
if (JSVAL_IS_NULL(*vp))
|
2008-03-06 13:40:43 -08:00
|
|
|
return 0;
|
2008-03-10 12:27:44 -07:00
|
|
|
*vp = JSVAL_TRUE;
|
2008-03-06 13:40:43 -08:00
|
|
|
}
|
|
|
|
return js_DoubleToECMAInt32(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
uint32
|
|
|
|
js_ValueToECMAUint32(JSContext *cx, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-06 13:40:43 -08:00
|
|
|
jsval v;
|
2008-03-10 12:27:44 -07:00
|
|
|
jsint i;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsdouble d;
|
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
v = *vp;
|
2008-03-10 12:27:44 -07:00
|
|
|
if (JSVAL_IS_INT(v)) {
|
|
|
|
i = JSVAL_TO_INT(v);
|
|
|
|
if (i < 0)
|
|
|
|
*vp = JSVAL_TRUE;
|
|
|
|
return (uint32) i;
|
|
|
|
}
|
2008-03-06 13:40:43 -08:00
|
|
|
if (JSVAL_IS_DOUBLE(v)) {
|
|
|
|
d = *JSVAL_TO_DOUBLE(v);
|
2008-03-10 12:27:44 -07:00
|
|
|
*vp = JSVAL_TRUE;
|
2008-03-06 13:40:43 -08:00
|
|
|
} else {
|
2008-03-06 15:24:08 -08:00
|
|
|
d = js_ValueToNumber(cx, vp);
|
|
|
|
if (JSVAL_IS_NULL(*vp))
|
2008-03-06 13:40:43 -08:00
|
|
|
return 0;
|
2008-03-10 12:27:44 -07:00
|
|
|
*vp = JSVAL_TRUE;
|
2008-03-06 13:40:43 -08:00
|
|
|
}
|
|
|
|
return js_DoubleToECMAUint32(d);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-12-21 14:06:37 -08:00
|
|
|
uint32
|
|
|
|
js_DoubleToECMAUint32(jsdouble d)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-06 13:40:43 -08:00
|
|
|
int32 i;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool neg;
|
2008-03-06 13:40:43 -08:00
|
|
|
jsdouble two32;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
if (!JSDOUBLE_IS_FINITE(d))
|
2007-12-21 14:06:37 -08:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
/*
|
|
|
|
* We check whether d fits int32, not uint32, as all but the ">>>" bit
|
|
|
|
* manipulation bytecode stores the result as int, not uint. When the
|
|
|
|
* result does not fit int jsval, it will be stored as a negative double.
|
|
|
|
*/
|
|
|
|
i = (int32) d;
|
|
|
|
if ((jsdouble) i == d)
|
|
|
|
return (int32)i;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
neg = (d < 0);
|
|
|
|
d = floor(neg ? -d : d);
|
|
|
|
d = neg ? -d : d;
|
|
|
|
|
2008-03-06 13:40:43 -08:00
|
|
|
two32 = 4294967296.0;
|
2007-03-22 10:30:00 -07:00
|
|
|
d = fmod(d, two32);
|
|
|
|
|
2008-02-06 13:34:51 -08:00
|
|
|
return (uint32) (d >= 0 ? d : d + two32);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
int32
|
|
|
|
js_ValueToInt32(JSContext *cx, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-03-06 15:24:08 -08:00
|
|
|
jsval v;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsdouble d;
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
v = *vp;
|
|
|
|
if (JSVAL_IS_INT(v))
|
|
|
|
return JSVAL_TO_INT(v);
|
|
|
|
d = js_ValueToNumber(cx, vp);
|
|
|
|
if (JSVAL_IS_NULL(*vp))
|
|
|
|
return 0;
|
2008-03-10 12:27:44 -07:00
|
|
|
if (JSVAL_IS_INT(*vp))
|
|
|
|
return JSVAL_TO_INT(*vp);
|
|
|
|
|
|
|
|
*vp = JSVAL_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (JSDOUBLE_IS_NaN(d) || d <= -2147483649.0 || 2147483648.0 <= d) {
|
|
|
|
js_ReportValueError(cx, JSMSG_CANT_CONVERT,
|
|
|
|
JSDVG_SEARCH_STACK, v, NULL);
|
2008-03-06 15:24:08 -08:00
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-03-06 15:24:08 -08:00
|
|
|
return (int32) floor(d + 0.5); /* Round to nearest */
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
uint16
|
|
|
|
js_ValueToUint16(JSContext *cx, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsdouble d;
|
2008-03-10 12:27:44 -07:00
|
|
|
uint16 u;
|
|
|
|
jsuint m;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool neg;
|
|
|
|
|
2008-03-06 15:24:08 -08:00
|
|
|
d = js_ValueToNumber(cx, vp);
|
|
|
|
if (JSVAL_IS_NULL(*vp))
|
|
|
|
return 0;
|
2008-03-10 12:27:44 -07:00
|
|
|
|
|
|
|
if (JSVAL_IS_INT(*vp)) {
|
|
|
|
u = (uint16) JSVAL_TO_INT(*vp);
|
|
|
|
} else if (d == 0 || !JSDOUBLE_IS_FINITE(d)) {
|
|
|
|
u = (uint16) 0;
|
|
|
|
} else {
|
|
|
|
u = (uint16) d;
|
|
|
|
if ((jsdouble) u != d) {
|
|
|
|
neg = (d < 0);
|
|
|
|
d = floor(neg ? -d : d);
|
|
|
|
d = neg ? -d : d;
|
|
|
|
m = JS_BIT(16);
|
|
|
|
d = fmod(d, (double) m);
|
|
|
|
if (d < 0)
|
|
|
|
d += m;
|
|
|
|
u = (uint16) d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*vp = INT_TO_JSVAL(u);
|
|
|
|
return u;
|
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,
|
|
|
|
const jschar **ep, jsdouble *dp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-04 12:05:16 -07:00
|
|
|
const jschar *s1;
|
|
|
|
size_t length, i;
|
2007-03-22 10:30:00 -07:00
|
|
|
char cbuf[32];
|
|
|
|
char *cstr, *istr, *estr;
|
|
|
|
JSBool negative;
|
|
|
|
jsdouble d;
|
2007-08-04 12:05:16 -07:00
|
|
|
|
|
|
|
s1 = js_SkipWhiteSpace(s, send);
|
|
|
|
length = send - s1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Use cbuf to avoid malloc */
|
|
|
|
if (length >= sizeof cbuf) {
|
2009-07-27 21:10: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++;
|
2009-08-29 02:28:00 -07:00
|
|
|
if (*istr == 'I' && !strncmp(istr, js_Infinity_str, sizeof js_Infinity_str - 1)) {
|
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;
|
|
|
|
d = JS_strtod(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)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(cstr);
|
2007-03-22 10:30:00 -07:00
|
|
|
*ep = i ? s1 + i : s;
|
|
|
|
*dp = d;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BinaryDigitReader
|
|
|
|
{
|
|
|
|
uintN base; /* Base of number; must be a power of 2 */
|
|
|
|
uintN digit; /* Current digit value in radix given by base */
|
|
|
|
uintN digitMask; /* Mask to extract the next bit from digit */
|
|
|
|
const jschar *digits; /* Pointer to the remaining digits */
|
|
|
|
const jschar *end; /* Pointer to first non-digit */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Return the next binary digit from the number or -1 if done */
|
|
|
|
static intN GetNextBinaryDigit(struct BinaryDigitReader *bdr)
|
|
|
|
{
|
|
|
|
intN bit;
|
|
|
|
|
|
|
|
if (bdr->digitMask == 0) {
|
|
|
|
uintN c;
|
|
|
|
|
|
|
|
if (bdr->digits == bdr->end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
c = *bdr->digits++;
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
bdr->digit = c - '0';
|
|
|
|
else if ('a' <= c && c <= 'z')
|
|
|
|
bdr->digit = c - 'a' + 10;
|
2009-06-10 18:29:44 -07:00
|
|
|
else
|
|
|
|
bdr->digit = c - 'A' + 10;
|
2007-03-22 10:30:00 -07:00
|
|
|
bdr->digitMask = bdr->base >> 1;
|
|
|
|
}
|
|
|
|
bit = (bdr->digit & bdr->digitMask) != 0;
|
|
|
|
bdr->digitMask >>= 1;
|
|
|
|
return bit;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2007-08-04 12:05:16 -07:00
|
|
|
js_strtointeger(JSContext *cx, const jschar *s, const jschar *send,
|
|
|
|
const jschar **ep, jsint base, jsdouble *dp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-04 12:05:16 -07:00
|
|
|
const jschar *s1, *start;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool negative;
|
|
|
|
jsdouble value;
|
|
|
|
|
2007-08-04 12:05:16 -07:00
|
|
|
s1 = js_SkipWhiteSpace(s, send);
|
|
|
|
if (s1 == send)
|
|
|
|
goto no_digits;
|
|
|
|
if ((negative = (*s1 == '-')) != 0 || *s1 == '+') {
|
2007-03-22 10:30:00 -07:00
|
|
|
s1++;
|
2007-08-04 12:05:16 -07:00
|
|
|
if (s1 == send)
|
|
|
|
goto no_digits;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (base == 0) {
|
|
|
|
/* No base supplied, or some base that evaluated to 0. */
|
|
|
|
if (*s1 == '0') {
|
|
|
|
/* It's either hex or octal; only increment char if str isn't '0' */
|
2007-08-04 12:05:16 -07:00
|
|
|
if (s1 + 1 != send && (s1[1] == 'X' || s1[1] == 'x')) {
|
2007-08-02 04:34:41 -07:00
|
|
|
base = 16;
|
2007-08-04 12:05:16 -07:00
|
|
|
s1 += 2;
|
|
|
|
if (s1 == send)
|
|
|
|
goto no_digits;
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
base = 10; /* Default to decimal. */
|
|
|
|
}
|
2007-08-04 12:05:16 -07:00
|
|
|
} else if (base == 16) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* If base is 16, ignore hex prefix. */
|
2007-08-04 12:05:16 -07:00
|
|
|
if (*s1 == '0' && s1 + 1 != send && (s1[1] == 'X' || s1[1] == 'x')) {
|
|
|
|
s1 += 2;
|
|
|
|
if (s1 == send)
|
|
|
|
goto no_digits;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Done with the preliminaries; find some prefix of the string that's
|
|
|
|
* a number in the given base.
|
|
|
|
*/
|
2007-08-04 12:05:16 -07:00
|
|
|
JS_ASSERT(s1 < send);
|
|
|
|
start = s1;
|
2007-03-22 10:30:00 -07:00
|
|
|
value = 0.0;
|
2007-08-04 12:05:16 -07:00
|
|
|
do {
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN digit;
|
|
|
|
jschar c = *s1;
|
|
|
|
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 >= (uintN)base)
|
|
|
|
break;
|
|
|
|
value = value * base + digit;
|
2007-08-04 12:05:16 -07:00
|
|
|
} while (++s1 != send);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (value >= 9007199254740992.0) {
|
|
|
|
if (base == 10) {
|
|
|
|
/*
|
|
|
|
* If we're accumulating a decimal number and the number is >=
|
|
|
|
* 2^53, then the result from the repeated multiply-add above may
|
|
|
|
* be inaccurate. Call JS_strtod to get the correct answer.
|
|
|
|
*/
|
|
|
|
size_t i;
|
|
|
|
size_t length = s1 - start;
|
2009-07-27 21:10:12 -07:00
|
|
|
char *cstr = (char *) cx->malloc(length + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
char *estr;
|
|
|
|
int err=0;
|
|
|
|
|
|
|
|
if (!cstr)
|
|
|
|
return JS_FALSE;
|
|
|
|
for (i = 0; i != length; i++)
|
|
|
|
cstr[i] = (char)start[i];
|
|
|
|
cstr[length] = 0;
|
|
|
|
|
|
|
|
value = JS_strtod(cstr, &estr, &err);
|
|
|
|
if (err == JS_DTOA_ENOMEM) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(cstr);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (err == JS_DTOA_ERANGE && value == HUGE_VAL)
|
2009-10-28 04:57:31 -07:00
|
|
|
value = js_PositiveInfinity;
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(cstr);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if ((base & (base - 1)) == 0) {
|
|
|
|
/*
|
|
|
|
* The number may also be inaccurate for power-of-two bases. This
|
|
|
|
* happens if the addition in value * base + 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.
|
|
|
|
*/
|
|
|
|
struct BinaryDigitReader bdr;
|
|
|
|
intN bit, bit2;
|
|
|
|
intN j;
|
|
|
|
|
|
|
|
bdr.base = base;
|
2009-09-13 21:17:45 -07:00
|
|
|
bdr.digit = 0; // shut GCC up
|
2007-03-22 10:30:00 -07:00
|
|
|
bdr.digitMask = 0;
|
|
|
|
bdr.digits = start;
|
|
|
|
bdr.end = s1;
|
|
|
|
value = 0.0;
|
|
|
|
|
|
|
|
/* Skip leading zeros. */
|
|
|
|
do {
|
|
|
|
bit = GetNextBinaryDigit(&bdr);
|
|
|
|
} while (bit == 0);
|
|
|
|
|
|
|
|
if (bit == 1) {
|
|
|
|
/* Gather the 53 significant bits (including the leading 1) */
|
|
|
|
value = 1.0;
|
|
|
|
for (j = 52; j; j--) {
|
|
|
|
bit = GetNextBinaryDigit(&bdr);
|
|
|
|
if (bit < 0)
|
|
|
|
goto done;
|
|
|
|
value = value*2 + bit;
|
|
|
|
}
|
|
|
|
/* bit2 is the 54th bit (the first dropped from the mantissa) */
|
|
|
|
bit2 = GetNextBinaryDigit(&bdr);
|
|
|
|
if (bit2 >= 0) {
|
|
|
|
jsdouble factor = 2.0;
|
|
|
|
intN sticky = 0; /* sticky is 1 if any bit beyond the 54th is 1 */
|
|
|
|
intN bit3;
|
|
|
|
|
|
|
|
while ((bit3 = GetNextBinaryDigit(&bdr)) >= 0) {
|
|
|
|
sticky |= bit3;
|
|
|
|
factor *= 2;
|
|
|
|
}
|
|
|
|
value += bit2 & (bit | sticky);
|
|
|
|
value *= factor;
|
|
|
|
}
|
|
|
|
done:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* We don't worry about inaccurate numbers for any other base. */
|
|
|
|
|
|
|
|
if (s1 == start) {
|
2007-08-04 12:05:16 -07:00
|
|
|
no_digits:
|
2007-03-22 10:30:00 -07:00
|
|
|
*dp = 0.0;
|
|
|
|
*ep = s;
|
|
|
|
} else {
|
|
|
|
*dp = negative ? -value : value;
|
|
|
|
*ep = s1;
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|