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

Can we load multiple hermes' bundles in a single VM? #98

Closed
RajeshBatth opened this issue Aug 30, 2019 · 14 comments
Closed

Can we load multiple hermes' bundles in a single VM? #98

RajeshBatth opened this issue Aug 30, 2019 · 14 comments

Comments

@RajeshBatth
Copy link

I'm implementing my own code-push solution, where I want to load one base-bundle(vendor dependencies like RN) and multiple module-bundles(module can be a feature-set) on demand, with this I can fetch from server & update individual module-bundles.

@tmikov
Copy link
Contributor

tmikov commented Aug 31, 2019

Yes, Hermes is designed to load multiple bundles in one VM instance.

@tmikov tmikov closed this as completed Aug 31, 2019
@RajeshBatth
Copy link
Author

RajeshBatth commented Sep 3, 2019

@tmikov Does this mean we can share JS context between bundles?,
Can we reference modules of one bundle from another bundle ?

@tmikov tmikov reopened this Sep 3, 2019
@tmikov
Copy link
Contributor

tmikov commented Sep 3, 2019

Yes. You can think about it like this: loading a bytecode bundle into an instance of the Hermes VM is exactly the same as loading a source file (for example by performing an eval() on it) in other VMs. In JSI it is done using Runtime::evaluateJavaScript(), which can accept either a pre-compiled bytecode bundle or a source file.

The second part of your question is whether you can reference modules from the other bundle. That depends on what packager you used, and is basically not up to Hermes.

@RajeshBatth
Copy link
Author

But with metro-bundler there's no way to generate bundled chunks, do you think we can use other bundlers like webpack and then transform the bundle-chunks with hermes-cli ?

@tmikov
Copy link
Contributor

tmikov commented Sep 5, 2019

Most likely. Hermes is agnostic to the bundler. Our compilation process doesn't change the semantic of the code, so if the bundler provides the functionality you need and depends only on standard JS features, it should just work with Hermes.

For example if it works when you load the separate chunks as source in JSC, it should similarly work in Hermes if you compile them separately and load them as bytecode.

@RajeshBatth
Copy link
Author

Thanks @tmikov, Will try this.

@RajeshBatth
Copy link
Author

Hi, I tried loading multiple bundles and I got this crash

019-09-08 16:58:30.850 25195-25195/com.makemytrip E/AndroidRuntime: FATAL EXCEPTION: main Process: com.makemytrip, PID: 25195 java.lang.RuntimeException: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'st.bundle' is packaged correctly for release. at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method) at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:220)

The first bundle was my application and it got loaded properly, the second bundle was just a file with console log

test.js
function hello() { console.log('Hello World'); } hello();

Bundle Commands

  • react-native bundle --platform android --dev false --verbose --entry-file test.js --bundle-output test.bundle --reset-cache
  • node_modules/hermesvm/osx-bin/hermes -emit-binary -out hermes_test.bundle test.bundle -O
  • cp hermes_test.bundle ../mobile/src/main/assets/hermes_test.bundle

@tmikov
Copy link
Contributor

tmikov commented Sep 10, 2019

It seems that he problem you are describing is not in Hermes but in integrating multiple bundles with ReactNative, which is a bit out of my expertise. Hermes can load multiple bundles, but how to do that in ReactNative is a different question. Was this working with JSC?

@RajeshBatth
Copy link
Author

Yes, this was working with JSC, I think the problem is in optimizer, optimizer generates the bundle where all the allocations like(strings, functions) are put in a table, I'm guessing that a bundle can access the table of another bundle. Can this be the case?

@tmikov
Copy link
Contributor

tmikov commented Sep 11, 2019

@RajeshBatth there is no semantic difference between loading source code and loading a bytecode bundle, except the latter is faster. The error you described isn't coming from Hermes, it happens before Hermes has even been invoked, as you can see here:

https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp#L62

Looking at the code, to get that error either the asset manager is missing, or it can't access the file.

@tmikov tmikov closed this as completed Sep 17, 2019
@nthtrung09it
Copy link

@RajeshBatth Hey, did you find a way to solve that problem? I'm trying to load base bundle containing common features, and other bundles containing just logic code.

@tmikov
Copy link
Contributor

tmikov commented Apr 19, 2021

@nthtrung09it my suggestion is to ask for help in React Native, since it isn't caused by Hermes per se.

@RajeshBatth
Copy link
Author

@nthtrung09it Yes, I was able to load multiple bundles. It was pretty straightforward.
Here is a POC: https://github.com/RajeshBatth/hermes-multibundle

Check this https://github.com/RajeshBatth/hermes-multibundle/blob/master/android/app/src/main/java/com/multibundle/ChunkLoaderModule.java#L64

mganandraj pushed a commit to mganandraj/hermes that referenced this issue Jun 22, 2022
- Disable Stats Timer (fixes facebook#92)
- Include revision as part of product version in addition to suffix (fixes facebook#91)
- Include license file in NuGet (fixes facebook#98)
- Add Source Link to linker (fixes facebook#102)

This is ported from the v0.11 branch. We bump the version up to 0.12 (even though no such version exists in upstream) to signal we're no longer compatible with 0.11 (and RN 0.68).
mganandraj pushed a commit to mganandraj/hermes that referenced this issue Jun 22, 2022
Include license metadata in NuGet (fixes facebook#98)
@krmao
Copy link

krmao commented Sep 29, 2022

with RajeshBatth's answers, the key is loadSynchronously muse be set to false
both loadScriptFromFile and loadScriptFromAsset load ok

catalystInstance.loadScriptFromAssets(application.assets, sourceUrl, false)
catalystInstance.loadScriptFromFile(bundleFileNameMustWithPath, sourceUrl, false)

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

No branches or pull requests

4 participants