Remove JsToNativeBridge's nativeQueue

Reviewed By: mhorowitz

Differential Revision: D4589737

fbshipit-source-id: 3b2730417d99c4f98cfaad386bc50328f2551592
This commit is contained in:
Pieter De Baets
2017-03-17 07:08:14 -07:00
committed by Facebook Github Bot
parent fbf6d1aaeb
commit d7b37c4050
13 changed files with 142 additions and 180 deletions
@@ -41,13 +41,17 @@ class Exception : public jni::JavaClass<Exception> {
class JInstanceCallback : public InstanceCallback {
public:
explicit JInstanceCallback(alias_ref<ReactCallback::javaobject> jobj)
: jobj_(make_global(jobj)) {}
explicit JInstanceCallback(
alias_ref<ReactCallback::javaobject> jobj,
std::shared_ptr<JMessageQueueThread> messageQueueThread)
: jobj_(make_global(jobj)), messageQueueThread_(std::move(messageQueueThread)) {}
void onBatchComplete() override {
static auto method =
ReactCallback::javaClassStatic()->getMethod<void()>("onBatchComplete");
method(jobj_);
messageQueueThread_->runOnQueue([this] {
static auto method =
ReactCallback::javaClassStatic()->getMethod<void()>("onBatchComplete");
method(jobj_);
});
}
void incrementPendingJSCalls() override {
@@ -61,6 +65,7 @@ class JInstanceCallback : public InstanceCallback {
}
void decrementPendingJSCalls() override {
jni::ThreadScope guard;
static auto method =
ReactCallback::javaClassStatic()->getMethod<void()>("decrementPendingJSCalls");
method(jobj_);
@@ -81,12 +86,11 @@ class JInstanceCallback : public InstanceCallback {
return jobj->cthis()->getExecutorToken(jobj);
}
void onExecutorStopped(ExecutorToken) override {
// TODO(cjhopman): implement this.
}
void onExecutorStopped(ExecutorToken) override {}
private:
global_ref<ReactCallback::javaobject> jobj_;
std::shared_ptr<JMessageQueueThread> messageQueueThread_;
};
}
@@ -97,7 +101,12 @@ jni::local_ref<CatalystInstanceImpl::jhybriddata> CatalystInstanceImpl::initHybr
}
CatalystInstanceImpl::CatalystInstanceImpl()
: instance_(folly::make_unique<Instance>()) {}
: instance_(folly::make_unique<Instance>()) {}
CatalystInstanceImpl::~CatalystInstanceImpl() {
// TODO: 16669252: this prevents onCatalystInstanceDestroy from being called
moduleMessageQueue_->quitSynchronous();
}
void CatalystInstanceImpl::registerNatives() {
registerHybrid({
@@ -127,11 +136,12 @@ void CatalystInstanceImpl::initializeBridge(
// This executor is actually a factory holder.
JavaScriptExecutorHolder* jseh,
jni::alias_ref<JavaMessageQueueThread::javaobject> jsQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> moduleQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> nativeModulesQueue,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules) {
// TODO mhorowitz: how to assert here?
// Assertions.assertCondition(mBridge == null, "initializeBridge should be called once");
moduleMessageQueue_ = std::make_shared<JMessageQueueThread>(nativeModulesQueue);
// This used to be:
//
@@ -149,12 +159,12 @@ void CatalystInstanceImpl::initializeBridge(
// don't need jsModuleDescriptions any more, all the way up and down the
// stack.
instance_->initializeBridge(folly::make_unique<JInstanceCallback>(callback),
jseh->getExecutorFactory(),
folly::make_unique<JMessageQueueThread>(jsQueue),
folly::make_unique<JMessageQueueThread>(moduleQueue),
buildModuleRegistry(std::weak_ptr<Instance>(instance_),
javaModules, cxxModules));
instance_->initializeBridge(
folly::make_unique<JInstanceCallback>(callback, moduleMessageQueue_),
jseh->getExecutorFactory(),
folly::make_unique<JMessageQueueThread>(jsQueue),
buildModuleRegistry(
std::weak_ptr<Instance>(instance_), javaModules, cxxModules, moduleMessageQueue_));
}
void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
@@ -28,6 +28,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/cxxbridge/CatalystInstanceImpl;";
static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jclass>);
~CatalystInstanceImpl() override;
static void registerNatives();
@@ -74,6 +75,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
// This should be the only long-lived strong reference, but every C++ class
// will have a weak reference.
std::shared_ptr<Instance> instance_;
std::shared_ptr<JMessageQueueThread> moduleMessageQueue_;
};
}}
@@ -87,30 +87,36 @@ bool JavaNativeModule::supportsWebWorkers() {
}
void JavaNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId, folly::dynamic&& params) {
static auto invokeMethod =
wrapper_->getClass()->getMethod<void(JExecutorToken::javaobject, jint, ReadableNativeArray::javaobject)>("invoke");
invokeMethod(wrapper_, JExecutorToken::extractJavaPartFromToken(token).get(), static_cast<jint>(reactMethodId),
ReadableNativeArray::newObjectCxxArgs(std::move(params)).get());
messageQueueThread_->runOnQueue([this, token, reactMethodId, params=std::move(params)] {
static auto invokeMethod = wrapper_->getClass()->
getMethod<void(JExecutorToken::javaobject, jint, ReadableNativeArray::javaobject)>("invoke");
invokeMethod(wrapper_,
JExecutorToken::extractJavaPartFromToken(token).get(),
static_cast<jint>(reactMethodId),
ReadableNativeArray::newObjectCxxArgs(std::move(params)).get());
});
}
MethodCallResult JavaNativeModule::callSerializableNativeHook(ExecutorToken token, unsigned int reactMethodId, folly::dynamic&& params) {
// TODO: evaluate whether calling through invoke is potentially faster
if (reactMethodId >= syncMethods_.size()) {
throw std::invalid_argument(
folly::to<std::string>("methodId ", reactMethodId, " out of range [0..", syncMethods_.size(), "]"));
}
// TODO: evaluate whether calling through invoke is potentially faster
if (reactMethodId >= syncMethods_.size()) {
throw std::invalid_argument(
folly::to<std::string>("methodId ", reactMethodId, " out of range [0..", syncMethods_.size(), "]"));
}
auto& method = syncMethods_[reactMethodId];
CHECK(method.hasValue() && method->isSyncHook()) << "Trying to invoke a asynchronous method as synchronous hook";
return method->invoke(instance_, wrapper_->getModule(), token, params);
auto& method = syncMethods_[reactMethodId];
CHECK(method.hasValue() && method->isSyncHook()) << "Trying to invoke a asynchronous method as synchronous hook";
return method->invoke(instance_, wrapper_->getModule(), token, params);
}
NewJavaNativeModule::NewJavaNativeModule(
std::weak_ptr<Instance> instance,
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper)
: instance_(std::move(instance)),
wrapper_(make_global(wrapper)),
module_(make_global(wrapper->getModule())) {
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper,
std::shared_ptr<MessageQueueThread> messageQueueThread)
: instance_(std::move(instance))
, wrapper_(make_global(wrapper))
, module_(make_global(wrapper->getModule()))
, messageQueueThread_(std::move(messageQueueThread)) {
auto descs = wrapper_->getMethodDescriptors();
std::string moduleName = getName();
methods_.reserve(descs->size());
@@ -161,7 +167,9 @@ void NewJavaNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId
folly::to<std::string>("methodId ", reactMethodId, " out of range [0..", methods_.size(), "]"));
}
CHECK(!methods_[reactMethodId].isSyncHook()) << "Trying to invoke a synchronous hook asynchronously";
invokeInner(token, reactMethodId, std::move(params));
messageQueueThread_->runOnQueue([this, token, reactMethodId, params=std::move(params)] () mutable {
invokeInner(token, reactMethodId, std::move(params));
});
}
MethodCallResult NewJavaNativeModule::callSerializableNativeHook(ExecutorToken token, unsigned int reactMethodId, folly::dynamic&& params) {
@@ -12,6 +12,7 @@ namespace facebook {
namespace react {
class Instance;
class MessageQueueThread;
struct JMethodDescriptor : public jni::JavaClass<JMethodDescriptor> {
static constexpr auto kJavaDescriptor =
@@ -44,8 +45,11 @@ class JavaNativeModule : public NativeModule {
public:
JavaNativeModule(
std::weak_ptr<Instance> instance,
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper)
: instance_(std::move(instance)), wrapper_(make_global(wrapper)) {}
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper,
std::shared_ptr<MessageQueueThread> messageQueueThread)
: instance_(std::move(instance))
, wrapper_(make_global(wrapper))
, messageQueueThread_(std::move(messageQueueThread)) {}
std::string getName() override;
folly::dynamic getConstants() override;
@@ -57,6 +61,7 @@ class JavaNativeModule : public NativeModule {
private:
std::weak_ptr<Instance> instance_;
jni::global_ref<JavaModuleWrapper::javaobject> wrapper_;
std::shared_ptr<MessageQueueThread> messageQueueThread_;
std::vector<folly::Optional<MethodInvoker>> syncMethods_;
};
@@ -65,7 +70,8 @@ class NewJavaNativeModule : public NativeModule {
public:
NewJavaNativeModule(
std::weak_ptr<Instance> instance,
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper);
jni::alias_ref<JavaModuleWrapper::javaobject> wrapper,
std::shared_ptr<MessageQueueThread> messageQueueThread);
std::string getName() override;
std::vector<MethodDescriptor> getMethods() override;
@@ -78,6 +84,7 @@ class NewJavaNativeModule : public NativeModule {
std::weak_ptr<Instance> instance_;
jni::global_ref<JavaModuleWrapper::javaobject> wrapper_;
jni::global_ref<JBaseJavaModule::javaobject> module_;
std::shared_ptr<MessageQueueThread> messageQueueThread_;
std::vector<MethodInvoker> methods_;
std::vector<MethodDescriptor> methodDescriptors_;
@@ -32,14 +32,16 @@ xplat::module::CxxModule::Provider ModuleHolder::getProvider() const {
std::unique_ptr<ModuleRegistry> buildModuleRegistry(
std::weak_ptr<Instance> winstance,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules) {
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules,
std::shared_ptr<MessageQueueThread> moduleMessageQueue) {
std::vector<std::unique_ptr<NativeModule>> modules;
for (const auto& jm : *javaModules) {
modules.emplace_back(folly::make_unique<JavaNativeModule>(winstance, jm));
modules.emplace_back(folly::make_unique<JavaNativeModule>(
winstance, jm, moduleMessageQueue));
}
for (const auto& cm : *cxxModules) {
modules.emplace_back(
folly::make_unique<CxxNativeModule>(winstance, cm->getName(), cm->getProvider()));
modules.emplace_back(folly::make_unique<CxxNativeModule>(
winstance, cm->getName(), cm->getProvider(), moduleMessageQueue));
}
if (modules.empty()) {
return nullptr;
@@ -12,6 +12,8 @@
namespace facebook {
namespace react {
class MessageQueueThread;
class ModuleHolder : public jni::JavaClass<ModuleHolder> {
public:
static auto constexpr kJavaDescriptor =
@@ -24,7 +26,8 @@ class ModuleHolder : public jni::JavaClass<ModuleHolder> {
std::unique_ptr<ModuleRegistry> buildModuleRegistry(
std::weak_ptr<Instance> winstance,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules);
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules,
std::shared_ptr<MessageQueueThread> moduleMessageQueue);
}
}