diff options
author | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-28 00:00:32 +0000 |
---|---|---|
committer | abarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-28 00:00:32 +0000 |
commit | f503f94c1b224d7e4edbd68577f59f23e2cc7009 (patch) | |
tree | 0a4f4f095f215bc68226e41e6855e9a25718e79c /gin/modules | |
parent | bb37b653d232b2d1dd7be87e90b37d3887906d9f (diff) | |
download | chromium_src-f503f94c1b224d7e4edbd68577f59f23e2cc7009.zip chromium_src-f503f94c1b224d7e4edbd68577f59f23e2cc7009.tar.gz chromium_src-f503f94c1b224d7e4edbd68577f59f23e2cc7009.tar.bz2 |
[Mojo] Remove static "bootstrap" state in mojo_js
This CL removes the static static in boostrap.cc that we used to transfer the
initial handle from C++ into JavaScript. Rather that storing the initial handle
in static state and evaling some script to kick off the loading process, we now
use an API to load a module by name and then pass the initial handle to the
function defined by that module.
This CL paves the say for adding threading support to mojo_js. The previous
static approach didn't scale to multiple threads, but the approach in this CL
does. I'll actually add the API to create a thread in the next CL.
R=aa@chromium.org
BUG=none
Review URL: https://codereview.chromium.org/90203002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@237647 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin/modules')
-rw-r--r-- | gin/modules/module_registry.cc | 38 | ||||
-rw-r--r-- | gin/modules/module_registry.h | 13 | ||||
-rw-r--r-- | gin/modules/module_runner_delegate.cc | 12 | ||||
-rw-r--r-- | gin/modules/module_runner_delegate.h | 3 |
4 files changed, 56 insertions, 10 deletions
diff --git a/gin/modules/module_registry.cc b/gin/modules/module_registry.cc index 6eba6f5..e9340db 100644 --- a/gin/modules/module_registry.cc +++ b/gin/modules/module_registry.cc @@ -135,6 +135,20 @@ void ModuleRegistry::AddPendingModule(Isolate* isolate, AttemptToLoad(isolate, pending.Pass()); } +void ModuleRegistry::LoadModule(Isolate* isolate, + const std::string& id, + LoadModuleCallback callback) { + if (available_modules_.find(id) != available_modules_.end()) { + // Should we call the callback asynchronously? + callback.Run(GetModule(isolate, id)); + return; + } + // Should we support multiple callers waiting on the same module? + DCHECK(waiting_callbacks_.find(id) == waiting_callbacks_.end()); + waiting_callbacks_[id] = callback; + unsatisfied_dependencies_.insert(id); +} + void ModuleRegistry::RegisterModule(Isolate* isolate, const std::string& id, v8::Handle<Value> module) { @@ -145,6 +159,14 @@ void ModuleRegistry::RegisterModule(Isolate* isolate, available_modules_.insert(id); v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); modules->Set(StringToSymbol(isolate, id), module); + + LoadModuleCallbackMap::iterator it = waiting_callbacks_.find(id); + if (it == waiting_callbacks_.end()) + return; + LoadModuleCallback callback = it->second; + waiting_callbacks_.erase(it); + // Should we call the callback asynchronously? + callback.Run(module); } void ModuleRegistry::Detach(v8::Handle<Context> context) { @@ -169,14 +191,10 @@ void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { if (!pending->id.empty() && available_modules_.count(pending->id)) return; // We've already loaded this module. - v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); std::vector<v8::Handle<Value> > argv(argc); - for (uint32_t i = 0; i < argc; ++i) { - v8::Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); - DCHECK(modules->HasOwnProperty(key)); - argv[i] = modules->Get(key); - } + for (uint32_t i = 0; i < argc; ++i) + argv[i] = GetModule(isolate, pending->dependencies[i]); v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); @@ -203,6 +221,14 @@ bool ModuleRegistry::AttemptToLoad(Isolate* isolate, return true; } +v8::Handle<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, + const std::string& id) { + v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); + v8::Handle<String> key = StringToSymbol(isolate, id); + DCHECK(modules->HasOwnProperty(key)); + return modules->Get(key); +} + void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { bool keep_trying = true; while (keep_trying) { diff --git a/gin/modules/module_registry.h b/gin/modules/module_registry.h index a7732c8..d3bed32 100644 --- a/gin/modules/module_registry.h +++ b/gin/modules/module_registry.h @@ -6,9 +6,11 @@ #define GIN_MODULES_MODULE_REGISTRY_H_ #include <list> +#include <map> #include <set> #include <string> +#include "base/callback.h" #include "base/compiler_specific.h" #include "base/memory/scoped_ptr.h" #include "gin/per_context_data.h" @@ -30,6 +32,8 @@ struct PendingModule; // class ModuleRegistry : public ContextSupplement { public: + typedef base::Callback<void (v8::Handle<v8::Value>)> LoadModuleCallback; + virtual ~ModuleRegistry(); static ModuleRegistry* From(v8::Handle<v8::Context> context); @@ -46,6 +50,10 @@ class ModuleRegistry : public ContextSupplement { void AddPendingModule(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); + void LoadModule(v8::Isolate* isolate, + const std::string& id, + LoadModuleCallback callback); + // The caller must have already entered our context. void AttemptToLoadMoreModules(v8::Isolate* isolate); @@ -59,6 +67,7 @@ class ModuleRegistry : public ContextSupplement { private: typedef ScopedVector<PendingModule> PendingModuleVector; + typedef std::map<std::string, LoadModuleCallback> LoadModuleCallbackMap; explicit ModuleRegistry(v8::Isolate* isolate); @@ -73,9 +82,13 @@ class ModuleRegistry : public ContextSupplement { bool CheckDependencies(PendingModule* pending); bool AttemptToLoad(v8::Isolate* isolate, scoped_ptr<PendingModule> pending); + v8::Handle<v8::Value> GetModule(v8::Isolate* isolate, const std::string& id); + std::set<std::string> available_modules_; std::set<std::string> unsatisfied_dependencies_; + LoadModuleCallbackMap waiting_callbacks_; + PendingModuleVector pending_modules_; v8::Persistent<v8::Object> modules_; diff --git a/gin/modules/module_runner_delegate.cc b/gin/modules/module_runner_delegate.cc index 9e0b762..9bf2863 100644 --- a/gin/modules/module_runner_delegate.cc +++ b/gin/modules/module_runner_delegate.cc @@ -21,6 +21,13 @@ void ModuleRunnerDelegate::AddBuiltinModule(const std::string& id, builtin_modules_[id] = templ; } +void ModuleRunnerDelegate::AttemptToLoadMoreModules(Runner* runner) { + ModuleRegistry* registry = ModuleRegistry::From(runner->context()); + registry->AttemptToLoadMoreModules(runner->isolate()); + module_provider_.AttempToLoadModules( + runner, registry->unsatisfied_dependencies()); +} + v8::Handle<v8::ObjectTemplate> ModuleRunnerDelegate::GetGlobalTemplate( Runner* runner) { v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); @@ -42,10 +49,7 @@ void ModuleRunnerDelegate::DidCreateContext(Runner* runner) { } void ModuleRunnerDelegate::DidRunScript(Runner* runner) { - ModuleRegistry* registry = ModuleRegistry::From(runner->context()); - registry->AttemptToLoadMoreModules(runner->isolate()); - module_provider_.AttempToLoadModules( - runner, registry->unsatisfied_dependencies()); + AttemptToLoadMoreModules(runner); } } // namespace gin diff --git a/gin/modules/module_runner_delegate.h b/gin/modules/module_runner_delegate.h index d5523b8..1873d5a 100644 --- a/gin/modules/module_runner_delegate.h +++ b/gin/modules/module_runner_delegate.h @@ -32,6 +32,9 @@ class ModuleRunnerDelegate : public RunnerDelegate { // lazily. void AddBuiltinModule(const std::string& id, ModuleTemplateGetter templ); + protected: + void AttemptToLoadMoreModules(Runner* runner); + private: typedef std::map<std::string, ModuleTemplateGetter> BuiltinModuleMap; |