Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix runtime creation #1960

Closed
wants to merge 13 commits into from
Closed

Fix runtime creation #1960

wants to merge 13 commits into from

Conversation

piaskowyk
Copy link
Member

@piaskowyk piaskowyk commented Apr 23, 2021

Description

In #1842 we use runtime factory from React Native. This factory can decorate runtime by chrome debugger - code here.

facebook::hermes::inspector::chrome::enableDebugging(std::move(adapter), "Hermes React Native");

This line registers runtime in the Metro server with the name Hermes React Native. When we use the same runtime factory to create reanimated runtime, we override React Native runtime in the Metro server because both runtimes have the same name. This happens just with Hermes runtime, so I changed the place where we create runtime and I moved the creation of Hermes runtime to C++ part. Now Metro server can see both runtimes.

Output form Metro server http://localhost:8081/json

json data
[
  {
    "id": "0-2",
    "description": "com.swmansion.reanimated.example",
    "title": "Reanimated Runtime",
    "faviconUrl": "https://reactjs.org/favicon.ico",
    "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=%5B%3A%3A%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D0%26page%3D2",
    "type": "node",
    "webSocketDebuggerUrl": "ws://[::]:8081/inspector/debug?device=0&page=2",
    "vm": "Hermes"
  },
  {
    "id": "0-1",
    "description": "com.swmansion.reanimated.example",
    "title": "Hermes React Native",
    "faviconUrl": "https://reactjs.org/favicon.ico",
    "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=%5B%3A%3A%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D0%26page%3D1",
    "type": "node",
    "webSocketDebuggerUrl": "ws://[::]:8081/inspector/debug?device=0&page=1",
    "vm": "Hermes"
  },
  {
    "id": "0--1",
    "description": "don't use",
    "title": "React Native Experimental (Improved Chrome Reloads)",
    "faviconUrl": "https://reactjs.org/favicon.ico",
    "devtoolsFrontendUrl": "chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=%5B%3A%3A%5D%3A8081%2Finspector%2Fdebug%3Fdevice%3D0%26page%3D-1",
    "type": "node",
    "webSocketDebuggerUrl": "ws://[::]:8081/inspector/debug?device=0&page=-1",
    "vm": "don't use"
  }
]

I decided to use getDefaultJSExecutorFactory() method from React Native, to avoid the compilation of Reanimated AAR's files for every JS engine. This a little tricky because I need access to the private method unfortunately but thanks to this we still need just 3 AARs. In additionally this PR should automatically add support for V8 runtime because the library with the V8 engine for React Native replacing the body of getDefaultJSExecutorFactory() method to method returning just V8 runtime - code here.
This PR is also the first big step to make Reanimated debuggable by Flipper or chrome debugger.

Fixes #1955
Fixes #1923

Update about V8:
I can start app with V8 runtime and use V8 as Reanimated runtime, but I found the issue in callWithThis() - this not set jsThis properly here, in effect ProxySetter and SharedValue don't work. This looks like bug in V8 implementation.

Checklist

  • Ensured that CI passes

@piaskowyk piaskowyk changed the title @piaskowyk/flipper Fix runtime creation Apr 23, 2021
@piaskowyk piaskowyk marked this pull request as ready for review May 7, 2021 09:48
@piaskowyk piaskowyk requested a review from Szymon20000 May 7, 2021 10:10
@piaskowyk piaskowyk self-assigned this May 7, 2021
@rplankenhorn
Copy link

@piaskowyk I tried pulling this PR down locally and running it but it didn't seem to fix the issue on Android. I still am getting a crash when connecting the Hermes Debugger in Flipper.

LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder
LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libreactnativejni
LOCAL_STATIC_LIBRARIES := libjsi callinvokerholder libhermes-inspector
LOCAL_SHARED_LIBRARIES := libhermes libfolly_json libfbjni libreactnativejni
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious that if adding libhermes here, will mark libhermes.so as NEEDED in libreanimated.so again?
For JSC apps to load libreanimated.so, there should be errors as libhermes.so is not packed into apk.

btw thank you @piaskowyk for the awesome work trying to decouple hermes in reanimated 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it won't work because RN doesn't provide libhermes.so if you choose to use jsc.

@@ -136,6 +144,7 @@ if(${FOR_HERMES})
${PACKAGE_NAME}
${LOG_LIB}
${HERMES_LIB}
${HERMES_INSPECTOR_LIB}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Hermes inspector is available only in debug build so we cannot merge this in the current shape.

@piaskowyk piaskowyk closed this May 25, 2021
@piaskowyk
Copy link
Member Author

I want to back to this in the future.

@piaskowyk piaskowyk mentioned this pull request May 26, 2021
1 task
piaskowyk added a commit that referenced this pull request May 26, 2021
## Description

Replacement for PR #1960

## Checklist

- [x] Ensured that CI passes
@Kudo
Copy link
Contributor

Kudo commented Oct 11, 2021

@piaskowyk i fixed the v8 callWithThis issue you reported. can you help to revisit the possibility of adding v8 support again when you get a chance?

here are some of my proposals:

  1. add v8 support as my fork and also ship v8 aar inside reanimated package.
  2. if reanimated can support user to build from source, for people want to use v8 + reanimated, they should build reanimated from source.
  3. i think your JSC for Android #1842 approach is still good for jsc and v8. maybe you can use makeHermesRuntime for hermes engine only and runtime factory approach as a generic solution for both jsc and v8.
  4. propose a RuntimeFactory from react-native core, so that we don't need differentiate hermes, jsc, and v8.

@mrousavy
Copy link
Contributor

Point 4. sounds very interesting. I've also thought about simply exposing their code to create a runtime instead, which can potentially be overwritten by other runtimes (v8, or more).

@jzxchiang1
Copy link

Just wanted to add that @Kudo has been extremely helpful, and I've tried his callWithThis fix and it finally makes reanimated v2 work for Android v8.

This would be huge for all of us v8 users. At a minimum, bundling the v8 AAR inside reanimated would be a great first step.

@DaniyarJakupov
Copy link

@piaskowyk sorry to bother you, but are there any updates on v8 support?

piaskowyk pushed a commit that referenced this pull request Sep 22, 2022
## Description

This PR is related to #1960 (and its replacement #2047). It adds the
ability to use Chrome DevTools to add breakpoints and debug worklets (or
the UI context in general) both on Android and iOS.

## Major changes

Runtime creation has been moved to `ReanimatedRuntime` for both Android
and iOS (in the `Common/cpp/ReanimatedRuntime` directory).

Before (Android) [file: `NativeProxy.cpp`]:
```cpp
#if JS_RUNTIME_HERMES
  auto config =
      ::hermes::vm::RuntimeConfig::Builder().withEnableSampleProfiling(false);
  std::shared_ptr<jsi::Runtime> animatedRuntime =
      facebook::hermes::makeHermesRuntime(config.build());
#elif JS_RUNTIME_V8
      …
#else
      …
#endif
```

After (shared) [file: `NativeProxy.cpp`]:
```cpp
ReanimatedRuntime::make(jsQueue);
```

The custom build config has been removed, as sample profiling is set to
false by default.

The created `HermesRuntimeManager` object is the stored inside
`ReanimatedNativeModule` as it is important that it’s lifetime is synced
with the lifetime of the module.

## Testing

- [x] Builds on Android
- [x] Builds on Android in release mode
- [x] App reloads work on Android
- [x] Builds on iOS
- [x] Builds on iOS in release mode
- [x] App reloads work on iOS
- [x] JSC still works
- [x] JSC debugging in Safari with iOS still works
- [x] Paper still works

Versions of React-Native tested: 0.70

## Things to look into

- ~~Breakpoint labels do not appear on iOS~~
_I tested this on a new app with Flipper on the main JS thread, and it
was also the case, so it seem to not be an issue on our side_
- ~~After removing a breakpoint on iOS it can't be set in the same
location again~~
_This seems to be an issue with Chrome DevTools as connecting to
Reanimated's runtime through Flipper fully works_
- ~~Edge-case: the app will crash if a reload is performed while the
debugger is open~~
_Will be fixed in a PR to metro and
[58e9e7b](https://github.com/software-mansion/react-native-reanimated/pull/3526/commits/58e9e7bcd7d3b6b590a5f6fca0db93a951eaa39e)_
- The latest release of Chrome (105) broke source maps, so only Flipper
works now. I reported the issue
[here](https://bugs.chromium.org/p/chromium/issues/detail?id=1360298#makechanges),
and I'm waiting for a response from the Chromium team

## TODO

- [x] Open PR to update debugging docs in documentation (will be
included in #3446)
- [x] Open PR in [facebook/flipper](https://github.com/facebook/flipper)
to enable Flipper support on custom runtimes (facebook/flipper#4047)
- [x] Open PR in [facebook/metro](https://github.com/facebook/metro) to
support debugger reloads on custom runtimes (facebook/metro#864)
@piaskowyk piaskowyk deleted the @piaskowyk/flipper branch January 10, 2023 15:40
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
…are-mansion#3526)

## Description

This PR is related to software-mansion#1960 (and its replacement software-mansion#2047). It adds the
ability to use Chrome DevTools to add breakpoints and debug worklets (or
the UI context in general) both on Android and iOS.

## Major changes

Runtime creation has been moved to `ReanimatedRuntime` for both Android
and iOS (in the `Common/cpp/ReanimatedRuntime` directory).

Before (Android) [file: `NativeProxy.cpp`]:
```cpp
#if JS_RUNTIME_HERMES
  auto config =
      ::hermes::vm::RuntimeConfig::Builder().withEnableSampleProfiling(false);
  std::shared_ptr<jsi::Runtime> animatedRuntime =
      facebook::hermes::makeHermesRuntime(config.build());
#elif JS_RUNTIME_V8
      …
#else
      …
#endif
```

After (shared) [file: `NativeProxy.cpp`]:
```cpp
ReanimatedRuntime::make(jsQueue);
```

The custom build config has been removed, as sample profiling is set to
false by default.

The created `HermesRuntimeManager` object is the stored inside
`ReanimatedNativeModule` as it is important that it’s lifetime is synced
with the lifetime of the module.

## Testing

- [x] Builds on Android
- [x] Builds on Android in release mode
- [x] App reloads work on Android
- [x] Builds on iOS
- [x] Builds on iOS in release mode
- [x] App reloads work on iOS
- [x] JSC still works
- [x] JSC debugging in Safari with iOS still works
- [x] Paper still works

Versions of React-Native tested: 0.70

## Things to look into

- ~~Breakpoint labels do not appear on iOS~~
_I tested this on a new app with Flipper on the main JS thread, and it
was also the case, so it seem to not be an issue on our side_
- ~~After removing a breakpoint on iOS it can't be set in the same
location again~~
_This seems to be an issue with Chrome DevTools as connecting to
Reanimated's runtime through Flipper fully works_
- ~~Edge-case: the app will crash if a reload is performed while the
debugger is open~~
_Will be fixed in a PR to metro and
[58e9e7b](https://github.com/software-mansion/react-native-reanimated/pull/3526/commits/58e9e7bcd7d3b6b590a5f6fca0db93a951eaa39e)_
- The latest release of Chrome (105) broke source maps, so only Flipper
works now. I reported the issue
[here](https://bugs.chromium.org/p/chromium/issues/detail?id=1360298#makechanges),
and I'm waiting for a response from the Chromium team

## TODO

- [x] Open PR to update debugging docs in documentation (will be
included in software-mansion#3446)
- [x] Open PR in [facebook/flipper](https://github.com/facebook/flipper)
to enable Flipper support on custom runtimes (facebook/flipper#4047)
- [x] Open PR in [facebook/metro](https://github.com/facebook/metro) to
support debugger reloads on custom runtimes (facebook/metro#864)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird error occurs rarely Reanimated 2.1.0 makes the Hermes Debugger in Flipper crash
8 participants