blob: 85050a7d6eceeae9be91aad8413bb9023445d07b [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]a714e462013-01-26 06:33:108#include "base/stl_util.h"
[email protected]4f1633f2013-03-09 14:26:249#include "base/string_util.h"
10#include "base/stringprintf.h"
11#include "chrome/common/extensions/extension_messages.h"
12#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]d2663612013-03-17 09:25:5613#include "chrome/renderer/extensions/console.h"
[email protected]4f1633f2013-03-09 14:26:2414#include "content/public/renderer/render_view.h"
[email protected]11d032ba2012-04-09 20:39:5115#include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSuppression.h"
[email protected]58e10452012-02-22 03:34:4516
[email protected]95ee77da2013-03-19 21:11:1117namespace extensions {
18
[email protected]58e10452012-02-22 03:34:4519namespace {
20
[email protected]561ddce2012-03-22 01:18:5521const char* kModuleSystem = "module_system";
22const char* kModuleName = "module_name";
23const char* kModuleField = "module_field";
[email protected]bad9a5f2012-04-13 19:16:5424const char* kModulesField = "modules";
[email protected]cc0457712012-03-21 03:56:3825
[email protected]95ee77da2013-03-19 21:11:1126// Formats |try_catch| as a nice string.
27std::string CreateExceptionString(const v8::TryCatch& try_catch) {
28 v8::Handle<v8::Message> message(try_catch.Message());
29 if (message.IsEmpty()) {
30 return "try_catch has no message";
31 }
[email protected]58e10452012-02-22 03:34:4532
[email protected]95ee77da2013-03-19 21:11:1133 std::string resource_name = "<unknown resource>";
34 if (!message->GetScriptResourceName().IsEmpty()) {
35 v8::String::Utf8Value resource_name_v8(
36 message->GetScriptResourceName()->ToString());
37 resource_name.assign(*resource_name_v8, resource_name_v8.length());
38 }
39
40 std::string error_message = "<no error message>";
41 if (!message->Get().IsEmpty()) {
42 v8::String::Utf8Value error_message_v8(message->Get());
43 error_message.assign(*error_message_v8, error_message_v8.length());
44 }
45
46 return base::StringPrintf("%s:%d: %s",
47 resource_name.c_str(),
48 message->GetLineNumber(),
49 error_message.c_str());
50}
51
52// Fatally dumps the debug info from |try_catch| to the console.
53// Don't use this for logging exceptions that might originate in external code!
54void DumpException(const v8::TryCatch& try_catch) {
55 v8::HandleScope handle_scope;
56
57 std::string stack_trace = "<stack trace unavailable>";
58 if (!try_catch.StackTrace().IsEmpty()) {
59 v8::String::Utf8Value stack_value(try_catch.StackTrace());
60 if (*stack_value)
61 stack_trace.assign(*stack_value, stack_value.length());
62 else
63 stack_trace = "<could not convert stack trace to string>";
64 }
65
66 console::Fatal(v8::Context::GetCalling(),
67 CreateExceptionString(try_catch) + "{" + stack_trace + "}");
68}
69
70} // namespace
[email protected]3c6babd2012-08-28 03:17:2971
[email protected]bad9a5f2012-04-13 19:16:5472ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context,
73 SourceMap* source_map)
[email protected]4f1633f2013-03-09 14:26:2474 : ObjectBackedNativeHandler(context),
[email protected]bad9a5f2012-04-13 19:16:5475 source_map_(source_map),
[email protected]e3e778722013-03-03 23:28:0376 natives_enabled_(0) {
[email protected]ecde1912012-03-16 06:25:3177 RouteFunction("require",
78 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
79 RouteFunction("requireNative",
[email protected]4f1633f2013-03-09 14:26:2480 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
[email protected]ecde1912012-03-16 06:25:3181
[email protected]4f1633f2013-03-09 14:26:2482 v8::Handle<v8::Object> global(context->Global());
[email protected]bad9a5f2012-04-13 19:16:5483 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
84 global->SetHiddenValue(v8::String::New(kModuleSystem),
85 v8::External::New(this));
[email protected]58e10452012-02-22 03:34:4586}
87
88ModuleSystem::~ModuleSystem() {
[email protected]4f1633f2013-03-09 14:26:2489 Invalidate();
90}
91
92void ModuleSystem::Invalidate() {
93 if (!is_valid())
94 return;
[email protected]079532242013-03-12 06:01:1095
96 // Clear the module system properties from the global context. It's polite,
97 // and we use this as a signal in lazy handlers that we no longer exist.
98 {
99 v8::HandleScope scope;
100 v8::Handle<v8::Object> global = v8_context()->Global();
101 global->DeleteHiddenValue(v8::String::New(kModulesField));
102 global->DeleteHiddenValue(v8::String::New(kModuleSystem));
103 }
104
105 // Invalidate all of the successfully required handlers we own.
[email protected]4f1633f2013-03-09 14:26:24106 for (NativeHandlerMap::iterator it = native_handler_map_.begin();
107 it != native_handler_map_.end(); ++it) {
108 it->second->Invalidate();
109 }
[email protected]079532242013-03-12 06:01:10110
[email protected]4f1633f2013-03-09 14:26:24111 ObjectBackedNativeHandler::Invalidate();
[email protected]58e10452012-02-22 03:34:45112}
113
[email protected]b6aad81c2012-03-27 08:45:15114ModuleSystem::NativesEnabledScope::NativesEnabledScope(
115 ModuleSystem* module_system)
116 : module_system_(module_system) {
117 module_system_->natives_enabled_++;
118}
119
120ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
121 module_system_->natives_enabled_--;
122 CHECK_GE(module_system_->natives_enabled_, 0);
123}
124
[email protected]144114942012-12-04 07:23:23125void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
[email protected]95ee77da2013-03-19 21:11:11126 if (exception_handler_)
[email protected]144114942012-12-04 07:23:23127 exception_handler_->HandleUncaughtException();
[email protected]95ee77da2013-03-19 21:11:11128 else
129 DumpException(try_catch);
[email protected]1c6189f2012-05-18 06:45:52130}
131
[email protected]4f1633f2013-03-09 14:26:24132v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
[email protected]58e10452012-02-22 03:34:45133 v8::HandleScope handle_scope;
[email protected]4f1633f2013-03-09 14:26:24134 return handle_scope.Close(
135 RequireForJsInner(v8::String::New(module_name.c_str())));
[email protected]ecde1912012-03-16 06:25:31136}
137
138v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
139 v8::HandleScope handle_scope;
140 v8::Handle<v8::String> module_name = args[0]->ToString();
141 return handle_scope.Close(RequireForJsInner(module_name));
142}
143
144v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
145 v8::Handle<v8::String> module_name) {
146 v8::HandleScope handle_scope;
[email protected]95ee77da2013-03-19 21:11:11147 v8::Context::Scope context_scope(v8_context());
148
[email protected]4f1633f2013-03-09 14:26:24149 v8::Handle<v8::Object> global(v8_context()->Global());
[email protected]079532242013-03-12 06:01:10150
151 // The module system might have been deleted. This can happen if a different
152 // context keeps a reference to us, but our frame is destroyed (e.g.
153 // background page keeps reference to chrome object in a closed popup).
154 v8::Handle<v8::Value> modules_value =
155 global->GetHiddenValue(v8::String::New(kModulesField));
[email protected]d2663612013-03-17 09:25:56156 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
[email protected]95ee77da2013-03-19 21:11:11157 console::Warn(v8::Context::GetCalling(), "Extension view no longer exists");
158 return v8::Undefined();
[email protected]d2663612013-03-17 09:25:56159 }
[email protected]079532242013-03-12 06:01:10160
161 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
[email protected]ecde1912012-03-16 06:25:31162 v8::Handle<v8::Value> exports(modules->Get(module_name));
163 if (!exports->IsUndefined())
164 return handle_scope.Close(exports);
[email protected]7c95c1a82012-04-17 02:11:33165
[email protected]d2663612013-03-17 09:25:56166 std::string module_name_str = *v8::String::AsciiValue(module_name);
167 v8::Handle<v8::Value> source(GetSource(module_name_str));
[email protected]95ee77da2013-03-19 21:11:11168 if (source.IsEmpty() || source->IsUndefined()) {
169 console::Error(v8::Context::GetCalling(),
170 "No source for require(" + module_name_str + ")");
171 return v8::Undefined();
172 }
[email protected]ecde1912012-03-16 06:25:31173 v8::Handle<v8::String> wrapped_source(WrapSource(
174 v8::Handle<v8::String>::Cast(source)));
[email protected]95ee77da2013-03-19 21:11:11175 // Modules are wrapped in (function(){...}) so they always return functions.
176 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name);
177 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
178 console::Error(v8::Context::GetCalling(),
179 "Bad source for require(" + module_name_str + ")");
180 return v8::Undefined();
181 }
182
183 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value);
[email protected]7c95c1a82012-04-17 02:11:33184
[email protected]ecde1912012-03-16 06:25:31185 exports = v8::Object::New();
186 v8::Handle<v8::Object> natives(NewInstance());
187 v8::Handle<v8::Value> args[] = {
188 natives->Get(v8::String::NewSymbol("require")),
189 natives->Get(v8::String::NewSymbol("requireNative")),
190 exports,
[email protected]58e10452012-02-22 03:34:45191 };
[email protected]11d032ba2012-04-09 20:39:51192 {
193 WebKit::WebScopedMicrotaskSuppression suppression;
[email protected]452b36f2012-07-12 05:27:54194 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54195 try_catch.SetCaptureMessage(true);
[email protected]11d032ba2012-04-09 20:39:51196 func->Call(global, 3, args);
[email protected]452b36f2012-07-12 05:27:54197 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23198 HandleException(try_catch);
[email protected]452b36f2012-07-12 05:27:54199 return v8::Undefined();
200 }
[email protected]11d032ba2012-04-09 20:39:51201 }
[email protected]ecde1912012-03-16 06:25:31202 modules->Set(module_name, exports);
203 return handle_scope.Close(exports);
[email protected]58e10452012-02-22 03:34:45204}
205
[email protected]4f1633f2013-03-09 14:26:24206v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
207 const std::string& module_name,
208 const std::string& method_name) {
[email protected]a714e462013-01-26 06:33:10209 std::vector<v8::Handle<v8::Value> > args;
[email protected]4f1633f2013-03-09 14:26:24210 return CallModuleMethod(module_name, method_name, &args);
[email protected]a714e462013-01-26 06:33:10211}
212
213v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
214 const std::string& module_name,
215 const std::string& method_name,
216 std::vector<v8::Handle<v8::Value> >* args) {
[email protected]2bcbf712012-09-05 06:58:49217 v8::HandleScope handle_scope;
218 v8::Local<v8::Value> module =
219 v8::Local<v8::Value>::New(
220 RequireForJsInner(v8::String::New(module_name.c_str())));
[email protected]95ee77da2013-03-19 21:11:11221 if (module.IsEmpty() || !module->IsObject()) {
222 console::Error(
223 v8::Context::GetCalling(),
224 "Failed to get module " + module_name + " to call " + method_name);
225 return handle_scope.Close(v8::Undefined());
226 }
227
[email protected]2bcbf712012-09-05 06:58:49228 v8::Local<v8::Value> value =
229 v8::Handle<v8::Object>::Cast(module)->Get(
230 v8::String::New(method_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11231 if (value.IsEmpty() || !value->IsFunction()) {
232 console::Error(v8::Context::GetCalling(),
233 module_name + "." + method_name + " is not a function");
234 return handle_scope.Close(v8::Undefined());
235 }
236
[email protected]2bcbf712012-09-05 06:58:49237 v8::Handle<v8::Function> func =
238 v8::Handle<v8::Function>::Cast(value);
[email protected]4f1633f2013-03-09 14:26:24239 v8::Handle<v8::Object> global(v8_context()->Global());
[email protected]a714e462013-01-26 06:33:10240 v8::Local<v8::Value> result;
[email protected]2bcbf712012-09-05 06:58:49241 {
242 WebKit::WebScopedMicrotaskSuppression suppression;
243 v8::TryCatch try_catch;
244 try_catch.SetCaptureMessage(true);
[email protected]a714e462013-01-26 06:33:10245 result = func->Call(global, args->size(), vector_as_array(args));
[email protected]2bcbf712012-09-05 06:58:49246 if (try_catch.HasCaught())
[email protected]144114942012-12-04 07:23:23247 HandleException(try_catch);
[email protected]2bcbf712012-09-05 06:58:49248 }
[email protected]a714e462013-01-26 06:33:10249 return handle_scope.Close(result);
[email protected]2bcbf712012-09-05 06:58:49250}
251
[email protected]58e10452012-02-22 03:34:45252void ModuleSystem::RegisterNativeHandler(const std::string& name,
253 scoped_ptr<NativeHandler> native_handler) {
254 native_handler_map_[name] =
255 linked_ptr<NativeHandler>(native_handler.release());
256}
257
[email protected]95ee77da2013-03-19 21:11:11258void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) {
[email protected]11844fa2012-05-10 00:35:59259 overridden_native_handlers_.insert(name);
260}
261
[email protected]ecde1912012-03-16 06:25:31262void ModuleSystem::RunString(const std::string& code, const std::string& name) {
[email protected]9fb990b2012-03-13 02:09:41263 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31264 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
[email protected]58e10452012-02-22 03:34:45265}
266
[email protected]cc0457712012-03-21 03:56:38267// static
[email protected]4f1633f2013-03-09 14:26:24268v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
269 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
270 return LazyFieldGetterInner(property,
271 info,
272 &ModuleSystem::RequireNativeFromString);
273}
274
275// static
[email protected]b6aad81c2012-03-27 08:45:15276v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
[email protected]561ddce2012-03-22 01:18:55277 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
[email protected]4f1633f2013-03-09 14:26:24278 return LazyFieldGetterInner(property, info, &ModuleSystem::Require);
279}
280
281// static
282v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner(
283 v8::Local<v8::String> property,
284 const v8::AccessorInfo& info,
285 RequireFunction require_function) {
[email protected]561ddce2012-03-22 01:18:55286 CHECK(!info.Data().IsEmpty());
287 CHECK(info.Data()->IsObject());
[email protected]cc0457712012-03-21 03:56:38288 v8::HandleScope handle_scope;
[email protected]561ddce2012-03-22 01:18:55289 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
[email protected]95ee77da2013-03-19 21:11:11290 // This context should be the same as v8_context().
[email protected]4f1633f2013-03-09 14:26:24291 v8::Handle<v8::Context> context = parameters->CreationContext();
292 v8::Handle<v8::Object> global(context->Global());
[email protected]561ddce2012-03-22 01:18:55293 v8::Handle<v8::Value> module_system_value =
[email protected]bad9a5f2012-04-13 19:16:54294 global->GetHiddenValue(v8::String::New(kModuleSystem));
[email protected]95ee77da2013-03-19 21:11:11295 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
[email protected]bad9a5f2012-04-13 19:16:54296 // ModuleSystem has been deleted.
[email protected]95ee77da2013-03-19 21:11:11297 // TODO(kalman): See comment in header file.
298 console::Warn(v8::Context::GetCalling(),
299 "Module system has been deleted, does extension view exist?");
[email protected]bad9a5f2012-04-13 19:16:54300 return v8::Undefined();
301 }
[email protected]95ee77da2013-03-19 21:11:11302
[email protected]561ddce2012-03-22 01:18:55303 ModuleSystem* module_system = static_cast<ModuleSystem*>(
304 v8::Handle<v8::External>::Cast(module_system_value)->Value());
[email protected]cc0457712012-03-21 03:56:38305
[email protected]4f1633f2013-03-09 14:26:24306 std::string name = *v8::String::AsciiValue(
307 parameters->Get(v8::String::New(kModuleName))->ToString());
308
309 // Switch to our v8 context because we need functions created while running
310 // the require()d module to belong to our context, not the current one.
311 v8::Context::Scope context_scope(context);
312 NativesEnabledScope natives_enabled_scope(module_system);
313
314 v8::TryCatch try_catch;
[email protected]95ee77da2013-03-19 21:11:11315 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
[email protected]4f1633f2013-03-09 14:26:24316 if (try_catch.HasCaught()) {
317 module_system->HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11318 return v8::Undefined();
319 }
320 if (module_value.IsEmpty() || !module_value->IsObject()) {
321 // require_function will have already logged this, we don't need to.
322 return v8::Undefined();
[email protected]b6aad81c2012-03-27 08:45:15323 }
[email protected]4f1633f2013-03-09 14:26:24324
[email protected]95ee77da2013-03-19 21:11:11325 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
[email protected]561ddce2012-03-22 01:18:55326 v8::Handle<v8::String> field =
327 parameters->Get(v8::String::New(kModuleField))->ToString();
328
[email protected]95ee77da2013-03-19 21:11:11329 if (!module->Has(field)) {
330 std::string field_str = *v8::String::AsciiValue(field);
331 console::Fatal(v8::Context::GetCalling(),
332 "Lazy require of " + name + "." + field_str + " did not " +
333 "set the " + field_str + " field");
334 return v8::Undefined();
335 }
[email protected]4f1633f2013-03-09 14:26:24336
337 v8::Local<v8::Value> new_field = module->Get(field);
[email protected]95ee77da2013-03-19 21:11:11338 if (try_catch.HasCaught()) {
339 module_system->HandleException(try_catch);
340 return v8::Undefined();
341 }
342
343 // Ok for it to be undefined, among other things it's how bindings signify
344 // that the extension doesn't have permission to use them.
345 CHECK(!new_field.IsEmpty());
346
[email protected]4f1633f2013-03-09 14:26:24347 // Delete the getter and set this field to |new_field| so the same object is
348 // returned every time a certain API is accessed.
[email protected]95ee77da2013-03-19 21:11:11349 v8::Handle<v8::Object> object = info.This();
350 object->Delete(property);
351 object->Set(property, new_field);
[email protected]4f1633f2013-03-09 14:26:24352 return handle_scope.Close(new_field);
[email protected]561ddce2012-03-22 01:18:55353}
354
355void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
356 const std::string& field,
357 const std::string& module_name,
358 const std::string& module_field) {
[email protected]4f1633f2013-03-09 14:26:24359 SetLazyField(object, field, module_name, module_field,
360 &ModuleSystem::LazyFieldGetter);
361}
362
363void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
364 const std::string& field,
365 const std::string& module_name,
366 const std::string& module_field,
367 v8::AccessorGetter getter) {
[email protected]561ddce2012-03-22 01:18:55368 v8::HandleScope handle_scope;
369 v8::Handle<v8::Object> parameters = v8::Object::New();
370 parameters->Set(v8::String::New(kModuleName),
371 v8::String::New(module_name.c_str()));
372 parameters->Set(v8::String::New(kModuleField),
373 v8::String::New(module_field.c_str()));
[email protected]561ddce2012-03-22 01:18:55374 object->SetAccessor(v8::String::New(field.c_str()),
[email protected]4f1633f2013-03-09 14:26:24375 getter,
[email protected]561ddce2012-03-22 01:18:55376 NULL,
377 parameters);
[email protected]cc0457712012-03-21 03:56:38378}
[email protected]ecde1912012-03-16 06:25:31379
[email protected]4f1633f2013-03-09 14:26:24380void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
381 const std::string& field,
382 const std::string& module_name,
383 const std::string& module_field) {
384 SetLazyField(object, field, module_name, module_field,
385 &ModuleSystem::NativeLazyFieldGetter);
386}
387
[email protected]ecde1912012-03-16 06:25:31388v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
389 v8::Handle<v8::String> name) {
[email protected]58e10452012-02-22 03:34:45390 v8::HandleScope handle_scope;
[email protected]95ee77da2013-03-19 21:11:11391
[email protected]11d032ba2012-04-09 20:39:51392 WebKit::WebScopedMicrotaskSuppression suppression;
[email protected]7c95c1a82012-04-17 02:11:33393 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54394 try_catch.SetCaptureMessage(true);
[email protected]7c95c1a82012-04-17 02:11:33395 v8::Handle<v8::Script> script(v8::Script::New(code, name));
396 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23397 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11398 return v8::Undefined();
[email protected]7c95c1a82012-04-17 02:11:33399 }
400
[email protected]95ee77da2013-03-19 21:11:11401 v8::Handle<v8::Value> result = script->Run();
402 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23403 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11404 return v8::Undefined();
405 }
[email protected]7c95c1a82012-04-17 02:11:33406
407 return handle_scope.Close(result);
[email protected]9e03ce22012-03-13 08:50:05408}
409
[email protected]d2663612013-03-17 09:25:56410v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
[email protected]9e03ce22012-03-13 08:50:05411 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31412 if (!source_map_->Contains(module_name))
[email protected]58e10452012-02-22 03:34:45413 return v8::Undefined();
[email protected]ecde1912012-03-16 06:25:31414 return handle_scope.Close(source_map_->GetSource(module_name));
[email protected]58e10452012-02-22 03:34:45415}
416
[email protected]4f1633f2013-03-09 14:26:24417v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) {
[email protected]58e10452012-02-22 03:34:45418 CHECK_EQ(1, args.Length());
[email protected]4f1633f2013-03-09 14:26:24419 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
420 return RequireNativeFromString(native_name);
421}
422
423v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
424 const std::string& native_name) {
[email protected]95ee77da2013-03-19 21:11:11425 if (natives_enabled_ == 0) {
426 // HACK: if in test throw exception so that we can test the natives-disabled
427 // logic; however, under normal circumstances, this is programmer error so
428 // we could crash.
429 if (exception_handler_)
430 return v8::ThrowException(v8::String::New("Natives disabled"));
431 console::Fatal(v8::Context::GetCalling(),
432 "Natives disabled for requireNative(" + native_name + ")");
433 return v8::Undefined();
434 }
435
[email protected]11844fa2012-05-10 00:35:59436 if (overridden_native_handlers_.count(native_name) > 0u)
[email protected]4f1633f2013-03-09 14:26:24437 return RequireForJsInner(v8::String::New(native_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11438
[email protected]58e10452012-02-22 03:34:45439 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
[email protected]95ee77da2013-03-19 21:11:11440 if (i == native_handler_map_.end()) {
441 console::Fatal(
442 v8::Context::GetCalling(),
443 "Couldn't find native for requireNative(" + native_name + ")");
[email protected]58e10452012-02-22 03:34:45444 return v8::Undefined();
[email protected]95ee77da2013-03-19 21:11:11445 }
[email protected]58e10452012-02-22 03:34:45446 return i->second->NewInstance();
447}
[email protected]ecde1912012-03-16 06:25:31448
449v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
450 v8::HandleScope handle_scope;
[email protected]b0c739f2013-02-20 09:21:40451 v8::Handle<v8::String> left = v8::String::New(
452 "(function(require, requireNative, exports) {'use strict';");
[email protected]ecde1912012-03-16 06:25:31453 v8::Handle<v8::String> right = v8::String::New("\n})");
454 return handle_scope.Close(
455 v8::String::Concat(left, v8::String::Concat(source, right)));
456}
457
[email protected]3c6babd2012-08-28 03:17:29458} // extensions