Skip to content

Commit

Permalink
fix: BigInts on hermes android were crashing during serialization for…
Browse files Browse the repository at this point in the history
… reactotron (#1498 by @markrickert)

## Please verify the following:

- [x] `yarn build-and-test:local` passes
- [x] I have added tests for any new features, if relevant
- [x] `README.md` (or relevant documentation) has been updated with your
changes

## Describe your PR

I though I had fixed this but we got more reports from the community
that it was still broken on android.

I added a few new buttons to the example app and was able to reproduce
the crash on my android emulator on expo 50 / rn 0.73.6.

With these changes, bigints show up properly in the reactotron app.
Closes #1436


![33A78A6F-C83B-4CA6-BDA2-8F3F6E1A5B16-75409-00059A133DB30F84](https://github.com/user-attachments/assets/43fc45a2-4194-4567-9d5d-8cb92ff0d2bf)

Co-authored-by: Frank Calise <[email protected]>
  • Loading branch information
markrickert and frankcalise authored Oct 30, 2024
1 parent 73bc5bc commit 3b64afb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
25 changes: 25 additions & 0 deletions apps/example-app/app/screens/LoggingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ export const LoggingScreen: React.FC<LoggingScreenProps> = function LoggingScree
console.error("This is a console.error()", { with: "possible data" })
}}
/>
<Button
text="console.log a BigInt"
textStyle={$darkText}
style={$button}
onPress={() => {
console.log(BigInt(0))
}}
/>
<Button
text="console.log a JSON Serialized BigInt"
textStyle={$darkText}
style={$button}
onPress={() => {
console.log(
JSON.stringify({
int: 12,
string: "hello",
function: function () {
console.log("HELLO!")
},
bigInt: BigInt(0),
})
)
}}
/>
</View>
<View style={$topContainer}>
<Text style={$text} tx="loggingsScreen.subtitle" />
Expand Down
1 change: 1 addition & 0 deletions apps/example-app/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (api) {
return {
assumptions: {
setPublicClassFields: true,
privateFieldsAsSymbols: true,
},
presets: ["babel-preset-expo"],
env: {
Expand Down
14 changes: 10 additions & 4 deletions lib/reactotron-core-client/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ const NEGATIVE_INFINITY = "~~~ -Infinity ~~~"

/**
* Fix BigInt serialization
* BigInts are not supported by JSON.stringify. This is a workaround.
* BigInts are not supported by JSON.stringify in Hermes android.
* This is a workaround.
* https://github.com/GoogleChromeLabs/jsbi/issues/30#issuecomment-953187833
* https://github.com/infinitered/reactotron/issues/1436
*/
declare global {
interface BigInt {
toJSON(): string
}
}
// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function () {
return this.toString()
if (typeof BigInt !== "undefined") {
// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function () {
return this.toString()
}
}

/**
Expand Down Expand Up @@ -89,6 +93,8 @@ function serialize(source: any, proxyHack = false) {
return value
case "number":
return value
case "bigint":
return value.toString()
case "function":
return getFunctionName(value)
}
Expand Down

0 comments on commit 3b64afb

Please sign in to comment.