Initial implementation of multiple RAM bundles registry

Differential Revision: D5850963

fbshipit-source-id: e1bd6d74953872d38e73a20f6d054905a7e4c80c
This commit is contained in:
Alex Dvornikov
2017-09-21 08:34:44 -07:00
committed by Facebook Github Bot
parent da2ea2601b
commit 2f952fbaac
6 changed files with 75 additions and 9 deletions

View File

@@ -22,6 +22,7 @@ LOCAL_SRC_FILES := \
ModuleRegistry.cpp \
NativeToJsBridge.cpp \
Platform.cpp \
RAMBundleRegistry.cpp \
LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)

View File

@@ -32,6 +32,7 @@
#include "JSModulesUnbundle.h"
#include "ModuleRegistry.h"
#include "Platform.h"
#include "RAMBundleRegistry.h"
#include "RecoverableError.h"
#include "SystraceSection.h"
@@ -354,10 +355,10 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
}
void JSCExecutor::setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle) {
if (!m_unbundle) {
if (!m_bundleRegistry) {
installNativeHook<&JSCExecutor::nativeRequire>("nativeRequire");
}
m_unbundle = std::move(unbundle);
m_bundleRegistry = folly::make_unique<RAMBundleRegistry>(std::move(unbundle));
}
void JSCExecutor::bindBridge() throw(JSException) {
@@ -550,8 +551,8 @@ void JSCExecutor::flushQueueImmediate(Value&& queue) {
m_delegate->callNativeModules(*this, folly::parseJson(queueStr), false);
}
void JSCExecutor::loadModule(uint32_t moduleId) {
auto module = m_unbundle->getModule(moduleId);
void JSCExecutor::loadModule(uint32_t bundleId, uint32_t moduleId) {
auto module = m_bundleRegistry->getModule(bundleId, moduleId);
auto sourceUrl = String::createExpectingAscii(m_context, module.name);
auto source = String::createExpectingAscii(m_context, module.code);
evaluateScript(m_context, source, sourceUrl);
@@ -574,9 +575,10 @@ JSValueRef JSCExecutor::getNativeModule(JSObjectRef object, JSStringRef property
JSValueRef JSCExecutor::nativeRequire(
size_t argumentCount,
const JSValueRef arguments[]) {
uint32_t moduleId = parseNativeRequireParameters(m_context, arguments, argumentCount).second;
uint32_t bundleId, moduleId;
std::tie(bundleId, moduleId) = parseNativeRequireParameters(m_context, arguments, argumentCount);
ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_START);
loadModule(moduleId);
loadModule(bundleId, moduleId);
ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_STOP);
return Value::makeUndefined(m_context);
}

View File

@@ -5,7 +5,6 @@
#include <cstdint>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <cxxreact/JSCNativeModules.h>
#include <cxxreact/JSExecutor.h>
@@ -24,6 +23,7 @@ namespace facebook {
namespace react {
class MessageQueueThread;
class RAMBundleRegistry;
class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory {
public:
@@ -106,7 +106,7 @@ private:
std::shared_ptr<ExecutorDelegate> m_delegate;
std::shared_ptr<bool> m_isDestroyed = std::shared_ptr<bool>(new bool(false));
std::shared_ptr<MessageQueueThread> m_messageQueueThread;
std::unique_ptr<JSModulesUnbundle> m_unbundle;
std::unique_ptr<RAMBundleRegistry> m_bundleRegistry;
JSCNativeModules m_nativeModules;
folly::dynamic m_jscConfig;
std::once_flag m_bindFlag;
@@ -125,7 +125,7 @@ private:
void callNativeModules(Value&&);
void flush();
void flushQueueImmediate(Value&&);
void loadModule(uint32_t moduleId);
void loadModule(uint32_t bundleId, uint32_t moduleId);
String adoptString(std::unique_ptr<const JSBigString>);

View File

@@ -0,0 +1,23 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "RAMBundleRegistry.h"
namespace facebook {
namespace react {
constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID;
RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle) {
m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle));
}
JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) {
return getBundle(bundleId)->getModule(moduleId);
}
JSModulesUnbundle *RAMBundleRegistry::getBundle(uint32_t bundleId) const {
return m_bundles.at(bundleId).get();
}
} // namespace react
} // namespace facebook

View File

@@ -0,0 +1,28 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <cstdint>
#include <memory>
#include <unordered_map>
#include <utility>
#include <cxxreact/JSModulesUnbundle.h>
namespace facebook {
namespace react {
class RAMBundleRegistry {
public:
constexpr static uint32_t MAIN_BUNDLE_ID = 0;
explicit RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle);
JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId);
private:
JSModulesUnbundle *getBundle(uint32_t bundleId) const;
std::unordered_map<uint32_t, std::unique_ptr<JSModulesUnbundle>> m_bundles;
};
} // namespace react
} // namespace facebook