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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 v8::Handle<ObjectTemplate> templ) { | 128 v8::Handle<ObjectTemplate> templ) { |
129 DCHECK(!id.empty()); | 129 DCHECK(!id.empty()); |
130 RegisterModule(isolate, id, templ->NewInstance()); | 130 RegisterModule(isolate, id, templ->NewInstance()); |
131 } | 131 } |
132 | 132 |
133 void ModuleRegistry::AddPendingModule(Isolate* isolate, | 133 void ModuleRegistry::AddPendingModule(Isolate* isolate, |
134 scoped_ptr<PendingModule> pending) { | 134 scoped_ptr<PendingModule> pending) { |
135 AttemptToLoad(isolate, pending.Pass()); | 135 AttemptToLoad(isolate, pending.Pass()); |
136 } | 136 } |
137 | 137 |
138 void ModuleRegistry::LoadModule(Isolate* isolate, | |
139 const std::string& id, | |
140 ModuleLoadCallback callback) { | |
141 if (available_modules_.count(id)) { | |
Aaron Boodman
2013/11/27 19:02:41
I think that the implicit int->bool conversion wil
abarth-chromium
2013/11/27 19:18:09
Done.
| |
142 // Should we call the callback asynchronously? | |
Aaron Boodman
2013/11/27 19:02:41
I think so, but slightlyoff would be able to answe
abarth-chromium
2013/11/27 19:18:09
This is just used by C++, so we can change out min
| |
143 callback.Run(GetModule(isolate, id)); | |
144 return; | |
145 } | |
146 // Should we support multiple callers waiting on the same module? | |
Aaron Boodman
2013/11/27 19:02:41
Couldn't you easily write JavaScript programs that
abarth-chromium
2013/11/27 19:18:09
This API is just for C++. The JavaScript API is a
| |
147 DCHECK(waiting_callbacks_.find(id) == waiting_callbacks_.end()); | |
148 waiting_callbacks_[id] = callback; | |
149 unsatisfied_dependencies_.insert(id); | |
150 } | |
151 | |
138 void ModuleRegistry::RegisterModule(Isolate* isolate, | 152 void ModuleRegistry::RegisterModule(Isolate* isolate, |
139 const std::string& id, | 153 const std::string& id, |
140 v8::Handle<Value> module) { | 154 v8::Handle<Value> module) { |
141 if (id.empty() || module.IsEmpty()) | 155 if (id.empty() || module.IsEmpty()) |
142 return; | 156 return; |
143 | 157 |
144 unsatisfied_dependencies_.erase(id); | 158 unsatisfied_dependencies_.erase(id); |
145 available_modules_.insert(id); | 159 available_modules_.insert(id); |
146 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | 160 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); |
147 modules->Set(StringToSymbol(isolate, id), module); | 161 modules->Set(StringToSymbol(isolate, id), module); |
162 | |
163 ModuleLoadCallbackMap::iterator it = waiting_callbacks_.find(id); | |
164 if (it == waiting_callbacks_.end()) | |
165 return; | |
166 // Should we call the callback asynchronously? | |
167 it->second.Run(module); | |
148 } | 168 } |
149 | 169 |
150 void ModuleRegistry::Detach(v8::Handle<Context> context) { | 170 void ModuleRegistry::Detach(v8::Handle<Context> context) { |
151 context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()), | 171 context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()), |
152 v8::Handle<Value>()); | 172 v8::Handle<Value>()); |
153 } | 173 } |
154 | 174 |
155 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { | 175 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { |
156 size_t num_missing_dependencies = 0; | 176 size_t num_missing_dependencies = 0; |
157 size_t len = pending->dependencies.size(); | 177 size_t len = pending->dependencies.size(); |
158 for (size_t i = 0; i < len; ++i) { | 178 for (size_t i = 0; i < len; ++i) { |
159 const std::string& dependency = pending->dependencies[i]; | 179 const std::string& dependency = pending->dependencies[i]; |
160 if (available_modules_.count(dependency)) | 180 if (available_modules_.count(dependency)) |
161 continue; | 181 continue; |
162 unsatisfied_dependencies_.insert(dependency); | 182 unsatisfied_dependencies_.insert(dependency); |
163 num_missing_dependencies++; | 183 num_missing_dependencies++; |
164 } | 184 } |
165 return num_missing_dependencies == 0; | 185 return num_missing_dependencies == 0; |
166 } | 186 } |
167 | 187 |
168 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { | 188 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { |
169 if (!pending->id.empty() && available_modules_.count(pending->id)) | 189 if (!pending->id.empty() && available_modules_.count(pending->id)) |
170 return; // We've already loaded this module. | 190 return; // We've already loaded this module. |
171 | 191 |
172 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | |
173 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); | 192 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); |
174 std::vector<v8::Handle<Value> > argv(argc); | 193 std::vector<v8::Handle<Value> > argv(argc); |
175 for (uint32_t i = 0; i < argc; ++i) { | 194 for (uint32_t i = 0; i < argc; ++i) |
176 v8::Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); | 195 argv[i] = GetModule(isolate, pending->dependencies[i]); |
177 DCHECK(modules->HasOwnProperty(key)); | |
178 argv[i] = modules->Get(key); | |
179 } | |
180 | 196 |
181 v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); | 197 v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); |
182 | 198 |
183 v8::Handle<Function> factory; | 199 v8::Handle<Function> factory; |
184 if (ConvertFromV8(module, &factory)) { | 200 if (ConvertFromV8(module, &factory)) { |
185 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); | 201 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); |
186 Runner* runner = data->runner(); | 202 Runner* runner = data->runner(); |
187 module = runner->Call(factory, runner->global(), argc, argv.data()); | 203 module = runner->Call(factory, runner->global(), argc, argv.data()); |
188 if (pending->id.empty()) | 204 if (pending->id.empty()) |
189 ConvertFromV8(factory->GetScriptOrigin().ResourceName(), &pending->id); | 205 ConvertFromV8(factory->GetScriptOrigin().ResourceName(), &pending->id); |
190 } | 206 } |
191 | 207 |
192 RegisterModule(isolate, pending->id, module); | 208 RegisterModule(isolate, pending->id, module); |
193 } | 209 } |
194 | 210 |
195 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 211 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
196 scoped_ptr<PendingModule> pending) { | 212 scoped_ptr<PendingModule> pending) { |
197 if (!CheckDependencies(pending.get())) { | 213 if (!CheckDependencies(pending.get())) { |
198 pending_modules_.push_back(pending.release()); | 214 pending_modules_.push_back(pending.release()); |
199 return false; | 215 return false; |
200 } | 216 } |
201 Load(isolate, pending.Pass()); | 217 Load(isolate, pending.Pass()); |
202 return true; | 218 return true; |
203 } | 219 } |
204 | 220 |
221 v8::Handle<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, | |
222 const std::string& id) { | |
223 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | |
224 v8::Handle<String> key = StringToSymbol(isolate, id); | |
225 DCHECK(modules->HasOwnProperty(key)); | |
226 return modules->Get(key); | |
227 } | |
228 | |
205 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 229 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
206 bool keep_trying = true; | 230 bool keep_trying = true; |
207 while (keep_trying) { | 231 while (keep_trying) { |
208 keep_trying = false; | 232 keep_trying = false; |
209 PendingModuleVector pending_modules; | 233 PendingModuleVector pending_modules; |
210 pending_modules.swap(pending_modules_); | 234 pending_modules.swap(pending_modules_); |
211 for (size_t i = 0; i < pending_modules.size(); ++i) { | 235 for (size_t i = 0; i < pending_modules.size(); ++i) { |
212 scoped_ptr<PendingModule> pending(pending_modules[i]); | 236 scoped_ptr<PendingModule> pending(pending_modules[i]); |
213 pending_modules[i] = NULL; | 237 pending_modules[i] = NULL; |
214 if (AttemptToLoad(isolate, pending.Pass())) | 238 if (AttemptToLoad(isolate, pending.Pass())) |
215 keep_trying = true; | 239 keep_trying = true; |
216 } | 240 } |
217 } | 241 } |
218 } | 242 } |
219 | 243 |
220 } // namespace gin | 244 } // namespace gin |
OLD | NEW |