Skip to content

Commit 11bae63

Browse files
jpportofacebook-github-bot
authored andcommitted
Add BigInt skeleton
Summary: Adds a jsi::BigInt skeleton. Changelog: [General][Added] jsi::BigInt Reviewed By: kodafb Differential Revision: D35706101 fbshipit-source-id: 435b108050279ff30953b3e209cdc2f0ff84f40b
1 parent b4e6a78 commit 11bae63

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

ReactCommon/jsi/JSCRuntime.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class JSCRuntime : public jsi::Runtime {
142142
};
143143

144144
PointerValue *cloneSymbol(const Runtime::PointerValue *pv) override;
145+
PointerValue *cloneBigInt(const Runtime::PointerValue *pv) override;
145146
PointerValue *cloneString(const Runtime::PointerValue *pv) override;
146147
PointerValue *cloneObject(const Runtime::PointerValue *pv) override;
147148
PointerValue *clonePropNameID(const Runtime::PointerValue *pv) override;
@@ -214,6 +215,7 @@ class JSCRuntime : public jsi::Runtime {
214215
size_t count) override;
215216

216217
bool strictEquals(const jsi::Symbol &a, const jsi::Symbol &b) const override;
218+
bool strictEquals(const jsi::BigInt &a, const jsi::BigInt &b) const override;
217219
bool strictEquals(const jsi::String &a, const jsi::String &b) const override;
218220
bool strictEquals(const jsi::Object &a, const jsi::Object &b) const override;
219221
bool instanceOf(const jsi::Object &o, const jsi::Function &f) override;
@@ -599,6 +601,11 @@ jsi::Runtime::PointerValue *JSCRuntime::cloneSymbol(
599601
return makeSymbolValue(symbol->sym_);
600602
}
601603

604+
jsi::Runtime::PointerValue *JSCRuntime::cloneBigInt(
605+
const Runtime::PointerValue *) {
606+
throw std::logic_error("Not implemented");
607+
}
608+
602609
jsi::Runtime::PointerValue *JSCRuntime::cloneString(
603610
const jsi::Runtime::PointerValue *pv) {
604611
if (!pv) {
@@ -1299,6 +1306,11 @@ bool JSCRuntime::strictEquals(const jsi::Symbol &a, const jsi::Symbol &b)
12991306
return ret;
13001307
}
13011308

1309+
bool JSCRuntime::strictEquals(const jsi::BigInt &a, const jsi::BigInt &b)
1310+
const {
1311+
throw std::logic_error("Not implemented");
1312+
}
1313+
13021314
bool JSCRuntime::strictEquals(const jsi::String &a, const jsi::String &b)
13031315
const {
13041316
return JSStringIsEqual(stringRef(a), stringRef(b));

ReactCommon/jsi/jsi/decorator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
154154
Runtime::PointerValue* cloneSymbol(const Runtime::PointerValue* pv) override {
155155
return plain_.cloneSymbol(pv);
156156
};
157+
Runtime::PointerValue* cloneBigInt(const Runtime::PointerValue* pv) override {
158+
return plain_.cloneBigInt(pv);
159+
};
157160
Runtime::PointerValue* cloneString(const Runtime::PointerValue* pv) override {
158161
return plain_.cloneString(pv);
159162
};
@@ -315,6 +318,9 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
315318
bool strictEquals(const Symbol& a, const Symbol& b) const override {
316319
return plain_.strictEquals(a, b);
317320
};
321+
bool strictEquals(const BigInt& a, const BigInt& b) const override {
322+
return plain_.strictEquals(a, b);
323+
};
318324
bool strictEquals(const String& a, const String& b) const override {
319325
return plain_.strictEquals(a, b);
320326
};

ReactCommon/jsi/jsi/jsi.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ std::string kindToString(const Value& v, Runtime* rt = nullptr) {
3232
return "a string";
3333
} else if (v.isSymbol()) {
3434
return "a symbol";
35+
} else if (v.isBigInt()) {
36+
return "a bigint";
3537
} else {
3638
assert(v.isObject() && "Expecting object.");
3739
return rt != nullptr && v.getObject(*rt).isFunction(*rt) ? "a function"
@@ -234,6 +236,8 @@ Value::Value(Runtime& runtime, const Value& other) : Value(other.kind_) {
234236
data_.number = other.data_.number;
235237
} else if (kind_ == SymbolKind) {
236238
new (&data_.pointer) Pointer(runtime.cloneSymbol(other.data_.pointer.ptr_));
239+
} else if (kind_ == BigIntKind) {
240+
new (&data_.pointer) Pointer(runtime.cloneBigInt(other.data_.pointer.ptr_));
237241
} else if (kind_ == StringKind) {
238242
new (&data_.pointer) Pointer(runtime.cloneString(other.data_.pointer.ptr_));
239243
} else if (kind_ >= ObjectKind) {
@@ -263,6 +267,10 @@ bool Value::strictEquals(Runtime& runtime, const Value& a, const Value& b) {
263267
return runtime.strictEquals(
264268
static_cast<const Symbol&>(a.data_.pointer),
265269
static_cast<const Symbol&>(b.data_.pointer));
270+
case BigIntKind:
271+
return runtime.strictEquals(
272+
static_cast<const BigInt&>(a.data_.pointer),
273+
static_cast<const BigInt&>(b.data_.pointer));
266274
case StringKind:
267275
return runtime.strictEquals(
268276
static_cast<const String&>(a.data_.pointer),
@@ -330,6 +338,24 @@ Symbol Value::asSymbol(Runtime& rt) && {
330338
return std::move(*this).getSymbol(rt);
331339
}
332340

341+
BigInt Value::asBigInt(Runtime& rt) const& {
342+
if (!isBigInt()) {
343+
throw JSError(
344+
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
345+
}
346+
347+
return getBigInt(rt);
348+
}
349+
350+
BigInt Value::asBigInt(Runtime& rt) && {
351+
if (!isBigInt()) {
352+
throw JSError(
353+
rt, "Value is " + kindToString(*this, &rt) + ", expected a BigInt");
354+
}
355+
356+
return std::move(*this).getBigInt(rt);
357+
}
358+
333359
String Value::asString(Runtime& rt) const& {
334360
if (!isString()) {
335361
throw JSError(

ReactCommon/jsi/jsi/jsi.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Runtime;
6868
class Pointer;
6969
class PropNameID;
7070
class Symbol;
71+
class BigInt;
7172
class String;
7273
class Object;
7374
class WeakObject;
@@ -240,6 +241,7 @@ class JSI_EXPORT Runtime {
240241
friend class Pointer;
241242
friend class PropNameID;
242243
friend class Symbol;
244+
friend class BigInt;
243245
friend class String;
244246
friend class Object;
245247
friend class WeakObject;
@@ -263,6 +265,7 @@ class JSI_EXPORT Runtime {
263265
};
264266

265267
virtual PointerValue* cloneSymbol(const Runtime::PointerValue* pv) = 0;
268+
virtual PointerValue* cloneBigInt(const Runtime::PointerValue* pv) = 0;
266269
virtual PointerValue* cloneString(const Runtime::PointerValue* pv) = 0;
267270
virtual PointerValue* cloneObject(const Runtime::PointerValue* pv) = 0;
268271
virtual PointerValue* clonePropNameID(const Runtime::PointerValue* pv) = 0;
@@ -337,6 +340,7 @@ class JSI_EXPORT Runtime {
337340
virtual void popScope(ScopeState*);
338341

339342
virtual bool strictEquals(const Symbol& a, const Symbol& b) const = 0;
343+
virtual bool strictEquals(const BigInt& a, const BigInt& b) const = 0;
340344
virtual bool strictEquals(const String& a, const String& b) const = 0;
341345
virtual bool strictEquals(const Object& a, const Object& b) const = 0;
342346

@@ -482,6 +486,18 @@ class JSI_EXPORT Symbol : public Pointer {
482486
friend class Value;
483487
};
484488

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+
485501
/// Represents a JS String. Movable, not copyable.
486502
class JSI_EXPORT String : public Pointer {
487503
public:
@@ -973,6 +989,7 @@ class JSI_EXPORT Value {
973989
/* implicit */ Value(T&& other) : Value(kindOf(other)) {
974990
static_assert(
975991
std::is_base_of<Symbol, T>::value ||
992+
std::is_base_of<BigInt, T>::value ||
976993
std::is_base_of<String, T>::value ||
977994
std::is_base_of<Object, T>::value,
978995
"Value cannot be implicitly move-constructed from this type");
@@ -995,6 +1012,11 @@ class JSI_EXPORT Value {
9951012
new (&data_.pointer) Symbol(runtime.cloneSymbol(sym.ptr_));
9961013
}
9971014

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+
9981020
/// Copies a String lvalue into a new JS value.
9991021
Value(Runtime& runtime, const String& str) : Value(StringKind) {
10001022
new (&data_.pointer) String(runtime.cloneString(str.ptr_));
@@ -1064,6 +1086,10 @@ class JSI_EXPORT Value {
10641086
return kind_ == StringKind;
10651087
}
10661088

1089+
bool isBigInt() const {
1090+
return kind_ == BigIntKind;
1091+
}
1092+
10671093
bool isSymbol() const {
10681094
return kind_ == SymbolKind;
10691095
}
@@ -1112,6 +1138,26 @@ class JSI_EXPORT Value {
11121138
Symbol asSymbol(Runtime& runtime) const&;
11131139
Symbol asSymbol(Runtime& runtime) &&;
11141140

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+
11151161
/// \return the String value, or asserts if not a string.
11161162
String getString(Runtime& runtime) const& {
11171163
assert(isString());
@@ -1164,6 +1210,7 @@ class JSI_EXPORT Value {
11641210
BooleanKind,
11651211
NumberKind,
11661212
SymbolKind,
1213+
BigIntKind,
11671214
StringKind,
11681215
ObjectKind,
11691216
PointerKind = SymbolKind,
@@ -1190,6 +1237,9 @@ class JSI_EXPORT Value {
11901237
constexpr static ValueKind kindOf(const Symbol&) {
11911238
return SymbolKind;
11921239
}
1240+
constexpr static ValueKind kindOf(const BigInt&) {
1241+
return BigIntKind;
1242+
}
11931243
constexpr static ValueKind kindOf(const String&) {
11941244
return StringKind;
11951245
}

0 commit comments

Comments
 (0)