Files
react-native/ReactCommon/turbomodule/core/TurboModuleBinding.cpp
T
Kevin Gozali 72e276a0f0 TM iOS: Keep LongLivedObject longer past invalidation
Summary:
There can be a race condition between bridge invalidation (hence TM binding invalidation) with pending Promise reject/resolve invocation. If invalidation happens first, invoking the resolve/reject from ObjC class might end up accessing destroyed PromiseWrapper, causing hard crash randomly.

The proper fix is to switch the objc promise resolve/reject block (objc block) to use C++ PromiseWrapper directly, such that the lifecycle of the shared_ptr<> can be correct.

Reviewed By: RSNara

Differential Revision: D15801403

fbshipit-source-id: 9b0c7d323b18d94e920ea3eafc3a6916dadba247
2019-06-13 09:13:51 -07:00

80 lines
2.3 KiB
C++

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "TurboModuleBinding.h"
#include <string>
#include <cxxreact/SystraceSection.h>
#include <jsireact/LongLivedObject.h>
using namespace facebook;
namespace facebook {
namespace react {
/**
* Public API to install the TurboModule system.
*/
TurboModuleBinding::TurboModuleBinding(const TurboModuleProviderFunctionType &moduleProvider)
: moduleProvider_(moduleProvider) {}
void TurboModuleBinding::install(
jsi::Runtime &runtime,
std::shared_ptr<TurboModuleBinding> binding) {
runtime.global().setProperty(
runtime,
"__turboModuleProxy",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
1,
[binding](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
return binding->jsProxy(rt, thisVal, args, count);
}));
}
void TurboModuleBinding::invalidate() const {
// TODO (T45804587): Revisit this invalidation logic.
// The issue was that Promise resolve/reject functions that are invoked in
// some distance future might end up accessing PromiseWrapper that was already
// destroyed, if the binding invalidation removed it from the
// LongLivedObjectCollection.
// LongLivedObjectCollection::get().clear();
}
std::shared_ptr<TurboModule> TurboModuleBinding::getModule(const std::string &name) {
std::shared_ptr<TurboModule> module = nullptr;
{
SystraceSection s("TurboModuleBinding::getModule", "module", name);
module = moduleProvider_(name);
}
return module;
}
jsi::Value TurboModuleBinding::jsProxy(
jsi::Runtime& runtime,
const jsi::Value& thisVal,
const jsi::Value* args,
size_t count) {
if (count != 1) {
throw std::invalid_argument("TurboModuleBinding::jsProxy arg count must be 1");
}
std::string moduleName = args[0].getString(runtime).utf8(runtime);
std::shared_ptr<TurboModule> module = getModule(moduleName);
if (module == nullptr) {
return jsi::Value::null();
}
return jsi::Object::createFromHostObject(runtime, std::move(module));
}
} // namespace react
} // namespace facebook