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

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

Issue 12632004: Revert 186643 - Caused a 10% regression on SunSpider benchmark (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/extensions/module_system.h" 5 #include "chrome/renderer/extensions/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/alias.h"
9 #include "base/stl_util.h" 8 #include "base/stl_util.h"
10 #include "base/string_util.h"
11 #include "base/stringprintf.h"
12 #include "chrome/common/extensions/extension_messages.h"
13 #include "chrome/renderer/extensions/chrome_v8_context.h"
14 #include "content/public/renderer/render_view.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h" 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h"
16 10
17 namespace { 11 namespace {
18 12
19 const char* kModuleSystem = "module_system"; 13 const char* kModuleSystem = "module_system";
20 const char* kModuleName = "module_name"; 14 const char* kModuleName = "module_name";
21 const char* kModuleField = "module_field"; 15 const char* kModuleField = "module_field";
22 const char* kModulesField = "modules"; 16 const char* kModulesField = "modules";
23 17
24 } // namespace 18 } // namespace
25 19
26 namespace extensions { 20 namespace extensions {
27 21
28 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, 22 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context,
29 SourceMap* source_map) 23 SourceMap* source_map)
30 : ObjectBackedNativeHandler(context), 24 : NativeHandler(context->GetIsolate()),
25 context_(v8::Persistent<v8::Context>::New(context->GetIsolate(),
26 context)),
31 source_map_(source_map), 27 source_map_(source_map),
32 natives_enabled_(0) { 28 natives_enabled_(0) {
33 RouteFunction("require", 29 RouteFunction("require",
34 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); 30 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
35 RouteFunction("requireNative", 31 RouteFunction("requireNative",
36 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); 32 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
37 33
38 v8::Handle<v8::Object> global(context->Global()); 34 v8::Handle<v8::Object> global(context_->Global());
39 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); 35 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
40 global->SetHiddenValue(v8::String::New(kModuleSystem), 36 global->SetHiddenValue(v8::String::New(kModuleSystem),
41 v8::External::New(this)); 37 v8::External::New(this));
42 } 38 }
43 39
44 ModuleSystem::~ModuleSystem() { 40 ModuleSystem::~ModuleSystem() {
45 Invalidate(); 41 v8::HandleScope handle_scope;
46 } 42 // Deleting this value here prevents future lazy field accesses from
47 43 // referencing ModuleSystem after it has been freed.
48 void ModuleSystem::Invalidate() { 44 context_->Global()->DeleteHiddenValue(v8::String::New(kModuleSystem));
49 if (!is_valid()) 45 context_.Dispose(context_->GetIsolate());
50 return;
51 for (NativeHandlerMap::iterator it = native_handler_map_.begin();
52 it != native_handler_map_.end(); ++it) {
53 it->second->Invalidate();
54 }
55 ObjectBackedNativeHandler::Invalidate();
56 } 46 }
57 47
58 ModuleSystem::NativesEnabledScope::NativesEnabledScope( 48 ModuleSystem::NativesEnabledScope::NativesEnabledScope(
59 ModuleSystem* module_system) 49 ModuleSystem* module_system)
60 : module_system_(module_system) { 50 : module_system_(module_system) {
61 module_system_->natives_enabled_++; 51 module_system_->natives_enabled_++;
62 } 52 }
63 53
64 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { 54 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
65 module_system_->natives_enabled_--; 55 module_system_->natives_enabled_--;
66 CHECK_GE(module_system_->natives_enabled_, 0); 56 CHECK_GE(module_system_->natives_enabled_, 0);
67 } 57 }
68 58
59 // static
60 bool ModuleSystem::IsPresentInCurrentContext() {
61 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
62 if (global.IsEmpty())
63 return false;
64 v8::Handle<v8::Value> module_system =
65 global->GetHiddenValue(v8::String::New(kModuleSystem));
66 return !module_system.IsEmpty() && !module_system->IsUndefined();
67 }
68
69 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { 69 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
70 DumpException(try_catch); 70 DumpException(try_catch);
71 if (exception_handler_.get()) 71 if (exception_handler_.get())
72 exception_handler_->HandleUncaughtException(); 72 exception_handler_->HandleUncaughtException();
73 } 73 }
74 74
75 // static 75 // static
76 std::string ModuleSystem::CreateExceptionString(const v8::TryCatch& try_catch) { 76 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) {
77 v8::HandleScope handle_scope;
78
77 v8::Handle<v8::Message> message(try_catch.Message()); 79 v8::Handle<v8::Message> message(try_catch.Message());
78 if (message.IsEmpty()) { 80 if (message.IsEmpty()) {
79 return "try_catch has no message"; 81 LOG(ERROR) << "try_catch has no message";
82 return;
80 } 83 }
81 84
82 std::string resource_name = "<unknown resource>"; 85 std::string resource_name = "<unknown resource>";
83 if (!message->GetScriptResourceName().IsEmpty()) { 86 if (!message->GetScriptResourceName().IsEmpty()) {
84 resource_name = 87 resource_name =
85 *v8::String::Utf8Value(message->GetScriptResourceName()->ToString()); 88 *v8::String::Utf8Value(message->GetScriptResourceName()->ToString());
86 } 89 }
87 90
88 std::string error_message = "<no error message>"; 91 std::string error_message = "<no error message>";
89 if (!message->Get().IsEmpty()) 92 if (!message->Get().IsEmpty())
90 error_message = *v8::String::Utf8Value(message->Get()); 93 error_message = *v8::String::Utf8Value(message->Get());
91 94
92 return base::StringPrintf("%s:%d: %s",
93 resource_name.c_str(),
94 message->GetLineNumber(),
95 error_message.c_str());
96 }
97
98 // static
99 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) {
100 v8::HandleScope handle_scope;
101
102 std::string stack_trace = "<stack trace unavailable>"; 95 std::string stack_trace = "<stack trace unavailable>";
103 if (!try_catch.StackTrace().IsEmpty()) { 96 if (!try_catch.StackTrace().IsEmpty()) {
104 v8::String::Utf8Value stack_value(try_catch.StackTrace()); 97 v8::String::Utf8Value stack_value(try_catch.StackTrace());
105 if (*stack_value) 98 if (*stack_value)
106 stack_trace.assign(*stack_value, stack_value.length()); 99 stack_trace.assign(*stack_value, stack_value.length());
107 else 100 else
108 stack_trace = "<could not convert stack trace to string>"; 101 stack_trace = "<could not convert stack trace to string>";
109 } 102 }
110 103
111 LOG(ERROR) << CreateExceptionString(try_catch) << "{" << stack_trace << "}"; 104 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] "
105 << error_message
106 << "{" << stack_trace << "}";
112 } 107 }
113 108
114 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { 109 void ModuleSystem::Require(const std::string& module_name) {
115 v8::HandleScope handle_scope; 110 v8::HandleScope handle_scope;
116 return handle_scope.Close( 111 RequireForJsInner(v8::String::New(module_name.c_str()));
117 RequireForJsInner(v8::String::New(module_name.c_str())));
118 } 112 }
119 113
120 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { 114 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
121 v8::HandleScope handle_scope; 115 v8::HandleScope handle_scope;
122 v8::Handle<v8::String> module_name = args[0]->ToString(); 116 v8::Handle<v8::String> module_name = args[0]->ToString();
123 return handle_scope.Close(RequireForJsInner(module_name)); 117 return handle_scope.Close(RequireForJsInner(module_name));
124 } 118 }
125 119
126 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( 120 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
127 v8::Handle<v8::String> module_name) { 121 v8::Handle<v8::String> module_name) {
128 v8::HandleScope handle_scope; 122 v8::HandleScope handle_scope;
129 v8::Handle<v8::Object> global(v8_context()->Global()); 123 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
130 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( 124 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(
131 global->GetHiddenValue(v8::String::New(kModulesField)))); 125 global->GetHiddenValue(v8::String::New(kModulesField))));
132 v8::Handle<v8::Value> exports(modules->Get(module_name)); 126 v8::Handle<v8::Value> exports(modules->Get(module_name));
133 if (!exports->IsUndefined()) 127 if (!exports->IsUndefined())
134 return handle_scope.Close(exports); 128 return handle_scope.Close(exports);
135 129
136 v8::Handle<v8::Value> source(GetSource(module_name)); 130 v8::Handle<v8::Value> source(GetSource(module_name));
137 if (source->IsUndefined()) 131 if (source->IsUndefined())
138 return handle_scope.Close(v8::Undefined()); 132 return handle_scope.Close(v8::Undefined());
139 v8::Handle<v8::String> wrapped_source(WrapSource( 133 v8::Handle<v8::String> wrapped_source(WrapSource(
(...skipping 19 matching lines...) Expand all
159 func->Call(global, 3, args); 153 func->Call(global, 3, args);
160 if (try_catch.HasCaught()) { 154 if (try_catch.HasCaught()) {
161 HandleException(try_catch); 155 HandleException(try_catch);
162 return v8::Undefined(); 156 return v8::Undefined();
163 } 157 }
164 } 158 }
165 modules->Set(module_name, exports); 159 modules->Set(module_name, exports);
166 return handle_scope.Close(exports); 160 return handle_scope.Close(exports);
167 } 161 }
168 162
169 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 163 void ModuleSystem::CallModuleMethod(const std::string& module_name,
170 const std::string& module_name, 164 const std::string& method_name) {
171 const std::string& method_name) {
172 std::vector<v8::Handle<v8::Value> > args; 165 std::vector<v8::Handle<v8::Value> > args;
173 return CallModuleMethod(module_name, method_name, &args); 166 CallModuleMethod(module_name, method_name, &args);
174 } 167 }
175 168
176 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 169 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
177 const std::string& module_name, 170 const std::string& module_name,
178 const std::string& method_name, 171 const std::string& method_name,
179 std::vector<v8::Handle<v8::Value> >* args) { 172 std::vector<v8::Handle<v8::Value> >* args) {
180 v8::HandleScope handle_scope; 173 v8::HandleScope handle_scope;
181 v8::Local<v8::Value> module = 174 v8::Local<v8::Value> module =
182 v8::Local<v8::Value>::New( 175 v8::Local<v8::Value>::New(
183 RequireForJsInner(v8::String::New(module_name.c_str()))); 176 RequireForJsInner(v8::String::New(module_name.c_str())));
184 if (module.IsEmpty() || !module->IsObject()) 177 if (module.IsEmpty() || !module->IsObject())
185 return v8::Local<v8::Value>(); 178 return v8::Local<v8::Value>();
186 v8::Local<v8::Value> value = 179 v8::Local<v8::Value> value =
187 v8::Handle<v8::Object>::Cast(module)->Get( 180 v8::Handle<v8::Object>::Cast(module)->Get(
188 v8::String::New(method_name.c_str())); 181 v8::String::New(method_name.c_str()));
189 if (value.IsEmpty() || !value->IsFunction()) 182 if (value.IsEmpty() || !value->IsFunction())
190 return v8::Local<v8::Value>(); 183 return v8::Local<v8::Value>();
191 v8::Handle<v8::Function> func = 184 v8::Handle<v8::Function> func =
192 v8::Handle<v8::Function>::Cast(value); 185 v8::Handle<v8::Function>::Cast(value);
193 v8::Handle<v8::Object> global(v8_context()->Global()); 186 // TODO(jeremya/koz): refer to context_ here, not the current context.
187 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
194 v8::Local<v8::Value> result; 188 v8::Local<v8::Value> result;
195 { 189 {
196 WebKit::WebScopedMicrotaskSuppression suppression; 190 WebKit::WebScopedMicrotaskSuppression suppression;
197 v8::TryCatch try_catch; 191 v8::TryCatch try_catch;
198 try_catch.SetCaptureMessage(true); 192 try_catch.SetCaptureMessage(true);
199 result = func->Call(global, args->size(), vector_as_array(args)); 193 result = func->Call(global, args->size(), vector_as_array(args));
200 if (try_catch.HasCaught()) 194 if (try_catch.HasCaught())
201 HandleException(try_catch); 195 HandleException(try_catch);
202 } 196 }
203 return handle_scope.Close(result); 197 return handle_scope.Close(result);
204 } 198 }
205 199
206 bool ModuleSystem::HasNativeHandler(const std::string& name) {
207 return native_handler_map_.find(name) != native_handler_map_.end();
208 }
209
210 void ModuleSystem::RegisterNativeHandler(const std::string& name, 200 void ModuleSystem::RegisterNativeHandler(const std::string& name,
211 scoped_ptr<NativeHandler> native_handler) { 201 scoped_ptr<NativeHandler> native_handler) {
212 native_handler_map_[name] = 202 native_handler_map_[name] =
213 linked_ptr<NativeHandler>(native_handler.release()); 203 linked_ptr<NativeHandler>(native_handler.release());
214 } 204 }
215 205
216 void ModuleSystem::OverrideNativeHandler(const std::string& name) { 206 void ModuleSystem::OverrideNativeHandler(const std::string& name) {
217 overridden_native_handlers_.insert(name); 207 overridden_native_handlers_.insert(name);
218 } 208 }
219 209
220 void ModuleSystem::RunString(const std::string& code, const std::string& name) { 210 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
221 v8::HandleScope handle_scope; 211 v8::HandleScope handle_scope;
222 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); 212 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
223 } 213 }
224 214
225 // static 215 // static
226 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
227 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
228 return LazyFieldGetterInner(property,
229 info,
230 &ModuleSystem::RequireNativeFromString);
231 }
232
233 // static
234 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( 216 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
235 v8::Local<v8::String> property, const v8::AccessorInfo& info) { 217 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
236 return LazyFieldGetterInner(property, info, &ModuleSystem::Require);
237 }
238
239 // static
240 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner(
241 v8::Local<v8::String> property,
242 const v8::AccessorInfo& info,
243 RequireFunction require_function) {
244 CHECK(!info.Data().IsEmpty()); 218 CHECK(!info.Data().IsEmpty());
245 CHECK(info.Data()->IsObject()); 219 CHECK(info.Data()->IsObject());
246 v8::HandleScope handle_scope; 220 v8::HandleScope handle_scope;
247 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); 221 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
248 // This context should be the same as v8_context_, but this method is static. 222 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
249 v8::Handle<v8::Context> context = parameters->CreationContext();
250 v8::Handle<v8::Object> global(context->Global());
251 v8::Handle<v8::Value> module_system_value = 223 v8::Handle<v8::Value> module_system_value =
252 global->GetHiddenValue(v8::String::New(kModuleSystem)); 224 global->GetHiddenValue(v8::String::New(kModuleSystem));
253 if (module_system_value.IsEmpty() || module_system_value->IsUndefined()) { 225 if (module_system_value.IsEmpty() || module_system_value->IsUndefined()) {
254 // ModuleSystem has been deleted. 226 // ModuleSystem has been deleted.
255 return v8::Undefined(); 227 return v8::Undefined();
256 } 228 }
257 ModuleSystem* module_system = static_cast<ModuleSystem*>( 229 ModuleSystem* module_system = static_cast<ModuleSystem*>(
258 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 230 v8::Handle<v8::External>::Cast(module_system_value)->Value());
259 231
260 std::string name = *v8::String::AsciiValue( 232 v8::Handle<v8::Object> module;
261 parameters->Get(v8::String::New(kModuleName))->ToString()); 233 {
262 234 NativesEnabledScope scope(module_system);
263 // Switch to our v8 context because we need functions created while running 235 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner(
264 // the require()d module to belong to our context, not the current one. 236 parameters->Get(v8::String::New(kModuleName))->ToString()));
265 v8::Context::Scope context_scope(context);
266 NativesEnabledScope natives_enabled_scope(module_system);
267
268 v8::TryCatch try_catch;
269 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
270 (module_system->*require_function)(name));
271 if (try_catch.HasCaught()) {
272 module_system->HandleException(try_catch);
273 return handle_scope.Close(v8::Handle<v8::Value>());
274 } 237 }
275
276 if (module.IsEmpty()) 238 if (module.IsEmpty())
277 return handle_scope.Close(v8::Handle<v8::Value>()); 239 return handle_scope.Close(v8::Handle<v8::Value>());
278 240
279 v8::Handle<v8::String> field = 241 v8::Handle<v8::String> field =
280 parameters->Get(v8::String::New(kModuleField))->ToString(); 242 parameters->Get(v8::String::New(kModuleField))->ToString();
281 243
282 // http://crbug.com/179741. 244 return handle_scope.Close(module->Get(field));
283 std::string field_name = *v8::String::AsciiValue(field);
284 char stack_debug[64];
285 base::debug::Alias(&stack_debug);
286 base::snprintf(stack_debug, arraysize(stack_debug),
287 "%s.%s", name.c_str(), field_name.c_str());
288
289 v8::Local<v8::Value> new_field = module->Get(field);
290 v8::Handle<v8::Object> object = info.This();
291 // Delete the getter and set this field to |new_field| so the same object is
292 // returned every time a certain API is accessed.
293 // CHECK is for http://crbug.com/179741.
294 CHECK(!new_field.IsEmpty()) << "Empty require " << name << "." << field_name;
295 if (!new_field->IsUndefined()) {
296 object->Delete(property);
297 object->Set(property, new_field);
298 }
299 return handle_scope.Close(new_field);
300 } 245 }
301 246
302 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 247 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
303 const std::string& field, 248 const std::string& field,
304 const std::string& module_name, 249 const std::string& module_name,
305 const std::string& module_field) { 250 const std::string& module_field) {
306 SetLazyField(object, field, module_name, module_field,
307 &ModuleSystem::LazyFieldGetter);
308 }
309
310 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
311 const std::string& field,
312 const std::string& module_name,
313 const std::string& module_field,
314 v8::AccessorGetter getter) {
315 v8::HandleScope handle_scope; 251 v8::HandleScope handle_scope;
316 v8::Handle<v8::Object> parameters = v8::Object::New(); 252 v8::Handle<v8::Object> parameters = v8::Object::New();
317 parameters->Set(v8::String::New(kModuleName), 253 parameters->Set(v8::String::New(kModuleName),
318 v8::String::New(module_name.c_str())); 254 v8::String::New(module_name.c_str()));
319 parameters->Set(v8::String::New(kModuleField), 255 parameters->Set(v8::String::New(kModuleField),
320 v8::String::New(module_field.c_str())); 256 v8::String::New(module_field.c_str()));
257
321 object->SetAccessor(v8::String::New(field.c_str()), 258 object->SetAccessor(v8::String::New(field.c_str()),
322 getter, 259 &ModuleSystem::LazyFieldGetter,
323 NULL, 260 NULL,
324 parameters); 261 parameters);
325 } 262 }
326 263
327 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
328 const std::string& field,
329 const std::string& module_name,
330 const std::string& module_field) {
331 SetLazyField(object, field, module_name, module_field,
332 &ModuleSystem::NativeLazyFieldGetter);
333 }
334
335 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 264 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
336 v8::Handle<v8::String> name) { 265 v8::Handle<v8::String> name) {
337 v8::HandleScope handle_scope; 266 v8::HandleScope handle_scope;
338 WebKit::WebScopedMicrotaskSuppression suppression; 267 WebKit::WebScopedMicrotaskSuppression suppression;
339 v8::Handle<v8::Value> result; 268 v8::Handle<v8::Value> result;
340 v8::TryCatch try_catch; 269 v8::TryCatch try_catch;
341 try_catch.SetCaptureMessage(true); 270 try_catch.SetCaptureMessage(true);
342 v8::Handle<v8::Script> script(v8::Script::New(code, name)); 271 v8::Handle<v8::Script> script(v8::Script::New(code, name));
343 if (try_catch.HasCaught()) { 272 if (try_catch.HasCaught()) {
344 HandleException(try_catch); 273 HandleException(try_catch);
345 return handle_scope.Close(result); 274 return handle_scope.Close(result);
346 } 275 }
347 276
348 result = script->Run(); 277 result = script->Run();
349 if (try_catch.HasCaught()) 278 if (try_catch.HasCaught())
350 HandleException(try_catch); 279 HandleException(try_catch);
351 280
352 return handle_scope.Close(result); 281 return handle_scope.Close(result);
353 } 282 }
354 283
355 v8::Handle<v8::Value> ModuleSystem::GetSource( 284 v8::Handle<v8::Value> ModuleSystem::GetSource(
356 v8::Handle<v8::String> source_name) { 285 v8::Handle<v8::String> source_name) {
357 v8::HandleScope handle_scope; 286 v8::HandleScope handle_scope;
358 std::string module_name = *v8::String::AsciiValue(source_name); 287 std::string module_name = *v8::String::AsciiValue(source_name);
359 if (!source_map_->Contains(module_name)) 288 if (!source_map_->Contains(module_name))
360 return v8::Undefined(); 289 return v8::Undefined();
361 return handle_scope.Close(source_map_->GetSource(module_name)); 290 return handle_scope.Close(source_map_->GetSource(module_name));
362 } 291 }
363 292
364 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { 293 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
365 CHECK_EQ(1, args.Length()); 294 CHECK_EQ(1, args.Length());
366 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
367 return RequireNativeFromString(native_name);
368 }
369
370 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
371 const std::string& native_name) {
372 if (natives_enabled_ == 0) 295 if (natives_enabled_ == 0)
373 return ThrowException("Natives disabled"); 296 return ThrowException("Natives disabled");
297 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
374 if (overridden_native_handlers_.count(native_name) > 0u) 298 if (overridden_native_handlers_.count(native_name) > 0u)
375 return RequireForJsInner(v8::String::New(native_name.c_str())); 299 return RequireForJs(args);
376 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 300 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
377 if (i == native_handler_map_.end()) 301 if (i == native_handler_map_.end())
378 return v8::Undefined(); 302 return v8::Undefined();
379 return i->second->NewInstance(); 303 return i->second->NewInstance();
380 } 304 }
381 305
382 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { 306 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
383 v8::HandleScope handle_scope; 307 v8::HandleScope handle_scope;
384 v8::Handle<v8::String> left = v8::String::New( 308 v8::Handle<v8::String> left = v8::String::New(
385 "(function(require, requireNative, exports) {'use strict';"); 309 "(function(require, requireNative, exports) {'use strict';");
386 v8::Handle<v8::String> right = v8::String::New("\n})"); 310 v8::Handle<v8::String> right = v8::String::New("\n})");
387 return handle_scope.Close( 311 return handle_scope.Close(
388 v8::String::Concat(left, v8::String::Concat(source, right))); 312 v8::String::Concat(left, v8::String::Concat(source, right)));
389 } 313 }
390 314
391 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { 315 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
392 return v8::ThrowException(v8::String::New(message.c_str())); 316 return v8::ThrowException(v8::String::New(message.c_str()));
393 } 317 }
394 318
395 } // extensions 319 } // extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/module_system.h ('k') | chrome/renderer/extensions/module_system_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698