| 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()->GetObjekt(), | 203 context_->safe_builtins()->GetObjekt(), |
| 204 context_->safe_builtins()->GetRegExp(), |
| 205 context_->safe_builtins()->GetString(), |
| 202 }; | 206 }; |
| 203 { | 207 { |
| 204 v8::TryCatch try_catch; | 208 v8::TryCatch try_catch; |
| 205 try_catch.SetCaptureMessage(true); | 209 try_catch.SetCaptureMessage(true); |
| 206 context_->CallFunction(func, arraysize(args), args); | 210 context_->CallFunction(func, arraysize(args), args); |
| 207 if (try_catch.HasCaught()) { | 211 if (try_catch.HasCaught()) { |
| 208 HandleException(try_catch); | 212 HandleException(try_catch); |
| 209 return v8::Undefined(); | 213 return v8::Undefined(); |
| 210 } | 214 } |
| 211 } | 215 } |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 return v8::Undefined(); | 479 return v8::Undefined(); |
| 476 } | 480 } |
| 477 return i->second->NewInstance(); | 481 return i->second->NewInstance(); |
| 478 } | 482 } |
| 479 | 483 |
| 480 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 484 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 481 v8::HandleScope handle_scope; | 485 v8::HandleScope handle_scope; |
| 482 // Keep in order with the arguments in RequireForJsInner. | 486 // Keep in order with the arguments in RequireForJsInner. |
| 483 v8::Handle<v8::String> left = v8::String::New( | 487 v8::Handle<v8::String> left = v8::String::New( |
| 484 "(function(require, requireNative, exports," | 488 "(function(require, requireNative, exports," |
| 485 "$Array, $Function, $Object) {" | 489 "$Array, $Function, $Object, $RegExp, $String) {" |
| 486 "'use strict';"); | 490 "'use strict';"); |
| 487 v8::Handle<v8::String> right = v8::String::New("\n})"); | 491 v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 488 return handle_scope.Close( | 492 return handle_scope.Close( |
| 489 v8::String::Concat(left, v8::String::Concat(source, right))); | 493 v8::String::Concat(left, v8::String::Concat(source, right))); |
| 490 } | 494 } |
| 491 | 495 |
| 492 } // namespace extensions | 496 } // namespace extensions |
| OLD | NEW |