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 "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; |
42 // Deleting this value here prevents future lazy field accesses from | 53 // Deleting this value here prevents future lazy field accesses from |
43 // referencing ModuleSystem after it has been freed. | 54 // referencing ModuleSystem after it has been freed. |
44 context_->Global()->DeleteHiddenValue(v8::String::New(kModuleSystem)); | 55 context_->Global()->DeleteHiddenValue( |
45 context_.Dispose(context_->GetIsolate()); | 56 v8::String::New(kModuleSystem)); |
| 57 is_valid_ = false; |
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 Loading... |
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", |
| 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) { |
| 140 CHECK(is_valid_); |
122 v8::HandleScope handle_scope; | 141 v8::HandleScope handle_scope; |
123 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 142 v8::Handle<v8::Object> global(context_->Global()); |
124 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( | 143 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( |
125 global->GetHiddenValue(v8::String::New(kModulesField)))); | 144 global->GetHiddenValue(v8::String::New(kModulesField)))); |
126 v8::Handle<v8::Value> exports(modules->Get(module_name)); | 145 v8::Handle<v8::Value> exports(modules->Get(module_name)); |
127 if (!exports->IsUndefined()) | 146 if (!exports->IsUndefined()) |
128 return handle_scope.Close(exports); | 147 return handle_scope.Close(exports); |
129 | 148 |
130 v8::Handle<v8::Value> source(GetSource(module_name)); | 149 v8::Handle<v8::Value> source(GetSource(module_name)); |
131 if (source->IsUndefined()) | 150 if (source->IsUndefined()) |
132 return handle_scope.Close(v8::Undefined()); | 151 return handle_scope.Close(v8::Undefined()); |
133 v8::Handle<v8::String> wrapped_source(WrapSource( | 152 v8::Handle<v8::String> wrapped_source(WrapSource( |
(...skipping 19 matching lines...) Expand all Loading... |
153 func->Call(global, 3, args); | 172 func->Call(global, 3, args); |
154 if (try_catch.HasCaught()) { | 173 if (try_catch.HasCaught()) { |
155 HandleException(try_catch); | 174 HandleException(try_catch); |
156 return v8::Undefined(); | 175 return v8::Undefined(); |
157 } | 176 } |
158 } | 177 } |
159 modules->Set(module_name, exports); | 178 modules->Set(module_name, exports); |
160 return handle_scope.Close(exports); | 179 return handle_scope.Close(exports); |
161 } | 180 } |
162 | 181 |
163 void ModuleSystem::CallModuleMethod(const std::string& module_name, | 182 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
164 const std::string& method_name) { | 183 const std::string& module_name, |
| 184 const std::string& method_name) { |
165 std::vector<v8::Handle<v8::Value> > args; | 185 std::vector<v8::Handle<v8::Value> > args; |
166 CallModuleMethod(module_name, method_name, &args); | 186 return CallModuleMethod(module_name, method_name, &args); |
167 } | 187 } |
168 | 188 |
169 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 189 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
170 const std::string& module_name, | 190 const std::string& module_name, |
171 const std::string& method_name, | 191 const std::string& method_name, |
172 std::vector<v8::Handle<v8::Value> >* args) { | 192 std::vector<v8::Handle<v8::Value> >* args) { |
173 v8::HandleScope handle_scope; | 193 v8::HandleScope handle_scope; |
174 v8::Local<v8::Value> module = | 194 v8::Local<v8::Value> module = |
175 v8::Local<v8::Value>::New( | 195 v8::Local<v8::Value>::New( |
176 RequireForJsInner(v8::String::New(module_name.c_str()))); | 196 RequireForJsInner(v8::String::New(module_name.c_str()))); |
177 if (module.IsEmpty() || !module->IsObject()) | 197 if (module.IsEmpty() || !module->IsObject()) |
178 return v8::Local<v8::Value>(); | 198 return v8::Local<v8::Value>(); |
179 v8::Local<v8::Value> value = | 199 v8::Local<v8::Value> value = |
180 v8::Handle<v8::Object>::Cast(module)->Get( | 200 v8::Handle<v8::Object>::Cast(module)->Get( |
181 v8::String::New(method_name.c_str())); | 201 v8::String::New(method_name.c_str())); |
182 if (value.IsEmpty() || !value->IsFunction()) | 202 if (value.IsEmpty() || !value->IsFunction()) |
183 return v8::Local<v8::Value>(); | 203 return v8::Local<v8::Value>(); |
184 v8::Handle<v8::Function> func = | 204 v8::Handle<v8::Function> func = |
185 v8::Handle<v8::Function>::Cast(value); | 205 v8::Handle<v8::Function>::Cast(value); |
186 // TODO(jeremya/koz): refer to context_ here, not the current context. | 206 v8::Handle<v8::Object> global(context_->Global()); |
187 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | |
188 v8::Local<v8::Value> result; | 207 v8::Local<v8::Value> result; |
189 { | 208 { |
190 WebKit::WebScopedMicrotaskSuppression suppression; | 209 WebKit::WebScopedMicrotaskSuppression suppression; |
191 v8::TryCatch try_catch; | 210 v8::TryCatch try_catch; |
192 try_catch.SetCaptureMessage(true); | 211 try_catch.SetCaptureMessage(true); |
193 result = func->Call(global, args->size(), vector_as_array(args)); | 212 result = func->Call(global, args->size(), vector_as_array(args)); |
194 if (try_catch.HasCaught()) | 213 if (try_catch.HasCaught()) |
195 HandleException(try_catch); | 214 HandleException(try_catch); |
196 } | 215 } |
197 return handle_scope.Close(result); | 216 return handle_scope.Close(result); |
198 } | 217 } |
199 | 218 |
| 219 bool ModuleSystem::HasNativeHandler(const std::string& name) { |
| 220 return native_handler_map_.find(name) != native_handler_map_.end(); |
| 221 } |
| 222 |
200 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 223 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
201 scoped_ptr<NativeHandler> native_handler) { | 224 scoped_ptr<NativeHandler> native_handler) { |
202 native_handler_map_[name] = | 225 native_handler_map_[name] = |
203 linked_ptr<NativeHandler>(native_handler.release()); | 226 linked_ptr<NativeHandler>(native_handler.release()); |
204 } | 227 } |
205 | 228 |
206 void ModuleSystem::OverrideNativeHandler(const std::string& name) { | 229 void ModuleSystem::OverrideNativeHandler(const std::string& name) { |
207 overridden_native_handlers_.insert(name); | 230 overridden_native_handlers_.insert(name); |
208 } | 231 } |
209 | 232 |
210 void ModuleSystem::RunString(const std::string& code, const std::string& name) { | 233 void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
211 v8::HandleScope handle_scope; | 234 v8::HandleScope handle_scope; |
212 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); | 235 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
213 } | 236 } |
214 | 237 |
215 // static | 238 // static |
| 239 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter( |
| 240 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 241 return LazyFieldGetterInner(property, |
| 242 info, |
| 243 &ModuleSystem::RequireNativeFromString); |
| 244 } |
| 245 |
| 246 // static |
216 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( | 247 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( |
217 v8::Local<v8::String> property, const v8::AccessorInfo& info) { | 248 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 249 return LazyFieldGetterInner(property, info, &ModuleSystem::Require); |
| 250 } |
| 251 |
| 252 // static |
| 253 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner( |
| 254 v8::Local<v8::String> property, |
| 255 const v8::AccessorInfo& info, |
| 256 GetModuleFunc get_module) { |
218 CHECK(!info.Data().IsEmpty()); | 257 CHECK(!info.Data().IsEmpty()); |
219 CHECK(info.Data()->IsObject()); | 258 CHECK(info.Data()->IsObject()); |
220 v8::HandleScope handle_scope; | 259 v8::HandleScope handle_scope; |
221 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 260 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
222 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 261 v8::Handle<v8::Object> global(parameters->CreationContext()->Global()); |
223 v8::Handle<v8::Value> module_system_value = | 262 v8::Handle<v8::Value> module_system_value = |
224 global->GetHiddenValue(v8::String::New(kModuleSystem)); | 263 global->GetHiddenValue(v8::String::New(kModuleSystem)); |
225 if (module_system_value->IsUndefined()) { | 264 if (module_system_value->IsUndefined()) { |
226 // ModuleSystem has been deleted. | 265 // ModuleSystem has been deleted. |
227 return v8::Undefined(); | 266 return v8::Undefined(); |
228 } | 267 } |
229 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 268 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
230 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | 269 v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
231 | 270 |
232 v8::Handle<v8::Object> module; | 271 std::string name = *v8::String::AsciiValue( |
233 { | 272 parameters->Get(v8::String::New(kModuleName))->ToString()); |
234 NativesEnabledScope scope(module_system); | 273 |
235 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner( | 274 NativesEnabledScope scope(module_system); |
236 parameters->Get(v8::String::New(kModuleName))->ToString())); | 275 v8::TryCatch try_catch; |
| 276 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast( |
| 277 (module_system->*get_module)(name)); |
| 278 if (try_catch.HasCaught()) { |
| 279 module_system->HandleException(try_catch); |
| 280 return handle_scope.Close(v8::Handle<v8::Value>()); |
237 } | 281 } |
| 282 |
238 if (module.IsEmpty()) | 283 if (module.IsEmpty()) |
239 return handle_scope.Close(v8::Handle<v8::Value>()); | 284 return handle_scope.Close(v8::Handle<v8::Value>()); |
240 | 285 |
241 v8::Handle<v8::String> field = | 286 v8::Handle<v8::String> field = |
242 parameters->Get(v8::String::New(kModuleField))->ToString(); | 287 parameters->Get(v8::String::New(kModuleField))->ToString(); |
243 | 288 |
244 return handle_scope.Close(module->Get(field)); | 289 v8::Local<v8::Value> new_field = module->Get(field); |
| 290 v8::Handle<v8::Object> object = info.This(); |
| 291 // Delete the getter and set this field to |new_field| so the same object is |
| 292 // returned every time a certain API is accessed. |
| 293 if (!new_field->IsUndefined()) { |
| 294 object->Delete(property); |
| 295 object->Set(property, new_field); |
| 296 } |
| 297 return handle_scope.Close(new_field); |
245 } | 298 } |
246 | 299 |
247 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 300 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
248 const std::string& field, | 301 const std::string& field, |
249 const std::string& module_name, | 302 const std::string& module_name, |
250 const std::string& module_field) { | 303 const std::string& module_field) { |
| 304 SetLazyField(object, field, module_name, module_field, |
| 305 &ModuleSystem::LazyFieldGetter); |
| 306 } |
| 307 |
| 308 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 309 const std::string& field, |
| 310 const std::string& module_name, |
| 311 const std::string& module_field, |
| 312 v8::AccessorGetter getter) { |
251 v8::HandleScope handle_scope; | 313 v8::HandleScope handle_scope; |
252 v8::Handle<v8::Object> parameters = v8::Object::New(); | 314 v8::Handle<v8::Object> parameters = v8::Object::New(); |
253 parameters->Set(v8::String::New(kModuleName), | 315 parameters->Set(v8::String::New(kModuleName), |
254 v8::String::New(module_name.c_str())); | 316 v8::String::New(module_name.c_str())); |
255 parameters->Set(v8::String::New(kModuleField), | 317 parameters->Set(v8::String::New(kModuleField), |
256 v8::String::New(module_field.c_str())); | 318 v8::String::New(module_field.c_str())); |
257 | |
258 object->SetAccessor(v8::String::New(field.c_str()), | 319 object->SetAccessor(v8::String::New(field.c_str()), |
259 &ModuleSystem::LazyFieldGetter, | 320 getter, |
260 NULL, | 321 NULL, |
261 parameters); | 322 parameters); |
262 } | 323 } |
263 | 324 |
| 325 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, |
| 326 const std::string& field, |
| 327 const std::string& module_name, |
| 328 const std::string& module_field) { |
| 329 SetLazyField(object, field, module_name, module_field, |
| 330 &ModuleSystem::NativeLazyFieldGetter); |
| 331 } |
| 332 |
264 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 333 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
265 v8::Handle<v8::String> name) { | 334 v8::Handle<v8::String> name) { |
266 v8::HandleScope handle_scope; | 335 v8::HandleScope handle_scope; |
267 WebKit::WebScopedMicrotaskSuppression suppression; | 336 WebKit::WebScopedMicrotaskSuppression suppression; |
268 v8::Handle<v8::Value> result; | 337 v8::Handle<v8::Value> result; |
269 v8::TryCatch try_catch; | 338 v8::TryCatch try_catch; |
270 try_catch.SetCaptureMessage(true); | 339 try_catch.SetCaptureMessage(true); |
271 v8::Handle<v8::Script> script(v8::Script::New(code, name)); | 340 v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
272 if (try_catch.HasCaught()) { | 341 if (try_catch.HasCaught()) { |
273 HandleException(try_catch); | 342 HandleException(try_catch); |
274 return handle_scope.Close(result); | 343 return handle_scope.Close(result); |
275 } | 344 } |
276 | 345 |
277 result = script->Run(); | 346 result = script->Run(); |
278 if (try_catch.HasCaught()) | 347 if (try_catch.HasCaught()) |
279 HandleException(try_catch); | 348 HandleException(try_catch); |
280 | 349 |
281 return handle_scope.Close(result); | 350 return handle_scope.Close(result); |
282 } | 351 } |
283 | 352 |
284 v8::Handle<v8::Value> ModuleSystem::GetSource( | 353 v8::Handle<v8::Value> ModuleSystem::GetSource( |
285 v8::Handle<v8::String> source_name) { | 354 v8::Handle<v8::String> source_name) { |
286 v8::HandleScope handle_scope; | 355 v8::HandleScope handle_scope; |
287 std::string module_name = *v8::String::AsciiValue(source_name); | 356 std::string module_name = *v8::String::AsciiValue(source_name); |
288 if (!source_map_->Contains(module_name)) | 357 if (!source_map_->Contains(module_name)) |
289 return v8::Undefined(); | 358 return v8::Undefined(); |
290 return handle_scope.Close(source_map_->GetSource(module_name)); | 359 return handle_scope.Close(source_map_->GetSource(module_name)); |
291 } | 360 } |
292 | 361 |
293 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { | 362 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { |
294 CHECK_EQ(1, args.Length()); | 363 CHECK_EQ(1, args.Length()); |
| 364 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 365 return RequireNativeFromString(native_name); |
| 366 } |
| 367 |
| 368 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
| 369 const std::string& native_name) { |
295 if (natives_enabled_ == 0) | 370 if (natives_enabled_ == 0) |
296 return ThrowException("Natives disabled"); | 371 return ThrowException("Natives disabled"); |
297 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); | |
298 if (overridden_native_handlers_.count(native_name) > 0u) | 372 if (overridden_native_handlers_.count(native_name) > 0u) |
299 return RequireForJs(args); | 373 return RequireForJsInner(v8::String::New(native_name.c_str())); |
300 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); | 374 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
301 if (i == native_handler_map_.end()) | 375 if (i == native_handler_map_.end()) |
302 return v8::Undefined(); | 376 return v8::Undefined(); |
303 return i->second->NewInstance(); | 377 return i->second->NewInstance(); |
304 } | 378 } |
305 | 379 |
306 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 380 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
307 v8::HandleScope handle_scope; | 381 v8::HandleScope handle_scope; |
308 v8::Handle<v8::String> left = v8::String::New( | 382 v8::Handle<v8::String> left = v8::String::New( |
309 "(function(require, requireNative, exports) {'use strict';"); | 383 "(function(require, requireNative, exports) {'use strict';"); |
310 v8::Handle<v8::String> right = v8::String::New("\n})"); | 384 v8::Handle<v8::String> right = v8::String::New("\n})"); |
311 return handle_scope.Close( | 385 return handle_scope.Close( |
312 v8::String::Concat(left, v8::String::Concat(source, right))); | 386 v8::String::Concat(left, v8::String::Concat(source, right))); |
313 } | 387 } |
314 | 388 |
315 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 389 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
316 return v8::ThrowException(v8::String::New(message.c_str())); | 390 return v8::ThrowException(v8::String::New(message.c_str())); |
317 } | 391 } |
318 | 392 |
319 } // extensions | 393 } // extensions |
OLD | NEW |