| 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" |
| 11 #include "gin/arguments.h" | 11 #include "gin/arguments.h" |
| 12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 13 #include "gin/per_isolate_data.h" | 13 #include "gin/per_isolate_data.h" |
| 14 #include "gin/public/wrapper_info.h" | 14 #include "gin/public/wrapper_info.h" |
| 15 #include "gin/try_catch.h" |
| 15 | 16 |
| 16 using v8::Context; | 17 using v8::Context; |
| 17 using v8::External; | 18 using v8::External; |
| 18 using v8::Function; | 19 using v8::Function; |
| 19 using v8::FunctionTemplate; | 20 using v8::FunctionTemplate; |
| 20 using v8::Handle; | 21 using v8::Handle; |
| 21 using v8::Isolate; | 22 using v8::Isolate; |
| 22 using v8::Local; | 23 using v8::Local; |
| 23 using v8::Object; | 24 using v8::Object; |
| 24 using v8::ObjectTemplate; | 25 using v8::ObjectTemplate; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 templ = FunctionTemplate::New(Define); | 85 templ = FunctionTemplate::New(Define); |
| 85 data->SetFunctionTemplate(&g_wrapper_info, templ); | 86 data->SetFunctionTemplate(&g_wrapper_info, templ); |
| 86 } | 87 } |
| 87 return templ; | 88 return templ; |
| 88 } | 89 } |
| 89 | 90 |
| 90 Handle<String> GetHiddenValueKey(Isolate* isolate) { | 91 Handle<String> GetHiddenValueKey(Isolate* isolate) { |
| 91 return StringToSymbol(isolate, "::gin::ModuleRegistry"); | 92 return StringToSymbol(isolate, "::gin::ModuleRegistry"); |
| 92 } | 93 } |
| 93 | 94 |
| 94 std::string GetImplicitModuleName(const std::string& explicit_name) { | |
| 95 if (!explicit_name.empty()) | |
| 96 return explicit_name; | |
| 97 std::string implicit_name; | |
| 98 Handle<StackTrace> trace = StackTrace::CurrentStackTrace(1); | |
| 99 if (!trace->GetFrameCount()) | |
| 100 return implicit_name; | |
| 101 Handle<String> script_name = trace->GetFrame(0)->GetScriptName(); | |
| 102 if (!script_name.IsEmpty()) | |
| 103 ConvertFromV8(script_name, &implicit_name); | |
| 104 return implicit_name; | |
| 105 } | |
| 106 | |
| 107 } // namespace | 95 } // namespace |
| 108 | 96 |
| 109 ModuleRegistry::ModuleRegistry(Isolate* isolate) | 97 ModuleRegistry::ModuleRegistry(Isolate* isolate) |
| 110 : modules_(isolate, Object::New()) { | 98 : modules_(isolate, Object::New()) { |
| 111 } | 99 } |
| 112 | 100 |
| 113 ModuleRegistry::~ModuleRegistry() { | 101 ModuleRegistry::~ModuleRegistry() { |
| 114 modules_.Reset(); | 102 modules_.Reset(); |
| 115 } | 103 } |
| 116 | 104 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); | 177 Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); |
| 190 DCHECK(modules->HasOwnProperty(key)); | 178 DCHECK(modules->HasOwnProperty(key)); |
| 191 argv[i] = modules->Get(key); | 179 argv[i] = modules->Get(key); |
| 192 } | 180 } |
| 193 | 181 |
| 194 Handle<Value> module = Local<Value>::New(isolate, pending->factory); | 182 Handle<Value> module = Local<Value>::New(isolate, pending->factory); |
| 195 | 183 |
| 196 Handle<Function> factory; | 184 Handle<Function> factory; |
| 197 if (ConvertFromV8(module, &factory)) { | 185 if (ConvertFromV8(module, &factory)) { |
| 198 Handle<Object> global = isolate->GetCurrentContext()->Global(); | 186 Handle<Object> global = isolate->GetCurrentContext()->Global(); |
| 199 module = factory->Call(global, argc, argv.data()); | 187 { |
| 200 // TODO(abarth): What should we do with exceptions? | 188 gin::TryCatch try_catch; |
| 189 module = factory->Call(global, argc, argv.data()); |
| 190 if (try_catch.HasCaught()) |
| 191 return; // TODO(abarth): What should we do with the exception? |
| 192 } |
| 193 if (pending->id.empty()) |
| 194 ConvertFromV8(factory->GetScriptOrigin().ResourceName(), &pending->id); |
| 201 } | 195 } |
| 202 | 196 |
| 203 RegisterModule(isolate, GetImplicitModuleName(pending->id), module); | 197 RegisterModule(isolate, pending->id, module); |
| 204 } | 198 } |
| 205 | 199 |
| 206 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 200 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
| 207 scoped_ptr<PendingModule> pending) { | 201 scoped_ptr<PendingModule> pending) { |
| 208 if (!CheckDependencies(pending.get())) { | 202 if (!CheckDependencies(pending.get())) { |
| 209 pending_modules_.push_back(pending.release()); | 203 pending_modules_.push_back(pending.release()); |
| 210 return false; | 204 return false; |
| 211 } | 205 } |
| 212 Load(isolate, pending.Pass()); | 206 Load(isolate, pending.Pass()); |
| 213 return true; | 207 return true; |
| 214 } | 208 } |
| 215 | 209 |
| 216 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 210 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
| 217 bool keep_trying = true; | 211 bool keep_trying = true; |
| 218 while (keep_trying) { | 212 while (keep_trying) { |
| 219 keep_trying = false; | 213 keep_trying = false; |
| 220 PendingModuleVector pending_modules; | 214 PendingModuleVector pending_modules; |
| 221 pending_modules.swap(pending_modules_); | 215 pending_modules.swap(pending_modules_); |
| 222 for (size_t i = 0; i < pending_modules.size(); ++i) { | 216 for (size_t i = 0; i < pending_modules.size(); ++i) { |
| 223 scoped_ptr<PendingModule> pending(pending_modules[i]); | 217 scoped_ptr<PendingModule> pending(pending_modules[i]); |
| 224 pending_modules[i] = NULL; | 218 pending_modules[i] = NULL; |
| 225 if (AttemptToLoad(isolate, pending.Pass())) | 219 if (AttemptToLoad(isolate, pending.Pass())) |
| 226 keep_trying = true; | 220 keep_trying = true; |
| 227 } | 221 } |
| 228 } | 222 } |
| 229 } | 223 } |
| 230 | 224 |
| 231 } // namespace gin | 225 } // namespace gin |
| OLD | NEW |