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/extensions/module_system.h" | 5 #include "chrome/renderer/extensions/module_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 stack_trace.assign(*stack_value, stack_value.length()); | 96 stack_trace.assign(*stack_value, stack_value.length()); |
97 else | 97 else |
98 stack_trace = "<could not convert stack trace to string>"; | 98 stack_trace = "<could not convert stack trace to string>"; |
99 } | 99 } |
100 | 100 |
101 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] " | 101 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] " |
102 << error_message | 102 << error_message |
103 << "{" << stack_trace << "}"; | 103 << "{" << stack_trace << "}"; |
104 } | 104 } |
105 | 105 |
106 void ModuleSystem::Require(const std::string& module_name) { | 106 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
107 v8::HandleScope handle_scope; | 107 v8::HandleScope handle_scope; |
108 RequireForJsInner(v8::String::New(module_name.c_str())); | 108 return handle_scope.Close( |
| 109 RequireForJsInner(v8::String::New(module_name.c_str()))); |
109 } | 110 } |
110 | 111 |
111 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 112 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
112 v8::HandleScope handle_scope; | 113 v8::HandleScope handle_scope; |
113 v8::Handle<v8::String> module_name = args[0]->ToString(); | 114 v8::Handle<v8::String> module_name = args[0]->ToString(); |
114 return handle_scope.Close(RequireForJsInner(module_name)); | 115 return handle_scope.Close(RequireForJsInner(module_name)); |
115 } | 116 } |
116 | 117 |
117 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( | 118 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
118 v8::Handle<v8::String> module_name) { | 119 v8::Handle<v8::String> module_name) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 const std::string& method_name) { | 162 const std::string& method_name) { |
162 v8::HandleScope handle_scope; | 163 v8::HandleScope handle_scope; |
163 v8::Local<v8::Value> module = | 164 v8::Local<v8::Value> module = |
164 v8::Local<v8::Value>::New( | 165 v8::Local<v8::Value>::New( |
165 RequireForJsInner(v8::String::New(module_name.c_str()))); | 166 RequireForJsInner(v8::String::New(module_name.c_str()))); |
166 if (module.IsEmpty() || !module->IsObject()) | 167 if (module.IsEmpty() || !module->IsObject()) |
167 return; | 168 return; |
168 v8::Local<v8::Value> value = | 169 v8::Local<v8::Value> value = |
169 v8::Handle<v8::Object>::Cast(module)->Get( | 170 v8::Handle<v8::Object>::Cast(module)->Get( |
170 v8::String::New(method_name.c_str())); | 171 v8::String::New(method_name.c_str())); |
| 172 CallModuleMethod(value, 0, NULL); |
| 173 } |
| 174 |
| 175 v8::Handle<v8::Value> ModuleSystem::CallModuleMethod( |
| 176 v8::Local<v8::Value> value, |
| 177 int argc, |
| 178 v8::Handle<v8::Value> argv[]) { |
171 if (value.IsEmpty() || !value->IsFunction()) | 179 if (value.IsEmpty() || !value->IsFunction()) |
172 return; | 180 return v8::Handle<v8::Value>(); |
173 v8::Handle<v8::Function> func = | 181 v8::Handle<v8::Function> func = |
174 v8::Handle<v8::Function>::Cast(value); | 182 v8::Handle<v8::Function>::Cast(value); |
175 // TODO(jeremya/koz): refer to context_ here, not the current context. | 183 // TODO(jeremya/koz): refer to context_ here, not the current context. |
176 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 184 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 185 v8::Handle<v8::Value> ret; |
177 { | 186 { |
178 WebKit::WebScopedMicrotaskSuppression suppression; | 187 WebKit::WebScopedMicrotaskSuppression suppression; |
179 v8::TryCatch try_catch; | 188 v8::TryCatch try_catch; |
180 try_catch.SetCaptureMessage(true); | 189 try_catch.SetCaptureMessage(true); |
181 func->Call(global, 0, NULL); | 190 ret = func->Call(global, argc, argv); |
182 if (try_catch.HasCaught()) | 191 if (try_catch.HasCaught()) |
183 HandleException(try_catch); | 192 HandleException(try_catch); |
184 } | 193 } |
| 194 return ret; |
| 195 } |
| 196 |
| 197 bool ModuleSystem::HasNativeHandler(const std::string& name) { |
| 198 return native_handler_map_.find(name) != native_handler_map_.end(); |
185 } | 199 } |
186 | 200 |
187 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 201 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
188 scoped_ptr<NativeHandler> native_handler) { | 202 scoped_ptr<NativeHandler> native_handler) { |
189 native_handler_map_[name] = | 203 native_handler_map_[name] = |
190 linked_ptr<NativeHandler>(native_handler.release()); | 204 linked_ptr<NativeHandler>(native_handler.release()); |
191 } | 205 } |
192 | 206 |
193 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 207 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
194 overridden_native_handlers_.insert(name); | 208 overridden_native_handlers_.insert(name); |
195 } | 209 } |
196 | 210 |
197 void ModuleSystem::RunString(const std::string& code, const std::string& name) { | 211 void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
198 v8::HandleScope handle_scope; | 212 v8::HandleScope handle_scope; |
199 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); | 213 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
200 } | 214 } |
201 | 215 |
| 216 v8::Handle<v8::Object> ModuleSystem::GetModule( |
| 217 v8::Handle<v8::Object> parameters) { |
| 218 NativesEnabledScope scope(this); |
| 219 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast( |
| 220 RequireForJsInner( |
| 221 parameters->Get(v8::String::New(kModuleName))->ToString())); |
| 222 return module; |
| 223 } |
| 224 |
| 225 v8::Handle<v8::Object> ModuleSystem::GetNativeModule( |
| 226 v8::Handle<v8::Object> parameters) { |
| 227 NativesEnabledScope scope(this); |
| 228 std::string name = *v8::String::AsciiValue( |
| 229 parameters->Get(v8::String::New(kModuleName))->ToString()); |
| 230 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast( |
| 231 GetNativeFromString(name)); |
| 232 return module; |
| 233 } |
| 234 |
| 235 // static |
| 236 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter( |
| 237 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 238 return BaseLazyFieldGetter(property, info, &ModuleSystem::GetNativeModule); |
| 239 } |
| 240 |
202 // static | 241 // static |
203 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( | 242 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( |
204 v8::Local<v8::String> property, const v8::AccessorInfo& info) { | 243 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 244 return BaseLazyFieldGetter(property, info, &ModuleSystem::GetModule); |
| 245 } |
| 246 |
| 247 // static |
| 248 v8::Handle<v8::Value> ModuleSystem::BaseLazyFieldGetter( |
| 249 v8::Local<v8::String> property, |
| 250 const v8::AccessorInfo& info, |
| 251 GetModuleFunc get_module) { |
205 CHECK(!info.Data().IsEmpty()); | 252 CHECK(!info.Data().IsEmpty()); |
206 CHECK(info.Data()->IsObject()); | 253 CHECK(info.Data()->IsObject()); |
207 v8::HandleScope handle_scope; | 254 v8::HandleScope handle_scope; |
208 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 255 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
209 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 256 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
210 v8::Handle<v8::Value> module_system_value = | 257 v8::Handle<v8::Value> module_system_value = |
211 global->GetHiddenValue(v8::String::New(kModuleSystem)); | 258 global->GetHiddenValue(v8::String::New(kModuleSystem)); |
212 if (module_system_value->IsUndefined()) { | 259 if (module_system_value->IsUndefined()) { |
213 // ModuleSystem has been deleted. | 260 // ModuleSystem has been deleted. |
214 return v8::Undefined(); | 261 return v8::Undefined(); |
215 } | 262 } |
216 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 263 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
217 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | 264 v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
218 | 265 |
219 v8::Handle<v8::Object> module; | 266 v8::Handle<v8::Object> module = (module_system->*get_module)(parameters); |
220 { | |
221 NativesEnabledScope scope(module_system); | |
222 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( | |
223 parameters->Get(v8::String::New(kModuleName))->ToString())); | |
224 } | |
225 if (module.IsEmpty()) | 267 if (module.IsEmpty()) |
226 return handle_scope.Close(v8::Handle<v8::Value>()); | 268 return handle_scope.Close(v8::Handle<v8::Value>()); |
227 | 269 |
228 v8::Handle<v8::String> field = | 270 v8::Handle<v8::String> field = |
229 parameters->Get(v8::String::New(kModuleField))->ToString(); | 271 parameters->Get(v8::String::New(kModuleField))->ToString(); |
230 | 272 |
231 return handle_scope.Close(module->Get(field)); | 273 return handle_scope.Close(module->Get(field)); |
232 } | 274 } |
233 | 275 |
234 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 276 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
235 const std::string& field, | 277 const std::string& field, |
236 const std::string& module_name, | 278 const std::string& module_name, |
237 const std::string& module_field) { | 279 const std::string& module_field) { |
| 280 SetLazyField(object, field, module_name, module_field, |
| 281 &ModuleSystem::LazyFieldGetter); |
| 282 } |
| 283 |
| 284 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 285 const std::string& field, |
| 286 const std::string& module_name, |
| 287 const std::string& module_field, |
| 288 v8::AccessorGetter getter) { |
238 v8::HandleScope handle_scope; | 289 v8::HandleScope handle_scope; |
239 v8::Handle<v8::Object> parameters = v8::Object::New(); | 290 v8::Handle<v8::Object> parameters = v8::Object::New(); |
240 parameters->Set(v8::String::New(kModuleName), | 291 parameters->Set(v8::String::New(kModuleName), |
241 v8::String::New(module_name.c_str())); | 292 v8::String::New(module_name.c_str())); |
242 parameters->Set(v8::String::New(kModuleField), | 293 parameters->Set(v8::String::New(kModuleField), |
243 v8::String::New(module_field.c_str())); | 294 v8::String::New(module_field.c_str())); |
244 | 295 |
245 object->SetAccessor(v8::String::New(field.c_str()), | 296 object->SetAccessor(v8::String::New(field.c_str()), |
246 &ModuleSystem::LazyFieldGetter, | 297 getter, |
247 NULL, | 298 NULL, |
248 parameters); | 299 parameters); |
249 } | 300 } |
250 | 301 |
| 302 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, |
| 303 const std::string& field, |
| 304 const std::string& module_name, |
| 305 const std::string& module_field) { |
| 306 SetLazyField(object, field, module_name, module_field, |
| 307 &ModuleSystem::NativeLazyFieldGetter); |
| 308 } |
| 309 |
251 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 310 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
252 v8::Handle<v8::String> name) { | 311 v8::Handle<v8::String> name) { |
253 v8::HandleScope handle_scope; | 312 v8::HandleScope handle_scope; |
254 WebKit::WebScopedMicrotaskSuppression suppression; | 313 WebKit::WebScopedMicrotaskSuppression suppression; |
255 v8::Handle<v8::Value> result; | 314 v8::Handle<v8::Value> result; |
256 v8::TryCatch try_catch; | 315 v8::TryCatch try_catch; |
257 try_catch.SetCaptureMessage(true); | 316 try_catch.SetCaptureMessage(true); |
258 v8::Handle<v8::Script> script(v8::Script::New(code, name)); | 317 v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
259 if (try_catch.HasCaught()) { | 318 if (try_catch.HasCaught()) { |
260 HandleException(try_catch); | 319 HandleException(try_catch); |
(...skipping 11 matching lines...) Expand all Loading... |
272 v8::Handle<v8::String> source_name) { | 331 v8::Handle<v8::String> source_name) { |
273 v8::HandleScope handle_scope; | 332 v8::HandleScope handle_scope; |
274 std::string module_name = *v8::String::AsciiValue(source_name); | 333 std::string module_name = *v8::String::AsciiValue(source_name); |
275 if (!source_map_->Contains(module_name)) | 334 if (!source_map_->Contains(module_name)) |
276 return v8::Undefined(); | 335 return v8::Undefined(); |
277 return handle_scope.Close(source_map_->GetSource(module_name)); | 336 return handle_scope.Close(source_map_->GetSource(module_name)); |
278 } | 337 } |
279 | 338 |
280 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { | 339 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { |
281 CHECK_EQ(1, args.Length()); | 340 CHECK_EQ(1, args.Length()); |
| 341 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 342 return GetNativeFromString(native_name); |
| 343 } |
| 344 |
| 345 v8::Handle<v8::Value> ModuleSystem::GetNativeFromString( |
| 346 const std::string& native_name) { |
282 if (natives_enabled_ == 0) | 347 if (natives_enabled_ == 0) |
283 return ThrowException("Natives disabled"); | 348 return ThrowException("Natives disabled"); |
284 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); | |
285 if (overridden_native_handlers_.count(native_name) > 0u) | 349 if (overridden_native_handlers_.count(native_name) > 0u) |
286 return RequireForJs(args); | 350 return RequireForJsInner(v8::String::New(native_name.c_str())); |
287 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 351 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
288 if (i == native_handler_map_.end()) | 352 if (i == native_handler_map_.end()) |
289 return v8::Undefined(); | 353 return v8::Undefined(); |
290 return i->second->NewInstance(); | 354 return i->second->NewInstance(); |
291 } | 355 } |
292 | 356 |
293 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 357 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
294 v8::HandleScope handle_scope; | 358 v8::HandleScope handle_scope; |
295 v8::Handle<v8::String> left = | 359 v8::Handle<v8::String> left = |
296 v8::String::New("(function(require, requireNative, exports) {"); | 360 v8::String::New("(function(require, requireNative, exports) {"); |
297 v8::Handle<v8::String> right = v8::String::New("\n})"); | 361 v8::Handle<v8::String> right = v8::String::New("\n})"); |
298 return handle_scope.Close( | 362 return handle_scope.Close( |
299 v8::String::Concat(left, v8::String::Concat(source, right))); | 363 v8::String::Concat(left, v8::String::Concat(source, right))); |
300 } | 364 } |
301 | 365 |
302 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 366 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
303 return v8::ThrowException(v8::String::New(message.c_str())); | 367 return v8::ThrowException(v8::String::New(message.c_str())); |
304 } | 368 } |
305 | 369 |
306 } // extensions | 370 } // extensions |
OLD | NEW |