Skip to content

Commit a4a47b9

Browse files
Emily Janzerfacebook-github-bot
Emily Janzer
authored andcommitted
Expose RuntimeExecutor on CatalystInstance (#28851)
Summary: Pull Request resolved: #28851 This diff creates a RuntimeExecutor that uses the bridge and exposes it on CatalystInstanceImpl. Changelog: [Internal] Reviewed By: mdvacca, RSNara Differential Revision: D21051949 fbshipit-source-id: b3977fc14fa19089f33e297d29cedba0d067526d
1 parent 13ee5c4 commit a4a47b9

File tree

14 files changed

+105
-3
lines changed

14 files changed

+105
-3
lines changed

RNTester/Podfile.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ PODS:
236236
- glog
237237
- React-callinvoker (= 1000.0.0)
238238
- React-jsinspector (= 1000.0.0)
239+
- React-runtimeexecutor (= 1000.0.0)
239240
- React-jsi (1000.0.0):
240241
- boost-for-react-native (= 1.63.0)
241242
- DoubleConversion
@@ -318,6 +319,8 @@ PODS:
318319
- React-Core/RCTVibrationHeaders (= 1000.0.0)
319320
- React-jsi (= 1000.0.0)
320321
- ReactCommon/turbomodule/core (= 1000.0.0)
322+
- React-runtimeexecutor (1000.0.0):
323+
- React-jsi (= 1000.0.0)
321324
- ReactCommon/turbomodule/core (1000.0.0):
322325
- DoubleConversion
323326
- Folly (= 2020.01.13.00)
@@ -388,6 +391,7 @@ DEPENDENCIES:
388391
- React-RCTTest (from `./RCTTest`)
389392
- React-RCTText (from `../Libraries/Text`)
390393
- React-RCTVibration (from `../Libraries/Vibration`)
394+
- React-runtimeexecutor (from `../ReactCommon/runtimeexecutor`)
391395
- ReactCommon/turbomodule/core (from `../ReactCommon`)
392396
- ReactCommon/turbomodule/samples (from `../ReactCommon`)
393397
- Yoga (from `../ReactCommon/yoga`)
@@ -462,6 +466,8 @@ EXTERNAL SOURCES:
462466
:path: "../Libraries/Text"
463467
React-RCTVibration:
464468
:path: "../Libraries/Vibration"
469+
React-runtimeexecutor:
470+
:path: "../ReactCommon/runtimeexecutor"
465471
ReactCommon:
466472
:path: "../ReactCommon"
467473
Yoga:
@@ -491,7 +497,7 @@ SPEC CHECKSUMS:
491497
React-callinvoker: 0dada022d38b73e6e15b33e2a96476153f79bbf6
492498
React-Core: 08c69f013e6fd654ea8f9fd84bbd66780a54d886
493499
React-CoreModules: d13d148c851af5780f864be74bc2165140923dc7
494-
React-cxxreact: 091da030e879ed93d970e95dd74fcbacb2a1d661
500+
React-cxxreact: b43a94e679b307660de530a3af872ab4c7d9925d
495501
React-jsi: fe94132da767bfc4801968c2a12abae43e9a833e
496502
React-jsiexecutor: 55eff40b2e0696e7a979016e321793ec8b28a2ac
497503
React-jsinspector: 7fbf9b42b58b02943a0d89b0ba9fff0070f2de98
@@ -506,6 +512,7 @@ SPEC CHECKSUMS:
506512
React-RCTTest: cfe25fcf70b04a747dba4326105db398250caa9a
507513
React-RCTText: 6c01963d3e562109f5548262b09b1b2bc260dd60
508514
React-RCTVibration: d42d73dafd9f63cf758656ee743aa80c566798ff
515+
React-runtimeexecutor: 60dd6204a13f68a1aa1118870edcc604a791df2b
509516
ReactCommon: 39e00b754f5e1628804fab28f44146d06280f700
510517
Yoga: f7fa200d8c49f97b54c9421079e781fb900b5cae
511518
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,18 @@ public interface CatalystInstance
9999
void setGlobalVariable(String propName, String jsonValue);
100100

101101
/**
102-
* Get the C pointer (as a long) to the JavaScriptCore context associated with this instance.
102+
* Do not use this anymore. Use {@link #getRuntimeExecutor()} instead. Get the C pointer (as a
103+
* long) to the JavaScriptCore context associated with this instance.
103104
*
104105
* <p>Use the following pattern to ensure that the JS context is not cleared while you are using
105106
* it: JavaScriptContextHolder jsContext = reactContext.getJavaScriptContextHolder()
106107
* synchronized(jsContext) { nativeThingNeedingJsContext(jsContext.get()); }
107108
*/
109+
@Deprecated
108110
JavaScriptContextHolder getJavaScriptContextHolder();
109111

112+
RuntimeExecutor getRuntimeExecutor();
113+
110114
void addJSIModules(List<JSIModuleSpec> jsiModules);
111115

112116
/**

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ public JavaScriptContextHolder getJavaScriptContextHolder() {
664664
return mJavaScriptContextHolder;
665665
}
666666

667+
@Override
668+
public native RuntimeExecutor getRuntimeExecutor();
669+
667670
@Override
668671
public void addJSIModules(List<JSIModuleSpec> jsiModules) {
669672
mJSIModuleRegistry.registerModules(jsiModules);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void CatalystInstanceImpl::registerNatives() {
131131
makeNativeMethod(
132132
"jniHandleMemoryPressure",
133133
CatalystInstanceImpl::handleMemoryPressure),
134+
makeNativeMethod(
135+
"getRuntimeExecutor", CatalystInstanceImpl::getRuntimeExecutor),
134136
});
135137

136138
JNativeRunnable::registerNatives();
@@ -326,5 +328,14 @@ CatalystInstanceImpl::getNativeCallInvokerHolder() {
326328
return nativeCallInvokerHolder_;
327329
}
328330

331+
jni::alias_ref<JRuntimeExecutor::javaobject>
332+
CatalystInstanceImpl::getRuntimeExecutor() {
333+
if (!runtimeExecutor_) {
334+
runtimeExecutor_ = jni::make_global(
335+
JRuntimeExecutor::newObjectCxxArgs(instance_->getRuntimeExecutor()));
336+
}
337+
return runtimeExecutor_;
338+
}
339+
329340
} // namespace react
330341
} // namespace facebook

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
#include <string>
1010

1111
#include <ReactCommon/CallInvokerHolder.h>
12+
#include <ReactCommon/RuntimeExecutor.h>
1213
#include <fbjni/fbjni.h>
1314

1415
#include "CxxModuleWrapper.h"
1516
#include "JMessageQueueThread.h"
17+
#include "JRuntimeExecutor.h"
1618
#include "JSLoader.h"
1719
#include "JavaModuleWrapper.h"
1820
#include "ModuleRegistryBuilder.h"
@@ -92,6 +94,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
9294
void jniCallJSCallback(jint callbackId, NativeArray *arguments);
9395
jni::alias_ref<CallInvokerHolder::javaobject> getJSCallInvokerHolder();
9496
jni::alias_ref<CallInvokerHolder::javaobject> getNativeCallInvokerHolder();
97+
jni::alias_ref<JRuntimeExecutor::javaobject> getRuntimeExecutor();
9598
void setGlobalVariable(std::string propName, std::string &&jsonValue);
9699
jlong getJavaScriptContext();
97100
void handleMemoryPressure(int pressureLevel);
@@ -103,6 +106,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
103106
std::shared_ptr<JMessageQueueThread> moduleMessageQueue_;
104107
jni::global_ref<CallInvokerHolder::javaobject> jsCallInvokerHolder_;
105108
jni::global_ref<CallInvokerHolder::javaobject> nativeCallInvokerHolder_;
109+
jni::global_ref<JRuntimeExecutor::javaobject> runtimeExecutor_;
106110
};
107111

108112
} // namespace react

ReactCommon/cxxreact/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LOCAL_CFLAGS := \
1919

2020
LOCAL_CFLAGS += -fexceptions -frtti -Wno-unused-lambda-capture
2121

22-
LOCAL_STATIC_LIBRARIES := boost jsi callinvoker
22+
LOCAL_STATIC_LIBRARIES := boost jsi callinvoker runtimeexecutor
2323
LOCAL_SHARED_LIBRARIES := jsinspector libfolly_json glog
2424

2525
include $(BUILD_STATIC_LIBRARY)

ReactCommon/cxxreact/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ rn_xplat_cxx_library(
154154
react_native_xplat_target("callinvoker:callinvoker"),
155155
react_native_xplat_target("jsinspector:jsinspector"),
156156
react_native_xplat_target("microprofiler:microprofiler"),
157+
react_native_xplat_target("runtimeexecutor:runtimeexecutor"),
157158
"//third-party/glog:glog",
158159
"//xplat/folly:optional",
159160
],

ReactCommon/cxxreact/Instance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ std::shared_ptr<CallInvoker> Instance::getJSCallInvoker() {
229229
return std::static_pointer_cast<CallInvoker>(jsCallInvoker_);
230230
}
231231

232+
RuntimeExecutor Instance::getRuntimeExecutor() {
233+
return nativeToJsBridge_->getRuntimeExecutor();
234+
}
235+
232236
std::shared_ptr<CallInvoker> Instance::getDecoratedNativeCallInvoker(
233237
std::shared_ptr<CallInvoker> nativeInvoker) {
234238
return nativeToJsBridge_->getDecoratedNativeCallInvoker(nativeInvoker);

ReactCommon/cxxreact/Instance.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <memory>
1313
#include <mutex>
1414

15+
#include <ReactCommon/RuntimeExecutor.h>
1516
#include <cxxreact/NativeToJsBridge.h>
1617

1718
#ifndef RN_EXPORT
@@ -129,6 +130,11 @@ class RN_EXPORT Instance {
129130
std::shared_ptr<CallInvoker> getDecoratedNativeCallInvoker(
130131
std::shared_ptr<CallInvoker> nativeInvoker);
131132

133+
/**
134+
* RuntimeExecutor is used by Fabric to access the jsi::Runtime.
135+
*/
136+
RuntimeExecutor getRuntimeExecutor();
137+
132138
private:
133139
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
134140
void loadBundle(

ReactCommon/cxxreact/NativeToJsBridge.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,22 @@ std::shared_ptr<CallInvoker> NativeToJsBridge::getDecoratedNativeCallInvoker(
331331
return std::make_shared<NativeCallInvoker>(m_delegate, nativeInvoker);
332332
}
333333

334+
RuntimeExecutor NativeToJsBridge::getRuntimeExecutor() {
335+
auto runtimeExecutor =
336+
[this, isDestroyed = m_destroyed](
337+
std::function<void(jsi::Runtime & runtime)> &&callback) {
338+
if (*isDestroyed) {
339+
return;
340+
}
341+
runOnExecutorQueue(
342+
[callback = std::move(callback)](JSExecutor *executor) {
343+
jsi::Runtime *runtime =
344+
(jsi::Runtime *)executor->getJavaScriptContext();
345+
callback(*runtime);
346+
});
347+
};
348+
return runtimeExecutor;
349+
}
350+
334351
} // namespace react
335352
} // namespace facebook

ReactCommon/cxxreact/NativeToJsBridge.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <vector>
1414

1515
#include <ReactCommon/CallInvoker.h>
16+
#include <ReactCommon/RuntimeExecutor.h>
1617
#include <cxxreact/JSExecutor.h>
1718

1819
namespace folly {
@@ -106,6 +107,12 @@ class NativeToJsBridge {
106107
std::shared_ptr<CallInvoker> getDecoratedNativeCallInvoker(
107108
std::shared_ptr<CallInvoker> nativeInvoker);
108109

110+
/**
111+
* RuntimeExecutor is used on Android to access the jsi::Runtime from Fabric
112+
* and TurboModules
113+
*/
114+
RuntimeExecutor getRuntimeExecutor();
115+
109116
private:
110117
// This is used to avoid a race condition where a proxyCallback gets queued
111118
// after ~NativeToJsBridge(), on the same thread. In that case, the callback

ReactCommon/cxxreact/React-cxxreact.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ Pod::Spec.new do |s|
4141
s.dependency "glog"
4242
s.dependency "React-jsinspector", version
4343
s.dependency "React-callinvoker", version
44+
s.dependency "React-runtimeexecutor", version
4445
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
require "json"
7+
8+
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
9+
version = package['version']
10+
11+
source = { :git => 'https://github.com/facebook/react-native.git' }
12+
if version == '1000.0.0'
13+
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
14+
source[:commit] = `git rev-parse HEAD`.strip
15+
else
16+
source[:tag] = "v#{version}"
17+
end
18+
19+
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
20+
folly_version = '2020.01.13.00'
21+
boost_compiler_flags = '-Wno-documentation'
22+
23+
Pod::Spec.new do |s|
24+
s.name = "React-runtimeexecutor"
25+
s.version = version
26+
s.summary = "-" # TODO
27+
s.homepage = "https://reactnative.dev/"
28+
s.license = package["license"]
29+
s.author = "Facebook, Inc. and its affiliates"
30+
s.platforms = { :ios => "10.0", :tvos => "10.0" }
31+
s.source = source
32+
s.source_files = "**/*.{cpp,h}"
33+
s.header_dir = "ReactCommon"
34+
35+
s.dependency "React-jsi", version
36+
end

scripts/react_native_pods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def use_react_native! (options={})
4141
pod 'React-jsiexecutor', :path => "#{prefix}/ReactCommon/jsiexecutor"
4242
pod 'React-jsinspector', :path => "#{prefix}/ReactCommon/jsinspector"
4343
pod 'React-callinvoker', :path => "#{prefix}/ReactCommon/callinvoker"
44+
pod 'React-runtimeexecutor', :path => "#{prefix}/ReactCommon/runtimeexecutor"
4445
pod 'ReactCommon/turbomodule/core', :path => "#{prefix}/ReactCommon"
4546
pod 'Yoga', :path => "#{prefix}/ReactCommon/yoga", :modular_headers => true
4647

0 commit comments

Comments
 (0)