Skip to content

Commit 6f627f6

Browse files
simkafacebook-github-bot
authored andcommitted
Split loadApplicationScript into initializeRuntime and loadBundle (react#27844)
Summary: This is the first of three PRs related to enabling multi-bundle support in React Native. More details, motivation and reasoning behind it can be found in RFC [here](react-native-community/discussions-and-proposals#152). Logic responsible for installing globals was pulled out from `loadApplicationScript` to `initializeRuntime` since it should be ran only once, what was left was renamed to `loadBundle`. It's based on dratwas work from [here](https://github.com/callstack/react-native/tree/feat/multibundle/split-load-application), but applied to current `master` to avoid rebasing 3-months old branch and issues that come with that. ## Changelog [Internal] [Changed] - split `loadApplicationScript` into `initializeRuntime` and `loadBundle` to enable multi-bundle support in the future Pull Request resolved: react#27844 Test Plan: Initialized new RN app with CLI, set RN to build from source and verified the still app builds and runs OK using code from this branch. Reviewed By: rickhanlonii Differential Revision: D19888605 Pulled By: ejanzer fbshipit-source-id: 24ace48ffe8978796591fe7c6cf53a61b127cce6
1 parent eab7fc0 commit 6f627f6

15 files changed

Lines changed: 80 additions & 48 deletions

File tree

React/CxxBridge/RCTCxxBridge.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ - (void)executeApplicationScript:(NSData *)script url:(NSURL *)url async:(BOOL)a
13451345
} else if (reactInstance) {
13461346
reactInstance->loadScriptFromString(std::make_unique<NSDataBigString>(script), sourceUrlStr.UTF8String, !async);
13471347
} else {
1348-
std::string methodName = async ? "loadApplicationScript" : "loadApplicationScriptSync";
1348+
std::string methodName = async ? "loadBundle" : "loadBundleSync";
13491349
throw std::logic_error("Attempt to call " + methodName + ": on uninitialized bridge");
13501350
}
13511351
}];

React/CxxBridge/RCTObjcExecutor.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@
6767
setGlobalVariable("__fbBatchedBridgeConfig", std::make_unique<JSBigStdString>(folly::toJson(config)));
6868
}
6969

70-
void loadApplicationScript(std::unique_ptr<const JSBigString> script, std::string sourceURL) override
70+
void initializeRuntime() override
71+
{
72+
// We do nothing here since initialization is done in the constructor
73+
}
74+
75+
void loadBundle(std::unique_ptr<const JSBigString> script, std::string sourceURL) override
7176
{
7277
RCTProfileBeginFlowEvent();
7378
[m_jse executeApplicationScript:[NSData dataWithBytes:script->c_str() length:script->size()]

ReactAndroid/src/main/java/com/facebook/react/bridge/JavaJSExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ProxyExecutorException(Throwable cause) {
3838
* @param sourceURL url or file location from which script content was loaded
3939
*/
4040
@DoNotStrip
41-
void loadApplicationScript(String sourceURL) throws ProxyExecutorException;
41+
void loadBundle(String sourceURL) throws ProxyExecutorException;
4242

4343
/**
4444
* Execute javascript method within js context

ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void prepareJSRuntime(JSDebuggerCallback callback) {
8181
}
8282
}
8383

84-
public void loadApplicationScript(
84+
public void loadBundle(
8585
String sourceURL, HashMap<String, String> injectedObjects, JSDebuggerCallback callback) {
8686
int requestID = mRequestID.getAndIncrement();
8787
mCallbacks.put(requestID, callback);

ReactAndroid/src/main/java/com/facebook/react/devsupport/WebsocketJavaScriptExecutor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,9 @@ public void close() {
153153
}
154154

155155
@Override
156-
public void loadApplicationScript(String sourceURL) throws JavaJSExecutor.ProxyExecutorException {
156+
public void loadBundle(String sourceURL) throws JavaJSExecutor.ProxyExecutorException {
157157
JSExecutorCallbackFuture callback = new JSExecutorCallbackFuture();
158-
Assertions.assertNotNull(mWebSocketClient)
159-
.loadApplicationScript(sourceURL, mInjectedObjects, callback);
158+
Assertions.assertNotNull(mWebSocketClient).loadBundle(sourceURL, mInjectedObjects, callback);
160159
try {
161160
callback.get();
162161
} catch (Throwable cause) {
@@ -178,7 +177,7 @@ public void loadApplicationScript(String sourceURL) throws JavaJSExecutor.ProxyE
178177

179178
@Override
180179
public void setGlobalVariable(String propertyName, String jsonEncodedValue) {
181-
// Store and use in the next loadApplicationScript() call.
180+
// Store and use in the next loadBundle() call.
182181
mInjectedObjects.put(propertyName, jsonEncodedValue);
183182
}
184183
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ ProxyExecutor::~ProxyExecutor() {
5353
m_executor.reset();
5454
}
5555

56-
void ProxyExecutor::loadApplicationScript(
57-
std::unique_ptr<const JSBigString>,
58-
std::string sourceURL) {
56+
void ProxyExecutor::initializeRuntime() {
5957
folly::dynamic nativeModuleConfig = folly::dynamic::array;
6058

6159
{
@@ -76,14 +74,17 @@ void ProxyExecutor::loadApplicationScript(
7674
"__fbBatchedBridgeConfig",
7775
std::make_unique<JSBigStdString>(folly::toJson(config)));
7876
}
77+
}
7978

80-
static auto loadApplicationScript =
81-
jni::findClassStatic(EXECUTOR_BASECLASS)
82-
->getMethod<void(jstring)>("loadApplicationScript");
79+
void ProxyExecutor::loadBundle(
80+
std::unique_ptr<const JSBigString>,
81+
std::string sourceURL) {
82+
static auto loadBundle = jni::findClassStatic(EXECUTOR_BASECLASS)
83+
->getMethod<void(jstring)>("loadBundle");
8384

8485
// The proxy ignores the script data passed in.
8586

86-
loadApplicationScript(m_executor.get(), jni::make_jstring(sourceURL).get());
87+
loadBundle(m_executor.get(), jni::make_jstring(sourceURL).get());
8788
// We can get pending calls here to native but the queue will be drained when
8889
// we launch the application.
8990
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class ProxyExecutor : public JSExecutor {
3737
jni::global_ref<jobject> &&executorInstance,
3838
std::shared_ptr<ExecutorDelegate> delegate);
3939
virtual ~ProxyExecutor() override;
40-
virtual void loadApplicationScript(
40+
virtual void initializeRuntime() override;
41+
virtual void loadBundle(
4142
std::unique_ptr<const JSBigString> script,
4243
std::string sourceURL) override;
4344
virtual void setBundleRegistry(

ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void test_prepareJSRuntime_ShouldSendCorrectMessage() throws Exception {
4040
}
4141

4242
@Test
43-
public void test_loadApplicationScript_ShouldSendCorrectMessage() throws Exception {
43+
public void test_loadBundle_ShouldSendCorrectMessage() throws Exception {
4444
final JSDebuggerWebSocketClient.JSDebuggerCallback cb =
4545
PowerMockito.mock(JSDebuggerWebSocketClient.JSDebuggerCallback.class);
4646

@@ -49,7 +49,7 @@ public void test_loadApplicationScript_ShouldSendCorrectMessage() throws Excepti
4949
injectedObjects.put("key1", "value1");
5050
injectedObjects.put("key2", "value2");
5151

52-
client.loadApplicationScript("http://localhost:8080/index.js", injectedObjects, cb);
52+
client.loadBundle("http://localhost:8080/index.js", injectedObjects, cb);
5353
PowerMockito.verifyPrivate(client)
5454
.invoke(
5555
"sendMessage",

ReactCommon/cxxreact/Instance.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void Instance::initializeBridge(
4949
nativeToJsBridge_ = std::make_shared<NativeToJsBridge>(
5050
jsef.get(), moduleRegistry_, jsQueue, callback_);
5151

52+
nativeToJsBridge_->initializeRuntime();
53+
5254
/**
5355
* After NativeToJsBridge is created, the jsi::Runtime should exist.
5456
* Also, the JS message queue thread exists. So, it's safe to
@@ -64,33 +66,33 @@ void Instance::initializeBridge(
6466
CHECK(nativeToJsBridge_);
6567
}
6668

67-
void Instance::loadApplication(
69+
void Instance::loadBundle(
6870
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
6971
std::unique_ptr<const JSBigString> string,
7072
std::string sourceURL) {
7173
callback_->incrementPendingJSCalls();
72-
SystraceSection s("Instance::loadApplication", "sourceURL", sourceURL);
73-
nativeToJsBridge_->loadApplication(
74+
SystraceSection s("Instance::loadBundle", "sourceURL", sourceURL);
75+
nativeToJsBridge_->loadBundle(
7476
std::move(bundleRegistry), std::move(string), std::move(sourceURL));
7577
}
7678

77-
void Instance::loadApplicationSync(
79+
void Instance::loadBundleSync(
7880
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
7981
std::unique_ptr<const JSBigString> string,
8082
std::string sourceURL) {
8183
std::unique_lock<std::mutex> lock(m_syncMutex);
8284
m_syncCV.wait(lock, [this] { return m_syncReady; });
8385

84-
SystraceSection s("Instance::loadApplicationSync", "sourceURL", sourceURL);
85-
nativeToJsBridge_->loadApplicationSync(
86+
SystraceSection s("Instance::loadBundleSync", "sourceURL", sourceURL);
87+
nativeToJsBridge_->loadBundleSync(
8688
std::move(bundleRegistry), std::move(string), std::move(sourceURL));
8789
}
8890

8991
void Instance::setSourceURL(std::string sourceURL) {
9092
callback_->incrementPendingJSCalls();
9193
SystraceSection s("Instance::setSourceURL", "sourceURL", sourceURL);
9294

93-
nativeToJsBridge_->loadApplication(nullptr, nullptr, std::move(sourceURL));
95+
nativeToJsBridge_->loadBundle(nullptr, nullptr, std::move(sourceURL));
9496
}
9597

9698
void Instance::loadScriptFromString(
@@ -99,9 +101,9 @@ void Instance::loadScriptFromString(
99101
bool loadSynchronously) {
100102
SystraceSection s("Instance::loadScriptFromString", "sourceURL", sourceURL);
101103
if (loadSynchronously) {
102-
loadApplicationSync(nullptr, std::move(string), std::move(sourceURL));
104+
loadBundleSync(nullptr, std::move(string), std::move(sourceURL));
103105
} else {
104-
loadApplication(nullptr, std::move(string), std::move(sourceURL));
106+
loadBundle(nullptr, std::move(string), std::move(sourceURL));
105107
}
106108
}
107109

@@ -157,12 +159,12 @@ void Instance::loadRAMBundle(
157159
std::string startupScriptSourceURL,
158160
bool loadSynchronously) {
159161
if (loadSynchronously) {
160-
loadApplicationSync(
162+
loadBundleSync(
161163
std::move(bundleRegistry),
162164
std::move(startupScript),
163165
std::move(startupScriptSourceURL));
164166
} else {
165-
loadApplication(
167+
loadBundle(
166168
std::move(bundleRegistry),
167169
std::move(startupScript),
168170
std::move(startupScriptSourceURL));

ReactCommon/cxxreact/Instance.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class RN_EXPORT Instance {
4747
std::shared_ptr<MessageQueueThread> jsQueue,
4848
std::shared_ptr<ModuleRegistry> moduleRegistry);
4949

50+
void initializeRuntime();
51+
5052
void setSourceURL(std::string sourceURL);
5153

5254
void loadScriptFromString(
@@ -130,11 +132,11 @@ class RN_EXPORT Instance {
130132

131133
private:
132134
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
133-
void loadApplication(
135+
void loadBundle(
134136
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
135137
std::unique_ptr<const JSBigString> startupScript,
136138
std::string startupScriptSourceURL);
137-
void loadApplicationSync(
139+
void loadBundleSync(
138140
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
139141
std::unique_ptr<const JSBigString> startupScript,
140142
std::string startupScriptSourceURL);

0 commit comments

Comments
 (0)