Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(598)

Side by Side Diff: chrome/renderer/module_system.cc

Issue 10105013: ModuleSystem should not crash when JS has a syntax error. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: blah Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {
11 11
12 const char* kModuleSystem = "module_system"; 12 const char* kModuleSystem = "module_system";
13 const char* kModuleName = "module_name"; 13 const char* kModuleName = "module_name";
14 const char* kModuleField = "module_field"; 14 const char* kModuleField = "module_field";
15 15
16 void DumpException(v8::Handle<v8::Message> message) {
17 LOG(ERROR) << "["
18 << *v8::String::Utf8Value(
19 message->GetScriptResourceName()->ToString())
20 << "(" << message->GetLineNumber() << ")] "
21 << *v8::String::Utf8Value(message->Get());
22 }
23
16 } // namespace 24 } // namespace
17 25
18 ModuleSystem::ModuleSystem(SourceMap* source_map) 26 ModuleSystem::ModuleSystem(SourceMap* source_map)
19 : source_map_(source_map), 27 : source_map_(source_map),
20 natives_enabled_(0) { 28 natives_enabled_(0) {
21 RouteFunction("require", 29 RouteFunction("require",
22 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); 30 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
23 RouteFunction("requireNative", 31 RouteFunction("requireNative",
24 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 32 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
25 33
(...skipping 28 matching lines...) Expand all
54 62
55 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( 63 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
56 v8::Handle<v8::String> module_name) { 64 v8::Handle<v8::String> module_name) {
57 v8::HandleScope handle_scope; 65 v8::HandleScope handle_scope;
58 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); 66 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
59 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( 67 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(
60 global->GetHiddenValue(v8::String::New("modules")))); 68 global->GetHiddenValue(v8::String::New("modules"))));
61 v8::Handle<v8::Value> exports(modules->Get(module_name)); 69 v8::Handle<v8::Value> exports(modules->Get(module_name));
62 if (!exports->IsUndefined()) 70 if (!exports->IsUndefined())
63 return handle_scope.Close(exports); 71 return handle_scope.Close(exports);
72
64 v8::Handle<v8::Value> source(GetSource(module_name)); 73 v8::Handle<v8::Value> source(GetSource(module_name));
65 if (source->IsUndefined()) 74 if (source->IsUndefined())
66 return handle_scope.Close(v8::Undefined()); 75 return handle_scope.Close(v8::Undefined());
67 v8::Handle<v8::String> wrapped_source(WrapSource( 76 v8::Handle<v8::String> wrapped_source(WrapSource(
68 v8::Handle<v8::String>::Cast(source))); 77 v8::Handle<v8::String>::Cast(source)));
69 v8::Handle<v8::Function> func = 78 v8::Handle<v8::Function> func =
70 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name)); 79 v8::Handle<v8::Function>::Cast(RunString(wrapped_source, module_name));
80 if (func.IsEmpty())
81 return handle_scope.Close(v8::Handle<v8::Value>());
82
71 exports = v8::Object::New(); 83 exports = v8::Object::New();
72 v8::Handle<v8::Object> natives(NewInstance()); 84 v8::Handle<v8::Object> natives(NewInstance());
73 v8::Handle<v8::Value> args[] = { 85 v8::Handle<v8::Value> args[] = {
74 natives->Get(v8::String::NewSymbol("require")), 86 natives->Get(v8::String::NewSymbol("require")),
75 natives->Get(v8::String::NewSymbol("requireNative")), 87 natives->Get(v8::String::NewSymbol("requireNative")),
76 exports, 88 exports,
77 }; 89 };
78 { 90 {
79 WebKit::WebScopedMicrotaskSuppression suppression; 91 WebKit::WebScopedMicrotaskSuppression suppression;
80 func->Call(global, 3, args); 92 func->Call(global, 3, args);
(...skipping 24 matching lines...) Expand all
105 parameters->Get(v8::String::New(kModuleSystem)); 117 parameters->Get(v8::String::New(kModuleSystem));
106 ModuleSystem* module_system = static_cast<ModuleSystem*>( 118 ModuleSystem* module_system = static_cast<ModuleSystem*>(
107 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 119 v8::Handle<v8::External>::Cast(module_system_value)->Value());
108 120
109 v8::Handle<v8::Object> module; 121 v8::Handle<v8::Object> module;
110 { 122 {
111 NativesEnabledScope scope(module_system); 123 NativesEnabledScope scope(module_system);
112 module = module_system->RequireForJsInner( 124 module = module_system->RequireForJsInner(
113 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject(); 125 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject();
114 } 126 }
127 if (module.IsEmpty())
128 return handle_scope.Close(v8::Handle<v8::Value>());
115 129
116 v8::Handle<v8::String> field = 130 v8::Handle<v8::String> field =
117 parameters->Get(v8::String::New(kModuleField))->ToString(); 131 parameters->Get(v8::String::New(kModuleField))->ToString();
118 132
119 return handle_scope.Close(module->Get(field)); 133 return handle_scope.Close(module->Get(field));
120 } 134 }
121 135
122 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 136 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
123 const std::string& field, 137 const std::string& field,
124 const std::string& module_name, 138 const std::string& module_name,
125 const std::string& module_field) { 139 const std::string& module_field) {
126 v8::HandleScope handle_scope; 140 v8::HandleScope handle_scope;
127 v8::Handle<v8::Object> parameters = v8::Object::New(); 141 v8::Handle<v8::Object> parameters = v8::Object::New();
128 parameters->Set(v8::String::New(kModuleName), 142 parameters->Set(v8::String::New(kModuleName),
129 v8::String::New(module_name.c_str())); 143 v8::String::New(module_name.c_str()));
130 parameters->Set(v8::String::New(kModuleField), 144 parameters->Set(v8::String::New(kModuleField),
131 v8::String::New(module_field.c_str())); 145 v8::String::New(module_field.c_str()));
132 parameters->Set(v8::String::New(kModuleSystem), v8::External::New(this)); 146 parameters->Set(v8::String::New(kModuleSystem), v8::External::New(this));
133 147
134 object->SetAccessor(v8::String::New(field.c_str()), 148 object->SetAccessor(v8::String::New(field.c_str()),
135 &ModuleSystem::LazyFieldGetter, 149 &ModuleSystem::LazyFieldGetter,
136 NULL, 150 NULL,
137 parameters); 151 parameters);
138 } 152 }
139 153
140 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 154 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
141 v8::Handle<v8::String> name) { 155 v8::Handle<v8::String> name) {
142 v8::HandleScope handle_scope; 156 v8::HandleScope handle_scope;
143 WebKit::WebScopedMicrotaskSuppression suppression; 157 WebKit::WebScopedMicrotaskSuppression suppression;
144 return handle_scope.Close(v8::Script::New(code, name)->Run()); 158 v8::Handle<v8::Value> result;
159 v8::TryCatch try_catch;
160 try_catch.SetCaptureMessage(true);
161 v8::Handle<v8::Script> script(v8::Script::New(code, name));
162 if (try_catch.HasCaught()) {
163 DumpException(try_catch.Message());
164 return handle_scope.Close(result);
165 }
166
167 result = script->Run();
168 if (try_catch.HasCaught())
169 DumpException(try_catch.Message());
170
171 return handle_scope.Close(result);
145 } 172 }
146 173
147 v8::Handle<v8::Value> ModuleSystem::GetSource( 174 v8::Handle<v8::Value> ModuleSystem::GetSource(
148 v8::Handle<v8::String> source_name) { 175 v8::Handle<v8::String> source_name) {
149 v8::HandleScope handle_scope; 176 v8::HandleScope handle_scope;
150 std::string module_name = *v8::String::AsciiValue(source_name); 177 std::string module_name = *v8::String::AsciiValue(source_name);
151 if (!source_map_->Contains(module_name)) 178 if (!source_map_->Contains(module_name))
152 return v8::Undefined(); 179 return v8::Undefined();
153 return handle_scope.Close(source_map_->GetSource(module_name)); 180 return handle_scope.Close(source_map_->GetSource(module_name));
154 } 181 }
(...skipping 14 matching lines...) Expand all
169 v8::Handle<v8::String> left = 196 v8::Handle<v8::String> left =
170 v8::String::New("(function(require, requireNative, exports) {"); 197 v8::String::New("(function(require, requireNative, exports) {");
171 v8::Handle<v8::String> right = v8::String::New("\n})"); 198 v8::Handle<v8::String> right = v8::String::New("\n})");
172 return handle_scope.Close( 199 return handle_scope.Close(
173 v8::String::Concat(left, v8::String::Concat(source, right))); 200 v8::String::Concat(left, v8::String::Concat(source, right)));
174 } 201 }
175 202
176 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { 203 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
177 return v8::ThrowException(v8::String::New(message.c_str())); 204 return v8::ThrowException(v8::String::New(message.c_str()));
178 } 205 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698