Chromium Code Reviews| 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 | 8 |
| 9 namespace { | 9 namespace { |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 RouteFunction("requireNative", | 22 RouteFunction("requireNative", |
| 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); | 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); |
| 24 | 24 |
| 25 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); | 25 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); |
| 26 global->SetHiddenValue(v8::String::New("modules"), v8::Object::New()); | 26 global->SetHiddenValue(v8::String::New("modules"), v8::Object::New()); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ModuleSystem::~ModuleSystem() { | 29 ModuleSystem::~ModuleSystem() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 ModuleSystem::NativesEnabledScope::NativesEnabledScope( | |
| 33 ModuleSystem* module_system) | |
| 34 : module_system_(module_system) { | |
| 35 module_system_->set_natives_enabled(true); | |
|
not at google - send to devlin
2012/03/25 22:42:58
This won't work with nested "natives enabled" scop
koz (OOO until 15th September)
2012/03/26 01:36:09
Good point. I've made it a counter and removed the
| |
| 36 } | |
| 37 | |
| 38 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { | |
| 39 module_system_->set_natives_enabled(false); | |
| 40 } | |
| 41 | |
| 32 void ModuleSystem::Require(const std::string& module_name) { | 42 void ModuleSystem::Require(const std::string& module_name) { |
| 33 v8::HandleScope handle_scope; | 43 v8::HandleScope handle_scope; |
| 34 RequireForJsInner(v8::String::New(module_name.c_str())); | 44 RequireForJsInner(v8::String::New(module_name.c_str())); |
| 35 } | 45 } |
| 36 | 46 |
| 37 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { | 47 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
| 38 v8::HandleScope handle_scope; | 48 v8::HandleScope handle_scope; |
| 39 v8::Handle<v8::String> module_name = args[0]->ToString(); | 49 v8::Handle<v8::String> module_name = args[0]->ToString(); |
| 40 return handle_scope.Close(RequireForJsInner(module_name)); | 50 return handle_scope.Close(RequireForJsInner(module_name)); |
| 41 } | 51 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 84 v8::Local<v8::String> property, const v8::AccessorInfo& info) { | 94 v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 85 CHECK(!info.Data().IsEmpty()); | 95 CHECK(!info.Data().IsEmpty()); |
| 86 CHECK(info.Data()->IsObject()); | 96 CHECK(info.Data()->IsObject()); |
| 87 v8::HandleScope handle_scope; | 97 v8::HandleScope handle_scope; |
| 88 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); | 98 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
| 89 v8::Handle<v8::Value> module_system_value = | 99 v8::Handle<v8::Value> module_system_value = |
| 90 parameters->Get(v8::String::New(kModuleSystem)); | 100 parameters->Get(v8::String::New(kModuleSystem)); |
| 91 ModuleSystem* module_system = static_cast<ModuleSystem*>( | 101 ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 92 v8::Handle<v8::External>::Cast(module_system_value)->Value()); | 102 v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
| 93 | 103 |
| 94 v8::Handle<v8::Object> module = module_system->RequireForJsInner( | 104 v8::Handle<v8::Object> module; |
| 95 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject(); | 105 { |
| 106 NativesEnabledScope scope(module_system); | |
| 107 module = module_system->RequireForJsInner( | |
| 108 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject(); | |
|
not at google - send to devlin
2012/03/25 22:42:58
(see comment above)
koz (OOO until 15th September)
2012/03/26 01:36:09
Done.
| |
| 109 } | |
| 96 | 110 |
| 97 v8::Handle<v8::String> field = | 111 v8::Handle<v8::String> field = |
| 98 parameters->Get(v8::String::New(kModuleField))->ToString(); | 112 parameters->Get(v8::String::New(kModuleField))->ToString(); |
| 99 | 113 |
| 100 return handle_scope.Close(module->Get(field)); | 114 return handle_scope.Close(module->Get(field)); |
| 101 } | 115 } |
| 102 | 116 |
| 103 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, | 117 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 104 const std::string& field, | 118 const std::string& field, |
| 105 const std::string& module_name, | 119 const std::string& module_name, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 149 v8::Handle<v8::String> left = | 163 v8::Handle<v8::String> left = |
| 150 v8::String::New("(function(require, requireNative, exports) {"); | 164 v8::String::New("(function(require, requireNative, exports) {"); |
| 151 v8::Handle<v8::String> right = v8::String::New("\n})"); | 165 v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 152 return handle_scope.Close( | 166 return handle_scope.Close( |
| 153 v8::String::Concat(left, v8::String::Concat(source, right))); | 167 v8::String::Concat(left, v8::String::Concat(source, right))); |
| 154 } | 168 } |
| 155 | 169 |
| 156 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { | 170 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { |
| 157 return v8::ThrowException(v8::String::New(message.c_str())); | 171 return v8::ThrowException(v8::String::New(message.c_str())); |
| 158 } | 172 } |
| OLD | NEW |