Skip to content

Commit

Permalink
Configure JSExector with BundleRegistry instead of JSModulesUnbundle.
Browse files Browse the repository at this point in the history
Differential Revision: D5850968

fbshipit-source-id: e5e7ad92c2347c2641551fcf820f061ffde5fed6
  • Loading branch information
fromcelticpark authored and facebook-github-bot committed Sep 21, 2017
1 parent 2f952fb commit 7982191
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 49 deletions.
6 changes: 4 additions & 2 deletions React/CxxBridge/RCTCxxBridge.mm
Expand Up @@ -36,6 +36,7 @@
#import <cxxreact/JSCExecutor.h>
#import <cxxreact/JSIndexedRAMBundle.h>
#import <cxxreact/Platform.h>
#import <cxxreact/RAMBundleRegistry.h>
#import <jschelpers/Value.h>

#import "NSDataBigString.h"
Expand Down Expand Up @@ -1184,8 +1185,9 @@ - (void)executeApplicationScript:(NSData *)script
[self->_performanceLogger markStopForTag:RCTPLRAMBundleLoad];
[self->_performanceLogger setValue:scriptStr->size() forTag:RCTPLRAMStartupCodeSize];
if (self->_reactInstance) {
self->_reactInstance->loadUnbundle(std::move(ramBundle), std::move(scriptStr),
sourceUrlStr.UTF8String, !async);
auto registry = std::make_unique<RAMBundleRegistry>(std::move(ramBundle));
self->_reactInstance->loadRAMBundle(std::move(registry), std::move(scriptStr),
sourceUrlStr.UTF8String, !async);
}
} else if (self->_reactInstance) {
self->_reactInstance->loadScriptFromString(std::make_unique<NSDataBigString>(script),
Expand Down
4 changes: 2 additions & 2 deletions React/CxxBridge/RCTObjcExecutor.mm
Expand Up @@ -91,8 +91,8 @@ void loadApplicationScript(
}];
}

void setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle>) override {
RCTAssert(NO, @"Unbundle is not supported in RCTObjcExecutor");
void setBundleRegistry(std::unique_ptr<RAMBundleRegistry>) override {
RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor");
}

void callFunction(const std::string &module, const std::string &method,
Expand Down
2 changes: 2 additions & 0 deletions React/React.xcodeproj/project.pbxproj
Expand Up @@ -1101,6 +1101,7 @@
C6194AB11EF156280034D062 /* RCTPackagerConnectionConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = C6194AAB1EF156280034D062 /* RCTPackagerConnectionConfig.h */; };
C654505E1F3BD9280090799B /* RCTManagedPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = C654505D1F3BD9280090799B /* RCTManagedPointer.h */; };
C654505F1F3BD9280090799B /* RCTManagedPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = C654505D1F3BD9280090799B /* RCTManagedPointer.h */; };
C669D8981F72E3DE006748EB /* RAMBundleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6D380181F71D75B00621378 /* RAMBundleRegistry.h */; };
C6827DF61EF17CCC00D66BEF /* RCTJSEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; };
C6827DF71EF17CCC00D66BEF /* RCTJSEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; };
C6827DFB1EF1800E00D66BEF /* RCTJSEnvironment.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; };
Expand Down Expand Up @@ -1608,6 +1609,7 @@
dstPath = include/cxxreact;
dstSubfolderSpec = 16;
files = (
C669D8981F72E3DE006748EB /* RAMBundleRegistry.h in Copy Headers */,
3DA981A01E5B0E34004F2374 /* CxxModule.h in Copy Headers */,
3DA981A11E5B0E34004F2374 /* CxxNativeModule.h in Copy Headers */,
3DA981A21E5B0E34004F2374 /* JSExecutor.h in Copy Headers */,
Expand Down
12 changes: 8 additions & 4 deletions ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp
Expand Up @@ -13,6 +13,7 @@
#include <cxxreact/MethodCall.h>
#include <cxxreact/RecoverableError.h>
#include <cxxreact/ModuleRegistry.h>
#include <cxxreact/RAMBundleRegistry.h>
#include <fb/log.h>
#include <folly/dynamic.h>
#include <folly/Memory.h>
Expand Down Expand Up @@ -185,8 +186,10 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
auto manager = extractAssetManager(assetManager);
auto script = loadScriptFromAssets(manager, sourceURL);
if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) {
instance_->loadUnbundle(
folly::make_unique<JniJSModulesUnbundle>(manager, sourceURL),
auto bundle = folly::make_unique<JniJSModulesUnbundle>(manager, sourceURL);
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
instance_->loadRAMBundle(
std::move(registry),
std::move(script),
sourceURL,
loadSynchronously);
Expand Down Expand Up @@ -214,8 +217,9 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
if (isIndexedRAMBundle(zFileName)) {
auto bundle = folly::make_unique<JSIndexedRAMBundle>(zFileName);
auto startupScript = bundle->getStartupCode();
instance_->loadUnbundle(
std::move(bundle),
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
instance_->loadRAMBundle(
std::move(registry),
std::move(startupScript),
sourceURL,
loadSynchronously);
Expand Down
4 changes: 2 additions & 2 deletions ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp
Expand Up @@ -84,10 +84,10 @@ void ProxyExecutor::loadApplicationScript(
// we launch the application.
}

void ProxyExecutor::setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle>) {
void ProxyExecutor::setBundleRegistry(std::unique_ptr<RAMBundleRegistry>) {
jni::throwNewJavaException(
"java/lang/UnsupportedOperationException",
"Loading application unbundles is not supported for proxy executors");
"Loading application RAM bundles is not supported for proxy executors");
}

void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
Expand Down
4 changes: 2 additions & 2 deletions ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h
Expand Up @@ -35,8 +35,8 @@ class ProxyExecutor : public JSExecutor {
virtual void loadApplicationScript(
std::unique_ptr<const JSBigString> script,
std::string sourceURL) override;
virtual void setJSModulesUnbundle(
std::unique_ptr<JSModulesUnbundle> bundle) override;
virtual void setBundleRegistry(
std::unique_ptr<RAMBundleRegistry> bundle) override;
virtual void callFunction(
const std::string& moduleId,
const std::string& methodId,
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/cxxreact/BUCK
Expand Up @@ -87,6 +87,7 @@ CXXREACT_PUBLIC_HEADERS = [
"NativeModule.h",
"NativeToJsBridge.h",
"Platform.h",
"RAMBundleRegistry.h",
"RecoverableError.h",
"SharedProxyCxxModule.h",
"SystraceSection.h",
Expand Down
22 changes: 11 additions & 11 deletions ReactCommon/cxxreact/Instance.cpp
Expand Up @@ -4,10 +4,10 @@

#include "JSBigString.h"
#include "JSExecutor.h"
#include "JSModulesUnbundle.h"
#include "MessageQueueThread.h"
#include "MethodCall.h"
#include "NativeToJsBridge.h"
#include "RAMBundleRegistry.h"
#include "RecoverableError.h"
#include "SystraceSection.h"

Expand Down Expand Up @@ -50,25 +50,25 @@ void Instance::initializeBridge(
CHECK(nativeToJsBridge_);
}

void Instance::loadApplication(std::unique_ptr<JSModulesUnbundle> unbundle,
void Instance::loadApplication(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
callback_->incrementPendingJSCalls();
SystraceSection s("Instance::loadApplication", "sourceURL",
sourceURL);
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(string),
nativeToJsBridge_->loadApplication(std::move(bundleRegistry), std::move(string),
std::move(sourceURL));
}

void Instance::loadApplicationSync(std::unique_ptr<JSModulesUnbundle> unbundle,
void Instance::loadApplicationSync(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
std::unique_lock<std::mutex> lock(m_syncMutex);
m_syncCV.wait(lock, [this] { return m_syncReady; });

SystraceSection s("Instance::loadApplicationSync", "sourceURL",
sourceURL);
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(string),
nativeToJsBridge_->loadApplicationSync(std::move(bundleRegistry), std::move(string),
std::move(sourceURL));
}

Expand All @@ -91,15 +91,15 @@ void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> string,
}
}

void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL,
bool loadSynchronously) {
void Instance::loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL,
bool loadSynchronously) {
if (loadSynchronously) {
loadApplicationSync(std::move(unbundle), std::move(startupScript),
loadApplicationSync(std::move(bundleRegistry), std::move(startupScript),
std::move(startupScriptSourceURL));
} else {
loadApplication(std::move(unbundle), std::move(startupScript),
loadApplication(std::move(bundleRegistry), std::move(startupScript),
std::move(startupScriptSourceURL));
}
}
Expand Down
12 changes: 6 additions & 6 deletions ReactCommon/cxxreact/Instance.h
Expand Up @@ -21,9 +21,9 @@ namespace react {

class JSBigString;
class JSExecutorFactory;
class JSModulesUnbundle;
class MessageQueueThread;
class ModuleRegistry;
class RAMBundleRegistry;

struct InstanceCallback {
virtual ~InstanceCallback() {}
Expand All @@ -44,9 +44,9 @@ class RN_EXPORT Instance {

void loadScriptFromString(std::unique_ptr<const JSBigString> string,
std::string sourceURL, bool loadSynchronously);
void loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL, bool loadSynchronously);
void loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL, bool loadSynchronously);
bool supportsProfiling();
void setGlobalVariable(std::string propName,
std::unique_ptr<const JSBigString> jsonValue);
Expand All @@ -73,10 +73,10 @@ class RN_EXPORT Instance {

private:
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
void loadApplication(std::unique_ptr<JSModulesUnbundle> unbundle,
void loadApplication(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
void loadApplicationSync(std::unique_ptr<JSModulesUnbundle> unbundle,
void loadApplicationSync(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);

Expand Down
4 changes: 2 additions & 2 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Expand Up @@ -354,11 +354,11 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
ReactMarker::logTaggedMarker(ReactMarker::RUN_JS_BUNDLE_STOP, scriptName.c_str());
}

void JSCExecutor::setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle) {
void JSCExecutor::setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) {
if (!m_bundleRegistry) {
installNativeHook<&JSCExecutor::nativeRequire>("nativeRequire");
}
m_bundleRegistry = folly::make_unique<RAMBundleRegistry>(std::move(unbundle));
m_bundleRegistry = std::move(bundleRegistry);
}

void JSCExecutor::bindBridge() throw(JSException) {
Expand Down
3 changes: 1 addition & 2 deletions ReactCommon/cxxreact/JSCExecutor.h
Expand Up @@ -65,8 +65,7 @@ class RN_EXPORT JSCExecutor : public JSExecutor, public PrivateDataBase {
std::unique_ptr<const JSBigString> script,
std::string sourceURL) override;

virtual void setJSModulesUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle) override;
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) override;

virtual void callFunction(
const std::string& moduleId,
Expand Down
5 changes: 3 additions & 2 deletions ReactCommon/cxxreact/JSExecutor.h
Expand Up @@ -16,6 +16,7 @@ class JSExecutor;
class JSModulesUnbundle;
class MessageQueueThread;
class ModuleRegistry;
class RAMBundleRegistry;

// This interface describes the delegate interface required by
// Executor implementations to call from JS into native code.
Expand Down Expand Up @@ -48,9 +49,9 @@ class JSExecutor {
std::string sourceURL) = 0;

/**
* Add an application "unbundle" file
* Add an application "RAM" bundle registry
*/
virtual void setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle> bundle) = 0;
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) = 0;

/**
* Executes BatchedBridge.callFunctionReturnFlushedQueue with the module ID,
Expand Down
18 changes: 9 additions & 9 deletions ReactCommon/cxxreact/NativeToJsBridge.cpp
Expand Up @@ -10,8 +10,8 @@
#include "JSBigString.h"
#include "SystraceSection.h"
#include "MethodCall.h"
#include "JSModulesUnbundle.h"
#include "MessageQueueThread.h"
#include "RAMBundleRegistry.h"

#ifdef WITH_FBSYSTRACE
#include <fbsystrace.h>
Expand Down Expand Up @@ -91,29 +91,29 @@ NativeToJsBridge::~NativeToJsBridge() {
}

void NativeToJsBridge::loadApplication(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {
runOnExecutorQueue(
[unbundleWrap=folly::makeMoveWrapper(std::move(unbundle)),
[bundleRegistryWrap=folly::makeMoveWrapper(std::move(bundleRegistry)),
startupScript=folly::makeMoveWrapper(std::move(startupScript)),
startupScriptSourceURL=std::move(startupScriptSourceURL)]
(JSExecutor* executor) mutable {
auto unbundle = unbundleWrap.move();
if (unbundle) {
executor->setJSModulesUnbundle(std::move(unbundle));
auto bundleRegistry = bundleRegistryWrap.move();
if (bundleRegistry) {
executor->setBundleRegistry(std::move(bundleRegistry));
}
executor->loadApplicationScript(std::move(*startupScript),
std::move(startupScriptSourceURL));
});
}

void NativeToJsBridge::loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {
if (unbundle) {
m_executor->setJSModulesUnbundle(std::move(unbundle));
if (bundleRegistry) {
m_executor->setBundleRegistry(std::move(bundleRegistry));
}
m_executor->loadApplicationScript(std::move(startupScript),
std::move(startupScriptSourceURL));
Expand Down
8 changes: 4 additions & 4 deletions ReactCommon/cxxreact/NativeToJsBridge.h
Expand Up @@ -18,10 +18,10 @@ namespace facebook {
namespace react {

struct InstanceCallback;
class JSModulesUnbundle;
class JsToNativeBridge;
class MessageQueueThread;
class ModuleRegistry;
class RAMBundleRegistry;

// This class manages calls from native code to JS. It also manages
// executors and their threads. All functions here can be called from
Expand Down Expand Up @@ -85,16 +85,16 @@ class NativeToJsBridge {
}

/**
* Starts the JS application. If unbundle is non-null, then it is
* Starts the JS application. If bundleRegistry is non-null, then it is
* used to fetch JavaScript modules as individual scripts.
* Otherwise, the script is assumed to include all the modules.
*/
void loadApplication(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupCode,
std::string sourceURL);
void loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupCode,
std::string sourceURL);

Expand Down
4 changes: 4 additions & 0 deletions ReactCommon/cxxreact/RAMBundleRegistry.cpp
Expand Up @@ -12,6 +12,10 @@ RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBund
}

JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) {
if (m_bundles.find(bundleId) == m_bundles.end()) {
m_bundles.emplace(bundleId, this->bundleById(bundleId));
}

return getBundle(bundleId)->getModule(moduleId);
}

Expand Down
11 changes: 10 additions & 1 deletion ReactCommon/cxxreact/RAMBundleRegistry.h
Expand Up @@ -8,16 +8,25 @@
#include <utility>

#include <cxxreact/JSModulesUnbundle.h>
#include <jschelpers/noncopyable.h>

namespace facebook {
namespace react {

class RAMBundleRegistry {
class RAMBundleRegistry : noncopyable {
public:
constexpr static uint32_t MAIN_BUNDLE_ID = 0;

explicit RAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle);
RAMBundleRegistry(RAMBundleRegistry&&) = default;
RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default;

JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId);
virtual ~RAMBundleRegistry() {};
protected:
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const {
throw std::runtime_error("Please, override this method in a subclass to support multiple RAM bundles.");
}
private:
JSModulesUnbundle *getBundle(uint32_t bundleId) const;

Expand Down

0 comments on commit 7982191

Please sign in to comment.