summaryrefslogtreecommitdiff
path: root/js/src/vm
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/vm')
-rw-r--r--js/src/vm/ArrayBufferObject.cpp21
-rw-r--r--js/src/vm/ArrayBufferObject.h16
-rw-r--r--js/src/vm/BigIntType.cpp3260
-rw-r--r--js/src/vm/BigIntType.h386
-rw-r--r--js/src/vm/CommonPropertyNames.h4
-rw-r--r--js/src/vm/EqualityOperations.cpp20
-rw-r--r--js/src/vm/GlobalObject.cpp1
-rw-r--r--js/src/vm/GlobalObject.h30
-rw-r--r--js/src/vm/Interpreter-inl.h287
-rw-r--r--js/src/vm/Interpreter.cpp214
-rw-r--r--js/src/vm/Interpreter.h3
-rw-r--r--js/src/vm/MemoryMetrics.cpp8
-rw-r--r--js/src/vm/NativeObject-inl.h11
-rw-r--r--js/src/vm/NativeObject.cpp62
-rw-r--r--js/src/vm/NativeObject.h5
-rw-r--r--js/src/vm/ObjectGroup.cpp2
-rw-r--r--js/src/vm/Opcodes.h12
-rw-r--r--js/src/vm/RegExpObject.h3
-rw-r--r--js/src/vm/Runtime.cpp3
-rw-r--r--js/src/vm/SelfHosting.cpp62
-rw-r--r--js/src/vm/Stopwatch.cpp6
-rw-r--r--js/src/vm/Stopwatch.h3
-rw-r--r--js/src/vm/StringBuffer.cpp7
-rw-r--r--js/src/vm/StructuredClone.cpp78
-rw-r--r--js/src/vm/TypeInference-inl.h4
-rw-r--r--js/src/vm/TypeInference.cpp8
-rw-r--r--js/src/vm/TypeInference.h18
-rw-r--r--js/src/vm/TypedArrayCommon.h103
-rw-r--r--js/src/vm/TypedArrayObject.cpp621
-rw-r--r--js/src/vm/TypedArrayObject.h38
-rw-r--r--js/src/vm/UbiNode.cpp10
31 files changed, 4935 insertions, 371 deletions
diff --git a/js/src/vm/ArrayBufferObject.cpp b/js/src/vm/ArrayBufferObject.cpp
index 5d355ada9d..3bed40af47 100644
--- a/js/src/vm/ArrayBufferObject.cpp
+++ b/js/src/vm/ArrayBufferObject.cpp
@@ -264,27 +264,24 @@ ArrayBufferObject::fun_isView(JSContext* cx, unsigned argc, Value* vp)
return true;
}
-/*
- * new ArrayBuffer(byteLength)
- */
+
+// ES2017 draft 24.1.2.1
bool
ArrayBufferObject::class_constructor(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
+ // Step 1.
if (!ThrowIfNotConstructing(cx, args, "ArrayBuffer"))
return false;
- int32_t nbytes = 0;
- if (argc > 0 && !ToInt32(cx, args[0], &nbytes))
+ // Step 2.
+ uint64_t byteLength;
+ if (!ToIndex(cx, args.get(0), &byteLength))
return false;
- if (nbytes < 0) {
- /*
- * We're just not going to support arrays that are bigger than what will fit
- * as an integer value; if someone actually ever complains (validly), then we
- * can fix.
- */
+ // Non-standard: Refuse to allocate buffers larger than ~2 GiB.
+ if (byteLength > INT32_MAX) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_ARRAY_LENGTH);
return false;
}
@@ -294,7 +291,7 @@ ArrayBufferObject::class_constructor(JSContext* cx, unsigned argc, Value* vp)
if (!GetPrototypeFromConstructor(cx, newTarget, &proto))
return false;
- JSObject* bufobj = create(cx, uint32_t(nbytes), proto);
+ JSObject* bufobj = create(cx, uint32_t(byteLength), proto);
if (!bufobj)
return false;
args.rval().setObject(*bufobj);
diff --git a/js/src/vm/ArrayBufferObject.h b/js/src/vm/ArrayBufferObject.h
index 87dce34ba1..4ff7962cfb 100644
--- a/js/src/vm/ArrayBufferObject.h
+++ b/js/src/vm/ArrayBufferObject.h
@@ -463,9 +463,11 @@ struct uint8_clamped {
explicit uint8_clamped(uint8_t x) { *this = x; }
explicit uint8_clamped(uint16_t x) { *this = x; }
explicit uint8_clamped(uint32_t x) { *this = x; }
+ explicit uint8_clamped(uint64_t x) { *this = x; }
explicit uint8_clamped(int8_t x) { *this = x; }
explicit uint8_clamped(int16_t x) { *this = x; }
explicit uint8_clamped(int32_t x) { *this = x; }
+ explicit uint8_clamped(int64_t x) { *this = x; }
explicit uint8_clamped(double x) { *this = x; }
uint8_clamped& operator=(const uint8_clamped& x) = default;
@@ -485,6 +487,11 @@ struct uint8_clamped {
return *this;
}
+ uint8_clamped& operator=(uint64_t x) {
+ val = (x > 255) ? 255 : uint8_t(x);
+ return *this;
+ }
+
uint8_clamped& operator=(int8_t x) {
val = (x >= 0) ? uint8_t(x) : 0;
return *this;
@@ -508,6 +515,15 @@ struct uint8_clamped {
return *this;
}
+ uint8_clamped& operator=(int64_t x) {
+ val = (x >= 0)
+ ? ((x < 255)
+ ? uint8_t(x)
+ : 255)
+ : 0;
+ return *this;
+ }
+
uint8_clamped& operator=(const double x) {
val = uint8_t(ClampDoubleToUint8(x));
return *this;
diff --git a/js/src/vm/BigIntType.cpp b/js/src/vm/BigIntType.cpp
new file mode 100644
index 0000000000..8382c641fa
--- /dev/null
+++ b/js/src/vm/BigIntType.cpp
@@ -0,0 +1,3260 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * Portions of this code taken from WebKit, whose copyright is as follows:
+ *
+ * Copyright (C) 2017 Caio Lima <ticaiolima@gmail.com>
+ * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Portions of this code taken from V8, whose copyright notice is as follows:
+ *
+ * Copyright 2017 the V8 project authors. All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Portions of this code taken from Dart, whose copyright notice is as follows:
+ *
+ * Copyright (c) 2014 the Dart project authors. Please see the AUTHORS file
+ * [1] for details. All rights reserved. Use of this source code is governed by
+ * a BSD-style license that can be found in the LICENSE file [2].
+ *
+ * [1] https://github.com/dart-lang/sdk/blob/master/AUTHORS
+ * [2] https://github.com/dart-lang/sdk/blob/master/LICENSE
+ *
+ * Portions of this code taken from Go, whose copyright notice is as follows:
+ *
+ * Copyright 2009 The Go Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style
+ * license that can be found in the LICENSE file [3].
+ *
+ * [3] https://golang.org/LICENSE
+ */
+
+#include "vm/BigIntType.h"
+
+#include "mozilla/Casting.h"
+#include "mozilla/FloatingPoint.h"
+#include "mozilla/HashFunctions.h"
+#include "mozilla/MathAlgorithms.h"
+#include "mozilla/Maybe.h"
+#include "mozilla/Range.h"
+#include "mozilla/RangedPtr.h"
+#include "mozilla/WrappingOperations.h"
+
+#include <functional>
+#include <limits>
+#include <math.h>
+#include <memory>
+
+#include "jsapi.h"
+#include "jsnum.h"
+#include "jscntxt.h"
+
+#include "builtin/BigInt.h"
+#include "gc/Allocator.h"
+#include "js/Initialization.h"
+#include "js/Utility.h"
+#include "vm/SelfHosting.h"
+
+#include "vm/String.h"
+
+using namespace js;
+
+using mozilla::Abs;
+using mozilla::AssertedCast;
+using mozilla::BitwiseCast;
+using mozilla::IsFinite;
+using mozilla::Maybe;
+using mozilla::NegativeInfinity;
+using mozilla::Nothing;
+using mozilla::PositiveInfinity;
+using mozilla::Range;
+using mozilla::RangedPtr;
+using mozilla::Some;
+using mozilla::WrapToSigned;
+
+static inline unsigned DigitLeadingZeroes(BigInt::Digit x) {
+ return sizeof(x) == 4 ? mozilla::CountLeadingZeroes32(x)
+ : mozilla::CountLeadingZeroes64(x);
+}
+
+BigInt* BigInt::createUninitialized(ExclusiveContext* cx, size_t length,
+ bool isNegative) {
+ if (length > MaxDigitLength) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TOO_LARGE);
+ }
+ return nullptr;
+ }
+
+ UniquePtr<Digit[], JS::FreePolicy> heapDigits;
+ if (length > InlineDigitsLength) {
+ heapDigits = cx->make_pod_array<Digit>(length);
+ if (!heapDigits) {
+ return nullptr;
+ }
+ } else {
+ heapDigits = nullptr;
+ }
+
+ BigInt* x = Allocate<BigInt>(cx);
+ if (!x) {
+ return nullptr;
+ }
+
+ x->lengthSignAndReservedBits_ =
+ (length << LengthShift) | (isNegative ? SignBit : 0);
+ MOZ_ASSERT(x->digitLength() == length);
+ MOZ_ASSERT(x->isNegative() == isNegative);
+
+ if (heapDigits) {
+ x->heapDigits_ = heapDigits.release();
+ }
+
+ return x;
+}
+
+void BigInt::initializeDigitsToZero() {
+ auto digs = digits();
+ std::uninitialized_fill_n(digs.begin(), digs.Length(), 0);
+}
+
+void BigInt::finalize(js::FreeOp* fop) {
+ if (hasHeapDigits()) {
+ fop->free_(heapDigits_);
+ }
+}
+
+js::HashNumber BigInt::hash() {
+ js::HashNumber h =
+ mozilla::HashBytes(digits().data(), digitLength() * sizeof(Digit));
+ return mozilla::AddToHash(h, isNegative());
+}
+
+size_t BigInt::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
+ return hasInlineDigits() ? 0 : mallocSizeOf(heapDigits_);
+}
+
+BigInt* BigInt::zero(ExclusiveContext* cx) {
+ return createUninitialized(cx, 0, false);
+}
+
+BigInt* BigInt::one(ExclusiveContext* cx) {
+ BigInt* ret = createUninitialized(cx, 1, false);
+
+ if (!ret) {
+ return nullptr;
+ }
+
+ ret->setDigit(0, 1);
+
+ return ret;
+}
+
+BigInt* BigInt::neg(ExclusiveContext* cx, HandleBigInt x) {
+ if (x->isZero()) {
+ return x;
+ }
+
+ BigInt* result = copy(cx, x);
+ if (!result) {
+ return nullptr;
+ }
+ result->lengthSignAndReservedBits_ ^= SignBit;
+ return result;
+}
+
+#if !defined(JS_64BIT)
+#define HAVE_TWO_DIGIT 1
+using TwoDigit = uint64_t;
+#elif defined(HAVE_INT128_SUPPORT)
+#define HAVE_TWO_DIGIT 1
+using TwoDigit = __uint128_t;
+#endif
+
+inline BigInt::Digit BigInt::digitMul(Digit a, Digit b, Digit* high) {
+#if defined(HAVE_TWO_DIGIT)
+ TwoDigit result = static_cast<TwoDigit>(a) * static_cast<TwoDigit>(b);
+ *high = result >> DigitBits;
+
+ return static_cast<Digit>(result);
+#else
+ // Multiply in half-pointer-sized chunks.
+ // For inputs [AH AL]*[BH BL], the result is:
+ //
+ // [AL*BL] // rLow
+ // + [AL*BH] // rMid1
+ // + [AH*BL] // rMid2
+ // + [AH*BH] // rHigh
+ // = [R4 R3 R2 R1] // high = [R4 R3], low = [R2 R1]
+ //
+ // Where of course we must be careful with carries between the columns.
+ Digit aLow = a & HalfDigitMask;
+ Digit aHigh = a >> HalfDigitBits;
+ Digit bLow = b & HalfDigitMask;
+ Digit bHigh = b >> HalfDigitBits;
+
+ Digit rLow = aLow * bLow;
+ Digit rMid1 = aLow * bHigh;
+ Digit rMid2 = aHigh * bLow;
+ Digit rHigh = aHigh * bHigh;
+
+ Digit carry = 0;
+ Digit low = digitAdd(rLow, rMid1 << HalfDigitBits, &carry);
+ low = digitAdd(low, rMid2 << HalfDigitBits, &carry);
+
+ *high = (rMid1 >> HalfDigitBits) + (rMid2 >> HalfDigitBits) + rHigh + carry;
+
+ return low;
+#endif
+}
+
+BigInt::Digit BigInt::digitDiv(Digit high, Digit low, Digit divisor,
+ Digit* remainder) {
+ MOZ_ASSERT(high < divisor, "division must not overflow");
+#if defined(__x86_64__)
+ Digit quotient;
+ Digit rem;
+ __asm__("divq %[divisor]"
+ // Outputs: `quotient` will be in rax, `rem` in rdx.
+ : "=a"(quotient), "=d"(rem)
+ // Inputs: put `high` into rdx, `low` into rax, and `divisor` into
+ // any register or stack slot.
+ : "d"(high), "a"(low), [divisor] "rm"(divisor));
+ *remainder = rem;
+ return quotient;
+#elif defined(__i386__)
+ Digit quotient;
+ Digit rem;
+ __asm__("divl %[divisor]"
+ // Outputs: `quotient` will be in eax, `rem` in edx.
+ : "=a"(quotient), "=d"(rem)
+ // Inputs: put `high` into edx, `low` into eax, and `divisor` into
+ // any register or stack slot.
+ : "d"(high), "a"(low), [divisor] "rm"(divisor));
+ *remainder = rem;
+ return quotient;
+#else
+ static constexpr Digit HalfDigitBase = 1ull << HalfDigitBits;
+ // Adapted from Warren, Hacker's Delight, p. 152.
+ unsigned s = DigitLeadingZeroes(divisor);
+ // If `s` is DigitBits here, it causes an undefined behavior.
+ // But `s` is never DigitBits since `divisor` is never zero here.
+ MOZ_ASSERT(s != DigitBits);
+ divisor <<= s;
+
+ Digit vn1 = divisor >> HalfDigitBits;
+ Digit vn0 = divisor & HalfDigitMask;
+
+ // `sZeroMask` which is 0 if s == 0 and all 1-bits otherwise.
+ //
+ // `s` can be 0. If `s` is 0, performing "low >> (DigitBits - s)" must not
+ // be done since it causes an undefined behavior since `>> DigitBits` is
+ // undefined in C++. Quoted from C++ spec, "The type of the result is that of
+ // the promoted left operand.
+ //
+ // The behavior is undefined if the right operand is negative, or greater
+ // than or equal to the length in bits of the promoted left operand". We
+ // mask the right operand of the shift by `shiftMask` (`DigitBits - 1`),
+ // which makes `DigitBits - 0` zero.
+ //
+ // This shifting produces a value which covers 0 < `s` <= (DigitBits - 1)
+ // cases. `s` == DigitBits never happen as we asserted. Since `sZeroMask`
+ // clears the value in the case of `s` == 0, `s` == 0 case is also covered.
+ static_assert(sizeof(intptr_t) == sizeof(Digit),
+ "unexpected size of BigInt::Digit");
+ Digit sZeroMask =
+ static_cast<Digit>((-static_cast<intptr_t>(s)) >> (DigitBits - 1));
+ static constexpr unsigned shiftMask = DigitBits - 1;
+ Digit un32 =
+ (high << s) | ((low >> ((DigitBits - s) & shiftMask)) & sZeroMask);
+
+ Digit un10 = low << s;
+ Digit un1 = un10 >> HalfDigitBits;
+ Digit un0 = un10 & HalfDigitMask;
+ Digit q1 = un32 / vn1;
+ Digit rhat = un32 - q1 * vn1;
+
+ while (q1 >= HalfDigitBase || q1 * vn0 > rhat * HalfDigitBase + un1) {
+ q1--;
+ rhat += vn1;
+ if (rhat >= HalfDigitBase) {
+ break;
+ }
+ }
+
+ Digit un21 = un32 * HalfDigitBase + un1 - q1 * divisor;
+ Digit q0 = un21 / vn1;
+ rhat = un21 - q0 * vn1;
+
+ while (q0 >= HalfDigitBase || q0 * vn0 > rhat * HalfDigitBase + un0) {
+ q0--;
+ rhat += vn1;
+ if (rhat >= HalfDigitBase) {
+ break;
+ }
+ }
+
+ *remainder = (un21 * HalfDigitBase + un0 - q0 * divisor) >> s;
+ return q1 * HalfDigitBase + q0;
+#endif
+}
+
+// Multiplies `source` with `factor` and adds `summand` to the result.
+// `result` and `source` may be the same BigInt for inplace modification.
+void BigInt::internalMultiplyAdd(BigInt* source, Digit factor, Digit summand,
+ unsigned n, BigInt* result) {
+ MOZ_ASSERT(source->digitLength() >= n);
+ MOZ_ASSERT(result->digitLength() >= n);
+
+ Digit carry = summand;
+ Digit high = 0;
+ for (unsigned i = 0; i < n; i++) {
+ Digit current = source->digit(i);
+ Digit newCarry = 0;
+
+ // Compute this round's multiplication.
+ Digit newHigh = 0;
+ current = digitMul(current, factor, &newHigh);
+
+ // Add last round's carryovers.
+ current = digitAdd(current, high, &newCarry);
+ current = digitAdd(current, carry, &newCarry);
+
+ // Store result and prepare for next round.
+ result->setDigit(i, current);
+ carry = newCarry;
+ high = newHigh;
+ }
+
+ if (result->digitLength() > n) {
+ result->setDigit(n++, carry + high);
+
+ // Current callers don't pass in such large results, but let's be robust.
+ while (n < result->digitLength()) {
+ result->setDigit(n++, 0);
+ }
+ } else {
+ MOZ_ASSERT(!(carry + high));
+ }
+}
+
+// Multiplies `this` with `factor` and adds `summand` to the result.
+void BigInt::inplaceMultiplyAdd(Digit factor, Digit summand) {
+ internalMultiplyAdd(this, factor, summand, digitLength(), this);
+}
+
+// Multiplies `multiplicand` with `multiplier` and adds the result to
+// `accumulator`, starting at `accumulatorIndex` for the least-significant
+// digit. Callers must ensure that `accumulator`'s digitLength and
+// corresponding digit storage is long enough to hold the result.
+void BigInt::multiplyAccumulate(BigInt* multiplicand, Digit multiplier,
+ BigInt* accumulator,
+ unsigned accumulatorIndex) {
+ MOZ_ASSERT(accumulator->digitLength() >
+ multiplicand->digitLength() + accumulatorIndex);
+ if (!multiplier) {
+ return;
+ }
+
+ Digit carry = 0;
+ Digit high = 0;
+ for (unsigned i = 0; i < multiplicand->digitLength();
+ i++, accumulatorIndex++) {
+ Digit acc = accumulator->digit(accumulatorIndex);
+ Digit newCarry = 0;
+
+ // Add last round's carryovers.
+ acc = digitAdd(acc, high, &newCarry);
+ acc = digitAdd(acc, carry, &newCarry);
+
+ // Compute this round's multiplication.
+ Digit multiplicandDigit = multiplicand->digit(i);
+ Digit low = digitMul(multiplier, multiplicandDigit, &high);
+ acc = digitAdd(acc, low, &newCarry);
+
+ // Store result and prepare for next round.
+ accumulator->setDigit(accumulatorIndex, acc);
+ carry = newCarry;
+ }
+
+ while (carry || high) {
+ MOZ_ASSERT(accumulatorIndex < accumulator->digitLength());
+ Digit acc = accumulator->digit(accumulatorIndex);
+ Digit newCarry = 0;
+ acc = digitAdd(acc, high, &newCarry);
+ high = 0;
+ acc = digitAdd(acc, carry, &newCarry);
+ accumulator->setDigit(accumulatorIndex, acc);
+ carry = newCarry;
+ accumulatorIndex++;
+ }
+}
+
+inline int8_t BigInt::absoluteCompare(BigInt* x, BigInt* y) {
+ MOZ_ASSERT(!x->digitLength() || x->digit(x->digitLength() - 1));
+ MOZ_ASSERT(!y->digitLength() || y->digit(y->digitLength() - 1));
+
+ // Sanity checks to catch negative zeroes escaping to the wild.
+ MOZ_ASSERT(!x->isNegative() || !x->isZero());
+ MOZ_ASSERT(!y->isNegative() || !y->isZero());
+
+ int diff = x->digitLength() - y->digitLength();
+ if (diff) {
+ return diff < 0 ? -1 : 1;
+ }
+
+ int i = x->digitLength() - 1;
+ while (i >= 0 && x->digit(i) == y->digit(i)) {
+ i--;
+ }
+
+ if (i < 0) {
+ return 0;
+ }
+
+ return x->digit(i) > y->digit(i) ? 1 : -1;
+}
+
+BigInt* BigInt::absoluteAdd(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y,
+ bool resultNegative) {
+ bool swap = x->digitLength() < y->digitLength();
+ // Ensure `left` has at least as many digits as `right`.
+ HandleBigInt& left = swap ? y : x;
+ HandleBigInt& right = swap ? x : y;
+
+ if (left->isZero()) {
+ MOZ_ASSERT(right->isZero());
+ return left;
+ }
+
+ if (right->isZero()) {
+ return resultNegative == left->isNegative() ? left : neg(cx, left);
+ }
+
+ RootedBigInt result(
+ cx, createUninitialized(cx, left->digitLength() + 1, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+ Digit carry = 0;
+ unsigned i = 0;
+ for (; i < right->digitLength(); i++) {
+ Digit newCarry = 0;
+ Digit sum = digitAdd(left->digit(i), right->digit(i), &newCarry);
+ sum = digitAdd(sum, carry, &newCarry);
+ result->setDigit(i, sum);
+ carry = newCarry;
+ }
+
+ for (; i < left->digitLength(); i++) {
+ Digit newCarry = 0;
+ Digit sum = digitAdd(left->digit(i), carry, &newCarry);
+ result->setDigit(i, sum);
+ carry = newCarry;
+ }
+
+ result->setDigit(i, carry);
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+BigInt* BigInt::absoluteSub(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y,
+ bool resultNegative) {
+ MOZ_ASSERT(x->digitLength() >= y->digitLength());
+
+ if (x->isZero()) {
+ MOZ_ASSERT(y->isZero());
+ return x;
+ }
+
+ if (y->isZero()) {
+ return resultNegative == x->isNegative() ? x : neg(cx, x);
+ }
+
+ int8_t comparisonResult = absoluteCompare(x, y);
+ MOZ_ASSERT(comparisonResult >= 0);
+ if (comparisonResult == 0) {
+ return zero(cx);
+ }
+
+ RootedBigInt result(
+ cx, createUninitialized(cx, x->digitLength(), resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+ Digit borrow = 0;
+ unsigned i = 0;
+ for (; i < y->digitLength(); i++) {
+ Digit newBorrow = 0;
+ Digit difference = digitSub(x->digit(i), y->digit(i), &newBorrow);
+ difference = digitSub(difference, borrow, &newBorrow);
+ result->setDigit(i, difference);
+ borrow = newBorrow;
+ }
+
+ for (; i < x->digitLength(); i++) {
+ Digit newBorrow = 0;
+ Digit difference = digitSub(x->digit(i), borrow, &newBorrow);
+ result->setDigit(i, difference);
+ borrow = newBorrow;
+ }
+
+ MOZ_ASSERT(!borrow);
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// Divides `x` by `divisor`, returning the result in `quotient` and `remainder`.
+// Mathematically, the contract is:
+//
+// quotient = (x - remainder) / divisor, with 0 <= remainder < divisor.
+//
+// If `quotient` is an empty handle, an appropriately sized BigInt will be
+// allocated for it; otherwise the caller must ensure that it is big enough.
+// `quotient` can be the same as `x` for an in-place division. `quotient` can
+// also be `Nothing()` if the caller is only interested in the remainder.
+//
+// This function returns false if `quotient` is an empty handle, but allocating
+// the quotient failed. Otherwise it returns true, indicating success.
+bool BigInt::absoluteDivWithDigitDivisor(ExclusiveContext* cx, HandleBigInt x,
+ Digit divisor,
+ const Maybe<MutableHandleBigInt>& quotient,
+ Digit* remainder,
+ bool quotientNegative) {
+ MOZ_ASSERT(divisor);
+
+ MOZ_ASSERT(!x->isZero());
+ *remainder = 0;
+ if (divisor == 1) {
+ if (quotient) {
+ BigInt* q;
+ if (x->isNegative() == quotientNegative) {
+ q = x;
+ } else {
+ q = neg(cx, x);
+ if (!q) {
+ return false;
+ }
+ }
+ quotient.value().set(q);
+ }
+ return true;
+ }
+
+ unsigned length = x->digitLength();
+ if (quotient) {
+ if (!quotient.value()) {
+ BigInt* q = createUninitialized(cx, length, quotientNegative);
+ if (!q) {
+ return false;
+ }
+ quotient.value().set(q);
+ }
+
+ for (int i = length - 1; i >= 0; i--) {
+ Digit q = digitDiv(*remainder, x->digit(i), divisor, remainder);
+ quotient.value()->setDigit(i, q);
+ }
+ } else {
+ for (int i = length - 1; i >= 0; i--) {
+ digitDiv(*remainder, x->digit(i), divisor, remainder);
+ }
+ }
+
+ return true;
+}
+
+// Adds `summand` onto `this`, starting with `summand`'s 0th digit
+// at `this`'s `startIndex`'th digit. Returns the "carry" (0 or 1).
+BigInt::Digit BigInt::absoluteInplaceAdd(BigInt* summand, unsigned startIndex) {
+ Digit carry = 0;
+ unsigned n = summand->digitLength();
+ MOZ_ASSERT(digitLength() > startIndex,
+ "must start adding at an in-range digit");
+ MOZ_ASSERT(digitLength() - startIndex >= n,
+ "digits being added to must not extend above the digits in "
+ "this (except for the returned carry digit)");
+ for (unsigned i = 0; i < n; i++) {
+ Digit newCarry = 0;
+ Digit sum = digitAdd(digit(startIndex + i), summand->digit(i), &newCarry);
+ sum = digitAdd(sum, carry, &newCarry);
+ setDigit(startIndex + i, sum);
+ carry = newCarry;
+ }
+
+ return carry;
+}
+
+// Subtracts `subtrahend` from this, starting with `subtrahend`'s 0th digit
+// at `this`'s `startIndex`-th digit. Returns the "borrow" (0 or 1).
+BigInt::Digit BigInt::absoluteInplaceSub(BigInt* subtrahend,
+ unsigned startIndex) {
+ Digit borrow = 0;
+ unsigned n = subtrahend->digitLength();
+ MOZ_ASSERT(digitLength() > startIndex,
+ "must start subtracting from an in-range digit");
+ MOZ_ASSERT(digitLength() - startIndex >= n,
+ "digits being subtracted from must not extend above the "
+ "digits in this (except for the returned borrow digit)");
+ for (unsigned i = 0; i < n; i++) {
+ Digit newBorrow = 0;
+ Digit difference =
+ digitSub(digit(startIndex + i), subtrahend->digit(i), &newBorrow);
+ difference = digitSub(difference, borrow, &newBorrow);
+ setDigit(startIndex + i, difference);
+ borrow = newBorrow;
+ }
+
+ return borrow;
+}
+
+// Returns whether (factor1 * factor2) > (high << kDigitBits) + low.
+inline bool BigInt::productGreaterThan(Digit factor1, Digit factor2, Digit high,
+ Digit low) {
+ Digit resultHigh;
+ Digit resultLow = digitMul(factor1, factor2, &resultHigh);
+ return resultHigh > high || (resultHigh == high && resultLow > low);
+}
+
+void BigInt::inplaceRightShiftLowZeroBits(unsigned shift) {
+ MOZ_ASSERT(shift < DigitBits);
+ MOZ_ASSERT(!(digit(0) & ((static_cast<Digit>(1) << shift) - 1)),
+ "should only be shifting away zeroes");
+
+ if (!shift) {
+ return;
+ }
+
+ Digit carry = digit(0) >> shift;
+ unsigned last = digitLength() - 1;
+ for (unsigned i = 0; i < last; i++) {
+ Digit d = digit(i + 1);
+ setDigit(i, (d << (DigitBits - shift)) | carry);
+ carry = d >> shift;
+ }
+ setDigit(last, carry);
+}
+
+// Always copies the input, even when `shift` == 0.
+BigInt* BigInt::absoluteLeftShiftAlwaysCopy(ExclusiveContext* cx, HandleBigInt x,
+ unsigned shift,
+ LeftShiftMode mode) {
+ MOZ_ASSERT(shift < DigitBits);
+ MOZ_ASSERT(!x->isZero());
+
+ unsigned n = x->digitLength();
+ unsigned resultLength = mode == LeftShiftMode::AlwaysAddOneDigit ? n + 1 : n;
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, x->isNegative()));
+ if (!result) {
+ return nullptr;
+ }
+
+ if (!shift) {
+ for (unsigned i = 0; i < n; i++) {
+ result->setDigit(i, x->digit(i));
+ }
+ if (mode == LeftShiftMode::AlwaysAddOneDigit) {
+ result->setDigit(n, 0);
+ }
+
+ return result;
+ }
+
+ Digit carry = 0;
+ for (unsigned i = 0; i < n; i++) {
+ Digit d = x->digit(i);
+ result->setDigit(i, (d << shift) | carry);
+ carry = d >> (DigitBits - shift);
+ }
+
+ if (mode == LeftShiftMode::AlwaysAddOneDigit) {
+ result->setDigit(n, carry);
+ } else {
+ MOZ_ASSERT(mode == LeftShiftMode::SameSizeResult);
+ MOZ_ASSERT(!carry);
+ }
+
+ return result;
+}
+
+// Divides `dividend` by `divisor`, returning the result in `quotient` and
+// `remainder`. Mathematically, the contract is:
+//
+// quotient = (dividend - remainder) / divisor, with 0 <= remainder < divisor.
+//
+// Both `quotient` and `remainder` are optional, for callers that are only
+// interested in one of them. See Knuth, Volume 2, section 4.3.1, Algorithm D.
+// Also see the overview of the algorithm by Jan Marthedal Rasmussen over at
+// https://janmr.com/blog/2014/04/basic-multiple-precision-long-division/.
+bool BigInt::absoluteDivWithBigIntDivisor(ExclusiveContext* cx, HandleBigInt dividend,
+ HandleBigInt divisor,
+ const Maybe<MutableHandleBigInt>& quotient,
+ const Maybe<MutableHandleBigInt>& remainder,
+ bool isNegative) {
+ MOZ_ASSERT(divisor->digitLength() >= 2);
+ MOZ_ASSERT(dividend->digitLength() >= divisor->digitLength());
+
+ // Any early error return is detectable by checking the quotient and/or
+ // remainder output values.
+ MOZ_ASSERT(!quotient || !quotient.value());
+ MOZ_ASSERT(!remainder || !remainder.value());
+
+ // The unusual variable names inside this function are consistent with
+ // Knuth's book, as well as with Go's implementation of this algorithm.
+ // Maintaining this consistency is probably more useful than trying to
+ // come up with more descriptive names for them.
+ const unsigned n = divisor->digitLength();
+ const unsigned m = dividend->digitLength() - n;
+
+ // The quotient to be computed.
+ RootedBigInt q(cx);
+ if (quotient) {
+ q = createUninitialized(cx, m + 1, isNegative);
+ if (!q) {
+ return false;
+ }
+ }
+
+ // In each iteration, `qhatv` holds `divisor` * `current quotient digit`.
+ // "v" is the book's name for `divisor`, `qhat` the current quotient digit.
+ RootedBigInt qhatv(cx, createUninitialized(cx, n + 1, isNegative));
+ if (!qhatv) {
+ return false;
+ }
+
+ // D1.
+ // Left-shift inputs so that the divisor's MSB is set. This is necessary to
+ // prevent the digit-wise divisions (see digitDiv call below) from
+ // overflowing (they take a two digits wide input, and return a one digit
+ // result).
+ Digit lastDigit = divisor->digit(n - 1);
+ unsigned shift = DigitLeadingZeroes(lastDigit);
+
+ RootedBigInt shiftedDivisor(cx);
+ if (shift > 0) {
+ shiftedDivisor = absoluteLeftShiftAlwaysCopy(cx, divisor, shift,
+ LeftShiftMode::SameSizeResult);
+ if (!shiftedDivisor) {
+ return false;
+ }
+ } else {
+ shiftedDivisor = divisor;
+ }
+
+ // Holds the (continuously updated) remaining part of the dividend, which
+ // eventually becomes the remainder.
+ RootedBigInt u(cx,
+ absoluteLeftShiftAlwaysCopy(cx, dividend, shift,
+ LeftShiftMode::AlwaysAddOneDigit));
+ if (!u) {
+ return false;
+ }
+
+ // D2.
+ // Iterate over the dividend's digit (like the "grade school" algorithm).
+ // `vn1` is the divisor's most significant digit.
+ Digit vn1 = shiftedDivisor->digit(n - 1);
+ for (int j = m; j >= 0; j--) {
+ // D3.
+ // Estimate the current iteration's quotient digit (see Knuth for details).
+ // `qhat` is the current quotient digit.
+ Digit qhat = std::numeric_limits<Digit>::max();
+
+ // `ujn` is the dividend's most significant remaining digit.
+ Digit ujn = u->digit(j + n);
+ if (ujn != vn1) {
+ // `rhat` is the current iteration's remainder.
+ Digit rhat = 0;
+ // Estimate the current quotient digit by dividing the most significant
+ // digits of dividend and divisor. The result will not be too small,
+ // but could be a bit too large.
+ qhat = digitDiv(ujn, u->digit(j + n - 1), vn1, &rhat);
+
+ // Decrement the quotient estimate as needed by looking at the next
+ // digit, i.e. by testing whether
+ // qhat * v_{n-2} > (rhat << DigitBits) + u_{j+n-2}.
+ Digit vn2 = shiftedDivisor->digit(n - 2);
+ Digit ujn2 = u->digit(j + n - 2);
+ while (productGreaterThan(qhat, vn2, rhat, ujn2)) {
+ qhat--;
+ Digit prevRhat = rhat;
+ rhat += vn1;
+ // v[n-1] >= 0, so this tests for overflow.
+ if (rhat < prevRhat) {
+ break;
+ }
+ }
+ }
+
+ // D4.
+ // Multiply the divisor with the current quotient digit, and subtract
+ // it from the dividend. If there was "borrow", then the quotient digit
+ // was one too high, so we must correct it and undo one subtraction of
+ // the (shifted) divisor.
+ internalMultiplyAdd(shiftedDivisor, qhat, 0, n, qhatv);
+ Digit c = u->absoluteInplaceSub(qhatv, j);
+ if (c) {
+ c = u->absoluteInplaceAdd(shiftedDivisor, j);
+ u->setDigit(j + n, u->digit(j + n) + c);
+ qhat--;
+ }
+
+ if (quotient) {
+ q->setDigit(j, qhat);
+ }
+ }
+
+ if (quotient) {
+ BigInt* bi = destructivelyTrimHighZeroDigits(cx, q);
+ if (!bi) {
+ return false;
+ }
+ quotient.value().set(q);
+ }
+
+ if (remainder) {
+ u->inplaceRightShiftLowZeroBits(shift);
+ remainder.value().set(u);
+ }
+
+ return true;
+}
+
+// Helper for Absolute{And,AndNot,Or,Xor}.
+// Performs the given binary `op` on digit pairs of `x` and `y`; when the
+// end of the shorter of the two is reached, `kind` configures how
+// remaining digits are handled.
+// Example:
+// y: [ y2 ][ y1 ][ y0 ]
+// x: [ x3 ][ x2 ][ x1 ][ x0 ]
+// | | | |
+// (Fill) (op) (op) (op)
+// | | | |
+// v v v v
+// result: [ 0 ][ x3 ][ r2 ][ r1 ][ r0 ]
+template <BigInt::BitwiseOpKind kind, typename BitwiseOp>
+inline BigInt* BigInt::absoluteBitwiseOp(ExclusiveContext* cx, HandleBigInt x,
+ HandleBigInt y, BitwiseOp&& op) {
+ unsigned xLength = x->digitLength();
+ unsigned yLength = y->digitLength();
+ unsigned numPairs = std::min(xLength, yLength);
+ unsigned resultLength;
+ if (kind == BitwiseOpKind::SymmetricTrim) {
+ resultLength = numPairs;
+ } else if (kind == BitwiseOpKind::SymmetricFill) {
+ resultLength = std::max(xLength, yLength);
+ } else {
+ MOZ_ASSERT(kind == BitwiseOpKind::AsymmetricFill);
+ resultLength = xLength;
+ }
+ bool resultNegative = false;
+
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+
+ unsigned i = 0;
+ for (; i < numPairs; i++) {
+ result->setDigit(i, op(x->digit(i), y->digit(i)));
+ }
+
+ if (kind != BitwiseOpKind::SymmetricTrim) {
+ HandleBigInt& source =
+ kind == BitwiseOpKind::AsymmetricFill ? x : xLength == i ? y : x;
+ for (; i < resultLength; i++) {
+ result->setDigit(i, source->digit(i));
+ }
+ }
+
+ MOZ_ASSERT(i == resultLength);
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+BigInt* BigInt::absoluteAnd(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ return absoluteBitwiseOp<BitwiseOpKind::SymmetricTrim>(cx, x, y,
+ std::bit_and<Digit>());
+}
+
+BigInt* BigInt::absoluteOr(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ return absoluteBitwiseOp<BitwiseOpKind::SymmetricFill>(cx, x, y,
+ std::bit_or<Digit>());
+}
+
+BigInt* BigInt::absoluteAndNot(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ auto digitOperation = [](Digit a, Digit b) { return a & ~b; };
+ return absoluteBitwiseOp<BitwiseOpKind::AsymmetricFill>(cx, x, y,
+ digitOperation);
+}
+
+BigInt* BigInt::absoluteXor(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ return absoluteBitwiseOp<BitwiseOpKind::SymmetricFill>(cx, x, y,
+ std::bit_xor<Digit>());
+}
+
+BigInt* BigInt::absoluteAddOne(ExclusiveContext* cx, HandleBigInt x,
+ bool resultNegative) {
+ unsigned inputLength = x->digitLength();
+ // The addition will overflow into a new digit if all existing digits are
+ // at maximum.
+ bool willOverflow = true;
+ for (unsigned i = 0; i < inputLength; i++) {
+ if (std::numeric_limits<Digit>::max() != x->digit(i)) {
+ willOverflow = false;
+ break;
+ }
+ }
+
+ unsigned resultLength = inputLength + willOverflow;
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+
+ Digit carry = 1;
+ for (unsigned i = 0; i < inputLength; i++) {
+ Digit newCarry = 0;
+ result->setDigit(i, digitAdd(x->digit(i), carry, &newCarry));
+ carry = newCarry;
+ }
+ if (resultLength > inputLength) {
+ MOZ_ASSERT(carry == 1);
+ result->setDigit(inputLength, 1);
+ } else {
+ MOZ_ASSERT(!carry);
+ }
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// Like the above, but you can specify that the allocated result should have
+// length `resultLength`, which must be at least as large as `x->digitLength()`.
+// The result will be unsigned.
+BigInt* BigInt::absoluteSubOne(ExclusiveContext* cx, HandleBigInt x,
+ unsigned resultLength) {
+ MOZ_ASSERT(!x->isZero());
+ MOZ_ASSERT(resultLength >= x->digitLength());
+ bool resultNegative = false;
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+
+ unsigned length = x->digitLength();
+ Digit borrow = 1;
+ for (unsigned i = 0; i < length; i++) {
+ Digit newBorrow = 0;
+ result->setDigit(i, digitSub(x->digit(i), borrow, &newBorrow));
+ borrow = newBorrow;
+ }
+ MOZ_ASSERT(!borrow);
+ for (unsigned i = length; i < resultLength; i++) {
+ result->setDigit(i, 0);
+ }
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// Lookup table for the maximum number of bits required per character of a
+// base-N string representation of a number. To increase accuracy, the array
+// value is the actual value multiplied by 32. To generate this table:
+// for (var i = 0; i <= 36; i++) { print(Math.ceil(Math.log2(i) * 32) + ","); }
+static constexpr uint8_t maxBitsPerCharTable[] = {
+ 0, 0, 32, 51, 64, 75, 83, 90, 96, // 0..8
+ 102, 107, 111, 115, 119, 122, 126, 128, // 9..16
+ 131, 134, 136, 139, 141, 143, 145, 147, // 17..24
+ 149, 151, 153, 154, 156, 158, 159, 160, // 25..32
+ 162, 163, 165, 166, // 33..36
+};
+
+static constexpr unsigned bitsPerCharTableShift = 5;
+static constexpr size_t bitsPerCharTableMultiplier = 1u
+ << bitsPerCharTableShift;
+static constexpr char radixDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+
+static inline uint64_t CeilDiv(uint64_t numerator, uint64_t denominator) {
+ MOZ_ASSERT(numerator != 0);
+ return 1 + (numerator - 1) / denominator;
+};
+
+// Compute (an overapproximation of) the length of the string representation of
+// a BigInt. In base B an X-digit number has maximum value:
+//
+// B**X - 1
+//
+// We're trying to find N for an N-digit number in base |radix| full
+// representing a |bitLength|-digit number in base 2, so we have:
+//
+// radix**N - 1 ≥ 2**bitLength - 1
+// radix**N ≥ 2**bitLength
+// N ≥ log2(2**bitLength) / log2(radix)
+// N ≥ bitLength / log2(radix)
+//
+// so the smallest N is:
+//
+// N = ⌈bitLength / log2(radix)⌉
+//
+// We want to avoid floating-point computations and precompute the logarithm, so
+// we multiply both sides of the division by |bitsPerCharTableMultiplier|:
+//
+// N = ⌈(bPCTM * bitLength) / (bPCTM * log2(radix))⌉
+//
+// and then because |maxBitsPerChar| representing the denominator may have been
+// rounded *up* -- which could produce an overall under-computation -- we reduce
+// by one to undo any rounding and conservatively compute:
+//
+// N ≥ ⌈(bPCTM * bitLength) / (maxBitsPerChar - 1)⌉
+//
+size_t BigInt::calculateMaximumCharactersRequired(HandleBigInt x,
+ unsigned radix) {
+ MOZ_ASSERT(!x->isZero());
+ MOZ_ASSERT(radix >= 2 && radix <= 36);
+
+ size_t length = x->digitLength();
+ Digit lastDigit = x->digit(length - 1);
+ size_t bitLength = length * DigitBits - DigitLeadingZeroes(lastDigit);
+
+ uint8_t maxBitsPerChar = maxBitsPerCharTable[radix];
+ uint64_t maximumCharactersRequired =
+ CeilDiv(static_cast<uint64_t>(bitsPerCharTableMultiplier) * bitLength,
+ maxBitsPerChar - 1);
+ maximumCharactersRequired += x->isNegative();
+
+ return AssertedCast<size_t>(maximumCharactersRequired);
+}
+
+JSLinearString* BigInt::toStringBasePowerOfTwo(ExclusiveContext* cx, HandleBigInt x,
+ unsigned radix) {
+ MOZ_ASSERT(mozilla::IsPowerOfTwo(radix));
+ MOZ_ASSERT(radix >= 2 && radix <= 32);
+ MOZ_ASSERT(!x->isZero());
+
+ const unsigned length = x->digitLength();
+ const bool sign = x->isNegative();
+ const unsigned bitsPerChar = mozilla::CountTrailingZeroes32(radix);
+ const unsigned charMask = radix - 1;
+ // Compute the length of the resulting string: divide the bit length of the
+ // BigInt by the number of bits representable per character (rounding up).
+ const Digit msd = x->digit(length - 1);
+
+ const size_t bitLength = length * DigitBits - DigitLeadingZeroes(msd);
+ const size_t charsRequired = CeilDiv(bitLength, bitsPerChar) + sign;
+
+ if (charsRequired > JSString::MAX_LENGTH) {
+ ReportOutOfMemory(cx);
+ return nullptr;
+ }
+
+ auto resultChars = cx->make_pod_array<char>(charsRequired);
+ if (!resultChars) {
+ return nullptr;
+ }
+
+ Digit digit = 0;
+ // Keeps track of how many unprocessed bits there are in |digit|.
+ unsigned availableBits = 0;
+ size_t pos = charsRequired;
+ for (unsigned i = 0; i < length - 1; i++) {
+ Digit newDigit = x->digit(i);
+ // Take any leftover bits from the last iteration into account.
+ unsigned current = (digit | (newDigit << availableBits)) & charMask;
+ MOZ_ASSERT(pos);
+ resultChars[--pos] = radixDigits[current];
+ unsigned consumedBits = bitsPerChar - availableBits;
+ digit = newDigit >> consumedBits;
+ availableBits = DigitBits - consumedBits;
+ while (availableBits >= bitsPerChar) {
+ MOZ_ASSERT(pos);
+ resultChars[--pos] = radixDigits[digit & charMask];
+ digit >>= bitsPerChar;
+ availableBits -= bitsPerChar;
+ }
+ }
+
+ // Write out the character containing the lowest-order bit of |msd|.
+ //
+ // This character may include leftover bits from the Digit below |msd|. For
+ // example, if |x === 2n**64n| and |radix == 32|: the preceding loop writes
+ // twelve zeroes for low-order bits 0-59 in |x->digit(0)| (and |x->digit(1)|
+ // on 32-bit); then the highest 4 bits of of |x->digit(0)| (or |x->digit(1)|
+ // on 32-bit) and bit 0 of |x->digit(1)| (|x->digit(2)| on 32-bit) will
+ // comprise the |current == 0b1'0000| computed below for the high-order 'g'
+ // character.
+ unsigned current = (digit | (msd << availableBits)) & charMask;
+ MOZ_ASSERT(pos);
+ resultChars[--pos] = radixDigits[current];
+
+ // Write out remaining characters represented by |msd|. (There may be none,
+ // as in the example above.)
+ digit = msd >> (bitsPerChar - availableBits);
+ while (digit != 0) {
+ MOZ_ASSERT(pos);
+ resultChars[--pos] = radixDigits[digit & charMask];
+ digit >>= bitsPerChar;
+ }
+
+ if (sign) {
+ MOZ_ASSERT(pos);
+ resultChars[--pos] = '-';
+ }
+
+ MOZ_ASSERT(pos == 0);
+ return NewStringCopyN<CanGC>(cx, resultChars.get(), charsRequired);
+}
+
+static constexpr BigInt::Digit MaxPowerInDigit(uint8_t radix) {
+ BigInt::Digit result = 1;
+ while (result < BigInt::Digit(-1) / radix) {
+ result *= radix;
+ }
+ return result;
+}
+
+static constexpr uint8_t MaxExponentInDigit(uint8_t radix) {
+ uint8_t exp = 0;
+ BigInt::Digit result = 1;
+ while (result < BigInt::Digit(-1) / radix) {
+ result *= radix;
+ exp += 1;
+ }
+ return exp;
+}
+
+struct RadixInfo {
+ BigInt::Digit maxPowerInDigit;
+ uint8_t maxExponentInDigit;
+
+ constexpr RadixInfo(BigInt::Digit maxPower, uint8_t maxExponent)
+ : maxPowerInDigit(maxPower), maxExponentInDigit(maxExponent) {}
+
+ explicit constexpr RadixInfo(uint8_t radix)
+ : RadixInfo(MaxPowerInDigit(radix), MaxExponentInDigit(radix)) {}
+};
+
+static constexpr const RadixInfo toStringInfo[37] = {
+ {0, 0}, {0, 0}, RadixInfo(2), RadixInfo(3), RadixInfo(4),
+ RadixInfo(5), RadixInfo(6), RadixInfo(7), RadixInfo(8), RadixInfo(9),
+ RadixInfo(10), RadixInfo(11), RadixInfo(12), RadixInfo(13), RadixInfo(14),
+ RadixInfo(15), RadixInfo(16), RadixInfo(17), RadixInfo(18), RadixInfo(19),
+ RadixInfo(20), RadixInfo(21), RadixInfo(22), RadixInfo(23), RadixInfo(24),
+ RadixInfo(25), RadixInfo(26), RadixInfo(27), RadixInfo(28), RadixInfo(29),
+ RadixInfo(30), RadixInfo(31), RadixInfo(32), RadixInfo(33), RadixInfo(34),
+ RadixInfo(35), RadixInfo(36),
+};
+
+JSLinearString* BigInt::toStringGeneric(ExclusiveContext* cx, HandleBigInt x,
+ unsigned radix) {
+ MOZ_ASSERT(radix >= 2 && radix <= 36);
+ MOZ_ASSERT(!x->isZero());
+
+ size_t maximumCharactersRequired =
+ calculateMaximumCharactersRequired(x, radix);
+ if (maximumCharactersRequired > JSString::MAX_LENGTH) {
+ ReportOutOfMemory(cx);
+ return nullptr;
+ }
+
+ UniqueChars resultString(js_pod_malloc<char>(maximumCharactersRequired));
+ if (!resultString) {
+ ReportOutOfMemory(cx);
+ return nullptr;
+ }
+
+ size_t writePos = maximumCharactersRequired;
+ unsigned length = x->digitLength();
+ Digit lastDigit;
+ if (length == 1) {
+ lastDigit = x->digit(0);
+ } else {
+ unsigned chunkChars = toStringInfo[radix].maxExponentInDigit;
+ Digit chunkDivisor = toStringInfo[radix].maxPowerInDigit;
+
+ unsigned nonZeroDigit = length - 1;
+ MOZ_ASSERT(x->digit(nonZeroDigit) != 0);
+
+ // `rest` holds the part of the BigInt that we haven't looked at yet.
+ // Not to be confused with "remainder"!
+ RootedBigInt rest(cx);
+
+ // In the first round, divide the input, allocating a new BigInt for
+ // the result == rest; from then on divide the rest in-place.
+ //
+ // FIXME: absoluteDivWithDigitDivisor doesn't
+ // destructivelyTrimHighZeroDigits for in-place divisions, leading to
+ // worse constant factors. See
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1510213.
+ RootedBigInt dividend(cx, x);
+ do {
+ Digit chunk;
+ if (!absoluteDivWithDigitDivisor(cx, dividend, chunkDivisor, Some(&rest),
+ &chunk, dividend->isNegative())) {
+ return nullptr;
+ }
+
+ dividend = rest;
+ for (unsigned i = 0; i < chunkChars; i++) {
+ MOZ_ASSERT(writePos > 0);
+ resultString[--writePos] = radixDigits[chunk % radix];
+ chunk /= radix;
+ }
+ MOZ_ASSERT(!chunk);
+
+ if (!rest->digit(nonZeroDigit)) {
+ nonZeroDigit--;
+ }
+
+ MOZ_ASSERT(rest->digit(nonZeroDigit) != 0,
+ "division by a single digit can't remove more than one "
+ "digit from a number");
+ } while (nonZeroDigit > 0);
+
+ lastDigit = rest->digit(0);
+ }
+
+ do {
+ MOZ_ASSERT(writePos > 0);
+ resultString[--writePos] = radixDigits[lastDigit % radix];
+ lastDigit /= radix;
+ } while (lastDigit > 0);
+ MOZ_ASSERT(writePos < maximumCharactersRequired);
+ MOZ_ASSERT(maximumCharactersRequired - writePos <=
+ static_cast<size_t>(maximumCharactersRequired));
+
+ // Remove leading zeroes.
+ while (writePos + 1 < maximumCharactersRequired &&
+ resultString[writePos] == '0') {
+ writePos++;
+ }
+
+ if (x->isNegative()) {
+ MOZ_ASSERT(writePos > 0);
+ resultString[--writePos] = '-';
+ }
+
+ MOZ_ASSERT(writePos < maximumCharactersRequired);
+ // Would be better to somehow adopt resultString directly.
+ return NewStringCopyN<CanGC>(cx, resultString.get() + writePos,
+ maximumCharactersRequired - writePos);
+}
+
+BigInt* BigInt::trimHighZeroDigits(ExclusiveContext* cx, HandleBigInt x) {
+ if (x->isZero()) {
+ MOZ_ASSERT(!x->isNegative());
+ return x;
+ }
+ MOZ_ASSERT(x->digitLength());
+
+ int nonZeroIndex = x->digitLength() - 1;
+ while (nonZeroIndex >= 0 && x->digit(nonZeroIndex) == 0) {
+ nonZeroIndex--;
+ }
+
+ if (nonZeroIndex < 0) {
+ return zero(cx);
+ }
+
+ if (nonZeroIndex == static_cast<int>(x->digitLength() - 1)) {
+ return x;
+ }
+
+ unsigned newLength = nonZeroIndex + 1;
+ BigInt* trimmedBigInt = createUninitialized(cx, newLength, x->isNegative());
+ if (!trimmedBigInt) {
+ return nullptr;
+ }
+ for (unsigned i = 0; i < newLength; i++) {
+ trimmedBigInt->setDigit(i, x->digit(i));
+ }
+
+ return trimmedBigInt;
+}
+
+BigInt* BigInt::destructivelyTrimHighZeroDigits(ExclusiveContext* cx, HandleBigInt x) {
+ // TODO: Modify in place instead of allocating.
+ return trimHighZeroDigits(cx, x);
+}
+
+// The maximum value `radix**charCount - 1` must be represented as a max number
+// `2**(N * DigitBits) - 1` for `N` digits, so
+//
+// 2**(N * DigitBits) - 1 ≥ radix**charcount - 1
+// 2**(N * DigitBits) ≥ radix**charcount
+// N * DigitBits ≥ log2(radix**charcount)
+// N * DigitBits ≥ charcount * log2(radix)
+// N ≥ ⌈charcount * log2(radix) / DigitBits⌉ (conservatively)
+//
+// or in the code's terms (all numbers promoted to exact mathematical values),
+//
+// N ≥ ⌈charcount * bitsPerChar / (DigitBits * bitsPerCharTableMultiplier)⌉
+//
+// Note that `N` is computed even more conservatively here because `bitsPerChar`
+// is rounded up.
+bool BigInt::calculateMaximumDigitsRequired(ExclusiveContext* cx, uint8_t radix,
+ size_t charcount, size_t* result) {
+ MOZ_ASSERT(2 <= radix && radix <= 36);
+
+ size_t bitsPerChar = maxBitsPerCharTable[radix];
+
+ MOZ_ASSERT(charcount > 0);
+ MOZ_ASSERT(charcount <= std::numeric_limits<size_t>::max() / bitsPerChar);
+ uint64_t n =
+ CeilDiv(charcount * bitsPerChar, DigitBits * bitsPerCharTableMultiplier);
+ if (n > MaxDigitLength) {
+ ReportAllocationOverflow(cx);
+ return false;
+ }
+
+ *result = n;
+ return true;
+}
+
+template <typename CharT>
+BigInt* BigInt::parseLiteralDigits(ExclusiveContext* cx,
+ const Range<const CharT> chars,
+ unsigned radix, bool isNegative,
+ bool* haveParseError) {
+ MOZ_ASSERT(chars.length());
+
+ RangedPtr<const CharT> start = chars.begin();
+ RangedPtr<const CharT> end = chars.end();
+
+ // Skipping leading zeroes.
+ while (start[0] == '0') {
+ start++;
+ if (start == end) {
+ return zero(cx);
+ }
+ }
+
+ unsigned limit0 = '0' + std::min(radix, 10u);
+ unsigned limita = 'a' + (radix - 10);
+ unsigned limitA = 'A' + (radix - 10);
+
+ size_t length;
+ if (!calculateMaximumDigitsRequired(cx, radix, end - start, &length)) {
+ return nullptr;
+ }
+ RootedBigInt result(cx, createUninitialized(cx, length, isNegative));
+ if (!result) {
+ return nullptr;
+ }
+
+ result->initializeDigitsToZero();
+
+ RangedPtr<const CharT> begin = start;
+ for (; start < end; start++) {
+ uint32_t digit;
+ CharT c = *start;
+ if (c == '_' && start > begin && start < end - 1) {
+ // skip over block delimiters unless at the very start or end
+ continue;
+ } else if (c >= '0' && c < limit0) {
+ digit = c - '0';
+ } else if (c >= 'a' && c < limita) {
+ digit = c - 'a' + 10;
+ } else if (c >= 'A' && c < limitA) {
+ digit = c - 'A' + 10;
+ } else {
+ *haveParseError = true;
+ return nullptr;
+ }
+
+ result->inplaceMultiplyAdd(static_cast<Digit>(radix),
+ static_cast<Digit>(digit));
+ }
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// BigInt proposal section 7.2
+template <typename CharT>
+BigInt* BigInt::parseLiteral(ExclusiveContext* cx, const Range<const CharT> chars,
+ bool* haveParseError) {
+ RangedPtr<const CharT> start = chars.begin();
+ const RangedPtr<const CharT> end = chars.end();
+ bool isNegative = false;
+
+ MOZ_ASSERT(chars.length());
+
+ if (end - start > 2 && start[0] == '0') {
+ if (start[1] == 'b' || start[1] == 'B') {
+ // StringNumericLiteral ::: BinaryIntegerLiteral
+ return parseLiteralDigits(cx, Range<const CharT>(start + 2, end), 2,
+ isNegative, haveParseError);
+ }
+ if (start[1] == 'x' || start[1] == 'X') {
+ // StringNumericLiteral ::: HexIntegerLiteral
+ return parseLiteralDigits(cx, Range<const CharT>(start + 2, end), 16,
+ isNegative, haveParseError);
+ }
+ if (start[1] == 'o' || start[1] == 'O') {
+ // StringNumericLiteral ::: OctalIntegerLiteral
+ return parseLiteralDigits(cx, Range<const CharT>(start + 2, end), 8,
+ isNegative, haveParseError);
+ }
+ }
+
+ return parseLiteralDigits(cx, Range<const CharT>(start, end), 10, isNegative,
+ haveParseError);
+}
+
+// BigInt proposal section 5.1.1
+static bool IsInteger(double d) {
+ // Step 1 is an assertion checked by the caller.
+ // Step 2.
+ if (!mozilla::IsFinite(d)) {
+ return false;
+ }
+
+ // Step 3.
+ double i = JS::ToInteger(d);
+
+ // Step 4.
+ if (i != d) {
+ return false;
+ }
+
+ // Step 5.
+ return true;
+}
+
+BigInt* BigInt::createFromDouble(ExclusiveContext* cx, double d) {
+ MOZ_ASSERT(::IsInteger(d),
+ "Only integer-valued doubles can convert to BigInt");
+
+ if (d == 0) {
+ return zero(cx);
+ }
+
+ int exponent = mozilla::ExponentComponent(d);
+ MOZ_ASSERT(exponent >= 0);
+ int length = exponent / DigitBits + 1;
+ BigInt* result = createUninitialized(cx, length, d < 0);
+ if (!result) {
+ return nullptr;
+ }
+
+ // We construct a BigInt from the double `d` by shifting its mantissa
+ // according to its exponent and mapping the bit pattern onto digits.
+ //
+ // <----------- bitlength = exponent + 1 ----------->
+ // <----- 52 ------> <------ trailing zeroes ------>
+ // mantissa: 1yyyyyyyyyyyyyyyyy 0000000000000000000000000000000
+ // digits: 0001xxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
+ // <--> <------>
+ // msdTopBits DigitBits
+ //
+ using Double = mozilla::FloatingPoint<double>;
+ uint64_t mantissa =
+ mozilla::BitwiseCast<uint64_t>(d) & Double::kSignificandBits;
+ // Add implicit high bit.
+ mantissa |= 1ull << Double::kSignificandWidth;
+
+ const int mantissaTopBit = Double::kSignificandWidth; // 0-indexed.
+
+ // 0-indexed position of `d`'s most significant bit within the `msd`.
+ int msdTopBit = exponent % DigitBits;
+
+ // Next digit under construction.
+ Digit digit;
+
+ // First, build the MSD by shifting the mantissa appropriately.
+ if (msdTopBit < mantissaTopBit) {
+ int remainingMantissaBits = mantissaTopBit - msdTopBit;
+ digit = mantissa >> remainingMantissaBits;
+ mantissa = mantissa << (64 - remainingMantissaBits);
+ } else {
+ MOZ_ASSERT(msdTopBit >= mantissaTopBit);
+ digit = mantissa << (msdTopBit - mantissaTopBit);
+ mantissa = 0;
+ }
+ result->setDigit(--length, digit);
+
+ // Fill in digits containing mantissa contributions.
+ while (mantissa) {
+ MOZ_ASSERT(length > 0,
+ "double bits were all non-fractional, so there must be "
+ "digits present to hold them");
+
+ if (DigitBits == 64) {
+ result->setDigit(--length, mantissa);
+ break;
+ }
+
+ MOZ_ASSERT(DigitBits == 32);
+ Digit current = mantissa >> 32;
+ mantissa = mantissa << 32;
+ result->setDigit(--length, current);
+ }
+
+ // Fill in low-order zeroes.
+ for (int i = length - 1; i >= 0; i--) {
+ result->setDigit(i, 0);
+ }
+
+ return result;
+}
+
+BigInt* BigInt::createFromUint64(ExclusiveContext* cx, uint64_t n) {
+ if (n == 0) {
+ return zero(cx);
+ }
+
+ const bool isNegative = false;
+
+ if (DigitBits == 32) {
+ Digit low = n;
+ Digit high = n >> 32;
+ size_t length = high ? 2 : 1;
+
+ BigInt* res = createUninitialized(cx, length, isNegative);
+ if (!res) {
+ return nullptr;
+ }
+ res->setDigit(0, low);
+ if (high) {
+ res->setDigit(1, high);
+ }
+ return res;
+ }
+
+ BigInt* res = createUninitialized(cx, 1, isNegative);
+ if (!res) {
+ return nullptr;
+ }
+
+ res->setDigit(0, n);
+ return res;
+}
+
+BigInt* BigInt::createFromInt64(ExclusiveContext* cx, int64_t n) {
+ BigInt* res = createFromUint64(cx, Abs(n));
+ if (!res) {
+ return nullptr;
+ }
+
+ if (n < 0) {
+ res->lengthSignAndReservedBits_ |= SignBit;
+ }
+ MOZ_ASSERT(res->isNegative() == (n < 0));
+
+ return res;
+}
+
+// BigInt proposal section 5.1.2
+BigInt* js::NumberToBigInt(ExclusiveContext* cx, double d) {
+ // Step 1 is an assertion checked by the caller.
+ // Step 2.
+ if (!::IsInteger(d)) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_NUMBER_TO_BIGINT);
+ }
+ return nullptr;
+ }
+
+ // Step 3.
+ return BigInt::createFromDouble(cx, d);
+}
+
+BigInt* BigInt::copy(ExclusiveContext* cx, HandleBigInt x) {
+ if (x->isZero()) {
+ return zero(cx);
+ }
+
+ BigInt* result = createUninitialized(cx, x->digitLength(), x->isNegative());
+ if (!result) {
+ return nullptr;
+ }
+ for (size_t i = 0; i < x->digitLength(); i++) {
+ result->setDigit(i, x->digit(i));
+ }
+ return result;
+}
+
+// BigInt proposal section 1.1.7
+BigInt* BigInt::add(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ bool xNegative = x->isNegative();
+
+ // x + y == x + y
+ // -x + -y == -(x + y)
+ if (xNegative == y->isNegative()) {
+ return absoluteAdd(cx, x, y, xNegative);
+ }
+
+ // x + -y == x - y == -(y - x)
+ // -x + y == y - x == -(x - y)
+ if (absoluteCompare(x, y) >= 0) {
+ return absoluteSub(cx, x, y, xNegative);
+ }
+
+ return absoluteSub(cx, y, x, !xNegative);
+}
+
+// BigInt proposal section 1.1.8
+BigInt* BigInt::sub(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ bool xNegative = x->isNegative();
+ if (xNegative != y->isNegative()) {
+ // x - (-y) == x + y
+ // (-x) - y == -(x + y)
+ return absoluteAdd(cx, x, y, xNegative);
+ }
+ // x - y == -(y - x)
+ // (-x) - (-y) == y - x == -(x - y)
+ if (absoluteCompare(x, y) >= 0) {
+ return absoluteSub(cx, x, y, xNegative);
+ }
+
+ return absoluteSub(cx, y, x, !xNegative);
+}
+
+// BigInt proposal section 1.1.4
+BigInt* BigInt::mul(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero()) {
+ return x;
+ }
+ if (y->isZero()) {
+ return y;
+ }
+
+ unsigned resultLength = x->digitLength() + y->digitLength();
+ bool resultNegative = x->isNegative() != y->isNegative();
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+ result->initializeDigitsToZero();
+
+ for (size_t i = 0; i < x->digitLength(); i++) {
+ multiplyAccumulate(y, x->digit(i), result, i);
+ }
+
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// BigInt proposal section 1.1.5
+BigInt* BigInt::div(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ // 1. If y is 0n, throw a RangeError exception.
+ if (y->isZero()) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_DIVISION_BY_ZERO);
+ }
+ return nullptr;
+ }
+
+ // 2. Let quotient be the mathematical value of x divided by y.
+ // 3. Return a BigInt representing quotient rounded towards 0 to the next
+ // integral value.
+ if (x->isZero()) {
+ return x;
+ }
+
+ if (absoluteCompare(x, y) < 0) {
+ return zero(cx);
+ }
+
+ RootedBigInt quotient(cx);
+ bool resultNegative = x->isNegative() != y->isNegative();
+ if (y->digitLength() == 1) {
+ Digit divisor = y->digit(0);
+ if (divisor == 1) {
+ return resultNegative == x->isNegative() ? x : neg(cx, x);
+ }
+
+ Digit remainder;
+ if (!absoluteDivWithDigitDivisor(cx, x, divisor, Some(&quotient),
+ &remainder, resultNegative)) {
+ return nullptr;
+ }
+ } else {
+ if (!absoluteDivWithBigIntDivisor(cx, x, y, Some(&quotient), Nothing(),
+ resultNegative)) {
+ return nullptr;
+ }
+ }
+
+ return destructivelyTrimHighZeroDigits(cx, quotient);
+}
+
+// BigInt proposal section 1.1.6
+BigInt* BigInt::mod(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ // 1. If y is 0n, throw a RangeError exception.
+ if (y->isZero()) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_DIVISION_BY_ZERO);
+ }
+ return nullptr;
+ }
+
+ // 2. If x is 0n, return x.
+ if (x->isZero()) {
+ return x;
+ }
+ // 3. Let r be the BigInt defined by the mathematical relation r = x - (y ×
+ // q) where q is a BigInt that is negative only if x/y is negative and
+ // positive only if x/y is positive, and whose magnitude is as large as
+ // possible without exceeding the magnitude of the true mathematical
+ // quotient of x and y.
+ if (absoluteCompare(x, y) < 0) {
+ return x;
+ }
+
+ if (y->digitLength() == 1) {
+ Digit divisor = y->digit(0);
+ if (divisor == 1) {
+ return zero(cx);
+ }
+
+ Digit remainderDigit;
+ bool unusedQuotientNegative = false;
+ if (!absoluteDivWithDigitDivisor(cx, x, divisor, Nothing(), &remainderDigit,
+ unusedQuotientNegative)) {
+ MOZ_CRASH("BigInt div by digit failed unexpectedly");
+ }
+
+ if (!remainderDigit) {
+ return zero(cx);
+ }
+
+ BigInt* remainder = createUninitialized(cx, 1, x->isNegative());
+ if (!remainder) {
+ return nullptr;
+ }
+ remainder->setDigit(0, remainderDigit);
+ return remainder;
+ } else {
+ RootedBigInt remainder(cx);
+ if (!absoluteDivWithBigIntDivisor(cx, x, y, Nothing(), Some(&remainder),
+ x->isNegative())) {
+ return nullptr;
+ }
+ MOZ_ASSERT(remainder);
+ return destructivelyTrimHighZeroDigits(cx, remainder);
+ }
+}
+
+// BigInt proposal section 1.1.3
+BigInt* BigInt::pow(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ // 1. If exponent is < 0, throw a RangeError exception.
+ if (y->isNegative()) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_NEGATIVE_EXPONENT);
+ }
+ return nullptr;
+ }
+
+ // 2. If base is 0n and exponent is 0n, return 1n.
+ if (y->isZero()) {
+ return one(cx);
+ }
+
+ if (x->isZero()) {
+ return x;
+ }
+
+ // 3. Return a BigInt representing the mathematical value of base raised
+ // to the power exponent.
+ if (x->digitLength() == 1 && x->digit(0) == 1) {
+ // (-1) ** even_number == 1.
+ if (x->isNegative() && (y->digit(0) & 1) == 0) {
+ return neg(cx, x);
+ }
+ // (-1) ** odd_number == -1; 1 ** anything == 1.
+ return x;
+ }
+
+ // For all bases >= 2, very large exponents would lead to unrepresentable
+ // results.
+ static_assert(MaxBitLength < std::numeric_limits<Digit>::max(),
+ "unexpectedly large MaxBitLength");
+ if (y->digitLength() > 1) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TOO_LARGE);
+ }
+ return nullptr;
+ }
+ Digit exponent = y->digit(0);
+ if (exponent == 1) {
+ return x;
+ }
+ if (exponent >= MaxBitLength) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TOO_LARGE);
+ }
+ return nullptr;
+ }
+
+ static_assert(MaxBitLength <= std::numeric_limits<int>::max(),
+ "unexpectedly large MaxBitLength");
+ int n = static_cast<int>(exponent);
+ if (x->digitLength() == 1 && x->digit(0) == 2) {
+ // Fast path for 2^n.
+ int length = 1 + (n / DigitBits);
+ // Result is negative for odd powers of -2n.
+ bool resultNegative = x->isNegative() && (n & 1);
+ RootedBigInt result(cx, createUninitialized(cx, length, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+ result->initializeDigitsToZero();
+ result->setDigit(length - 1, static_cast<Digit>(1) << (n % DigitBits));
+ return result;
+ }
+
+ // This implicitly sets the result's sign correctly.
+ RootedBigInt result(cx, (n & 1) ? x : nullptr);
+ RootedBigInt runningSquare(cx, x);
+ for (n /= 2; n; n /= 2) {
+ runningSquare = mul(cx, runningSquare, runningSquare);
+ if (!runningSquare) {
+ return nullptr;
+ }
+ if (n & 1) {
+ if (!result) {
+ result = runningSquare;
+ } else {
+ result = mul(cx, result, runningSquare);
+ if (!result) {
+ return nullptr;
+ }
+ }
+ }
+ }
+ return result;
+}
+
+BigInt* BigInt::lshByAbsolute(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero() || y->isZero()) {
+ return x;
+ }
+
+ if (y->digitLength() > 1 || y->digit(0) > MaxBitLength) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TOO_LARGE);
+ }
+ return nullptr;
+ }
+ Digit shift = y->digit(0);
+ int digitShift = static_cast<int>(shift / DigitBits);
+ int bitsShift = static_cast<int>(shift % DigitBits);
+ int length = x->digitLength();
+ bool grow = bitsShift && (x->digit(length - 1) >> (DigitBits - bitsShift));
+ int resultLength = length + digitShift + grow;
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, x->isNegative()));
+ if (!result) {
+ return nullptr;
+ }
+
+ int i = 0;
+ for (; i < digitShift; i++) {
+ result->setDigit(i, 0);
+ }
+
+ if (bitsShift == 0) {
+ for (int j = 0; i < resultLength; i++, j++) {
+ result->setDigit(i, x->digit(j));
+ }
+ } else {
+ Digit carry = 0;
+ for (int j = 0; j < length; i++, j++) {
+ Digit d = x->digit(j);
+ result->setDigit(i, (d << bitsShift) | carry);
+ carry = d >> (DigitBits - bitsShift);
+ }
+ if (grow) {
+ result->setDigit(i, carry);
+ } else {
+ MOZ_ASSERT(!carry);
+ }
+ }
+ return result;
+}
+
+BigInt* BigInt::rshByMaximum(ExclusiveContext* cx, bool isNegative) {
+ if (isNegative) {
+ RootedBigInt negativeOne(cx, createUninitialized(cx, 1, isNegative));
+ if (!negativeOne) {
+ return nullptr;
+ }
+ negativeOne->setDigit(0, 1);
+ return negativeOne;
+ }
+ return zero(cx);
+}
+
+BigInt* BigInt::rshByAbsolute(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero() || y->isZero()) {
+ return x;
+ }
+
+ if (y->digitLength() > 1 || y->digit(0) >= MaxBitLength) {
+ return rshByMaximum(cx, x->isNegative());
+ }
+ Digit shift = y->digit(0);
+ int length = x->digitLength();
+ int digitShift = static_cast<int>(shift / DigitBits);
+ int bitsShift = static_cast<int>(shift % DigitBits);
+ int resultLength = length - digitShift;
+ if (resultLength <= 0) {
+ return rshByMaximum(cx, x->isNegative());
+ }
+ // For negative numbers, round down if any bit was shifted out (so that e.g.
+ // -5n >> 1n == -3n and not -2n). Check now whether this will happen and
+ // whether it can cause overflow into a new digit. If we allocate the result
+ // large enough up front, it avoids having to do a second allocation later.
+ bool mustRoundDown = false;
+ if (x->isNegative()) {
+ const Digit mask = (static_cast<Digit>(1) << bitsShift) - 1;
+ if ((x->digit(digitShift) & mask)) {
+ mustRoundDown = true;
+ } else {
+ for (int i = 0; i < digitShift; i++) {
+ if (x->digit(i)) {
+ mustRoundDown = true;
+ break;
+ }
+ }
+ }
+ }
+ // If bits_shift is non-zero, it frees up bits, preventing overflow.
+ if (mustRoundDown && bitsShift == 0) {
+ // Overflow cannot happen if the most significant digit has unset bits.
+ Digit msd = x->digit(length - 1);
+ bool roundingCanOverflow = msd == std::numeric_limits<Digit>::max();
+ if (roundingCanOverflow) {
+ resultLength++;
+ }
+ }
+
+ MOZ_ASSERT(resultLength <= length);
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, x->isNegative()));
+ if (!result) {
+ return nullptr;
+ }
+ if (!bitsShift) {
+ // If roundingCanOverflow, manually initialize the overflow digit.
+ result->setDigit(resultLength - 1, 0);
+ for (int i = digitShift; i < length; i++) {
+ result->setDigit(i - digitShift, x->digit(i));
+ }
+ } else {
+ Digit carry = x->digit(digitShift) >> bitsShift;
+ int last = length - digitShift - 1;
+ for (int i = 0; i < last; i++) {
+ Digit d = x->digit(i + digitShift + 1);
+ result->setDigit(i, (d << (DigitBits - bitsShift)) | carry);
+ carry = d >> bitsShift;
+ }
+ result->setDigit(last, carry);
+ }
+
+ if (mustRoundDown) {
+ MOZ_ASSERT(x->isNegative());
+ // Since the result is negative, rounding down means adding one to
+ // its absolute value. This cannot overflow. TODO: modify the result in
+ // place.
+ return absoluteAddOne(cx, result, x->isNegative());
+ }
+ return destructivelyTrimHighZeroDigits(cx, result);
+}
+
+// BigInt proposal section 1.1.9. BigInt::leftShift ( x, y )
+BigInt* BigInt::lsh(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (y->isNegative()) {
+ return rshByAbsolute(cx, x, y);
+ }
+ return lshByAbsolute(cx, x, y);
+}
+
+// BigInt proposal section 1.1.10. BigInt::signedRightShift ( x, y )
+BigInt* BigInt::rsh(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (y->isNegative()) {
+ return lshByAbsolute(cx, x, y);
+ }
+ return rshByAbsolute(cx, x, y);
+}
+
+// BigInt proposal section 1.1.17. BigInt::bitwiseAND ( x, y )
+BigInt* BigInt::bitAnd(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero()) {
+ return x;
+ }
+
+ if (y->isZero()) {
+ return y;
+ }
+
+ if (!x->isNegative() && !y->isNegative()) {
+ return absoluteAnd(cx, x, y);
+ }
+
+ if (x->isNegative() && y->isNegative()) {
+ int resultLength = std::max(x->digitLength(), y->digitLength()) + 1;
+ // (-x) & (-y) == ~(x-1) & ~(y-1) == ~((x-1) | (y-1))
+ // == -(((x-1) | (y-1)) + 1)
+ RootedBigInt x1(cx, absoluteSubOne(cx, x, resultLength));
+ if (!x1) {
+ return nullptr;
+ }
+ RootedBigInt y1(cx, absoluteSubOne(cx, y, y->digitLength()));
+ if (!y1) {
+ return nullptr;
+ }
+ RootedBigInt result(cx, absoluteOr(cx, x1, y1));
+ if (!result) {
+ return nullptr;
+ }
+ bool resultNegative = true;
+ return absoluteAddOne(cx, result, resultNegative);
+ }
+
+ MOZ_ASSERT(x->isNegative() != y->isNegative());
+ HandleBigInt& pos = x->isNegative() ? y : x;
+ HandleBigInt& neg = x->isNegative() ? x : y;
+
+ RootedBigInt neg1(cx, absoluteSubOne(cx, neg, neg->digitLength()));
+ if (!neg1) {
+ return nullptr;
+ }
+
+ // x & (-y) == x & ~(y-1) == x & ~(y-1)
+ return absoluteAndNot(cx, pos, neg1);
+}
+
+// BigInt proposal section 1.1.18. BigInt::bitwiseXOR ( x, y )
+BigInt* BigInt::bitXor(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero()) {
+ return y;
+ }
+
+ if (y->isZero()) {
+ return x;
+ }
+
+ if (!x->isNegative() && !y->isNegative()) {
+ return absoluteXor(cx, x, y);
+ }
+
+ if (x->isNegative() && y->isNegative()) {
+ int resultLength = std::max(x->digitLength(), y->digitLength());
+
+ // (-x) ^ (-y) == ~(x-1) ^ ~(y-1) == (x-1) ^ (y-1)
+ RootedBigInt x1(cx, absoluteSubOne(cx, x, resultLength));
+ if (!x1) {
+ return nullptr;
+ }
+ RootedBigInt y1(cx, absoluteSubOne(cx, y, y->digitLength()));
+ if (!y1) {
+ return nullptr;
+ }
+ return absoluteXor(cx, x1, y1);
+ }
+ MOZ_ASSERT(x->isNegative() != y->isNegative());
+ int resultLength = std::max(x->digitLength(), y->digitLength()) + 1;
+
+ HandleBigInt& pos = x->isNegative() ? y : x;
+ HandleBigInt& neg = x->isNegative() ? x : y;
+
+ // x ^ (-y) == x ^ ~(y-1) == ~(x ^ (y-1)) == -((x ^ (y-1)) + 1)
+ RootedBigInt result(cx, absoluteSubOne(cx, neg, resultLength));
+ if (!result) {
+ return nullptr;
+ }
+ result = absoluteXor(cx, result, pos);
+ if (!result) {
+ return nullptr;
+ }
+ bool resultNegative = true;
+ return absoluteAddOne(cx, result, resultNegative);
+}
+
+// BigInt proposal section 1.1.19. BigInt::bitwiseOR ( x, y )
+BigInt* BigInt::bitOr(ExclusiveContext* cx, HandleBigInt x, HandleBigInt y) {
+ if (x->isZero()) {
+ return y;
+ }
+
+ if (y->isZero()) {
+ return x;
+ }
+
+ unsigned resultLength = std::max(x->digitLength(), y->digitLength());
+ bool resultNegative = x->isNegative() || y->isNegative();
+
+ if (!resultNegative) {
+ return absoluteOr(cx, x, y);
+ }
+
+ if (x->isNegative() && y->isNegative()) {
+ // (-x) | (-y) == ~(x-1) | ~(y-1) == ~((x-1) & (y-1))
+ // == -(((x-1) & (y-1)) + 1)
+ RootedBigInt result(cx, absoluteSubOne(cx, x, resultLength));
+ if (!result) {
+ return nullptr;
+ }
+ RootedBigInt y1(cx, absoluteSubOne(cx, y, y->digitLength()));
+ if (!y1) {
+ return nullptr;
+ }
+ result = absoluteAnd(cx, result, y1);
+ if (!result) {
+ return nullptr;
+ }
+ return absoluteAddOne(cx, result, resultNegative);
+ }
+
+ MOZ_ASSERT(x->isNegative() != y->isNegative());
+ HandleBigInt& pos = x->isNegative() ? y : x;
+ HandleBigInt& neg = x->isNegative() ? x : y;
+
+ // x | (-y) == x | ~(y-1) == ~((y-1) &~ x) == -(((y-1) &~ x) + 1)
+ RootedBigInt result(cx, absoluteSubOne(cx, neg, resultLength));
+ if (!result) {
+ return nullptr;
+ }
+ result = absoluteAndNot(cx, result, pos);
+ if (!result) {
+ return nullptr;
+ }
+ return absoluteAddOne(cx, result, resultNegative);
+}
+
+// BigInt proposal section 1.1.2. BigInt::bitwiseNOT ( x )
+BigInt* BigInt::bitNot(ExclusiveContext* cx, HandleBigInt x) {
+ if (x->isNegative()) {
+ // ~(-x) == ~(~(x-1)) == x-1
+ return absoluteSubOne(cx, x, x->digitLength());
+ } else {
+ // ~x == -x-1 == -(x+1)
+ bool resultNegative = true;
+ return absoluteAddOne(cx, x, resultNegative);
+ }
+}
+
+int64_t BigInt::toInt64(BigInt* x) { return WrapToSigned(toUint64(x)); }
+
+uint64_t BigInt::toUint64(BigInt* x) {
+ if (x->isZero()) {
+ return 0;
+ }
+
+ uint64_t digit = x->digit(0);
+
+ if (DigitBits == 32 && x->digitLength() > 1) {
+ digit |= static_cast<uint64_t>(x->digit(1)) << 32;
+ }
+
+ // Return the two's complement if x is negative.
+ if (x->isNegative()) {
+ return ~(digit - 1);
+ }
+
+ return digit;
+}
+
+bool BigInt::isInt64(BigInt* x, int64_t* result) {
+ MOZ_MAKE_MEM_UNDEFINED(result, sizeof(*result));
+
+ size_t length = x->digitLength();
+ if (length > (DigitBits == 32 ? 2 : 1)) {
+ return false;
+ }
+
+ if (length == 0) {
+ *result = 0;
+ return true;
+ }
+
+ uint64_t magnitude = x->digit(0);
+ if (DigitBits == 32 && length > 1) {
+ magnitude |= static_cast<uint64_t>(x->digit(1)) << 32;
+ }
+
+ if (x->isNegative()) {
+ constexpr uint64_t Int64MinMagnitude = uint64_t(1) << 63;
+ if (magnitude <= Int64MinMagnitude) {
+ *result = magnitude == Int64MinMagnitude
+ ? std::numeric_limits<int64_t>::min()
+ : -AssertedCast<int64_t>(magnitude);
+ return true;
+ }
+ } else {
+ if (magnitude <=
+ static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
+ *result = AssertedCast<int64_t>(magnitude);
+ return true;
+ }
+ }
+
+ return false;
+}
+
+// Compute `2**bits - (x & (2**bits - 1))`. Used when treating BigInt values as
+// arbitrary-precision two's complement signed integers.
+BigInt* BigInt::truncateAndSubFromPowerOfTwo(ExclusiveContext* cx, HandleBigInt x,
+ uint64_t bits,
+ bool resultNegative) {
+ MOZ_ASSERT(bits != 0);
+ MOZ_ASSERT(!x->isZero());
+
+ size_t resultLength = CeilDiv(bits, DigitBits);
+ RootedBigInt result(cx,
+ createUninitialized(cx, resultLength, resultNegative));
+ if (!result) {
+ return nullptr;
+ }
+
+ // Process all digits except the MSD.
+ size_t xLength = x->digitLength();
+ Digit borrow = 0;
+ // Take digits from `x` until its length is exhausted.
+ for (size_t i = 0; i < std::min(resultLength - 1, xLength); i++) {
+ Digit newBorrow = 0;
+ Digit difference = digitSub(0, x->digit(i), &newBorrow);
+ difference = digitSub(difference, borrow, &newBorrow);
+ result->setDigit(i, difference);
+ borrow = newBorrow;
+ }
+ // Then simulate leading zeroes in `x` as needed.
+ for (size_t i = xLength; i < resultLength - 1; i++) {
+ Digit newBorrow = 0;
+ Digit difference = digitSub(0, borrow, &newBorrow);
+ result->setDigit(i, difference);
+ borrow = newBorrow;
+ }
+
+ // The MSD might contain extra bits that we don't want.
+ Digit xMSD = resultLength <= xLength ? x->digit(resultLength - 1) : 0;
+ Digit resultMSD;
+ if (bits % DigitBits == 0) {
+ Digit newBorrow = 0;
+ resultMSD = digitSub(0, xMSD, &newBorrow);
+ resultMSD = digitSub(resultMSD, borrow, &newBorrow);
+ } else {
+ size_t drop = DigitBits - (bits % DigitBits);
+ xMSD = (xMSD << drop) >> drop;
+ Digit minuendMSD = Digit(1) << (DigitBits - drop);
+ Digit newBorrow = 0;
+ resultMSD = digitSub(minuendMSD, xMSD, &newBorrow);
+ resultMSD = digitSub(resultMSD, borrow, &newBorrow);
+ MOZ_ASSERT(newBorrow == 0, "result < 2^bits");
+ // If all subtracted bits were zero, we have to get rid of the
+ // materialized minuendMSD again.
+ resultMSD &= (minuendMSD - 1);
+ }
+ result->setDigit(resultLength - 1, resultMSD);
+
+ return trimHighZeroDigits(cx, result);
+}
+
+BigInt* BigInt::asUintN(ExclusiveContext* cx, HandleBigInt x, uint64_t bits) {
+ if (x->isZero()) {
+ return x;
+ }
+
+ if (bits == 0) {
+ return zero(cx);
+ }
+
+ // When truncating a negative number, simulate two's complement.
+ if (x->isNegative()) {
+ bool resultNegative = false;
+ return truncateAndSubFromPowerOfTwo(cx, x, bits, resultNegative);
+ }
+
+ if (bits <= 64) {
+ uint64_t u64 = toUint64(x);
+ uint64_t mask = uint64_t(-1) >> (64 - bits);
+ return createFromUint64(cx, u64 & mask);
+ }
+
+ if (bits >= MaxBitLength) {
+ return x;
+ }
+
+ Digit msd = x->digit(x->digitLength() - 1);
+ size_t msdBits = DigitBits - DigitLeadingZeroes(msd);
+ size_t bitLength = msdBits + (x->digitLength() - 1) * DigitBits;
+
+ if (bits >= bitLength) {
+ return x;
+ }
+
+ size_t length = CeilDiv(bits, DigitBits);
+ bool isNegative = false;
+
+ BigInt* res = createUninitialized(cx, length, isNegative);
+ if (!res) {
+ return nullptr;
+ }
+
+ MOZ_ASSERT(length >= 2, "single-digit cases should be handled above");
+ MOZ_ASSERT(length <= x->digitLength());
+ for (size_t i = 0; i < length - 1; i++) {
+ res->setDigit(i, x->digit(i));
+ }
+
+ Digit mask = Digit(-1) >> (DigitBits - (bits % DigitBits));
+ res->setDigit(length - 1, x->digit(length - 1) & mask);
+
+ return res;
+}
+
+BigInt* BigInt::asIntN(ExclusiveContext* cx, HandleBigInt x, uint64_t bits) {
+ if (x->isZero()) {
+ return x;
+ }
+
+ if (bits == 0) {
+ return zero(cx);
+ }
+
+ if (bits == 64) {
+ return createFromInt64(cx, toInt64(x));
+ }
+
+ if (bits > MaxBitLength) {
+ return x;
+ }
+
+ Digit msd = x->digit(x->digitLength() - 1);
+ size_t msdBits = DigitBits - DigitLeadingZeroes(msd);
+ size_t bitLength = msdBits + (x->digitLength() - 1) * DigitBits;
+
+ if (bits > bitLength) {
+ return x;
+ }
+
+ Digit signBit = Digit(1) << ((bits - 1) % DigitBits);
+ if (bits == bitLength && msd < signBit) {
+ return x;
+ }
+
+ // All the cases above were the trivial cases: truncating zero, or to zero
+ // bits, or to more bits than are in `x` (so we return `x` directly), or we
+ // already have the 64-bit fast path. If we get here, follow the textbook
+ // algorithm from the specification.
+
+ // BigInt.asIntN step 3: Let `mod` be `x` modulo `2**bits`.
+ RootedBigInt mod(cx, asUintN(cx, x, bits));
+ if (!mod) {
+ return nullptr;
+ }
+
+ // Step 4: If `mod >= 2**(bits - 1)`, return `mod - 2**bits`; otherwise,
+ // return `mod`.
+ if (mod->digitLength() == CeilDiv(bits, DigitBits) &&
+ (mod->digit(mod->digitLength() - 1) & signBit) != 0) {
+ bool resultNegative = true;
+ return truncateAndSubFromPowerOfTwo(cx, mod, bits, resultNegative);
+ }
+
+ return mod;
+}
+
+static bool ValidBigIntOperands(ExclusiveContext* cx, HandleValue lhs,
+ HandleValue rhs) {
+ MOZ_ASSERT(lhs.isBigInt() || rhs.isBigInt());
+
+ if (!lhs.isBigInt() || !rhs.isBigInt()) {
+ if (cx->isJSContext()) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TO_NUMBER);
+ }
+ return false;
+ }
+
+ return true;
+}
+
+bool BigInt::add(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::add(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::sub(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::sub(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::mul(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::mul(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::div(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::div(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::mod(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::mod(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::pow(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::pow(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::neg(ExclusiveContext* cx, HandleValue operand, MutableHandleValue res) {
+ MOZ_ASSERT(operand.isBigInt());
+
+ RootedBigInt operandBigInt(cx, operand.toBigInt());
+ BigInt* resBigInt = BigInt::neg(cx, operandBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::lsh(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::lsh(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::rsh(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::rsh(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::bitAnd(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::bitAnd(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::bitXor(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::bitXor(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::bitOr(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ MutableHandleValue res) {
+ if (!ValidBigIntOperands(cx, lhs, rhs)) {
+ return false;
+ }
+
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ BigInt* resBigInt = BigInt::bitOr(cx, lhsBigInt, rhsBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+bool BigInt::bitNot(ExclusiveContext* cx, HandleValue operand,
+ MutableHandleValue res) {
+ MOZ_ASSERT(operand.isBigInt());
+
+ RootedBigInt operandBigInt(cx, operand.toBigInt());
+ BigInt* resBigInt = BigInt::bitNot(cx, operandBigInt);
+ if (!resBigInt) {
+ return false;
+ }
+ res.setBigInt(resBigInt);
+ return true;
+}
+
+// BigInt proposal section 7.3
+BigInt* js::ToBigInt(ExclusiveContext* cx, HandleValue val) {
+ RootedValue v(cx, val);
+
+ if(cx->isJSContext()) {
+ // Step 1.
+ if (!ToPrimitive(cx->asJSContext(), JSTYPE_NUMBER, &v)) {
+ return nullptr;
+ }
+
+ // Step 2.
+ if (v.isBigInt()) {
+ return v.toBigInt();
+ }
+
+ if (v.isBoolean()) {
+ return v.toBoolean() ? BigInt::one(cx) : BigInt::zero(cx);
+ }
+
+ if (v.isString()) {
+ BigInt* bi = nullptr;
+ RootedString str(cx, v.toString());
+ JS_TRY_VAR_OR_RETURN_NULL(cx, bi, StringToBigInt(cx, str));
+ if (!bi) {
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr,
+ JSMSG_BIGINT_INVALID_SYNTAX);
+ return nullptr;
+ }
+ return bi;
+ }
+
+ JS_ReportErrorNumberASCII(cx->asJSContext(), GetErrorMessage, nullptr, JSMSG_NOT_BIGINT);
+ }
+ return nullptr;
+}
+
+JS::Result<int64_t> js::ToBigInt64(JSContext* cx, HandleValue v) {
+ BigInt* bi = ToBigInt(cx, v);
+ if (!bi) {
+ return cx->alreadyReportedError();
+ }
+ return BigInt::toInt64(bi);
+}
+
+JS::Result<uint64_t> js::ToBigUint64(JSContext* cx, HandleValue v) {
+ BigInt* bi = ToBigInt(cx, v);
+ if (!bi) {
+ return cx->alreadyReportedError();
+ }
+ return BigInt::toUint64(bi);
+}
+
+double BigInt::numberValue(BigInt* x) {
+ if (x->isZero()) {
+ return 0.0;
+ }
+
+ using Double = mozilla::FloatingPoint<double>;
+ constexpr uint8_t ExponentShift = Double::kExponentShift;
+ constexpr uint8_t SignificandWidth = Double::kSignificandWidth;
+ constexpr unsigned ExponentBias = Double::kExponentBias;
+ constexpr uint8_t SignShift = Double::kExponentWidth + SignificandWidth;
+
+ size_t length = x->digitLength();
+ MOZ_ASSERT(length != 0);
+
+ // Fast path for the likely-common case of up to a uint64_t of magnitude
+ // that doesn't exceed integral precision in IEEE-754.
+ if (length <= 64 / DigitBits) {
+ uint64_t magnitude = x->digit(0);
+ if (DigitBits == 32 && length > 1) {
+ magnitude |= static_cast<uint64_t>(x->digit(1)) << 32;
+ }
+ const uint64_t MaxIntegralPrecisionDouble = uint64_t(1)
+ << (SignificandWidth + 1);
+ if (magnitude <= MaxIntegralPrecisionDouble) {
+ return x->isNegative() ? -double(magnitude) : +double(magnitude);
+ }
+ }
+
+ Digit msd = x->digit(length - 1);
+ uint8_t msdLeadingZeroes = DigitLeadingZeroes(msd);
+
+ // `2**ExponentBias` is the largest power of two in a finite IEEE-754
+ // double. If this bigint has a greater power of two, it'll round to
+ // infinity.
+ uint64_t exponent = length * DigitBits - msdLeadingZeroes - 1;
+ if (exponent > ExponentBias) {
+ return x->isNegative() ? mozilla::NegativeInfinity<double>()
+ : mozilla::PositiveInfinity<double>();
+ }
+
+ // Otherwise munge the most significant bits of the number into proper
+ // position in an IEEE-754 double and go to town.
+
+ // Omit the most significant bit: the IEEE-754 format includes this bit
+ // implicitly for all double-precision integers.
+ const uint8_t msdIgnoredBits = msdLeadingZeroes + 1;
+ const uint8_t msdIncludedBits = DigitBits - msdIgnoredBits;
+
+ uint8_t bitsFilled = msdIncludedBits;
+
+ // Shift `msd`'s contributed bits upward to remove high-order zeroes and
+ // the highest set bit (which is implicit in IEEE-754 integral values so
+ // must be removed) and to add low-order zeroes.
+ uint64_t shiftedMantissa =
+ msdIncludedBits == 0 ? 0 : uint64_t(msd) << (64 - msdIncludedBits);
+
+ // Add in bits from the next one or two digits if `msd` didn't contain all
+ // bits necessary to define the result. (The extra bit allows us to
+ // properly round an inexact overall result.) Any lower bits that are
+ // uselessly set will be shifted away when `shiftedMantissa` is converted to
+ // a real mantissa.
+ if (bitsFilled < SignificandWidth + 1) {
+ MOZ_ASSERT(length >= 2,
+ "single-Digit numbers with this few bits should have been "
+ "handled by the fast-path above");
+
+ Digit second = x->digit(length - 2);
+ if (DigitBits == 32) {
+ shiftedMantissa |= uint64_t(second) << msdIgnoredBits;
+ bitsFilled += DigitBits;
+
+ // Add in bits from another digit, if any, if we still have unfilled
+ // significand bits.
+ if (bitsFilled < SignificandWidth + 1 && length >= 3) {
+ Digit third = x->digit(length - 3);
+ shiftedMantissa |= uint64_t(third) >> msdIncludedBits;
+ // The second and third 32-bit digits contributed 64 bits total, filling
+ // well beyond the mantissa.
+ bitsFilled = 64;
+ }
+ } else {
+ shiftedMantissa |= second >> msdIncludedBits;
+ // A full 64-bit digit's worth of bits (some from the most significant
+ // digit, the rest from the next) fills well beyond the mantissa.
+ bitsFilled = 64;
+ }
+ }
+
+ // Round the overall result, if necessary. (It's possible we don't need to
+ // round -- the number might not have enough bits to round.)
+ if (bitsFilled >= SignificandWidth + 1) {
+ constexpr uint64_t LeastSignificantBit = uint64_t(1)
+ << (64 - SignificandWidth);
+ constexpr uint64_t ExtraBit = LeastSignificantBit >> 1;
+
+ // When the first bit outside the significand is set, the overall value
+ // is rounded: downward (i.e. no change to the bits) if the least
+ // significant bit in the significand is zero, upward if it instead is
+ // one.
+ if ((shiftedMantissa & ExtraBit) &&
+ (shiftedMantissa & LeastSignificantBit)) {
+ // We're rounding upward: add to the significand bits. If they
+ // overflow, the exponent must also be increased. If *that*
+ // overflows, return the appropriate infinity.
+ uint64_t before = shiftedMantissa;
+ shiftedMantissa += ExtraBit;
+ if (shiftedMantissa < before) {
+ exponent++;
+ if (exponent > ExponentBias) {
+ return x->isNegative() ? NegativeInfinity<double>()
+ : PositiveInfinity<double>();
+ }
+ }
+ }
+ }
+
+ uint64_t significandBits = shiftedMantissa >> (64 - SignificandWidth);
+ uint64_t signBit = uint64_t(x->isNegative() ? 1 : 0) << SignShift;
+ uint64_t exponentBits = (exponent + ExponentBias) << ExponentShift;
+ return mozilla::BitwiseCast<double>(signBit | exponentBits | significandBits);
+}
+
+int8_t BigInt::compare(BigInt* x, BigInt* y) {
+ // Sanity checks to catch negative zeroes escaping to the wild.
+ MOZ_ASSERT(!x->isNegative() || !x->isZero());
+ MOZ_ASSERT(!y->isNegative() || !y->isZero());
+
+ bool xSign = x->isNegative();
+
+ if (xSign != y->isNegative()) {
+ return xSign ? -1 : 1;
+ }
+
+ if (xSign) {
+ mozilla::Swap(x, y);
+ }
+
+ return absoluteCompare(x, y);
+}
+
+bool BigInt::equal(BigInt* lhs, BigInt* rhs) {
+ if (lhs == rhs) {
+ return true;
+ }
+ if (lhs->digitLength() != rhs->digitLength()) {
+ return false;
+ }
+ if (lhs->isNegative() != rhs->isNegative()) {
+ return false;
+ }
+ for (size_t i = 0; i < lhs->digitLength(); i++) {
+ if (lhs->digit(i) != rhs->digit(i)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+int8_t BigInt::compare(BigInt* x, double y) {
+ MOZ_ASSERT(!mozilla::IsNaN(y));
+
+ constexpr int LessThan = -1, Equal = 0, GreaterThan = 1;
+
+ // ±Infinity exceeds a finite bigint value.
+ if (!mozilla::IsFinite(y)) {
+ return y > 0 ? LessThan : GreaterThan;
+ }
+
+ // Handle `x === 0n` and `y == 0` special cases.
+ if (x->isZero()) {
+ if (y == 0) {
+ // -0 and +0 are treated identically.
+ return Equal;
+ }
+
+ return y > 0 ? LessThan : GreaterThan;
+ }
+
+ const bool xNegative = x->isNegative();
+ if (y == 0) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+
+ // Nonzero `x` and `y` with different signs are trivially compared.
+ const bool yNegative = y < 0;
+ if (xNegative != yNegative) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+
+ // `x` and `y` are same-signed. Determine which has greater magnitude,
+ // then combine that with the signedness just computed to reach a result.
+ const int exponent = mozilla::ExponentComponent(y);
+ if (exponent < 0) {
+ // `y` is a nonzero fraction of magnitude less than 1.
+ return xNegative ? LessThan : GreaterThan;
+ }
+
+ size_t xLength = x->digitLength();
+ MOZ_ASSERT(xLength > 0);
+
+ Digit xMSD = x->digit(xLength - 1);
+ const int shift = DigitLeadingZeroes(xMSD);
+ int xBitLength = xLength * DigitBits - shift;
+
+ // Differing bit-length makes for a simple comparison.
+ int yBitLength = exponent + 1;
+ if (xBitLength < yBitLength) {
+ return xNegative ? GreaterThan : LessThan;
+ }
+ if (xBitLength > yBitLength) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+
+ // Compare the high 64 bits of both numbers. (Lower-order bits not present
+ // in either number are zeroed.) Either that distinguishes `x` and `y`, or
+ // `x` and `y` differ only if a subsequent nonzero bit in `x` means `x` has
+ // larger magnitude.
+
+ using Double = mozilla::FloatingPoint<double>;
+ constexpr uint8_t SignificandWidth = Double::kSignificandWidth;
+ constexpr uint64_t SignificandBits = Double::kSignificandBits;
+
+ const uint64_t doubleBits = mozilla::BitwiseCast<uint64_t>(y);
+ const uint64_t significandBits = doubleBits & SignificandBits;
+
+ // Readd the implicit-one bit when constructing `y`'s high 64 bits.
+ const uint64_t yHigh64Bits =
+ ((uint64_t(1) << SignificandWidth) | significandBits)
+ << (64 - SignificandWidth - 1);
+
+ // Cons up `x`'s high 64 bits, backfilling zeroes for binary fractions of 1
+ // if `x` doesn't have 64 bits.
+ uint8_t xBitsFilled = DigitBits - shift;
+ uint64_t xHigh64Bits = uint64_t(xMSD) << (64 - xBitsFilled);
+
+ // At this point we no longer need to look at the most significant digit.
+ xLength--;
+
+ // The high 64 bits from `x` will probably not align to a digit boundary.
+ // `xHasNonZeroLeftoverBits` will be set to true if any remaining
+ // least-significant bit from the digit holding xHigh64Bits's
+ // least-significant bit is nonzero.
+ bool xHasNonZeroLeftoverBits = false;
+
+ if (xBitsFilled < std::min(xBitLength, 64)) {
+ MOZ_ASSERT(xLength >= 1,
+ "If there are more bits to fill, there should be "
+ "more digits to fill them from");
+
+ Digit second = x->digit(--xLength);
+ if (DigitBits == 32) {
+ xBitsFilled += 32;
+ xHigh64Bits |= uint64_t(second) << (64 - xBitsFilled);
+ if (xBitsFilled < 64 && xLength >= 1) {
+ Digit third = x->digit(--xLength);
+ const uint8_t neededBits = 64 - xBitsFilled;
+ xHigh64Bits |= uint64_t(third) >> (DigitBits - neededBits);
+ xHasNonZeroLeftoverBits = (third << neededBits) != 0;
+ }
+ } else {
+ const uint8_t neededBits = 64 - xBitsFilled;
+ xHigh64Bits |= uint64_t(second) >> (DigitBits - neededBits);
+ xHasNonZeroLeftoverBits = (second << neededBits) != 0;
+ }
+ }
+
+ // If high bits are unequal, the larger one has greater magnitude.
+ if (yHigh64Bits > xHigh64Bits) {
+ return xNegative ? GreaterThan : LessThan;
+ }
+ if (xHigh64Bits > yHigh64Bits) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+
+ // Otherwise the top 64 bits of both are equal. If the values differ, a
+ // lower-order bit in `x` is nonzero and `x` has greater magnitude than
+ // `y`; otherwise `x == y`.
+ if (xHasNonZeroLeftoverBits) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+ while (xLength != 0) {
+ if (x->digit(--xLength) != 0) {
+ return xNegative ? LessThan : GreaterThan;
+ }
+ }
+
+ return Equal;
+}
+
+bool BigInt::equal(BigInt* lhs, double rhs) {
+ if (mozilla::IsNaN(rhs)) {
+ return false;
+ }
+ return compare(lhs, rhs) == 0;
+}
+
+// BigInt proposal section 3.2.5
+JS::Result<bool> BigInt::looselyEqual(ExclusiveContext* cx, HandleBigInt lhs,
+ HandleValue rhs) {
+ // Step 1.
+ if (rhs.isBigInt()) {
+ return equal(lhs, rhs.toBigInt());
+ }
+
+ // Steps 2-5 (not applicable).
+
+ // Steps 6-7.
+ if (rhs.isString()) {
+ RootedBigInt rhsBigInt(cx);
+ RootedString rhsString(cx, rhs.toString());
+ MOZ_TRY_VAR(rhsBigInt, StringToBigInt(cx, rhsString));
+ if (!rhsBigInt) {
+ return false;
+ }
+ return equal(lhs, rhsBigInt);
+ }
+
+ // Steps 8-9 (not applicable).
+
+ // Steps 10-11.
+ if (rhs.isObject()) {
+ RootedValue rhsPrimitive(cx, rhs);
+ if (!cx->isJSContext() || !ToPrimitive(cx->asJSContext(), &rhsPrimitive)) {
+ return cx->alreadyReportedError();
+ }
+ return looselyEqual(cx, lhs, rhsPrimitive);
+ }
+
+ // Step 12.
+ if (rhs.isNumber()) {
+ return equal(lhs, rhs.toNumber());
+ }
+
+ // Step 13.
+ return false;
+}
+
+// BigInt proposal section 1.1.12. BigInt::lessThan ( x, y )
+bool BigInt::lessThan(BigInt* x, BigInt* y) { return compare(x, y) < 0; }
+
+Maybe<bool> BigInt::lessThan(BigInt* lhs, double rhs) {
+ if (mozilla::IsNaN(rhs)) {
+ return Maybe<bool>(Nothing());
+ }
+ return Some(compare(lhs, rhs) < 0);
+}
+
+Maybe<bool> BigInt::lessThan(double lhs, BigInt* rhs) {
+ if (mozilla::IsNaN(lhs)) {
+ return Maybe<bool>(Nothing());
+ }
+ return Some(-compare(rhs, lhs) < 0);
+}
+
+bool BigInt::lessThan(ExclusiveContext* cx, HandleBigInt lhs, HandleString rhs,
+ Maybe<bool>& res) {
+ RootedBigInt rhsBigInt(cx);
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, rhsBigInt, StringToBigInt(cx, rhs));
+ if (!rhsBigInt) {
+ res = Nothing();
+ return true;
+ }
+ res = Some(lessThan(lhs, rhsBigInt));
+ return true;
+}
+
+bool BigInt::lessThan(ExclusiveContext* cx, HandleString lhs, HandleBigInt rhs,
+ Maybe<bool>& res) {
+ RootedBigInt lhsBigInt(cx);
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, lhsBigInt, StringToBigInt(cx, lhs));
+ if (!lhsBigInt) {
+ res = Nothing();
+ return true;
+ }
+ res = Some(lessThan(lhsBigInt, rhs));
+ return true;
+}
+
+bool BigInt::lessThan(ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ Maybe<bool>& res) {
+ if (lhs.isBigInt()) {
+ if (rhs.isString()) {
+ RootedBigInt lhsBigInt(cx, lhs.toBigInt());
+ RootedString rhsString(cx, rhs.toString());
+ return lessThan(cx, lhsBigInt, rhsString, res);
+ }
+
+ if (rhs.isNumber()) {
+ res = lessThan(lhs.toBigInt(), rhs.toNumber());
+ return true;
+ }
+
+ MOZ_ASSERT(rhs.isBigInt());
+ res = Some(lessThan(lhs.toBigInt(), rhs.toBigInt()));
+ return true;
+ }
+
+ MOZ_ASSERT(rhs.isBigInt());
+ if (lhs.isString()) {
+ RootedString lhsString(cx, lhs.toString());
+ RootedBigInt rhsBigInt(cx, rhs.toBigInt());
+ return lessThan(cx, lhsString, rhsBigInt, res);
+ }
+
+ MOZ_ASSERT(lhs.isNumber());
+ res = lessThan(lhs.toNumber(), rhs.toBigInt());
+ return true;
+}
+
+JSLinearString* BigInt::toString(ExclusiveContext* cx, HandleBigInt x, uint8_t radix) {
+ MOZ_ASSERT(2 <= radix && radix <= 36);
+
+ if (x->isZero()) {
+ return cx->staticStrings().getInt(0);
+ }
+
+ if (mozilla::IsPowerOfTwo(radix)) {
+ return toStringBasePowerOfTwo(cx, x, radix);
+ }
+
+ return toStringGeneric(cx, x, radix);
+}
+
+template <typename CharT>
+static inline BigInt* ParseStringBigIntLiteral(ExclusiveContext* cx,
+ Range<const CharT> range,
+ bool* haveParseError) {
+ auto start = range.begin();
+ auto end = range.end();
+
+ while (start < end && unicode::IsSpace(start[0])) {
+ start++;
+ }
+
+ while (start < end && unicode::IsSpace(end[-1])) {
+ end--;
+ }
+
+ if (start == end) {
+ return BigInt::zero(cx);
+ }
+
+ // StringNumericLiteral ::: StrDecimalLiteral, but without Infinity, decimal
+ // points, or exponents. Note that the raw '+' or '-' cases fall through
+ // because the string is too short, and eventually signal a parse error.
+ if (end - start > 1) {
+ if (start[0] == '+') {
+ bool isNegative = false;
+ start++;
+ return BigInt::parseLiteralDigits(cx, Range<const CharT>(start, end), 10,
+ isNegative, haveParseError);
+ } else if (start[0] == '-') {
+ bool isNegative = true;
+ start++;
+ return BigInt::parseLiteralDigits(cx, Range<const CharT>(start, end), 10,
+ isNegative, haveParseError);
+ }
+ }
+
+ return BigInt::parseLiteral(cx, Range<const CharT>(start, end),
+ haveParseError);
+}
+
+// Called from BigInt constructor.
+JS::Result<BigInt*, JS::OOM&> js::StringToBigInt(ExclusiveContext* cx,
+ HandleString str) {
+ JSLinearString* linear = str->ensureLinear(cx);
+ if (!linear) {
+ return cx->alreadyReportedOOM();
+ }
+
+ BigInt* res = nullptr;
+ bool parseError = false;
+
+ if(cx->isJSContext()) {
+ AutoStableStringChars chars(cx->asJSContext());
+ if (!chars.init(cx->asJSContext(), str)) {
+ return cx->alreadyReportedOOM();
+ }
+
+ if (chars.isLatin1()) {
+ res = ParseStringBigIntLiteral(cx->asJSContext(), chars.latin1Range(), &parseError);
+ } else {
+ res = ParseStringBigIntLiteral(cx->asJSContext(), chars.twoByteRange(), &parseError);
+ }
+ }
+
+ // A nullptr result can indicate either a parse error or out-of-memory.
+ if (!res && !parseError) {
+ return cx->alreadyReportedOOM();
+ }
+
+ return res;
+}
+
+// Called from parser with already trimmed and validated token.
+BigInt* js::ParseBigIntLiteral(ExclusiveContext* cx,
+ const Range<const char16_t>& chars) {
+ bool parseError = false;
+ BigInt* res = BigInt::parseLiteral(cx, chars, &parseError);
+ if (!res) {
+ return nullptr;
+ }
+ MOZ_RELEASE_ASSERT(!parseError);
+ return res;
+}
+
+JSAtom* js::BigIntToAtom(ExclusiveContext* cx, HandleBigInt bi) {
+ JSString* str = BigInt::toString(cx, bi, 10);
+ if (!str) {
+ return nullptr;
+ }
+ return AtomizeString(cx, str);
+}
+
+JS::ubi::Node::Size JS::ubi::Concrete<BigInt>::size(
+ mozilla::MallocSizeOf mallocSizeOf) const {
+ BigInt& bi = get();
+ MOZ_ASSERT(bi.isTenured());
+ size_t size = js::gc::Arena::thingSize(bi.asTenured().getAllocKind());
+ size += bi.sizeOfExcludingThis(mallocSizeOf);
+ return size;
+}
+
+template <XDRMode mode>
+bool js::XDRBigInt(XDRState<mode>* xdr, MutableHandleBigInt bi) {
+ ExclusiveContext* cx = xdr->cx();
+
+ uint8_t sign;
+ uint32_t length;
+
+ if (mode == XDR_ENCODE) {
+ sign = static_cast<uint8_t>(bi->isNegative());
+ uint64_t sz = bi->digitLength() * sizeof(BigInt::Digit);
+ // As the maximum source code size is currently UINT32_MAX code units
+ // (see BytecodeCompiler::checkLength), any bigint literal's length in
+ // word-sized digits will be less than UINT32_MAX as well. That could
+ // change or FoldConstants could start creating these though, so leave
+ // this as a release-enabled assert.
+ MOZ_RELEASE_ASSERT(sz <= UINT32_MAX);
+ length = static_cast<uint32_t>(sz);
+ }
+
+ if(!xdr->codeUint8(&sign))
+ return false;
+ if(!xdr->codeUint32(&length))
+ return false;
+
+ MOZ_RELEASE_ASSERT(length % sizeof(BigInt::Digit) == 0);
+ uint32_t digitLength = length / sizeof(BigInt::Digit);
+ auto buf = cx->make_pod_array<BigInt::Digit>(digitLength);
+ if (!buf) {
+ return xdr->fail(JS::TranscodeResult_Throw);
+ }
+
+ if (mode == XDR_ENCODE) {
+ std::uninitialized_copy_n(bi->digits().Elements(), digitLength, buf.get());
+ }
+
+ if(!xdr->codeBytes(buf.get(), length))
+ return false;
+
+ if (mode == XDR_DECODE) {
+ BigInt* res = BigInt::createUninitialized(cx, digitLength, sign);
+ if (!res) {
+ return xdr->fail(JS::TranscodeResult_Throw);
+ }
+ std::uninitialized_copy_n(buf.get(), digitLength, bi->digits().Elements());
+ bi.set(res);
+ }
+
+ return true;
+}
+
+template bool js::XDRBigInt(XDRState<XDR_ENCODE>* xdr, MutableHandleBigInt bi);
+
+template bool js::XDRBigInt(XDRState<XDR_DECODE>* xdr, MutableHandleBigInt bi);
+
diff --git a/js/src/vm/BigIntType.h b/js/src/vm/BigIntType.h
new file mode 100644
index 0000000000..ea0317fd9c
--- /dev/null
+++ b/js/src/vm/BigIntType.h
@@ -0,0 +1,386 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef vm_BigIntType_h
+#define vm_BigIntType_h
+
+#include "mozilla/Range.h"
+#include "mozilla/Span.h"
+
+#include "gc/Barrier.h"
+#include "gc/Marking.h"
+#include "gc/Heap.h"
+#include "js/GCHashTable.h"
+#include "js/RootingAPI.h"
+#include "js/TraceKind.h"
+#include "js/TypeDecls.h"
+#include "vm/String.h"
+#include "vm/Xdr.h"
+
+// Handle future js::gc::Cell::ReservedBits, we have no reserved bits...
+#define js_gc_Cell_ReservedBits 0
+// Handle future js::gc::MinCellSize, 16 bytes, twice our js:gc:CellSize
+#define js_gc_MinCellSize (js::gc::CellSize*2)
+
+namespace JS {
+
+class BigInt;
+
+} // namespace JS
+
+namespace js {
+
+template <XDRMode mode>
+bool XDRBigInt(XDRState<mode>* xdr, MutableHandle<JS::BigInt*> bi);
+
+} // namespace js
+
+namespace JS {
+
+class BigInt final : public js::gc::TenuredCell {
+ public:
+ using Digit = uintptr_t;
+
+ private:
+ // The low js::gc::Cell::ReservedBits are reserved.
+ static constexpr uintptr_t SignBit = JS_BIT(js_gc_Cell_ReservedBits);
+ static constexpr uintptr_t LengthShift = js_gc_Cell_ReservedBits + 1;
+ static constexpr size_t InlineDigitsLength =
+ (js_gc_MinCellSize - sizeof(uintptr_t)) / sizeof(Digit);
+
+ uintptr_t lengthSignAndReservedBits_;
+
+ // The digit storage starts with the least significant digit (little-endian
+ // digit order). Byte order within a digit is of course native endian.
+ union {
+ Digit* heapDigits_;
+ Digit inlineDigits_[InlineDigitsLength];
+ };
+
+ public:
+ static const JS::TraceKind TraceKind = JS::TraceKind::BigInt;
+
+ size_t digitLength() const {
+ return lengthSignAndReservedBits_ >> LengthShift;
+ }
+
+ bool hasInlineDigits() const { return digitLength() <= InlineDigitsLength; }
+ bool hasHeapDigits() const { return !hasInlineDigits(); }
+
+ using Digits = mozilla::Span<Digit>;
+ Digits digits() {
+ return Digits(hasInlineDigits() ? inlineDigits_ : heapDigits_,
+ digitLength());
+ }
+ Digit digit(size_t idx) { return digits()[idx]; }
+ void setDigit(size_t idx, Digit digit) { digits()[idx] = digit; }
+
+ bool isZero() const { return digitLength() == 0; }
+ bool isNegative() const { return lengthSignAndReservedBits_ & SignBit; }
+
+ // Offset for direct access from JIT code.
+ static constexpr size_t offsetOfLengthSignAndReservedBits() {
+ return offsetof(BigInt, lengthSignAndReservedBits_);
+ }
+
+ void initializeDigitsToZero();
+
+ void traceChildren(JSTracer* trc);
+ void finalize(js::FreeOp* fop);
+ js::HashNumber hash();
+ size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
+
+ static BigInt* createUninitialized(js::ExclusiveContext* cx, size_t length,
+ bool isNegative);
+ static BigInt* createFromDouble(js::ExclusiveContext* cx, double d);
+ static BigInt* createFromUint64(js::ExclusiveContext* cx, uint64_t n);
+ static BigInt* createFromInt64(js::ExclusiveContext* cx, int64_t n);
+ // FIXME: Cache these values.
+ static BigInt* zero(js::ExclusiveContext* cx);
+ static BigInt* one(js::ExclusiveContext* cx);
+
+ static BigInt* copy(js::ExclusiveContext* cx, Handle<BigInt*> x);
+ static BigInt* add(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* sub(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* mul(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* div(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* mod(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* pow(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* neg(js::ExclusiveContext* cx, Handle<BigInt*> x);
+ static BigInt* lsh(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* rsh(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* bitAnd(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* bitXor(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* bitOr(js::ExclusiveContext* cx, Handle<BigInt*> x, Handle<BigInt*> y);
+ static BigInt* bitNot(js::ExclusiveContext* cx, Handle<BigInt*> x);
+
+ static int64_t toInt64(BigInt* x);
+ static uint64_t toUint64(BigInt* x);
+
+ // Return true if the BigInt is without loss of precision representable as an
+ // int64 and store the int64 value in the output. Otherwise return false and
+ // leave the value of the output parameter unspecified.
+ static bool isInt64(BigInt* x, int64_t* result);
+
+ static BigInt* asIntN(js::ExclusiveContext* cx, Handle<BigInt*> x, uint64_t bits);
+ static BigInt* asUintN(js::ExclusiveContext* cx, Handle<BigInt*> x, uint64_t bits);
+
+ // Type-checking versions of arithmetic operations. These methods
+ // must be called with at least one BigInt operand. Binary
+ // operations will throw a TypeError if one of the operands is not a
+ // BigInt value.
+ static bool add(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool sub(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool mul(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool div(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool mod(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool pow(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool neg(js::ExclusiveContext* cx, Handle<Value> operand,
+ MutableHandle<Value> res);
+ static bool lsh(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool rsh(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool bitAnd(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool bitXor(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool bitOr(js::ExclusiveContext* cx, Handle<Value> lhs, Handle<Value> rhs,
+ MutableHandle<Value> res);
+ static bool bitNot(js::ExclusiveContext* cx, Handle<Value> operand,
+ MutableHandle<Value> res);
+
+ static double numberValue(BigInt* x);
+
+ static JSLinearString* toString(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ uint8_t radix);
+ template <typename CharT>
+ static BigInt* parseLiteral(js::ExclusiveContext* cx,
+ const mozilla::Range<const CharT> chars,
+ bool* haveParseError);
+ template <typename CharT>
+ static BigInt* parseLiteralDigits(js::ExclusiveContext* cx,
+ const mozilla::Range<const CharT> chars,
+ unsigned radix, bool isNegative,
+ bool* haveParseError);
+
+ static int8_t compare(BigInt* lhs, BigInt* rhs);
+ static bool equal(BigInt* lhs, BigInt* rhs);
+ static JS::Result<bool> looselyEqual(js::ExclusiveContext* cx, Handle<BigInt*> lhs,
+ HandleValue rhs);
+
+ static bool lessThan(BigInt* x, BigInt* y);
+ // These methods return Nothing when the non-BigInt operand is NaN
+ // or a string that can't be interpreted as a BigInt.
+ static mozilla::Maybe<bool> lessThan(BigInt* lhs, double rhs);
+ static mozilla::Maybe<bool> lessThan(double lhs, BigInt* rhs);
+ static bool lessThan(js::ExclusiveContext* cx, Handle<BigInt*> lhs, HandleString rhs,
+ mozilla::Maybe<bool>& res);
+ static bool lessThan(js::ExclusiveContext* cx, HandleString lhs, Handle<BigInt*> rhs,
+ mozilla::Maybe<bool>& res);
+ static bool lessThan(js::ExclusiveContext* cx, HandleValue lhs, HandleValue rhs,
+ mozilla::Maybe<bool>& res);
+
+ private:
+ static constexpr size_t DigitBits = sizeof(Digit) * CHAR_BIT;
+ static constexpr size_t HalfDigitBits = DigitBits / 2;
+ static constexpr Digit HalfDigitMask = (1ull << HalfDigitBits) - 1;
+
+ static_assert(DigitBits == 32 || DigitBits == 64,
+ "Unexpected BigInt Digit size");
+
+ // The maximum number of digits that the current implementation supports
+ // would be 0x7fffffff / DigitBits. However, we use a lower limit for now,
+ // because raising it later is easier than lowering it. Support up to 1
+ // million bits.
+ static constexpr size_t MaxBitLength = 1024 * 1024;
+ static constexpr size_t MaxDigitLength = MaxBitLength / DigitBits;
+
+ // BigInts can be serialized to strings of radix between 2 and 36. For a
+ // given bigint, radix 2 will take the most characters (one per bit).
+ // Ensure that the max bigint size is small enough so that we can fit the
+ // corresponding character count into a size_t, with space for a possible
+ // sign prefix.
+ static_assert(MaxBitLength <= std::numeric_limits<size_t>::max() - 1,
+ "BigInt max length must be small enough to be serialized as a "
+ "binary string");
+
+ static size_t calculateMaximumCharactersRequired(HandleBigInt x,
+ unsigned radix);
+ static MOZ_MUST_USE bool calculateMaximumDigitsRequired(js::ExclusiveContext* cx,
+ uint8_t radix,
+ size_t charCount,
+ size_t* result);
+
+ static bool absoluteDivWithDigitDivisor(
+ js::ExclusiveContext* cx, Handle<BigInt*> x, Digit divisor,
+ const mozilla::Maybe<MutableHandle<BigInt*>>& quotient, Digit* remainder,
+ bool quotientNegative);
+ static void internalMultiplyAdd(BigInt* source, Digit factor, Digit summand,
+ unsigned, BigInt* result);
+ static void multiplyAccumulate(BigInt* multiplicand, Digit multiplier,
+ BigInt* accumulator,
+ unsigned accumulatorIndex);
+ static bool absoluteDivWithBigIntDivisor(
+ js::ExclusiveContext* cx, Handle<BigInt*> dividend, Handle<BigInt*> divisor,
+ const mozilla::Maybe<MutableHandle<BigInt*>>& quotient,
+ const mozilla::Maybe<MutableHandle<BigInt*>>& remainder,
+ bool quotientNegative);
+
+ enum class LeftShiftMode { SameSizeResult, AlwaysAddOneDigit };
+
+ static BigInt* absoluteLeftShiftAlwaysCopy(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ unsigned shift, LeftShiftMode);
+ static bool productGreaterThan(Digit factor1, Digit factor2, Digit high,
+ Digit low);
+ static BigInt* lshByAbsolute(js::ExclusiveContext* cx, HandleBigInt x, HandleBigInt y);
+ static BigInt* rshByAbsolute(js::ExclusiveContext* cx, HandleBigInt x, HandleBigInt y);
+ static BigInt* rshByMaximum(js::ExclusiveContext* cx, bool isNegative);
+ static BigInt* truncateAndSubFromPowerOfTwo(js::ExclusiveContext* cx, HandleBigInt x,
+ uint64_t bits,
+ bool resultNegative);
+
+ Digit absoluteInplaceAdd(BigInt* summand, unsigned startIndex);
+ Digit absoluteInplaceSub(BigInt* subtrahend, unsigned startIndex);
+ void inplaceRightShiftLowZeroBits(unsigned shift);
+ void inplaceMultiplyAdd(Digit multiplier, Digit part);
+
+ // The result of an SymmetricTrim bitwise op has as many digits as the
+ // smaller operand. A SymmetricFill bitwise op result has as many digits as
+ // the larger operand, with high digits (if any) copied from the larger
+ // operand. AsymmetricFill is like SymmetricFill, except the result has as
+ // many digits as the first operand; this kind is used for the and-not
+ // operation.
+ enum class BitwiseOpKind { SymmetricTrim, SymmetricFill, AsymmetricFill };
+
+ template <BitwiseOpKind kind, typename BitwiseOp>
+ static BigInt* absoluteBitwiseOp(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y, BitwiseOp&& op);
+
+ // Return `|x| & |y|`.
+ static BigInt* absoluteAnd(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y);
+
+ // Return `|x| | |y|`.
+ static BigInt* absoluteOr(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y);
+
+ // Return `|x| & ~|y|`.
+ static BigInt* absoluteAndNot(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y);
+
+ // Return `|x| ^ |y|`.
+ static BigInt* absoluteXor(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y);
+
+ // Return `(|x| + 1) * (resultNegative ? -1 : +1)`.
+ static BigInt* absoluteAddOne(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ bool resultNegative);
+
+ // Return `(|x| - 1) * (resultNegative ? -1 : +1)`, with the precondition that
+ // |x| != 0.
+ static BigInt* absoluteSubOne(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ unsigned resultLength);
+
+ // Return `a + b`, incrementing `*carry` if the addition overflows.
+ static inline Digit digitAdd(Digit a, Digit b, Digit* carry) {
+ Digit result = a + b;
+ *carry += static_cast<Digit>(result < a);
+ return result;
+ }
+
+ // Return `left - right`, incrementing `*borrow` if the addition overflows.
+ static inline Digit digitSub(Digit left, Digit right, Digit* borrow) {
+ Digit result = left - right;
+ *borrow += static_cast<Digit>(result > left);
+ return result;
+ }
+
+ // Compute `a * b`, returning the low half of the result and putting the
+ // high half in `*high`.
+ static Digit digitMul(Digit a, Digit b, Digit* high);
+
+ // Divide `(high << DigitBits) + low` by `divisor`, returning the quotient
+ // and storing the remainder in `*remainder`, with the precondition that
+ // `high < divisor` so that the result fits in a Digit.
+ static Digit digitDiv(Digit high, Digit low, Digit divisor, Digit* remainder);
+
+ // Return `(|x| + |y|) * (resultNegative ? -1 : +1)`.
+ static BigInt* absoluteAdd(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y, bool resultNegative);
+
+ // Return `(|x| - |y|) * (resultNegative ? -1 : +1)`, with the precondition
+ // that |x| >= |y|.
+ static BigInt* absoluteSub(js::ExclusiveContext* cx, Handle<BigInt*> x,
+ Handle<BigInt*> y, bool resultNegative);
+
+ // If `|x| < |y|` return -1; if `|x| == |y|` return 0; otherwise return 1.
+ static int8_t absoluteCompare(BigInt* lhs, BigInt* rhs);
+
+ static int8_t compare(BigInt* lhs, double rhs);
+
+ static bool equal(BigInt* lhs, double rhs);
+
+ static JSLinearString* toStringBasePowerOfTwo(js::ExclusiveContext* cx, Handle<BigInt*>,
+ unsigned radix);
+ static JSLinearString* toStringGeneric(js::ExclusiveContext* cx, Handle<BigInt*>,
+ unsigned radix);
+
+ static BigInt* trimHighZeroDigits(js::ExclusiveContext* cx, Handle<BigInt*> x);
+ static BigInt* destructivelyTrimHighZeroDigits(js::ExclusiveContext* cx,
+ Handle<BigInt*> x);
+
+ friend struct JSStructuredCloneReader;
+ friend struct JSStructuredCloneWriter;
+ template <js::XDRMode mode>
+ friend bool js::XDRBigInt(js::XDRState<mode>* xdr, MutableHandle<BigInt*> bi);
+
+ BigInt() = delete;
+ BigInt(const BigInt& other) = delete;
+ void operator=(const BigInt& other) = delete;
+};
+
+static_assert(
+ sizeof(BigInt) >= js_gc_MinCellSize,
+ "sizeof(BigInt) must be greater than the minimum allocation size");
+
+static_assert(
+ sizeof(BigInt) == js_gc_MinCellSize,
+ "sizeof(BigInt) intended to be the same as the minimum allocation size");
+
+} // namespace JS
+
+namespace js {
+
+extern JSAtom* BigIntToAtom(js::ExclusiveContext* cx, JS::HandleBigInt bi);
+
+extern JS::BigInt* NumberToBigInt(js::ExclusiveContext* cx, double d);
+extern JS::Result<int64_t> ToBigInt64(JSContext* cx, JS::Handle<JS::Value> v);
+extern JS::Result<uint64_t> ToBigUint64(JSContext* cx, JS::Handle<JS::Value> v);
+
+// Parse a BigInt from a string, using the method specified for StringToBigInt.
+// Used by the BigInt constructor among other places.
+extern JS::Result<JS::BigInt*, JS::OOM&> StringToBigInt(
+ js::ExclusiveContext* cx, JS::Handle<JSString*> str);
+
+// Parse a BigInt from an already-validated numeric literal. Used by the
+// parser. Can only fail in out-of-memory situations.
+extern JS::BigInt* ParseBigIntLiteral(
+ js::ExclusiveContext* cx, const mozilla::Range<const char16_t>& chars);
+
+extern JS::BigInt* ToBigInt(js::ExclusiveContext* cx, JS::Handle<JS::Value> v);
+
+} // namespace js
+
+#undef js_gc_MinCellSize
+#undef js_gc_Cell_ReservedBits
+
+#endif
diff --git a/js/src/vm/CommonPropertyNames.h b/js/src/vm/CommonPropertyNames.h
index d5e7a2d058..f05a4db9be 100644
--- a/js/src/vm/CommonPropertyNames.h
+++ b/js/src/vm/CommonPropertyNames.h
@@ -36,6 +36,8 @@
macro(AsyncWrapped, AsyncWrapped, "AsyncWrapped") \
macro(async, async, "async") \
macro(await, await, "await") \
+ macro(bigint64, bigint64, "bigint64") \
+ macro(biguint64, biguint64, "biguint64") \
macro(Bool8x16, Bool8x16, "Bool8x16") \
macro(Bool16x8, Bool16x8, "Bool16x8") \
macro(Bool32x4, Bool32x4, "Bool32x4") \
@@ -275,6 +277,7 @@
macro(objectArguments, objectArguments, "[object Arguments]") \
macro(objectArray, objectArray, "[object Array]") \
macro(objectBoolean, objectBoolean, "[object Boolean]") \
+ macro(objectBigInt, objectBigInt, "[object BigInt]") \
macro(objectDate, objectDate, "[object Date]") \
macro(objectError, objectError, "[object Error]") \
macro(objectFunction, objectFunction, "[object Function]") \
@@ -424,5 +427,6 @@
macro(boolean, boolean, "boolean") \
macro(null, null, "null") \
macro(symbol, symbol, "symbol") \
+ macro(bigint, bigint, "bigint") \
#endif /* vm_CommonPropertyNames_h */
diff --git a/js/src/vm/EqualityOperations.cpp b/js/src/vm/EqualityOperations.cpp
index 6f90450b49..91a9ca06c7 100644
--- a/js/src/vm/EqualityOperations.cpp
+++ b/js/src/vm/EqualityOperations.cpp
@@ -31,6 +31,10 @@ EqualGivenSameType(JSContext* cx, JS::HandleValue lval, JS::HandleValue rval, bo
*equal = (lval.toDouble() == rval.toDouble());
return true;
}
+ if (lval.isBigInt()) {
+ *equal = JS::BigInt::equal(lval.toBigInt(), rval.toBigInt());
+ return true;
+ }
if (lval.isGCThing()) { // objects or symbols
*equal = (lval.toGCThing() == rval.toGCThing());
return true;
@@ -134,6 +138,22 @@ js::LooselyEqual(JSContext* cx, JS::HandleValue lval, JS::HandleValue rval, bool
return js::LooselyEqual(cx, lvalue, rval, result);
}
+ if (lval.isBigInt()) {
+ RootedBigInt lbi(cx, lval.toBigInt());
+ bool tmpResult;
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, tmpResult, BigInt::looselyEqual(cx, lbi, rval));
+ *result = tmpResult;
+ return true;
+ }
+
+ if (rval.isBigInt()) {
+ RootedBigInt rbi(cx, rval.toBigInt());
+ bool tmpResult;
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, tmpResult, BigInt::looselyEqual(cx, rbi, lval));
+ *result = tmpResult;
+ return true;
+ }
+
// Step 12.
*result = false;
return true;
diff --git a/js/src/vm/GlobalObject.cpp b/js/src/vm/GlobalObject.cpp
index 542160ce56..b7d3344b3e 100644
--- a/js/src/vm/GlobalObject.cpp
+++ b/js/src/vm/GlobalObject.cpp
@@ -15,6 +15,7 @@
#include "jsweakmap.h"
#include "builtin/AtomicsObject.h"
+#include "builtin/BigInt.h"
#include "builtin/Eval.h"
#include "builtin/MapObject.h"
#include "builtin/ModuleObject.h"
diff --git a/js/src/vm/GlobalObject.h b/js/src/vm/GlobalObject.h
index 1e10fe5da3..c4d9cf7287 100644
--- a/js/src/vm/GlobalObject.h
+++ b/js/src/vm/GlobalObject.h
@@ -82,6 +82,8 @@ class GlobalObject : public NativeObject
FROM_BUFFER_INT16,
FROM_BUFFER_UINT32,
FROM_BUFFER_INT32,
+ FROM_BUFFER_UINT64,
+ FROM_BUFFER_INT64,
FROM_BUFFER_FLOAT32,
FROM_BUFFER_FLOAT64,
FROM_BUFFER_UINT8CLAMPED,
@@ -959,6 +961,20 @@ GlobalObject::setCreateArrayFromBuffer<int32_t>(Handle<JSFunction*> fun)
template<>
inline void
+GlobalObject::setCreateArrayFromBuffer<uint64_t>(Handle<JSFunction*> fun)
+{
+ setCreateArrayFromBufferHelper(FROM_BUFFER_UINT64, fun);
+}
+
+template<>
+inline void
+GlobalObject::setCreateArrayFromBuffer<int64_t>(Handle<JSFunction*> fun)
+{
+ setCreateArrayFromBufferHelper(FROM_BUFFER_INT64, fun);
+}
+
+template<>
+inline void
GlobalObject::setCreateArrayFromBuffer<float>(Handle<JSFunction*> fun)
{
setCreateArrayFromBufferHelper(FROM_BUFFER_FLOAT32, fun);
@@ -1022,6 +1038,20 @@ GlobalObject::createArrayFromBuffer<int32_t>() const
template<>
inline Value
+GlobalObject::createArrayFromBuffer<uint64_t>() const
+{
+ return createArrayFromBufferHelper(FROM_BUFFER_UINT64);
+}
+
+template<>
+inline Value
+GlobalObject::createArrayFromBuffer<int64_t>() const
+{
+ return createArrayFromBufferHelper(FROM_BUFFER_INT64);
+}
+
+template<>
+inline Value
GlobalObject::createArrayFromBuffer<float>() const
{
return createArrayFromBufferHelper(FROM_BUFFER_FLOAT32);
diff --git a/js/src/vm/Interpreter-inl.h b/js/src/vm/Interpreter-inl.h
index 79a4b90200..a48c753f1d 100644
--- a/js/src/vm/Interpreter-inl.h
+++ b/js/src/vm/Interpreter-inl.h
@@ -8,6 +8,8 @@
#include "vm/Interpreter.h"
+#include "mozilla/Maybe.h"
+
#include "jscompartment.h"
#include "jsnum.h"
#include "jsstr.h"
@@ -390,7 +392,7 @@ DefVarOperation(JSContext* cx, HandleObject varobj, HandlePropertyName dn, unsig
}
static MOZ_ALWAYS_INLINE bool
-NegOperation(JSContext* cx, HandleScript script, jsbytecode* pc, HandleValue val,
+NegOperation(JSContext* cx, HandleScript script, jsbytecode* pc, MutableHandleValue val,
MutableHandleValue res)
{
/*
@@ -401,13 +403,16 @@ NegOperation(JSContext* cx, HandleScript script, jsbytecode* pc, HandleValue val
int32_t i;
if (val.isInt32() && (i = val.toInt32()) != 0 && i != INT32_MIN) {
res.setInt32(-i);
- } else {
- double d;
- if (!ToNumber(cx, val, &d))
- return false;
- res.setNumber(-d);
+ return true;
}
+ if (!ToNumeric(cx, val))
+ return false;
+
+ if (val.isBigInt())
+ return BigInt::neg(cx, val, res);
+
+ res.setNumber(-val.toNumber());
return true;
}
@@ -657,120 +662,268 @@ ProcessCallSiteObjOperation(JSContext* cx, RootedObject& cso, RootedObject& raw,
return true;
}
-#define RELATIONAL_OP(OP) \
- JS_BEGIN_MACRO \
- /* Optimize for two int-tagged operands (typical loop control). */ \
- if (lhs.isInt32() && rhs.isInt32()) { \
- *res = lhs.toInt32() OP rhs.toInt32(); \
- } else { \
- if (!ToPrimitive(cx, JSTYPE_NUMBER, lhs)) \
- return false; \
- if (!ToPrimitive(cx, JSTYPE_NUMBER, rhs)) \
- return false; \
- if (lhs.isString() && rhs.isString()) { \
- JSString* l = lhs.toString(); \
- JSString* r = rhs.toString(); \
- int32_t result; \
- if (!CompareStrings(cx, l, r, &result)) \
- return false; \
- *res = result OP 0; \
- } else { \
- double l, r; \
- if (!ToNumber(cx, lhs, &l) || !ToNumber(cx, rhs, &r)) \
- return false; \
- *res = (l OP r); \
- } \
- } \
- return true; \
- JS_END_MACRO
+// BigInt proposal 3.2.4 Abstract Relational Comparison
+// Returns Nothing when at least one operand is a NaN, or when
+// ToNumeric or StringToBigInt can't interpret a string as a numeric
+// value. (These cases correspond to a NaN result in the spec.)
+// Otherwise, return a boolean to indicate whether lhs is less than
+// rhs. The operands must be primitives; the caller is responsible for
+// evaluating them in the correct order.
+static MOZ_ALWAYS_INLINE bool
+LessThanImpl(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs,
+ mozilla::Maybe<bool>& res)
+{
+ // Steps 1 and 2 are performed by the caller.
+
+ // Step 3.
+ if (lhs.isString() && rhs.isString()) {
+ JSString* l = lhs.toString();
+ JSString* r = rhs.toString();
+ int32_t result;
+ if (!CompareStrings(cx, l, r, &result)) {
+ return false;
+ }
+ res = mozilla::Some(result < 0);
+ return true;
+ }
+
+ // Step 4a.
+ if (lhs.isBigInt() && rhs.isString()) {
+ return BigInt::lessThan(cx, lhs, rhs, res);
+ }
+ // Step 4b.
+ if (lhs.isString() && rhs.isBigInt()) {
+ return BigInt::lessThan(cx, lhs, rhs, res);
+ }
+
+ // Steps 4c and 4d.
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
+ return false;
+ }
+
+ // Steps 4e-j.
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::lessThan(cx, lhs, rhs, res);
+ }
+
+ // Step 4e for Number operands.
+ MOZ_ASSERT(lhs.isNumber() && rhs.isNumber());
+ double lhsNum = lhs.toNumber();
+ double rhsNum = rhs.toNumber();
+
+ if (mozilla::IsNaN(lhsNum) || mozilla::IsNaN(rhsNum)) {
+ res = mozilla::Maybe<bool>(mozilla::Nothing());
+ return true;
+ }
+
+ res = mozilla::Some(lhsNum < rhsNum);
+ return true;
+}
static MOZ_ALWAYS_INLINE bool
-LessThanOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res) {
- RELATIONAL_OP(<);
+LessThanOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res)
+{
+ if (lhs.isInt32() && rhs.isInt32()) {
+ *res = lhs.toInt32() < rhs.toInt32();
+ return true;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, lhs)) {
+ return false;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, rhs)) {
+ return false;
+ }
+
+ mozilla::Maybe<bool> tmpResult;
+ if (!LessThanImpl(cx, lhs, rhs, tmpResult)) {
+ return false;
+ }
+ *res = tmpResult.valueOr(false);
+ return true;
}
static MOZ_ALWAYS_INLINE bool
-LessThanOrEqualOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res) {
- RELATIONAL_OP(<=);
+LessThanOrEqualOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res)
+{
+ if (lhs.isInt32() && rhs.isInt32()) {
+ *res = lhs.toInt32() <= rhs.toInt32();
+ return true;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, lhs)) {
+ return false;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, rhs)) {
+ return false;
+ }
+
+ mozilla::Maybe<bool> tmpResult;
+ if (!LessThanImpl(cx, rhs, lhs, tmpResult)) {
+ return false;
+ }
+ *res = !tmpResult.valueOr(true);
+ return true;
}
static MOZ_ALWAYS_INLINE bool
-GreaterThanOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res) {
- RELATIONAL_OP(>);
+GreaterThanOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res)
+{
+ if (lhs.isInt32() && rhs.isInt32()) {
+ *res = lhs.toInt32() > rhs.toInt32();
+ return true;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, lhs)) {
+ return false;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, rhs)) {
+ return false;
+ }
+
+ mozilla::Maybe<bool> tmpResult;
+ if (!LessThanImpl(cx, rhs, lhs, tmpResult)) {
+ return false;
+ }
+ *res = tmpResult.valueOr(false);
+ return true;
}
static MOZ_ALWAYS_INLINE bool
-GreaterThanOrEqualOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res) {
- RELATIONAL_OP(>=);
+GreaterThanOrEqualOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, bool* res)
+{
+ if (lhs.isInt32() && rhs.isInt32()) {
+ *res = lhs.toInt32() >= rhs.toInt32();
+ return true;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, lhs)) {
+ return false;
+ }
+
+ if (!ToPrimitive(cx, JSTYPE_NUMBER, rhs)) {
+ return false;
+ }
+
+ mozilla::Maybe<bool> tmpResult;
+ if (!LessThanImpl(cx, lhs, rhs, tmpResult)) {
+ return false;
+ }
+ *res = !tmpResult.valueOr(true);
+ return true;
}
static MOZ_ALWAYS_INLINE bool
-BitNot(JSContext* cx, HandleValue in, int* out)
+BitNot(JSContext* cx, MutableHandleValue in, MutableHandleValue out)
{
- int i;
- if (!ToInt32(cx, in, &i))
+ if (!ToInt32OrBigInt(cx, in)) {
return false;
- *out = ~i;
+ }
+
+ if (in.isBigInt()) {
+ return BigInt::bitNot(cx, in, out);
+ }
+
+ out.setInt32(~in.toInt32());
return true;
}
static MOZ_ALWAYS_INLINE bool
-BitXor(JSContext* cx, HandleValue lhs, HandleValue rhs, int* out)
+BitXor(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
- int left, right;
- if (!ToInt32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
return false;
- *out = left ^ right;
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::bitXor(cx, lhs, rhs, out);
+ }
+
+ out.setInt32(lhs.toInt32() ^ rhs.toInt32());
return true;
}
static MOZ_ALWAYS_INLINE bool
-BitOr(JSContext* cx, HandleValue lhs, HandleValue rhs, int* out)
+BitOr(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
- int left, right;
- if (!ToInt32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
return false;
- *out = left | right;
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::bitOr(cx, lhs, rhs, out);
+ }
+
+ out.setInt32(lhs.toInt32() | rhs.toInt32());
return true;
}
static MOZ_ALWAYS_INLINE bool
-BitAnd(JSContext* cx, HandleValue lhs, HandleValue rhs, int* out)
+BitAnd(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
- int left, right;
- if (!ToInt32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
return false;
- *out = left & right;
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::bitAnd(cx, lhs, rhs, out);
+ }
+
+ out.setInt32(lhs.toInt32() & rhs.toInt32());
return true;
}
static MOZ_ALWAYS_INLINE bool
-BitLsh(JSContext* cx, HandleValue lhs, HandleValue rhs, int* out)
+BitLsh(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
- int32_t left, right;
- if (!ToInt32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
return false;
- *out = uint32_t(left) << (right & 31);
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::lsh(cx, lhs, rhs, out);
+ }
+
+ out.setInt32(lhs.toInt32() << (rhs.toInt32() & 31));
return true;
}
static MOZ_ALWAYS_INLINE bool
-BitRsh(JSContext* cx, HandleValue lhs, HandleValue rhs, int* out)
+BitRsh(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
- int32_t left, right;
- if (!ToInt32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ if (!ToInt32OrBigInt(cx, lhs) || !ToInt32OrBigInt(cx, rhs)) {
return false;
- *out = left >> (right & 31);
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ return BigInt::rsh(cx, lhs, rhs, out);
+ }
+
+ out.setInt32(lhs.toInt32() >> (rhs.toInt32() & 31));
return true;
}
static MOZ_ALWAYS_INLINE bool
-UrshOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue out)
+UrshOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue out)
{
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs)) {
+ return false;
+ }
+
+ if (lhs.isBigInt() || rhs.isBigInt()) {
+ JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
+ JSMSG_BIGINT_TO_NUMBER);
+ return false;
+ }
+
uint32_t left;
- int32_t right;
- if (!ToUint32(cx, lhs, &left) || !ToInt32(cx, rhs, &right))
+ int32_t right;
+ if (!ToUint32(cx, lhs, &left) || !ToInt32(cx, rhs, &right)) {
return false;
+ }
left >>= right & 31;
out.setNumber(uint32_t(left));
return true;
diff --git a/js/src/vm/Interpreter.cpp b/js/src/vm/Interpreter.cpp
index 3515a9336b..a03fa847f7 100644
--- a/js/src/vm/Interpreter.cpp
+++ b/js/src/vm/Interpreter.cpp
@@ -40,6 +40,7 @@
#include "jit/IonAnalysis.h"
#include "vm/AsyncFunction.h"
#include "vm/AsyncIteration.h"
+#include "vm/BigIntType.h"
#include "vm/Debugger.h"
#include "vm/EqualityOperations.h" // js::StrictlyEqual
#include "vm/GeneratorObject.h"
@@ -812,6 +813,8 @@ js::TypeOfValue(const Value& v)
return TypeOfObject(&v.toObject());
if (v.isBoolean())
return JSTYPE_BOOLEAN;
+ if (v.isBigInt())
+ return JSTYPE_BIGINT;
MOZ_ASSERT(v.isSymbol());
return JSTYPE_SYMBOL;
}
@@ -1325,50 +1328,63 @@ AddOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, Muta
return false;
}
res.setString(str);
- } else {
- double l, r;
- if (!ToNumber(cx, lhs, &l) || !ToNumber(cx, rhs, &r))
- return false;
- res.setNumber(l + r);
+ return true;
}
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
+ return false;
+
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::add(cx, lhs, rhs, res);
+
+ res.setNumber(lhs.toNumber() + rhs.toNumber());
return true;
}
static MOZ_ALWAYS_INLINE bool
-SubOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue res)
+SubOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
{
- double d1, d2;
- if (!ToNumber(cx, lhs, &d1) || !ToNumber(cx, rhs, &d2))
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
return false;
- res.setNumber(d1 - d2);
+
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::sub(cx, lhs, rhs, res);
+
+ res.setNumber(lhs.toNumber() - rhs.toNumber());
return true;
}
static MOZ_ALWAYS_INLINE bool
-MulOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue res)
+MulOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
{
- double d1, d2;
- if (!ToNumber(cx, lhs, &d1) || !ToNumber(cx, rhs, &d2))
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
return false;
- res.setNumber(d1 * d2);
+
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::mul(cx, lhs, rhs, res);
+
+ res.setNumber(lhs.toNumber() * rhs.toNumber());
return true;
}
static MOZ_ALWAYS_INLINE bool
-DivOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue res)
+DivOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
{
- double d1, d2;
- if (!ToNumber(cx, lhs, &d1) || !ToNumber(cx, rhs, &d2))
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
return false;
- res.setNumber(NumberDiv(d1, d2));
+
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::div(cx, lhs, rhs, res);
+
+ res.setNumber(NumberDiv(lhs.toNumber(), rhs.toNumber()));
return true;
}
static MOZ_ALWAYS_INLINE bool
-ModOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue res)
+ModOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
{
int32_t l, r;
+
if (lhs.isInt32() && rhs.isInt32() &&
(l = lhs.toInt32()) >= 0 && (r = rhs.toInt32()) > 0) {
int32_t mod = l % r;
@@ -1376,11 +1392,26 @@ ModOperation(JSContext* cx, HandleValue lhs, HandleValue rhs, MutableHandleValue
return true;
}
- double d1, d2;
- if (!ToNumber(cx, lhs, &d1) || !ToNumber(cx, rhs, &d2))
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
return false;
- res.setNumber(NumberMod(d1, d2));
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::mod(cx, lhs, rhs, res);
+
+ res.setNumber(NumberMod(lhs.toNumber(), rhs.toNumber()));
+ return true;
+}
+
+static MOZ_ALWAYS_INLINE bool
+PowOperation(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
+{
+ if (!ToNumeric(cx, lhs) || !ToNumeric(cx, rhs))
+ return false;
+
+ if (lhs.isBigInt() || rhs.isBigInt())
+ return BigInt::pow(cx, lhs, rhs, res);
+
+ res.setNumber(ecmaPow(lhs.toNumber(), rhs.toNumber()));
return true;
}
@@ -2147,32 +2178,42 @@ CASE(JSOP_BINDVAR)
}
END_CASE(JSOP_BINDVAR)
-#define BITWISE_OP(OP) \
- JS_BEGIN_MACRO \
- int32_t i, j; \
- if (!ToInt32(cx, REGS.stackHandleAt(-2), &i)) \
- goto error; \
- if (!ToInt32(cx, REGS.stackHandleAt(-1), &j)) \
- goto error; \
- i = i OP j; \
- REGS.sp--; \
- REGS.sp[-1].setInt32(i); \
- JS_END_MACRO
-
CASE(JSOP_BITOR)
- BITWISE_OP(|);
+{
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-2);
+ if (!BitOr(cx, lhs, rhs, res)) {
+ goto error;
+ }
+ REGS.sp--;
+}
END_CASE(JSOP_BITOR)
CASE(JSOP_BITXOR)
- BITWISE_OP(^);
+{
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-2);
+ if (!BitXor(cx, lhs, rhs, res)) {
+ goto error;
+ }
+ REGS.sp--;
+}
END_CASE(JSOP_BITXOR)
CASE(JSOP_BITAND)
- BITWISE_OP(&);
+{
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-2);
+ if (!BitAnd(cx, lhs, rhs, res)) {
+ goto error;
+ }
+ REGS.sp--;
+}
END_CASE(JSOP_BITAND)
-#undef BITWISE_OP
-
CASE(JSOP_EQ)
if (!LooseEqualityOp<true>(cx, REGS))
goto error;
@@ -2228,8 +2269,9 @@ CASE(JSOP_LT)
bool cond;
MutableHandleValue lval = REGS.stackHandleAt(-2);
MutableHandleValue rval = REGS.stackHandleAt(-1);
- if (!LessThanOperation(cx, lval, rval, &cond))
+ if (!LessThanOperation(cx, lval, rval, &cond)) {
goto error;
+ }
TRY_BRANCH_AFTER_COND(cond, 2);
REGS.sp[-2].setBoolean(cond);
REGS.sp--;
@@ -2241,8 +2283,9 @@ CASE(JSOP_LE)
bool cond;
MutableHandleValue lval = REGS.stackHandleAt(-2);
MutableHandleValue rval = REGS.stackHandleAt(-1);
- if (!LessThanOrEqualOperation(cx, lval, rval, &cond))
+ if (!LessThanOrEqualOperation(cx, lval, rval, &cond)) {
goto error;
+ }
TRY_BRANCH_AFTER_COND(cond, 2);
REGS.sp[-2].setBoolean(cond);
REGS.sp--;
@@ -2254,8 +2297,9 @@ CASE(JSOP_GT)
bool cond;
MutableHandleValue lval = REGS.stackHandleAt(-2);
MutableHandleValue rval = REGS.stackHandleAt(-1);
- if (!GreaterThanOperation(cx, lval, rval, &cond))
+ if (!GreaterThanOperation(cx, lval, rval, &cond)) {
goto error;
+ }
TRY_BRANCH_AFTER_COND(cond, 2);
REGS.sp[-2].setBoolean(cond);
REGS.sp--;
@@ -2267,43 +2311,47 @@ CASE(JSOP_GE)
bool cond;
MutableHandleValue lval = REGS.stackHandleAt(-2);
MutableHandleValue rval = REGS.stackHandleAt(-1);
- if (!GreaterThanOrEqualOperation(cx, lval, rval, &cond))
+ if (!GreaterThanOrEqualOperation(cx, lval, rval, &cond)) {
goto error;
+ }
TRY_BRANCH_AFTER_COND(cond, 2);
REGS.sp[-2].setBoolean(cond);
REGS.sp--;
}
END_CASE(JSOP_GE)
-#define SIGNED_SHIFT_OP(OP) \
- JS_BEGIN_MACRO \
- int32_t i, j; \
- if (!ToInt32(cx, REGS.stackHandleAt(-2), &i)) \
- goto error; \
- if (!ToInt32(cx, REGS.stackHandleAt(-1), &j)) \
- goto error; \
- i = i OP (j & 31); \
- REGS.sp--; \
- REGS.sp[-1].setInt32(i); \
- JS_END_MACRO
-
CASE(JSOP_LSH)
- SIGNED_SHIFT_OP(<<);
+{
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-2);
+ if (!BitLsh(cx, lhs, rhs, res)) {
+ goto error;
+ }
+ REGS.sp--;
+}
END_CASE(JSOP_LSH)
CASE(JSOP_RSH)
- SIGNED_SHIFT_OP(>>);
+{
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-2);
+ if (!BitRsh(cx, lhs, rhs, res)) {
+ goto error;
+ }
+ REGS.sp--;
+}
END_CASE(JSOP_RSH)
-#undef SIGNED_SHIFT_OP
-
CASE(JSOP_URSH)
{
- HandleValue lval = REGS.stackHandleAt(-2);
- HandleValue rval = REGS.stackHandleAt(-1);
+ MutableHandleValue lhs = REGS.stackHandleAt(-2);
+ MutableHandleValue rhs = REGS.stackHandleAt(-1);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!UrshOperation(cx, lval, rval, res))
+ if (!UrshOperation(cx, lhs, rhs, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_URSH)
@@ -2313,8 +2361,9 @@ CASE(JSOP_ADD)
MutableHandleValue lval = REGS.stackHandleAt(-2);
MutableHandleValue rval = REGS.stackHandleAt(-1);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!AddOperation(cx, lval, rval, res))
+ if (!AddOperation(cx, lval, rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_ADD)
@@ -2324,8 +2373,9 @@ CASE(JSOP_SUB)
ReservedRooted<Value> lval(&rootValue0, REGS.sp[-2]);
ReservedRooted<Value> rval(&rootValue1, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!SubOperation(cx, lval, rval, res))
+ if (!SubOperation(cx, &lval, &rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_SUB)
@@ -2335,8 +2385,9 @@ CASE(JSOP_MUL)
ReservedRooted<Value> lval(&rootValue0, REGS.sp[-2]);
ReservedRooted<Value> rval(&rootValue1, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!MulOperation(cx, lval, rval, res))
+ if (!MulOperation(cx, &lval, &rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_MUL)
@@ -2346,8 +2397,9 @@ CASE(JSOP_DIV)
ReservedRooted<Value> lval(&rootValue0, REGS.sp[-2]);
ReservedRooted<Value> rval(&rootValue1, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!DivOperation(cx, lval, rval, res))
+ if (!DivOperation(cx, &lval, &rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_DIV)
@@ -2357,8 +2409,9 @@ CASE(JSOP_MOD)
ReservedRooted<Value> lval(&rootValue0, REGS.sp[-2]);
ReservedRooted<Value> rval(&rootValue1, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!ModOperation(cx, lval, rval, res))
+ if (!ModOperation(cx, &lval, &rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_MOD)
@@ -2368,8 +2421,9 @@ CASE(JSOP_POW)
ReservedRooted<Value> lval(&rootValue0, REGS.sp[-2]);
ReservedRooted<Value> rval(&rootValue1, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-2);
- if (!math_pow_handle(cx, lval, rval, res))
+ if (!PowOperation(cx, &lval, &rval, res)) {
goto error;
+ }
REGS.sp--;
}
END_CASE(JSOP_POW)
@@ -2384,11 +2438,11 @@ END_CASE(JSOP_NOT)
CASE(JSOP_BITNOT)
{
- int32_t i;
- HandleValue value = REGS.stackHandleAt(-1);
- if (!BitNot(cx, value, &i))
+ MutableHandleValue value = REGS.stackHandleAt(-1);
+ MutableHandleValue res = REGS.stackHandleAt(-1);
+ if (!BitNot(cx, value, res)) {
goto error;
- REGS.sp[-1].setInt32(i);
+ }
}
END_CASE(JSOP_BITNOT)
@@ -2396,7 +2450,7 @@ CASE(JSOP_NEG)
{
ReservedRooted<Value> val(&rootValue0, REGS.sp[-1]);
MutableHandleValue res = REGS.stackHandleAt(-1);
- if (!NegOperation(cx, script, REGS.pc, val, res))
+ if (!NegOperation(cx, script, REGS.pc, &val, res))
goto error;
}
END_CASE(JSOP_NEG)
@@ -4122,6 +4176,13 @@ CASE(JSOP_IS_CONSTRUCTING)
PUSH_MAGIC(JS_IS_CONSTRUCTING);
END_CASE(JSOP_IS_CONSTRUCTING)
+CASE(JSOP_BIGINT)
+{
+ PUSH_COPY(script->getConst(GET_UINT32_INDEX(REGS.pc)));
+ MOZ_ASSERT(REGS.sp[-1].isBigInt());
+}
+END_CASE(JSOP_BIGINT)
+
DEFAULT()
{
char numBuf[12];
@@ -4222,7 +4283,8 @@ js::GetProperty(JSContext* cx, HandleValue v, HandlePropertyName name, MutableHa
// Optimize common cases like (2).toString() or "foo".valueOf() to not
// create a wrapper object.
- if (v.isPrimitive() && !v.isNullOrUndefined()) {
+ if (v.isPrimitive() && !v.isNullOrUndefined() && !v.isBigInt())
+ {
NativeObject* proto;
if (v.isNumber()) {
proto = GlobalObject::getOrCreateNumberPrototype(cx, cx->global());
@@ -4577,6 +4639,12 @@ js::ModValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, Mut
}
bool
+js::PowValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
+{
+ return PowOperation(cx, lhs, rhs, res);
+}
+
+bool
js::UrshValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res)
{
return UrshOperation(cx, lhs, rhs, res);
diff --git a/js/src/vm/Interpreter.h b/js/src/vm/Interpreter.h
index 1927e8cc7f..3c7b80a14c 100644
--- a/js/src/vm/Interpreter.h
+++ b/js/src/vm/Interpreter.h
@@ -457,6 +457,9 @@ bool
ModValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res);
bool
+PowValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res);
+
+bool
UrshValues(JSContext* cx, MutableHandleValue lhs, MutableHandleValue rhs, MutableHandleValue res);
bool
diff --git a/js/src/vm/MemoryMetrics.cpp b/js/src/vm/MemoryMetrics.cpp
index 9b6d3dda72..2a28cf23c0 100644
--- a/js/src/vm/MemoryMetrics.cpp
+++ b/js/src/vm/MemoryMetrics.cpp
@@ -17,6 +17,7 @@
#include "jit/BaselineJIT.h"
#include "jit/Ion.h"
#include "vm/ArrayObject.h"
+#include "vm/BigIntType.h"
#include "vm/Runtime.h"
#include "vm/Shape.h"
#include "vm/String.h"
@@ -546,6 +547,13 @@ StatsCellCallback(JSRuntime* rt, void* data, void* thing, JS::TraceKind traceKin
zStats->symbolsGCHeap += thingSize;
break;
+ case JS::TraceKind::BigInt: {
+ JS::BigInt* bi = static_cast<BigInt*>(thing);
+ zStats->bigIntsGCHeap += thingSize;
+ zStats->bigIntsMallocHeap += bi->sizeOfExcludingThis(rtStats->mallocSizeOf_);
+ break;
+ }
+
case JS::TraceKind::BaseShape: {
JS::ShapeInfo info; // This zeroes all the sizes.
info.shapesGCHeapBase += thingSize;
diff --git a/js/src/vm/NativeObject-inl.h b/js/src/vm/NativeObject-inl.h
index 69976bc462..1fa5cbd10e 100644
--- a/js/src/vm/NativeObject-inl.h
+++ b/js/src/vm/NativeObject-inl.h
@@ -239,12 +239,15 @@ NativeObject::ensureDenseElements(ExclusiveContext* cx, uint32_t index, uint32_t
return DenseElementResult::Success;
}
-inline Value
-NativeObject::getDenseOrTypedArrayElement(uint32_t idx)
+template <AllowGC allowGC>
+inline bool
+NativeObject::getDenseOrTypedArrayElement(ExclusiveContext* cx, uint32_t idx,
+ typename MaybeRooted<Value, allowGC>::MutableHandleType val)
{
if (is<TypedArrayObject>())
- return as<TypedArrayObject>().getElement(idx);
- return getDenseElement(idx);
+ return as<TypedArrayObject>().getElement<allowGC>(cx, idx, val);
+ val.set(getDenseElement(idx));
+ return true;
}
/* static */ inline NativeObject*
diff --git a/js/src/vm/NativeObject.cpp b/js/src/vm/NativeObject.cpp
index a6bb9826ee..cde86fb829 100644
--- a/js/src/vm/NativeObject.cpp
+++ b/js/src/vm/NativeObject.cpp
@@ -20,6 +20,7 @@
#include "vm/ArrayObject-inl.h"
#include "vm/EnvironmentObject-inl.h"
#include "vm/Shape-inl.h"
+#include "vm/TypedArrayObject.h"
using namespace js;
@@ -1283,8 +1284,7 @@ GetExistingPropertyValue(ExclusiveContext* cx, HandleNativeObject obj, HandleId
Handle<PropertyResult> prop, MutableHandleValue vp)
{
if (prop.isDenseOrTypedArrayElement()) {
- vp.set(obj->getDenseOrTypedArrayElement(JSID_TO_INT(id)));
- return true;
+ return obj->getDenseOrTypedArrayElement<CanGC>(cx, JSID_TO_INT(id), vp);
}
if (!cx->shouldBeJSContext())
return false;
@@ -1814,7 +1814,9 @@ js::NativeGetOwnPropertyDescriptor(JSContext* cx, HandleNativeObject obj, Handle
desc.attributesRef() &= ~JSPROP_SHARED;
if (prop.isDenseOrTypedArrayElement()) {
- desc.value().set(obj->getDenseOrTypedArrayElement(JSID_TO_INT(id)));
+ if (!obj->getDenseOrTypedArrayElement<CanGC>(cx, JSID_TO_INT(id), desc.value())) {
+ return false;
+ }
} else {
RootedShape shape(cx, prop.shape());
if (!NativeGetExistingProperty(cx, obj, obj, shape, desc.value()))
@@ -2110,8 +2112,7 @@ NativeGetPropertyInline(JSContext* cx,
// Steps 5-8. Special case for dense elements because
// GetExistingProperty doesn't support those.
if (prop.isDenseOrTypedArrayElement()) {
- vp.set(pobj->getDenseOrTypedArrayElement(JSID_TO_INT(id)));
- return true;
+ return pobj->template getDenseOrTypedArrayElement<allowGC>(cx, JSID_TO_INT(id), vp);
}
typename MaybeRooted<Shape*, allowGC>::RootType shape(cx, prop.shape());
@@ -2365,38 +2366,6 @@ SetNonexistentProperty(JSContext* cx, HandleId id, HandleValue v, HandleValue re
}
/*
- * Set an existing own property obj[index] that's a dense element or typed
- * array element.
- */
-static bool
-SetDenseOrTypedArrayElement(JSContext* cx, HandleNativeObject obj, uint32_t index, HandleValue v,
- ObjectOpResult& result)
-{
- if (obj->is<TypedArrayObject>()) {
- double d;
- if (!ToNumber(cx, v, &d))
- return false;
-
- // Silently do nothing for out-of-bounds sets, for consistency with
- // current behavior. (ES6 currently says to throw for this in
- // strict mode code, so we may eventually need to change.)
- uint32_t len = obj->as<TypedArrayObject>().length();
- if (index < len)
- TypedArrayObject::setElement(obj->as<TypedArrayObject>(), index, d);
- return result.succeed();
- }
-
- if (WouldDefinePastNonwritableLength(obj, index))
- return result.fail(JSMSG_CANT_DEFINE_PAST_ARRAY_LENGTH);
-
- if (!obj->maybeCopyElementsForWrite(cx))
- return false;
-
- obj->setDenseElementWithType(cx, index, v);
- return result.succeed();
-}
-
-/*
* Finish the assignment `receiver[id] = v` when an existing property (shape)
* has been found on a native object (pobj). This implements ES6 draft rev 32
* (2015 Feb 2) 9.1.9 steps 5 and 6.
@@ -2416,8 +2385,23 @@ SetExistingProperty(JSContext* cx, HandleNativeObject obj, HandleId id, HandleVa
return result.fail(JSMSG_READ_ONLY);
// Pure optimization for the common case:
- if (receiver.isObject() && pobj == &receiver.toObject())
- return SetDenseOrTypedArrayElement(cx, pobj, JSID_TO_INT(id), v, result);
+ if (receiver.isObject() && pobj == &receiver.toObject()) {
+ uint32_t index = JSID_TO_INT(id);
+
+ if (pobj->is<TypedArrayObject>()) {
+ Rooted<TypedArrayObject*> tobj(cx, &pobj->as<TypedArrayObject>());
+ return SetTypedArrayElement(cx, tobj, index, v, result);
+ }
+
+ if (WouldDefinePastNonwritableLength(pobj, index))
+ return result.fail(JSMSG_CANT_DEFINE_PAST_ARRAY_LENGTH);
+
+ if (!pobj->maybeCopyElementsForWrite(cx))
+ return false;
+
+ pobj->setDenseElementWithType(cx, index, v);
+ return result.succeed();
+ }
// Steps 5.b-f.
return SetPropertyByDefining(cx, id, v, receiver, result);
diff --git a/js/src/vm/NativeObject.h b/js/src/vm/NativeObject.h
index c5865caa03..86977d109f 100644
--- a/js/src/vm/NativeObject.h
+++ b/js/src/vm/NativeObject.h
@@ -18,6 +18,7 @@
#include "gc/Barrier.h"
#include "gc/Heap.h"
#include "gc/Marking.h"
+#include "js/RootingAPI.h"
#include "js/Value.h"
#include "vm/Shape.h"
#include "vm/ShapedObject.h"
@@ -1088,7 +1089,9 @@ class NativeObject : public ShapedObject
static inline void removeDenseElementForSparseIndex(ExclusiveContext* cx,
HandleNativeObject obj, uint32_t index);
- inline Value getDenseOrTypedArrayElement(uint32_t idx);
+ template <AllowGC allowGC> inline bool
+ getDenseOrTypedArrayElement(ExclusiveContext* cx, uint32_t idx,
+ typename MaybeRooted<Value, allowGC>::MutableHandleType val);
void copyDenseElements(uint32_t dstStart, const Value* src, uint32_t count) {
MOZ_ASSERT(dstStart + count <= getDenseCapacity());
diff --git a/js/src/vm/ObjectGroup.cpp b/js/src/vm/ObjectGroup.cpp
index 741531f015..408e346608 100644
--- a/js/src/vm/ObjectGroup.cpp
+++ b/js/src/vm/ObjectGroup.cpp
@@ -686,6 +686,8 @@ GetClassForProtoKey(JSProtoKey key)
case JSProto_Float32Array:
case JSProto_Float64Array:
case JSProto_Uint8ClampedArray:
+ case JSProto_BigInt64Array:
+ case JSProto_BigUint64Array:
return &TypedArrayObject::classes[key - JSProto_Int8Array];
case JSProto_ArrayBuffer:
diff --git a/js/src/vm/Opcodes.h b/js/src/vm/Opcodes.h
index f6856f2290..ff707aac08 100644
--- a/js/src/vm/Opcodes.h
+++ b/js/src/vm/Opcodes.h
@@ -2357,14 +2357,20 @@
* Operands:
* Stack: arg => rval
*/ \
- macro(JSOP_DYNAMIC_IMPORT, 234, "call-import", NULL, 1, 1, 1, JOF_BYTE)
-
+ macro(JSOP_DYNAMIC_IMPORT, 234, "call-import", NULL, 1, 1, 1, JOF_BYTE) \
+ /*
+ * Pushes a BigInt constant onto the stack.
+ * Category: Literals
+ * Type: Constants
+ * Operands: uint32_t constIndex
+ * Stack: => val
+ */ \
+ macro(JSOP_BIGINT, 235, "bigint", NULL, 5, 0, 1, JOF_BIGINT)
/*
* In certain circumstances it may be useful to "pad out" the opcode space to
* a power of two. Use this macro to do so.
*/
#define FOR_EACH_TRAILING_UNUSED_OPCODE(macro) \
- macro(235) \
macro(236) \
macro(237) \
macro(238) \
diff --git a/js/src/vm/RegExpObject.h b/js/src/vm/RegExpObject.h
index 14ec8509ee..5247731112 100644
--- a/js/src/vm/RegExpObject.h
+++ b/js/src/vm/RegExpObject.h
@@ -259,6 +259,9 @@ class RegExpShared : public gc::TenuredCell
static bool dumpBytecode(JSContext* cx, MutableHandleRegExpShared res, bool match_only,
HandleLinearString input);
#endif
+
+ public:
+ static const JS::TraceKind TraceKind = JS::TraceKind::RegExpShared;
};
class RegExpCompartment
diff --git a/js/src/vm/Runtime.cpp b/js/src/vm/Runtime.cpp
index ceb7a498b0..053b7c44b0 100644
--- a/js/src/vm/Runtime.cpp
+++ b/js/src/vm/Runtime.cpp
@@ -877,6 +877,9 @@ js::CurrentThreadCanAccessRuntime(const JSRuntime* rt)
bool
js::CurrentThreadCanAccessZone(Zone* zone)
{
+ if (!zone)
+ return false;
+
if (CurrentThreadCanAccessRuntime(zone->runtime_))
return true;
diff --git a/js/src/vm/SelfHosting.cpp b/js/src/vm/SelfHosting.cpp
index de497d02e1..2a60a1885c 100644
--- a/js/src/vm/SelfHosting.cpp
+++ b/js/src/vm/SelfHosting.cpp
@@ -22,6 +22,7 @@
#include "jswrapper.h"
#include "selfhosted.out.h"
+#include "builtin/BigInt.h"
#include "builtin/intl/Collator.h"
#include "builtin/intl/DateTimeFormat.h"
#include "builtin/intl/IntlObject.h"
@@ -44,6 +45,7 @@
#include "jit/InlinableNatives.h"
#include "js/CharacterEncoding.h"
#include "js/Date.h"
+#include "vm/BigIntType.h"
#include "vm/Compression.h"
#include "vm/GeneratorObject.h"
#include "vm/Interpreter.h"
@@ -1182,6 +1184,18 @@ intrinsic_IsFloat32TypedArray(JSContext* cx, unsigned argc, Value* vp)
}
static bool
+intrinsic_IsBigInt64TypedArray(JSContext* cx, unsigned argc, Value* vp)
+{
+ return intrinsic_IsSpecificTypedArray(cx, argc, vp, Scalar::BigInt64);
+}
+
+static bool
+intrinsic_IsBigUint64TypedArray(JSContext* cx, unsigned argc, Value* vp)
+{
+ return intrinsic_IsSpecificTypedArray(cx, argc, vp, Scalar::BigUint64);
+}
+
+static bool
intrinsic_TypedArrayBuffer(JSContext* cx, unsigned argc, Value* vp)
{
CallArgs args = CallArgsFromVp(argc, vp);
@@ -1521,6 +1535,14 @@ struct DisjointElements
CopyValues(dest, src.cast<double*>(), count);
return;
+ case Scalar::BigInt64:
+ CopyValues(dest, src.cast<int64_t*>(), count);
+ return;
+
+ case Scalar::BigUint64:
+ CopyValues(dest, src.cast<uint64_t*>(), count);
+ return;
+
case Scalar::Uint8Clamped:
CopyValues(dest, src.cast<uint8_clamped*>(), count);
return;
@@ -1579,6 +1601,16 @@ CopyToDisjointArray(TypedArrayObject* target, uint32_t targetOffset, SharedMem<v
break;
}
+ case Scalar::BigInt64: {
+ DisjointElements::copy(dest.cast<int64_t*>(), src, srcType, count);
+ break;
+ }
+
+ case Scalar::BigUint64: {
+ DisjointElements::copy(dest.cast<uint64_t*>(), src, srcType, count);
+ break;
+ }
+
case Scalar::Uint8Clamped: {
DisjointElements::copy(dest.cast<uint8_clamped*>(), src, srcType, count);
break;
@@ -2164,6 +2196,29 @@ intrinsic_PromiseResolve(JSContext* cx, unsigned argc, Value* vp)
return true;
}
+static bool intrinsic_ToBigInt(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ MOZ_ASSERT(args.length() == 1);
+ BigInt* res = ToBigInt(cx, args[0]);
+ if (!res) {
+ return false;
+ }
+ args.rval().setBigInt(res);
+ return true;
+}
+
+static bool intrinsic_ToNumeric(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ MOZ_ASSERT(args.length() == 1);
+ if (!ToNumeric(cx, args[0])) {
+ return false;
+ }
+ args.rval().set(args[0]);
+ return true;
+}
+
// The self-hosting global isn't initialized with the normal set of builtins.
// Instead, individual C++-implemented functions that're required by
// self-hosted code are defined as global functions. Accessing these
@@ -2187,6 +2242,8 @@ static const JSFunctionSpec intrinsic_functions[] = {
JS_FN("std_Array_reverse", array_reverse, 0,0),
JS_FNINFO("std_Array_splice", array_splice, &array_splice_info, 2,0),
+ JS_FN("std_BigInt_valueOf", BigIntObject::valueOf, 0,0),
+
JS_FN("std_Date_now", date_now, 0,0),
JS_FN("std_Date_valueOf", date_valueOf, 0,0),
@@ -2399,6 +2456,8 @@ static const JSFunctionSpec intrinsic_functions[] = {
JS_FN("IsUint32TypedArray", intrinsic_IsUint32TypedArray, 1,0),
JS_FN("IsInt32TypedArray", intrinsic_IsInt32TypedArray, 1,0),
JS_FN("IsFloat32TypedArray", intrinsic_IsFloat32TypedArray, 1,0),
+ JS_FN("IsBigInt64TypedArray", intrinsic_IsBigInt64TypedArray, 1,0),
+ JS_FN("IsBigUint64TypedArray", intrinsic_IsBigUint64TypedArray, 1,0),
JS_INLINABLE_FN("IsTypedArray",
intrinsic_IsInstanceOfBuiltin<TypedArrayObject>, 1,0,
IntrinsicIsTypedArray),
@@ -2591,6 +2650,9 @@ static const JSFunctionSpec intrinsic_functions[] = {
JS_FN("CallPromiseMethodIfWrapped", CallNonGenericSelfhostedMethod<Is<PromiseObject>>, 2, 0),
JS_FN("PromiseResolve", intrinsic_PromiseResolve, 2, 0),
+ JS_FN("ToBigInt", intrinsic_ToBigInt, 1, 0),
+ JS_FN("ToNumeric", intrinsic_ToNumeric, 1, 0),
+
JS_FS_END
};
diff --git a/js/src/vm/Stopwatch.cpp b/js/src/vm/Stopwatch.cpp
index 46841b27f8..2c267d446d 100644
--- a/js/src/vm/Stopwatch.cpp
+++ b/js/src/vm/Stopwatch.cpp
@@ -409,7 +409,8 @@ AutoStopwatch::getCycles(JSRuntime* runtime) const
cpuid_t inline
AutoStopwatch::getCPU() const
{
-#if defined(XP_WIN) && WINVER >= _WIN32_WINNT_VISTA
+// Temporary disable untested code path. Issue #2255
+#if 0 //defined(XP_WIN) && WINVER >= _WIN32_WINNT_VISTA
PROCESSOR_NUMBER proc;
GetCurrentProcessorNumberEx(&proc);
@@ -423,7 +424,8 @@ AutoStopwatch::getCPU() const
bool inline
AutoStopwatch::isSameCPU(const cpuid_t& a, const cpuid_t& b) const
{
-#if defined(XP_WIN) && WINVER >= _WIN32_WINNT_VISTA
+// Temporary disable untested code path. Issue #2255
+#if 0 //defined(XP_WIN) && WINVER >= _WIN32_WINNT_VISTA
return a.group_ == b.group_ && a.number_ == b.number_;
#else
return true;
diff --git a/js/src/vm/Stopwatch.h b/js/src/vm/Stopwatch.h
index f3985c53aa..84da265a25 100644
--- a/js/src/vm/Stopwatch.h
+++ b/js/src/vm/Stopwatch.h
@@ -271,7 +271,8 @@ struct PerformanceMonitoring {
uint64_t highestTimestampCounter_;
};
-#if WINVER >= 0x0600
+// Temporary disable untested code path. Issue #2255
+#if 0 // WINVER >= 0x0600
struct cpuid_t {
uint16_t group_;
uint8_t number_;
diff --git a/js/src/vm/StringBuffer.cpp b/js/src/vm/StringBuffer.cpp
index e4f0e4f4d6..ce0fc4e719 100644
--- a/js/src/vm/StringBuffer.cpp
+++ b/js/src/vm/StringBuffer.cpp
@@ -170,6 +170,13 @@ js::ValueToStringBufferSlow(JSContext* cx, const Value& arg, StringBuffer& sb)
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_SYMBOL_TO_STRING);
return false;
}
+ if (v.isBigInt()) {
+ RootedBigInt i(cx, v.toBigInt());
+ JSLinearString* str = BigInt::toString(cx, i, 10);
+ if (!str)
+ return false;
+ return sb.append(str);
+ }
MOZ_ASSERT(v.isUndefined());
return sb.append(cx->names().undefined);
}
diff --git a/js/src/vm/StructuredClone.cpp b/js/src/vm/StructuredClone.cpp
index 26e57976fc..e99cfe8f71 100644
--- a/js/src/vm/StructuredClone.cpp
+++ b/js/src/vm/StructuredClone.cpp
@@ -31,6 +31,7 @@
#include "mozilla/CheckedInt.h"
#include "mozilla/EndianUtils.h"
#include "mozilla/FloatingPoint.h"
+#include "mozilla/RangedPtr.h"
#include <algorithm>
@@ -39,6 +40,8 @@
#include "jsdate.h"
#include "jswrapper.h"
+#include "vm/BigIntType.h"
+
#include "builtin/MapObject.h"
#include "js/Date.h"
#include "js/GCHashTable.h"
@@ -57,6 +60,7 @@ using mozilla::IsNaN;
using mozilla::LittleEndian;
using mozilla::NativeEndian;
using mozilla::NumbersAreIdentical;
+using mozilla::RangedPtr;
using JS::CanonicalizeNaN;
// When you make updates here, make sure you consider whether you need to bump the
@@ -104,6 +108,9 @@ enum StructuredDataType : uint32_t {
SCTAG_SHARED_ARRAY_BUFFER_OBJECT,
+ SCTAG_BIGINT,
+ SCTAG_BIGINT_OBJECT,
+
SCTAG_TYPED_ARRAY_V1_MIN = 0xFFFF0100,
SCTAG_TYPED_ARRAY_V1_INT8 = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::Int8,
SCTAG_TYPED_ARRAY_V1_UINT8 = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::Uint8,
@@ -114,7 +121,8 @@ enum StructuredDataType : uint32_t {
SCTAG_TYPED_ARRAY_V1_FLOAT32 = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::Float32,
SCTAG_TYPED_ARRAY_V1_FLOAT64 = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::Float64,
SCTAG_TYPED_ARRAY_V1_UINT8_CLAMPED = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::Uint8Clamped,
- SCTAG_TYPED_ARRAY_V1_MAX = SCTAG_TYPED_ARRAY_V1_MIN + Scalar::MaxTypedArrayViewType - 1,
+ // BigInt64 and BigUint64 are not supported in the v1 format.
+ SCTAG_TYPED_ARRAY_V1_MAX = SCTAG_TYPED_ARRAY_V1_UINT8_CLAMPED,
/*
* Define a separate range of numbers for Transferable-only tags, since
@@ -349,6 +357,8 @@ struct JSStructuredCloneReader {
JSString* readStringImpl(uint32_t nchars);
JSString* readString(uint32_t data);
+ BigInt* readBigInt(uint32_t data);
+
bool checkDouble(double d);
bool readTypedArray(uint32_t arrayType, uint32_t nelems, MutableHandleValue vp,
bool v1Read = false);
@@ -439,6 +449,8 @@ struct JSStructuredCloneWriter {
bool traverseSet(HandleObject obj);
bool traverseSavedFrame(HandleObject obj);
+ bool writeBigInt(uint32_t tag, BigInt* bi);
+
bool reportDataCloneError(uint32_t errorId);
bool parseTransferable();
@@ -1055,6 +1067,23 @@ JSStructuredCloneWriter::writeString(uint32_t tag, JSString* str)
: out.writeChars(linear->twoByteChars(nogc), length);
}
+bool
+JSStructuredCloneWriter::writeBigInt(uint32_t tag, BigInt* bi)
+{
+ bool signBit = bi->isNegative();
+ size_t length = bi->digitLength();
+ // The length must fit in 31 bits to leave room for a sign bit.
+ if (length > size_t(INT32_MAX)) {
+ return false;
+ }
+ uint32_t lengthAndSign = length | (static_cast<uint32_t>(signBit) << 31);
+
+ if (!out.writePair(tag, lengthAndSign)) {
+ return false;
+ }
+ return out.writeArray(bi->digits().data(), length);
+}
+
inline void
JSStructuredCloneWriter::checkStack()
{
@@ -1388,6 +1417,8 @@ JSStructuredCloneWriter::startWrite(HandleValue v)
return out.writePair(SCTAG_NULL, 0);
} else if (v.isUndefined()) {
return out.writePair(SCTAG_UNDEFINED, 0);
+ } else if (v.isBigInt()) {
+ return writeBigInt(SCTAG_BIGINT, v.toBigInt());
} else if (v.isObject()) {
RootedObject obj(context(), &v.toObject());
@@ -1441,6 +1472,12 @@ JSStructuredCloneWriter::startWrite(HandleValue v)
return writeString(SCTAG_STRING_OBJECT, unboxed.toString());
} else if (cls == ESClass::Map) {
return traverseMap(obj);
+ } else if (cls == ESClass::BigInt) {
+ RootedValue unboxed(context());
+ if (!Unbox(context(), obj, &unboxed)) {
+ return false;
+ }
+ return writeBigInt(SCTAG_BIGINT_OBJECT, unboxed.toBigInt());
} else if (cls == ESClass::Set) {
return traverseSet(obj);
} else if (SavedFrame::isSavedFrameOrWrapperAndNotProto(*obj)) {
@@ -1745,6 +1782,22 @@ JSStructuredCloneReader::readString(uint32_t data)
return latin1 ? readStringImpl<Latin1Char>(nchars) : readStringImpl<char16_t>(nchars);
}
+BigInt* JSStructuredCloneReader::readBigInt(uint32_t data) {
+ size_t length = data & JS_BITMASK(31);
+ bool isNegative = data & (1 << 31);
+ if (length == 0) {
+ return BigInt::zero(context());
+ }
+ BigInt* result = BigInt::createUninitialized(context(), length, isNegative);
+ if (!result) {
+ return nullptr;
+ }
+ if (!in.readArray(result->digits().data(), length)) {
+ return nullptr;
+ }
+ return result;
+}
+
static uint32_t
TagToV1ArrayType(uint32_t tag)
{
@@ -1756,7 +1809,7 @@ bool
JSStructuredCloneReader::readTypedArray(uint32_t arrayType, uint32_t nelems, MutableHandleValue vp,
bool v1Read)
{
- if (arrayType > Scalar::Uint8Clamped) {
+ if (arrayType > (v1Read ? Scalar::Uint8Clamped : Scalar::BigUint64)) {
JS_ReportErrorNumberASCII(context(), GetErrorMessage, nullptr, JSMSG_SC_BAD_SERIALIZED_DATA,
"unhandled typed array element type");
return false;
@@ -1820,6 +1873,12 @@ JSStructuredCloneReader::readTypedArray(uint32_t arrayType, uint32_t nelems, Mut
case Scalar::Uint8Clamped:
obj = JS_NewUint8ClampedArrayWithBuffer(context(), buffer, byteOffset, nelems);
break;
+ case Scalar::BigInt64:
+ obj = JS_NewBigInt64ArrayWithBuffer(context(), buffer, byteOffset, nelems);
+ break;
+ case Scalar::BigUint64:
+ obj = JS_NewBigUint64ArrayWithBuffer(context(), buffer, byteOffset, nelems);
+ break;
default:
MOZ_CRASH("Can't happen: arrayType range checked above");
}
@@ -1955,6 +2014,8 @@ JSStructuredCloneReader::readV1ArrayBuffer(uint32_t arrayType, uint32_t nelems,
case Scalar::Float32:
return in.readArray((uint32_t*) buffer.dataPointer(), nelems);
case Scalar::Float64:
+ case Scalar::BigInt64:
+ case Scalar::BigUint64:
return in.readArray((uint64_t*) buffer.dataPointer(), nelems);
default:
MOZ_CRASH("Can't happen: arrayType range checked by caller");
@@ -2021,6 +2082,19 @@ JSStructuredCloneReader::startRead(MutableHandleValue vp)
break;
}
+ case SCTAG_BIGINT:
+ case SCTAG_BIGINT_OBJECT: {
+ RootedBigInt bi(context(), readBigInt(data));
+ if (!bi) {
+ return false;
+ }
+ vp.setBigInt(bi);
+ if (tag == SCTAG_BIGINT_OBJECT && !PrimitiveToObject(context(), vp)) {
+ return false;
+ }
+ break;
+ }
+
case SCTAG_DATE_OBJECT: {
double d;
if (!in.readDouble(&d) || !checkDouble(d))
diff --git a/js/src/vm/TypeInference-inl.h b/js/src/vm/TypeInference-inl.h
index 5e1bac6bc9..8c9b179223 100644
--- a/js/src/vm/TypeInference-inl.h
+++ b/js/src/vm/TypeInference-inl.h
@@ -213,6 +213,8 @@ PrimitiveTypeFlag(JSValueType type)
return TYPE_FLAG_STRING;
case JSVAL_TYPE_SYMBOL:
return TYPE_FLAG_SYMBOL;
+ case JSVAL_TYPE_BIGINT:
+ return TYPE_FLAG_BIGINT;
case JSVAL_TYPE_MAGIC:
return TYPE_FLAG_LAZYARGS;
default:
@@ -238,6 +240,8 @@ TypeFlagPrimitive(TypeFlags flags)
return JSVAL_TYPE_STRING;
case TYPE_FLAG_SYMBOL:
return JSVAL_TYPE_SYMBOL;
+ case TYPE_FLAG_BIGINT:
+ return JSVAL_TYPE_BIGINT;
case TYPE_FLAG_LAZYARGS:
return JSVAL_TYPE_MAGIC;
default:
diff --git a/js/src/vm/TypeInference.cpp b/js/src/vm/TypeInference.cpp
index ca34b184e8..a36926eb94 100644
--- a/js/src/vm/TypeInference.cpp
+++ b/js/src/vm/TypeInference.cpp
@@ -111,6 +111,8 @@ TypeSet::NonObjectTypeString(TypeSet::Type type)
return "string";
case JSVAL_TYPE_SYMBOL:
return "symbol";
+ case JSVAL_TYPE_BIGINT:
+ return "BigInt";
case JSVAL_TYPE_MAGIC:
return "lazyargs";
default:
@@ -366,6 +368,8 @@ TypeSet::mightBeMIRType(jit::MIRType type) const
return baseFlags() & TYPE_FLAG_STRING;
case jit::MIRType::Symbol:
return baseFlags() & TYPE_FLAG_SYMBOL;
+ case jit::MIRType::BigInt:
+ return baseFlags() & TYPE_FLAG_BIGINT;
case jit::MIRType::MagicOptimizedArguments:
return baseFlags() & TYPE_FLAG_LAZYARGS;
case jit::MIRType::MagicHole:
@@ -755,6 +759,8 @@ TypeSet::print(FILE* fp)
fprintf(fp, " string");
if (flags & TYPE_FLAG_SYMBOL)
fprintf(fp, " symbol");
+ if (flags & TYPE_FLAG_BIGINT)
+ fprintf(fp, " BigInt");
if (flags & TYPE_FLAG_LAZYARGS)
fprintf(fp, " lazyargs");
@@ -1631,6 +1637,8 @@ GetMIRTypeFromTypeFlags(TypeFlags flags)
return jit::MIRType::String;
case TYPE_FLAG_SYMBOL:
return jit::MIRType::Symbol;
+ case TYPE_FLAG_BIGINT:
+ return jit::MIRType::BigInt;
case TYPE_FLAG_LAZYARGS:
return jit::MIRType::MagicOptimizedArguments;
case TYPE_FLAG_ANYOBJECT:
diff --git a/js/src/vm/TypeInference.h b/js/src/vm/TypeInference.h
index 764f99ca07..58c606912c 100644
--- a/js/src/vm/TypeInference.h
+++ b/js/src/vm/TypeInference.h
@@ -59,17 +59,18 @@ enum : uint32_t {
TYPE_FLAG_DOUBLE = 0x10,
TYPE_FLAG_STRING = 0x20,
TYPE_FLAG_SYMBOL = 0x40,
- TYPE_FLAG_LAZYARGS = 0x80,
- TYPE_FLAG_ANYOBJECT = 0x100,
+ TYPE_FLAG_BIGINT = 0x80,
+ TYPE_FLAG_LAZYARGS = 0x100,
+ TYPE_FLAG_ANYOBJECT = 0x200,
/* Mask containing all primitives */
TYPE_FLAG_PRIMITIVE = TYPE_FLAG_UNDEFINED | TYPE_FLAG_NULL | TYPE_FLAG_BOOLEAN |
TYPE_FLAG_INT32 | TYPE_FLAG_DOUBLE | TYPE_FLAG_STRING |
- TYPE_FLAG_SYMBOL,
+ TYPE_FLAG_SYMBOL |TYPE_FLAG_BIGINT,
/* Mask/shift for the number of objects in objectSet */
- TYPE_FLAG_OBJECT_COUNT_MASK = 0x3e00,
- TYPE_FLAG_OBJECT_COUNT_SHIFT = 9,
+ TYPE_FLAG_OBJECT_COUNT_MASK = 0x3c00,
+ TYPE_FLAG_OBJECT_COUNT_SHIFT = 10,
TYPE_FLAG_OBJECT_COUNT_LIMIT = 7,
TYPE_FLAG_DOMOBJECT_COUNT_LIMIT =
TYPE_FLAG_OBJECT_COUNT_MASK >> TYPE_FLAG_OBJECT_COUNT_SHIFT,
@@ -78,7 +79,7 @@ enum : uint32_t {
TYPE_FLAG_UNKNOWN = 0x00004000,
/* Mask of normal type flags on a type set. */
- TYPE_FLAG_BASE_MASK = 0x000041ff,
+ TYPE_FLAG_BASE_MASK = TYPE_FLAG_PRIMITIVE | TYPE_FLAG_LAZYARGS | TYPE_FLAG_ANYOBJECT | TYPE_FLAG_UNKNOWN,
/* Additional flags for HeapTypeSet sets. */
@@ -109,6 +110,10 @@ enum : uint32_t {
};
typedef uint32_t TypeFlags;
+static_assert(TYPE_FLAG_PRIMITIVE < TYPE_FLAG_ANYOBJECT &&
+ TYPE_FLAG_LAZYARGS < TYPE_FLAG_ANYOBJECT,
+ "TYPE_FLAG_ANYOBJECT should be greater than primitive type flags");
+
/* Flags and other state stored in ObjectGroup::Flags */
enum : uint32_t {
/* Whether this group is associated with some allocation site. */
@@ -366,6 +371,7 @@ class TypeSet
static inline Type DoubleType() { return Type(JSVAL_TYPE_DOUBLE); }
static inline Type StringType() { return Type(JSVAL_TYPE_STRING); }
static inline Type SymbolType() { return Type(JSVAL_TYPE_SYMBOL); }
+ static inline Type BigIntType() { return Type(JSVAL_TYPE_BIGINT); }
static inline Type MagicArgType() { return Type(JSVAL_TYPE_MAGIC); }
static inline Type AnyObjectType() { return Type(JSVAL_TYPE_OBJECT); }
static inline Type UnknownType() { return Type(JSVAL_TYPE_UNKNOWN); }
diff --git a/js/src/vm/TypedArrayCommon.h b/js/src/vm/TypedArrayCommon.h
index eb7b94f109..8e66587a1e 100644
--- a/js/src/vm/TypedArrayCommon.h
+++ b/js/src/vm/TypedArrayCommon.h
@@ -112,6 +112,20 @@ ConvertNumber<uint32_t, float>(float src)
return JS::ToUint32(src);
}
+template <>
+inline int64_t
+ConvertNumber<int64_t, float>(float src)
+{
+ return JS::ToInt64(src);
+}
+
+template <>
+inline uint64_t
+ConvertNumber<uint64_t, float>(float src)
+{
+ return JS::ToUint64(src);
+}
+
template<> inline int8_t
ConvertNumber<int8_t, double>(double src)
{
@@ -160,6 +174,20 @@ ConvertNumber<uint32_t, double>(double src)
return JS::ToUint32(src);
}
+template <>
+inline int64_t
+ConvertNumber<int64_t, double>(double src)
+{
+ return JS::ToInt64(src);
+}
+
+template <>
+inline uint64_t
+ConvertNumber<uint64_t, double>(double src)
+{
+ return JS::ToUint64(src);
+}
+
template<typename To, typename From>
inline To
ConvertNumber(From src)
@@ -178,6 +206,8 @@ template<> struct TypeIDOfType<int16_t> { static const Scalar::Type id = Scalar:
template<> struct TypeIDOfType<uint16_t> { static const Scalar::Type id = Scalar::Uint16; };
template<> struct TypeIDOfType<int32_t> { static const Scalar::Type id = Scalar::Int32; };
template<> struct TypeIDOfType<uint32_t> { static const Scalar::Type id = Scalar::Uint32; };
+template<> struct TypeIDOfType<int64_t> { static const Scalar::Type id = Scalar::BigInt64; };
+template<> struct TypeIDOfType<uint64_t> { static const Scalar::Type id = Scalar::BigUint64; };
template<> struct TypeIDOfType<float> { static const Scalar::Type id = Scalar::Float32; };
template<> struct TypeIDOfType<double> { static const Scalar::Type id = Scalar::Float64; };
template<> struct TypeIDOfType<uint8_clamped> { static const Scalar::Type id = Scalar::Uint8Clamped; };
@@ -355,6 +385,18 @@ class ElementSpecific
Ops::store(dest++, ConvertNumber<T>(Ops::load(src++)));
break;
}
+ case Scalar::BigInt64: {
+ SharedMem<int64_t*> src = data.cast<int64_t*>();
+ for (uint32_t i = 0; i < count; ++i)
+ Ops::store(dest++, ConvertNumber<T>(Ops::load(src++)));
+ break;
+ }
+ case Scalar::BigUint64: {
+ SharedMem<uint64_t*> src = data.cast<uint64_t*>();
+ for (uint32_t i = 0; i < count; ++i)
+ Ops::store(dest++, ConvertNumber<T>(Ops::load(src++)));
+ break;
+ }
case Scalar::Float32: {
SharedMem<JS_VOLATILE_ARM float*> src = data.cast<JS_VOLATILE_ARM float*>();
for (uint32_t i = 0; i < count; ++i)
@@ -399,12 +441,12 @@ class ElementSpecific
SharedMem<T*> dest =
target->template as<TypedArrayObject>().viewDataEither().template cast<T*>() + offset;
- MOZ_ASSERT(!canConvertInfallibly(MagicValue(JS_ELEMENTS_HOLE)),
+ MOZ_ASSERT(!canConvertInfallibly(MagicValue(JS_ELEMENTS_HOLE), target->type()),
"the following loop must abort on holes");
const Value* srcValues = source->as<NativeObject>().getDenseElements();
for (; i < bound; i++) {
- if (!canConvertInfallibly(srcValues[i]))
+ if (!canConvertInfallibly(srcValues[i], target->type()))
break;
Ops::store(dest + i, infallibleValueToNative(srcValues[i]));
}
@@ -459,7 +501,7 @@ class ElementSpecific
const Value* srcValues = source->getDenseElements();
for (; i < len; i++) {
- if (!canConvertInfallibly(srcValues[i]))
+ if (!canConvertInfallibly(srcValues[i], target->type()))
break;
Ops::store(dest + i, infallibleValueToNative(srcValues[i]));
}
@@ -568,6 +610,18 @@ class ElementSpecific
Ops::store(dest++, ConvertNumber<T>(*src++));
break;
}
+ case Scalar::BigInt64: {
+ int64_t* src = static_cast<int64_t*>(data);
+ for (uint32_t i = 0; i < len; ++i)
+ Ops::store(dest++, ConvertNumber<T>(*src++));
+ break;
+ }
+ case Scalar::BigUint64: {
+ uint64_t* src = static_cast<uint64_t*>(data);
+ for (uint32_t i = 0; i < len; ++i)
+ Ops::store(dest++, ConvertNumber<T>(*src++));
+ break;
+ }
case Scalar::Float32: {
float* src = static_cast<float*>(data);
for (uint32_t i = 0; i < len; ++i)
@@ -589,8 +643,11 @@ class ElementSpecific
}
static bool
- canConvertInfallibly(const Value& v)
+ canConvertInfallibly(const Value& v, Scalar::Type type)
{
+ if (type == Scalar::BigInt64 || type == Scalar::BigUint64) {
+ return false;
+ }
return v.isNumber() || v.isBoolean() || v.isNull() || v.isUndefined();
}
@@ -615,11 +672,21 @@ class ElementSpecific
{
MOZ_ASSERT(!v.isMagic());
- if (MOZ_LIKELY(canConvertInfallibly(v))) {
+ if (MOZ_LIKELY(canConvertInfallibly(v, TypeIDOfType<T>::id))) {
*result = infallibleValueToNative(v);
return true;
}
+ if (std::is_same<T, int64_t>::value) {
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, *result, ToBigInt64(cx, v));
+ return true;
+ }
+
+ if (std::is_same<T, uint64_t>::value) {
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, *result, ToBigUint64(cx, v));
+ return true;
+ }
+
double d;
MOZ_ASSERT(v.isString() || v.isObject() || v.isSymbol());
if (!(v.isString() ? StringToNumber(cx, v.toString(), &d) : ToNumber(cx, v, &d)))
@@ -668,6 +735,8 @@ class TypedArrayMethods
typedef typename SomeTypedArray::template OfType<uint16_t>::Type Uint16ArrayType;
typedef typename SomeTypedArray::template OfType<int32_t>::Type Int32ArrayType;
typedef typename SomeTypedArray::template OfType<uint32_t>::Type Uint32ArrayType;
+ typedef typename SomeTypedArray::template OfType<int64_t>::Type BigInt64ArrayType;
+ typedef typename SomeTypedArray::template OfType<uint64_t>::Type BigUint64ArrayType;
typedef typename SomeTypedArray::template OfType<float>::Type Float32ArrayType;
typedef typename SomeTypedArray::template OfType<double>::Type Float64ArrayType;
typedef typename SomeTypedArray::template OfType<uint8_clamped>::Type Uint8ClampedArrayType;
@@ -759,6 +828,14 @@ class TypedArrayMethods
if (isShared)
return ElementSpecific<Uint32ArrayType, SharedOps>::setFromTypedArray(cx, target, source, offset);
return ElementSpecific<Uint32ArrayType, UnsharedOps>::setFromTypedArray(cx, target, source, offset);
+ case Scalar::BigInt64:
+ if (isShared)
+ return ElementSpecific<BigInt64ArrayType, SharedOps>::setFromTypedArray(cx, target, source, offset);
+ return ElementSpecific<BigInt64ArrayType, UnsharedOps>::setFromTypedArray(cx, target, source, offset);
+ case Scalar::BigUint64:
+ if (isShared)
+ return ElementSpecific<BigUint64ArrayType, SharedOps>::setFromTypedArray(cx, target, source, offset);
+ return ElementSpecific<BigUint64ArrayType, UnsharedOps>::setFromTypedArray(cx, target, source, offset);
case Scalar::Float32:
if (isShared)
return ElementSpecific<Float32ArrayType, SharedOps>::setFromTypedArray(cx, target, source, offset);
@@ -816,6 +893,14 @@ class TypedArrayMethods
if (isShared)
return ElementSpecific<Uint32ArrayType, SharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
return ElementSpecific<Uint32ArrayType, UnsharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
+ case Scalar::BigInt64:
+ if (isShared)
+ return ElementSpecific<BigInt64ArrayType, SharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
+ return ElementSpecific<BigInt64ArrayType, UnsharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
+ case Scalar::BigUint64:
+ if (isShared)
+ return ElementSpecific<BigUint64ArrayType, SharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
+ return ElementSpecific<BigUint64ArrayType, UnsharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
case Scalar::Float32:
if (isShared)
return ElementSpecific<Float32ArrayType, SharedOps>::setFromNonTypedArray(cx, target, source, len, offset);
@@ -870,6 +955,14 @@ class TypedArrayMethods
if (isShared)
return ElementSpecific<Uint32ArrayType, SharedOps>::initFromIterablePackedArray(cx, target, source);
return ElementSpecific<Uint32ArrayType, UnsharedOps>::initFromIterablePackedArray(cx, target, source);
+ case Scalar::BigInt64:
+ if (isShared)
+ return ElementSpecific<BigInt64ArrayType, SharedOps>::initFromIterablePackedArray(cx, target, source);
+ return ElementSpecific<BigInt64ArrayType, UnsharedOps>::initFromIterablePackedArray(cx, target, source);
+ case Scalar::BigUint64:
+ if (isShared)
+ return ElementSpecific<BigUint64ArrayType, SharedOps>::initFromIterablePackedArray(cx, target, source);
+ return ElementSpecific<BigUint64ArrayType, UnsharedOps>::initFromIterablePackedArray(cx, target, source);
case Scalar::Float32:
if (isShared)
return ElementSpecific<Float32ArrayType, SharedOps>::initFromIterablePackedArray(cx, target, source);
diff --git a/js/src/vm/TypedArrayObject.cpp b/js/src/vm/TypedArrayObject.cpp
index 9d82fca6ec..28e4090eb8 100644
--- a/js/src/vm/TypedArrayObject.cpp
+++ b/js/src/vm/TypedArrayObject.cpp
@@ -65,6 +65,33 @@ using JS::ToUint32;
* the subclasses.
*/
+bool TypedArrayObject::convertForSideEffect(JSContext* cx, HandleValue v) const
+{
+ switch (type()) {
+ case Scalar::BigInt64:
+ case Scalar::BigUint64: {
+ return ToBigInt(cx, v) != nullptr;
+ }
+ case Scalar::Int8:
+ case Scalar::Uint8:
+ case Scalar::Int16:
+ case Scalar::Uint16:
+ case Scalar::Int32:
+ case Scalar::Uint32:
+ case Scalar::Float32:
+ case Scalar::Float64:
+ case Scalar::Uint8Clamped: {
+ double ignore;
+ return ToNumber(cx, v, &ignore);
+ }
+ case Scalar::MaxTypedArrayViewType:
+ case Scalar::Int64:
+ MOZ_CRASH("Unsupported TypedArray type");
+ }
+ MOZ_ASSERT_UNREACHABLE("Invalid scalar type");
+ return false;
+}
+
/* static */ int
TypedArrayObject::lengthOffset()
{
@@ -414,30 +441,7 @@ class TypedArrayObjectTemplate : public TypedArrayObject
return v.isObject() && v.toObject().hasClass(instanceClass());
}
- static void
- setIndexValue(TypedArrayObject& tarray, uint32_t index, double d)
- {
- // If the array is an integer array, we only handle up to
- // 32-bit ints from this point on. if we want to handle
- // 64-bit ints, we'll need some changes.
-
- // Assign based on characteristics of the destination type
- if (ArrayTypeIsFloatingPoint()) {
- setIndex(tarray, index, NativeType(d));
- } else if (ArrayTypeIsUnsigned()) {
- MOZ_ASSERT(sizeof(NativeType) <= 4);
- uint32_t n = ToUint32(d);
- setIndex(tarray, index, NativeType(n));
- } else if (ArrayTypeID() == Scalar::Uint8Clamped) {
- // The uint8_clamped type has a special rounding converter
- // for doubles.
- setIndex(tarray, index, NativeType(d));
- } else {
- MOZ_ASSERT(sizeof(NativeType) <= 4);
- int32_t n = ToInt32(d);
- setIndex(tarray, index, NativeType(n));
- }
- }
+ static bool convertValue(JSContext* cx, HandleValue v, NativeType* result);
static TypedArrayObject*
makeProtoInstance(JSContext* cx, HandleObject proto, AllocKind allocKind)
@@ -1001,9 +1005,128 @@ class TypedArrayObjectTemplate : public TypedArrayObject
jit::AtomicOperations::storeSafeWhenRacy(tarray.viewDataEither().cast<NativeType*>() + index, val);
}
- static Value getIndexValue(JSObject* tarray, uint32_t index);
+ static bool getElement(ExclusiveContext* cx, TypedArrayObject* tarray, uint32_t index, MutableHandleValue val);
+ static bool getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp);
+
+ static bool setElement(JSContext* cx, Handle<TypedArrayObject*> obj, uint64_t index, HandleValue v,
+ ObjectOpResult& result);
+ static bool defineElement(JSContext* cx, HandleObject obj, uint64_t index, HandleValue v,
+ ObjectOpResult& result);
};
+template <typename NativeType>
+bool TypedArrayObjectTemplate<NativeType>::convertValue(JSContext* cx, HandleValue v, NativeType* result)
+{
+ double d;
+ if (!ToNumber(cx, v, &d)) {
+ return false;
+ }
+
+#ifdef JS_MORE_DETERMINISTIC
+ // See the comment in ElementSpecific::doubleToNative.
+ d = JS::CanonicalizeNaN(d);
+#endif
+
+ // Assign based on characteristics of the destination type
+ if (ArrayTypeIsFloatingPoint()) {
+ *result = NativeType(d);
+ } else if (ArrayTypeIsUnsigned()) {
+ MOZ_ASSERT(sizeof(NativeType) <= 4);
+ uint32_t n = ToUint32(d);
+ *result = NativeType(n);
+ } else if (ArrayTypeID() == Scalar::Uint8Clamped) {
+ // The uint8_clamped type has a special rounding converter
+ // for doubles.
+ *result = NativeType(d);
+ } else {
+ MOZ_ASSERT(sizeof(NativeType) <= 4);
+ int32_t n = ToInt32(d);
+ *result = NativeType(n);
+ }
+ return true;
+}
+
+template <>
+bool TypedArrayObjectTemplate<int64_t>::convertValue(JSContext* cx, HandleValue v, int64_t* result)
+{
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, *result, ToBigInt64(cx, v));
+ return true;
+}
+
+template <>
+bool TypedArrayObjectTemplate<uint64_t>::convertValue(JSContext* cx, HandleValue v, uint64_t* result)
+{
+ JS_TRY_VAR_OR_RETURN_FALSE(cx, *result, ToBigUint64(cx, v));
+ return true;
+}
+
+// https://tc39.github.io/proposal-bigint/#sec-integerindexedelementset
+// 7.8 IntegerIndexedElementSet ( O, index, value )
+template <typename NativeType>
+/* static */ bool TypedArrayObjectTemplate<NativeType>::setElement(
+ JSContext* cx, Handle<TypedArrayObject*> obj, uint64_t index, HandleValue v,
+ ObjectOpResult& result)
+{
+ // Steps 1-2 are enforced by the caller.
+
+ // Steps 3-6.
+ NativeType nativeValue;
+ if (!convertValue(cx, v, &nativeValue)) {
+ return false;
+ }
+
+ // Step 8.
+ if (obj->hasDetachedBuffer()) {
+ return result.failSoft(JSMSG_TYPED_ARRAY_DETACHED);
+ }
+
+ // Steps 9-10 are enforced by the caller.
+
+ // Step 11.
+ uint32_t length = obj->length();
+
+ // Step 12.
+ if (index >= length) {
+ return result.failSoft(JSMSG_BAD_INDEX);
+ }
+
+ // Steps 7, 13-16.
+ TypedArrayObjectTemplate<NativeType>::setIndex(*obj, index, nativeValue);
+
+ // Step 17.
+ return result.succeed();
+}
+
+// Version of IntegerIndexedElementSet with no length check, used in
+// [[DefineOwnProperty]]
+template <typename NativeType>
+/* static */ bool TypedArrayObjectTemplate<NativeType>::defineElement(
+ JSContext* cx, HandleObject obj, uint64_t index, HandleValue v,
+ ObjectOpResult& result)
+{
+ // Steps 1-2 are enforced by the caller.
+
+ // Steps 3-6.
+ NativeType nativeValue;
+ if (!convertValue(cx, v, &nativeValue)) {
+ return false;
+ }
+
+ // Step 8.
+ if (obj->as<TypedArrayObject>().hasDetachedBuffer()) {
+ return result.fail(JSMSG_TYPED_ARRAY_DETACHED);
+ }
+
+ // Steps 9-12 are enforced by the caller.
+
+ // Steps 7, 13-16.
+ TypedArrayObjectTemplate<NativeType>::setIndex(obj->as<TypedArrayObject>(),
+ index, nativeValue);
+
+ // Step 17.
+ return result.succeed();
+}
+
#define CREATE_TYPE_FOR_TYPED_ARRAY(T, N) \
typedef TypedArrayObjectTemplate<T> N##Array;
JS_FOR_EACH_TYPED_ARRAY(CREATE_TYPE_FOR_TYPED_ARRAY)
@@ -1240,6 +1363,13 @@ TypedArrayObjectTemplate<T>::fromTypedArray(JSContext* cx, HandleObject other, b
}
}
+ // BigInt proposal 7.24, step 19.c.
+ if (Scalar::isBigIntType(ArrayTypeID()) !=
+ Scalar::isBigIntType(srcArray->type())) {
+ JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_NOT_BIGINT);
+ return nullptr;
+ }
+
// Steps 3-4 (remaining part), 18-21.
Rooted<TypedArrayObject*> obj(cx, makeInstance(cx, buffer, 0, elementLength, proto));
if (!obj)
@@ -1584,40 +1714,43 @@ ArrayBufferObject::createTypedArrayFromBuffer(JSContext* cx, unsigned argc, Valu
return CallNonGenericMethod<IsAnyArrayBuffer, createTypedArrayFromBufferImpl<T> >(cx, args);
}
-// this default implementation is only valid for integer types
-// less than 32-bits in size.
+// This default implementation is only valid for integer types less
+// than 32-bits in size.
template<typename NativeType>
-Value
-TypedArrayObjectTemplate<NativeType>::getIndexValue(JSObject* tarray, uint32_t index)
+bool
+TypedArrayObjectTemplate<NativeType>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
{
static_assert(sizeof(NativeType) < 4,
"this method must only handle NativeType values that are "
"always exact int32_t values");
- return Int32Value(getIndex(tarray, index));
+ *vp = Int32Value(getIndex(tarray, index));
+ return true;
}
namespace {
-// and we need to specialize for 32-bit integers and floats
+// We need to specialize for floats and other integer types.
template<>
-Value
-TypedArrayObjectTemplate<int32_t>::getIndexValue(JSObject* tarray, uint32_t index)
+bool
+TypedArrayObjectTemplate<int32_t>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
{
- return Int32Value(getIndex(tarray, index));
+ *vp = Int32Value(getIndex(tarray, index));
+ return true;
}
template<>
-Value
-TypedArrayObjectTemplate<uint32_t>::getIndexValue(JSObject* tarray, uint32_t index)
+bool
+TypedArrayObjectTemplate<uint32_t>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
{
uint32_t val = getIndex(tarray, index);
- return NumberValue(val);
+ *vp = NumberValue(val);
+ return true;
}
template<>
-Value
-TypedArrayObjectTemplate<float>::getIndexValue(JSObject* tarray, uint32_t index)
+bool
+TypedArrayObjectTemplate<float>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
{
float val = getIndex(tarray, index);
double dval = val;
@@ -1632,12 +1765,13 @@ TypedArrayObjectTemplate<float>::getIndexValue(JSObject* tarray, uint32_t index)
* This could be removed for platforms/compilers known to convert a 32-bit
* non-canonical nan to a 64-bit canonical nan.
*/
- return DoubleValue(CanonicalizeNaN(dval));
+ *vp = DoubleValue(CanonicalizeNaN(dval));
+ return true;
}
template<>
-Value
-TypedArrayObjectTemplate<double>::getIndexValue(JSObject* tarray, uint32_t index)
+bool
+TypedArrayObjectTemplate<double>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
{
double val = getIndex(tarray, index);
@@ -1648,9 +1782,60 @@ TypedArrayObjectTemplate<double>::getIndexValue(JSObject* tarray, uint32_t index
* confuse the engine into interpreting a double-typed jsval as an
* object-typed jsval.
*/
- return DoubleValue(CanonicalizeNaN(val));
+ *vp = DoubleValue(CanonicalizeNaN(val));
+ return true;
+}
+
+template <>
+bool
+TypedArrayObjectTemplate<int64_t>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
+{
+ return false;
}
+template <>
+bool
+TypedArrayObjectTemplate<uint64_t>::getElementPure(TypedArrayObject* tarray, uint32_t index, Value* vp)
+{
+ return false;
+}
+} /* anonymous namespace */
+
+namespace {
+
+template <typename NativeType>
+bool TypedArrayObjectTemplate<NativeType>::getElement(ExclusiveContext* cx, TypedArrayObject* tarray, uint32_t index,
+ MutableHandleValue val)
+{
+ MOZ_ALWAYS_TRUE(getElementPure(tarray, index, val.address()));
+ return true;
+}
+
+template <>
+bool TypedArrayObjectTemplate<int64_t>::getElement(ExclusiveContext* cx, TypedArrayObject* tarray, uint32_t index,
+ MutableHandleValue val)
+{
+ int64_t n = getIndex(tarray, index);
+ BigInt* res = BigInt::createFromInt64(cx, n);
+ if (!res) {
+ return false;
+ }
+ val.setBigInt(res);
+ return true;
+}
+
+template <>
+bool TypedArrayObjectTemplate<uint64_t>::getElement(ExclusiveContext* cx, TypedArrayObject* tarray, uint32_t index,
+ MutableHandleValue val)
+{
+ uint64_t n = getIndex(tarray, index);
+ BigInt* res = BigInt::createFromUint64(cx, n);
+ if (!res) {
+ return false;
+ }
+ val.setBigInt(res);
+ return true;
+}
} /* anonymous namespace */
static NewObjectKind
@@ -1898,10 +2083,8 @@ DataViewObject::class_constructor(JSContext* cx, unsigned argc, Value* vp)
template <typename NativeType>
/* static */ uint8_t*
-DataViewObject::getDataPointer(JSContext* cx, Handle<DataViewObject*> obj, double offset)
+DataViewObject::getDataPointer(JSContext* cx, Handle<DataViewObject*> obj, uint64_t offset)
{
- MOZ_ASSERT(offset >= 0);
-
const size_t TypeSize = sizeof(NativeType);
if (offset > UINT32_MAX - TypeSize || offset + TypeSize > obj->byteLength()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_ARG_INDEX_OUT_OF_RANGE,
@@ -1959,6 +2142,8 @@ template <> struct DataToRepType<int16_t> { typedef uint16_t result; };
template <> struct DataToRepType<uint16_t> { typedef uint16_t result; };
template <> struct DataToRepType<int32_t> { typedef uint32_t result; };
template <> struct DataToRepType<uint32_t> { typedef uint32_t result; };
+template <> struct DataToRepType<int64_t> { typedef uint64_t result; };
+template <> struct DataToRepType<uint64_t> { typedef uint64_t result; };
template <> struct DataToRepType<float> { typedef uint32_t result; };
template <> struct DataToRepType<double> { typedef uint64_t result; };
@@ -1987,31 +2172,6 @@ struct DataViewIO
}
};
-static bool
-ToIndex(JSContext* cx, HandleValue v, double* index)
-{
- if (v.isUndefined()) {
- *index = 0.0;
- return true;
- }
-
- double integerIndex;
- if (!ToInteger(cx, v, &integerIndex))
- return false;
-
- // Inlined version of ToLength.
- // 1. Already an integer
- // 2. Step eliminates < 0, +0 == -0 with SameValueZero
- // 3/4. Limit to <= 2^53-1, so everything above should fail.
- if (integerIndex < 0 || integerIndex > 9007199254740991) {
- JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_BAD_INDEX);
- return false;
- }
-
- *index = integerIndex;
- return true;
-}
-
template<typename NativeType>
/* static */ bool
DataViewObject::read(JSContext* cx, Handle<DataViewObject*> obj,
@@ -2021,7 +2181,7 @@ DataViewObject::read(JSContext* cx, Handle<DataViewObject*> obj,
// Step 3. unnecessary assert
// Step 4.
- double getIndex;
+ uint64_t getIndex;
if (!ToIndex(cx, args.get(0), &getIndex))
return false;
@@ -2059,6 +2219,28 @@ WebIDLCast(JSContext* cx, HandleValue value, NativeType* out)
}
template <>
+inline bool WebIDLCast<int64_t>(JSContext* cx, HandleValue value, int64_t* out)
+{
+ RootedBigInt bi(cx, ToBigInt(cx, value));
+ if (!bi) {
+ return false;
+ }
+ *out = BigInt::toInt64(bi);
+ return true;
+}
+
+template <>
+inline bool WebIDLCast<uint64_t>(JSContext* cx, HandleValue value, uint64_t* out)
+{
+ RootedBigInt bi(cx, ToBigInt(cx, value));
+ if (!bi) {
+ return false;
+ }
+ *out = BigInt::toUint64(bi);
+ return true;
+}
+
+template <>
inline bool
WebIDLCast<float>(JSContext* cx, HandleValue value, float* out)
{
@@ -2076,6 +2258,8 @@ WebIDLCast<double>(JSContext* cx, HandleValue value, double* out)
return ToNumber(cx, value, out);
}
+// https://tc39.github.io/ecma262/#sec-setviewvalue
+// SetViewValue ( view, requestIndex, isLittleEndian, type, value )
template<typename NativeType>
/* static */ bool
DataViewObject::write(JSContext* cx, Handle<DataViewObject*> obj,
@@ -2085,11 +2269,11 @@ DataViewObject::write(JSContext* cx, Handle<DataViewObject*> obj,
// Step 3. unnecessary assert
// Step 4.
- double getIndex;
+ uint64_t getIndex;
if (!ToIndex(cx, args.get(0), &getIndex))
return false;
- // Step 5. Should just call ToNumber (unobservable)
+ // Step 5. Extended by the BigInt proposal to call either ToBigInt or ToNumber
NativeType value;
if (!WebIDLCast(cx, args.get(1), &value))
return false;
@@ -2245,6 +2429,60 @@ DataViewObject::fun_getUint32(JSContext* cx, unsigned argc, Value* vp)
return CallNonGenericMethod<is, getUint32Impl>(cx, args);
}
+// BigInt proposal 7.26
+// DataView.prototype.getBigInt64 ( byteOffset [ , littleEndian ] )
+bool DataViewObject::getBigInt64Impl(JSContext* cx, const CallArgs& args)
+{
+ MOZ_ASSERT(is(args.thisv()));
+
+ Rooted<DataViewObject*> thisView(cx, &args.thisv().toObject().as<DataViewObject>());
+
+ int64_t val;
+ if (!read(cx, thisView, args, &val, "getBigInt64")) {
+ return false;
+ }
+
+ BigInt* bi = BigInt::createFromInt64(cx, val);
+ if (!bi) {
+ return false;
+ }
+ args.rval().setBigInt(bi);
+ return true;
+}
+
+bool DataViewObject::fun_getBigInt64(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ return CallNonGenericMethod<is, getBigInt64Impl>(cx, args);
+}
+
+// BigInt proposal 7.27
+// DataView.prototype.getBigUint64 ( byteOffset [ , littleEndian ] )
+bool DataViewObject::getBigUint64Impl(JSContext* cx, const CallArgs& args)
+{
+ MOZ_ASSERT(is(args.thisv()));
+
+ Rooted<DataViewObject*> thisView(cx, &args.thisv().toObject().as<DataViewObject>());
+
+ int64_t val;
+ if (!read(cx, thisView, args, &val, "getBigUint64")) {
+ return false;
+ }
+
+ BigInt* bi = BigInt::createFromUint64(cx, val);
+ if (!bi) {
+ return false;
+ }
+ args.rval().setBigInt(bi);
+ return true;
+}
+
+bool DataViewObject::fun_getBigUint64(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ return CallNonGenericMethod<is, getBigUint64Impl>(cx, args);
+}
+
bool
DataViewObject::getFloat32Impl(JSContext* cx, const CallArgs& args)
{
@@ -2409,6 +2647,48 @@ DataViewObject::fun_setUint32(JSContext* cx, unsigned argc, Value* vp)
return CallNonGenericMethod<is, setUint32Impl>(cx, args);
}
+// BigInt proposal 7.28
+// DataView.prototype.setBigInt64 ( byteOffset, value [ , littleEndian ] )
+bool DataViewObject::setBigInt64Impl(JSContext* cx, const CallArgs& args)
+{
+ MOZ_ASSERT(is(args.thisv()));
+
+ Rooted<DataViewObject*> thisView(cx, &args.thisv().toObject().as<DataViewObject>());
+
+ if (!write<int64_t>(cx, thisView, args, "setBigInt64")) {
+ return false;
+ }
+ args.rval().setUndefined();
+ return true;
+}
+
+bool DataViewObject::fun_setBigInt64(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ return CallNonGenericMethod<is, setBigInt64Impl>(cx, args);
+}
+
+// BigInt proposal 7.29
+// DataView.prototype.setBigUint64 ( byteOffset, value [ , littleEndian ] )
+bool DataViewObject::setBigUint64Impl(JSContext* cx, const CallArgs& args)
+{
+ MOZ_ASSERT(is(args.thisv()));
+
+ Rooted<DataViewObject*> thisView(cx, &args.thisv().toObject().as<DataViewObject>());
+
+ if (!write<uint64_t>(cx, thisView, args, "setBigUint64")) {
+ return false;
+ }
+ args.rval().setUndefined();
+ return true;
+}
+
+bool DataViewObject::fun_setBigUint64(JSContext* cx, unsigned argc, Value* vp)
+{
+ CallArgs args = CallArgsFromVp(argc, vp);
+ return CallNonGenericMethod<is, setBigUint64Impl>(cx, args);
+}
+
bool
DataViewObject::setFloat32Impl(JSContext* cx, const CallArgs& args)
{
@@ -2449,28 +2729,17 @@ DataViewObject::fun_setFloat64(JSContext* cx, unsigned argc, Value* vp)
return CallNonGenericMethod<is, setFloat64Impl>(cx, args);
}
-Value
-TypedArrayObject::getElement(uint32_t index)
+namespace js {
+
+template <>
+bool TypedArrayObject::getElement<CanGC>(ExclusiveContext* cx, uint32_t index, MutableHandleValue val)
{
switch (type()) {
- case Scalar::Int8:
- return Int8Array::getIndexValue(this, index);
- case Scalar::Uint8:
- return Uint8Array::getIndexValue(this, index);
- case Scalar::Int16:
- return Int16Array::getIndexValue(this, index);
- case Scalar::Uint16:
- return Uint16Array::getIndexValue(this, index);
- case Scalar::Int32:
- return Int32Array::getIndexValue(this, index);
- case Scalar::Uint32:
- return Uint32Array::getIndexValue(this, index);
- case Scalar::Float32:
- return Float32Array::getIndexValue(this, index);
- case Scalar::Float64:
- return Float64Array::getIndexValue(this, index);
- case Scalar::Uint8Clamped:
- return Uint8ClampedArray::getIndexValue(this, index);
+#define GET_ELEMENT(T, N) \
+ case Scalar::N: \
+ return N##Array::getElement(cx, this, index, val);
+ JS_FOR_EACH_TYPED_ARRAY(GET_ELEMENT)
+#undef GET_ELEMENT
case Scalar::Int64:
case Scalar::Float32x4:
case Scalar::Int8x16:
@@ -2483,44 +2752,23 @@ TypedArrayObject::getElement(uint32_t index)
MOZ_CRASH("Unknown TypedArray type");
}
-void
-TypedArrayObject::setElement(TypedArrayObject& obj, uint32_t index, double d)
-{
- MOZ_ASSERT(index < obj.length());
+template <>
+bool TypedArrayObject::getElement<NoGC>(
+ ExclusiveContext* cx, uint32_t index,
+ typename MaybeRooted<Value, NoGC>::MutableHandleType vp) {
+ return getElementPure(index, vp.address());
+}
-#ifdef JS_MORE_DETERMINISTIC
- // See the comment in ElementSpecific::doubleToNative.
- d = JS::CanonicalizeNaN(d);
-#endif
+} // namespace js
- switch (obj.type()) {
- case Scalar::Int8:
- Int8Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Uint8:
- Uint8Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Uint8Clamped:
- Uint8ClampedArray::setIndexValue(obj, index, d);
- return;
- case Scalar::Int16:
- Int16Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Uint16:
- Uint16Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Int32:
- Int32Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Uint32:
- Uint32Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Float32:
- Float32Array::setIndexValue(obj, index, d);
- return;
- case Scalar::Float64:
- Float64Array::setIndexValue(obj, index, d);
- return;
+bool TypedArrayObject::getElementPure(uint32_t index, Value* vp)
+{
+ switch (type()) {
+#define GET_ELEMENT_PURE(T, N) \
+ case Scalar::N: \
+ return N##Array::getElementPure(this, index, vp);
+ JS_FOR_EACH_TYPED_ARRAY(GET_ELEMENT_PURE)
+#undef GET_ELEMENT
case Scalar::Int64:
case Scalar::Float32x4:
case Scalar::Int8x16:
@@ -2533,6 +2781,38 @@ TypedArrayObject::setElement(TypedArrayObject& obj, uint32_t index, double d)
MOZ_CRASH("Unknown TypedArray type");
}
+/* static */
+bool TypedArrayObject::getElements(JSContext* cx, Handle<TypedArrayObject*> tarray, Value* vp)
+{
+ uint32_t length = tarray->length();
+ MOZ_ASSERT_IF(length > 0, !tarray->hasDetachedBuffer());
+
+ switch (tarray->type()) {
+#define GET_ELEMENTS(T, N) \
+ case Scalar::N: \
+ for (uint32_t i = 0; i < length; ++i, ++vp) { \
+ if (!N##Array::getElement(cx, tarray, i, \
+ MutableHandleValue::fromMarkedLocation(vp))) { \
+ return false; \
+ } \
+ } \
+ return true;
+ JS_FOR_EACH_TYPED_ARRAY(GET_ELEMENTS)
+#undef GET_ELEMENTS
+ default:
+ MOZ_CRASH("Unknown TypedArray type");
+ case Scalar::MaxTypedArrayViewType:
+ case Scalar::Int64:
+ case Scalar::Float32x4:
+ case Scalar::Int8x16:
+ case Scalar::Int16x8:
+ case Scalar::Int32x4:
+ break;
+ }
+
+ MOZ_CRASH("Unknown TypedArray type");
+}
+
/***
*** JS impl
***/
@@ -2585,6 +2865,8 @@ IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(Int32, int32_t)
IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(Uint32, uint32_t)
IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(Float32, float)
IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(Float64, double)
+IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(BigInt64, int64_t)
+IMPL_TYPED_ARRAY_JSAPI_CONSTRUCTORS(BigUint64, uint64_t)
#define IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(Name, ExternalType, InternalType) \
JS_FRIEND_API(JSObject*) JS_GetObjectAs ## Name ## Array(JSObject* obj, \
@@ -2616,6 +2898,8 @@ IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(Int32, int32_t, int32_t)
IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(Uint32, uint32_t, uint32_t)
IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(Float32, float, float)
IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(Float64, double, double)
+IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(BigInt64, int64_t, int64_t)
+IMPL_TYPED_ARRAY_COMBINED_UNWRAPPERS(BigUint64, uint32_t, uint64_t)
static const ClassOps TypedArrayClassOps = {
nullptr, /* addProperty */
@@ -2653,7 +2937,9 @@ static const JSPropertySpec static_prototype_properties[Scalar::MaxTypedArrayVie
IMPL_TYPED_ARRAY_PROPERTIES(Uint32),
IMPL_TYPED_ARRAY_PROPERTIES(Float32),
IMPL_TYPED_ARRAY_PROPERTIES(Float64),
- IMPL_TYPED_ARRAY_PROPERTIES(Uint8Clamped)
+ IMPL_TYPED_ARRAY_PROPERTIES(Uint8Clamped),
+ IMPL_TYPED_ARRAY_PROPERTIES(BigInt64),
+ IMPL_TYPED_ARRAY_PROPERTIES(BigUint64)
};
#define IMPL_TYPED_ARRAY_CLASS_SPEC(_type) \
@@ -2677,7 +2963,9 @@ static const ClassSpec TypedArrayObjectClassSpecs[Scalar::MaxTypedArrayViewType]
IMPL_TYPED_ARRAY_CLASS_SPEC(Uint32),
IMPL_TYPED_ARRAY_CLASS_SPEC(Float32),
IMPL_TYPED_ARRAY_CLASS_SPEC(Float64),
- IMPL_TYPED_ARRAY_CLASS_SPEC(Uint8Clamped)
+ IMPL_TYPED_ARRAY_CLASS_SPEC(Uint8Clamped),
+ IMPL_TYPED_ARRAY_CLASS_SPEC(BigInt64),
+ IMPL_TYPED_ARRAY_CLASS_SPEC(BigUint64)
};
#define IMPL_TYPED_ARRAY_CLASS(_type) \
@@ -2703,7 +2991,9 @@ const Class TypedArrayObject::classes[Scalar::MaxTypedArrayViewType] = {
IMPL_TYPED_ARRAY_CLASS(Uint32),
IMPL_TYPED_ARRAY_CLASS(Float32),
IMPL_TYPED_ARRAY_CLASS(Float64),
- IMPL_TYPED_ARRAY_CLASS(Uint8Clamped)
+ IMPL_TYPED_ARRAY_CLASS(Uint8Clamped),
+ IMPL_TYPED_ARRAY_CLASS(BigInt64),
+ IMPL_TYPED_ARRAY_CLASS(BigUint64)
};
#define IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(_type) \
@@ -2727,7 +3017,9 @@ static const ClassSpec TypedArrayObjectProtoClassSpecs[Scalar::MaxTypedArrayView
IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(Uint32),
IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(Float32),
IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(Float64),
- IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(Uint8Clamped)
+ IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(Uint8Clamped),
+ IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(BigInt64),
+ IMPL_TYPED_ARRAY_PROTO_CLASS_SPEC(BigUint64)
};
// The various typed array prototypes are supposed to 1) be normal objects,
@@ -2761,7 +3053,9 @@ const Class TypedArrayObject::protoClasses[Scalar::MaxTypedArrayViewType] = {
IMPL_TYPED_ARRAY_PROTO_CLASS(Uint32),
IMPL_TYPED_ARRAY_PROTO_CLASS(Float32),
IMPL_TYPED_ARRAY_PROTO_CLASS(Float64),
- IMPL_TYPED_ARRAY_PROTO_CLASS(Uint8Clamped)
+ IMPL_TYPED_ARRAY_PROTO_CLASS(Uint8Clamped),
+ IMPL_TYPED_ARRAY_PROTO_CLASS(BigInt64),
+ IMPL_TYPED_ARRAY_PROTO_CLASS(BigUint64)
};
/* static */ bool
@@ -2820,6 +3114,13 @@ const JSFunctionSpec DataViewObject::jsfuncs[] = {
JS_FS_END
};
+const JSFunctionSpec DataViewObject::bigIntMethods[] = {
+ JS_FN("getBigInt64", DataViewObject::fun_getBigInt64, 1, 0),
+ JS_FN("getBigUint64", DataViewObject::fun_getBigUint64, 1, 0),
+ JS_FN("setBigInt64", DataViewObject::fun_setBigInt64, 2, 0),
+ JS_FN("setBigUint64", DataViewObject::fun_setBigUint64, 2, 0),
+ JS_FS_END};
+
template<Value ValueGetter(DataViewObject* view)>
bool
DataViewObject::getterImpl(JSContext* cx, const CallArgs& args)
@@ -2888,6 +3189,9 @@ DataViewObject::initClass(JSContext* cx)
if (!JS_DefineFunctions(cx, proto, DataViewObject::jsfuncs))
return false;
+ if (!JS_DefineFunctions(cx, proto, DataViewObject::bigIntMethods))
+ return false;
+
if (!DefineToStringTag(cx, proto, cx->names().DataView))
return false;
@@ -3007,6 +3311,25 @@ js::StringIsTypedArrayIndex(const char16_t* s, size_t length, uint64_t* indexp);
template bool
js::StringIsTypedArrayIndex(const Latin1Char* s, size_t length, uint64_t* indexp);
+bool js::SetTypedArrayElement(JSContext* cx, Handle<TypedArrayObject*> obj,
+ uint64_t index, HandleValue v, ObjectOpResult& result)
+{
+ TypedArrayObject* tobj = &obj->as<TypedArrayObject>();
+
+ switch (tobj->type()) {
+#define SET_TYPED_ARRAY_ELEMENT(T, N) \
+ case Scalar::N: \
+ return TypedArrayObjectTemplate<T>::setElement(cx, obj, index, v, result);
+ JS_FOR_EACH_TYPED_ARRAY(SET_TYPED_ARRAY_ELEMENT)
+#undef SET_TYPED_ARRAY_ELEMENT
+ case Scalar::MaxTypedArrayViewType:
+ case Scalar::Int64:
+ break;
+ }
+
+ MOZ_CRASH("Unsupported TypedArray type");
+}
+
/* ES6 draft rev 34 (2015 Feb 20) 9.4.5.3 [[DefineOwnProperty]] step 3.c. */
bool
js::DefineTypedArrayElement(JSContext* cx, HandleObject obj, uint64_t index,
@@ -3042,22 +3365,18 @@ js::DefineTypedArrayElement(JSContext* cx, HandleObject obj, uint64_t index,
// Step x.
if (desc.hasValue()) {
- // The following step numbers refer to 9.4.5.9
- // IntegerIndexedElementSet.
-
- // Steps 1-2 are enforced by the caller.
-
- // Step 3.
- double numValue;
- if (!ToNumber(cx, desc.value(), &numValue))
- return false;
-
- // Steps 4-5, 8-9.
- if (obj->as<TypedArrayObject>().hasDetachedBuffer())
- return result.fail(JSMSG_TYPED_ARRAY_DETACHED);
-
- // Steps 10-16.
- TypedArrayObject::setElement(obj->as<TypedArrayObject>(), index, numValue);
+ TypedArrayObject* tobj = &obj->as<TypedArrayObject>();
+ switch (tobj->type()) {
+#define DEFINE_TYPED_ARRAY_ELEMENT(T, N) \
+ case Scalar::N: \
+ return TypedArrayObjectTemplate<T>::defineElement(cx, obj, index, \
+ desc.value(), result);
+ JS_FOR_EACH_TYPED_ARRAY(DEFINE_TYPED_ARRAY_ELEMENT)
+#undef DEFINE_TYPED_ARRAY_ELEMENT
+ case Scalar::MaxTypedArrayViewType:
+ case Scalar::Int64:
+ break;
+ }
}
// Step xii.
diff --git a/js/src/vm/TypedArrayObject.h b/js/src/vm/TypedArrayObject.h
index 2616402b3d..06aaea0617 100644
--- a/js/src/vm/TypedArrayObject.h
+++ b/js/src/vm/TypedArrayObject.h
@@ -24,7 +24,9 @@
macro(uint32_t, Uint32) \
macro(float, Float32) \
macro(double, Float64) \
- macro(uint8_clamped, Uint8Clamped)
+ macro(uint8_clamped, Uint8Clamped) \
+ macro(int64_t, BigInt64) \
+ macro(uint64_t, BigUint64)
typedef struct JSProperty JSProperty;
@@ -183,8 +185,12 @@ class TypedArrayObject : public NativeObject
void assertZeroLengthArrayData() const {};
#endif
- Value getElement(uint32_t index);
- static void setElement(TypedArrayObject& obj, uint32_t index, double d);
+ template <AllowGC allowGC>
+ bool getElement(ExclusiveContext* cx, uint32_t index,
+ typename MaybeRooted<Value, allowGC>::MutableHandleType val);
+ bool getElementPure(uint32_t index, Value* vp);
+
+ static bool getElements(JSContext* cx, Handle<TypedArrayObject*> tarray, Value* vp);
void notifyBufferDetached(JSContext* cx, void* newData);
@@ -306,6 +312,8 @@ class TypedArrayObject : public NativeObject
static bool is(HandleValue v);
static bool set(JSContext* cx, unsigned argc, Value* vp);
+
+ bool convertForSideEffect(JSContext* cx, HandleValue v) const;
};
MOZ_MUST_USE bool TypedArray_bufferGetter(JSContext* cx, unsigned argc, Value* vp);
@@ -373,6 +381,10 @@ IsTypedArrayIndex(jsid id, uint64_t* indexp)
return StringIsTypedArrayIndex(s, length, indexp);
}
+bool SetTypedArrayElement(JSContext* cx, Handle<TypedArrayObject*> obj,
+ uint64_t index, HandleValue v,
+ ObjectOpResult& result);
+
/*
* Implements [[DefineOwnProperty]] for TypedArrays when the property
* key is a TypedArray index.
@@ -396,6 +408,8 @@ TypedArrayShift(Scalar::Type viewType)
case Scalar::Uint32:
case Scalar::Float32:
return 2;
+ case Scalar::BigInt64:
+ case Scalar::BigUint64:
case Scalar::Int64:
case Scalar::Float64:
return 3;
@@ -442,7 +456,7 @@ class DataViewObject : public NativeObject
template <typename NativeType>
static uint8_t*
- getDataPointer(JSContext* cx, Handle<DataViewObject*> obj, double offset);
+ getDataPointer(JSContext* cx, Handle<DataViewObject*> obj, uint64_t offset);
template<Value ValueGetter(DataViewObject* view)>
static bool
@@ -521,6 +535,12 @@ class DataViewObject : public NativeObject
static bool getUint32Impl(JSContext* cx, const CallArgs& args);
static bool fun_getUint32(JSContext* cx, unsigned argc, Value* vp);
+ static bool getBigInt64Impl(JSContext* cx, const CallArgs& args);
+ static bool fun_getBigInt64(JSContext* cx, unsigned argc, Value* vp);
+
+ static bool getBigUint64Impl(JSContext* cx, const CallArgs& args);
+ static bool fun_getBigUint64(JSContext* cx, unsigned argc, Value* vp);
+
static bool getFloat32Impl(JSContext* cx, const CallArgs& args);
static bool fun_getFloat32(JSContext* cx, unsigned argc, Value* vp);
@@ -545,6 +565,12 @@ class DataViewObject : public NativeObject
static bool setUint32Impl(JSContext* cx, const CallArgs& args);
static bool fun_setUint32(JSContext* cx, unsigned argc, Value* vp);
+ static bool setBigInt64Impl(JSContext* cx, const CallArgs& args);
+ static bool fun_setBigInt64(JSContext* cx, unsigned argc, Value* vp);
+
+ static bool setBigUint64Impl(JSContext* cx, const CallArgs& args);
+ static bool fun_setBigUint64(JSContext* cx, unsigned argc, Value* vp);
+
static bool setFloat32Impl(JSContext* cx, const CallArgs& args);
static bool fun_setFloat32(JSContext* cx, unsigned argc, Value* vp);
@@ -564,6 +590,10 @@ class DataViewObject : public NativeObject
private:
static const JSFunctionSpec jsfuncs[];
+
+ static const JSFunctionSpec bigIntMethods[];
+ static bool finishInit(JSContext* cx, JS::HandleObject ctor,
+ JS::HandleObject proto);
};
static inline int32_t
diff --git a/js/src/vm/UbiNode.cpp b/js/src/vm/UbiNode.cpp
index ab966cbb4d..3a3b1ee2bf 100644
--- a/js/src/vm/UbiNode.cpp
+++ b/js/src/vm/UbiNode.cpp
@@ -23,6 +23,7 @@
#include "js/TypeDecls.h"
#include "js/Utility.h"
#include "js/Vector.h"
+#include "vm/BigIntType.h"
#include "vm/Debugger.h"
#include "vm/EnvironmentObject.h"
#include "vm/GlobalObject.h"
@@ -202,7 +203,11 @@ Node::exposeToJS() const
v.setString(as<JSString>());
} else if (is<JS::Symbol>()) {
v.setSymbol(as<JS::Symbol>());
- } else {
+ }
+ else if (is<BigInt>()) {
+ v.setBigInt(as<BigInt>());
+ }
+ else {
v.setUndefined();
}
@@ -314,6 +319,7 @@ template JS::Zone* TracerConcrete<js::ObjectGroup>::zone() const;
template JS::Zone* TracerConcrete<js::RegExpShared>::zone() const;
template JS::Zone* TracerConcrete<js::Scope>::zone() const;
template JS::Zone* TracerConcrete<JS::Symbol>::zone() const;
+template JS::Zone* TracerConcrete<BigInt>::zone() const;
template JS::Zone* TracerConcrete<JSString>::zone() const;
template<typename Referent>
@@ -337,6 +343,7 @@ template UniquePtr<EdgeRange> TracerConcrete<js::ObjectGroup>::edges(JSContext*
template UniquePtr<EdgeRange> TracerConcrete<js::RegExpShared>::edges(JSContext* cx, bool wantNames) const;
template UniquePtr<EdgeRange> TracerConcrete<js::Scope>::edges(JSContext* cx, bool wantNames) const;
template UniquePtr<EdgeRange> TracerConcrete<JS::Symbol>::edges(JSContext* cx, bool wantNames) const;
+template UniquePtr<EdgeRange> TracerConcrete<BigInt>::edges(JSContext* cx, bool wantNames) const;
template UniquePtr<EdgeRange> TracerConcrete<JSString>::edges(JSContext* cx, bool wantNames) const;
template<typename Referent>
@@ -392,6 +399,7 @@ Concrete<JSObject>::jsObjectConstructorName(JSContext* cx, UniqueTwoByteChars& o
}
const char16_t Concrete<JS::Symbol>::concreteTypeName[] = u"JS::Symbol";
+const char16_t Concrete<BigInt>::concreteTypeName[] = u"JS::BigInt";
const char16_t Concrete<JSScript>::concreteTypeName[] = u"JSScript";
const char16_t Concrete<js::LazyScript>::concreteTypeName[] = u"js::LazyScript";
const char16_t Concrete<js::jit::JitCode>::concreteTypeName[] = u"js::jit::JitCode";