1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsnum.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1785 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * JS number type and wrapper class. 1.12 + */ 1.13 + 1.14 +#include "jsnum.h" 1.15 + 1.16 +#include "mozilla/FloatingPoint.h" 1.17 +#include "mozilla/PodOperations.h" 1.18 +#include "mozilla/RangedPtr.h" 1.19 + 1.20 +#ifdef HAVE_LOCALECONV 1.21 +#include <locale.h> 1.22 +#endif 1.23 +#include <math.h> 1.24 +#include <string.h> 1.25 + 1.26 +#include "double-conversion.h" 1.27 +#include "jsatom.h" 1.28 +#include "jscntxt.h" 1.29 +#include "jsdtoa.h" 1.30 +#include "jsobj.h" 1.31 +#include "jsstr.h" 1.32 +#include "jstypes.h" 1.33 + 1.34 +#include "vm/GlobalObject.h" 1.35 +#include "vm/NumericConversions.h" 1.36 +#include "vm/StringBuffer.h" 1.37 + 1.38 +#include "jsatominlines.h" 1.39 + 1.40 +#include "vm/NumberObject-inl.h" 1.41 +#include "vm/String-inl.h" 1.42 + 1.43 +using namespace js; 1.44 +using namespace js::types; 1.45 + 1.46 +using mozilla::Abs; 1.47 +using mozilla::MinNumberValue; 1.48 +using mozilla::NegativeInfinity; 1.49 +using mozilla::PodCopy; 1.50 +using mozilla::PositiveInfinity; 1.51 +using mozilla::RangedPtr; 1.52 +using JS::GenericNaN; 1.53 + 1.54 +/* 1.55 + * If we're accumulating a decimal number and the number is >= 2^53, then the 1.56 + * fast result from the loop in Get{Prefix,Decimal}Integer may be inaccurate. 1.57 + * Call js_strtod_harder to get the correct answer. 1.58 + */ 1.59 +static bool 1.60 +ComputeAccurateDecimalInteger(ThreadSafeContext *cx, 1.61 + const jschar *start, const jschar *end, double *dp) 1.62 +{ 1.63 + size_t length = end - start; 1.64 + char *cstr = cx->pod_malloc<char>(length + 1); 1.65 + if (!cstr) 1.66 + return false; 1.67 + 1.68 + for (size_t i = 0; i < length; i++) { 1.69 + char c = char(start[i]); 1.70 + JS_ASSERT(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')); 1.71 + cstr[i] = c; 1.72 + } 1.73 + cstr[length] = 0; 1.74 + 1.75 + char *estr; 1.76 + int err = 0; 1.77 + *dp = js_strtod_harder(cx->dtoaState(), cstr, &estr, &err); 1.78 + if (err == JS_DTOA_ENOMEM) { 1.79 + js_ReportOutOfMemory(cx); 1.80 + js_free(cstr); 1.81 + return false; 1.82 + } 1.83 + js_free(cstr); 1.84 + return true; 1.85 +} 1.86 + 1.87 +namespace { 1.88 + 1.89 +class BinaryDigitReader 1.90 +{ 1.91 + const int base; /* Base of number; must be a power of 2 */ 1.92 + int digit; /* Current digit value in radix given by base */ 1.93 + int digitMask; /* Mask to extract the next bit from digit */ 1.94 + const jschar *start; /* Pointer to the remaining digits */ 1.95 + const jschar *end; /* Pointer to first non-digit */ 1.96 + 1.97 + public: 1.98 + BinaryDigitReader(int base, const jschar *start, const jschar *end) 1.99 + : base(base), digit(0), digitMask(0), start(start), end(end) 1.100 + { 1.101 + } 1.102 + 1.103 + /* Return the next binary digit from the number, or -1 if done. */ 1.104 + int nextDigit() { 1.105 + if (digitMask == 0) { 1.106 + if (start == end) 1.107 + return -1; 1.108 + 1.109 + int c = *start++; 1.110 + JS_ASSERT(('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')); 1.111 + if ('0' <= c && c <= '9') 1.112 + digit = c - '0'; 1.113 + else if ('a' <= c && c <= 'z') 1.114 + digit = c - 'a' + 10; 1.115 + else 1.116 + digit = c - 'A' + 10; 1.117 + digitMask = base >> 1; 1.118 + } 1.119 + 1.120 + int bit = (digit & digitMask) != 0; 1.121 + digitMask >>= 1; 1.122 + return bit; 1.123 + } 1.124 +}; 1.125 + 1.126 +} /* anonymous namespace */ 1.127 + 1.128 +/* 1.129 + * The fast result might also have been inaccurate for power-of-two bases. This 1.130 + * happens if the addition in value * 2 + digit causes a round-down to an even 1.131 + * least significant mantissa bit when the first dropped bit is a one. If any 1.132 + * of the following digits in the number (which haven't been added in yet) are 1.133 + * nonzero, then the correct action would have been to round up instead of 1.134 + * down. An example occurs when reading the number 0x1000000000000081, which 1.135 + * rounds to 0x1000000000000000 instead of 0x1000000000000100. 1.136 + */ 1.137 +static double 1.138 +ComputeAccurateBinaryBaseInteger(const jschar *start, const jschar *end, int base) 1.139 +{ 1.140 + BinaryDigitReader bdr(base, start, end); 1.141 + 1.142 + /* Skip leading zeroes. */ 1.143 + int bit; 1.144 + do { 1.145 + bit = bdr.nextDigit(); 1.146 + } while (bit == 0); 1.147 + 1.148 + JS_ASSERT(bit == 1); // guaranteed by Get{Prefix,Decimal}Integer 1.149 + 1.150 + /* Gather the 53 significant bits (including the leading 1). */ 1.151 + double value = 1.0; 1.152 + for (int j = 52; j > 0; j--) { 1.153 + bit = bdr.nextDigit(); 1.154 + if (bit < 0) 1.155 + return value; 1.156 + value = value * 2 + bit; 1.157 + } 1.158 + 1.159 + /* bit2 is the 54th bit (the first dropped from the mantissa). */ 1.160 + int bit2 = bdr.nextDigit(); 1.161 + if (bit2 >= 0) { 1.162 + double factor = 2.0; 1.163 + int sticky = 0; /* sticky is 1 if any bit beyond the 54th is 1 */ 1.164 + int bit3; 1.165 + 1.166 + while ((bit3 = bdr.nextDigit()) >= 0) { 1.167 + sticky |= bit3; 1.168 + factor *= 2; 1.169 + } 1.170 + value += bit2 & (bit | sticky); 1.171 + value *= factor; 1.172 + } 1.173 + 1.174 + return value; 1.175 +} 1.176 + 1.177 +double 1.178 +js::ParseDecimalNumber(const JS::TwoByteChars chars) 1.179 +{ 1.180 + MOZ_ASSERT(chars.length() > 0); 1.181 + uint64_t dec = 0; 1.182 + RangedPtr<jschar> s = chars.start(), end = chars.end(); 1.183 + do { 1.184 + jschar c = *s; 1.185 + MOZ_ASSERT('0' <= c && c <= '9'); 1.186 + uint8_t digit = c - '0'; 1.187 + uint64_t next = dec * 10 + digit; 1.188 + MOZ_ASSERT(next < DOUBLE_INTEGRAL_PRECISION_LIMIT, 1.189 + "next value won't be an integrally-precise double"); 1.190 + dec = next; 1.191 + } while (++s < end); 1.192 + return static_cast<double>(dec); 1.193 +} 1.194 + 1.195 +bool 1.196 +js::GetPrefixInteger(ThreadSafeContext *cx, const jschar *start, const jschar *end, int base, 1.197 + const jschar **endp, double *dp) 1.198 +{ 1.199 + JS_ASSERT(start <= end); 1.200 + JS_ASSERT(2 <= base && base <= 36); 1.201 + 1.202 + const jschar *s = start; 1.203 + double d = 0.0; 1.204 + for (; s < end; s++) { 1.205 + int digit; 1.206 + jschar c = *s; 1.207 + if ('0' <= c && c <= '9') 1.208 + digit = c - '0'; 1.209 + else if ('a' <= c && c <= 'z') 1.210 + digit = c - 'a' + 10; 1.211 + else if ('A' <= c && c <= 'Z') 1.212 + digit = c - 'A' + 10; 1.213 + else 1.214 + break; 1.215 + if (digit >= base) 1.216 + break; 1.217 + d = d * base + digit; 1.218 + } 1.219 + 1.220 + *endp = s; 1.221 + *dp = d; 1.222 + 1.223 + /* If we haven't reached the limit of integer precision, we're done. */ 1.224 + if (d < DOUBLE_INTEGRAL_PRECISION_LIMIT) 1.225 + return true; 1.226 + 1.227 + /* 1.228 + * Otherwise compute the correct integer from the prefix of valid digits 1.229 + * if we're computing for base ten or a power of two. Don't worry about 1.230 + * other bases; see 15.1.2.2 step 13. 1.231 + */ 1.232 + if (base == 10) 1.233 + return ComputeAccurateDecimalInteger(cx, start, s, dp); 1.234 + if ((base & (base - 1)) == 0) 1.235 + *dp = ComputeAccurateBinaryBaseInteger(start, s, base); 1.236 + 1.237 + return true; 1.238 +} 1.239 + 1.240 +bool 1.241 +js::GetDecimalInteger(ExclusiveContext *cx, const jschar *start, const jschar *end, double *dp) 1.242 +{ 1.243 + JS_ASSERT(start <= end); 1.244 + 1.245 + const jschar *s = start; 1.246 + double d = 0.0; 1.247 + for (; s < end; s++) { 1.248 + jschar c = *s; 1.249 + JS_ASSERT('0' <= c && c <= '9'); 1.250 + int digit = c - '0'; 1.251 + d = d * 10 + digit; 1.252 + } 1.253 + 1.254 + *dp = d; 1.255 + 1.256 + // If we haven't reached the limit of integer precision, we're done. 1.257 + if (d < DOUBLE_INTEGRAL_PRECISION_LIMIT) 1.258 + return true; 1.259 + 1.260 + // Otherwise compute the correct integer from the prefix of valid digits. 1.261 + return ComputeAccurateDecimalInteger(cx, start, s, dp); 1.262 +} 1.263 + 1.264 +static bool 1.265 +num_isNaN(JSContext *cx, unsigned argc, Value *vp) 1.266 +{ 1.267 + CallArgs args = CallArgsFromVp(argc, vp); 1.268 + 1.269 + if (args.length() == 0) { 1.270 + args.rval().setBoolean(true); 1.271 + return true; 1.272 + } 1.273 + 1.274 + double x; 1.275 + if (!ToNumber(cx, args[0], &x)) 1.276 + return false; 1.277 + 1.278 + args.rval().setBoolean(mozilla::IsNaN(x)); 1.279 + return true; 1.280 +} 1.281 + 1.282 +static bool 1.283 +num_isFinite(JSContext *cx, unsigned argc, Value *vp) 1.284 +{ 1.285 + CallArgs args = CallArgsFromVp(argc, vp); 1.286 + 1.287 + if (args.length() == 0) { 1.288 + args.rval().setBoolean(false); 1.289 + return true; 1.290 + } 1.291 + 1.292 + double x; 1.293 + if (!ToNumber(cx, args[0], &x)) 1.294 + return false; 1.295 + 1.296 + args.rval().setBoolean(mozilla::IsFinite(x)); 1.297 + return true; 1.298 +} 1.299 + 1.300 +static bool 1.301 +num_parseFloat(JSContext *cx, unsigned argc, Value *vp) 1.302 +{ 1.303 + CallArgs args = CallArgsFromVp(argc, vp); 1.304 + 1.305 + if (args.length() == 0) { 1.306 + args.rval().setNaN(); 1.307 + return true; 1.308 + } 1.309 + JSString *str = ToString<CanGC>(cx, args[0]); 1.310 + if (!str) 1.311 + return false; 1.312 + const jschar *bp = str->getChars(cx); 1.313 + if (!bp) 1.314 + return false; 1.315 + const jschar *end = bp + str->length(); 1.316 + const jschar *ep; 1.317 + double d; 1.318 + if (!js_strtod(cx, bp, end, &ep, &d)) 1.319 + return false; 1.320 + if (ep == bp) { 1.321 + args.rval().setNaN(); 1.322 + return true; 1.323 + } 1.324 + args.rval().setDouble(d); 1.325 + return true; 1.326 +} 1.327 + 1.328 +/* ES5 15.1.2.2. */ 1.329 +bool 1.330 +js::num_parseInt(JSContext *cx, unsigned argc, Value *vp) 1.331 +{ 1.332 + CallArgs args = CallArgsFromVp(argc, vp); 1.333 + 1.334 + /* Fast paths and exceptional cases. */ 1.335 + if (args.length() == 0) { 1.336 + args.rval().setNaN(); 1.337 + return true; 1.338 + } 1.339 + 1.340 + if (args.length() == 1 || 1.341 + (args[1].isInt32() && (args[1].toInt32() == 0 || args[1].toInt32() == 10))) { 1.342 + if (args[0].isInt32()) { 1.343 + args.rval().set(args[0]); 1.344 + return true; 1.345 + } 1.346 + 1.347 + /* 1.348 + * Step 1 is |inputString = ToString(string)|. When string >= 1.349 + * 1e21, ToString(string) is in the form "NeM". 'e' marks the end of 1.350 + * the word, which would mean the result of parseInt(string) should be |N|. 1.351 + * 1.352 + * To preserve this behaviour, we can't use the fast-path when string > 1.353 + * 1e21, or else the result would be |NeM|. 1.354 + * 1.355 + * The same goes for values smaller than 1.0e-6, because the string would be in 1.356 + * the form of "Ne-M". 1.357 + */ 1.358 + if (args[0].isDouble()) { 1.359 + double d = args[0].toDouble(); 1.360 + if (1.0e-6 < d && d < 1.0e21) { 1.361 + args.rval().setNumber(floor(d)); 1.362 + return true; 1.363 + } 1.364 + if (-1.0e21 < d && d < -1.0e-6) { 1.365 + args.rval().setNumber(-floor(-d)); 1.366 + return true; 1.367 + } 1.368 + if (d == 0.0) { 1.369 + args.rval().setInt32(0); 1.370 + return true; 1.371 + } 1.372 + } 1.373 + } 1.374 + 1.375 + /* Step 1. */ 1.376 + RootedString inputString(cx, ToString<CanGC>(cx, args[0])); 1.377 + if (!inputString) 1.378 + return false; 1.379 + args[0].setString(inputString); 1.380 + 1.381 + /* Steps 6-9. */ 1.382 + bool stripPrefix = true; 1.383 + int32_t radix; 1.384 + if (!args.hasDefined(1)) { 1.385 + radix = 10; 1.386 + } else { 1.387 + if (!ToInt32(cx, args[1], &radix)) 1.388 + return false; 1.389 + if (radix == 0) { 1.390 + radix = 10; 1.391 + } else { 1.392 + if (radix < 2 || radix > 36) { 1.393 + args.rval().setNaN(); 1.394 + return true; 1.395 + } 1.396 + if (radix != 16) 1.397 + stripPrefix = false; 1.398 + } 1.399 + } 1.400 + 1.401 + /* Step 2. */ 1.402 + const jschar *s; 1.403 + const jschar *end; 1.404 + { 1.405 + const jschar *ws = inputString->getChars(cx); 1.406 + if (!ws) 1.407 + return false; 1.408 + end = ws + inputString->length(); 1.409 + s = SkipSpace(ws, end); 1.410 + 1.411 + MOZ_ASSERT(ws <= s); 1.412 + MOZ_ASSERT(s <= end); 1.413 + } 1.414 + 1.415 + /* Steps 3-4. */ 1.416 + bool negative = (s != end && s[0] == '-'); 1.417 + 1.418 + /* Step 5. */ 1.419 + if (s != end && (s[0] == '-' || s[0] == '+')) 1.420 + s++; 1.421 + 1.422 + /* Step 10. */ 1.423 + if (stripPrefix) { 1.424 + if (end - s >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { 1.425 + s += 2; 1.426 + radix = 16; 1.427 + } 1.428 + } 1.429 + 1.430 + /* Steps 11-15. */ 1.431 + const jschar *actualEnd; 1.432 + double number; 1.433 + if (!GetPrefixInteger(cx, s, end, radix, &actualEnd, &number)) 1.434 + return false; 1.435 + if (s == actualEnd) 1.436 + args.rval().setNaN(); 1.437 + else 1.438 + args.rval().setNumber(negative ? -number : number); 1.439 + return true; 1.440 +} 1.441 + 1.442 +static const JSFunctionSpec number_functions[] = { 1.443 + JS_FN(js_isNaN_str, num_isNaN, 1,0), 1.444 + JS_FN(js_isFinite_str, num_isFinite, 1,0), 1.445 + JS_FN(js_parseFloat_str, num_parseFloat, 1,0), 1.446 + JS_FN(js_parseInt_str, num_parseInt, 2,0), 1.447 + JS_FS_END 1.448 +}; 1.449 + 1.450 +const Class NumberObject::class_ = { 1.451 + js_Number_str, 1.452 + JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Number), 1.453 + JS_PropertyStub, /* addProperty */ 1.454 + JS_DeletePropertyStub, /* delProperty */ 1.455 + JS_PropertyStub, /* getProperty */ 1.456 + JS_StrictPropertyStub, /* setProperty */ 1.457 + JS_EnumerateStub, 1.458 + JS_ResolveStub, 1.459 + JS_ConvertStub 1.460 +}; 1.461 + 1.462 +static bool 1.463 +Number(JSContext *cx, unsigned argc, Value *vp) 1.464 +{ 1.465 + CallArgs args = CallArgsFromVp(argc, vp); 1.466 + 1.467 + /* Sample JS_CALLEE before clobbering. */ 1.468 + bool isConstructing = args.isConstructing(); 1.469 + 1.470 + if (args.length() > 0) { 1.471 + if (!ToNumber(cx, args[0])) 1.472 + return false; 1.473 + args.rval().set(args[0]); 1.474 + } else { 1.475 + args.rval().setInt32(0); 1.476 + } 1.477 + 1.478 + if (!isConstructing) 1.479 + return true; 1.480 + 1.481 + JSObject *obj = NumberObject::create(cx, args.rval().toNumber()); 1.482 + if (!obj) 1.483 + return false; 1.484 + args.rval().setObject(*obj); 1.485 + return true; 1.486 +} 1.487 + 1.488 +MOZ_ALWAYS_INLINE bool 1.489 +IsNumber(HandleValue v) 1.490 +{ 1.491 + return v.isNumber() || (v.isObject() && v.toObject().is<NumberObject>()); 1.492 +} 1.493 + 1.494 +static inline double 1.495 +Extract(const Value &v) 1.496 +{ 1.497 + if (v.isNumber()) 1.498 + return v.toNumber(); 1.499 + return v.toObject().as<NumberObject>().unbox(); 1.500 +} 1.501 + 1.502 +#if JS_HAS_TOSOURCE 1.503 +MOZ_ALWAYS_INLINE bool 1.504 +num_toSource_impl(JSContext *cx, CallArgs args) 1.505 +{ 1.506 + double d = Extract(args.thisv()); 1.507 + 1.508 + StringBuffer sb(cx); 1.509 + if (!sb.append("(new Number(") || 1.510 + !NumberValueToStringBuffer(cx, NumberValue(d), sb) || 1.511 + !sb.append("))")) 1.512 + { 1.513 + return false; 1.514 + } 1.515 + 1.516 + JSString *str = sb.finishString(); 1.517 + if (!str) 1.518 + return false; 1.519 + args.rval().setString(str); 1.520 + return true; 1.521 +} 1.522 + 1.523 +static bool 1.524 +num_toSource(JSContext *cx, unsigned argc, Value *vp) 1.525 +{ 1.526 + CallArgs args = CallArgsFromVp(argc, vp); 1.527 + return CallNonGenericMethod<IsNumber, num_toSource_impl>(cx, args); 1.528 +} 1.529 +#endif 1.530 + 1.531 +ToCStringBuf::ToCStringBuf() :dbuf(nullptr) 1.532 +{ 1.533 + JS_STATIC_ASSERT(sbufSize >= DTOSTR_STANDARD_BUFFER_SIZE); 1.534 +} 1.535 + 1.536 +ToCStringBuf::~ToCStringBuf() 1.537 +{ 1.538 + js_free(dbuf); 1.539 +} 1.540 + 1.541 +MOZ_ALWAYS_INLINE 1.542 +static JSFlatString * 1.543 +LookupDtoaCache(ThreadSafeContext *cx, double d) 1.544 +{ 1.545 + if (!cx->isExclusiveContext()) 1.546 + return nullptr; 1.547 + 1.548 + if (JSCompartment *comp = cx->asExclusiveContext()->compartment()) { 1.549 + if (JSFlatString *str = comp->dtoaCache.lookup(10, d)) 1.550 + return str; 1.551 + } 1.552 + 1.553 + return nullptr; 1.554 +} 1.555 + 1.556 +MOZ_ALWAYS_INLINE 1.557 +static void 1.558 +CacheNumber(ThreadSafeContext *cx, double d, JSFlatString *str) 1.559 +{ 1.560 + if (!cx->isExclusiveContext()) 1.561 + return; 1.562 + 1.563 + if (JSCompartment *comp = cx->asExclusiveContext()->compartment()) 1.564 + comp->dtoaCache.cache(10, d, str); 1.565 +} 1.566 + 1.567 +MOZ_ALWAYS_INLINE 1.568 +static JSFlatString * 1.569 +LookupInt32ToString(ThreadSafeContext *cx, int32_t si) 1.570 +{ 1.571 + if (si >= 0 && StaticStrings::hasInt(si)) 1.572 + return cx->staticStrings().getInt(si); 1.573 + 1.574 + return LookupDtoaCache(cx, si); 1.575 +} 1.576 + 1.577 +template <typename T> 1.578 +MOZ_ALWAYS_INLINE 1.579 +static T * 1.580 +BackfillInt32InBuffer(int32_t si, T *buffer, size_t size, size_t *length) 1.581 +{ 1.582 + uint32_t ui = Abs(si); 1.583 + JS_ASSERT_IF(si == INT32_MIN, ui == uint32_t(INT32_MAX) + 1); 1.584 + 1.585 + RangedPtr<T> end(buffer + size - 1, buffer, size); 1.586 + *end = '\0'; 1.587 + RangedPtr<T> start = BackfillIndexInCharBuffer(ui, end); 1.588 + if (si < 0) 1.589 + *--start = '-'; 1.590 + 1.591 + *length = end - start; 1.592 + return start.get(); 1.593 +} 1.594 + 1.595 +template <AllowGC allowGC> 1.596 +JSFlatString * 1.597 +js::Int32ToString(ThreadSafeContext *cx, int32_t si) 1.598 +{ 1.599 + if (JSFlatString *str = LookupInt32ToString(cx, si)) 1.600 + return str; 1.601 + 1.602 + JSFatInlineString *str = js_NewGCFatInlineString<allowGC>(cx); 1.603 + if (!str) 1.604 + return nullptr; 1.605 + 1.606 + jschar buffer[JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1]; 1.607 + size_t length; 1.608 + jschar *start = BackfillInt32InBuffer(si, buffer, 1.609 + JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1, &length); 1.610 + 1.611 + PodCopy(str->init(length), start, length + 1); 1.612 + 1.613 + CacheNumber(cx, si, str); 1.614 + return str; 1.615 +} 1.616 + 1.617 +template JSFlatString * 1.618 +js::Int32ToString<CanGC>(ThreadSafeContext *cx, int32_t si); 1.619 + 1.620 +template JSFlatString * 1.621 +js::Int32ToString<NoGC>(ThreadSafeContext *cx, int32_t si); 1.622 + 1.623 +JSAtom * 1.624 +js::Int32ToAtom(ExclusiveContext *cx, int32_t si) 1.625 +{ 1.626 + if (JSFlatString *str = LookupInt32ToString(cx, si)) 1.627 + return js::AtomizeString(cx, str); 1.628 + 1.629 + char buffer[JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1]; 1.630 + size_t length; 1.631 + char *start = BackfillInt32InBuffer(si, buffer, JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1, &length); 1.632 + 1.633 + JSAtom *atom = Atomize(cx, start, length); 1.634 + if (!atom) 1.635 + return nullptr; 1.636 + 1.637 + CacheNumber(cx, si, atom); 1.638 + return atom; 1.639 +} 1.640 + 1.641 +/* Returns a non-nullptr pointer to inside cbuf. */ 1.642 +static char * 1.643 +Int32ToCString(ToCStringBuf *cbuf, int32_t i, size_t *len, int base = 10) 1.644 +{ 1.645 + uint32_t u = Abs(i); 1.646 + 1.647 + RangedPtr<char> cp(cbuf->sbuf + ToCStringBuf::sbufSize - 1, cbuf->sbuf, ToCStringBuf::sbufSize); 1.648 + char *end = cp.get(); 1.649 + *cp = '\0'; 1.650 + 1.651 + /* Build the string from behind. */ 1.652 + switch (base) { 1.653 + case 10: 1.654 + cp = BackfillIndexInCharBuffer(u, cp); 1.655 + break; 1.656 + case 16: 1.657 + do { 1.658 + unsigned newu = u / 16; 1.659 + *--cp = "0123456789abcdef"[u - newu * 16]; 1.660 + u = newu; 1.661 + } while (u != 0); 1.662 + break; 1.663 + default: 1.664 + JS_ASSERT(base >= 2 && base <= 36); 1.665 + do { 1.666 + unsigned newu = u / base; 1.667 + *--cp = "0123456789abcdefghijklmnopqrstuvwxyz"[u - newu * base]; 1.668 + u = newu; 1.669 + } while (u != 0); 1.670 + break; 1.671 + } 1.672 + if (i < 0) 1.673 + *--cp = '-'; 1.674 + 1.675 + *len = end - cp.get(); 1.676 + return cp.get(); 1.677 +} 1.678 + 1.679 +template <AllowGC allowGC> 1.680 +static JSString * JS_FASTCALL 1.681 +js_NumberToStringWithBase(ThreadSafeContext *cx, double d, int base); 1.682 + 1.683 +MOZ_ALWAYS_INLINE bool 1.684 +num_toString_impl(JSContext *cx, CallArgs args) 1.685 +{ 1.686 + JS_ASSERT(IsNumber(args.thisv())); 1.687 + 1.688 + double d = Extract(args.thisv()); 1.689 + 1.690 + int32_t base = 10; 1.691 + if (args.hasDefined(0)) { 1.692 + double d2; 1.693 + if (!ToInteger(cx, args[0], &d2)) 1.694 + return false; 1.695 + 1.696 + if (d2 < 2 || d2 > 36) { 1.697 + JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_RADIX); 1.698 + return false; 1.699 + } 1.700 + 1.701 + base = int32_t(d2); 1.702 + } 1.703 + JSString *str = js_NumberToStringWithBase<CanGC>(cx, d, base); 1.704 + if (!str) { 1.705 + JS_ReportOutOfMemory(cx); 1.706 + return false; 1.707 + } 1.708 + args.rval().setString(str); 1.709 + return true; 1.710 +} 1.711 + 1.712 +bool 1.713 +js_num_toString(JSContext *cx, unsigned argc, Value *vp) 1.714 +{ 1.715 + CallArgs args = CallArgsFromVp(argc, vp); 1.716 + return CallNonGenericMethod<IsNumber, num_toString_impl>(cx, args); 1.717 +} 1.718 + 1.719 +#if !EXPOSE_INTL_API 1.720 +MOZ_ALWAYS_INLINE bool 1.721 +num_toLocaleString_impl(JSContext *cx, CallArgs args) 1.722 +{ 1.723 + JS_ASSERT(IsNumber(args.thisv())); 1.724 + 1.725 + double d = Extract(args.thisv()); 1.726 + 1.727 + Rooted<JSString*> str(cx, js_NumberToStringWithBase<CanGC>(cx, d, 10)); 1.728 + if (!str) { 1.729 + JS_ReportOutOfMemory(cx); 1.730 + return false; 1.731 + } 1.732 + 1.733 + /* 1.734 + * Create the string, move back to bytes to make string twiddling 1.735 + * a bit easier and so we can insert platform charset seperators. 1.736 + */ 1.737 + JSAutoByteString numBytes(cx, str); 1.738 + if (!numBytes) 1.739 + return false; 1.740 + const char *num = numBytes.ptr(); 1.741 + if (!num) 1.742 + return false; 1.743 + 1.744 + /* 1.745 + * Find the first non-integer value, whether it be a letter as in 1.746 + * 'Infinity', a decimal point, or an 'e' from exponential notation. 1.747 + */ 1.748 + const char *nint = num; 1.749 + if (*nint == '-') 1.750 + nint++; 1.751 + while (*nint >= '0' && *nint <= '9') 1.752 + nint++; 1.753 + int digits = nint - num; 1.754 + const char *end = num + digits; 1.755 + if (!digits) { 1.756 + args.rval().setString(str); 1.757 + return true; 1.758 + } 1.759 + 1.760 + JSRuntime *rt = cx->runtime(); 1.761 + size_t thousandsLength = strlen(rt->thousandsSeparator); 1.762 + size_t decimalLength = strlen(rt->decimalSeparator); 1.763 + 1.764 + /* Figure out how long resulting string will be. */ 1.765 + int buflen = strlen(num); 1.766 + if (*nint == '.') 1.767 + buflen += decimalLength - 1; /* -1 to account for existing '.' */ 1.768 + 1.769 + const char *numGrouping; 1.770 + const char *tmpGroup; 1.771 + numGrouping = tmpGroup = rt->numGrouping; 1.772 + int remainder = digits; 1.773 + if (*num == '-') 1.774 + remainder--; 1.775 + 1.776 + while (*tmpGroup != CHAR_MAX && *tmpGroup != '\0') { 1.777 + if (*tmpGroup >= remainder) 1.778 + break; 1.779 + buflen += thousandsLength; 1.780 + remainder -= *tmpGroup; 1.781 + tmpGroup++; 1.782 + } 1.783 + 1.784 + int nrepeat; 1.785 + if (*tmpGroup == '\0' && *numGrouping != '\0') { 1.786 + nrepeat = (remainder - 1) / tmpGroup[-1]; 1.787 + buflen += thousandsLength * nrepeat; 1.788 + remainder -= nrepeat * tmpGroup[-1]; 1.789 + } else { 1.790 + nrepeat = 0; 1.791 + } 1.792 + tmpGroup--; 1.793 + 1.794 + char *buf = cx->pod_malloc<char>(buflen + 1); 1.795 + if (!buf) 1.796 + return false; 1.797 + 1.798 + char *tmpDest = buf; 1.799 + const char *tmpSrc = num; 1.800 + 1.801 + while (*tmpSrc == '-' || remainder--) { 1.802 + JS_ASSERT(tmpDest - buf < buflen); 1.803 + *tmpDest++ = *tmpSrc++; 1.804 + } 1.805 + while (tmpSrc < end) { 1.806 + JS_ASSERT(tmpDest - buf + ptrdiff_t(thousandsLength) <= buflen); 1.807 + strcpy(tmpDest, rt->thousandsSeparator); 1.808 + tmpDest += thousandsLength; 1.809 + JS_ASSERT(tmpDest - buf + *tmpGroup <= buflen); 1.810 + js_memcpy(tmpDest, tmpSrc, *tmpGroup); 1.811 + tmpDest += *tmpGroup; 1.812 + tmpSrc += *tmpGroup; 1.813 + if (--nrepeat < 0) 1.814 + tmpGroup--; 1.815 + } 1.816 + 1.817 + if (*nint == '.') { 1.818 + JS_ASSERT(tmpDest - buf + ptrdiff_t(decimalLength) <= buflen); 1.819 + strcpy(tmpDest, rt->decimalSeparator); 1.820 + tmpDest += decimalLength; 1.821 + JS_ASSERT(tmpDest - buf + ptrdiff_t(strlen(nint + 1)) <= buflen); 1.822 + strcpy(tmpDest, nint + 1); 1.823 + } else { 1.824 + JS_ASSERT(tmpDest - buf + ptrdiff_t(strlen(nint)) <= buflen); 1.825 + strcpy(tmpDest, nint); 1.826 + } 1.827 + 1.828 + if (cx->runtime()->localeCallbacks && cx->runtime()->localeCallbacks->localeToUnicode) { 1.829 + Rooted<Value> v(cx, StringValue(str)); 1.830 + bool ok = !!cx->runtime()->localeCallbacks->localeToUnicode(cx, buf, &v); 1.831 + if (ok) 1.832 + args.rval().set(v); 1.833 + js_free(buf); 1.834 + return ok; 1.835 + } 1.836 + 1.837 + str = js_NewStringCopyN<CanGC>(cx, buf, buflen); 1.838 + js_free(buf); 1.839 + if (!str) 1.840 + return false; 1.841 + 1.842 + args.rval().setString(str); 1.843 + return true; 1.844 +} 1.845 + 1.846 +static bool 1.847 +num_toLocaleString(JSContext *cx, unsigned argc, Value *vp) 1.848 +{ 1.849 + CallArgs args = CallArgsFromVp(argc, vp); 1.850 + return CallNonGenericMethod<IsNumber, num_toLocaleString_impl>(cx, args); 1.851 +} 1.852 +#endif /* !EXPOSE_INTL_API */ 1.853 + 1.854 +MOZ_ALWAYS_INLINE bool 1.855 +num_valueOf_impl(JSContext *cx, CallArgs args) 1.856 +{ 1.857 + JS_ASSERT(IsNumber(args.thisv())); 1.858 + args.rval().setNumber(Extract(args.thisv())); 1.859 + return true; 1.860 +} 1.861 + 1.862 +bool 1.863 +js_num_valueOf(JSContext *cx, unsigned argc, Value *vp) 1.864 +{ 1.865 + CallArgs args = CallArgsFromVp(argc, vp); 1.866 + return CallNonGenericMethod<IsNumber, num_valueOf_impl>(cx, args); 1.867 +} 1.868 + 1.869 +static const unsigned MAX_PRECISION = 100; 1.870 + 1.871 +static bool 1.872 +ComputePrecisionInRange(JSContext *cx, int minPrecision, int maxPrecision, HandleValue v, 1.873 + int *precision) 1.874 +{ 1.875 + double prec; 1.876 + if (!ToInteger(cx, v, &prec)) 1.877 + return false; 1.878 + if (minPrecision <= prec && prec <= maxPrecision) { 1.879 + *precision = int(prec); 1.880 + return true; 1.881 + } 1.882 + 1.883 + ToCStringBuf cbuf; 1.884 + if (char *numStr = NumberToCString(cx, &cbuf, prec, 10)) 1.885 + JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_PRECISION_RANGE, numStr); 1.886 + return false; 1.887 +} 1.888 + 1.889 +static bool 1.890 +DToStrResult(JSContext *cx, double d, JSDToStrMode mode, int precision, CallArgs args) 1.891 +{ 1.892 + char buf[DTOSTR_VARIABLE_BUFFER_SIZE(MAX_PRECISION + 1)]; 1.893 + char *numStr = js_dtostr(cx->mainThread().dtoaState, buf, sizeof buf, mode, precision, d); 1.894 + if (!numStr) { 1.895 + JS_ReportOutOfMemory(cx); 1.896 + return false; 1.897 + } 1.898 + JSString *str = js_NewStringCopyZ<CanGC>(cx, numStr); 1.899 + if (!str) 1.900 + return false; 1.901 + args.rval().setString(str); 1.902 + return true; 1.903 +} 1.904 + 1.905 +/* 1.906 + * In the following three implementations, we allow a larger range of precision 1.907 + * than ECMA requires; this is permitted by ECMA-262. 1.908 + */ 1.909 +MOZ_ALWAYS_INLINE bool 1.910 +num_toFixed_impl(JSContext *cx, CallArgs args) 1.911 +{ 1.912 + JS_ASSERT(IsNumber(args.thisv())); 1.913 + 1.914 + int precision; 1.915 + if (args.length() == 0) { 1.916 + precision = 0; 1.917 + } else { 1.918 + if (!ComputePrecisionInRange(cx, -20, MAX_PRECISION, args[0], &precision)) 1.919 + return false; 1.920 + } 1.921 + 1.922 + return DToStrResult(cx, Extract(args.thisv()), DTOSTR_FIXED, precision, args); 1.923 +} 1.924 + 1.925 +static bool 1.926 +num_toFixed(JSContext *cx, unsigned argc, Value *vp) 1.927 +{ 1.928 + CallArgs args = CallArgsFromVp(argc, vp); 1.929 + return CallNonGenericMethod<IsNumber, num_toFixed_impl>(cx, args); 1.930 +} 1.931 + 1.932 +MOZ_ALWAYS_INLINE bool 1.933 +num_toExponential_impl(JSContext *cx, CallArgs args) 1.934 +{ 1.935 + JS_ASSERT(IsNumber(args.thisv())); 1.936 + 1.937 + JSDToStrMode mode; 1.938 + int precision; 1.939 + if (!args.hasDefined(0)) { 1.940 + mode = DTOSTR_STANDARD_EXPONENTIAL; 1.941 + precision = 0; 1.942 + } else { 1.943 + mode = DTOSTR_EXPONENTIAL; 1.944 + if (!ComputePrecisionInRange(cx, 0, MAX_PRECISION, args[0], &precision)) 1.945 + return false; 1.946 + } 1.947 + 1.948 + return DToStrResult(cx, Extract(args.thisv()), mode, precision + 1, args); 1.949 +} 1.950 + 1.951 +static bool 1.952 +num_toExponential(JSContext *cx, unsigned argc, Value *vp) 1.953 +{ 1.954 + CallArgs args = CallArgsFromVp(argc, vp); 1.955 + return CallNonGenericMethod<IsNumber, num_toExponential_impl>(cx, args); 1.956 +} 1.957 + 1.958 +MOZ_ALWAYS_INLINE bool 1.959 +num_toPrecision_impl(JSContext *cx, CallArgs args) 1.960 +{ 1.961 + JS_ASSERT(IsNumber(args.thisv())); 1.962 + 1.963 + double d = Extract(args.thisv()); 1.964 + 1.965 + if (!args.hasDefined(0)) { 1.966 + JSString *str = js_NumberToStringWithBase<CanGC>(cx, d, 10); 1.967 + if (!str) { 1.968 + JS_ReportOutOfMemory(cx); 1.969 + return false; 1.970 + } 1.971 + args.rval().setString(str); 1.972 + return true; 1.973 + } 1.974 + 1.975 + int precision; 1.976 + if (!ComputePrecisionInRange(cx, 1, MAX_PRECISION, args[0], &precision)) 1.977 + return false; 1.978 + 1.979 + return DToStrResult(cx, d, DTOSTR_PRECISION, precision, args); 1.980 +} 1.981 + 1.982 +static bool 1.983 +num_toPrecision(JSContext *cx, unsigned argc, Value *vp) 1.984 +{ 1.985 + CallArgs args = CallArgsFromVp(argc, vp); 1.986 + return CallNonGenericMethod<IsNumber, num_toPrecision_impl>(cx, args); 1.987 +} 1.988 + 1.989 +static const JSFunctionSpec number_methods[] = { 1.990 +#if JS_HAS_TOSOURCE 1.991 + JS_FN(js_toSource_str, num_toSource, 0, 0), 1.992 +#endif 1.993 + JS_FN(js_toString_str, js_num_toString, 1, 0), 1.994 +#if EXPOSE_INTL_API 1.995 + JS_SELF_HOSTED_FN(js_toLocaleString_str, "Number_toLocaleString", 0,0), 1.996 +#else 1.997 + JS_FN(js_toLocaleString_str, num_toLocaleString, 0,0), 1.998 +#endif 1.999 + JS_FN(js_valueOf_str, js_num_valueOf, 0, 0), 1.1000 + JS_FN("toFixed", num_toFixed, 1, 0), 1.1001 + JS_FN("toExponential", num_toExponential, 1, 0), 1.1002 + JS_FN("toPrecision", num_toPrecision, 1, 0), 1.1003 + JS_FS_END 1.1004 +}; 1.1005 + 1.1006 + 1.1007 +// ES6 draft ES6 15.7.3.10 1.1008 +static bool 1.1009 +Number_isNaN(JSContext *cx, unsigned argc, Value *vp) 1.1010 +{ 1.1011 + CallArgs args = CallArgsFromVp(argc, vp); 1.1012 + if (args.length() < 1 || !args[0].isDouble()) { 1.1013 + args.rval().setBoolean(false); 1.1014 + return true; 1.1015 + } 1.1016 + args.rval().setBoolean(mozilla::IsNaN(args[0].toDouble())); 1.1017 + return true; 1.1018 +} 1.1019 + 1.1020 +// ES6 draft ES6 15.7.3.11 1.1021 +static bool 1.1022 +Number_isFinite(JSContext *cx, unsigned argc, Value *vp) 1.1023 +{ 1.1024 + CallArgs args = CallArgsFromVp(argc, vp); 1.1025 + if (args.length() < 1 || !args[0].isNumber()) { 1.1026 + args.rval().setBoolean(false); 1.1027 + return true; 1.1028 + } 1.1029 + args.rval().setBoolean(args[0].isInt32() || 1.1030 + mozilla::IsFinite(args[0].toDouble())); 1.1031 + return true; 1.1032 +} 1.1033 + 1.1034 +// ES6 draft ES6 15.7.3.12 1.1035 +static bool 1.1036 +Number_isInteger(JSContext *cx, unsigned argc, Value *vp) 1.1037 +{ 1.1038 + CallArgs args = CallArgsFromVp(argc, vp); 1.1039 + if (args.length() < 1 || !args[0].isNumber()) { 1.1040 + args.rval().setBoolean(false); 1.1041 + return true; 1.1042 + } 1.1043 + Value val = args[0]; 1.1044 + args.rval().setBoolean(val.isInt32() || 1.1045 + (mozilla::IsFinite(val.toDouble()) && 1.1046 + ToInteger(val.toDouble()) == val.toDouble())); 1.1047 + return true; 1.1048 +} 1.1049 + 1.1050 +// ES6 drafult ES6 15.7.3.13 1.1051 +static bool 1.1052 +Number_toInteger(JSContext *cx, unsigned argc, Value *vp) 1.1053 +{ 1.1054 + CallArgs args = CallArgsFromVp(argc, vp); 1.1055 + if (args.length() < 1) { 1.1056 + args.rval().setInt32(0); 1.1057 + return true; 1.1058 + } 1.1059 + double asint; 1.1060 + if (!ToInteger(cx, args[0], &asint)) 1.1061 + return false; 1.1062 + args.rval().setNumber(asint); 1.1063 + return true; 1.1064 +} 1.1065 + 1.1066 + 1.1067 +static const JSFunctionSpec number_static_methods[] = { 1.1068 + JS_FN("isFinite", Number_isFinite, 1, 0), 1.1069 + JS_FN("isInteger", Number_isInteger, 1, 0), 1.1070 + JS_FN("isNaN", Number_isNaN, 1, 0), 1.1071 + JS_FN("toInteger", Number_toInteger, 1, 0), 1.1072 + /* ES6 additions. */ 1.1073 + JS_FN("parseFloat", num_parseFloat, 1, 0), 1.1074 + JS_FN("parseInt", num_parseInt, 2, 0), 1.1075 + JS_FS_END 1.1076 +}; 1.1077 + 1.1078 + 1.1079 +/* NB: Keep this in synch with number_constants[]. */ 1.1080 +enum nc_slot { 1.1081 + NC_NaN, 1.1082 + NC_POSITIVE_INFINITY, 1.1083 + NC_NEGATIVE_INFINITY, 1.1084 + NC_MAX_VALUE, 1.1085 + NC_MIN_VALUE, 1.1086 + NC_MAX_SAFE_INTEGER, 1.1087 + NC_MIN_SAFE_INTEGER, 1.1088 + NC_EPSILON, 1.1089 + NC_LIMIT 1.1090 +}; 1.1091 + 1.1092 +/* 1.1093 + * Some to most C compilers forbid spelling these at compile time, or barf 1.1094 + * if you try, so all but MAX_VALUE are set up by InitRuntimeNumberState 1.1095 + * using union jsdpun. 1.1096 + */ 1.1097 +static JSConstDoubleSpec number_constants[] = { 1.1098 + {0, "NaN", 0,{0,0,0}}, 1.1099 + {0, "POSITIVE_INFINITY", 0,{0,0,0}}, 1.1100 + {0, "NEGATIVE_INFINITY", 0,{0,0,0}}, 1.1101 + {1.7976931348623157E+308, "MAX_VALUE", 0,{0,0,0}}, 1.1102 + {0, "MIN_VALUE", 0,{0,0,0}}, 1.1103 + /* ES6 (April 2014 draft) 20.1.2.6 */ 1.1104 + {9007199254740991, "MAX_SAFE_INTEGER", 0,{0,0,0}}, 1.1105 + /* ES6 (April 2014 draft) 20.1.2.10 */ 1.1106 + {-9007199254740991, "MIN_SAFE_INTEGER", 0,{0,0,0}}, 1.1107 + /* ES6 (May 2013 draft) 15.7.3.7 */ 1.1108 + {2.2204460492503130808472633361816e-16, "EPSILON", 0,{0,0,0}}, 1.1109 + {0,0,0,{0,0,0}} 1.1110 +}; 1.1111 + 1.1112 +#if (defined __GNUC__ && defined __i386__) || \ 1.1113 + (defined __SUNPRO_CC && defined __i386) 1.1114 + 1.1115 +/* 1.1116 + * Set the exception mask to mask all exceptions and set the FPU precision 1.1117 + * to 53 bit mantissa (64 bit doubles). 1.1118 + */ 1.1119 +static inline void FIX_FPU() { 1.1120 + short control; 1.1121 + asm("fstcw %0" : "=m" (control) : ); 1.1122 + control &= ~0x300; // Lower bits 8 and 9 (precision control). 1.1123 + control |= 0x2f3; // Raise bits 0-5 (exception masks) and 9 (64-bit precision). 1.1124 + asm("fldcw %0" : : "m" (control) ); 1.1125 +} 1.1126 + 1.1127 +#else 1.1128 + 1.1129 +#define FIX_FPU() ((void)0) 1.1130 + 1.1131 +#endif 1.1132 + 1.1133 +bool 1.1134 +js::InitRuntimeNumberState(JSRuntime *rt) 1.1135 +{ 1.1136 + FIX_FPU(); 1.1137 + 1.1138 + /* 1.1139 + * Our NaN must be one particular canonical value, because we rely on NaN 1.1140 + * encoding for our value representation. See Value.h. 1.1141 + */ 1.1142 + number_constants[NC_NaN].dval = GenericNaN(); 1.1143 + 1.1144 + number_constants[NC_POSITIVE_INFINITY].dval = mozilla::PositiveInfinity<double>(); 1.1145 + number_constants[NC_NEGATIVE_INFINITY].dval = mozilla::NegativeInfinity<double>(); 1.1146 + 1.1147 + number_constants[NC_MIN_VALUE].dval = MinNumberValue<double>(); 1.1148 + 1.1149 + // XXX If EXPOSE_INTL_API becomes true all the time at some point, 1.1150 + // js::InitRuntimeNumberState is no longer fallible, and we should 1.1151 + // change its return type. 1.1152 +#if !EXPOSE_INTL_API 1.1153 + /* Copy locale-specific separators into the runtime strings. */ 1.1154 + const char *thousandsSeparator, *decimalPoint, *grouping; 1.1155 +#ifdef HAVE_LOCALECONV 1.1156 + struct lconv *locale = localeconv(); 1.1157 + thousandsSeparator = locale->thousands_sep; 1.1158 + decimalPoint = locale->decimal_point; 1.1159 + grouping = locale->grouping; 1.1160 +#else 1.1161 + thousandsSeparator = getenv("LOCALE_THOUSANDS_SEP"); 1.1162 + decimalPoint = getenv("LOCALE_DECIMAL_POINT"); 1.1163 + grouping = getenv("LOCALE_GROUPING"); 1.1164 +#endif 1.1165 + if (!thousandsSeparator) 1.1166 + thousandsSeparator = "'"; 1.1167 + if (!decimalPoint) 1.1168 + decimalPoint = "."; 1.1169 + if (!grouping) 1.1170 + grouping = "\3\0"; 1.1171 + 1.1172 + /* 1.1173 + * We use single malloc to get the memory for all separator and grouping 1.1174 + * strings. 1.1175 + */ 1.1176 + size_t thousandsSeparatorSize = strlen(thousandsSeparator) + 1; 1.1177 + size_t decimalPointSize = strlen(decimalPoint) + 1; 1.1178 + size_t groupingSize = strlen(grouping) + 1; 1.1179 + 1.1180 + char *storage = js_pod_malloc<char>(thousandsSeparatorSize + 1.1181 + decimalPointSize + 1.1182 + groupingSize); 1.1183 + if (!storage) 1.1184 + return false; 1.1185 + 1.1186 + js_memcpy(storage, thousandsSeparator, thousandsSeparatorSize); 1.1187 + rt->thousandsSeparator = storage; 1.1188 + storage += thousandsSeparatorSize; 1.1189 + 1.1190 + js_memcpy(storage, decimalPoint, decimalPointSize); 1.1191 + rt->decimalSeparator = storage; 1.1192 + storage += decimalPointSize; 1.1193 + 1.1194 + js_memcpy(storage, grouping, groupingSize); 1.1195 + rt->numGrouping = grouping; 1.1196 +#endif /* !EXPOSE_INTL_API */ 1.1197 + return true; 1.1198 +} 1.1199 + 1.1200 +#if !EXPOSE_INTL_API 1.1201 +void 1.1202 +js::FinishRuntimeNumberState(JSRuntime *rt) 1.1203 +{ 1.1204 + /* 1.1205 + * The free also releases the memory for decimalSeparator and numGrouping 1.1206 + * strings. 1.1207 + */ 1.1208 + char *storage = const_cast<char *>(rt->thousandsSeparator); 1.1209 + js_free(storage); 1.1210 +} 1.1211 +#endif 1.1212 + 1.1213 +JSObject * 1.1214 +js_InitNumberClass(JSContext *cx, HandleObject obj) 1.1215 +{ 1.1216 + JS_ASSERT(obj->isNative()); 1.1217 + 1.1218 + /* XXX must do at least once per new thread, so do it per JSContext... */ 1.1219 + FIX_FPU(); 1.1220 + 1.1221 + Rooted<GlobalObject*> global(cx, &obj->as<GlobalObject>()); 1.1222 + 1.1223 + RootedObject numberProto(cx, global->createBlankPrototype(cx, &NumberObject::class_)); 1.1224 + if (!numberProto) 1.1225 + return nullptr; 1.1226 + numberProto->as<NumberObject>().setPrimitiveValue(0); 1.1227 + 1.1228 + RootedFunction ctor(cx); 1.1229 + ctor = global->createConstructor(cx, Number, cx->names().Number, 1); 1.1230 + if (!ctor) 1.1231 + return nullptr; 1.1232 + 1.1233 + if (!LinkConstructorAndPrototype(cx, ctor, numberProto)) 1.1234 + return nullptr; 1.1235 + 1.1236 + /* Add numeric constants (MAX_VALUE, NaN, &c.) to the Number constructor. */ 1.1237 + if (!JS_DefineConstDoubles(cx, ctor, number_constants)) 1.1238 + return nullptr; 1.1239 + 1.1240 + if (!DefinePropertiesAndBrand(cx, ctor, nullptr, number_static_methods)) 1.1241 + return nullptr; 1.1242 + 1.1243 + if (!DefinePropertiesAndBrand(cx, numberProto, nullptr, number_methods)) 1.1244 + return nullptr; 1.1245 + 1.1246 + if (!JS_DefineFunctions(cx, global, number_functions)) 1.1247 + return nullptr; 1.1248 + 1.1249 + RootedValue valueNaN(cx, cx->runtime()->NaNValue); 1.1250 + RootedValue valueInfinity(cx, cx->runtime()->positiveInfinityValue); 1.1251 + 1.1252 + /* ES5 15.1.1.1, 15.1.1.2 */ 1.1253 + if (!DefineNativeProperty(cx, global, cx->names().NaN, valueNaN, 1.1254 + JS_PropertyStub, JS_StrictPropertyStub, 1.1255 + JSPROP_PERMANENT | JSPROP_READONLY) || 1.1256 + !DefineNativeProperty(cx, global, cx->names().Infinity, valueInfinity, 1.1257 + JS_PropertyStub, JS_StrictPropertyStub, 1.1258 + JSPROP_PERMANENT | JSPROP_READONLY)) 1.1259 + { 1.1260 + return nullptr; 1.1261 + } 1.1262 + 1.1263 + if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Number, ctor, numberProto)) 1.1264 + return nullptr; 1.1265 + 1.1266 + return numberProto; 1.1267 +} 1.1268 + 1.1269 +static char * 1.1270 +FracNumberToCString(ThreadSafeContext *cx, ToCStringBuf *cbuf, double d, int base = 10) 1.1271 +{ 1.1272 +#ifdef DEBUG 1.1273 + { 1.1274 + int32_t _; 1.1275 + JS_ASSERT(!mozilla::NumberIsInt32(d, &_)); 1.1276 + } 1.1277 +#endif 1.1278 + 1.1279 + char* numStr; 1.1280 + if (base == 10) { 1.1281 + /* 1.1282 + * This is V8's implementation of the algorithm described in the 1.1283 + * following paper: 1.1284 + * 1.1285 + * Printing floating-point numbers quickly and accurately with integers. 1.1286 + * Florian Loitsch, PLDI 2010. 1.1287 + */ 1.1288 + const double_conversion::DoubleToStringConverter &converter 1.1289 + = double_conversion::DoubleToStringConverter::EcmaScriptConverter(); 1.1290 + double_conversion::StringBuilder builder(cbuf->sbuf, cbuf->sbufSize); 1.1291 + converter.ToShortest(d, &builder); 1.1292 + numStr = builder.Finalize(); 1.1293 + } else { 1.1294 + numStr = cbuf->dbuf = js_dtobasestr(cx->dtoaState(), base, d); 1.1295 + } 1.1296 + return numStr; 1.1297 +} 1.1298 + 1.1299 +char * 1.1300 +js::NumberToCString(JSContext *cx, ToCStringBuf *cbuf, double d, int base/* = 10*/) 1.1301 +{ 1.1302 + int32_t i; 1.1303 + size_t len; 1.1304 + return mozilla::NumberIsInt32(d, &i) 1.1305 + ? Int32ToCString(cbuf, i, &len, base) 1.1306 + : FracNumberToCString(cx, cbuf, d, base); 1.1307 +} 1.1308 + 1.1309 +template <AllowGC allowGC> 1.1310 +static JSString * JS_FASTCALL 1.1311 +js_NumberToStringWithBase(ThreadSafeContext *cx, double d, int base) 1.1312 +{ 1.1313 + ToCStringBuf cbuf; 1.1314 + char *numStr; 1.1315 + 1.1316 + /* 1.1317 + * Caller is responsible for error reporting. When called from trace, 1.1318 + * returning nullptr here will cause us to fall of trace and then retry 1.1319 + * from the interpreter (which will report the error). 1.1320 + */ 1.1321 + if (base < 2 || base > 36) 1.1322 + return nullptr; 1.1323 + 1.1324 + JSCompartment *comp = cx->isExclusiveContext() 1.1325 + ? cx->asExclusiveContext()->compartment() 1.1326 + : nullptr; 1.1327 + 1.1328 + int32_t i; 1.1329 + if (mozilla::NumberIsInt32(d, &i)) { 1.1330 + if (base == 10 && StaticStrings::hasInt(i)) 1.1331 + return cx->staticStrings().getInt(i); 1.1332 + if (unsigned(i) < unsigned(base)) { 1.1333 + if (i < 10) 1.1334 + return cx->staticStrings().getInt(i); 1.1335 + jschar c = 'a' + i - 10; 1.1336 + JS_ASSERT(StaticStrings::hasUnit(c)); 1.1337 + return cx->staticStrings().getUnit(c); 1.1338 + } 1.1339 + 1.1340 + if (comp) { 1.1341 + if (JSFlatString *str = comp->dtoaCache.lookup(base, d)) 1.1342 + return str; 1.1343 + } 1.1344 + 1.1345 + size_t len; 1.1346 + numStr = Int32ToCString(&cbuf, i, &len, base); 1.1347 + JS_ASSERT(!cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize); 1.1348 + } else { 1.1349 + if (comp) { 1.1350 + if (JSFlatString *str = comp->dtoaCache.lookup(base, d)) 1.1351 + return str; 1.1352 + } 1.1353 + 1.1354 + numStr = FracNumberToCString(cx, &cbuf, d, base); 1.1355 + if (!numStr) { 1.1356 + js_ReportOutOfMemory(cx); 1.1357 + return nullptr; 1.1358 + } 1.1359 + JS_ASSERT_IF(base == 10, 1.1360 + !cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize); 1.1361 + JS_ASSERT_IF(base != 10, 1.1362 + cbuf.dbuf && cbuf.dbuf == numStr); 1.1363 + } 1.1364 + 1.1365 + JSFlatString *s = js_NewStringCopyZ<allowGC>(cx, numStr); 1.1366 + 1.1367 + if (comp) 1.1368 + comp->dtoaCache.cache(base, d, s); 1.1369 + 1.1370 + return s; 1.1371 +} 1.1372 + 1.1373 +template <AllowGC allowGC> 1.1374 +JSString * 1.1375 +js::NumberToString(ThreadSafeContext *cx, double d) 1.1376 +{ 1.1377 + return js_NumberToStringWithBase<allowGC>(cx, d, 10); 1.1378 +} 1.1379 + 1.1380 +template JSString * 1.1381 +js::NumberToString<CanGC>(ThreadSafeContext *cx, double d); 1.1382 + 1.1383 +template JSString * 1.1384 +js::NumberToString<NoGC>(ThreadSafeContext *cx, double d); 1.1385 + 1.1386 +JSAtom * 1.1387 +js::NumberToAtom(ExclusiveContext *cx, double d) 1.1388 +{ 1.1389 + int32_t si; 1.1390 + if (mozilla::NumberIsInt32(d, &si)) 1.1391 + return Int32ToAtom(cx, si); 1.1392 + 1.1393 + if (JSFlatString *str = LookupDtoaCache(cx, d)) 1.1394 + return AtomizeString(cx, str); 1.1395 + 1.1396 + ToCStringBuf cbuf; 1.1397 + char *numStr = FracNumberToCString(cx, &cbuf, d); 1.1398 + if (!numStr) { 1.1399 + js_ReportOutOfMemory(cx); 1.1400 + return nullptr; 1.1401 + } 1.1402 + JS_ASSERT(!cbuf.dbuf && numStr >= cbuf.sbuf && numStr < cbuf.sbuf + cbuf.sbufSize); 1.1403 + 1.1404 + size_t length = strlen(numStr); 1.1405 + JSAtom *atom = Atomize(cx, numStr, length); 1.1406 + if (!atom) 1.1407 + return nullptr; 1.1408 + 1.1409 + CacheNumber(cx, d, atom); 1.1410 + 1.1411 + return atom; 1.1412 +} 1.1413 + 1.1414 +JSFlatString * 1.1415 +js::NumberToString(JSContext *cx, double d) 1.1416 +{ 1.1417 + if (JSString *str = js_NumberToStringWithBase<CanGC>(cx, d, 10)) 1.1418 + return &str->asFlat(); 1.1419 + return nullptr; 1.1420 +} 1.1421 + 1.1422 +JSFlatString * 1.1423 +js::IndexToString(JSContext *cx, uint32_t index) 1.1424 +{ 1.1425 + if (StaticStrings::hasUint(index)) 1.1426 + return cx->staticStrings().getUint(index); 1.1427 + 1.1428 + JSCompartment *c = cx->compartment(); 1.1429 + if (JSFlatString *str = c->dtoaCache.lookup(10, index)) 1.1430 + return str; 1.1431 + 1.1432 + JSFatInlineString *str = js_NewGCFatInlineString<CanGC>(cx); 1.1433 + if (!str) 1.1434 + return nullptr; 1.1435 + 1.1436 + jschar buffer[JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1]; 1.1437 + RangedPtr<jschar> end(buffer + JSFatInlineString::MAX_FAT_INLINE_LENGTH, 1.1438 + buffer, JSFatInlineString::MAX_FAT_INLINE_LENGTH + 1); 1.1439 + *end = '\0'; 1.1440 + RangedPtr<jschar> start = BackfillIndexInCharBuffer(index, end); 1.1441 + 1.1442 + jschar *dst = str->init(end - start); 1.1443 + PodCopy(dst, start.get(), end - start + 1); 1.1444 + 1.1445 + c->dtoaCache.cache(10, index, str); 1.1446 + return str; 1.1447 +} 1.1448 + 1.1449 +bool JS_FASTCALL 1.1450 +js::NumberValueToStringBuffer(JSContext *cx, const Value &v, StringBuffer &sb) 1.1451 +{ 1.1452 + /* Convert to C-string. */ 1.1453 + ToCStringBuf cbuf; 1.1454 + const char *cstr; 1.1455 + size_t cstrlen; 1.1456 + if (v.isInt32()) { 1.1457 + cstr = Int32ToCString(&cbuf, v.toInt32(), &cstrlen); 1.1458 + JS_ASSERT(cstrlen == strlen(cstr)); 1.1459 + } else { 1.1460 + cstr = NumberToCString(cx, &cbuf, v.toDouble()); 1.1461 + if (!cstr) { 1.1462 + JS_ReportOutOfMemory(cx); 1.1463 + return false; 1.1464 + } 1.1465 + cstrlen = strlen(cstr); 1.1466 + } 1.1467 + 1.1468 + /* 1.1469 + * Inflate to jschar string. The input C-string characters are < 127, so 1.1470 + * even if jschars are UTF-8, all chars should map to one jschar. 1.1471 + */ 1.1472 + JS_ASSERT(!cbuf.dbuf && cstrlen < cbuf.sbufSize); 1.1473 + return sb.appendInflated(cstr, cstrlen); 1.1474 +} 1.1475 + 1.1476 +static bool 1.1477 +CharsToNumber(ThreadSafeContext *cx, const jschar *chars, size_t length, double *result) 1.1478 +{ 1.1479 + if (length == 1) { 1.1480 + jschar c = chars[0]; 1.1481 + if ('0' <= c && c <= '9') 1.1482 + *result = c - '0'; 1.1483 + else if (unicode::IsSpace(c)) 1.1484 + *result = 0.0; 1.1485 + else 1.1486 + *result = GenericNaN(); 1.1487 + return true; 1.1488 + } 1.1489 + 1.1490 + const jschar *end = chars + length; 1.1491 + const jschar *bp = SkipSpace(chars, end); 1.1492 + 1.1493 + /* ECMA doesn't allow signed hex numbers (bug 273467). */ 1.1494 + if (end - bp >= 2 && bp[0] == '0' && (bp[1] == 'x' || bp[1] == 'X')) { 1.1495 + /* 1.1496 + * It's probably a hex number. Accept if there's at least one hex 1.1497 + * digit after the 0x, and if no non-whitespace characters follow all 1.1498 + * the hex digits. 1.1499 + */ 1.1500 + const jschar *endptr; 1.1501 + double d; 1.1502 + if (!GetPrefixInteger(cx, bp + 2, end, 16, &endptr, &d) || 1.1503 + endptr == bp + 2 || 1.1504 + SkipSpace(endptr, end) != end) 1.1505 + { 1.1506 + *result = GenericNaN(); 1.1507 + } else { 1.1508 + *result = d; 1.1509 + } 1.1510 + return true; 1.1511 + } 1.1512 + 1.1513 + /* 1.1514 + * Note that ECMA doesn't treat a string beginning with a '0' as 1.1515 + * an octal number here. This works because all such numbers will 1.1516 + * be interpreted as decimal by js_strtod. Also, any hex numbers 1.1517 + * that have made it here (which can only be negative ones) will 1.1518 + * be treated as 0 without consuming the 'x' by js_strtod. 1.1519 + */ 1.1520 + const jschar *ep; 1.1521 + double d; 1.1522 + if (!js_strtod(cx, bp, end, &ep, &d)) { 1.1523 + *result = GenericNaN(); 1.1524 + return false; 1.1525 + } 1.1526 + 1.1527 + if (SkipSpace(ep, end) != end) 1.1528 + *result = GenericNaN(); 1.1529 + else 1.1530 + *result = d; 1.1531 + 1.1532 + return true; 1.1533 +} 1.1534 + 1.1535 +bool 1.1536 +js::StringToNumber(ThreadSafeContext *cx, JSString *str, double *result) 1.1537 +{ 1.1538 + ScopedThreadSafeStringInspector inspector(str); 1.1539 + if (!inspector.ensureChars(cx)) 1.1540 + return false; 1.1541 + 1.1542 + return CharsToNumber(cx, inspector.chars(), str->length(), result); 1.1543 +} 1.1544 + 1.1545 +bool 1.1546 +js::NonObjectToNumberSlow(ThreadSafeContext *cx, Value v, double *out) 1.1547 +{ 1.1548 + JS_ASSERT(!v.isNumber()); 1.1549 + JS_ASSERT(!v.isObject()); 1.1550 + 1.1551 + if (v.isString()) 1.1552 + return StringToNumber(cx, v.toString(), out); 1.1553 + if (v.isBoolean()) { 1.1554 + *out = v.toBoolean() ? 1.0 : 0.0; 1.1555 + return true; 1.1556 + } 1.1557 + if (v.isNull()) { 1.1558 + *out = 0.0; 1.1559 + return true; 1.1560 + } 1.1561 + 1.1562 + JS_ASSERT(v.isUndefined()); 1.1563 + *out = GenericNaN(); 1.1564 + return true; 1.1565 +} 1.1566 + 1.1567 +#if defined(_MSC_VER) 1.1568 +# pragma optimize("g", off) 1.1569 +#endif 1.1570 + 1.1571 +bool 1.1572 +js::ToNumberSlow(ExclusiveContext *cx, Value v, double *out) 1.1573 +{ 1.1574 + JS_ASSERT(!v.isNumber()); 1.1575 + goto skip_int_double; 1.1576 + for (;;) { 1.1577 + if (v.isNumber()) { 1.1578 + *out = v.toNumber(); 1.1579 + return true; 1.1580 + } 1.1581 + 1.1582 + skip_int_double: 1.1583 + if (!v.isObject()) 1.1584 + return NonObjectToNumberSlow(cx, v, out); 1.1585 + 1.1586 + if (!cx->isJSContext()) 1.1587 + return false; 1.1588 + 1.1589 + RootedValue v2(cx, v); 1.1590 + if (!ToPrimitive(cx->asJSContext(), JSTYPE_NUMBER, &v2)) 1.1591 + return false; 1.1592 + v = v2; 1.1593 + if (v.isObject()) 1.1594 + break; 1.1595 + } 1.1596 + 1.1597 + *out = GenericNaN(); 1.1598 + return true; 1.1599 +} 1.1600 + 1.1601 +JS_PUBLIC_API(bool) 1.1602 +js::ToNumberSlow(JSContext *cx, Value v, double *out) 1.1603 +{ 1.1604 + return ToNumberSlow(static_cast<ExclusiveContext *>(cx), v, out); 1.1605 +} 1.1606 + 1.1607 +#if defined(_MSC_VER) 1.1608 +# pragma optimize("", on) 1.1609 +#endif 1.1610 + 1.1611 +/* 1.1612 + * Convert a value to an int64_t, according to the WebIDL rules for long long 1.1613 + * conversion. Return converted value in *out on success, false on failure. 1.1614 + */ 1.1615 +JS_PUBLIC_API(bool) 1.1616 +js::ToInt64Slow(JSContext *cx, const HandleValue v, int64_t *out) 1.1617 +{ 1.1618 + JS_ASSERT(!v.isInt32()); 1.1619 + double d; 1.1620 + if (v.isDouble()) { 1.1621 + d = v.toDouble(); 1.1622 + } else { 1.1623 + if (!ToNumberSlow(cx, v, &d)) 1.1624 + return false; 1.1625 + } 1.1626 + *out = ToInt64(d); 1.1627 + return true; 1.1628 +} 1.1629 + 1.1630 +/* 1.1631 + * Convert a value to an uint64_t, according to the WebIDL rules for unsigned long long 1.1632 + * conversion. Return converted value in *out on success, false on failure. 1.1633 + */ 1.1634 +JS_PUBLIC_API(bool) 1.1635 +js::ToUint64Slow(JSContext *cx, const HandleValue v, uint64_t *out) 1.1636 +{ 1.1637 + JS_ASSERT(!v.isInt32()); 1.1638 + double d; 1.1639 + if (v.isDouble()) { 1.1640 + d = v.toDouble(); 1.1641 + } else { 1.1642 + if (!ToNumberSlow(cx, v, &d)) 1.1643 + return false; 1.1644 + } 1.1645 + *out = ToUint64(d); 1.1646 + return true; 1.1647 +} 1.1648 + 1.1649 +template <typename ContextType, 1.1650 + bool (*ToNumberSlowFn)(ContextType *, Value, double *), 1.1651 + typename ValueType> 1.1652 +static bool 1.1653 +ToInt32SlowImpl(ContextType *cx, const ValueType v, int32_t *out) 1.1654 +{ 1.1655 + JS_ASSERT(!v.isInt32()); 1.1656 + double d; 1.1657 + if (v.isDouble()) { 1.1658 + d = v.toDouble(); 1.1659 + } else { 1.1660 + if (!ToNumberSlowFn(cx, v, &d)) 1.1661 + return false; 1.1662 + } 1.1663 + *out = ToInt32(d); 1.1664 + return true; 1.1665 +} 1.1666 + 1.1667 +JS_PUBLIC_API(bool) 1.1668 +js::ToInt32Slow(JSContext *cx, const HandleValue v, int32_t *out) 1.1669 +{ 1.1670 + return ToInt32SlowImpl<JSContext, ToNumberSlow>(cx, v, out); 1.1671 +} 1.1672 + 1.1673 +bool 1.1674 +js::NonObjectToInt32Slow(ThreadSafeContext *cx, const Value &v, int32_t *out) 1.1675 +{ 1.1676 + return ToInt32SlowImpl<ThreadSafeContext, NonObjectToNumberSlow>(cx, v, out); 1.1677 +} 1.1678 + 1.1679 +template <typename ContextType, 1.1680 + bool (*ToNumberSlowFn)(ContextType *, Value, double *), 1.1681 + typename ValueType> 1.1682 +static bool 1.1683 +ToUint32SlowImpl(ContextType *cx, const ValueType v, uint32_t *out) 1.1684 +{ 1.1685 + JS_ASSERT(!v.isInt32()); 1.1686 + double d; 1.1687 + if (v.isDouble()) { 1.1688 + d = v.toDouble(); 1.1689 + } else { 1.1690 + if (!ToNumberSlowFn(cx, v, &d)) 1.1691 + return false; 1.1692 + } 1.1693 + *out = ToUint32(d); 1.1694 + return true; 1.1695 +} 1.1696 + 1.1697 +JS_PUBLIC_API(bool) 1.1698 +js::ToUint32Slow(JSContext *cx, const HandleValue v, uint32_t *out) 1.1699 +{ 1.1700 + return ToUint32SlowImpl<JSContext, ToNumberSlow>(cx, v, out); 1.1701 +} 1.1702 + 1.1703 +bool 1.1704 +js::NonObjectToUint32Slow(ThreadSafeContext *cx, const Value &v, uint32_t *out) 1.1705 +{ 1.1706 + return ToUint32SlowImpl<ThreadSafeContext, NonObjectToNumberSlow>(cx, v, out); 1.1707 +} 1.1708 + 1.1709 +JS_PUBLIC_API(bool) 1.1710 +js::ToUint16Slow(JSContext *cx, const HandleValue v, uint16_t *out) 1.1711 +{ 1.1712 + JS_ASSERT(!v.isInt32()); 1.1713 + double d; 1.1714 + if (v.isDouble()) { 1.1715 + d = v.toDouble(); 1.1716 + } else if (!ToNumberSlow(cx, v, &d)) { 1.1717 + return false; 1.1718 + } 1.1719 + 1.1720 + if (d == 0 || !mozilla::IsFinite(d)) { 1.1721 + *out = 0; 1.1722 + return true; 1.1723 + } 1.1724 + 1.1725 + uint16_t u = (uint16_t) d; 1.1726 + if ((double)u == d) { 1.1727 + *out = u; 1.1728 + return true; 1.1729 + } 1.1730 + 1.1731 + bool neg = (d < 0); 1.1732 + d = floor(neg ? -d : d); 1.1733 + d = neg ? -d : d; 1.1734 + unsigned m = JS_BIT(16); 1.1735 + d = fmod(d, (double) m); 1.1736 + if (d < 0) 1.1737 + d += m; 1.1738 + *out = (uint16_t) d; 1.1739 + return true; 1.1740 +} 1.1741 + 1.1742 +bool 1.1743 +js_strtod(ThreadSafeContext *cx, const jschar *s, const jschar *send, 1.1744 + const jschar **ep, double *dp) 1.1745 +{ 1.1746 + size_t i; 1.1747 + char cbuf[32]; 1.1748 + char *cstr, *istr, *estr; 1.1749 + bool negative; 1.1750 + double d; 1.1751 + 1.1752 + const jschar *s1 = SkipSpace(s, send); 1.1753 + size_t length = send - s1; 1.1754 + 1.1755 + /* Use cbuf to avoid malloc */ 1.1756 + if (length >= sizeof cbuf) { 1.1757 + cstr = (char *) cx->malloc_(length + 1); 1.1758 + if (!cstr) 1.1759 + return false; 1.1760 + } else { 1.1761 + cstr = cbuf; 1.1762 + } 1.1763 + 1.1764 + for (i = 0; i != length; i++) { 1.1765 + if (s1[i] >> 8) 1.1766 + break; 1.1767 + cstr[i] = (char)s1[i]; 1.1768 + } 1.1769 + cstr[i] = 0; 1.1770 + 1.1771 + istr = cstr; 1.1772 + if ((negative = (*istr == '-')) != 0 || *istr == '+') 1.1773 + istr++; 1.1774 + if (*istr == 'I' && !strncmp(istr, "Infinity", 8)) { 1.1775 + d = negative ? NegativeInfinity<double>() : PositiveInfinity<double>(); 1.1776 + estr = istr + 8; 1.1777 + } else { 1.1778 + int err; 1.1779 + d = js_strtod_harder(cx->dtoaState(), cstr, &estr, &err); 1.1780 + } 1.1781 + 1.1782 + i = estr - cstr; 1.1783 + if (cstr != cbuf) 1.1784 + js_free(cstr); 1.1785 + *ep = i ? s1 + i : s; 1.1786 + *dp = d; 1.1787 + return true; 1.1788 +}