Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: gin/modules/module_registry.cc

Issue 179803007: Refactors parts of gin: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge 2 trunk Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: gin/modules/module_registry.cc
diff --git a/gin/modules/module_registry.cc b/gin/modules/module_registry.cc
index eda34880e7149e9b71c6c38069448da29f37f753..8d29893dac1fa1ae51ac6adaf05b451924fe0c43 100644
--- a/gin/modules/module_registry.cc
+++ b/gin/modules/module_registry.cc
@@ -11,6 +11,7 @@
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/per_isolate_data.h"
+#include "gin/public/context_holder.h"
#include "gin/public/wrapper_info.h"
#include "gin/runner.h"
@@ -29,15 +30,6 @@ using v8::Value;
namespace gin {
-struct PendingModule {
- PendingModule();
- ~PendingModule();
-
- std::string id;
- std::vector<std::string> dependencies;
- Persistent<Value> factory;
-};
-
PendingModule::PendingModule() {
}
@@ -69,9 +61,9 @@ void Define(const v8::FunctionCallbackInfo<Value>& info) {
pending->dependencies = dependencies;
pending->factory.Reset(args.isolate(), factory);
- ModuleRegistry* registry =
- ModuleRegistry::From(args.isolate()->GetCurrentContext());
- registry->AddPendingModule(args.isolate(), pending.Pass());
+ ModuleLoader* loader =
+ ModuleLoader::From(args.isolate()->GetCurrentContext());
+ loader->Load(args.isolate()->GetCurrentContext(), pending.Pass());
}
WrapperInfo g_wrapper_info = { kEmbedderNativeGin };
@@ -87,12 +79,59 @@ Local<FunctionTemplate> GetDefineTemplate(Isolate* isolate) {
return templ;
}
-v8::Handle<String> GetHiddenValueKey(Isolate* isolate) {
+v8::Handle<String> GetModuleRegistryHiddenValueKey(Isolate* isolate) {
return StringToSymbol(isolate, "::gin::ModuleRegistry");
}
+v8::Handle<String> GetModuleLoaderHiddenValueKey(Isolate* isolate) {
+ return StringToSymbol(isolate, "::gin::ModuleLoader");
+}
+
} // namespace
+ModuleLoader* ModuleLoader::From(v8::Handle<Context> context) {
+ Isolate* isolate = context->GetIsolate();
+ v8::Handle<String> key = GetModuleLoaderHiddenValueKey(isolate);
+ v8::Handle<Value> value = context->Global()->GetHiddenValue(key);
+ v8::Handle<External> external;
+ return (value.IsEmpty() || !ConvertFromV8(isolate, value, &external)) ?
+ NULL : static_cast<ModuleLoader*>(external->Value());
+}
+
+ModuleLoader::ModuleLoader(v8::Handle<Context> context) {
+ Isolate* isolate = context->GetIsolate();
+ v8::Handle<String> key = GetModuleLoaderHiddenValueKey(isolate);
+ v8::Handle<Value> value = context->Global()->GetHiddenValue(key);
+ v8::Handle<External> external;
+ DCHECK(value.IsEmpty() || !ConvertFromV8(isolate, value, &external));
+ PerContextData* data = PerContextData::From(context);
+ DCHECK(data);
+ context->Global()->SetHiddenValue(key, External::New(isolate, this));
+ data->AddSupplement(scoped_ptr<ContextSupplement>(this));
+}
+
+ModuleLoader::~ModuleLoader() {
+}
+
+// static
+void ModuleLoader::RegisterGlobals(Isolate* isolate,
+ v8::Handle<ObjectTemplate> templ) {
+ templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate));
+}
+
+// static
+void ModuleLoader::RegisterGlobalsObject(v8::Isolate* isolate,
+ v8::Handle<v8::Object> obj) {
+ obj->Set(StringToSymbol(isolate, "define"),
+ GetDefineTemplate(isolate)->GetFunction());
+}
+
+void ModuleLoader::Detach(v8::Handle<Context> context) {
+ context->Global()->SetHiddenValue(GetModuleLoaderHiddenValueKey(
+ context->GetIsolate()),
+ v8::Handle<Value>());
+}
+
ModuleRegistry::ModuleRegistry(Isolate* isolate)
: modules_(isolate, Object::New(isolate)) {
}
@@ -101,14 +140,10 @@ ModuleRegistry::~ModuleRegistry() {
modules_.Reset();
}
-void ModuleRegistry::RegisterGlobals(Isolate* isolate,
- v8::Handle<ObjectTemplate> templ) {
- templ->Set(StringToSymbol(isolate, "define"), GetDefineTemplate(isolate));
-}
-
+// static
ModuleRegistry* ModuleRegistry::From(v8::Handle<Context> context) {
Isolate* isolate = context->GetIsolate();
- v8::Handle<String> key = GetHiddenValueKey(isolate);
+ v8::Handle<String> key = GetModuleRegistryHiddenValueKey(isolate);
v8::Handle<Value> value = context->Global()->GetHiddenValue(key);
v8::Handle<External> external;
if (value.IsEmpty() || !ConvertFromV8(isolate, value, &external)) {
@@ -169,7 +204,8 @@ void ModuleRegistry::RegisterModule(Isolate* isolate,
}
void ModuleRegistry::Detach(v8::Handle<Context> context) {
- context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()),
+ context->Global()->SetHiddenValue(GetModuleRegistryHiddenValueKey(
+ context->GetIsolate()),
v8::Handle<Value>());
}

Powered by Google App Engine
This is Rietveld 408576698