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

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

Issue 9812025: Implement lazy JS module initialisation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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
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 8
9 namespace { 9 namespace {
10 10
11 const char* kLazyObjectSource = "lazy_object_source"; 11 const char* kModuleSystem = "module_system";
12 const char* kLazyObjectName = "lazy_object_name"; 12 const char* kModuleName = "module_name";
13 const char* kLazyObject = "lazy_object"; 13 const char* kModuleField = "module_field";
14
15 v8::Handle<v8::Object> EvaluateLazySource(v8::Handle<v8::Object> object) {
16 v8::HandleScope handle_scope;
17 v8::Handle<v8::Value> source_value =
18 object->GetHiddenValue(v8::String::New(kLazyObjectSource));
19 CHECK(!source_value.IsEmpty());
20 v8::Handle<v8::String> source = v8::Handle<v8::String>::Cast(source_value);
21 v8::Handle<v8::Value> name =
22 object->GetHiddenValue(v8::String::New(kLazyObjectName));
23 CHECK(!name.IsEmpty());
24 CHECK(name->IsString());
25 v8::Handle<v8::Script> script = v8::Script::New(source, name);
26 v8::Handle<v8::Value> result = script->Run();
27 CHECK(result->IsObject());
28 return handle_scope.Close(v8::Handle<v8::Object>::Cast(result));
29 }
30
31 v8::Handle<v8::Value> LazyObjectGetter(
32 const v8::Local<v8::String> property,
33 const v8::AccessorInfo& info) {
34 v8::HandleScope handle_scope;
35 v8::Handle<v8::Object> object = info.Holder();
36 v8::Handle<v8::String> lazy_object_name = v8::String::New(kLazyObject);
37 v8::Handle<v8::Value> lazy_object_value =
38 object->GetHiddenValue(lazy_object_name);
39 CHECK(lazy_object_value.IsEmpty() || lazy_object_value->IsObject());
40 v8::Handle<v8::Object> lazy_object =
41 v8::Handle<v8::Object>::Cast(lazy_object_value);
42 if (lazy_object.IsEmpty()) {
43 lazy_object = EvaluateLazySource(object);
44 object->SetHiddenValue(lazy_object_name, lazy_object);
45 }
46 return handle_scope.Close(lazy_object->Get(property));
47 }
48 14
49 } // namespace 15 } // namespace
50 16
51 ModuleSystem::ModuleSystem(SourceMap* source_map) 17 ModuleSystem::ModuleSystem(SourceMap* source_map)
52 : source_map_(source_map), 18 : source_map_(source_map),
53 natives_enabled_(true) { 19 natives_enabled_(true) {
54 RouteFunction("require", 20 RouteFunction("require",
55 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); 21 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
56 RouteFunction("requireNative", 22 RouteFunction("requireNative",
57 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 23 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 native_handler_map_[name] = 73 native_handler_map_[name] =
108 linked_ptr<NativeHandler>(native_handler.release()); 74 linked_ptr<NativeHandler>(native_handler.release());
109 } 75 }
110 76
111 void ModuleSystem::RunString(const std::string& code, const std::string& name) { 77 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
112 v8::HandleScope handle_scope; 78 v8::HandleScope handle_scope;
113 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); 79 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
114 } 80 }
115 81
116 // static 82 // static
117 v8::Handle<v8::Object> ModuleSystem::CreateLazyObject( 83 v8::Handle<v8::Value> ModuleSystem::GetterRouter(
not at google - send to devlin 2012/03/22 00:35:53 RequireLazyField?
koz (OOO until 15th September) 2012/03/22 01:13:01 Yes, or even better - LazyFieldAccessor.
118 const std::string& source_name, v8::Handle<v8::String> source) { 84 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
85 CHECK(!info.Data().IsEmpty());
86 CHECK(info.Data()->IsObject());
119 v8::HandleScope handle_scope; 87 v8::HandleScope handle_scope;
120 v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); 88 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
121 object_template->SetNamedPropertyHandler(LazyObjectGetter, NULL); 89 v8::Handle<v8::Value> module_system_value =
122 v8::Handle<v8::Object> object = object_template->NewInstance(); 90 parameters->Get(v8::String::New(kModuleSystem));
123 object->SetHiddenValue(v8::String::New(kLazyObjectSource), source); 91 ModuleSystem* module_system = static_cast<ModuleSystem*>(
124 object->SetHiddenValue(v8::String::New(kLazyObjectName), 92 v8::Handle<v8::External>::Cast(module_system_value)->Value());
125 v8::String::New(source_name.c_str()));
126 93
127 return handle_scope.Close(object); 94 v8::Handle<v8::Object> module = module_system->RequireForJsInner(
95 parameters->Get(v8::String::New(kModuleName))->ToString())->ToObject();
96
97 v8::Handle<v8::String> field =
98 parameters->Get(v8::String::New(kModuleField))->ToString();
99
100 return handle_scope.Close(module->Get(field));
101 }
102
103 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
104 const std::string& field,
105 const std::string& module_name,
106 const std::string& module_field) {
107 v8::HandleScope handle_scope;
108 v8::Handle<v8::Object> parameters = v8::Object::New();
109 parameters->Set(v8::String::New(kModuleName),
110 v8::String::New(module_name.c_str()));
111 parameters->Set(v8::String::New(kModuleField),
112 v8::String::New(module_field.c_str()));
113 parameters->Set(v8::String::New(kModuleSystem), v8::External::New(this));
114
115 object->SetAccessor(v8::String::New(field.c_str()),
116 &ModuleSystem::GetterRouter,
117 NULL,
118 parameters);
128 } 119 }
129 120
130 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 121 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
131 v8::Handle<v8::String> name) { 122 v8::Handle<v8::String> name) {
132 v8::HandleScope handle_scope; 123 v8::HandleScope handle_scope;
133 return handle_scope.Close(v8::Script::New(code, name)->Run()); 124 return handle_scope.Close(v8::Script::New(code, name)->Run());
134 } 125 }
135 126
136 v8::Handle<v8::Value> ModuleSystem::GetSource( 127 v8::Handle<v8::Value> ModuleSystem::GetSource(
137 v8::Handle<v8::String> source_name) { 128 v8::Handle<v8::String> source_name) {
(...skipping 20 matching lines...) Expand all
158 v8::Handle<v8::String> left = 149 v8::Handle<v8::String> left =
159 v8::String::New("(function(require, requireNative, exports) {"); 150 v8::String::New("(function(require, requireNative, exports) {");
160 v8::Handle<v8::String> right = v8::String::New("\n})"); 151 v8::Handle<v8::String> right = v8::String::New("\n})");
161 return handle_scope.Close( 152 return handle_scope.Close(
162 v8::String::Concat(left, v8::String::Concat(source, right))); 153 v8::String::Concat(left, v8::String::Concat(source, right)));
163 } 154 }
164 155
165 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { 156 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
166 return v8::ThrowException(v8::String::New(message.c_str())); 157 return v8::ThrowException(v8::String::New(message.c_str()));
167 } 158 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698