| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gin/modules/module_registry.h" | 5 #include "gin/modules/module_registry.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 modules_.Reset(); | 106 modules_.Reset(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 // static | 109 // static |
| 110 void ModuleRegistry::RegisterGlobals(Isolate* isolate, | 110 void ModuleRegistry::RegisterGlobals(Isolate* isolate, |
| 111 v8::Local<ObjectTemplate> templ) { | 111 v8::Local<ObjectTemplate> templ) { |
| 112 templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate)); | 112 templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // static | 115 // static |
| 116 bool ModuleRegistry::InstallGlobals(v8::Isolate* isolate, | 116 void ModuleRegistry::InstallGlobals(v8::Isolate* isolate, |
| 117 v8::Local<v8::Object> obj) { | 117 v8::Local<v8::Object> obj) { |
| 118 v8::Local<v8::Function> function; | 118 obj->Set(StringToSymbol(isolate, "define"), |
| 119 auto maybe_function = | 119 GetDefineTemplate(isolate)->GetFunction()); |
| 120 GetDefineTemplate(isolate)->GetFunction(isolate->GetCurrentContext()); | |
| 121 if (!maybe_function.ToLocal(&function)) | |
| 122 return false; | |
| 123 return SetProperty(isolate, obj, StringToSymbol(isolate, "define"), function); | |
| 124 } | 120 } |
| 125 | 121 |
| 126 // static | 122 // static |
| 127 ModuleRegistry* ModuleRegistry::From(v8::Local<Context> context) { | 123 ModuleRegistry* ModuleRegistry::From(v8::Local<Context> context) { |
| 128 PerContextData* data = PerContextData::From(context); | 124 PerContextData* data = PerContextData::From(context); |
| 129 if (!data) | 125 if (!data) |
| 130 return NULL; | 126 return NULL; |
| 131 | 127 |
| 132 ModuleRegistryData* registry_data = static_cast<ModuleRegistryData*>( | 128 ModuleRegistryData* registry_data = static_cast<ModuleRegistryData*>( |
| 133 data->GetUserData(kModuleRegistryKey)); | 129 data->GetUserData(kModuleRegistryKey)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 waiting_callbacks_.insert(std::make_pair(id, callback)); | 170 waiting_callbacks_.insert(std::make_pair(id, callback)); |
| 175 | 171 |
| 176 for (size_t i = 0; i < pending_modules_.size(); ++i) { | 172 for (size_t i = 0; i < pending_modules_.size(); ++i) { |
| 177 if (pending_modules_[i]->id == id) | 173 if (pending_modules_[i]->id == id) |
| 178 return; | 174 return; |
| 179 } | 175 } |
| 180 | 176 |
| 181 unsatisfied_dependencies_.insert(id); | 177 unsatisfied_dependencies_.insert(id); |
| 182 } | 178 } |
| 183 | 179 |
| 184 bool ModuleRegistry::RegisterModule(Isolate* isolate, | 180 void ModuleRegistry::RegisterModule(Isolate* isolate, |
| 185 const std::string& id, | 181 const std::string& id, |
| 186 v8::Local<Value> module) { | 182 v8::Local<Value> module) { |
| 187 if (id.empty() || module.IsEmpty()) | 183 if (id.empty() || module.IsEmpty()) |
| 188 return false; | 184 return; |
| 189 | 185 |
| 190 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); | |
| 191 if (!SetProperty(isolate, modules, StringToSymbol(isolate, id), module)) | |
| 192 return false; | |
| 193 unsatisfied_dependencies_.erase(id); | 186 unsatisfied_dependencies_.erase(id); |
| 194 available_modules_.insert(id); | 187 available_modules_.insert(id); |
| 188 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
| 189 modules->Set(StringToSymbol(isolate, id), module); |
| 195 | 190 |
| 196 std::pair<LoadModuleCallbackMap::iterator, LoadModuleCallbackMap::iterator> | 191 std::pair<LoadModuleCallbackMap::iterator, LoadModuleCallbackMap::iterator> |
| 197 range = waiting_callbacks_.equal_range(id); | 192 range = waiting_callbacks_.equal_range(id); |
| 198 std::vector<LoadModuleCallback> callbacks; | 193 std::vector<LoadModuleCallback> callbacks; |
| 199 callbacks.reserve(waiting_callbacks_.count(id)); | 194 callbacks.reserve(waiting_callbacks_.count(id)); |
| 200 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; | 195 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; |
| 201 ++it) { | 196 ++it) { |
| 202 callbacks.push_back(it->second); | 197 callbacks.push_back(it->second); |
| 203 } | 198 } |
| 204 waiting_callbacks_.erase(range.first, range.second); | 199 waiting_callbacks_.erase(range.first, range.second); |
| 205 for (std::vector<LoadModuleCallback>::iterator it = callbacks.begin(); | 200 for (std::vector<LoadModuleCallback>::iterator it = callbacks.begin(); |
| 206 it != callbacks.end(); | 201 it != callbacks.end(); |
| 207 ++it) { | 202 ++it) { |
| 208 // Should we call the callback asynchronously? | 203 // Should we call the callback asynchronously? |
| 209 it->Run(module); | 204 it->Run(module); |
| 210 } | 205 } |
| 211 return true; | |
| 212 } | 206 } |
| 213 | 207 |
| 214 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { | 208 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { |
| 215 size_t num_missing_dependencies = 0; | 209 size_t num_missing_dependencies = 0; |
| 216 size_t len = pending->dependencies.size(); | 210 size_t len = pending->dependencies.size(); |
| 217 for (size_t i = 0; i < len; ++i) { | 211 for (size_t i = 0; i < len; ++i) { |
| 218 const std::string& dependency = pending->dependencies[i]; | 212 const std::string& dependency = pending->dependencies[i]; |
| 219 if (available_modules_.count(dependency)) | 213 if (available_modules_.count(dependency)) |
| 220 continue; | 214 continue; |
| 221 unsatisfied_dependencies_.insert(dependency); | 215 unsatisfied_dependencies_.insert(dependency); |
| 222 num_missing_dependencies++; | 216 num_missing_dependencies++; |
| 223 } | 217 } |
| 224 return num_missing_dependencies == 0; | 218 return num_missing_dependencies == 0; |
| 225 } | 219 } |
| 226 | 220 |
| 227 bool ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { | 221 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { |
| 228 if (!pending->id.empty() && available_modules_.count(pending->id)) | 222 if (!pending->id.empty() && available_modules_.count(pending->id)) |
| 229 return true; // We've already loaded this module. | 223 return; // We've already loaded this module. |
| 230 | 224 |
| 231 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); | 225 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); |
| 232 std::vector<v8::Local<Value> > argv(argc); | 226 std::vector<v8::Local<Value> > argv(argc); |
| 233 for (uint32_t i = 0; i < argc; ++i) | 227 for (uint32_t i = 0; i < argc; ++i) |
| 234 argv[i] = GetModule(isolate, pending->dependencies[i]); | 228 argv[i] = GetModule(isolate, pending->dependencies[i]); |
| 235 | 229 |
| 236 v8::Local<Value> module = Local<Value>::New(isolate, pending->factory); | 230 v8::Local<Value> module = Local<Value>::New(isolate, pending->factory); |
| 237 | 231 |
| 238 v8::Local<Function> factory; | 232 v8::Local<Function> factory; |
| 239 if (ConvertFromV8(isolate, module, &factory)) { | 233 if (ConvertFromV8(isolate, module, &factory)) { |
| 240 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); | 234 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); |
| 241 Runner* runner = data->runner(); | 235 Runner* runner = data->runner(); |
| 242 module = runner->Call(factory, runner->global(), argc, | 236 module = runner->Call(factory, runner->global(), argc, |
| 243 argv.empty() ? NULL : &argv.front()); | 237 argv.empty() ? NULL : &argv.front()); |
| 244 if (pending->id.empty()) | 238 if (pending->id.empty()) |
| 245 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), | 239 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), |
| 246 &pending->id); | 240 &pending->id); |
| 247 } | 241 } |
| 248 | 242 |
| 249 return RegisterModule(isolate, pending->id, module); | 243 RegisterModule(isolate, pending->id, module); |
| 250 } | 244 } |
| 251 | 245 |
| 252 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 246 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
| 253 scoped_ptr<PendingModule> pending) { | 247 scoped_ptr<PendingModule> pending) { |
| 254 if (!CheckDependencies(pending.get())) { | 248 if (!CheckDependencies(pending.get())) { |
| 255 pending_modules_.push_back(pending.release()); | 249 pending_modules_.push_back(pending.release()); |
| 256 return false; | 250 return false; |
| 257 } | 251 } |
| 258 return Load(isolate, pending.Pass()); | 252 Load(isolate, pending.Pass()); |
| 253 return true; |
| 259 } | 254 } |
| 260 | 255 |
| 261 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, | 256 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, |
| 262 const std::string& id) { | 257 const std::string& id) { |
| 263 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); | 258 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
| 264 v8::Local<String> key = StringToSymbol(isolate, id); | 259 v8::Local<String> key = StringToSymbol(isolate, id); |
| 265 DCHECK(modules->HasOwnProperty(isolate->GetCurrentContext(), key).FromJust()); | 260 DCHECK(modules->HasOwnProperty(key)); |
| 266 return modules->Get(isolate->GetCurrentContext(), key).ToLocalChecked(); | 261 return modules->Get(key); |
| 267 } | 262 } |
| 268 | 263 |
| 269 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 264 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
| 270 bool keep_trying = true; | 265 bool keep_trying = true; |
| 271 while (keep_trying) { | 266 while (keep_trying) { |
| 272 keep_trying = false; | 267 keep_trying = false; |
| 273 PendingModuleVector pending_modules; | 268 PendingModuleVector pending_modules; |
| 274 pending_modules.swap(pending_modules_); | 269 pending_modules.swap(pending_modules_); |
| 275 for (size_t i = 0; i < pending_modules.size(); ++i) { | 270 for (size_t i = 0; i < pending_modules.size(); ++i) { |
| 276 scoped_ptr<PendingModule> pending(pending_modules[i]); | 271 scoped_ptr<PendingModule> pending(pending_modules[i]); |
| 277 pending_modules[i] = NULL; | 272 pending_modules[i] = NULL; |
| 278 if (AttemptToLoad(isolate, pending.Pass())) | 273 if (AttemptToLoad(isolate, pending.Pass())) |
| 279 keep_trying = true; | 274 keep_trying = true; |
| 280 } | 275 } |
| 281 } | 276 } |
| 282 } | 277 } |
| 283 | 278 |
| 284 } // namespace gin | 279 } // namespace gin |
| OLD | NEW |