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

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

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

Powered by Google App Engine
This is Rietveld 408576698