diff --git a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp index 360918d672c..0f1370a7eeb 100644 --- a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp +++ b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp @@ -27,14 +27,20 @@ namespace react { class JSIExecutor::NativeModuleProxy : public jsi::HostObject { public: - NativeModuleProxy(JSIExecutor &executor) : executor_(executor) {} + NativeModuleProxy(std::shared_ptr nativeModules) + : weakNativeModules_(nativeModules) {} Value get(Runtime &rt, const PropNameID &name) override { if (name.utf8(rt) == "name") { return jsi::String::createFromAscii(rt, "NativeModules"); } - return executor_.nativeModules_.getModule(rt, name); + auto nativeModules = weakNativeModules_.lock(); + if (!nativeModules) { + return nullptr; + } + + return nativeModules->getModule(rt, name); } void set(Runtime &, const PropNameID &, const Value &) override { @@ -43,7 +49,7 @@ class JSIExecutor::NativeModuleProxy : public jsi::HostObject { } private: - JSIExecutor &executor_; + std::weak_ptr weakNativeModules_; }; namespace { @@ -63,7 +69,8 @@ JSIExecutor::JSIExecutor( RuntimeInstaller runtimeInstaller) : runtime_(runtime), delegate_(delegate), - nativeModules_(delegate ? delegate->getModuleRegistry() : nullptr), + nativeModules_(std::make_shared( + delegate ? delegate->getModuleRegistry() : nullptr)), scopedTimeoutInvoker_(scopedTimeoutInvoker), runtimeInstaller_(runtimeInstaller) { runtime_->global().setProperty( @@ -76,7 +83,7 @@ void JSIExecutor::initializeRuntime() { *runtime_, "nativeModuleProxy", Object::createFromHostObject( - *runtime_, std::make_shared(*this))); + *runtime_, std::make_shared(nativeModules_))); runtime_->global().setProperty( *runtime_, diff --git a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h index a8a1042ab87..2cd3776baf5 100644 --- a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h +++ b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h @@ -122,7 +122,7 @@ class JSIExecutor : public JSExecutor { std::shared_ptr runtime_; std::shared_ptr delegate_; - JSINativeModules nativeModules_; + std::shared_ptr nativeModules_; std::once_flag bindFlag_; std::unique_ptr bundleRegistry_; JSIScopedTimeoutInvoker scopedTimeoutInvoker_;