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/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 182 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
183 console::Error(v8::Context::GetCalling(), | 183 console::Error(v8::Context::GetCalling(), |
184 "Bad source for require(" + module_name_str + ")"); | 184 "Bad source for require(" + module_name_str + ")"); |
185 return v8::Undefined(); | 185 return v8::Undefined(); |
186 } | 186 } |
187 | 187 |
188 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); | 188 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
189 | 189 |
190 exports = v8::Object::New(); | 190 exports = v8::Object::New(); |
191 v8::Handle<v8::Object> natives(NewInstance()); | 191 v8::Handle<v8::Object> natives(NewInstance()); |
| 192 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues |
| 193 |
192 // These must match the argument order in WrapSource. | 194 // These must match the argument order in WrapSource. |
193 v8::Handle<v8::Value> args[] = { | 195 v8::Handle<v8::Value> args[] = { |
194 // CommonJS. | 196 // CommonJS. |
195 natives->Get(v8::String::NewSymbol("require")), | 197 natives->Get(v8::String::NewSymbol("require")), |
196 natives->Get(v8::String::NewSymbol("requireNative")), | 198 natives->Get(v8::String::NewSymbol("requireNative")), |
197 exports, | 199 exports, |
198 // Each safe builtin. Keep in order with the arguments in WrapSource. | 200 // Each safe builtin. Keep in order with the arguments in WrapSource. |
199 context_->safe_builtins()->GetArray(), | 201 context_->safe_builtins()->GetArray(), |
200 context_->safe_builtins()->GetFunction(), | 202 context_->safe_builtins()->GetFunction(), |
201 context_->safe_builtins()->GetJSON(), | 203 context_->safe_builtins()->GetJSON(), |
202 context_->safe_builtins()->GetObjekt(), | 204 context_->safe_builtins()->GetObjekt(), |
| 205 context_->safe_builtins()->GetRegExp(), |
| 206 context_->safe_builtins()->GetString(), |
203 }; | 207 }; |
204 { | 208 { |
205 v8::TryCatch try_catch; | 209 v8::TryCatch try_catch; |
206 try_catch.SetCaptureMessage(true); | 210 try_catch.SetCaptureMessage(true); |
207 context_->CallFunction(func, arraysize(args), args); | 211 context_->CallFunction(func, arraysize(args), args); |
208 if (try_catch.HasCaught()) { | 212 if (try_catch.HasCaught()) { |
209 HandleException(try_catch); | 213 HandleException(try_catch); |
210 return v8::Undefined(); | 214 return v8::Undefined(); |
211 } | 215 } |
212 } | 216 } |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 return v8::Undefined(); | 480 return v8::Undefined(); |
477 } | 481 } |
478 return i->second->NewInstance(); | 482 return i->second->NewInstance(); |
479 } | 483 } |
480 | 484 |
481 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 485 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
482 v8::HandleScope handle_scope; | 486 v8::HandleScope handle_scope; |
483 // Keep in order with the arguments in RequireForJsInner. | 487 // Keep in order with the arguments in RequireForJsInner. |
484 v8::Handle<v8::String> left = v8::String::New( | 488 v8::Handle<v8::String> left = v8::String::New( |
485 "(function(require, requireNative, exports," | 489 "(function(require, requireNative, exports," |
486 "$Array, $Function, $JSON, $Object) {" | 490 "$Array, $Function, $JSON, $Object, $RegExp, $String) {" |
487 "'use strict';"); | 491 "'use strict';"); |
488 v8::Handle<v8::String> right = v8::String::New("\n})"); | 492 v8::Handle<v8::String> right = v8::String::New("\n})"); |
489 return handle_scope.Close( | 493 return handle_scope.Close( |
490 v8::String::Concat(left, v8::String::Concat(source, right))); | 494 v8::String::Concat(left, v8::String::Concat(source, right))); |
491 } | 495 } |
492 | 496 |
493 } // namespace extensions | 497 } // namespace extensions |
OLD | NEW |