ReactNative 源码分析4——ReactActivity之加载JSBundle

接着ReactInstanceManager.createReactContext方法继续讲

scss 复制代码
////ReactInstanceManager.createReactContext
private ReactApplicationContext createReactContext(
    JavaScriptExecutor jsExecutor, JSBundleLoader jsBundleLoader) {
    ...
    catalystInstance.runJSBundle();
    ...
}

最终会调用到如下 2 个方法进入C++层

arduino 复制代码
private native void jniLoadScriptFromAssets(
    AssetManager assetManager, String assetURL, boolean loadSynchronously);

private native void jniLoadScriptFromFile(
    String fileName, String sourceURL, boolean loadSynchronously);

jniLoadScriptFromFile是调用到instance_->loadScriptFromString,然后调用loadBundle

c 复制代码
void Instance::loadBundle(
    std::unique_ptr<RAMBundleRegistry> bundleRegistry,
    std::unique_ptr<const JSBigString> string,
    std::string sourceURL) {
  callback_->incrementPendingJSCalls();
  SystraceSection s( "Instance::loadBundle" , "sourceURL" , sourceURL);
  nativeToJsBridge_->loadBundle(
      std::move(bundleRegistry), std::move(string), std::move(sourceURL));
}
  • callback_是Java层的InstanceCallback实例,这里是通知Java层
  • nativeToJsBridge_是NativeToJsBridge对象
arduino 复制代码
void NativeToJsBridge::loadBundle(
    std::unique_ptr<RAMBundleRegistry> bundleRegistry,
    std::unique_ptr<const JSBigString> startupScript,
    std::string startupScriptSourceURL) {
  runOnExecutorQueue(
      [this,
       bundleRegistryWrap = makeMoveWrapper(std::move(bundleRegistry)),
       startupScript = makeMoveWrapper(std::move(startupScript)),
       startupScriptSourceURL =
           std::move(startupScriptSourceURL)](JSExecutor* executor) mutable {
        auto bundleRegistry = bundleRegistryWrap.move();
        if (bundleRegistry) {
          executor->setBundleRegistry(std::move(bundleRegistry));
        }
        try {
          executor->loadBundle(
              std::move(*startupScript), std::move(startupScriptSourceURL));
        } catch (...) {
          m_applicationScriptHasFailure = true;
          throw;
        }
      });
}
  • runOnExecutorQueue在JS线程中执行
  • executor是HermesExecutor对象
c 复制代码
void JSIExecutor::loadBundle(
    std::unique_ptr<const JSBigString> script,
    std::string sourceURL) {
  ...
  runtime_->evaluateJavaScript(
      std::make_unique<BigStringBuffer>(std::move(script)), sourceURL);
  ...
}
  • runtime_:DecoratedRuntime对象,它里面包装了HermesRuntime运行时

到这里其实就明确了,调用Hermes引擎去加载JSBundle

相关推荐
zhangjin11201 小时前
解决Android Studio gradle下载超时和缓慢问题(二)
android·ide·android studio
xingxiliang8 小时前
深入浅出 Android CTS 视频测试:从环境搭建、用例设计到底层通信原理
android·音视频
我命由我123458 小时前
Android 开发问题:为 PDFView 设置一个带有黑色边框的背景 drawable,但边框没有生效
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
雨白10 小时前
深入理解 Kotlin 协程 (八):拾遗补阙,探秘官方框架的调度细节与取消闭环
android·kotlin
海天鹰12 小时前
PHP上传文件
android·开发语言·php
2501_9159184113 小时前
详解iOS App上架至App Store的全流程步骤与注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
码农coding14 小时前
android12 SystemUI之StatusBar(二)
android
Lesile16 小时前
Interview#1 历史演进:MVC · MVP · MVVM · MVI架构详解
android·android jetpack
杉氧17 小时前
Flutter 像素级还原实战:用 CustomPaint 与 Bezier 曲线手绘精致图针
android·前端·flutter
我命由我1234519 小时前
Android 在构建过程中,发现有两个依赖库都包含了相同路径的资源文件
android·java·开发语言·java-ee·kotlin·android-studio·android runtime