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>()); |
} |