Skip to content

Commit cff0d8e

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Register split segment paths with RAMBundleRegistry
Differential Revision: D6284466 fbshipit-source-id: c80cf929af38f92f06cca5b366c58785ae992d83
1 parent 820cfa1 commit cff0d8e

16 files changed

+62
-13
lines changed

React/CxxBridge/RCTCxxBridge.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ - (void)executeApplicationScript:(NSData *)script
11521152
? [[self.delegate jsSegmentsDirectory].path stringByAppendingString:@"/"]
11531153
: nil;
11541154
auto registry = jsSegmentsDirectory != nil
1155-
? RAMBundleRegistry::multipleBundlesRegistry(std::move(ramBundle), JSIndexedRAMBundle::buildFactory(jsSegmentsDirectory.UTF8String))
1155+
? RAMBundleRegistry::multipleBundlesRegistry(std::move(ramBundle), JSIndexedRAMBundle::buildFactory())
11561156
: RAMBundleRegistry::singleBundleRegistry(std::move(ramBundle));
11571157
self->_reactInstance->loadRAMBundle(std::move(registry), std::move(scriptStr),
11581158
sourceUrlStr.UTF8String, !async);

React/CxxBridge/RCTObjcExecutor.mm

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ void setBundleRegistry(std::unique_ptr<RAMBundleRegistry>) override {
9595
RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor");
9696
}
9797

98+
void registerBundle(uint32_t bundleId, const std::string &bundlePath) override {
99+
RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor");
100+
}
101+
98102
void callFunction(const std::string &module, const std::string &method,
99103
const folly::dynamic &arguments) override {
100104
[m_jse callFunctionOnModule:@(module.c_str())

ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
212212
auto script = bundle->getStartupCode();
213213
auto registry = jsSegmentsDirectory_.empty()
214214
? RAMBundleRegistry::singleBundleRegistry(std::move(bundle))
215-
: RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory(jsSegmentsDirectory_));
215+
: RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory());
216216
instance_->loadRAMBundle(
217217
std::move(registry),
218218
std::move(script),

ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ void ProxyExecutor::setBundleRegistry(std::unique_ptr<RAMBundleRegistry>) {
9090
"Loading application RAM bundles is not supported for proxy executors");
9191
}
9292

93+
void ProxyExecutor::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
94+
jni::throwNewJavaException(
95+
"java/lang/UnsupportedOperationException",
96+
"Loading application RAM bundles is not supported for proxy executors");
97+
}
98+
9399
void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
94100
auto call = folly::dynamic::array(moduleId, methodId, std::move(arguments));
95101

ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class ProxyExecutor : public JSExecutor {
3737
std::string sourceURL) override;
3838
virtual void setBundleRegistry(
3939
std::unique_ptr<RAMBundleRegistry> bundle) override;
40+
virtual void registerBundle(
41+
uint32_t bundleId, const std::string& bundlePath) override;
4042
virtual void callFunction(
4143
const std::string& moduleId,
4244
const std::string& methodId,

ReactCommon/cxxreact/Instance.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic &&params) {
156156
nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params));
157157
}
158158

159+
void Instance::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
160+
nativeToJsBridge_->registerBundle(bundleId, bundlePath);
161+
}
162+
159163
const ModuleRegistry &Instance::getModuleRegistry() const {
160164
return *moduleRegistry_;
161165
}

ReactCommon/cxxreact/Instance.h

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class RN_EXPORT Instance {
5959
folly::dynamic &&params);
6060
void callJSCallback(uint64_t callbackId, folly::dynamic &&params);
6161

62+
// This method is experimental, and may be modified or removed.
63+
void registerBundle(uint32_t bundleId, const std::string& bundlePath);
64+
6265
// This method is experimental, and may be modified or removed.
6366
template <typename T>
6467
Value callFunctionSync(const std::string &module, const std::string &method,

ReactCommon/cxxreact/JSCExecutor.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,12 @@ namespace facebook {
458458
m_bundleRegistry = std::move(bundleRegistry);
459459
}
460460

461+
void JSCExecutor::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
462+
if (m_bundleRegistry) {
463+
m_bundleRegistry->registerBundle(bundleId, bundlePath);
464+
}
465+
}
466+
461467
void JSCExecutor::bindBridge() throw(JSException) {
462468
SystraceSection s("JSCExecutor::bindBridge");
463469
std::call_once(m_bindFlag, [this] {

ReactCommon/cxxreact/JSCExecutor.h

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class RN_EXPORT JSCExecutor : public JSExecutor, public PrivateDataBase {
6666
std::string sourceURL) override;
6767

6868
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) override;
69+
virtual void registerBundle(uint32_t bundleId, const std::string& bundlePath) override;
6970

7071
virtual void callFunction(
7172
const std::string& moduleId,

ReactCommon/cxxreact/JSExecutor.h

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class JSExecutor {
5353
*/
5454
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) = 0;
5555

56+
/**
57+
* Register a file path for an additional "RAM" bundle
58+
*/
59+
virtual void registerBundle(uint32_t bundleId, const std::string& bundlePath) = 0;
60+
5661
/**
5762
* Executes BatchedBridge.callFunctionReturnFlushedQueue with the module ID,
5863
* method ID and optional additional arguments in JS. The executor is responsible

ReactCommon/cxxreact/JSIndexedRAMBundle.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
namespace facebook {
1010
namespace react {
1111

12-
std::function<std::unique_ptr<JSModulesUnbundle>(uint32_t)> JSIndexedRAMBundle::buildFactory(const std::string& baseDirectoryPath) {
13-
return [baseDirectoryPath](uint32_t index){
14-
std::string bundlePathById = baseDirectoryPath + toString(index) + ".jsbundle";
15-
return folly::make_unique<JSIndexedRAMBundle>(bundlePathById.c_str());
12+
std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> JSIndexedRAMBundle::buildFactory() {
13+
return [](const std::string& bundlePath){
14+
return folly::make_unique<JSIndexedRAMBundle>(bundlePath.c_str());
1615
};
1716
}
1817

ReactCommon/cxxreact/JSIndexedRAMBundle.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace react {
1717

1818
class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle {
1919
public:
20-
static std::function<std::unique_ptr<JSModulesUnbundle>(uint32_t)> buildFactory(const std::string& baseDirectoryPath);
20+
static std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> buildFactory();
2121

2222
// Throws std::runtime_error on failure.
2323
JSIndexedRAMBundle(const char *sourceURL);

ReactCommon/cxxreact/NativeToJsBridge.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ void NativeToJsBridge::invokeCallback(double callbackId, folly::dynamic&& argume
171171
});
172172
}
173173

174+
void NativeToJsBridge::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
175+
runOnExecutorQueue([bundleId, bundlePath] (JSExecutor* executor) {
176+
executor->registerBundle(bundleId, bundlePath);
177+
});
178+
}
179+
174180
void NativeToJsBridge::setGlobalVariable(std::string propName,
175181
std::unique_ptr<const JSBigString> jsonValue) {
176182
runOnExecutorQueue([propName=std::move(propName), jsonValue=folly::makeMoveWrapper(std::move(jsonValue))]

ReactCommon/cxxreact/NativeToJsBridge.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class NativeToJsBridge {
9898
std::unique_ptr<const JSBigString> startupCode,
9999
std::string sourceURL);
100100

101+
void registerBundle(uint32_t bundleId, const std::string& bundlePath);
101102
void setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue);
102103
void* getJavaScriptContext();
103104

ReactCommon/cxxreact/RAMBundleRegistry.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,30 @@ std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::singleBundleRegistry(std::
1616
return std::unique_ptr<RAMBundleRegistry>(registry);
1717
}
1818

19-
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::multipleBundlesRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(uint32_t)> factory) {
19+
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::multipleBundlesRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory) {
2020
RAMBundleRegistry *registry = new RAMBundleRegistry(std::move(mainBundle), std::move(factory));
2121
return std::unique_ptr<RAMBundleRegistry>(registry);
2222
}
2323

24-
RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(uint32_t)> factory): m_factory(factory) {
24+
RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory): m_factory(factory) {
2525
m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle));
2626
}
2727

28+
void RAMBundleRegistry::registerBundle(uint32_t bundleId, std::string bundlePath) {
29+
m_bundlePaths.emplace(bundleId, bundlePath);
30+
}
31+
2832
JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) {
2933
if (m_bundles.find(bundleId) == m_bundles.end()) {
3034
if (!m_factory) {
3135
throw std::runtime_error("You need to register factory function in order to support multiple RAM bundles.");
3236
}
33-
m_bundles.emplace(bundleId, m_factory(bundleId));
37+
38+
auto bundlePath = m_bundlePaths.find(bundleId);
39+
if (bundlePath == m_bundlePaths.end()) {
40+
throw std::runtime_error("In order to fetch RAM bundle from the registry, its file path needs to be registered first.");
41+
}
42+
m_bundles.emplace(bundleId, m_factory(bundlePath->second));
3443
}
3544

3645
return getBundle(bundleId)->getModule(moduleId);

ReactCommon/cxxreact/RAMBundleRegistry.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ namespace react {
2121
class RN_EXPORT RAMBundleRegistry : noncopyable {
2222
public:
2323
using unique_ram_bundle = std::unique_ptr<JSModulesUnbundle>;
24+
using bundle_path = std::string;
2425
constexpr static uint32_t MAIN_BUNDLE_ID = 0;
2526

2627
static std::unique_ptr<RAMBundleRegistry> singleBundleRegistry(unique_ram_bundle mainBundle);
27-
static std::unique_ptr<RAMBundleRegistry> multipleBundlesRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(uint32_t)> factory);
28+
static std::unique_ptr<RAMBundleRegistry> multipleBundlesRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(bundle_path)> factory);
2829

2930
RAMBundleRegistry(RAMBundleRegistry&&) = default;
3031
RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default;
3132

33+
void registerBundle(uint32_t bundleId, bundle_path bundlePath);
3234
JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId);
3335
virtual ~RAMBundleRegistry() {};
3436
private:
35-
explicit RAMBundleRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(uint32_t)> factory = {});
37+
explicit RAMBundleRegistry(unique_ram_bundle mainBundle, std::function<unique_ram_bundle(bundle_path)> factory = {});
3638
JSModulesUnbundle *getBundle(uint32_t bundleId) const;
3739

38-
std::function<unique_ram_bundle(uint32_t)> m_factory;
40+
std::function<unique_ram_bundle(bundle_path)> m_factory;
41+
std::unordered_map<uint32_t, bundle_path> m_bundlePaths;
3942
std::unordered_map<uint32_t, unique_ram_bundle> m_bundles;
4043
};
4144

0 commit comments

Comments
 (0)