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