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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 }; | 57 }; |
58 | 58 |
59 void Define(const v8::FunctionCallbackInfo<Value>& info) { | 59 void Define(const v8::FunctionCallbackInfo<Value>& info) { |
60 Arguments args(info); | 60 Arguments args(info); |
61 | 61 |
62 if (!info.Length()) | 62 if (!info.Length()) |
63 return args.ThrowTypeError("At least one argument is required."); | 63 return args.ThrowTypeError("At least one argument is required."); |
64 | 64 |
65 std::string id; | 65 std::string id; |
66 std::vector<std::string> dependencies; | 66 std::vector<std::string> dependencies; |
67 v8::Handle<Value> factory; | 67 v8::Local<Value> factory; |
68 | 68 |
69 if (args.PeekNext()->IsString()) | 69 if (args.PeekNext()->IsString()) |
70 args.GetNext(&id); | 70 args.GetNext(&id); |
71 if (args.PeekNext()->IsArray()) | 71 if (args.PeekNext()->IsArray()) |
72 args.GetNext(&dependencies); | 72 args.GetNext(&dependencies); |
73 if (!args.GetNext(&factory)) | 73 if (!args.GetNext(&factory)) |
74 return args.ThrowError(); | 74 return args.ThrowError(); |
75 | 75 |
76 scoped_ptr<PendingModule> pending(new PendingModule); | 76 scoped_ptr<PendingModule> pending(new PendingModule); |
77 pending->id = id; | 77 pending->id = id; |
(...skipping 23 matching lines...) Expand all Loading... |
101 ModuleRegistry::ModuleRegistry(Isolate* isolate) | 101 ModuleRegistry::ModuleRegistry(Isolate* isolate) |
102 : modules_(isolate, Object::New(isolate)) { | 102 : modules_(isolate, Object::New(isolate)) { |
103 } | 103 } |
104 | 104 |
105 ModuleRegistry::~ModuleRegistry() { | 105 ModuleRegistry::~ModuleRegistry() { |
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::Handle<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 void ModuleRegistry::InstallGlobals(v8::Isolate* isolate, |
117 v8::Handle<v8::Object> obj) { | 117 v8::Local<v8::Object> obj) { |
118 obj->Set(StringToSymbol(isolate, "define"), | 118 obj->Set(StringToSymbol(isolate, "define"), |
119 GetDefineTemplate(isolate)->GetFunction()); | 119 GetDefineTemplate(isolate)->GetFunction()); |
120 } | 120 } |
121 | 121 |
122 // static | 122 // static |
123 ModuleRegistry* ModuleRegistry::From(v8::Handle<Context> context) { | 123 ModuleRegistry* ModuleRegistry::From(v8::Local<Context> context) { |
124 PerContextData* data = PerContextData::From(context); | 124 PerContextData* data = PerContextData::From(context); |
125 if (!data) | 125 if (!data) |
126 return NULL; | 126 return NULL; |
127 | 127 |
128 ModuleRegistryData* registry_data = static_cast<ModuleRegistryData*>( | 128 ModuleRegistryData* registry_data = static_cast<ModuleRegistryData*>( |
129 data->GetUserData(kModuleRegistryKey)); | 129 data->GetUserData(kModuleRegistryKey)); |
130 if (!registry_data) { | 130 if (!registry_data) { |
131 // PerContextData takes ownership of ModuleRegistryData. | 131 // PerContextData takes ownership of ModuleRegistryData. |
132 registry_data = new ModuleRegistryData; | 132 registry_data = new ModuleRegistryData; |
133 registry_data->registry.reset(new ModuleRegistry(context->GetIsolate())); | 133 registry_data->registry.reset(new ModuleRegistry(context->GetIsolate())); |
134 data->SetUserData(kModuleRegistryKey, registry_data); | 134 data->SetUserData(kModuleRegistryKey, registry_data); |
135 } | 135 } |
136 return registry_data->registry.get(); | 136 return registry_data->registry.get(); |
137 } | 137 } |
138 | 138 |
139 void ModuleRegistry::AddObserver(ModuleRegistryObserver* observer) { | 139 void ModuleRegistry::AddObserver(ModuleRegistryObserver* observer) { |
140 observer_list_.AddObserver(observer); | 140 observer_list_.AddObserver(observer); |
141 } | 141 } |
142 | 142 |
143 void ModuleRegistry::RemoveObserver(ModuleRegistryObserver* observer) { | 143 void ModuleRegistry::RemoveObserver(ModuleRegistryObserver* observer) { |
144 observer_list_.RemoveObserver(observer); | 144 observer_list_.RemoveObserver(observer); |
145 } | 145 } |
146 | 146 |
147 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, const std::string& id, | 147 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, const std::string& id, |
148 v8::Handle<Value> module) { | 148 v8::Local<Value> module) { |
149 DCHECK(!id.empty()); | 149 DCHECK(!id.empty()); |
150 RegisterModule(isolate, id, module); | 150 RegisterModule(isolate, id, module); |
151 } | 151 } |
152 | 152 |
153 void ModuleRegistry::AddPendingModule(Isolate* isolate, | 153 void ModuleRegistry::AddPendingModule(Isolate* isolate, |
154 scoped_ptr<PendingModule> pending) { | 154 scoped_ptr<PendingModule> pending) { |
155 const std::string pending_id = pending->id; | 155 const std::string pending_id = pending->id; |
156 const std::vector<std::string> pending_dependencies = pending->dependencies; | 156 const std::vector<std::string> pending_dependencies = pending->dependencies; |
157 AttemptToLoad(isolate, pending.Pass()); | 157 AttemptToLoad(isolate, pending.Pass()); |
158 FOR_EACH_OBSERVER(ModuleRegistryObserver, observer_list_, | 158 FOR_EACH_OBSERVER(ModuleRegistryObserver, observer_list_, |
(...skipping 13 matching lines...) Expand all Loading... |
172 for (size_t i = 0; i < pending_modules_.size(); ++i) { | 172 for (size_t i = 0; i < pending_modules_.size(); ++i) { |
173 if (pending_modules_[i]->id == id) | 173 if (pending_modules_[i]->id == id) |
174 return; | 174 return; |
175 } | 175 } |
176 | 176 |
177 unsatisfied_dependencies_.insert(id); | 177 unsatisfied_dependencies_.insert(id); |
178 } | 178 } |
179 | 179 |
180 void ModuleRegistry::RegisterModule(Isolate* isolate, | 180 void ModuleRegistry::RegisterModule(Isolate* isolate, |
181 const std::string& id, | 181 const std::string& id, |
182 v8::Handle<Value> module) { | 182 v8::Local<Value> module) { |
183 if (id.empty() || module.IsEmpty()) | 183 if (id.empty() || module.IsEmpty()) |
184 return; | 184 return; |
185 | 185 |
186 unsatisfied_dependencies_.erase(id); | 186 unsatisfied_dependencies_.erase(id); |
187 available_modules_.insert(id); | 187 available_modules_.insert(id); |
188 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | 188 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
189 modules->Set(StringToSymbol(isolate, id), module); | 189 modules->Set(StringToSymbol(isolate, id), module); |
190 | 190 |
191 std::pair<LoadModuleCallbackMap::iterator, LoadModuleCallbackMap::iterator> | 191 std::pair<LoadModuleCallbackMap::iterator, LoadModuleCallbackMap::iterator> |
192 range = waiting_callbacks_.equal_range(id); | 192 range = waiting_callbacks_.equal_range(id); |
193 std::vector<LoadModuleCallback> callbacks; | 193 std::vector<LoadModuleCallback> callbacks; |
194 callbacks.reserve(waiting_callbacks_.count(id)); | 194 callbacks.reserve(waiting_callbacks_.count(id)); |
195 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; | 195 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; |
196 ++it) { | 196 ++it) { |
197 callbacks.push_back(it->second); | 197 callbacks.push_back(it->second); |
198 } | 198 } |
(...skipping 17 matching lines...) Expand all Loading... |
216 num_missing_dependencies++; | 216 num_missing_dependencies++; |
217 } | 217 } |
218 return num_missing_dependencies == 0; | 218 return num_missing_dependencies == 0; |
219 } | 219 } |
220 | 220 |
221 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { | 221 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { |
222 if (!pending->id.empty() && available_modules_.count(pending->id)) | 222 if (!pending->id.empty() && available_modules_.count(pending->id)) |
223 return; // We've already loaded this module. | 223 return; // We've already loaded this module. |
224 | 224 |
225 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); | 225 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); |
226 std::vector<v8::Handle<Value> > argv(argc); | 226 std::vector<v8::Local<Value> > argv(argc); |
227 for (uint32_t i = 0; i < argc; ++i) | 227 for (uint32_t i = 0; i < argc; ++i) |
228 argv[i] = GetModule(isolate, pending->dependencies[i]); | 228 argv[i] = GetModule(isolate, pending->dependencies[i]); |
229 | 229 |
230 v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); | 230 v8::Local<Value> module = Local<Value>::New(isolate, pending->factory); |
231 | 231 |
232 v8::Handle<Function> factory; | 232 v8::Local<Function> factory; |
233 if (ConvertFromV8(isolate, module, &factory)) { | 233 if (ConvertFromV8(isolate, module, &factory)) { |
234 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); | 234 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); |
235 Runner* runner = data->runner(); | 235 Runner* runner = data->runner(); |
236 module = runner->Call(factory, runner->global(), argc, | 236 module = runner->Call(factory, runner->global(), argc, |
237 argv.empty() ? NULL : &argv.front()); | 237 argv.empty() ? NULL : &argv.front()); |
238 if (pending->id.empty()) | 238 if (pending->id.empty()) |
239 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), | 239 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), |
240 &pending->id); | 240 &pending->id); |
241 } | 241 } |
242 | 242 |
243 RegisterModule(isolate, pending->id, module); | 243 RegisterModule(isolate, pending->id, module); |
244 } | 244 } |
245 | 245 |
246 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 246 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
247 scoped_ptr<PendingModule> pending) { | 247 scoped_ptr<PendingModule> pending) { |
248 if (!CheckDependencies(pending.get())) { | 248 if (!CheckDependencies(pending.get())) { |
249 pending_modules_.push_back(pending.release()); | 249 pending_modules_.push_back(pending.release()); |
250 return false; | 250 return false; |
251 } | 251 } |
252 Load(isolate, pending.Pass()); | 252 Load(isolate, pending.Pass()); |
253 return true; | 253 return true; |
254 } | 254 } |
255 | 255 |
256 v8::Handle<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, | 256 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, |
257 const std::string& id) { | 257 const std::string& id) { |
258 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | 258 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
259 v8::Handle<String> key = StringToSymbol(isolate, id); | 259 v8::Local<String> key = StringToSymbol(isolate, id); |
260 DCHECK(modules->HasOwnProperty(key)); | 260 DCHECK(modules->HasOwnProperty(key)); |
261 return modules->Get(key); | 261 return modules->Get(key); |
262 } | 262 } |
263 | 263 |
264 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 264 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
265 bool keep_trying = true; | 265 bool keep_trying = true; |
266 while (keep_trying) { | 266 while (keep_trying) { |
267 keep_trying = false; | 267 keep_trying = false; |
268 PendingModuleVector pending_modules; | 268 PendingModuleVector pending_modules; |
269 pending_modules.swap(pending_modules_); | 269 pending_modules.swap(pending_modules_); |
270 for (size_t i = 0; i < pending_modules.size(); ++i) { | 270 for (size_t i = 0; i < pending_modules.size(); ++i) { |
271 scoped_ptr<PendingModule> pending(pending_modules[i]); | 271 scoped_ptr<PendingModule> pending(pending_modules[i]); |
272 pending_modules[i] = NULL; | 272 pending_modules[i] = NULL; |
273 if (AttemptToLoad(isolate, pending.Pass())) | 273 if (AttemptToLoad(isolate, pending.Pass())) |
274 keep_trying = true; | 274 keep_trying = true; |
275 } | 275 } |
276 } | 276 } |
277 } | 277 } |
278 | 278 |
279 } // namespace gin | 279 } // namespace gin |
OLD | NEW |