Skip to content

Commit e8a273d

Browse files
christophpurrermeta-codesync[bot]
authored andcommitted
Add BigInt Support to C++ Turbo Module Example (#56017)
Summary: Pull Request resolved: #56017 This diff adds BigInt method support to the C++ sample Turbo Module (NativeCxxModuleExample), demonstrating how to use `facebook::react::BigInt` with the bridging layer. Changes: - Added getBigInt method to NativeCxxModuleExample.cpp/.h using `facebook::react::BigInt` as the C++ type - Added getBigInt to NativeCxxModuleExample.js spec - Added BigIntKind to TurboModule.h for the method dispatch infrastructure - Updated RCTTurboModule.mm to handle BigIntKind in the iOS TurboModule bridge - Fixed BigInt rendering in NativeCxxModuleExampleExample.js — JSON.stringify() cannot serialize BigInt, so bigint values are now rendered via .toString() The `facebook::react::BigInt` wrapper class (introduced in the bridging diff) stores values as `std::variant<int64_t, uint64_t>`, preserving signedness. The `Bridging<BigInt>` specialization handles conversion to/from `jsi::BigInt`. Example: ```cpp BigInt NativeCxxModuleExample::getBigInt(jsi::Runtime& rt, BigInt arg) { return arg; } ``` Changelog: [General][Added] - Add BigInt bridging support for Turbo Modules Differential Revision: D95232265
1 parent b5a868d commit e8a273d

13 files changed

Lines changed: 30 additions & 2 deletions

File tree

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ TurboModuleMethodValueKind getTurboModuleMethodValueKind(
2121
return NumberKind;
2222
} else if (value->isString()) {
2323
return StringKind;
24+
} else if (value->isBigInt()) {
25+
return BigIntKind;
2426
} else if (value->isObject()) {
2527
auto object = value->asObject(rt);
2628
if (object.isArray(rt)) {

packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum TurboModuleMethodValueKind {
2626
VoidKind,
2727
BooleanKind,
2828
NumberKind,
29+
BigIntKind,
2930
StringKind,
3031
ObjectKind,
3132
ArrayKind,

packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ TraceSection s(
526526
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: FunctionKind is not supported yet.");
527527
case PromiseKind:
528528
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: PromiseKind wasn't handled properly.");
529+
case BigIntKind: {
530+
returnValue = jsi::BigInt::fromInt64(runtime, [(NSNumber *)result longLongValue]);
531+
break;
532+
}
529533
}
530534

531535
return returnValue;
@@ -813,7 +817,8 @@ TraceSection s(
813817
case StringKind:
814818
case ObjectKind:
815819
case ArrayKind:
816-
case FunctionKind: {
820+
case FunctionKind:
821+
case BigIntKind: {
817822
id result = performMethodInvocation(runtime, true, methodName, inv, retainedObjectsForInvocation);
818823
TurboModulePerfLogger::syncMethodCallReturnConversionStart(moduleName, methodName);
819824
returnValue = convertReturnIdToJSIValue(runtime, methodName, returnType, result);

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "NativeCxxModuleExample.h"
99
#include <react/bridging/ArrayBuffer.h>
1010
#include <react/debug/react_native_assert.h>
11+
#include <cstdint>
1112
#include <iomanip>
1213
#include <numeric>
1314
#include <ostream>
@@ -327,4 +328,8 @@ AsyncPromise<> NativeCxxModuleExample::promiseAssert(jsi::Runtime& rt) {
327328
return promise;
328329
};
329330

331+
BigInt NativeCxxModuleExample::getBigInt(jsi::Runtime& /*rt*/, BigInt arg) {
332+
return arg;
333+
}
334+
330335
} // namespace facebook::react

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ class NativeCxxModuleExample : public NativeCxxModuleExampleCxxSpec<NativeCxxMod
190190

191191
AsyncPromise<> promiseAssert(jsi::Runtime &rt);
192192

193+
BigInt getBigInt(jsi::Runtime &rt, BigInt arg);
194+
193195
private:
194196
std::optional<AsyncCallback<std::string>> valueCallback_;
195197
};

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export interface Spec extends TurboModule {
118118
+voidFuncAssert: () => void;
119119
+getObjectAssert: (arg: ObjectStruct) => ObjectStruct;
120120
+promiseAssert: () => Promise<void>;
121+
+getBigInt: (arg: bigint) => bigint;
121122
}
122123

123124
export default TurboModuleRegistry.get<Spec>(

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type Examples =
5353
| 'getStrEnum'
5454
| 'getMap'
5555
| 'getNumber'
56+
| 'getBigInt'
5657
| 'getObject'
5758
| 'getSet'
5859
| 'getString'
@@ -150,7 +151,10 @@ class NativeCxxModuleExampleExample extends React.Component<{}, State> {
150151
}),
151152
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
152153
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
154+
getMap: () => NativeCxxModuleExample?.getMap({one: 1, two: 2, three: null}),
153155
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
156+
getBigInt: () =>
157+
NativeCxxModuleExample?.getBigInt(BigInt('9223372036854775807')),
154158
getObject: () =>
155159
NativeCxxModuleExample?.getObject({a: 1, b: 'foo', c: null}),
156160
getSet: () => NativeCxxModuleExample?.getSet([1, 1.1, 1.1, 1.1, 2]),
@@ -287,7 +291,9 @@ class NativeCxxModuleExampleExample extends React.Component<{}, State> {
287291
return (
288292
<View style={styles.result}>
289293
<RNTesterText style={[styles.value]}>
290-
{JSON.stringify(result.value)}
294+
{typeof result.value === 'bigint'
295+
? result.value.toString()
296+
: JSON.stringify(result.value)}
291297
</RNTesterText>
292298
<RNTesterText style={[styles.type]}>{result.type}</RNTesterText>
293299
</View>

scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6592,6 +6592,7 @@ enum facebook::react::TransformOperationType : uint8_t {
65926592

65936593
enum facebook::react::TurboModuleMethodValueKind {
65946594
ArrayKind,
6595+
BigIntKind,
65956596
BooleanKind,
65966597
FunctionKind,
65976598
NumberKind,

scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6583,6 +6583,7 @@ enum facebook::react::TransformOperationType : uint8_t {
65836583

65846584
enum facebook::react::TurboModuleMethodValueKind {
65856585
ArrayKind,
6586+
BigIntKind,
65866587
BooleanKind,
65876588
FunctionKind,
65886589
NumberKind,

scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9184,6 +9184,7 @@ enum facebook::react::TransformOperationType : uint8_t {
91849184

91859185
enum facebook::react::TurboModuleMethodValueKind {
91869186
ArrayKind,
9187+
BigIntKind,
91879188
BooleanKind,
91889189
FunctionKind,
91899190
NumberKind,

0 commit comments

Comments
 (0)