Chromium Code Reviews| 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 "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 full_message += " context"; | 48 full_message += " context"; |
| 49 if (context->extension()) { | 49 if (context->extension()) { |
| 50 full_message += " for "; | 50 full_message += " for "; |
| 51 full_message += context->extension()->id(); | 51 full_message += context->extension()->id(); |
| 52 } | 52 } |
| 53 full_message += ") "; | 53 full_message += ") "; |
| 54 full_message += message; | 54 full_message += message; |
| 55 | 55 |
| 56 // <= dev means dev, canary, and trunk. | 56 // <= dev means dev, canary, and trunk. |
| 57 if (GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV) | 57 if (GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV) |
| 58 console::Fatal(v8::Context::GetCalling(), full_message); | 58 console::Fatal(context->isolate()->GetCallingContext(), full_message); |
| 59 else | 59 else |
| 60 console::Error(v8::Context::GetCalling(), full_message); | 60 console::Error(context->isolate()->GetCallingContext(), full_message); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void Warn(const std::string& message) { | 63 void Warn(v8::Isolate* isolate, const std::string& message) { |
| 64 console::Warn(v8::Context::GetCalling(), message); | 64 console::Warn(isolate->GetCallingContext(), message); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Default exception handler which logs the exception. | 67 // Default exception handler which logs the exception. |
| 68 class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { | 68 class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
| 69 public: | 69 public: |
| 70 explicit DefaultExceptionHandler(ChromeV8Context* context) | 70 explicit DefaultExceptionHandler(ChromeV8Context* context) |
| 71 : context_(context) {} | 71 : context_(context) {} |
| 72 | 72 |
| 73 // Fatally dumps the debug info from |try_catch| to the console. | 73 // Fatally dumps the debug info from |try_catch| to the console. |
| 74 // Make sure this is never used for exceptions that originate in external | 74 // Make sure this is never used for exceptions that originate in external |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { | 175 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 176 module_system_->natives_enabled_--; | 176 module_system_->natives_enabled_--; |
| 177 CHECK_GE(module_system_->natives_enabled_, 0); | 177 CHECK_GE(module_system_->natives_enabled_, 0); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { | 180 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
| 181 exception_handler_->HandleUncaughtException(try_catch); | 181 exception_handler_->HandleUncaughtException(try_catch); |
| 182 } | 182 } |
| 183 | 183 |
| 184 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { | 184 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
| 185 v8::HandleScope handle_scope(GetIsolate()); | 185 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 186 return handle_scope.Close(RequireForJsInner( | 186 return handle_scope.Escape(RequireForJsInner( |
| 187 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); | 187 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); |
| 188 } | 188 } |
| 189 | 189 |
| 190 void ModuleSystem::RequireForJs( | 190 void ModuleSystem::RequireForJs( |
| 191 const v8::FunctionCallbackInfo<v8::Value>& args) { | 191 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 192 v8::Handle<v8::String> module_name = args[0]->ToString(); | 192 v8::Handle<v8::String> module_name = args[0]->ToString(); |
| 193 args.GetReturnValue().Set(RequireForJsInner(module_name)); | 193 args.GetReturnValue().Set(RequireForJsInner(module_name)); |
| 194 } | 194 } |
| 195 | 195 |
| 196 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( | 196 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
| 197 v8::Handle<v8::String> module_name) { | 197 v8::Handle<v8::String> module_name) { |
| 198 v8::HandleScope handle_scope(GetIsolate()); | 198 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 199 v8::Context::Scope context_scope(context()->v8_context()); | 199 v8::Context::Scope context_scope(context()->v8_context()); |
| 200 | 200 |
| 201 v8::Handle<v8::Object> global(context()->v8_context()->Global()); | 201 v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
| 202 | 202 |
| 203 // The module system might have been deleted. This can happen if a different | 203 // The module system might have been deleted. This can happen if a different |
| 204 // context keeps a reference to us, but our frame is destroyed (e.g. | 204 // context keeps a reference to us, but our frame is destroyed (e.g. |
| 205 // background page keeps reference to chrome object in a closed popup). | 205 // background page keeps reference to chrome object in a closed popup). |
| 206 v8::Handle<v8::Value> modules_value = global->GetHiddenValue( | 206 v8::Handle<v8::Value> modules_value = global->GetHiddenValue( |
| 207 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); | 207 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); |
| 208 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { | 208 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
| 209 Warn("Extension view no longer exists"); | 209 Warn(GetIsolate(), "Extension view no longer exists"); |
| 210 return v8::Undefined(); | 210 return v8::Undefined(GetIsolate()); |
| 211 } | 211 } |
| 212 | 212 |
| 213 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); | 213 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
| 214 v8::Handle<v8::Value> exports(modules->Get(module_name)); | 214 v8::Local<v8::Value> exports(modules->Get(module_name)); |
|
marja
2013/12/02 13:36:21
You're changing some Handles to Locals, but leavin
jochen (gone - plz use gerrit)
2013/12/02 13:45:12
Yes.
The once I pass to EscapableHandleScope::Esc
| |
| 215 if (!exports->IsUndefined()) | 215 if (!exports->IsUndefined()) |
| 216 return handle_scope.Close(exports); | 216 return handle_scope.Escape(exports); |
| 217 | 217 |
| 218 std::string module_name_str = *v8::String::Utf8Value(module_name); | 218 std::string module_name_str = *v8::String::Utf8Value(module_name); |
| 219 v8::Handle<v8::Value> source(GetSource(module_name_str)); | 219 v8::Handle<v8::Value> source(GetSource(module_name_str)); |
| 220 if (source.IsEmpty() || source->IsUndefined()) { | 220 if (source.IsEmpty() || source->IsUndefined()) { |
| 221 Fatal(context_, "No source for require(" + module_name_str + ")"); | 221 Fatal(context_, "No source for require(" + module_name_str + ")"); |
| 222 return v8::Undefined(); | 222 return v8::Undefined(GetIsolate()); |
| 223 } | 223 } |
| 224 v8::Handle<v8::String> wrapped_source(WrapSource( | 224 v8::Handle<v8::String> wrapped_source(WrapSource( |
| 225 v8::Handle<v8::String>::Cast(source))); | 225 v8::Handle<v8::String>::Cast(source))); |
| 226 // Modules are wrapped in (function(){...}) so they always return functions. | 226 // Modules are wrapped in (function(){...}) so they always return functions. |
| 227 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); | 227 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); |
| 228 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 228 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
| 229 Fatal(context_, "Bad source for require(" + module_name_str + ")"); | 229 Fatal(context_, "Bad source for require(" + module_name_str + ")"); |
| 230 return v8::Undefined(); | 230 return v8::Undefined(GetIsolate()); |
| 231 } | 231 } |
| 232 | 232 |
| 233 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); | 233 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
| 234 | 234 |
| 235 exports = v8::Object::New(); | 235 exports = v8::Object::New(); |
| 236 v8::Handle<v8::Object> natives(NewInstance()); | 236 v8::Handle<v8::Object> natives(NewInstance()); |
| 237 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues | 237 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues |
| 238 | 238 |
| 239 // These must match the argument order in WrapSource. | 239 // These must match the argument order in WrapSource. |
| 240 v8::Handle<v8::Value> args[] = { | 240 v8::Handle<v8::Value> args[] = { |
| 241 // CommonJS. | 241 // CommonJS. |
| 242 natives->Get(v8::String::NewSymbol("require")), | 242 natives->Get(v8::String::NewFromUtf8( |
| 243 natives->Get(v8::String::NewSymbol("requireNative")), | 243 GetIsolate(), "require", v8::String::kInternalizedString)), |
| 244 exports, | 244 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireNative", |
| 245 // Libraries that we magically expose to every module. | 245 v8::String::kInternalizedString)), |
| 246 console::AsV8Object(), | 246 exports, |
| 247 // Each safe builtin. Keep in order with the arguments in WrapSource. | 247 // Libraries that we magically expose to every module. |
| 248 context_->safe_builtins()->GetArray(), | 248 console::AsV8Object(), |
| 249 context_->safe_builtins()->GetFunction(), | 249 // Each safe builtin. Keep in order with the arguments in WrapSource. |
| 250 context_->safe_builtins()->GetJSON(), | 250 context_->safe_builtins()->GetArray(), |
| 251 context_->safe_builtins()->GetObjekt(), | 251 context_->safe_builtins()->GetFunction(), |
| 252 context_->safe_builtins()->GetRegExp(), | 252 context_->safe_builtins()->GetJSON(), |
| 253 context_->safe_builtins()->GetString(), | 253 context_->safe_builtins()->GetObjekt(), |
| 254 }; | 254 context_->safe_builtins()->GetRegExp(), |
| 255 context_->safe_builtins()->GetString(), }; | |
| 255 { | 256 { |
| 256 v8::TryCatch try_catch; | 257 v8::TryCatch try_catch; |
| 257 try_catch.SetCaptureMessage(true); | 258 try_catch.SetCaptureMessage(true); |
| 258 context_->CallFunction(func, arraysize(args), args); | 259 context_->CallFunction(func, arraysize(args), args); |
| 259 if (try_catch.HasCaught()) { | 260 if (try_catch.HasCaught()) { |
| 260 HandleException(try_catch); | 261 HandleException(try_catch); |
| 261 return v8::Undefined(); | 262 return v8::Undefined(GetIsolate()); |
| 262 } | 263 } |
| 263 } | 264 } |
| 264 modules->Set(module_name, exports); | 265 modules->Set(module_name, exports); |
| 265 return handle_scope.Close(exports); | 266 return handle_scope.Escape(exports); |
| 266 } | 267 } |
| 267 | 268 |
| 268 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 269 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 269 const std::string& module_name, | 270 const std::string& module_name, |
| 270 const std::string& method_name) { | 271 const std::string& method_name) { |
| 271 v8::HandleScope handle_scope(GetIsolate()); | 272 v8::HandleScope handle_scope(GetIsolate()); |
| 272 v8::Handle<v8::Value> no_args; | 273 v8::Handle<v8::Value> no_args; |
| 273 return CallModuleMethod(module_name, method_name, 0, &no_args); | 274 return CallModuleMethod(module_name, method_name, 0, &no_args); |
| 274 } | 275 } |
| 275 | 276 |
| 276 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 277 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 277 const std::string& module_name, | 278 const std::string& module_name, |
| 278 const std::string& method_name, | 279 const std::string& method_name, |
| 279 std::vector<v8::Handle<v8::Value> >* args) { | 280 std::vector<v8::Handle<v8::Value> >* args) { |
| 280 return CallModuleMethod( | 281 return CallModuleMethod( |
| 281 module_name, method_name, args->size(), vector_as_array(args)); | 282 module_name, method_name, args->size(), vector_as_array(args)); |
| 282 } | 283 } |
| 283 | 284 |
| 284 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 285 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 285 const std::string& module_name, | 286 const std::string& module_name, |
| 286 const std::string& method_name, | 287 const std::string& method_name, |
| 287 int argc, | 288 int argc, |
| 288 v8::Handle<v8::Value> argv[]) { | 289 v8::Handle<v8::Value> argv[]) { |
| 289 TRACE_EVENT2("v8", "v8.callModuleMethod", | 290 TRACE_EVENT2("v8", "v8.callModuleMethod", |
| 290 "module_name", module_name, | 291 "module_name", module_name, |
| 291 "method_name", method_name); | 292 "method_name", method_name); |
| 292 | 293 |
| 293 v8::HandleScope handle_scope(GetIsolate()); | 294 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 294 v8::Context::Scope context_scope(context()->v8_context()); | 295 v8::Context::Scope context_scope(context()->v8_context()); |
| 295 | 296 |
| 296 v8::Local<v8::Value> module; | 297 v8::Local<v8::Value> module; |
| 297 { | 298 { |
| 298 NativesEnabledScope natives_enabled(this); | 299 NativesEnabledScope natives_enabled(this); |
| 299 module = RequireForJsInner( | 300 module = RequireForJsInner( |
| 300 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | 301 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
| 301 } | 302 } |
| 302 | 303 |
| 303 if (module.IsEmpty() || !module->IsObject()) { | 304 if (module.IsEmpty() || !module->IsObject()) { |
| 304 Fatal(context_, | 305 Fatal(context_, |
| 305 "Failed to get module " + module_name + " to call " + method_name); | 306 "Failed to get module " + module_name + " to call " + method_name); |
| 306 return handle_scope.Close(v8::Undefined()); | 307 return handle_scope.Escape( |
| 308 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | |
| 307 } | 309 } |
| 308 | 310 |
| 309 v8::Local<v8::Value> value = | 311 v8::Local<v8::Value> value = |
| 310 v8::Handle<v8::Object>::Cast(module)->Get( | 312 v8::Handle<v8::Object>::Cast(module)->Get( |
| 311 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); | 313 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); |
| 312 if (value.IsEmpty() || !value->IsFunction()) { | 314 if (value.IsEmpty() || !value->IsFunction()) { |
| 313 Fatal(context_, module_name + "." + method_name + " is not a function"); | 315 Fatal(context_, module_name + "." + method_name + " is not a function"); |
| 314 return handle_scope.Close(v8::Undefined()); | 316 return handle_scope.Escape( |
| 317 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | |
| 315 } | 318 } |
| 316 | 319 |
| 317 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); | 320 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); |
| 318 v8::Local<v8::Value> result; | 321 v8::Local<v8::Value> result; |
| 319 { | 322 { |
| 320 v8::TryCatch try_catch; | 323 v8::TryCatch try_catch; |
| 321 try_catch.SetCaptureMessage(true); | 324 try_catch.SetCaptureMessage(true); |
| 322 result = context_->CallFunction(func, argc, argv); | 325 result = context_->CallFunction(func, argc, argv); |
| 323 if (try_catch.HasCaught()) | 326 if (try_catch.HasCaught()) |
| 324 HandleException(try_catch); | 327 HandleException(try_catch); |
| 325 } | 328 } |
| 326 return handle_scope.Close(result); | 329 return handle_scope.Escape(result); |
| 327 } | 330 } |
| 328 | 331 |
| 329 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 332 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
| 330 scoped_ptr<NativeHandler> native_handler) { | 333 scoped_ptr<NativeHandler> native_handler) { |
| 331 native_handler_map_[name] = | 334 native_handler_map_[name] = |
| 332 linked_ptr<NativeHandler>(native_handler.release()); | 335 linked_ptr<NativeHandler>(native_handler.release()); |
| 333 } | 336 } |
| 334 | 337 |
| 335 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { | 338 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
| 336 overridden_native_handlers_.insert(name); | 339 overridden_native_handlers_.insert(name); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 368 v8::HandleScope handle_scope(info.GetIsolate()); | 371 v8::HandleScope handle_scope(info.GetIsolate()); |
| 369 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 372 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
| 370 // This context should be the same as context()->v8_context(). | 373 // This context should be the same as context()->v8_context(). |
| 371 v8::Handle<v8::Context> context = parameters->CreationContext(); | 374 v8::Handle<v8::Context> context = parameters->CreationContext(); |
| 372 v8::Handle<v8::Object> global(context->Global()); | 375 v8::Handle<v8::Object> global(context->Global()); |
| 373 v8::Handle<v8::Value> module_system_value = global->GetHiddenValue( | 376 v8::Handle<v8::Value> module_system_value = global->GetHiddenValue( |
| 374 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); | 377 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); |
| 375 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { | 378 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
| 376 // ModuleSystem has been deleted. | 379 // ModuleSystem has been deleted. |
| 377 // TODO(kalman): See comment in header file. | 380 // TODO(kalman): See comment in header file. |
| 378 Warn("Module system has been deleted, does extension view exist?"); | 381 Warn(info.GetIsolate(), |
| 382 "Module system has been deleted, does extension view exist?"); | |
| 379 return; | 383 return; |
| 380 } | 384 } |
| 381 | 385 |
| 382 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 386 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 383 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | 387 v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
| 384 | 388 |
| 385 std::string name = *v8::String::Utf8Value(parameters->Get( | 389 std::string name = *v8::String::Utf8Value(parameters->Get( |
| 386 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))->ToString()); | 390 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))->ToString()); |
| 387 | 391 |
| 388 // Switch to our v8 context because we need functions created while running | 392 // Switch to our v8 context because we need functions created while running |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 &ModuleSystem::NativeLazyFieldGetter); | 469 &ModuleSystem::NativeLazyFieldGetter); |
| 466 } | 470 } |
| 467 | 471 |
| 468 | 472 |
| 469 v8::Isolate* ModuleSystem::GetIsolate() const { | 473 v8::Isolate* ModuleSystem::GetIsolate() const { |
| 470 return context_->isolate(); | 474 return context_->isolate(); |
| 471 } | 475 } |
| 472 | 476 |
| 473 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 477 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 474 v8::Handle<v8::String> name) { | 478 v8::Handle<v8::String> name) { |
| 475 v8::HandleScope handle_scope(GetIsolate()); | 479 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 476 v8::Context::Scope context_scope(context()->v8_context()); | 480 v8::Context::Scope context_scope(context()->v8_context()); |
| 477 | 481 |
| 478 // Prepend extensions:: to |name| so that internal code can be differentiated | 482 // Prepend extensions:: to |name| so that internal code can be differentiated |
| 479 // from external code in stack traces. This has no effect on behaviour. | 483 // from external code in stack traces. This has no effect on behaviour. |
| 480 std::string internal_name = base::StringPrintf("extensions::%s", | 484 std::string internal_name = base::StringPrintf("extensions::%s", |
| 481 *v8::String::Utf8Value(name)); | 485 *v8::String::Utf8Value(name)); |
| 482 | 486 |
| 483 blink::WebScopedMicrotaskSuppression suppression; | 487 blink::WebScopedMicrotaskSuppression suppression; |
| 484 v8::TryCatch try_catch; | 488 v8::TryCatch try_catch; |
| 485 try_catch.SetCaptureMessage(true); | 489 try_catch.SetCaptureMessage(true); |
| 486 v8::Handle<v8::Script> script( | 490 v8::Handle<v8::Script> script( |
| 487 v8::Script::New(code, | 491 v8::Script::New(code, |
| 488 v8::String::NewFromUtf8(GetIsolate(), | 492 v8::String::NewFromUtf8(GetIsolate(), |
| 489 internal_name.c_str(), | 493 internal_name.c_str(), |
| 490 v8::String::kNormalString, | 494 v8::String::kNormalString, |
| 491 internal_name.size()))); | 495 internal_name.size()))); |
| 492 if (try_catch.HasCaught()) { | 496 if (try_catch.HasCaught()) { |
| 493 HandleException(try_catch); | 497 HandleException(try_catch); |
| 494 return v8::Undefined(); | 498 return v8::Undefined(GetIsolate()); |
| 495 } | 499 } |
| 496 | 500 |
| 497 v8::Handle<v8::Value> result = script->Run(); | 501 v8::Local<v8::Value> result = script->Run(); |
| 498 if (try_catch.HasCaught()) { | 502 if (try_catch.HasCaught()) { |
| 499 HandleException(try_catch); | 503 HandleException(try_catch); |
| 500 return v8::Undefined(); | 504 return v8::Undefined(GetIsolate()); |
| 501 } | 505 } |
| 502 | 506 |
| 503 return handle_scope.Close(result); | 507 return handle_scope.Escape(result); |
| 504 } | 508 } |
| 505 | 509 |
| 506 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { | 510 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
| 507 v8::HandleScope handle_scope(GetIsolate()); | 511 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 508 if (!source_map_->Contains(module_name)) | 512 if (!source_map_->Contains(module_name)) |
| 509 return v8::Undefined(); | 513 return v8::Undefined(GetIsolate()); |
| 510 return handle_scope.Close(source_map_->GetSource(GetIsolate(), module_name)); | 514 return handle_scope.Escape( |
| 515 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); | |
| 511 } | 516 } |
| 512 | 517 |
| 513 void ModuleSystem::RequireNative( | 518 void ModuleSystem::RequireNative( |
| 514 const v8::FunctionCallbackInfo<v8::Value>& args) { | 519 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 515 CHECK_EQ(1, args.Length()); | 520 CHECK_EQ(1, args.Length()); |
| 516 std::string native_name = *v8::String::Utf8Value(args[0]->ToString()); | 521 std::string native_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 517 args.GetReturnValue().Set(RequireNativeFromString(native_name)); | 522 args.GetReturnValue().Set(RequireNativeFromString(native_name)); |
| 518 } | 523 } |
| 519 | 524 |
| 520 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( | 525 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
| 521 const std::string& native_name) { | 526 const std::string& native_name) { |
| 522 if (natives_enabled_ == 0) { | 527 if (natives_enabled_ == 0) { |
| 523 // HACK: if in test throw exception so that we can test the natives-disabled | 528 // HACK: if in test throw exception so that we can test the natives-disabled |
| 524 // logic; however, under normal circumstances, this is programmer error so | 529 // logic; however, under normal circumstances, this is programmer error so |
| 525 // we could crash. | 530 // we could crash. |
| 526 if (exception_handler_) { | 531 if (exception_handler_) { |
| 527 return v8::ThrowException( | 532 return GetIsolate()->ThrowException( |
| 528 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); | 533 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); |
| 529 } | 534 } |
| 530 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); | 535 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); |
| 531 return v8::Undefined(); | 536 return v8::Undefined(GetIsolate()); |
| 532 } | 537 } |
| 533 | 538 |
| 534 if (overridden_native_handlers_.count(native_name) > 0u) { | 539 if (overridden_native_handlers_.count(native_name) > 0u) { |
| 535 return RequireForJsInner( | 540 return RequireForJsInner( |
| 536 v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); | 541 v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); |
| 537 } | 542 } |
| 538 | 543 |
| 539 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 544 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
| 540 if (i == native_handler_map_.end()) { | 545 if (i == native_handler_map_.end()) { |
| 541 Fatal(context_, | 546 Fatal(context_, |
| 542 "Couldn't find native for requireNative(" + native_name + ")"); | 547 "Couldn't find native for requireNative(" + native_name + ")"); |
| 543 return v8::Undefined(); | 548 return v8::Undefined(GetIsolate()); |
| 544 } | 549 } |
| 545 return i->second->NewInstance(); | 550 return i->second->NewInstance(); |
| 546 } | 551 } |
| 547 | 552 |
| 548 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 553 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 549 v8::HandleScope handle_scope(GetIsolate()); | 554 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 550 // Keep in order with the arguments in RequireForJsInner. | 555 // Keep in order with the arguments in RequireForJsInner. |
| 551 v8::Handle<v8::String> left = v8::String::NewFromUtf8(GetIsolate(), | 556 v8::Handle<v8::String> left = v8::String::NewFromUtf8(GetIsolate(), |
| 552 "(function(require, requireNative, exports, " | 557 "(function(require, requireNative, exports, " |
| 553 "console, " | 558 "console, " |
| 554 "$Array, $Function, $JSON, $Object, $RegExp, $String) {" | 559 "$Array, $Function, $JSON, $Object, $RegExp, $String) {" |
| 555 "'use strict';"); | 560 "'use strict';"); |
| 556 v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); | 561 v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); |
| 557 return handle_scope.Close( | 562 return handle_scope.Escape(v8::Local<v8::String>( |
| 558 v8::String::Concat(left, v8::String::Concat(source, right))); | 563 v8::String::Concat(left, v8::String::Concat(source, right)))); |
| 559 } | 564 } |
| 560 | 565 |
| 561 } // namespace extensions | 566 } // namespace extensions |
| OLD | NEW |