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/module_system.h" | 5 #include "chrome/renderer/module_system.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup
pression.h" |
9 | 9 |
10 namespace { | 10 namespace { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 // static | 54 // static |
55 bool ModuleSystem::IsPresentInCurrentContext() { | 55 bool ModuleSystem::IsPresentInCurrentContext() { |
56 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 56 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
57 v8::Handle<v8::Value> module_system = | 57 v8::Handle<v8::Value> module_system = |
58 global->GetHiddenValue(v8::String::New(kModuleSystem)); | 58 global->GetHiddenValue(v8::String::New(kModuleSystem)); |
59 return !module_system.IsEmpty() && !module_system->IsUndefined(); | 59 return !module_system.IsEmpty() && !module_system->IsUndefined(); |
60 } | 60 } |
61 | 61 |
62 // static | 62 // static |
63 void ModuleSystem::DumpException(v8::Handle<v8::Message> message) { | 63 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) { |
| 64 v8::Handle<v8::Message> message(try_catch.Message()); |
| 65 |
64 LOG(ERROR) << "[" | 66 LOG(ERROR) << "[" |
65 << *v8::String::Utf8Value( | 67 << *v8::String::Utf8Value( |
66 message->GetScriptResourceName()->ToString()) | 68 message->GetScriptResourceName()->ToString()) |
67 << "(" << message->GetLineNumber() << ")] " | 69 << "(" << message->GetLineNumber() << ")] " |
68 << *v8::String::Utf8Value(message->Get()); | 70 << *v8::String::Utf8Value(message->Get()) |
| 71 << "{" << *v8::String::Utf8Value(try_catch.StackTrace()) << "}"; |
69 } | 72 } |
70 | 73 |
71 void ModuleSystem::Require(const std::string& module_name) { | 74 void ModuleSystem::Require(const std::string& module_name) { |
72 v8::HandleScope handle_scope; | 75 v8::HandleScope handle_scope; |
73 RequireForJsInner(v8::String::New(module_name.c_str())); | 76 RequireForJsInner(v8::String::New(module_name.c_str())); |
74 } | 77 } |
75 | 78 |
76 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 79 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
77 v8::HandleScope handle_scope; | 80 v8::HandleScope handle_scope; |
78 v8::Handle<v8::String> module_name = args[0]->ToString(); | 81 v8::Handle<v8::String> module_name = args[0]->ToString(); |
(...skipping 10 matching lines...) Expand all Loading... |
89 if (!exports->IsUndefined()) | 92 if (!exports->IsUndefined()) |
90 return handle_scope.Close(exports); | 93 return handle_scope.Close(exports); |
91 | 94 |
92 v8::Handle<v8::Value> source(GetSource(module_name)); | 95 v8::Handle<v8::Value> source(GetSource(module_name)); |
93 if (source->IsUndefined()) | 96 if (source->IsUndefined()) |
94 return handle_scope.Close(v8::Undefined()); | 97 return handle_scope.Close(v8::Undefined()); |
95 v8::Handle<v8::String> wrapped_source(WrapSource( | 98 v8::Handle<v8::String> wrapped_source(WrapSource( |
96 v8::Handle<v8::String>::Cast(source))); | 99 v8::Handle<v8::String>::Cast(source))); |
97 v8::Handle<v8::Function> func = | 100 v8::Handle<v8::Function> func = |
98 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); | 101 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); |
99 if (func.IsEmpty()) | 102 if (func.IsEmpty()) { |
100 return handle_scope.Close(v8::Handle<v8::Value>()); | 103 return ThrowException(std::string(*v8::String::AsciiValue(module_name)) + |
| 104 ": Bad source"); |
| 105 } |
101 | 106 |
102 exports = v8::Object::New(); | 107 exports = v8::Object::New(); |
103 v8::Handle<v8::Object> natives(NewInstance()); | 108 v8::Handle<v8::Object> natives(NewInstance()); |
104 v8::Handle<v8::Value> args[] = { | 109 v8::Handle<v8::Value> args[] = { |
105 natives->Get(v8::String::NewSymbol("require")), | 110 natives->Get(v8::String::NewSymbol("require")), |
106 natives->Get(v8::String::NewSymbol("requireNative")), | 111 natives->Get(v8::String::NewSymbol("requireNative")), |
107 exports, | 112 exports, |
108 }; | 113 }; |
109 { | 114 { |
110 WebKit::WebScopedMicrotaskSuppression suppression; | 115 WebKit::WebScopedMicrotaskSuppression suppression; |
| 116 v8::TryCatch try_catch; |
111 func->Call(global, 3, args); | 117 func->Call(global, 3, args); |
| 118 if (try_catch.HasCaught()) { |
| 119 DumpException(try_catch); |
| 120 return v8::Undefined(); |
| 121 } |
112 } | 122 } |
113 modules->Set(module_name, exports); | 123 modules->Set(module_name, exports); |
114 return handle_scope.Close(exports); | 124 return handle_scope.Close(exports); |
115 } | 125 } |
116 | 126 |
117 void ModuleSystem::RegisterNativeHandler(const std::string& name, | 127 void ModuleSystem::RegisterNativeHandler(const std::string& name, |
118 scoped_ptr<NativeHandler> native_handler) { | 128 scoped_ptr<NativeHandler> native_handler) { |
119 native_handler_map_[name] = | 129 native_handler_map_[name] = |
120 linked_ptr<NativeHandler>(native_handler.release()); | 130 linked_ptr<NativeHandler>(native_handler.release()); |
121 } | 131 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 NULL, | 187 NULL, |
178 parameters); | 188 parameters); |
179 } | 189 } |
180 | 190 |
181 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, | 191 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
182 v8::Handle<v8::String> name) { | 192 v8::Handle<v8::String> name) { |
183 v8::HandleScope handle_scope; | 193 v8::HandleScope handle_scope; |
184 WebKit::WebScopedMicrotaskSuppression suppression; | 194 WebKit::WebScopedMicrotaskSuppression suppression; |
185 v8::Handle<v8::Value> result; | 195 v8::Handle<v8::Value> result; |
186 v8::TryCatch try_catch; | 196 v8::TryCatch try_catch; |
187 try_catch.SetCaptureMessage(true); | |
188 v8::Handle<v8::Script> script(v8::Script::New(code, name)); | 197 v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
189 if (try_catch.HasCaught()) { | 198 if (try_catch.HasCaught()) { |
190 DumpException(try_catch.Message()); | 199 DumpException(try_catch); |
191 return handle_scope.Close(result); | 200 return handle_scope.Close(result); |
192 } | 201 } |
193 | 202 |
194 result = script->Run(); | 203 result = script->Run(); |
195 if (try_catch.HasCaught()) | 204 if (try_catch.HasCaught()) |
196 DumpException(try_catch.Message()); | 205 DumpException(try_catch); |
197 | 206 |
198 return handle_scope.Close(result); | 207 return handle_scope.Close(result); |
199 } | 208 } |
200 | 209 |
201 v8::Handle<v8::Value> ModuleSystem::GetSource( | 210 v8::Handle<v8::Value> ModuleSystem::GetSource( |
202 v8::Handle<v8::String> source_name) { | 211 v8::Handle<v8::String> source_name) { |
203 v8::HandleScope handle_scope; | 212 v8::HandleScope handle_scope; |
204 std::string module_name = *v8::String::AsciiValue(source_name); | 213 std::string module_name = *v8::String::AsciiValue(source_name); |
205 if (!source_map_->Contains(module_name)) | 214 if (!source_map_->Contains(module_name)) |
206 return v8::Undefined(); | 215 return v8::Undefined(); |
(...skipping 18 matching lines...) Expand all Loading... |
225 v8::Handle<v8::String> left = | 234 v8::Handle<v8::String> left = |
226 v8::String::New("(function(require, requireNative, exports) {"); | 235 v8::String::New("(function(require, requireNative, exports) {"); |
227 v8::Handle<v8::String> right = v8::String::New("\n})"); | 236 v8::Handle<v8::String> right = v8::String::New("\n})"); |
228 return handle_scope.Close( | 237 return handle_scope.Close( |
229 v8::String::Concat(left, v8::String::Concat(source, right))); | 238 v8::String::Concat(left, v8::String::Concat(source, right))); |
230 } | 239 } |
231 | 240 |
232 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 241 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
233 return v8::ThrowException(v8::String::New(message.c_str())); | 242 return v8::ThrowException(v8::String::New(message.c_str())); |
234 } | 243 } |
OLD | NEW |