| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/module_system.h" | 5 #include "extensions/renderer/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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/trace_event/trace_event.h" | 13 #include "base/trace_event/trace_event.h" |
| 14 #include "content/public/renderer/render_frame.h" | 14 #include "content/public/renderer/render_frame.h" |
| 15 #include "content/public/renderer/render_view.h" | 15 #include "content/public/renderer/render_view.h" |
| 16 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 17 #include "extensions/common/extensions_client.h" | 17 #include "extensions/common/extensions_client.h" |
| 18 #include "extensions/renderer/console.h" | 18 #include "extensions/renderer/console.h" |
| 19 #include "extensions/renderer/safe_builtins.h" | 19 #include "extensions/renderer/safe_builtins.h" |
| 20 #include "extensions/renderer/script_context.h" | 20 #include "extensions/renderer/script_context.h" |
| 21 #include "extensions/renderer/v8_maybe_helpers.h" |
| 21 #include "gin/modules/module_registry.h" | 22 #include "gin/modules/module_registry.h" |
| 22 #include "third_party/WebKit/public/web/WebFrame.h" | 23 #include "third_party/WebKit/public/web/WebFrame.h" |
| 23 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 24 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 24 | 25 |
| 25 namespace extensions { | 26 namespace extensions { |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const char* kModuleSystem = "module_system"; | 30 const char* kModuleSystem = "module_system"; |
| 30 const char* kModuleName = "module_name"; | 31 const char* kModuleName = "module_name"; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 63 } |
| 63 | 64 |
| 64 void Warn(v8::Isolate* isolate, const std::string& message) { | 65 void Warn(v8::Isolate* isolate, const std::string& message) { |
| 65 console::Warn(isolate->GetCallingContext(), message); | 66 console::Warn(isolate->GetCallingContext(), message); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Default exception handler which logs the exception. | 69 // Default exception handler which logs the exception. |
| 69 class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { | 70 class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
| 70 public: | 71 public: |
| 71 explicit DefaultExceptionHandler(ScriptContext* context) | 72 explicit DefaultExceptionHandler(ScriptContext* context) |
| 72 : context_(context) {} | 73 : ModuleSystem::ExceptionHandler(context) {} |
| 73 | 74 |
| 74 // Fatally dumps the debug info from |try_catch| to the console. | 75 // Fatally dumps the debug info from |try_catch| to the console. |
| 75 // Make sure this is never used for exceptions that originate in external | 76 // Make sure this is never used for exceptions that originate in external |
| 76 // code! | 77 // code! |
| 77 void HandleUncaughtException(const v8::TryCatch& try_catch) override { | 78 void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
| 78 v8::HandleScope handle_scope(context_->isolate()); | 79 v8::HandleScope handle_scope(context_->isolate()); |
| 79 std::string stack_trace = "<stack trace unavailable>"; | 80 std::string stack_trace = "<stack trace unavailable>"; |
| 80 if (!try_catch.StackTrace().IsEmpty()) { | 81 v8::Local<v8::Value> v8_stack_trace; |
| 81 v8::String::Utf8Value stack_value(try_catch.StackTrace()); | 82 if (try_catch.StackTrace(context_->v8_context()).ToLocal(&v8_stack_trace)) { |
| 83 v8::String::Utf8Value stack_value(v8_stack_trace); |
| 82 if (*stack_value) | 84 if (*stack_value) |
| 83 stack_trace.assign(*stack_value, stack_value.length()); | 85 stack_trace.assign(*stack_value, stack_value.length()); |
| 84 else | 86 else |
| 85 stack_trace = "<could not convert stack trace to string>"; | 87 stack_trace = "<could not convert stack trace to string>"; |
| 86 } | 88 } |
| 87 Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); | 89 Fatal(context_, |
| 90 CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
| 88 } | 91 } |
| 89 | 92 |
| 90 private: | 93 private: |
| 91 ScriptContext* context_; | 94 ScriptContext* context_; |
| 92 }; | 95 }; |
| 93 | 96 |
| 94 } // namespace | 97 } // namespace |
| 95 | 98 |
| 96 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( | 99 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
| 97 const v8::TryCatch& try_catch) { | 100 const v8::TryCatch& try_catch) { |
| 98 v8::Local<v8::Message> message(try_catch.Message()); | 101 v8::Local<v8::Message> message(try_catch.Message()); |
| 99 if (message.IsEmpty()) { | 102 if (message.IsEmpty()) { |
| 100 return "try_catch has no message"; | 103 return "try_catch has no message"; |
| 101 } | 104 } |
| 102 | 105 |
| 103 std::string resource_name = "<unknown resource>"; | 106 std::string resource_name = "<unknown resource>"; |
| 104 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { | 107 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { |
| 105 v8::String::Utf8Value resource_name_v8( | 108 v8::String::Utf8Value resource_name_v8( |
| 106 message->GetScriptOrigin().ResourceName()); | 109 message->GetScriptOrigin().ResourceName()); |
| 107 resource_name.assign(*resource_name_v8, resource_name_v8.length()); | 110 resource_name.assign(*resource_name_v8, resource_name_v8.length()); |
| 108 } | 111 } |
| 109 | 112 |
| 110 std::string error_message = "<no error message>"; | 113 std::string error_message = "<no error message>"; |
| 111 if (!message->Get().IsEmpty()) { | 114 if (!message->Get().IsEmpty()) { |
| 112 v8::String::Utf8Value error_message_v8(message->Get()); | 115 v8::String::Utf8Value error_message_v8(message->Get()); |
| 113 error_message.assign(*error_message_v8, error_message_v8.length()); | 116 error_message.assign(*error_message_v8, error_message_v8.length()); |
| 114 } | 117 } |
| 115 | 118 |
| 119 auto maybe = message->GetLineNumber(context_->v8_context()); |
| 120 int line_number = maybe.IsJust() ? maybe.FromJust() : 0; |
| 116 return base::StringPrintf("%s:%d: %s", | 121 return base::StringPrintf("%s:%d: %s", |
| 117 resource_name.c_str(), | 122 resource_name.c_str(), |
| 118 message->GetLineNumber(), | 123 line_number, |
| 119 error_message.c_str()); | 124 error_message.c_str()); |
| 120 } | 125 } |
| 121 | 126 |
| 122 ModuleSystem::ModuleSystem(ScriptContext* context, SourceMap* source_map) | 127 ModuleSystem::ModuleSystem(ScriptContext* context, SourceMap* source_map) |
| 123 : ObjectBackedNativeHandler(context), | 128 : ObjectBackedNativeHandler(context), |
| 124 context_(context), | 129 context_(context), |
| 125 source_map_(source_map), | 130 source_map_(source_map), |
| 126 natives_enabled_(0), | 131 natives_enabled_(0), |
| 127 exception_handler_(new DefaultExceptionHandler(context)), | 132 exception_handler_(new DefaultExceptionHandler(context)), |
| 128 weak_factory_(this) { | 133 weak_factory_(this) { |
| 129 RouteFunction( | 134 RouteFunction( |
| 130 "require", | 135 "require", |
| 131 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); | 136 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
| 132 RouteFunction( | 137 RouteFunction( |
| 133 "requireNative", | 138 "requireNative", |
| 134 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); | 139 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); |
| 135 RouteFunction( | 140 RouteFunction( |
| 136 "requireAsync", | 141 "requireAsync", |
| 137 base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this))); | 142 base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this))); |
| 138 RouteFunction("privates", | 143 RouteFunction("privates", |
| 139 base::Bind(&ModuleSystem::Private, base::Unretained(this))); | 144 base::Bind(&ModuleSystem::Private, base::Unretained(this))); |
| 140 | 145 |
| 141 v8::Local<v8::Object> global(context->v8_context()->Global()); | 146 v8::Local<v8::Object> global(context->v8_context()->Global()); |
| 142 v8::Isolate* isolate = context->isolate(); | 147 v8::Isolate* isolate = context->isolate(); |
| 143 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField), | 148 global->SetHiddenValue(ToV8String(isolate, kModulesField), |
| 144 v8::Object::New(isolate)); | 149 v8::Object::New(isolate)); |
| 145 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem), | 150 global->SetHiddenValue(ToV8String(isolate, kModuleSystem), |
| 146 v8::External::New(isolate, this)); | 151 v8::External::New(isolate, this)); |
| 147 | 152 |
| 148 gin::ModuleRegistry::From(context->v8_context())->AddObserver(this); | 153 gin::ModuleRegistry::From(context->v8_context())->AddObserver(this); |
| 149 if (context_->GetRenderFrame()) { | 154 if (context_->GetRenderFrame()) { |
| 150 context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable( | 155 context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable( |
| 151 context->isolate(), context->v8_context()); | 156 context->isolate(), context->v8_context()); |
| 152 } | 157 } |
| 153 } | 158 } |
| 154 | 159 |
| 155 ModuleSystem::~ModuleSystem() { | 160 ModuleSystem::~ModuleSystem() { |
| 156 } | 161 } |
| 157 | 162 |
| 158 void ModuleSystem::Invalidate() { | 163 void ModuleSystem::Invalidate() { |
| 159 // Clear the module system properties from the global context. It's polite, | 164 // Clear the module system properties from the global context. It's polite, |
| 160 // and we use this as a signal in lazy handlers that we no longer exist. | 165 // and we use this as a signal in lazy handlers that we no longer exist. |
| 161 { | 166 { |
| 162 v8::HandleScope scope(GetIsolate()); | 167 v8::HandleScope scope(GetIsolate()); |
| 163 v8::Local<v8::Object> global = context()->v8_context()->Global(); | 168 v8::Local<v8::Object> global = context()->v8_context()->Global(); |
| 164 global->DeleteHiddenValue( | 169 global->DeleteHiddenValue(ToV8String(GetIsolate(), kModulesField)); |
| 165 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); | 170 global->DeleteHiddenValue(ToV8String(GetIsolate(), kModuleSystem)); |
| 166 global->DeleteHiddenValue( | |
| 167 v8::String::NewFromUtf8(GetIsolate(), kModuleSystem)); | |
| 168 } | 171 } |
| 169 | 172 |
| 170 // Invalidate all active and clobbered NativeHandlers we own. | 173 // Invalidate all active and clobbered NativeHandlers we own. |
| 171 for (const auto& handler : native_handler_map_) | 174 for (const auto& handler : native_handler_map_) |
| 172 handler.second->Invalidate(); | 175 handler.second->Invalidate(); |
| 173 for (const auto& clobbered_handler : clobbered_native_handlers_) | 176 for (const auto& clobbered_handler : clobbered_native_handlers_) |
| 174 clobbered_handler->Invalidate(); | 177 clobbered_handler->Invalidate(); |
| 175 | 178 |
| 176 ObjectBackedNativeHandler::Invalidate(); | 179 ObjectBackedNativeHandler::Invalidate(); |
| 177 } | 180 } |
| 178 | 181 |
| 179 ModuleSystem::NativesEnabledScope::NativesEnabledScope( | 182 ModuleSystem::NativesEnabledScope::NativesEnabledScope( |
| 180 ModuleSystem* module_system) | 183 ModuleSystem* module_system) |
| 181 : module_system_(module_system) { | 184 : module_system_(module_system) { |
| 182 module_system_->natives_enabled_++; | 185 module_system_->natives_enabled_++; |
| 183 } | 186 } |
| 184 | 187 |
| 185 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { | 188 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 186 module_system_->natives_enabled_--; | 189 module_system_->natives_enabled_--; |
| 187 CHECK_GE(module_system_->natives_enabled_, 0); | 190 CHECK_GE(module_system_->natives_enabled_, 0); |
| 188 } | 191 } |
| 189 | 192 |
| 190 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { | 193 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
| 191 exception_handler_->HandleUncaughtException(try_catch); | 194 exception_handler_->HandleUncaughtException(try_catch); |
| 192 } | 195 } |
| 193 | 196 |
| 194 v8::Local<v8::Value> ModuleSystem::Require(const std::string& module_name) { | 197 v8::Local<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
| 198 if (module_name.size() >= v8::String::kMaxLength) |
| 199 return v8::Undefined(GetIsolate()); |
| 195 v8::EscapableHandleScope handle_scope(GetIsolate()); | 200 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 196 return handle_scope.Escape(RequireForJsInner( | 201 return handle_scope.Escape(RequireForJsInner( |
| 197 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); | 202 ToV8String(GetIsolate(), module_name.c_str()))); |
| 198 } | 203 } |
| 199 | 204 |
| 200 void ModuleSystem::RequireForJs( | 205 void ModuleSystem::RequireForJs( |
| 201 const v8::FunctionCallbackInfo<v8::Value>& args) { | 206 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 202 v8::Local<v8::String> module_name = args[0]->ToString(args.GetIsolate()); | 207 if (!args[0]->IsString()) |
| 208 return; |
| 209 v8::Local<v8::String> module_name = args[0].As<v8::String>(); |
| 203 args.GetReturnValue().Set(RequireForJsInner(module_name)); | 210 args.GetReturnValue().Set(RequireForJsInner(module_name)); |
| 204 } | 211 } |
| 205 | 212 |
| 206 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( | 213 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
| 207 v8::Local<v8::String> module_name) { | 214 v8::Local<v8::String> module_name) { |
| 208 v8::EscapableHandleScope handle_scope(GetIsolate()); | 215 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 209 v8::Context::Scope context_scope(context()->v8_context()); | 216 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 217 v8::Context::Scope context_scope(v8_context); |
| 210 | 218 |
| 211 v8::Local<v8::Object> global(context()->v8_context()->Global()); | 219 v8::Local<v8::Object> global(context()->v8_context()->Global()); |
| 212 | 220 |
| 213 // The module system might have been deleted. This can happen if a different | 221 // The module system might have been deleted. This can happen if a different |
| 214 // context keeps a reference to us, but our frame is destroyed (e.g. | 222 // context keeps a reference to us, but our frame is destroyed (e.g. |
| 215 // background page keeps reference to chrome object in a closed popup). | 223 // background page keeps reference to chrome object in a closed popup). |
| 216 v8::Local<v8::Value> modules_value = global->GetHiddenValue( | 224 v8::Local<v8::Value> modules_value = global->GetHiddenValue( |
| 217 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); | 225 ToV8String(GetIsolate(), kModulesField)); |
| 218 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { | 226 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
| 219 Warn(GetIsolate(), "Extension view no longer exists"); | 227 Warn(GetIsolate(), "Extension view no longer exists"); |
| 220 return v8::Undefined(GetIsolate()); | 228 return v8::Undefined(GetIsolate()); |
| 221 } | 229 } |
| 222 | 230 |
| 223 v8::Local<v8::Object> modules(v8::Local<v8::Object>::Cast(modules_value)); | 231 v8::Local<v8::Object> modules(v8::Local<v8::Object>::Cast(modules_value)); |
| 224 v8::Local<v8::Value> exports(modules->Get(module_name)); | 232 v8::Local<v8::Value> exports; |
| 225 if (!exports->IsUndefined()) | 233 if (!modules->Get(v8_context, module_name).ToLocal(&exports) || |
| 234 !exports->IsUndefined()) |
| 226 return handle_scope.Escape(exports); | 235 return handle_scope.Escape(exports); |
| 227 | 236 |
| 228 exports = LoadModule(*v8::String::Utf8Value(module_name)); | 237 exports = LoadModule(*v8::String::Utf8Value(module_name)); |
| 229 modules->Set(module_name, exports); | 238 SetProperty(v8_context, modules, module_name, exports); |
| 230 return handle_scope.Escape(exports); | 239 return handle_scope.Escape(exports); |
| 231 } | 240 } |
| 232 | 241 |
| 233 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 242 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 234 const std::string& module_name, | 243 const std::string& module_name, |
| 235 const std::string& method_name) { | 244 const std::string& method_name) { |
| 236 v8::EscapableHandleScope handle_scope(GetIsolate()); | 245 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 237 v8::Local<v8::Value> no_args; | 246 v8::Local<v8::Value> no_args; |
| 238 return handle_scope.Escape( | 247 return handle_scope.Escape( |
| 239 CallModuleMethod(module_name, method_name, 0, &no_args)); | 248 CallModuleMethod(module_name, method_name, 0, &no_args)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 253 int argc, | 262 int argc, |
| 254 v8::Local<v8::Value> argv[]) { | 263 v8::Local<v8::Value> argv[]) { |
| 255 TRACE_EVENT2("v8", | 264 TRACE_EVENT2("v8", |
| 256 "v8.callModuleMethod", | 265 "v8.callModuleMethod", |
| 257 "module_name", | 266 "module_name", |
| 258 module_name, | 267 module_name, |
| 259 "method_name", | 268 "method_name", |
| 260 method_name); | 269 method_name); |
| 261 | 270 |
| 262 v8::EscapableHandleScope handle_scope(GetIsolate()); | 271 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 263 v8::Context::Scope context_scope(context()->v8_context()); | 272 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 273 v8::Context::Scope context_scope(v8_context); |
| 274 |
| 275 if (module_name.size() >= v8::String::kMaxLength || |
| 276 method_name.size() >= v8::String::kMaxLength) |
| 277 return handle_scope.Escape( |
| 278 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
| 264 | 279 |
| 265 v8::Local<v8::Value> module; | 280 v8::Local<v8::Value> module; |
| 266 { | 281 { |
| 267 NativesEnabledScope natives_enabled(this); | 282 NativesEnabledScope natives_enabled(this); |
| 268 module = RequireForJsInner( | 283 module = RequireForJsInner(ToV8String(GetIsolate(), module_name.c_str())); |
| 269 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | |
| 270 } | 284 } |
| 271 | 285 |
| 272 if (module.IsEmpty() || !module->IsObject()) { | 286 if (module.IsEmpty() || !module->IsObject()) { |
| 273 Fatal(context_, | 287 Fatal(context_, |
| 274 "Failed to get module " + module_name + " to call " + method_name); | 288 "Failed to get module " + module_name + " to call " + method_name); |
| 275 return handle_scope.Escape( | 289 return handle_scope.Escape( |
| 276 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | 290 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
| 277 } | 291 } |
| 278 | 292 |
| 279 v8::Local<v8::Value> value = v8::Local<v8::Object>::Cast(module)->Get( | 293 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module); |
| 280 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); | 294 v8::Local<v8::String> v8_method_name = |
| 281 if (value.IsEmpty() || !value->IsFunction()) { | 295 ToV8String(GetIsolate(), method_name.c_str()); |
| 296 v8::Local<v8::Value> value; |
| 297 if (!object->Get(v8_context, v8_method_name).ToLocal(&value) || |
| 298 !value->IsFunction()) { |
| 282 Fatal(context_, module_name + "." + method_name + " is not a function"); | 299 Fatal(context_, module_name + "." + method_name + " is not a function"); |
| 283 return handle_scope.Escape( | 300 return handle_scope.Escape( |
| 284 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | 301 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
| 285 } | 302 } |
| 286 | 303 |
| 287 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value); | 304 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value); |
| 288 v8::Local<v8::Value> result; | 305 v8::Local<v8::Value> result; |
| 289 { | 306 { |
| 290 v8::TryCatch try_catch; | 307 v8::TryCatch try_catch(GetIsolate()); |
| 291 try_catch.SetCaptureMessage(true); | 308 try_catch.SetCaptureMessage(true); |
| 292 result = context_->CallFunction(func, argc, argv); | 309 result = context_->CallFunction(func, argc, argv); |
| 293 if (try_catch.HasCaught()) | 310 if (try_catch.HasCaught()) { |
| 294 HandleException(try_catch); | 311 HandleException(try_catch); |
| 312 result = v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())); |
| 313 } |
| 295 } | 314 } |
| 296 return handle_scope.Escape(result); | 315 return handle_scope.Escape(result); |
| 297 } | 316 } |
| 298 | 317 |
| 299 void ModuleSystem::RegisterNativeHandler( | 318 void ModuleSystem::RegisterNativeHandler( |
| 300 const std::string& name, | 319 const std::string& name, |
| 301 scoped_ptr<NativeHandler> native_handler) { | 320 scoped_ptr<NativeHandler> native_handler) { |
| 302 ClobberExistingNativeHandler(name); | 321 ClobberExistingNativeHandler(name); |
| 303 native_handler_map_[name] = | 322 native_handler_map_[name] = |
| 304 linked_ptr<NativeHandler>(native_handler.release()); | 323 linked_ptr<NativeHandler>(native_handler.release()); |
| 305 } | 324 } |
| 306 | 325 |
| 307 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { | 326 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
| 308 ClobberExistingNativeHandler(name); | 327 ClobberExistingNativeHandler(name); |
| 309 overridden_native_handlers_.insert(name); | 328 overridden_native_handlers_.insert(name); |
| 310 } | 329 } |
| 311 | 330 |
| 312 void ModuleSystem::RunString(const std::string& code, const std::string& name) { | 331 void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
| 313 v8::HandleScope handle_scope(GetIsolate()); | 332 v8::HandleScope handle_scope(GetIsolate()); |
| 314 RunString(v8::String::NewFromUtf8(GetIsolate(), code.c_str()), | 333 if (code.size() >= v8::String::kMaxLength || |
| 315 v8::String::NewFromUtf8(GetIsolate(), name.c_str())); | 334 name.size() >= v8::String::kMaxLength) { |
| 335 Warn(GetIsolate(), "Too long code or name."); |
| 336 return; |
| 337 } |
| 338 RunString(ToV8String(GetIsolate(), code.c_str()), |
| 339 ToV8String(GetIsolate(), name.c_str())); |
| 316 } | 340 } |
| 317 | 341 |
| 318 // static | 342 // static |
| 319 void ModuleSystem::NativeLazyFieldGetter( | 343 void ModuleSystem::NativeLazyFieldGetter( |
| 320 v8::Local<v8::String> property, | 344 v8::Local<v8::Name> property, |
| 321 const v8::PropertyCallbackInfo<v8::Value>& info) { | 345 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 322 LazyFieldGetterInner(property, info, &ModuleSystem::RequireNativeFromString); | 346 LazyFieldGetterInner(property.As<v8::String>(), info, |
| 347 &ModuleSystem::RequireNativeFromString); |
| 323 } | 348 } |
| 324 | 349 |
| 325 // static | 350 // static |
| 326 void ModuleSystem::LazyFieldGetter( | 351 void ModuleSystem::LazyFieldGetter( |
| 327 v8::Local<v8::String> property, | 352 v8::Local<v8::Name> property, |
| 328 const v8::PropertyCallbackInfo<v8::Value>& info) { | 353 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 329 LazyFieldGetterInner(property, info, &ModuleSystem::Require); | 354 LazyFieldGetterInner(property.As<v8::String>(), info, &ModuleSystem::Require); |
| 330 } | 355 } |
| 331 | 356 |
| 332 // static | 357 // static |
| 333 void ModuleSystem::LazyFieldGetterInner( | 358 void ModuleSystem::LazyFieldGetterInner( |
| 334 v8::Local<v8::String> property, | 359 v8::Local<v8::String> property, |
| 335 const v8::PropertyCallbackInfo<v8::Value>& info, | 360 const v8::PropertyCallbackInfo<v8::Value>& info, |
| 336 RequireFunction require_function) { | 361 RequireFunction require_function) { |
| 337 CHECK(!info.Data().IsEmpty()); | 362 CHECK(!info.Data().IsEmpty()); |
| 338 CHECK(info.Data()->IsObject()); | 363 CHECK(info.Data()->IsObject()); |
| 339 v8::HandleScope handle_scope(info.GetIsolate()); | 364 v8::HandleScope handle_scope(info.GetIsolate()); |
| 340 v8::Local<v8::Object> parameters = v8::Local<v8::Object>::Cast(info.Data()); | 365 v8::Local<v8::Object> parameters = v8::Local<v8::Object>::Cast(info.Data()); |
| 341 // This context should be the same as context()->v8_context(). | 366 // This context should be the same as context()->v8_context(). |
| 342 v8::Local<v8::Context> context = parameters->CreationContext(); | 367 v8::Local<v8::Context> context = parameters->CreationContext(); |
| 343 v8::Local<v8::Object> global(context->Global()); | 368 v8::Local<v8::Object> global(context->Global()); |
| 344 v8::Local<v8::Value> module_system_value = global->GetHiddenValue( | 369 v8::Local<v8::Value> module_system_value = global->GetHiddenValue( |
| 345 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); | 370 ToV8String(info.GetIsolate(), kModuleSystem)); |
| 346 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { | 371 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
| 347 // ModuleSystem has been deleted. | 372 // ModuleSystem has been deleted. |
| 348 // TODO(kalman): See comment in header file. | 373 // TODO(kalman): See comment in header file. |
| 349 Warn(info.GetIsolate(), | 374 Warn(info.GetIsolate(), |
| 350 "Module system has been deleted, does extension view exist?"); | 375 "Module system has been deleted, does extension view exist?"); |
| 351 return; | 376 return; |
| 352 } | 377 } |
| 353 | 378 |
| 354 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 379 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 355 v8::Local<v8::External>::Cast(module_system_value)->Value()); | 380 v8::Local<v8::External>::Cast(module_system_value)->Value()); |
| 356 | 381 |
| 357 std::string name = *v8::String::Utf8Value(parameters->Get( | 382 v8::Local<v8::String> v8_module_name = |
| 358 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))); | 383 ToV8String(info.GetIsolate(), kModuleName); |
| 384 v8::Local<v8::Value> v8_name; |
| 385 if (!parameters->Get(context, v8_module_name).ToLocal(&v8_name)) { |
| 386 Warn(info.GetIsolate(), "Cannot find module."); |
| 387 return; |
| 388 } |
| 389 std::string name = *v8::String::Utf8Value(v8_name); |
| 359 | 390 |
| 360 // Switch to our v8 context because we need functions created while running | 391 // Switch to our v8 context because we need functions created while running |
| 361 // the require()d module to belong to our context, not the current one. | 392 // the require()d module to belong to our context, not the current one. |
| 362 v8::Context::Scope context_scope(context); | 393 v8::Context::Scope context_scope(context); |
| 363 NativesEnabledScope natives_enabled_scope(module_system); | 394 NativesEnabledScope natives_enabled_scope(module_system); |
| 364 | 395 |
| 365 v8::TryCatch try_catch; | 396 v8::TryCatch try_catch(info.GetIsolate()); |
| 366 v8::Local<v8::Value> module_value = (module_system->*require_function)(name); | 397 v8::Local<v8::Value> module_value = (module_system->*require_function)(name); |
| 367 if (try_catch.HasCaught()) { | 398 if (try_catch.HasCaught()) { |
| 368 module_system->HandleException(try_catch); | 399 module_system->HandleException(try_catch); |
| 369 return; | 400 return; |
| 370 } | 401 } |
| 371 if (module_value.IsEmpty() || !module_value->IsObject()) { | 402 if (module_value.IsEmpty() || !module_value->IsObject()) { |
| 372 // require_function will have already logged this, we don't need to. | 403 // require_function will have already logged this, we don't need to. |
| 373 return; | 404 return; |
| 374 } | 405 } |
| 375 | 406 |
| 376 v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value); | 407 v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value); |
| 377 v8::Local<v8::String> field = | 408 v8::Local<v8::Value> field_value; |
| 378 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) | 409 if (!parameters->Get(context, ToV8String(info.GetIsolate(), kModuleField)) |
| 379 ->ToString(info.GetIsolate()); | 410 .ToLocal(&field_value)) { |
| 411 module_system->HandleException(try_catch); |
| 412 return; |
| 413 } |
| 414 v8::Local<v8::String> field; |
| 415 if (!field_value->ToString(context).ToLocal(&field)) { |
| 416 module_system->HandleException(try_catch); |
| 417 return; |
| 418 } |
| 380 | 419 |
| 381 if (!module->Has(field)) { | 420 if (!CheckV8Call(module->Has(context, field))) { |
| 382 std::string field_str = *v8::String::Utf8Value(field); | 421 std::string field_str = *v8::String::Utf8Value(field); |
| 383 Fatal(module_system->context_, | 422 Fatal(module_system->context_, |
| 384 "Lazy require of " + name + "." + field_str + " did not set the " + | 423 "Lazy require of " + name + "." + field_str + " did not set the " + |
| 385 field_str + " field"); | 424 field_str + " field"); |
| 386 return; | 425 return; |
| 387 } | 426 } |
| 388 | 427 |
| 389 v8::Local<v8::Value> new_field = module->Get(field); | 428 v8::Local<v8::Value> new_field; |
| 390 if (try_catch.HasCaught()) { | 429 if (!module->Get(context, field).ToLocal(&new_field)) { |
| 391 module_system->HandleException(try_catch); | 430 module_system->HandleException(try_catch); |
| 392 return; | 431 return; |
| 393 } | 432 } |
| 394 | 433 |
| 395 // Ok for it to be undefined, among other things it's how bindings signify | 434 // Ok for it to be undefined, among other things it's how bindings signify |
| 396 // that the extension doesn't have permission to use them. | 435 // that the extension doesn't have permission to use them. |
| 397 CHECK(!new_field.IsEmpty()); | 436 CHECK(!new_field.IsEmpty()); |
| 398 | 437 |
| 399 // Delete the getter and set this field to |new_field| so the same object is | 438 // Delete the getter and set this field to |new_field| so the same object is |
| 400 // returned every time a certain API is accessed. | 439 // returned every time a certain API is accessed. |
| 401 v8::Local<v8::Value> val = info.This(); | 440 v8::Local<v8::Value> val = info.This(); |
| 402 if (val->IsObject()) { | 441 if (val->IsObject()) { |
| 403 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(val); | 442 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(val); |
| 404 object->Delete(property); | 443 object->Delete(context, property); |
| 405 object->Set(property, new_field); | 444 SetProperty(context, object, property, new_field); |
| 406 } else { | 445 } else { |
| 407 NOTREACHED(); | 446 NOTREACHED(); |
| 408 } | 447 } |
| 409 info.GetReturnValue().Set(new_field); | 448 info.GetReturnValue().Set(new_field); |
| 410 } | 449 } |
| 411 | 450 |
| 412 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object, | 451 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object, |
| 413 const std::string& field, | 452 const std::string& field, |
| 414 const std::string& module_name, | 453 const std::string& module_name, |
| 415 const std::string& module_field) { | 454 const std::string& module_field) { |
| 416 SetLazyField( | 455 SetLazyField( |
| 417 object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter); | 456 object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter); |
| 418 } | 457 } |
| 419 | 458 |
| 420 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object, | 459 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object, |
| 421 const std::string& field, | 460 const std::string& field, |
| 422 const std::string& module_name, | 461 const std::string& module_name, |
| 423 const std::string& module_field, | 462 const std::string& module_field, |
| 424 v8::AccessorGetterCallback getter) { | 463 v8::AccessorNameGetterCallback getter) { |
| 464 CHECK(field.size() < v8::String::kMaxLength); |
| 465 CHECK(module_name.size() < v8::String::kMaxLength); |
| 466 CHECK(module_field.size() < v8::String::kMaxLength); |
| 425 v8::HandleScope handle_scope(GetIsolate()); | 467 v8::HandleScope handle_scope(GetIsolate()); |
| 426 v8::Local<v8::Object> parameters = v8::Object::New(GetIsolate()); | 468 v8::Local<v8::Object> parameters = v8::Object::New(GetIsolate()); |
| 427 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName), | 469 v8::Local<v8::Context> context = context_->v8_context(); |
| 428 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | 470 SetProperty(context, parameters, ToV8String(GetIsolate(), kModuleName), |
| 429 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField), | 471 ToV8String(GetIsolate(), module_name.c_str())); |
| 430 v8::String::NewFromUtf8(GetIsolate(), module_field.c_str())); | 472 SetProperty(context, parameters, ToV8String(GetIsolate(), kModuleField), |
| 431 object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()), | 473 ToV8String(GetIsolate(), module_field.c_str())); |
| 432 getter, | 474 auto maybe = |
| 433 NULL, | 475 object->SetAccessor(context, ToV8String(GetIsolate(), field.c_str()), |
| 434 parameters); | 476 getter, NULL, parameters); |
| 477 DCHECK(maybe.IsJust() && maybe.FromJust()); |
| 435 } | 478 } |
| 436 | 479 |
| 437 void ModuleSystem::SetNativeLazyField(v8::Local<v8::Object> object, | 480 void ModuleSystem::SetNativeLazyField(v8::Local<v8::Object> object, |
| 438 const std::string& field, | 481 const std::string& field, |
| 439 const std::string& module_name, | 482 const std::string& module_name, |
| 440 const std::string& module_field) { | 483 const std::string& module_field) { |
| 441 SetLazyField(object, | 484 SetLazyField(object, |
| 442 field, | 485 field, |
| 443 module_name, | 486 module_name, |
| 444 module_field, | 487 module_field, |
| 445 &ModuleSystem::NativeLazyFieldGetter); | 488 &ModuleSystem::NativeLazyFieldGetter); |
| 446 } | 489 } |
| 447 | 490 |
| 448 v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code, | 491 v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code, |
| 449 v8::Local<v8::String> name) { | 492 v8::Local<v8::String> name) { |
| 450 v8::EscapableHandleScope handle_scope(GetIsolate()); | 493 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 451 v8::Context::Scope context_scope(context()->v8_context()); | 494 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 495 v8::Context::Scope context_scope(v8_context); |
| 452 | 496 |
| 453 // Prepend extensions:: to |name| so that internal code can be differentiated | 497 // Prepend extensions:: to |name| so that internal code can be differentiated |
| 454 // from external code in stack traces. This has no effect on behaviour. | 498 // from external code in stack traces. This has no effect on behaviour. |
| 455 std::string internal_name = | 499 std::string internal_name = |
| 456 base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name)); | 500 base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name)); |
| 457 | 501 |
| 502 if (internal_name.size() >= v8::String::kMaxLength) |
| 503 return v8::Undefined(GetIsolate()); |
| 504 |
| 458 blink::WebScopedMicrotaskSuppression suppression; | 505 blink::WebScopedMicrotaskSuppression suppression; |
| 459 v8::TryCatch try_catch; | 506 v8::TryCatch try_catch(GetIsolate()); |
| 460 try_catch.SetCaptureMessage(true); | 507 try_catch.SetCaptureMessage(true); |
| 461 v8::Local<v8::Script> script(v8::Script::Compile( | 508 v8::ScriptOrigin origin(ToV8String(GetIsolate(), internal_name.c_str())); |
| 462 code, v8::String::NewFromUtf8(GetIsolate(), internal_name.c_str(), | 509 v8::Local<v8::Script> script; |
| 463 v8::String::kNormalString, | 510 if (!v8::Script::Compile(v8_context, code, &origin).ToLocal(&script)) { |
| 464 internal_name.size()))); | |
| 465 if (try_catch.HasCaught()) { | |
| 466 HandleException(try_catch); | 511 HandleException(try_catch); |
| 467 return v8::Undefined(GetIsolate()); | 512 return v8::Undefined(GetIsolate()); |
| 468 } | 513 } |
| 469 | 514 |
| 470 v8::Local<v8::Value> result = script->Run(); | 515 v8::Local<v8::Value> result; |
| 471 if (try_catch.HasCaught()) { | 516 if (!script->Run(v8_context).ToLocal(&result)) { |
| 472 HandleException(try_catch); | 517 HandleException(try_catch); |
| 473 return v8::Undefined(GetIsolate()); | 518 return v8::Undefined(GetIsolate()); |
| 474 } | 519 } |
| 475 | 520 |
| 476 return handle_scope.Escape(result); | 521 return handle_scope.Escape(result); |
| 477 } | 522 } |
| 478 | 523 |
| 479 v8::Local<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { | 524 v8::Local<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
| 480 v8::EscapableHandleScope handle_scope(GetIsolate()); | 525 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 481 if (!source_map_->Contains(module_name)) | 526 if (!source_map_->Contains(module_name)) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 492 } | 537 } |
| 493 | 538 |
| 494 v8::Local<v8::Value> ModuleSystem::RequireNativeFromString( | 539 v8::Local<v8::Value> ModuleSystem::RequireNativeFromString( |
| 495 const std::string& native_name) { | 540 const std::string& native_name) { |
| 496 if (natives_enabled_ == 0) { | 541 if (natives_enabled_ == 0) { |
| 497 // HACK: if in test throw exception so that we can test the natives-disabled | 542 // HACK: if in test throw exception so that we can test the natives-disabled |
| 498 // logic; however, under normal circumstances, this is programmer error so | 543 // logic; however, under normal circumstances, this is programmer error so |
| 499 // we could crash. | 544 // we could crash. |
| 500 if (exception_handler_) { | 545 if (exception_handler_) { |
| 501 return GetIsolate()->ThrowException( | 546 return GetIsolate()->ThrowException( |
| 502 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); | 547 ToV8String(GetIsolate(), "Natives disabled")); |
| 503 } | 548 } |
| 504 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); | 549 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); |
| 505 return v8::Undefined(GetIsolate()); | 550 return v8::Undefined(GetIsolate()); |
| 506 } | 551 } |
| 507 | 552 |
| 508 if (overridden_native_handlers_.count(native_name) > 0u) { | 553 if (overridden_native_handlers_.count(native_name) > 0u) { |
| 509 return RequireForJsInner( | 554 return RequireForJsInner( |
| 510 v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); | 555 ToV8String(GetIsolate(), native_name.c_str())); |
| 511 } | 556 } |
| 512 | 557 |
| 513 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 558 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
| 514 if (i == native_handler_map_.end()) { | 559 if (i == native_handler_map_.end()) { |
| 515 Fatal(context_, | 560 Fatal(context_, |
| 516 "Couldn't find native for requireNative(" + native_name + ")"); | 561 "Couldn't find native for requireNative(" + native_name + ")"); |
| 517 return v8::Undefined(GetIsolate()); | 562 return v8::Undefined(GetIsolate()); |
| 518 } | 563 } |
| 519 return i->second->NewInstance(); | 564 v8::Local<v8::Value> instance; |
| 565 if (!i->second->NewInstance().ToLocal(&instance)) { |
| 566 Fatal(context_, |
| 567 "Cannot instanciate native for requireNative(" + native_name + ")"); |
| 568 return v8::Undefined(GetIsolate()); |
| 569 } |
| 570 return instance; |
| 520 } | 571 } |
| 521 | 572 |
| 522 void ModuleSystem::RequireAsync( | 573 void ModuleSystem::RequireAsync( |
| 523 const v8::FunctionCallbackInfo<v8::Value>& args) { | 574 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 524 CHECK_EQ(1, args.Length()); | 575 CHECK_EQ(1, args.Length()); |
| 525 std::string module_name = *v8::String::Utf8Value(args[0]); | 576 std::string module_name = *v8::String::Utf8Value(args[0]); |
| 577 v8::Local<v8::Context> v8_context = context_->v8_context(); |
| 526 v8::Local<v8::Promise::Resolver> resolver( | 578 v8::Local<v8::Promise::Resolver> resolver( |
| 527 v8::Promise::Resolver::New(GetIsolate())); | 579 v8::Promise::Resolver::New(v8_context).ToLocalChecked()); |
| 528 args.GetReturnValue().Set(resolver->GetPromise()); | 580 args.GetReturnValue().Set(resolver->GetPromise()); |
| 529 scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver( | 581 scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver( |
| 530 new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver)); | 582 new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver)); |
| 531 gin::ModuleRegistry* module_registry = | 583 gin::ModuleRegistry* module_registry = |
| 532 gin::ModuleRegistry::From(context_->v8_context()); | 584 gin::ModuleRegistry::From(v8_context); |
| 533 if (!module_registry) { | 585 if (!module_registry) { |
| 534 Warn(GetIsolate(), "Extension view no longer exists"); | 586 Warn(GetIsolate(), "Extension view no longer exists"); |
| 535 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( | 587 resolver->Reject(v8_context, v8::Exception::Error(ToV8String( |
| 536 GetIsolate(), "Extension view no longer exists"))); | 588 GetIsolate(), "Extension view no longer exists"))); |
| 537 return; | 589 return; |
| 538 } | 590 } |
| 539 module_registry->LoadModule( | 591 module_registry->LoadModule( |
| 540 GetIsolate(), module_name, | 592 GetIsolate(), module_name, |
| 541 base::Bind(&ModuleSystem::OnModuleLoaded, weak_factory_.GetWeakPtr(), | 593 base::Bind(&ModuleSystem::OnModuleLoaded, weak_factory_.GetWeakPtr(), |
| 542 base::Passed(&global_resolver))); | 594 base::Passed(&global_resolver))); |
| 543 if (module_registry->available_modules().count(module_name) == 0) | 595 if (module_registry->available_modules().count(module_name) == 0) |
| 544 LoadModule(module_name); | 596 LoadModule(module_name); |
| 545 } | 597 } |
| 546 | 598 |
| 547 v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) { | 599 v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) { |
| 548 v8::EscapableHandleScope handle_scope(GetIsolate()); | 600 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 549 // Keep in order with the arguments in RequireForJsInner. | 601 // Keep in order with the arguments in RequireForJsInner. |
| 550 v8::Local<v8::String> left = v8::String::NewFromUtf8( | 602 v8::Local<v8::String> left = ToV8String( |
| 551 GetIsolate(), | 603 GetIsolate(), |
| 552 "(function(define, require, requireNative, requireAsync, exports, " | 604 "(function(define, require, requireNative, requireAsync, exports, " |
| 553 "console, privates," | 605 "console, privates," |
| 554 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" | 606 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" |
| 555 "'use strict';"); | 607 "'use strict';"); |
| 556 v8::Local<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); | 608 v8::Local<v8::String> right = ToV8String(GetIsolate(), "\n})"); |
| 557 return handle_scope.Escape(v8::Local<v8::String>( | 609 return handle_scope.Escape(v8::Local<v8::String>( |
| 558 v8::String::Concat(left, v8::String::Concat(source, right)))); | 610 v8::String::Concat(left, v8::String::Concat(source, right)))); |
| 559 } | 611 } |
| 560 | 612 |
| 561 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { | 613 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 562 CHECK_EQ(1, args.Length()); | 614 CHECK_EQ(1, args.Length()); |
| 563 if (!args[0]->IsObject() || args[0]->IsNull()) { | 615 if (!args[0]->IsObject() || args[0]->IsNull()) { |
| 564 GetIsolate()->ThrowException( | 616 GetIsolate()->ThrowException( |
| 565 v8::Exception::TypeError(v8::String::NewFromUtf8(GetIsolate(), | 617 v8::Exception::TypeError(ToV8String(GetIsolate(), |
| 566 args[0]->IsUndefined() | 618 args[0]->IsUndefined() |
| 567 ? "Method called without a valid receiver (this). " | 619 ? "Method called without a valid receiver (this). " |
| 568 "Did you forget to call .bind()?" | 620 "Did you forget to call .bind()?" |
| 569 : "Invalid invocation: receiver is not an object!"))); | 621 : "Invalid invocation: receiver is not an object!"))); |
| 570 return; | 622 return; |
| 571 } | 623 } |
| 572 v8::Local<v8::Object> obj = args[0].As<v8::Object>(); | 624 v8::Local<v8::Object> obj = args[0].As<v8::Object>(); |
| 573 v8::Local<v8::String> privates_key = | 625 v8::Local<v8::String> privates_key = ToV8String(GetIsolate(), "privates"); |
| 574 v8::String::NewFromUtf8(GetIsolate(), "privates"); | |
| 575 v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key); | 626 v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key); |
| 576 if (privates.IsEmpty()) { | 627 if (privates.IsEmpty()) { |
| 577 privates = v8::Object::New(args.GetIsolate()); | 628 privates = v8::Object::New(args.GetIsolate()); |
| 578 if (privates.IsEmpty()) { | 629 if (privates.IsEmpty()) { |
| 579 GetIsolate()->ThrowException( | 630 GetIsolate()->ThrowException( |
| 580 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); | 631 ToV8String(GetIsolate(), "Failed to create privates")); |
| 581 return; | 632 return; |
| 582 } | 633 } |
| 583 obj->SetHiddenValue(privates_key, privates); | 634 obj->SetHiddenValue(privates_key, privates); |
| 584 } | 635 } |
| 585 args.GetReturnValue().Set(privates); | 636 args.GetReturnValue().Set(privates); |
| 586 } | 637 } |
| 587 | 638 |
| 588 v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { | 639 v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
| 589 v8::EscapableHandleScope handle_scope(GetIsolate()); | 640 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 590 v8::Context::Scope context_scope(context()->v8_context()); | 641 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 642 v8::Context::Scope context_scope(v8_context); |
| 591 | 643 |
| 592 v8::Local<v8::Value> source(GetSource(module_name)); | 644 v8::Local<v8::Value> source(GetSource(module_name)); |
| 593 if (source.IsEmpty() || source->IsUndefined()) { | 645 if (source.IsEmpty() || source->IsUndefined()) { |
| 594 Fatal(context_, "No source for require(" + module_name + ")"); | 646 Fatal(context_, "No source for require(" + module_name + ")"); |
| 595 return v8::Undefined(GetIsolate()); | 647 return v8::Undefined(GetIsolate()); |
| 596 } | 648 } |
| 597 v8::Local<v8::String> wrapped_source( | 649 v8::Local<v8::String> wrapped_source( |
| 598 WrapSource(v8::Local<v8::String>::Cast(source))); | 650 WrapSource(v8::Local<v8::String>::Cast(source))); |
| 651 if (module_name.size() >= v8::String::kMaxLength) { |
| 652 Fatal(context_, "Module name is too long"); |
| 653 return v8::Undefined(GetIsolate()); |
| 654 } |
| 599 // Modules are wrapped in (function(){...}) so they always return functions. | 655 // Modules are wrapped in (function(){...}) so they always return functions. |
| 600 v8::Local<v8::Value> func_as_value = | 656 v8::Local<v8::Value> func_as_value = |
| 601 RunString(wrapped_source, | 657 RunString(wrapped_source, |
| 602 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | 658 ToV8String(GetIsolate(), module_name.c_str())); |
| 603 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 659 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
| 604 Fatal(context_, "Bad source for require(" + module_name + ")"); | 660 Fatal(context_, "Bad source for require(" + module_name + ")"); |
| 605 return v8::Undefined(GetIsolate()); | 661 return v8::Undefined(GetIsolate()); |
| 606 } | 662 } |
| 607 | 663 |
| 608 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_as_value); | 664 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_as_value); |
| 609 | 665 |
| 610 v8::Local<v8::Object> define_object = v8::Object::New(GetIsolate()); | 666 v8::Local<v8::Object> define_object = v8::Object::New(GetIsolate()); |
| 611 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); | 667 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); |
| 612 | 668 |
| 613 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); | 669 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); |
| 614 v8::Local<v8::Object> natives(NewInstance()); | 670 v8::Local<v8::Object> natives; |
| 615 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues | 671 CHECK(NewInstance().ToLocal(&natives)); // this can fail if v8 has issues |
| 616 | 672 |
| 617 // These must match the argument order in WrapSource. | 673 // These must match the argument order in WrapSource. |
| 618 v8::Local<v8::Value> args[] = { | 674 v8::Local<v8::Value> args[] = { |
| 619 // AMD. | 675 // AMD. |
| 620 define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")), | 676 UnsafeGet(v8_context, define_object, ToV8String(GetIsolate(), "define")), |
| 621 // CommonJS. | 677 // CommonJS. |
| 622 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "require", | 678 UnsafeGet(v8_context, natives, |
| 623 v8::String::kInternalizedString)), | 679 ToV8String(GetIsolate(), "require", |
| 624 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireNative", | 680 v8::NewStringType::kInternalized)), |
| 625 v8::String::kInternalizedString)), | 681 UnsafeGet(v8_context, natives, |
| 626 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireAsync", | 682 ToV8String(GetIsolate(), "requireNative", |
| 627 v8::String::kInternalizedString)), | 683 v8::NewStringType::kInternalized)), |
| 684 UnsafeGet(v8_context, natives, |
| 685 ToV8String(GetIsolate(), "requireAsync", |
| 686 v8::NewStringType::kInternalized)), |
| 628 exports, | 687 exports, |
| 629 // Libraries that we magically expose to every module. | 688 // Libraries that we magically expose to every module. |
| 630 console::AsV8Object(GetIsolate()), | 689 console::AsV8Object(GetIsolate()), |
| 631 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "privates", | 690 UnsafeGet(v8_context, natives, |
| 632 v8::String::kInternalizedString)), | 691 ToV8String(GetIsolate(), "privates", |
| 692 v8::NewStringType::kInternalized)), |
| 633 // Each safe builtin. Keep in order with the arguments in WrapSource. | 693 // Each safe builtin. Keep in order with the arguments in WrapSource. |
| 634 context_->safe_builtins()->GetArray(), | 694 context_->safe_builtins()->GetArray(), |
| 635 context_->safe_builtins()->GetFunction(), | 695 context_->safe_builtins()->GetFunction(), |
| 636 context_->safe_builtins()->GetJSON(), | 696 context_->safe_builtins()->GetJSON(), |
| 637 context_->safe_builtins()->GetObjekt(), | 697 context_->safe_builtins()->GetObjekt(), |
| 638 context_->safe_builtins()->GetRegExp(), | 698 context_->safe_builtins()->GetRegExp(), |
| 639 context_->safe_builtins()->GetString(), | 699 context_->safe_builtins()->GetString(), |
| 640 context_->safe_builtins()->GetError(), | 700 context_->safe_builtins()->GetError(), |
| 641 }; | 701 }; |
| 642 { | 702 { |
| 643 v8::TryCatch try_catch; | 703 v8::TryCatch try_catch(GetIsolate()); |
| 644 try_catch.SetCaptureMessage(true); | 704 try_catch.SetCaptureMessage(true); |
| 645 context_->CallFunction(func, arraysize(args), args); | 705 context_->CallFunction(func, arraysize(args), args); |
| 646 if (try_catch.HasCaught()) { | 706 if (try_catch.HasCaught()) { |
| 647 HandleException(try_catch); | 707 HandleException(try_catch); |
| 648 return v8::Undefined(GetIsolate()); | 708 return v8::Undefined(GetIsolate()); |
| 649 } | 709 } |
| 650 } | 710 } |
| 651 return handle_scope.Escape(exports); | 711 return handle_scope.Escape(exports); |
| 652 } | 712 } |
| 653 | 713 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 673 } | 733 } |
| 674 | 734 |
| 675 void ModuleSystem::OnModuleLoaded( | 735 void ModuleSystem::OnModuleLoaded( |
| 676 scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, | 736 scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, |
| 677 v8::Local<v8::Value> value) { | 737 v8::Local<v8::Value> value) { |
| 678 if (!is_valid()) | 738 if (!is_valid()) |
| 679 return; | 739 return; |
| 680 v8::HandleScope handle_scope(GetIsolate()); | 740 v8::HandleScope handle_scope(GetIsolate()); |
| 681 v8::Local<v8::Promise::Resolver> resolver_local( | 741 v8::Local<v8::Promise::Resolver> resolver_local( |
| 682 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); | 742 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); |
| 683 resolver_local->Resolve(value); | 743 resolver_local->Resolve(GetIsolate()->GetCurrentContext(), value); |
| 684 } | 744 } |
| 685 | 745 |
| 686 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { | 746 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { |
| 687 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); | 747 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); |
| 688 if (existing_handler != native_handler_map_.end()) { | 748 if (existing_handler != native_handler_map_.end()) { |
| 689 clobbered_native_handlers_.push_back(existing_handler->second); | 749 clobbered_native_handlers_.push_back(existing_handler->second); |
| 690 native_handler_map_.erase(existing_handler); | 750 native_handler_map_.erase(existing_handler); |
| 691 } | 751 } |
| 692 } | 752 } |
| 693 | 753 |
| 694 } // namespace extensions | 754 } // namespace extensions |
| OLD | NEW |