Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: extensions/renderer/module_system.cc

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

Powered by Google App Engine
This is Rietveld 408576698