OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/renderer/extensions/module_system.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/debug/trace_event.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "chrome/common/extensions/features/feature_channel.h" | |
15 #include "chrome/renderer/extensions/chrome_v8_context.h" | |
16 #include "chrome/renderer/extensions/console.h" | |
17 #include "chrome/renderer/extensions/safe_builtins.h" | |
18 #include "content/public/renderer/render_view.h" | |
19 #include "extensions/common/extension_messages.h" | |
20 #include "third_party/WebKit/public/web/WebFrame.h" | |
21 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | |
22 | |
23 namespace extensions { | |
24 | |
25 namespace { | |
26 | |
27 const char* kModuleSystem = "module_system"; | |
28 const char* kModuleName = "module_name"; | |
29 const char* kModuleField = "module_field"; | |
30 const char* kModulesField = "modules"; | |
31 | |
32 // Logs a fatal error for the calling context, with some added metadata about | |
33 // the context: | |
34 // - Its type (blessed, unblessed, etc). | |
35 // - Whether it's valid. | |
36 // - The extension ID, if one exists. | |
37 // | |
38 // This will only actual be fatal in in dev/canary, since in too many cases | |
39 // we're at the mercy of the extension or web page's environment. They can mess | |
40 // up our JS in unexpected ways. Hopefully dev/canary channel will pick up such | |
41 // problems, but given the wider variety on stable/beta it's impossible to know. | |
42 void Fatal(ChromeV8Context* context, const std::string& message) { | |
43 // Prepend some context metadata. | |
44 std::string full_message = "("; | |
45 if (!context->is_valid()) | |
46 full_message += "Invalid "; | |
47 full_message += context->GetContextTypeDescription(); | |
48 full_message += " context"; | |
49 if (context->extension()) { | |
50 full_message += " for "; | |
51 full_message += context->extension()->id(); | |
52 } | |
53 full_message += ") "; | |
54 full_message += message; | |
55 | |
56 // <= dev means dev, canary, and trunk. | |
57 if (GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV) | |
58 console::Fatal(context->isolate()->GetCallingContext(), full_message); | |
59 else | |
60 console::Error(context->isolate()->GetCallingContext(), full_message); | |
61 } | |
62 | |
63 void Warn(v8::Isolate* isolate, const std::string& message) { | |
64 console::Warn(isolate->GetCallingContext(), message); | |
65 } | |
66 | |
67 // Default exception handler which logs the exception. | |
68 class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { | |
69 public: | |
70 explicit DefaultExceptionHandler(ChromeV8Context* context) | |
71 : context_(context) {} | |
72 | |
73 // Fatally dumps the debug info from |try_catch| to the console. | |
74 // Make sure this is never used for exceptions that originate in external | |
75 // code! | |
76 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { | |
77 v8::HandleScope handle_scope(context_->isolate()); | |
78 std::string stack_trace = "<stack trace unavailable>"; | |
79 if (!try_catch.StackTrace().IsEmpty()) { | |
80 v8::String::Utf8Value stack_value(try_catch.StackTrace()); | |
81 if (*stack_value) | |
82 stack_trace.assign(*stack_value, stack_value.length()); | |
83 else | |
84 stack_trace = "<could not convert stack trace to string>"; | |
85 } | |
86 Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); | |
87 } | |
88 | |
89 private: | |
90 ChromeV8Context* context_; | |
91 }; | |
92 | |
93 } // namespace | |
94 | |
95 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( | |
96 const v8::TryCatch& try_catch) { | |
97 v8::Handle<v8::Message> message(try_catch.Message()); | |
98 if (message.IsEmpty()) { | |
99 return "try_catch has no message"; | |
100 } | |
101 | |
102 std::string resource_name = "<unknown resource>"; | |
103 if (!message->GetScriptResourceName().IsEmpty()) { | |
104 v8::String::Utf8Value resource_name_v8( | |
105 message->GetScriptResourceName()->ToString()); | |
106 resource_name.assign(*resource_name_v8, resource_name_v8.length()); | |
107 } | |
108 | |
109 std::string error_message = "<no error message>"; | |
110 if (!message->Get().IsEmpty()) { | |
111 v8::String::Utf8Value error_message_v8(message->Get()); | |
112 error_message.assign(*error_message_v8, error_message_v8.length()); | |
113 } | |
114 | |
115 return base::StringPrintf("%s:%d: %s", | |
116 resource_name.c_str(), | |
117 message->GetLineNumber(), | |
118 error_message.c_str()); | |
119 } | |
120 | |
121 ModuleSystem::ModuleSystem(ChromeV8Context* context, SourceMap* source_map) | |
122 : ObjectBackedNativeHandler(context), | |
123 context_(context), | |
124 source_map_(source_map), | |
125 natives_enabled_(0), | |
126 exception_handler_(new DefaultExceptionHandler(context)) { | |
127 RouteFunction("require", | |
128 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); | |
129 RouteFunction("requireNative", | |
130 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); | |
131 RouteFunction("privates", | |
132 base::Bind(&ModuleSystem::Private, base::Unretained(this))); | |
133 | |
134 v8::Handle<v8::Object> global(context->v8_context()->Global()); | |
135 v8::Isolate* isolate = context->isolate(); | |
136 global->SetHiddenValue( | |
137 v8::String::NewFromUtf8(isolate, kModulesField), | |
138 v8::Object::New(isolate)); | |
139 global->SetHiddenValue( | |
140 v8::String::NewFromUtf8(isolate, kModuleSystem), | |
141 v8::External::New(isolate, this)); | |
142 } | |
143 | |
144 ModuleSystem::~ModuleSystem() { | |
145 Invalidate(); | |
146 } | |
147 | |
148 void ModuleSystem::Invalidate() { | |
149 if (!is_valid()) | |
150 return; | |
151 | |
152 // Clear the module system properties from the global context. It's polite, | |
153 // and we use this as a signal in lazy handlers that we no longer exist. | |
154 { | |
155 v8::HandleScope scope(GetIsolate()); | |
156 v8::Handle<v8::Object> global = context()->v8_context()->Global(); | |
157 global->DeleteHiddenValue( | |
158 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); | |
159 global->DeleteHiddenValue( | |
160 v8::String::NewFromUtf8(GetIsolate(), kModuleSystem)); | |
161 } | |
162 | |
163 // Invalidate all of the successfully required handlers we own. | |
164 for (NativeHandlerMap::iterator it = native_handler_map_.begin(); | |
165 it != native_handler_map_.end(); ++it) { | |
166 it->second->Invalidate(); | |
167 } | |
168 | |
169 ObjectBackedNativeHandler::Invalidate(); | |
170 } | |
171 | |
172 ModuleSystem::NativesEnabledScope::NativesEnabledScope( | |
173 ModuleSystem* module_system) | |
174 : module_system_(module_system) { | |
175 module_system_->natives_enabled_++; | |
176 } | |
177 | |
178 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { | |
179 module_system_->natives_enabled_--; | |
180 CHECK_GE(module_system_->natives_enabled_, 0); | |
181 } | |
182 | |
183 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { | |
184 exception_handler_->HandleUncaughtException(try_catch); | |
185 } | |
186 | |
187 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { | |
188 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
189 return handle_scope.Escape(RequireForJsInner( | |
190 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); | |
191 } | |
192 | |
193 void ModuleSystem::RequireForJs( | |
194 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
195 v8::Handle<v8::String> module_name = args[0]->ToString(); | |
196 args.GetReturnValue().Set(RequireForJsInner(module_name)); | |
197 } | |
198 | |
199 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( | |
200 v8::Handle<v8::String> module_name) { | |
201 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
202 v8::Context::Scope context_scope(context()->v8_context()); | |
203 | |
204 v8::Handle<v8::Object> global(context()->v8_context()->Global()); | |
205 | |
206 // The module system might have been deleted. This can happen if a different | |
207 // context keeps a reference to us, but our frame is destroyed (e.g. | |
208 // background page keeps reference to chrome object in a closed popup). | |
209 v8::Handle<v8::Value> modules_value = global->GetHiddenValue( | |
210 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); | |
211 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { | |
212 Warn(GetIsolate(), "Extension view no longer exists"); | |
213 return v8::Undefined(GetIsolate()); | |
214 } | |
215 | |
216 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); | |
217 v8::Local<v8::Value> exports(modules->Get(module_name)); | |
218 if (!exports->IsUndefined()) | |
219 return handle_scope.Escape(exports); | |
220 | |
221 std::string module_name_str = *v8::String::Utf8Value(module_name); | |
222 v8::Handle<v8::Value> source(GetSource(module_name_str)); | |
223 if (source.IsEmpty() || source->IsUndefined()) { | |
224 Fatal(context_, "No source for require(" + module_name_str + ")"); | |
225 return v8::Undefined(GetIsolate()); | |
226 } | |
227 v8::Handle<v8::String> wrapped_source(WrapSource( | |
228 v8::Handle<v8::String>::Cast(source))); | |
229 // Modules are wrapped in (function(){...}) so they always return functions. | |
230 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); | |
231 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | |
232 Fatal(context_, "Bad source for require(" + module_name_str + ")"); | |
233 return v8::Undefined(GetIsolate()); | |
234 } | |
235 | |
236 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); | |
237 | |
238 exports = v8::Object::New(GetIsolate()); | |
239 v8::Handle<v8::Object> natives(NewInstance()); | |
240 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues | |
241 | |
242 // These must match the argument order in WrapSource. | |
243 v8::Handle<v8::Value> args[] = { | |
244 // CommonJS. | |
245 natives->Get(v8::String::NewFromUtf8( | |
246 GetIsolate(), "require", v8::String::kInternalizedString)), | |
247 natives->Get(v8::String::NewFromUtf8( | |
248 GetIsolate(), "requireNative", v8::String::kInternalizedString)), | |
249 exports, | |
250 // Libraries that we magically expose to every module. | |
251 console::AsV8Object(), | |
252 natives->Get(v8::String::NewFromUtf8( | |
253 GetIsolate(), "privates", v8::String::kInternalizedString)), | |
254 // Each safe builtin. Keep in order with the arguments in WrapSource. | |
255 context_->safe_builtins()->GetArray(), | |
256 context_->safe_builtins()->GetFunction(), | |
257 context_->safe_builtins()->GetJSON(), | |
258 context_->safe_builtins()->GetObjekt(), | |
259 context_->safe_builtins()->GetRegExp(), | |
260 context_->safe_builtins()->GetString(), }; | |
261 { | |
262 v8::TryCatch try_catch; | |
263 try_catch.SetCaptureMessage(true); | |
264 context_->CallFunction(func, arraysize(args), args); | |
265 if (try_catch.HasCaught()) { | |
266 HandleException(try_catch); | |
267 return v8::Undefined(GetIsolate()); | |
268 } | |
269 } | |
270 modules->Set(module_name, exports); | |
271 return handle_scope.Escape(exports); | |
272 } | |
273 | |
274 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | |
275 const std::string& module_name, | |
276 const std::string& method_name) { | |
277 v8::HandleScope handle_scope(GetIsolate()); | |
278 v8::Handle<v8::Value> no_args; | |
279 return CallModuleMethod(module_name, method_name, 0, &no_args); | |
280 } | |
281 | |
282 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | |
283 const std::string& module_name, | |
284 const std::string& method_name, | |
285 std::vector<v8::Handle<v8::Value> >* args) { | |
286 return CallModuleMethod( | |
287 module_name, method_name, args->size(), vector_as_array(args)); | |
288 } | |
289 | |
290 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | |
291 const std::string& module_name, | |
292 const std::string& method_name, | |
293 int argc, | |
294 v8::Handle<v8::Value> argv[]) { | |
295 TRACE_EVENT2("v8", "v8.callModuleMethod", | |
296 "module_name", module_name, | |
297 "method_name", method_name); | |
298 | |
299 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
300 v8::Context::Scope context_scope(context()->v8_context()); | |
301 | |
302 v8::Local<v8::Value> module; | |
303 { | |
304 NativesEnabledScope natives_enabled(this); | |
305 module = RequireForJsInner( | |
306 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | |
307 } | |
308 | |
309 if (module.IsEmpty() || !module->IsObject()) { | |
310 Fatal(context_, | |
311 "Failed to get module " + module_name + " to call " + method_name); | |
312 return handle_scope.Escape( | |
313 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | |
314 } | |
315 | |
316 v8::Local<v8::Value> value = | |
317 v8::Handle<v8::Object>::Cast(module)->Get( | |
318 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); | |
319 if (value.IsEmpty() || !value->IsFunction()) { | |
320 Fatal(context_, module_name + "." + method_name + " is not a function"); | |
321 return handle_scope.Escape( | |
322 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); | |
323 } | |
324 | |
325 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); | |
326 v8::Local<v8::Value> result; | |
327 { | |
328 v8::TryCatch try_catch; | |
329 try_catch.SetCaptureMessage(true); | |
330 result = context_->CallFunction(func, argc, argv); | |
331 if (try_catch.HasCaught()) | |
332 HandleException(try_catch); | |
333 } | |
334 return handle_scope.Escape(result); | |
335 } | |
336 | |
337 void ModuleSystem::RegisterNativeHandler(const std::string& name, | |
338 scoped_ptr<NativeHandler> native_handler) { | |
339 native_handler_map_[name] = | |
340 linked_ptr<NativeHandler>(native_handler.release()); | |
341 } | |
342 | |
343 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { | |
344 overridden_native_handlers_.insert(name); | |
345 } | |
346 | |
347 void ModuleSystem::RunString(const std::string& code, const std::string& name) { | |
348 v8::HandleScope handle_scope(GetIsolate()); | |
349 RunString(v8::String::NewFromUtf8(GetIsolate(), code.c_str()), | |
350 v8::String::NewFromUtf8(GetIsolate(), name.c_str())); | |
351 } | |
352 | |
353 // static | |
354 void ModuleSystem::NativeLazyFieldGetter( | |
355 v8::Local<v8::String> property, | |
356 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
357 LazyFieldGetterInner(property, | |
358 info, | |
359 &ModuleSystem::RequireNativeFromString); | |
360 } | |
361 | |
362 // static | |
363 void ModuleSystem::LazyFieldGetter( | |
364 v8::Local<v8::String> property, | |
365 const v8::PropertyCallbackInfo<v8::Value>& info) { | |
366 LazyFieldGetterInner(property, info, &ModuleSystem::Require); | |
367 } | |
368 | |
369 // static | |
370 void ModuleSystem::LazyFieldGetterInner( | |
371 v8::Local<v8::String> property, | |
372 const v8::PropertyCallbackInfo<v8::Value>& info, | |
373 RequireFunction require_function) { | |
374 CHECK(!info.Data().IsEmpty()); | |
375 CHECK(info.Data()->IsObject()); | |
376 v8::HandleScope handle_scope(info.GetIsolate()); | |
377 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | |
378 // This context should be the same as context()->v8_context(). | |
379 v8::Handle<v8::Context> context = parameters->CreationContext(); | |
380 v8::Handle<v8::Object> global(context->Global()); | |
381 v8::Handle<v8::Value> module_system_value = global->GetHiddenValue( | |
382 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); | |
383 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { | |
384 // ModuleSystem has been deleted. | |
385 // TODO(kalman): See comment in header file. | |
386 Warn(info.GetIsolate(), | |
387 "Module system has been deleted, does extension view exist?"); | |
388 return; | |
389 } | |
390 | |
391 ModuleSystem* module_system = static_cast<ModuleSystem*>( | |
392 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | |
393 | |
394 std::string name = *v8::String::Utf8Value(parameters->Get( | |
395 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))->ToString()); | |
396 | |
397 // Switch to our v8 context because we need functions created while running | |
398 // the require()d module to belong to our context, not the current one. | |
399 v8::Context::Scope context_scope(context); | |
400 NativesEnabledScope natives_enabled_scope(module_system); | |
401 | |
402 v8::TryCatch try_catch; | |
403 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); | |
404 if (try_catch.HasCaught()) { | |
405 module_system->HandleException(try_catch); | |
406 return; | |
407 } | |
408 if (module_value.IsEmpty() || !module_value->IsObject()) { | |
409 // require_function will have already logged this, we don't need to. | |
410 return; | |
411 } | |
412 | |
413 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); | |
414 v8::Handle<v8::String> field = | |
415 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) | |
416 ->ToString(); | |
417 | |
418 if (!module->Has(field)) { | |
419 std::string field_str = *v8::String::Utf8Value(field); | |
420 Fatal(module_system->context_, | |
421 "Lazy require of " + name + "." + field_str + " did not set the " + | |
422 field_str + " field"); | |
423 return; | |
424 } | |
425 | |
426 v8::Local<v8::Value> new_field = module->Get(field); | |
427 if (try_catch.HasCaught()) { | |
428 module_system->HandleException(try_catch); | |
429 return; | |
430 } | |
431 | |
432 // Ok for it to be undefined, among other things it's how bindings signify | |
433 // that the extension doesn't have permission to use them. | |
434 CHECK(!new_field.IsEmpty()); | |
435 | |
436 // Delete the getter and set this field to |new_field| so the same object is | |
437 // returned every time a certain API is accessed. | |
438 v8::Handle<v8::Object> object = info.This(); | |
439 object->Delete(property); | |
440 object->Set(property, new_field); | |
441 info.GetReturnValue().Set(new_field); | |
442 } | |
443 | |
444 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | |
445 const std::string& field, | |
446 const std::string& module_name, | |
447 const std::string& module_field) { | |
448 SetLazyField(object, field, module_name, module_field, | |
449 &ModuleSystem::LazyFieldGetter); | |
450 } | |
451 | |
452 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | |
453 const std::string& field, | |
454 const std::string& module_name, | |
455 const std::string& module_field, | |
456 v8::AccessorGetterCallback getter) { | |
457 v8::HandleScope handle_scope(GetIsolate()); | |
458 v8::Handle<v8::Object> parameters = v8::Object::New(GetIsolate()); | |
459 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName), | |
460 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | |
461 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField), | |
462 v8::String::NewFromUtf8(GetIsolate(), module_field.c_str())); | |
463 object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()), | |
464 getter, | |
465 NULL, | |
466 parameters); | |
467 } | |
468 | |
469 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, | |
470 const std::string& field, | |
471 const std::string& module_name, | |
472 const std::string& module_field) { | |
473 SetLazyField(object, field, module_name, module_field, | |
474 &ModuleSystem::NativeLazyFieldGetter); | |
475 } | |
476 | |
477 | |
478 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | |
479 v8::Handle<v8::String> name) { | |
480 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
481 v8::Context::Scope context_scope(context()->v8_context()); | |
482 | |
483 // Prepend extensions:: to |name| so that internal code can be differentiated | |
484 // from external code in stack traces. This has no effect on behaviour. | |
485 std::string internal_name = base::StringPrintf("extensions::%s", | |
486 *v8::String::Utf8Value(name)); | |
487 | |
488 blink::WebScopedMicrotaskSuppression suppression; | |
489 v8::TryCatch try_catch; | |
490 try_catch.SetCaptureMessage(true); | |
491 v8::Handle<v8::Script> script( | |
492 v8::Script::Compile(code, | |
493 v8::String::NewFromUtf8(GetIsolate(), | |
494 internal_name.c_str(), | |
495 v8::String::kNormalString, | |
496 internal_name.size()))); | |
497 if (try_catch.HasCaught()) { | |
498 HandleException(try_catch); | |
499 return v8::Undefined(GetIsolate()); | |
500 } | |
501 | |
502 v8::Local<v8::Value> result = script->Run(); | |
503 if (try_catch.HasCaught()) { | |
504 HandleException(try_catch); | |
505 return v8::Undefined(GetIsolate()); | |
506 } | |
507 | |
508 return handle_scope.Escape(result); | |
509 } | |
510 | |
511 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { | |
512 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
513 if (!source_map_->Contains(module_name)) | |
514 return v8::Undefined(GetIsolate()); | |
515 return handle_scope.Escape( | |
516 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); | |
517 } | |
518 | |
519 void ModuleSystem::RequireNative( | |
520 const v8::FunctionCallbackInfo<v8::Value>& args) { | |
521 CHECK_EQ(1, args.Length()); | |
522 std::string native_name = *v8::String::Utf8Value(args[0]->ToString()); | |
523 args.GetReturnValue().Set(RequireNativeFromString(native_name)); | |
524 } | |
525 | |
526 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( | |
527 const std::string& native_name) { | |
528 if (natives_enabled_ == 0) { | |
529 // HACK: if in test throw exception so that we can test the natives-disabled | |
530 // logic; however, under normal circumstances, this is programmer error so | |
531 // we could crash. | |
532 if (exception_handler_) { | |
533 return GetIsolate()->ThrowException( | |
534 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); | |
535 } | |
536 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); | |
537 return v8::Undefined(GetIsolate()); | |
538 } | |
539 | |
540 if (overridden_native_handlers_.count(native_name) > 0u) { | |
541 return RequireForJsInner( | |
542 v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); | |
543 } | |
544 | |
545 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | |
546 if (i == native_handler_map_.end()) { | |
547 Fatal(context_, | |
548 "Couldn't find native for requireNative(" + native_name + ")"); | |
549 return v8::Undefined(GetIsolate()); | |
550 } | |
551 return i->second->NewInstance(); | |
552 } | |
553 | |
554 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | |
555 v8::EscapableHandleScope handle_scope(GetIsolate()); | |
556 // Keep in order with the arguments in RequireForJsInner. | |
557 v8::Handle<v8::String> left = v8::String::NewFromUtf8( | |
558 GetIsolate(), | |
559 "(function(require, requireNative, exports, " | |
560 "console, privates," | |
561 "$Array, $Function, $JSON, $Object, $RegExp, $String) {" | |
562 "'use strict';"); | |
563 v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); | |
564 return handle_scope.Escape(v8::Local<v8::String>( | |
565 v8::String::Concat(left, v8::String::Concat(source, right)))); | |
566 } | |
567 | |
568 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
569 CHECK_EQ(1, args.Length()); | |
570 CHECK(args[0]->IsObject()); | |
571 v8::Local<v8::Object> obj = args[0].As<v8::Object>(); | |
572 v8::Local<v8::String> privates_key = | |
573 v8::String::NewFromUtf8(GetIsolate(), "privates"); | |
574 v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key); | |
575 if (privates.IsEmpty()) { | |
576 privates = v8::Object::New(args.GetIsolate()); | |
577 obj->SetHiddenValue(privates_key, privates); | |
578 } | |
579 args.GetReturnValue().Set(privates); | |
580 } | |
581 | |
582 } // namespace extensions | |
OLD | NEW |