Skip to content

Commit c24b24a

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 d999f18 commit c24b24a

7 files changed

Lines changed: 24 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
@@ -7,6 +7,7 @@
77

88
#include "NativeCxxModuleExample.h"
99
#include <react/debug/react_native_assert.h>
10+
#include <cstdint>
1011
#include <iomanip>
1112
#include <ostream>
1213
#include <sstream>
@@ -265,4 +266,8 @@ AsyncPromise<> NativeCxxModuleExample::promiseAssert(jsi::Runtime& rt) {
265266
return promise;
266267
};
267268

269+
BigInt NativeCxxModuleExample::getBigInt(jsi::Runtime& /*rt*/, BigInt arg) {
270+
return arg;
271+
}
272+
268273
} // namespace facebook::react

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

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

183183
AsyncPromise<> promiseAssert(jsi::Runtime &rt);
184184

185+
BigInt getBigInt(jsi::Runtime &rt, BigInt arg);
186+
185187
private:
186188
std::optional<AsyncCallback<std::string>> valueCallback_;
187189
};

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ export interface Spec extends TurboModule {
114114
+voidFuncAssert: () => void;
115115
+getObjectAssert: (arg: ObjectStruct) => ObjectStruct;
116116
+promiseAssert: () => Promise<void>;
117+
+getBigInt: (arg: bigint) => bigint;
117118
}
118119

119120
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
@@ -50,6 +50,7 @@ type Examples =
5050
| 'getStrEnum'
5151
| 'getMap'
5252
| 'getNumber'
53+
| 'getBigInt'
5354
| 'getObject'
5455
| 'getSet'
5556
| 'getString'
@@ -122,7 +123,10 @@ class NativeCxxModuleExampleExample extends React.Component<{}, State> {
122123
}),
123124
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
124125
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
126+
getMap: () => NativeCxxModuleExample?.getMap({one: 1, two: 2, three: null}),
125127
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
128+
getBigInt: () =>
129+
NativeCxxModuleExample?.getBigInt(BigInt('9223372036854775807')),
126130
getObject: () =>
127131
NativeCxxModuleExample?.getObject({a: 1, b: 'foo', c: null}),
128132
getSet: () => NativeCxxModuleExample?.getSet([1, 1.1, 1.1, 1.1, 2]),
@@ -259,7 +263,9 @@ class NativeCxxModuleExampleExample extends React.Component<{}, State> {
259263
return (
260264
<View style={styles.result}>
261265
<RNTesterText style={[styles.value]}>
262-
{JSON.stringify(result.value)}
266+
{typeof result.value === 'bigint'
267+
? result.value.toString()
268+
: JSON.stringify(result.value)}
263269
</RNTesterText>
264270
<RNTesterText style={[styles.type]}>{result.type}</RNTesterText>
265271
</View>

0 commit comments

Comments
 (0)