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