| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/module_system.h" | 5 #include "chrome/renderer/module_system.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| 11 const char* kModuleSystem = "module_system"; | 11 const char* kModuleSystem = "module_system"; |
| 12 const char* kModuleName = "module_name"; | 12 const char* kModuleName = "module_name"; |
| 13 const char* kModuleField = "module_field"; | 13 const char* kModuleField = "module_field"; |
| 14 | 14 |
| 15 } // namespace | 15 } // namespace |
| 16 | 16 |
| 17 ModuleSystem::ModuleSystem(SourceMap* source_map) | 17 ModuleSystem::ModuleSystem(SourceMap* source_map) |
| 18 : source_map_(source_map), | 18 : source_map_(source_map), |
| 19 natives_enabled_(true) { | 19 natives_enabled_(0) { |
| 20 RouteFunction("require", | 20 RouteFunction("require", |
| 21 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); | 21 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
| 22 RouteFunction("requireNative", | 22 RouteFunction("requireNative", |
| 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); | 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); |
| 24 | 24 |
| 25 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 25 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 26 global->SetHiddenValue(v8::String::New("modules"), v8::Object::New()); | 26 global->SetHiddenValue(v8::String::New("modules"), v8::Object::New()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ModuleSystem::~ModuleSystem() { | 29 ModuleSystem::~ModuleSystem() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 ModuleSystem::NativesEnabledScope::NativesEnabledScope( |
| 33 ModuleSystem* module_system) |
| 34 : module_system_(module_system) { |
| 35 module_system_->natives_enabled_++; |
| 36 } |
| 37 |
| 38 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 39 module_system_->natives_enabled_--; |
| 40 CHECK_GE(module_system_->natives_enabled_, 0); |
| 41 } |
| 42 |
| 32 void ModuleSystem::Require(const std::string& module_name) { | 43 void ModuleSystem::Require(const std::string& module_name) { |
| 33 v8::HandleScope handle_scope; | 44 v8::HandleScope handle_scope; |
| 34 RequireForJsInner(v8::String::New(module_name.c_str())); | 45 RequireForJsInner(v8::String::New(module_name.c_str())); |
| 35 } | 46 } |
| 36 | 47 |
| 37 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 48 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
| 38 v8::HandleScope handle_scope; | 49 v8::HandleScope handle_scope; |
| 39 v8::Handle<v8::String> module_name = args[0]->ToString(); | 50 v8::Handle<v8::String> module_name = args[0]->ToString(); |
| 40 return handle_scope.Close(RequireForJsInner(module_name)); | 51 return handle_scope.Close(RequireForJsInner(module_name)); |
| 41 } | 52 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 native_handler_map_[name] = | 84 native_handler_map_[name] = |
| 74 linked_ptr<NativeHandler>(native_handler.release()); | 85 linked_ptr<NativeHandler>(native_handler.release()); |
| 75 } | 86 } |
| 76 | 87 |
| 77 void ModuleSystem::RunString(const std::string& code, const std::string& name) { | 88 void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
| 78 v8::HandleScope handle_scope; | 89 v8::HandleScope handle_scope; |
| 79 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); | 90 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
| 80 } | 91 } |
| 81 | 92 |
| 82 // static | 93 // static |
| 83 v8::Handle<v8::Value> ModuleSystem::GetterRouter( | 94 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( |
| 84 v8::Local<v8::String> property, const v8::AccessorInfo& info) { | 95 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 85 CHECK(!info.Data().IsEmpty()); | 96 CHECK(!info.Data().IsEmpty()); |
| 86 CHECK(info.Data()->IsObject()); | 97 CHECK(info.Data()->IsObject()); |
| 87 v8::HandleScope handle_scope; | 98 v8::HandleScope handle_scope; |
| 88 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 99 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
| 89 v8::Handle<v8::Value> module_system_value = | 100 v8::Handle<v8::Value> module_system_value = |
| 90 parameters->Get(v8::String::New(kModuleSystem)); | 101 parameters->Get(v8::String::New(kModuleSystem)); |
| 91 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 102 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 92 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | 103 v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
| 93 | 104 |
| 94 v8::Handle<v8::Object> module = module_system->RequireForJsInner( | 105 v8::Handle<v8::Object> module; |
| 95 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject(); | 106 { |
| 107 NativesEnabledScope scope(module_system); |
| 108 module = module_system->RequireForJsInner( |
| 109 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject(); |
| 110 } |
| 96 | 111 |
| 97 v8::Handle<v8::String> field = | 112 v8::Handle<v8::String> field = |
| 98 parameters->Get(v8::String::New(kModuleField))->ToString(); | 113 parameters->Get(v8::String::New(kModuleField))->ToString(); |
| 99 | 114 |
| 100 return handle_scope.Close(module->Get(field)); | 115 return handle_scope.Close(module->Get(field)); |
| 101 } | 116 } |
| 102 | 117 |
| 103 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 118 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 104 const std::string& field, | 119 const std::string& field, |
| 105 const std::string& module_name, | 120 const std::string& module_name, |
| 106 const std::string& module_field) { | 121 const std::string& module_field) { |
| 107 v8::HandleScope handle_scope; | 122 v8::HandleScope handle_scope; |
| 108 v8::Handle<v8::Object> parameters = v8::Object::New(); | 123 v8::Handle<v8::Object> parameters = v8::Object::New(); |
| 109 parameters->Set(v8::String::New(kModuleName), | 124 parameters->Set(v8::String::New(kModuleName), |
| 110 v8::String::New(module_name.c_str())); | 125 v8::String::New(module_name.c_str())); |
| 111 parameters->Set(v8::String::New(kModuleField), | 126 parameters->Set(v8::String::New(kModuleField), |
| 112 v8::String::New(module_field.c_str())); | 127 v8::String::New(module_field.c_str())); |
| 113 parameters->Set(v8::String::New(kModuleSystem), v8::External::New(this)); | 128 parameters->Set(v8::String::New(kModuleSystem), v8::External::New(this)); |
| 114 | 129 |
| 115 object->SetAccessor(v8::String::New(field.c_str()), | 130 object->SetAccessor(v8::String::New(field.c_str()), |
| 116 &ModuleSystem::GetterRouter, | 131 &ModuleSystem::LazyFieldGetter, |
| 117 NULL, | 132 NULL, |
| 118 parameters); | 133 parameters); |
| 119 } | 134 } |
| 120 | 135 |
| 121 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 136 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 122 v8::Handle<v8::String> name) { | 137 v8::Handle<v8::String> name) { |
| 123 v8::HandleScope handle_scope; | 138 v8::HandleScope handle_scope; |
| 124 return handle_scope.Close(v8::Script::New(code, name)->Run()); | 139 return handle_scope.Close(v8::Script::New(code, name)->Run()); |
| 125 } | 140 } |
| 126 | 141 |
| 127 v8::Handle<v8::Value> ModuleSystem::GetSource( | 142 v8::Handle<v8::Value> ModuleSystem::GetSource( |
| 128 v8::Handle<v8::String> source_name) { | 143 v8::Handle<v8::String> source_name) { |
| 129 v8::HandleScope handle_scope; | 144 v8::HandleScope handle_scope; |
| 130 std::string module_name = *v8::String::AsciiValue(source_name); | 145 std::string module_name = *v8::String::AsciiValue(source_name); |
| 131 if (!source_map_->Contains(module_name)) | 146 if (!source_map_->Contains(module_name)) |
| 132 return v8::Undefined(); | 147 return v8::Undefined(); |
| 133 return handle_scope.Close(source_map_->GetSource(module_name)); | 148 return handle_scope.Close(source_map_->GetSource(module_name)); |
| 134 } | 149 } |
| 135 | 150 |
| 136 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { | 151 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { |
| 137 CHECK_EQ(1, args.Length()); | 152 CHECK_EQ(1, args.Length()); |
| 138 if (!natives_enabled_) | 153 if (natives_enabled_ == 0) |
| 139 return ThrowException("Natives disabled"); | 154 return ThrowException("Natives disabled"); |
| 140 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); | 155 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 141 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 156 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
| 142 if (i == native_handler_map_.end()) | 157 if (i == native_handler_map_.end()) |
| 143 return v8::Undefined(); | 158 return v8::Undefined(); |
| 144 return i->second->NewInstance(); | 159 return i->second->NewInstance(); |
| 145 } | 160 } |
| 146 | 161 |
| 147 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 162 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 148 v8::HandleScope handle_scope; | 163 v8::HandleScope handle_scope; |
| 149 v8::Handle<v8::String> left = | 164 v8::Handle<v8::String> left = |
| 150 v8::String::New("(function(require, requireNative, exports) {"); | 165 v8::String::New("(function(require, requireNative, exports) {"); |
| 151 v8::Handle<v8::String> right = v8::String::New("\n})"); | 166 v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 152 return handle_scope.Close( | 167 return handle_scope.Close( |
| 153 v8::String::Concat(left, v8::String::Concat(source, right))); | 168 v8::String::Concat(left, v8::String::Concat(source, right))); |
| 154 } | 169 } |
| 155 | 170 |
| 156 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 171 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
| 157 return v8::ThrowException(v8::String::New(message.c_str())); | 172 return v8::ThrowException(v8::String::New(message.c_str())); |
| 158 } | 173 } |
| OLD | NEW |