Index: chrome/renderer/module_system.cc |
diff --git a/chrome/renderer/module_system.cc b/chrome/renderer/module_system.cc |
index 6c685f3b888badf51fe9aa789329176e9150b840..96f79cbb59b332185667393287687da0e04e81fb 100644 |
--- a/chrome/renderer/module_system.cc |
+++ b/chrome/renderer/module_system.cc |
@@ -5,40 +5,63 @@ |
#include "chrome/renderer/module_system.h" |
#include "base/bind.h" |
-#include "chrome/renderer/static_v8_external_string_resource.h" |
-#include "grit/renderer_resources.h" |
-#include "ui/base/resource/resource_bundle.h" |
namespace { |
-v8::Handle<v8::String> GetResource(int resourceId) { |
- const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
- return v8::String::NewExternal(new StaticV8ExternalAsciiStringResource( |
- resource_bundle.GetRawDataResource(resourceId))); |
-} |
- |
} // namespace |
-ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map) |
- : source_map_(source_map) { |
- RouteFunction("Run", |
- base::Bind(&ModuleSystem::Run, base::Unretained(this))); |
- RouteFunction("GetSource", |
- base::Bind(&ModuleSystem::GetSource, base::Unretained(this))); |
- RouteFunction("GetNative", |
+ModuleSystem::ModuleSystem(SourceMap* source_map) |
+ : source_map_(source_map), |
+ natives_enabled_(true) { |
+ RouteFunction("require", |
+ base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
+ RouteFunction("requireNative", |
base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); |
+ |
+ v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
+ global->SetHiddenValue(v8::String::New("modules"), v8::Object::New()); |
} |
ModuleSystem::~ModuleSystem() { |
} |
void ModuleSystem::Require(const std::string& module_name) { |
- EnsureRequireLoaded(); |
v8::HandleScope handle_scope; |
- v8::Handle<v8::Value> argv[] = { |
- v8::String::New(module_name.c_str()), |
+ RequireForJsInner(v8::String::New(module_name.c_str())); |
+} |
+ |
+v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
+ v8::HandleScope handle_scope; |
+ v8::Handle<v8::String> module_name = args[0]->ToString(); |
+ return handle_scope.Close(RequireForJsInner(module_name)); |
+} |
+ |
+v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
+ v8::Handle<v8::String> module_name) { |
+ v8::HandleScope handle_scope; |
+ v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
+ v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( |
+ global->GetHiddenValue(v8::String::New("modules")))); |
+ v8::Handle<v8::Value> exports(modules->Get(module_name)); |
+ if (!exports->IsUndefined()) |
+ return handle_scope.Close(exports); |
+ v8::Handle<v8::Value> source(GetSource(module_name)); |
+ if (source->IsUndefined()) |
+ return handle_scope.Close(v8::Undefined()); |
+ v8::Handle<v8::String> wrapped_source(WrapSource( |
+ v8::Handle<v8::String>::Cast(source))); |
+ v8::Handle<v8::Function> func = |
+ v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); |
+ exports = v8::Object::New(); |
+ v8::Handle<v8::Object> natives(NewInstance()); |
+ v8::Handle<v8::Value> args[] = { |
+ natives->Get(v8::String::NewSymbol("require")), |
+ natives->Get(v8::String::NewSymbol("requireNative")), |
+ exports, |
}; |
- require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv); |
+ func->Call(global, 3, args); |
+ modules->Set(module_name, exports); |
+ return handle_scope.Close(exports); |
} |
void ModuleSystem::RegisterNativeHandler(const std::string& name, |
@@ -47,52 +70,47 @@ void ModuleSystem::RegisterNativeHandler(const std::string& name, |
linked_ptr<NativeHandler>(native_handler.release()); |
} |
-void ModuleSystem::EnsureRequireLoaded() { |
+void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
v8::HandleScope handle_scope; |
- if (!require_.IsEmpty()) |
- return; |
- v8::Handle<v8::Object> bootstrap = NewInstance(); |
- v8::Handle<v8::Value> result = RunString(GetResource(IDR_REQUIRE_JS)); |
- v8::Handle<v8::Function> require_factory = |
- v8::Handle<v8::Function>::Cast(result); |
- CHECK(!require_factory.IsEmpty()) |
- << "require.js should define a function that returns a require() " |
- << "function"; |
- v8::Handle<v8::Value> argv[] = { |
- bootstrap, |
- }; |
- v8::Handle<v8::Value> require = require_factory->Call( |
- v8::Context::GetCurrent()->Global(), arraysize(argv), argv); |
- require_ = v8::Persistent<v8::Function>::New( |
- v8::Handle<v8::Function>::Cast(require)); |
+ RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
} |
-v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) { |
- v8::HandleScope handle_scope; |
- return handle_scope.Close(v8::Script::New(code)->Run()); |
-} |
-v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) { |
- CHECK_EQ(1, args.Length()); |
+v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
+ v8::Handle<v8::String> name) { |
v8::HandleScope handle_scope; |
- return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run()); |
+ return handle_scope.Close(v8::Script::New(code, name)->Run()); |
} |
-v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) { |
- CHECK_EQ(1, args.Length()); |
- std::string module_name = *v8::String::AsciiValue(args[0]->ToString()); |
- std::map<std::string, std::string>::const_iterator p = |
- source_map_->find(module_name); |
- if (p == source_map_->end()) |
+v8::Handle<v8::Value> ModuleSystem::GetSource( |
+ v8::Handle<v8::String> source_name) { |
+ v8::HandleScope handle_scope; |
+ std::string module_name = *v8::String::AsciiValue(source_name); |
+ if (!source_map_->Contains(module_name)) |
return v8::Undefined(); |
- return v8::String::New(p->second.c_str()); |
+ return handle_scope.Close(source_map_->GetSource(module_name)); |
} |
v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { |
CHECK_EQ(1, args.Length()); |
+ if (!natives_enabled_) |
+ return ThrowException("Natives disabled"); |
std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
if (i == native_handler_map_.end()) |
return v8::Undefined(); |
return i->second->NewInstance(); |
} |
+ |
+v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
+ v8::HandleScope handle_scope; |
+ v8::Handle<v8::String> left = |
+ v8::String::New("(function(require, requireNative, exports) {"); |
+ v8::Handle<v8::String> right = v8::String::New("\n})"); |
+ return handle_scope.Close( |
+ v8::String::Concat(left, v8::String::Concat(source, right))); |
+} |
+ |
+v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
+ return v8::ThrowException(v8::String::New(message.c_str())); |
+} |