-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Instead of sending a list of modules over to JS on startup (and actually blocking script execution) instead provide a proxy object that constructs each of these lazily. Reviewed By: lexs Differential Revision: D3936979 fbshipit-source-id: 71bde822f01eb17a29f56c5e60e95e98e207d74d
- Loading branch information
Showing
12 changed files
with
210 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#include "JSCNativeModules.h" | ||
|
||
#include <string> | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
JSCNativeModules::JSCNativeModules(std::shared_ptr<ModuleRegistry> moduleRegistry) : | ||
m_moduleRegistry(std::move(moduleRegistry)) {} | ||
|
||
JSValueRef JSCNativeModules::getModule(JSContextRef context, JSStringRef jsName) { | ||
std::string moduleName = String::ref(jsName).str(); | ||
|
||
const auto it = m_objects.find(moduleName); | ||
if (it != m_objects.end()) { | ||
return static_cast<JSObjectRef>(it->second); | ||
} | ||
|
||
auto module = createModule(moduleName, context); | ||
if (!module.hasValue()) { | ||
return JSValueMakeUndefined(context); | ||
} | ||
|
||
// Protect since we'll be holding on to this value, even though JS may not | ||
module->makeProtected(); | ||
|
||
auto result = m_objects.emplace(std::move(moduleName), std::move(*module)).first; | ||
return static_cast<JSObjectRef>(result->second); | ||
} | ||
|
||
void JSCNativeModules::reset() { | ||
m_genNativeModuleJS = nullptr; | ||
m_objects.clear(); | ||
} | ||
|
||
folly::Optional<Object> JSCNativeModules::createModule(const std::string& name, JSContextRef context) { | ||
if (!m_genNativeModuleJS) { | ||
auto global = Object::getGlobalObject(context); | ||
m_genNativeModuleJS = global.getProperty("__fbGenNativeModule").asObject(); | ||
m_genNativeModuleJS->makeProtected(); | ||
|
||
// Initialize the module name list, otherwise getModuleConfig won't work | ||
// TODO (pieterdb): fix this in ModuleRegistry | ||
m_moduleRegistry->moduleNames(); | ||
} | ||
|
||
auto result = m_moduleRegistry->getConfig(name); | ||
if (!result.hasValue()) { | ||
return nullptr; | ||
} | ||
|
||
Value moduleInfo = m_genNativeModuleJS->callAsFunction({ | ||
Value::fromDynamic(context, result->config), | ||
JSValueMakeNumber(context, result->index) | ||
}); | ||
CHECK(!moduleInfo.isNull()) << "Module returned from genNativeModule is null"; | ||
|
||
return moduleInfo.asObject().getProperty("module").asObject(); | ||
} | ||
|
||
} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <folly/Optional.h> | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "Value.h" | ||
#include "ModuleRegistry.h" | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
/** | ||
* Holds and creates JS representations of the modules in ModuleRegistry | ||
*/ | ||
class JSCNativeModules { | ||
|
||
public: | ||
explicit JSCNativeModules(std::shared_ptr<ModuleRegistry> moduleRegistry); | ||
JSValueRef getModule(JSContextRef context, JSStringRef name); | ||
void reset(); | ||
|
||
private: | ||
folly::Optional<Object> m_genNativeModuleJS; | ||
std::shared_ptr<ModuleRegistry> m_moduleRegistry; | ||
std::unordered_map<std::string, Object> m_objects; | ||
|
||
folly::Optional<Object> createModule(const std::string& name, JSContextRef context); | ||
}; | ||
|
||
} | ||
} |
Oops, something went wrong.