| 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 <assert.h> | |
| 8 #include <string> | 7 #include <string> |
| 9 #include <vector> | 8 #include <vector> |
| 9 #include "base/logging.h" |
| 10 #include "gin/arguments.h" | 10 #include "gin/arguments.h" |
| 11 #include "gin/converter.h" | 11 #include "gin/converter.h" |
| 12 #include "gin/per_isolate_data.h" | 12 #include "gin/per_isolate_data.h" |
| 13 #include "gin/wrapper_info.h" | 13 #include "gin/wrapper_info.h" |
| 14 | 14 |
| 15 using v8::External; | 15 using v8::External; |
| 16 using v8::Handle; | 16 using v8::Handle; |
| 17 using v8::Isolate; | 17 using v8::Isolate; |
| 18 using v8::ObjectTemplate; | 18 using v8::ObjectTemplate; |
| 19 | 19 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 97 } |
| 98 | 98 |
| 99 void ModuleRegistry::RegisterGlobals(v8::Isolate* isolate, | 99 void ModuleRegistry::RegisterGlobals(v8::Isolate* isolate, |
| 100 Handle<v8::ObjectTemplate> templ) { | 100 Handle<v8::ObjectTemplate> templ) { |
| 101 templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate)); | 101 templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, | 104 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, |
| 105 const std::string& id, | 105 const std::string& id, |
| 106 Handle<ObjectTemplate> templ) { | 106 Handle<ObjectTemplate> templ) { |
| 107 assert(!id.empty()); | 107 DCHECK(!id.empty()); |
| 108 Handle<v8::Object> modules = v8::Local<v8::Object>::New(isolate, modules_); | 108 Handle<v8::Object> modules = v8::Local<v8::Object>::New(isolate, modules_); |
| 109 modules->Set(StringToV8(isolate, id), templ->NewInstance()); | 109 modules->Set(StringToV8(isolate, id), templ->NewInstance()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 ModuleRegistry* ModuleRegistry::From(Handle<v8::Context> context) { | 112 ModuleRegistry* ModuleRegistry::From(Handle<v8::Context> context) { |
| 113 v8::Isolate* isolate = context->GetIsolate(); | 113 v8::Isolate* isolate = context->GetIsolate(); |
| 114 Handle<v8::String> key = GetHiddenValueKey(isolate); | 114 Handle<v8::String> key = GetHiddenValueKey(isolate); |
| 115 Handle<v8::Value> value = context->Global()->GetHiddenValue(key); | 115 Handle<v8::Value> value = context->Global()->GetHiddenValue(key); |
| 116 Handle<v8::External> external; | 116 Handle<v8::External> external; |
| 117 if (value.IsEmpty() || !ConvertFromV8(value, &external)) { | 117 if (value.IsEmpty() || !ConvertFromV8(value, &external)) { |
| 118 PerContextData* data = PerContextData::From(context); | 118 PerContextData* data = PerContextData::From(context); |
| 119 if (!data) | 119 if (!data) |
| 120 return NULL; | 120 return NULL; |
| 121 ModuleRegistry* registry = new ModuleRegistry(isolate); | 121 ModuleRegistry* registry = new ModuleRegistry(isolate); |
| 122 context->Global()->SetHiddenValue(key, v8::External::New(registry)); | 122 context->Global()->SetHiddenValue(key, v8::External::New(registry)); |
| 123 data->AddSupplement(registry); | 123 data->AddSupplement(scoped_ptr<ContextSupplement>(registry)); |
| 124 return registry; | 124 return registry; |
| 125 } | 125 } |
| 126 return static_cast<ModuleRegistry*>(external->Value()); | 126 return static_cast<ModuleRegistry*>(external->Value()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 void ModuleRegistry::AddPendingModule(v8::Isolate* isolate, | 129 void ModuleRegistry::AddPendingModule(v8::Isolate* isolate, |
| 130 PendingModule* pending) { | 130 PendingModule* pending) { |
| 131 if (AttemptToLoad(isolate, pending)) | 131 if (AttemptToLoad(isolate, pending)) |
| 132 AttemptToLoadPendingModules(isolate); | 132 AttemptToLoadPendingModules(isolate); |
| 133 } | 133 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 void ModuleRegistry::AttemptToLoadPendingModules(v8::Isolate* isolate) { | 179 void ModuleRegistry::AttemptToLoadPendingModules(v8::Isolate* isolate) { |
| 180 PendingModuleList pending_modules; | 180 PendingModuleList pending_modules; |
| 181 pending_modules.swap(pending_modules_); | 181 pending_modules.swap(pending_modules_); |
| 182 for (PendingModuleList::iterator it = pending_modules.begin(); | 182 for (PendingModuleList::iterator it = pending_modules.begin(); |
| 183 it != pending_modules.end(); ++it) { | 183 it != pending_modules.end(); ++it) { |
| 184 AttemptToLoad(isolate, *it); | 184 AttemptToLoad(isolate, *it); |
| 185 } | 185 } |
| 186 } | 186 } |
| 187 | 187 |
| 188 } // namespace gin | 188 } // namespace gin |
| OLD | NEW |