@@ -68,6 +68,7 @@ class Runtime;
68
68
class Pointer ;
69
69
class PropNameID ;
70
70
class Symbol ;
71
+ class BigInt ;
71
72
class String ;
72
73
class Object ;
73
74
class WeakObject ;
@@ -240,6 +241,7 @@ class JSI_EXPORT Runtime {
240
241
friend class Pointer ;
241
242
friend class PropNameID ;
242
243
friend class Symbol ;
244
+ friend class BigInt ;
243
245
friend class String ;
244
246
friend class Object ;
245
247
friend class WeakObject ;
@@ -263,6 +265,7 @@ class JSI_EXPORT Runtime {
263
265
};
264
266
265
267
virtual PointerValue* cloneSymbol (const Runtime::PointerValue* pv) = 0;
268
+ virtual PointerValue* cloneBigInt (const Runtime::PointerValue* pv) = 0;
266
269
virtual PointerValue* cloneString (const Runtime::PointerValue* pv) = 0;
267
270
virtual PointerValue* cloneObject (const Runtime::PointerValue* pv) = 0;
268
271
virtual PointerValue* clonePropNameID (const Runtime::PointerValue* pv) = 0;
@@ -337,6 +340,7 @@ class JSI_EXPORT Runtime {
337
340
virtual void popScope (ScopeState*);
338
341
339
342
virtual bool strictEquals (const Symbol& a, const Symbol& b) const = 0;
343
+ virtual bool strictEquals (const BigInt& a, const BigInt& b) const = 0;
340
344
virtual bool strictEquals (const String& a, const String& b) const = 0;
341
345
virtual bool strictEquals (const Object& a, const Object& b) const = 0;
342
346
@@ -482,6 +486,18 @@ class JSI_EXPORT Symbol : public Pointer {
482
486
friend class Value ;
483
487
};
484
488
489
+ // / Represents a JS BigInt. Movable, not copyable.
490
+ class JSI_EXPORT BigInt : public Pointer {
491
+ public:
492
+ using Pointer::Pointer;
493
+
494
+ BigInt (BigInt&& other) = default ;
495
+ BigInt& operator =(BigInt&& other) = default ;
496
+
497
+ friend class Runtime ;
498
+ friend class Value ;
499
+ };
500
+
485
501
// / Represents a JS String. Movable, not copyable.
486
502
class JSI_EXPORT String : public Pointer {
487
503
public:
@@ -973,6 +989,7 @@ class JSI_EXPORT Value {
973
989
/* implicit */ Value(T&& other) : Value(kindOf(other)) {
974
990
static_assert (
975
991
std::is_base_of<Symbol, T>::value ||
992
+ std::is_base_of<BigInt, T>::value ||
976
993
std::is_base_of<String, T>::value ||
977
994
std::is_base_of<Object, T>::value,
978
995
" Value cannot be implicitly move-constructed from this type" );
@@ -995,6 +1012,11 @@ class JSI_EXPORT Value {
995
1012
new (&data_.pointer ) Symbol (runtime.cloneSymbol (sym.ptr_ ));
996
1013
}
997
1014
1015
+ // / Copies a BigInt lvalue into a new JS value.
1016
+ Value (Runtime& runtime, const BigInt& bigint) : Value(BigIntKind) {
1017
+ new (&data_.pointer ) BigInt (runtime.cloneBigInt (bigint.ptr_ ));
1018
+ }
1019
+
998
1020
// / Copies a String lvalue into a new JS value.
999
1021
Value (Runtime& runtime, const String& str) : Value(StringKind) {
1000
1022
new (&data_.pointer ) String (runtime.cloneString (str.ptr_ ));
@@ -1064,6 +1086,10 @@ class JSI_EXPORT Value {
1064
1086
return kind_ == StringKind;
1065
1087
}
1066
1088
1089
+ bool isBigInt () const {
1090
+ return kind_ == BigIntKind;
1091
+ }
1092
+
1067
1093
bool isSymbol () const {
1068
1094
return kind_ == SymbolKind;
1069
1095
}
@@ -1112,6 +1138,26 @@ class JSI_EXPORT Value {
1112
1138
Symbol asSymbol (Runtime& runtime) const &;
1113
1139
Symbol asSymbol (Runtime& runtime) &&;
1114
1140
1141
+ // / \return the BigInt value, or asserts if not a bigint.
1142
+ BigInt getBigInt (Runtime& runtime) const & {
1143
+ assert (isBigInt ());
1144
+ return BigInt (runtime.cloneBigInt (data_.pointer .ptr_ ));
1145
+ }
1146
+
1147
+ // / \return the BigInt value, or asserts if not a bigint.
1148
+ // / Can be used on rvalue references to avoid cloning more bigints.
1149
+ BigInt getBigInt (Runtime&) && {
1150
+ assert (isBigInt ());
1151
+ auto ptr = data_.pointer .ptr_ ;
1152
+ data_.pointer .ptr_ = nullptr ;
1153
+ return static_cast <BigInt>(ptr);
1154
+ }
1155
+
1156
+ // / \return the BigInt value, or throws JSIException if not a
1157
+ // / bigint
1158
+ BigInt asBigInt (Runtime& runtime) const &;
1159
+ BigInt asBigInt (Runtime& runtime) &&;
1160
+
1115
1161
// / \return the String value, or asserts if not a string.
1116
1162
String getString (Runtime& runtime) const & {
1117
1163
assert (isString ());
@@ -1164,6 +1210,7 @@ class JSI_EXPORT Value {
1164
1210
BooleanKind,
1165
1211
NumberKind,
1166
1212
SymbolKind,
1213
+ BigIntKind,
1167
1214
StringKind,
1168
1215
ObjectKind,
1169
1216
PointerKind = SymbolKind,
@@ -1190,6 +1237,9 @@ class JSI_EXPORT Value {
1190
1237
constexpr static ValueKind kindOf (const Symbol&) {
1191
1238
return SymbolKind;
1192
1239
}
1240
+ constexpr static ValueKind kindOf (const BigInt&) {
1241
+ return BigIntKind;
1242
+ }
1193
1243
constexpr static ValueKind kindOf (const String&) {
1194
1244
return StringKind;
1195
1245
}
0 commit comments