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