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