blob: 0033587f03706332de35c3bd084b979ff3e927fb [file] [log] [blame]
[email protected]58e10452012-02-22 03:34:451// 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
[email protected]3c6babd2012-08-28 03:17:295#include "chrome/renderer/extensions/module_system.h"
[email protected]58e10452012-02-22 03:34:456
7#include "base/bind.h"
[email protected]68e63ea12013-06-05 05:00:548#include "base/debug/trace_event.h"
[email protected]a714e462013-01-26 06:33:109#include "base/stl_util.h"
[email protected]4f1633f2013-03-09 14:26:2410#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"
[email protected]d2663612013-03-17 09:25:5614#include "chrome/renderer/extensions/console.h"
[email protected]4f1633f2013-03-09 14:26:2415#include "content/public/renderer/render_view.h"
[email protected]68e63ea12013-06-05 05:00:5416#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
[email protected]11d032ba2012-04-09 20:39:5117#include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSuppression.h"
[email protected]58e10452012-02-22 03:34:4518
[email protected]95ee77da2013-03-19 21:11:1119namespace extensions {
20
[email protected]58e10452012-02-22 03:34:4521namespace {
22
[email protected]561ddce2012-03-22 01:18:5523const char* kModuleSystem = "module_system";
24const char* kModuleName = "module_name";
25const char* kModuleField = "module_field";
[email protected]bad9a5f2012-04-13 19:16:5426const char* kModulesField = "modules";
[email protected]cc0457712012-03-21 03:56:3827
[email protected]68e63ea12013-06-05 05:00:5428// Default exception handler which logs the exception.
29class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler {
30 public:
31 // Fatally dumps the debug info from |try_catch| to the console.
32 // Make sure this is never used for exceptions that originate in external
33 // code!
34 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
35 v8::HandleScope handle_scope;
36 std::string stack_trace = "<stack trace unavailable>";
37 if (!try_catch.StackTrace().IsEmpty()) {
38 v8::String::Utf8Value stack_value(try_catch.StackTrace());
39 if (*stack_value)
40 stack_trace.assign(*stack_value, stack_value.length());
41 else
42 stack_trace = "<could not convert stack trace to string>";
43 }
44 console::Fatal(v8::Context::GetCalling(),
45 CreateExceptionString(try_catch) + "{" + stack_trace + "}");
46 }
47};
48
49} // namespace
50
51std::string ModuleSystem::ExceptionHandler::CreateExceptionString(
52 const v8::TryCatch& try_catch) {
[email protected]95ee77da2013-03-19 21:11:1153 v8::Handle<v8::Message> message(try_catch.Message());
54 if (message.IsEmpty()) {
55 return "try_catch has no message";
56 }
[email protected]58e10452012-02-22 03:34:4557
[email protected]95ee77da2013-03-19 21:11:1158 std::string resource_name = "<unknown resource>";
59 if (!message->GetScriptResourceName().IsEmpty()) {
60 v8::String::Utf8Value resource_name_v8(
61 message->GetScriptResourceName()->ToString());
62 resource_name.assign(*resource_name_v8, resource_name_v8.length());
63 }
64
65 std::string error_message = "<no error message>";
66 if (!message->Get().IsEmpty()) {
67 v8::String::Utf8Value error_message_v8(message->Get());
68 error_message.assign(*error_message_v8, error_message_v8.length());
69 }
70
71 return base::StringPrintf("%s:%d: %s",
72 resource_name.c_str(),
73 message->GetLineNumber(),
74 error_message.c_str());
75}
76
[email protected]9a598442013-06-04 16:39:1277ModuleSystem::ModuleSystem(ChromeV8Context* context,
[email protected]bad9a5f2012-04-13 19:16:5478 SourceMap* source_map)
[email protected]4f1633f2013-03-09 14:26:2479 : ObjectBackedNativeHandler(context),
[email protected]68e63ea12013-06-05 05:00:5480 context_(context),
[email protected]bad9a5f2012-04-13 19:16:5481 source_map_(source_map),
[email protected]68e63ea12013-06-05 05:00:5482 natives_enabled_(0),
83 exception_handler_(new DefaultExceptionHandler()) {
[email protected]ecde1912012-03-16 06:25:3184 RouteFunction("require",
85 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
86 RouteFunction("requireNative",
[email protected]4f1633f2013-03-09 14:26:2487 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
[email protected]ecde1912012-03-16 06:25:3188
[email protected]9a598442013-06-04 16:39:1289 v8::Handle<v8::Object> global(context->v8_context()->Global());
[email protected]bad9a5f2012-04-13 19:16:5490 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
91 global->SetHiddenValue(v8::String::New(kModuleSystem),
92 v8::External::New(this));
[email protected]58e10452012-02-22 03:34:4593}
94
95ModuleSystem::~ModuleSystem() {
[email protected]4f1633f2013-03-09 14:26:2496 Invalidate();
97}
98
99void ModuleSystem::Invalidate() {
100 if (!is_valid())
101 return;
[email protected]079532242013-03-12 06:01:10102
103 // Clear the module system properties from the global context. It's polite,
104 // and we use this as a signal in lazy handlers that we no longer exist.
105 {
106 v8::HandleScope scope;
[email protected]9a598442013-06-04 16:39:12107 v8::Handle<v8::Object> global = context()->v8_context()->Global();
[email protected]079532242013-03-12 06:01:10108 global->DeleteHiddenValue(v8::String::New(kModulesField));
109 global->DeleteHiddenValue(v8::String::New(kModuleSystem));
110 }
111
112 // Invalidate all of the successfully required handlers we own.
[email protected]4f1633f2013-03-09 14:26:24113 for (NativeHandlerMap::iterator it = native_handler_map_.begin();
114 it != native_handler_map_.end(); ++it) {
115 it->second->Invalidate();
116 }
[email protected]079532242013-03-12 06:01:10117
[email protected]4f1633f2013-03-09 14:26:24118 ObjectBackedNativeHandler::Invalidate();
[email protected]58e10452012-02-22 03:34:45119}
120
[email protected]b6aad81c2012-03-27 08:45:15121ModuleSystem::NativesEnabledScope::NativesEnabledScope(
122 ModuleSystem* module_system)
123 : module_system_(module_system) {
124 module_system_->natives_enabled_++;
125}
126
127ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
128 module_system_->natives_enabled_--;
129 CHECK_GE(module_system_->natives_enabled_, 0);
130}
131
[email protected]144114942012-12-04 07:23:23132void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
[email protected]68e63ea12013-06-05 05:00:54133 exception_handler_->HandleUncaughtException(try_catch);
[email protected]1c6189f2012-05-18 06:45:52134}
135
[email protected]4f1633f2013-03-09 14:26:24136v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
[email protected]58e10452012-02-22 03:34:45137 v8::HandleScope handle_scope;
[email protected]4f1633f2013-03-09 14:26:24138 return handle_scope.Close(
139 RequireForJsInner(v8::String::New(module_name.c_str())));
[email protected]ecde1912012-03-16 06:25:31140}
141
142v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
143 v8::HandleScope handle_scope;
144 v8::Handle<v8::String> module_name = args[0]->ToString();
145 return handle_scope.Close(RequireForJsInner(module_name));
146}
147
148v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
149 v8::Handle<v8::String> module_name) {
150 v8::HandleScope handle_scope;
[email protected]9a598442013-06-04 16:39:12151 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11152
[email protected]9a598442013-06-04 16:39:12153 v8::Handle<v8::Object> global(context()->v8_context()->Global());
[email protected]079532242013-03-12 06:01:10154
155 // The module system might have been deleted. This can happen if a different
156 // context keeps a reference to us, but our frame is destroyed (e.g.
157 // background page keeps reference to chrome object in a closed popup).
158 v8::Handle<v8::Value> modules_value =
159 global->GetHiddenValue(v8::String::New(kModulesField));
[email protected]d2663612013-03-17 09:25:56160 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
[email protected]95ee77da2013-03-19 21:11:11161 console::Warn(v8::Context::GetCalling(), "Extension view no longer exists");
162 return v8::Undefined();
[email protected]d2663612013-03-17 09:25:56163 }
[email protected]079532242013-03-12 06:01:10164
165 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
[email protected]ecde1912012-03-16 06:25:31166 v8::Handle<v8::Value> exports(modules->Get(module_name));
167 if (!exports->IsUndefined())
168 return handle_scope.Close(exports);
[email protected]7c95c1a82012-04-17 02:11:33169
[email protected]d2663612013-03-17 09:25:56170 std::string module_name_str = *v8::String::AsciiValue(module_name);
171 v8::Handle<v8::Value> source(GetSource(module_name_str));
[email protected]95ee77da2013-03-19 21:11:11172 if (source.IsEmpty() || source->IsUndefined()) {
173 console::Error(v8::Context::GetCalling(),
174 "No source for require(" + module_name_str + ")");
175 return v8::Undefined();
176 }
[email protected]ecde1912012-03-16 06:25:31177 v8::Handle<v8::String> wrapped_source(WrapSource(
178 v8::Handle<v8::String>::Cast(source)));
[email protected]95ee77da2013-03-19 21:11:11179 // Modules are wrapped in (function(){...}) so they always return functions.
180 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name);
181 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
182 console::Error(v8::Context::GetCalling(),
183 "Bad source for require(" + module_name_str + ")");
184 return v8::Undefined();
185 }
186
187 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value);
[email protected]7c95c1a82012-04-17 02:11:33188
[email protected]ecde1912012-03-16 06:25:31189 exports = v8::Object::New();
190 v8::Handle<v8::Object> natives(NewInstance());
191 v8::Handle<v8::Value> args[] = {
192 natives->Get(v8::String::NewSymbol("require")),
193 natives->Get(v8::String::NewSymbol("requireNative")),
194 exports,
[email protected]58e10452012-02-22 03:34:45195 };
[email protected]11d032ba2012-04-09 20:39:51196 {
[email protected]452b36f2012-07-12 05:27:54197 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54198 try_catch.SetCaptureMessage(true);
[email protected]68e63ea12013-06-05 05:00:54199 context_->CallFunction(func, arraysize(args), args);
[email protected]452b36f2012-07-12 05:27:54200 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23201 HandleException(try_catch);
[email protected]452b36f2012-07-12 05:27:54202 return v8::Undefined();
203 }
[email protected]11d032ba2012-04-09 20:39:51204 }
[email protected]ecde1912012-03-16 06:25:31205 modules->Set(module_name, exports);
206 return handle_scope.Close(exports);
[email protected]58e10452012-02-22 03:34:45207}
208
[email protected]4f1633f2013-03-09 14:26:24209v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
210 const std::string& module_name,
211 const std::string& method_name) {
[email protected]68e63ea12013-06-05 05:00:54212 v8::HandleScope handle_scope;
213 v8::Handle<v8::Value> no_args;
214 return CallModuleMethod(module_name, method_name, 0, &no_args);
[email protected]a714e462013-01-26 06:33:10215}
216
217v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
218 const std::string& module_name,
219 const std::string& method_name,
220 std::vector<v8::Handle<v8::Value> >* args) {
[email protected]68e63ea12013-06-05 05:00:54221 return CallModuleMethod(
222 module_name, method_name, args->size(), vector_as_array(args));
223}
224
225v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
226 const std::string& module_name,
227 const std::string& method_name,
228 int argc,
229 v8::Handle<v8::Value> argv[]) {
230 TRACE_EVENT2("v8", "v8.callModuleMethod",
231 "module_name", module_name,
232 "method_name", method_name);
233
[email protected]2bcbf712012-09-05 06:58:49234 v8::HandleScope handle_scope;
[email protected]68e63ea12013-06-05 05:00:54235 v8::Context::Scope context_scope(context()->v8_context());
236
237 v8::Local<v8::Value> module;
238 {
239 NativesEnabledScope natives_enabled(this);
240 module = v8::Local<v8::Value>::New(
241 RequireForJsInner(v8::String::New(module_name.c_str())));
242 }
243
[email protected]95ee77da2013-03-19 21:11:11244 if (module.IsEmpty() || !module->IsObject()) {
245 console::Error(
246 v8::Context::GetCalling(),
247 "Failed to get module " + module_name + " to call " + method_name);
248 return handle_scope.Close(v8::Undefined());
249 }
250
[email protected]2bcbf712012-09-05 06:58:49251 v8::Local<v8::Value> value =
252 v8::Handle<v8::Object>::Cast(module)->Get(
253 v8::String::New(method_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11254 if (value.IsEmpty() || !value->IsFunction()) {
255 console::Error(v8::Context::GetCalling(),
256 module_name + "." + method_name + " is not a function");
257 return handle_scope.Close(v8::Undefined());
258 }
259
[email protected]68e63ea12013-06-05 05:00:54260 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
[email protected]a714e462013-01-26 06:33:10261 v8::Local<v8::Value> result;
[email protected]2bcbf712012-09-05 06:58:49262 {
[email protected]2bcbf712012-09-05 06:58:49263 v8::TryCatch try_catch;
264 try_catch.SetCaptureMessage(true);
[email protected]68e63ea12013-06-05 05:00:54265 result = context_->CallFunction(func, argc, argv);
[email protected]2bcbf712012-09-05 06:58:49266 if (try_catch.HasCaught())
[email protected]144114942012-12-04 07:23:23267 HandleException(try_catch);
[email protected]2bcbf712012-09-05 06:58:49268 }
[email protected]a714e462013-01-26 06:33:10269 return handle_scope.Close(result);
[email protected]2bcbf712012-09-05 06:58:49270}
271
[email protected]58e10452012-02-22 03:34:45272void ModuleSystem::RegisterNativeHandler(const std::string& name,
273 scoped_ptr<NativeHandler> native_handler) {
274 native_handler_map_[name] =
275 linked_ptr<NativeHandler>(native_handler.release());
276}
277
[email protected]95ee77da2013-03-19 21:11:11278void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) {
[email protected]11844fa2012-05-10 00:35:59279 overridden_native_handlers_.insert(name);
280}
281
[email protected]ecde1912012-03-16 06:25:31282void ModuleSystem::RunString(const std::string& code, const std::string& name) {
[email protected]9fb990b2012-03-13 02:09:41283 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31284 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
[email protected]58e10452012-02-22 03:34:45285}
286
[email protected]cc0457712012-03-21 03:56:38287// static
[email protected]4f1633f2013-03-09 14:26:24288v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
289 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
290 return LazyFieldGetterInner(property,
291 info,
292 &ModuleSystem::RequireNativeFromString);
293}
294
295// static
[email protected]b6aad81c2012-03-27 08:45:15296v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
[email protected]561ddce2012-03-22 01:18:55297 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
[email protected]4f1633f2013-03-09 14:26:24298 return LazyFieldGetterInner(property, info, &ModuleSystem::Require);
299}
300
301// static
302v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner(
303 v8::Local<v8::String> property,
304 const v8::AccessorInfo& info,
305 RequireFunction require_function) {
[email protected]561ddce2012-03-22 01:18:55306 CHECK(!info.Data().IsEmpty());
307 CHECK(info.Data()->IsObject());
[email protected]cc0457712012-03-21 03:56:38308 v8::HandleScope handle_scope;
[email protected]561ddce2012-03-22 01:18:55309 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
[email protected]9a598442013-06-04 16:39:12310 // This context should be the same as context()->v8_context().
[email protected]4f1633f2013-03-09 14:26:24311 v8::Handle<v8::Context> context = parameters->CreationContext();
312 v8::Handle<v8::Object> global(context->Global());
[email protected]561ddce2012-03-22 01:18:55313 v8::Handle<v8::Value> module_system_value =
[email protected]bad9a5f2012-04-13 19:16:54314 global->GetHiddenValue(v8::String::New(kModuleSystem));
[email protected]95ee77da2013-03-19 21:11:11315 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
[email protected]bad9a5f2012-04-13 19:16:54316 // ModuleSystem has been deleted.
[email protected]95ee77da2013-03-19 21:11:11317 // TODO(kalman): See comment in header file.
318 console::Warn(v8::Context::GetCalling(),
319 "Module system has been deleted, does extension view exist?");
[email protected]bad9a5f2012-04-13 19:16:54320 return v8::Undefined();
321 }
[email protected]95ee77da2013-03-19 21:11:11322
[email protected]561ddce2012-03-22 01:18:55323 ModuleSystem* module_system = static_cast<ModuleSystem*>(
324 v8::Handle<v8::External>::Cast(module_system_value)->Value());
[email protected]cc0457712012-03-21 03:56:38325
[email protected]4f1633f2013-03-09 14:26:24326 std::string name = *v8::String::AsciiValue(
327 parameters->Get(v8::String::New(kModuleName))->ToString());
328
329 // Switch to our v8 context because we need functions created while running
330 // the require()d module to belong to our context, not the current one.
331 v8::Context::Scope context_scope(context);
332 NativesEnabledScope natives_enabled_scope(module_system);
333
334 v8::TryCatch try_catch;
[email protected]95ee77da2013-03-19 21:11:11335 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
[email protected]4f1633f2013-03-09 14:26:24336 if (try_catch.HasCaught()) {
337 module_system->HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11338 return v8::Undefined();
339 }
340 if (module_value.IsEmpty() || !module_value->IsObject()) {
341 // require_function will have already logged this, we don't need to.
342 return v8::Undefined();
[email protected]b6aad81c2012-03-27 08:45:15343 }
[email protected]4f1633f2013-03-09 14:26:24344
[email protected]95ee77da2013-03-19 21:11:11345 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
[email protected]561ddce2012-03-22 01:18:55346 v8::Handle<v8::String> field =
347 parameters->Get(v8::String::New(kModuleField))->ToString();
348
[email protected]95ee77da2013-03-19 21:11:11349 if (!module->Has(field)) {
350 std::string field_str = *v8::String::AsciiValue(field);
351 console::Fatal(v8::Context::GetCalling(),
352 "Lazy require of " + name + "." + field_str + " did not " +
353 "set the " + field_str + " field");
354 return v8::Undefined();
355 }
[email protected]4f1633f2013-03-09 14:26:24356
357 v8::Local<v8::Value> new_field = module->Get(field);
[email protected]95ee77da2013-03-19 21:11:11358 if (try_catch.HasCaught()) {
359 module_system->HandleException(try_catch);
360 return v8::Undefined();
361 }
362
363 // Ok for it to be undefined, among other things it's how bindings signify
364 // that the extension doesn't have permission to use them.
365 CHECK(!new_field.IsEmpty());
366
[email protected]4f1633f2013-03-09 14:26:24367 // Delete the getter and set this field to |new_field| so the same object is
368 // returned every time a certain API is accessed.
[email protected]95ee77da2013-03-19 21:11:11369 v8::Handle<v8::Object> object = info.This();
370 object->Delete(property);
371 object->Set(property, new_field);
[email protected]4f1633f2013-03-09 14:26:24372 return handle_scope.Close(new_field);
[email protected]561ddce2012-03-22 01:18:55373}
374
375void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
376 const std::string& field,
377 const std::string& module_name,
378 const std::string& module_field) {
[email protected]4f1633f2013-03-09 14:26:24379 SetLazyField(object, field, module_name, module_field,
380 &ModuleSystem::LazyFieldGetter);
381}
382
383void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
384 const std::string& field,
385 const std::string& module_name,
386 const std::string& module_field,
387 v8::AccessorGetter getter) {
[email protected]561ddce2012-03-22 01:18:55388 v8::HandleScope handle_scope;
389 v8::Handle<v8::Object> parameters = v8::Object::New();
390 parameters->Set(v8::String::New(kModuleName),
391 v8::String::New(module_name.c_str()));
392 parameters->Set(v8::String::New(kModuleField),
393 v8::String::New(module_field.c_str()));
[email protected]561ddce2012-03-22 01:18:55394 object->SetAccessor(v8::String::New(field.c_str()),
[email protected]4f1633f2013-03-09 14:26:24395 getter,
[email protected]561ddce2012-03-22 01:18:55396 NULL,
397 parameters);
[email protected]cc0457712012-03-21 03:56:38398}
[email protected]ecde1912012-03-16 06:25:31399
[email protected]4f1633f2013-03-09 14:26:24400void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
401 const std::string& field,
402 const std::string& module_name,
403 const std::string& module_field) {
404 SetLazyField(object, field, module_name, module_field,
405 &ModuleSystem::NativeLazyFieldGetter);
406}
407
[email protected]ecde1912012-03-16 06:25:31408v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
409 v8::Handle<v8::String> name) {
[email protected]58e10452012-02-22 03:34:45410 v8::HandleScope handle_scope;
[email protected]68e63ea12013-06-05 05:00:54411 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11412
[email protected]11d032ba2012-04-09 20:39:51413 WebKit::WebScopedMicrotaskSuppression suppression;
[email protected]7c95c1a82012-04-17 02:11:33414 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54415 try_catch.SetCaptureMessage(true);
[email protected]7c95c1a82012-04-17 02:11:33416 v8::Handle<v8::Script> script(v8::Script::New(code, name));
417 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23418 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11419 return v8::Undefined();
[email protected]7c95c1a82012-04-17 02:11:33420 }
421
[email protected]95ee77da2013-03-19 21:11:11422 v8::Handle<v8::Value> result = script->Run();
423 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23424 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11425 return v8::Undefined();
426 }
[email protected]7c95c1a82012-04-17 02:11:33427
428 return handle_scope.Close(result);
[email protected]9e03ce22012-03-13 08:50:05429}
430
[email protected]d2663612013-03-17 09:25:56431v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
[email protected]9e03ce22012-03-13 08:50:05432 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31433 if (!source_map_->Contains(module_name))
[email protected]58e10452012-02-22 03:34:45434 return v8::Undefined();
[email protected]ecde1912012-03-16 06:25:31435 return handle_scope.Close(source_map_->GetSource(module_name));
[email protected]58e10452012-02-22 03:34:45436}
437
[email protected]4f1633f2013-03-09 14:26:24438v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) {
[email protected]58e10452012-02-22 03:34:45439 CHECK_EQ(1, args.Length());
[email protected]4f1633f2013-03-09 14:26:24440 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
441 return RequireNativeFromString(native_name);
442}
443
444v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
445 const std::string& native_name) {
[email protected]95ee77da2013-03-19 21:11:11446 if (natives_enabled_ == 0) {
447 // HACK: if in test throw exception so that we can test the natives-disabled
448 // logic; however, under normal circumstances, this is programmer error so
449 // we could crash.
450 if (exception_handler_)
451 return v8::ThrowException(v8::String::New("Natives disabled"));
452 console::Fatal(v8::Context::GetCalling(),
453 "Natives disabled for requireNative(" + native_name + ")");
454 return v8::Undefined();
455 }
456
[email protected]11844fa2012-05-10 00:35:59457 if (overridden_native_handlers_.count(native_name) > 0u)
[email protected]4f1633f2013-03-09 14:26:24458 return RequireForJsInner(v8::String::New(native_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11459
[email protected]58e10452012-02-22 03:34:45460 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
[email protected]95ee77da2013-03-19 21:11:11461 if (i == native_handler_map_.end()) {
462 console::Fatal(
463 v8::Context::GetCalling(),
464 "Couldn't find native for requireNative(" + native_name + ")");
[email protected]58e10452012-02-22 03:34:45465 return v8::Undefined();
[email protected]95ee77da2013-03-19 21:11:11466 }
[email protected]58e10452012-02-22 03:34:45467 return i->second->NewInstance();
468}
[email protected]ecde1912012-03-16 06:25:31469
470v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
471 v8::HandleScope handle_scope;
[email protected]b0c739f2013-02-20 09:21:40472 v8::Handle<v8::String> left = v8::String::New(
473 "(function(require, requireNative, exports) {'use strict';");
[email protected]ecde1912012-03-16 06:25:31474 v8::Handle<v8::String> right = v8::String::New("\n})");
475 return handle_scope.Close(
476 v8::String::Concat(left, v8::String::Concat(source, right)));
477}
478
[email protected]3c6babd2012-08-28 03:17:29479} // extensions