Skip to content

Commit 73c3d4f

Browse files
committed
add bigint
1 parent 90f92c4 commit 73c3d4f

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

napi-inl.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ inline bool Value::IsNumber() const {
287287
return Type() == napi_number;
288288
}
289289

290+
inline bool Value::IsBigInt() const {
291+
return Type() == napi_bigint;
292+
}
293+
290294
inline bool Value::IsString() const {
291295
return Type() == napi_string;
292296
}
@@ -505,6 +509,67 @@ inline double Number::DoubleValue() const {
505509
return result;
506510
}
507511

512+
////////////////////////////////////////////////////////////////////////////////
513+
// BigInt Class
514+
////////////////////////////////////////////////////////////////////////////////
515+
516+
inline BigInt BigInt::New(napi_env env, int64_t val) {
517+
napi_value value;
518+
napi_status status = napi_create_double(env, val, &value);
519+
NAPI_THROW_IF_FAILED(env, status, BigInt());
520+
return BigInt(env, value);
521+
}
522+
523+
inline BigInt BigInt::New(napi_env env, uint64_t val) {
524+
napi_value value;
525+
napi_status status = napi_create_bigint_uint64(env, val, &value);
526+
NAPI_THROW_IF_FAILED(env, status, BigInt());
527+
return BigInt(env, value);
528+
}
529+
530+
inline BigInt BigInt::New(napi_env env, int sign_bit, size_t word_count, uint64_t* words) {
531+
napi_value value;
532+
napi_status status = napi_create_bigint_words(env, sign_bit, word_count, words, &value);
533+
NAPI_THROW_IF_FAILED(env, status, BigInt());
534+
return BigInt(env, value);
535+
}
536+
537+
inline BigInt::BigInt() : Value() {
538+
}
539+
540+
inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {
541+
}
542+
543+
inline int64_t BigInt::Int64Value(bool* lossless) const {
544+
int64_t result;
545+
napi_status status = napi_get_value_bigint_int64(
546+
_env, _value, &result, lossless);
547+
NAPI_THROW_IF_FAILED(_env, status, 0);
548+
return result;
549+
}
550+
551+
inline uint64_t BigInt::Uint64Value(bool* lossless) const {
552+
uint64_t result;
553+
napi_status status = napi_get_value_bigint_uint64(
554+
_env, _value, &result, lossless);
555+
NAPI_THROW_IF_FAILED(_env, status, 0);
556+
return result;
557+
}
558+
559+
inline size_t BigInt::WordCount() const {
560+
size_t word_count;
561+
napi_status status = napi_get_bigint_word_count(_env, _value, &word_count);
562+
NAPI_THROW_IF_FAILED(_env, status, 0);
563+
return word_count;
564+
}
565+
566+
inline void BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words) {
567+
napi_status status = napi_get_value_bigint_words(_env, _value, sign_bit, wourd_count, words);
568+
NAPI_THROW_IF_FAILED(_env, status);
569+
}
570+
571+
inline void ToWords(int* sign_bit, size_t* word_count, uint64_t* words) {}
572+
508573
////////////////////////////////////////////////////////////////////////////////
509574
// Name class
510575
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace Napi {
5252
class Value;
5353
class Boolean;
5454
class Number;
55+
class BigInt;
5556
class String;
5657
class Object;
5758
class Array;
@@ -169,6 +170,7 @@ namespace Napi {
169170
bool IsNull() const; ///< Tests if a value is a null JavaScript value.
170171
bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
171172
bool IsNumber() const; ///< Tests if a value is a JavaScript number.
173+
bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
172174
bool IsString() const; ///< Tests if a value is a JavaScript string.
173175
bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
174176
bool IsArray() const; ///< Tests if a value is a JavaScript array.
@@ -240,6 +242,48 @@ namespace Napi {
240242
double DoubleValue() const; ///< Converts a Number value to a 64-bit floating-point value.
241243
};
242244

245+
/// A JavaScript bigint value.
246+
class BigInt : public Value {
247+
public:
248+
static BigInt New(
249+
napi_env env, ///< N-API environment
250+
int64_t value, ///< Number value
251+
);
252+
static BigInt New(
253+
napi_env env, ///< N-API environment
254+
uint64_t value, ///< Number value
255+
);
256+
257+
/// Creates a new BigInt object using a specified sign bit and a
258+
/// specified list of digits/words.
259+
/// The resulting number is calculated as:
260+
/// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
261+
static BigInt New(
262+
napi_env env, ///< N-API environment
263+
int sign_bit, ///< Sign bit. 1 if negative.
264+
size_t word_count, ///< Number of words in array
265+
const uint64_t* words, ///< Array of words
266+
);
267+
268+
BigInt(); ///< Creates a new _empty_ BigInt instance.
269+
BigInt(napi_env env, napi_value value); ///< Wraps a N-API value primitive.
270+
271+
operator int64_t(); ///< Converts a BigInt value to a 64-bit signed integer value.
272+
operator uint64_t(); ///< Converts a BigInt value to a 64-bit unsigned integer value.
273+
274+
int64_t Int64Value(bool* lossless); ///< Converts a BigInt value to a 64-bit signed integer value.
275+
uint64_t Uint64Value(bool* lossless); ///< Converts a BigInt value to a 64-bit unsigned integer value.
276+
277+
size_t WordCount(); ///< The number of 64-bit words needed to store the result of WriteWords();
278+
279+
/// Writes the contents of this BigInt to a specified memory location.
280+
/// `sign_bit` must be provided and will be set to 1 if this BigInt is negative.
281+
/// `*word_count` has to be initialized to the length of the `words` array.
282+
/// Upon return, it will be set to the actual number of words that would
283+
/// be needed to store this BigInt (i.e. the return value of `WordCount()`).
284+
void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
285+
};
286+
243287
/// A JavaScript string or symbol value (that can be used as a property name).
244288
class Name : public Value {
245289
public:

0 commit comments

Comments
 (0)