V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
bigint.h
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_OBJECTS_BIGINT_H_
6 #define V8_OBJECTS_BIGINT_H_
7 
8 #include "src/globals.h"
9 #include "src/objects.h"
10 #include "src/utils.h"
11 
12 // Has to be the last include (doesn't have include guards):
13 #include "src/objects/object-macros.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 class BigInt;
19 class ValueDeserializer;
20 class ValueSerializer;
21 
22 // BigIntBase is just the raw data object underlying a BigInt. Use with care!
23 // Most code should be using BigInts instead.
24 class BigIntBase : public HeapObject {
25  public:
26  inline int length() const {
27  intptr_t bitfield = RELAXED_READ_INTPTR_FIELD(this, kBitfieldOffset);
28  return LengthBits::decode(static_cast<uint32_t>(bitfield));
29  }
30 
31  // For use by the GC.
32  inline int synchronized_length() const {
33  intptr_t bitfield = ACQUIRE_READ_INTPTR_FIELD(this, kBitfieldOffset);
34  return LengthBits::decode(static_cast<uint32_t>(bitfield));
35  }
36 
37  // Increasing kMaxLength will require code changes.
38  static const int kMaxLengthBits = kMaxInt - kPointerSize * kBitsPerByte - 1;
39  static const int kMaxLength = kMaxLengthBits / (kPointerSize * kBitsPerByte);
40 
41  // Sign and length are stored in the same bitfield. Since the GC needs to be
42  // able to read the length concurrently, the getters and setters are atomic.
43  static const int kLengthFieldBits = 30;
44  STATIC_ASSERT(kMaxLength <= ((1 << kLengthFieldBits) - 1));
45  class SignBits : public BitField<bool, 0, 1> {};
46  class LengthBits : public BitField<int, SignBits::kNext, kLengthFieldBits> {};
47  STATIC_ASSERT(LengthBits::kNext <= 32);
48 
49  static const int kBitfieldOffset = HeapObject::kHeaderSize;
50  static const int kDigitsOffset = kBitfieldOffset + kPointerSize;
51  static const int kHeaderSize = kDigitsOffset;
52 
53  private:
54  friend class ::v8::internal::BigInt; // MSVC wants full namespace.
55  friend class MutableBigInt;
56 
57  typedef uintptr_t digit_t;
58  static const int kDigitSize = sizeof(digit_t);
59  // kMaxLength definition assumes this:
60  STATIC_ASSERT(kDigitSize == kPointerSize);
61 
62  static const int kDigitBits = kDigitSize * kBitsPerByte;
63  static const int kHalfDigitBits = kDigitBits / 2;
64  static const digit_t kHalfDigitMask = (1ull << kHalfDigitBits) - 1;
65 
66  // sign() == true means negative.
67  inline bool sign() const {
68  intptr_t bitfield = RELAXED_READ_INTPTR_FIELD(this, kBitfieldOffset);
69  return SignBits::decode(static_cast<uint32_t>(bitfield));
70  }
71 
72  inline digit_t digit(int n) const {
73  SLOW_DCHECK(0 <= n && n < length());
74  Address address = FIELD_ADDR(this, kDigitsOffset + n * kDigitSize);
75  return *reinterpret_cast<digit_t*>(address);
76  }
77 
78  bool is_zero() const { return length() == 0; }
79 
80  DISALLOW_IMPLICIT_CONSTRUCTORS(BigIntBase);
81 };
82 
84  // This class is essentially the publicly accessible abstract version of
85  // MutableBigInt (which is a hidden implementation detail). It serves as
86  // the return type of Factory::NewBigInt, and makes it possible to enforce
87  // casting restrictions:
88  // - FreshlyAllocatedBigInt can be cast explicitly to MutableBigInt
89  // (with MutableBigInt::Cast) for initialization.
90  // - MutableBigInt can be cast/converted explicitly to BigInt
91  // (with MutableBigInt::MakeImmutable); is afterwards treated as readonly.
92  // - No accidental implicit casting is possible from BigInt to MutableBigInt
93  // (and no explicit operator is provided either).
94 
95  public:
96  inline static FreshlyAllocatedBigInt* cast(Object* object);
97 
98  private:
99  DISALLOW_IMPLICIT_CONSTRUCTORS(FreshlyAllocatedBigInt);
100 };
101 
102 // UNDER CONSTRUCTION!
103 // Arbitrary precision integers in JavaScript.
104 class V8_EXPORT_PRIVATE BigInt : public BigIntBase {
105  public:
106  // Implementation of the Spec methods, see:
107  // https://tc39.github.io/proposal-bigint/#sec-numeric-types
108  // Sections 1.1.1 through 1.1.19.
109  static Handle<BigInt> UnaryMinus(Isolate* isolate, Handle<BigInt> x);
110  static MaybeHandle<BigInt> BitwiseNot(Isolate* isolate, Handle<BigInt> x);
111  static MaybeHandle<BigInt> Exponentiate(Isolate* isolate, Handle<BigInt> base,
112  Handle<BigInt> exponent);
113  static MaybeHandle<BigInt> Multiply(Isolate* isolate, Handle<BigInt> x,
114  Handle<BigInt> y);
115  static MaybeHandle<BigInt> Divide(Isolate* isolate, Handle<BigInt> x,
116  Handle<BigInt> y);
117  static MaybeHandle<BigInt> Remainder(Isolate* isolate, Handle<BigInt> x,
118  Handle<BigInt> y);
119  static MaybeHandle<BigInt> Add(Isolate* isolate, Handle<BigInt> x,
120  Handle<BigInt> y);
121  static MaybeHandle<BigInt> Subtract(Isolate* isolate, Handle<BigInt> x,
122  Handle<BigInt> y);
123  static MaybeHandle<BigInt> LeftShift(Isolate* isolate, Handle<BigInt> x,
124  Handle<BigInt> y);
125  static MaybeHandle<BigInt> SignedRightShift(Isolate* isolate,
126  Handle<BigInt> x,
127  Handle<BigInt> y);
128  static MaybeHandle<BigInt> UnsignedRightShift(Isolate* isolate,
129  Handle<BigInt> x,
130  Handle<BigInt> y);
131  // More convenient version of "bool LessThan(x, y)".
132  static ComparisonResult CompareToBigInt(Handle<BigInt> x, Handle<BigInt> y);
133  static bool EqualToBigInt(BigInt* x, BigInt* y);
134  static MaybeHandle<BigInt> BitwiseAnd(Isolate* isolate, Handle<BigInt> x,
135  Handle<BigInt> y);
136  static MaybeHandle<BigInt> BitwiseXor(Isolate* isolate, Handle<BigInt> x,
137  Handle<BigInt> y);
138  static MaybeHandle<BigInt> BitwiseOr(Isolate* isolate, Handle<BigInt> x,
139  Handle<BigInt> y);
140 
141  // Other parts of the public interface.
142  static MaybeHandle<BigInt> Increment(Isolate* isolate, Handle<BigInt> x);
143  static MaybeHandle<BigInt> Decrement(Isolate* isolate, Handle<BigInt> x);
144 
145  bool ToBoolean() { return !is_zero(); }
146  uint32_t Hash() {
147  // TODO(jkummerow): Improve this. At least use length and sign.
148  return is_zero() ? 0 : ComputeLongHash(static_cast<uint64_t>(digit(0)));
149  }
150 
151  static bool EqualToString(Isolate* isolate, Handle<BigInt> x,
152  Handle<String> y);
153  static bool EqualToNumber(Handle<BigInt> x, Handle<Object> y);
154  static ComparisonResult CompareToString(Isolate* isolate, Handle<BigInt> x,
155  Handle<String> y);
156  static ComparisonResult CompareToNumber(Handle<BigInt> x, Handle<Object> y);
157  // Exposed for tests, do not call directly. Use CompareToNumber() instead.
158  static ComparisonResult CompareToDouble(Handle<BigInt> x, double y);
159 
160  static Handle<BigInt> AsIntN(Isolate* isolate, uint64_t n, Handle<BigInt> x);
161  static MaybeHandle<BigInt> AsUintN(Isolate* isolate, uint64_t n,
162  Handle<BigInt> x);
163 
164  static Handle<BigInt> FromInt64(Isolate* isolate, int64_t n);
165  static Handle<BigInt> FromUint64(Isolate* isolate, uint64_t n);
166  static MaybeHandle<BigInt> FromWords64(Isolate* isolate, int sign_bit,
167  int words64_count,
168  const uint64_t* words);
169  int64_t AsInt64(bool* lossless = nullptr);
170  uint64_t AsUint64(bool* lossless = nullptr);
171  int Words64Count();
172  void ToWordsArray64(int* sign_bit, int* words64_count, uint64_t* words);
173 
174  DECL_CAST(BigInt)
175  DECL_VERIFIER(BigInt)
176  DECL_PRINTER(BigInt)
177  void BigIntShortPrint(std::ostream& os);
178 
179  inline static int SizeFor(int length) {
180  return kHeaderSize + length * kDigitSize;
181  }
182 
183  static MaybeHandle<String> ToString(Isolate* isolate, Handle<BigInt> bigint,
184  int radix = 10,
185  ShouldThrow should_throw = kThrowOnError);
186  // "The Number value for x", see:
187  // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type
188  // Returns a Smi or HeapNumber.
189  static Handle<Object> ToNumber(Isolate* isolate, Handle<BigInt> x);
190 
191  // ECMAScript's NumberToBigInt
192  static MaybeHandle<BigInt> FromNumber(Isolate* isolate,
193  Handle<Object> number);
194 
195  // ECMAScript's ToBigInt (throws for Number input)
196  static MaybeHandle<BigInt> FromObject(Isolate* isolate, Handle<Object> obj);
197 
198  class BodyDescriptor;
199 
200  private:
201  friend class StringToBigIntHelper;
202  friend class ValueDeserializer;
203  friend class ValueSerializer;
204 
205  // Special functions for StringToBigIntHelper:
206  static Handle<BigInt> Zero(Isolate* isolate);
207  static MaybeHandle<FreshlyAllocatedBigInt> AllocateFor(
208  Isolate* isolate, int radix, int charcount, ShouldThrow should_throw,
209  PretenureFlag pretenure);
210  static void InplaceMultiplyAdd(Handle<FreshlyAllocatedBigInt> x,
211  uintptr_t factor, uintptr_t summand);
212  static Handle<BigInt> Finalize(Handle<FreshlyAllocatedBigInt> x, bool sign);
213 
214  // Special functions for ValueSerializer/ValueDeserializer:
215  uint32_t GetBitfieldForSerialization() const;
216  static int DigitsByteLengthForBitfield(uint32_t bitfield);
217  // Expects {storage} to have a length of at least
218  // {DigitsByteLengthForBitfield(GetBitfieldForSerialization())}.
219  void SerializeDigits(uint8_t* storage);
220  V8_WARN_UNUSED_RESULT static MaybeHandle<BigInt> FromSerializedDigits(
221  Isolate* isolate, uint32_t bitfield, Vector<const uint8_t> digits_storage,
222  PretenureFlag pretenure);
223 
224  DISALLOW_IMPLICIT_CONSTRUCTORS(BigInt);
225 };
226 
227 } // namespace internal
228 } // namespace v8
229 
230 #include "src/objects/object-macros-undef.h"
231 
232 #endif // V8_OBJECTS_BIGINT_H_
Definition: libplatform.h:13