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

Side by Side Diff: src/ast/modules.cc

Issue 2353403003: [modules] Support exporting a local variable under multiple export names. (Closed)
Patch Set: Comments Created 4 years, 2 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
« no previous file with comments | « src/ast/modules.h ('k') | src/ast/scopeinfo.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project 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 "src/ast/modules.h" 5 #include "src/ast/modules.h"
6 #include "src/ast/ast-value-factory.h" 6 #include "src/ast/ast-value-factory.h"
7 #include "src/ast/scopes.h" 7 #include "src/ast/scopes.h"
8 8
9 namespace v8 { 9 namespace v8 {
10 namespace internal { 10 namespace internal {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 result->export_name = FromStringOrUndefined( 103 result->export_name = FromStringOrUndefined(
104 isolate, avfactory, handle(entry->export_name(), isolate)); 104 isolate, avfactory, handle(entry->export_name(), isolate));
105 result->local_name = FromStringOrUndefined( 105 result->local_name = FromStringOrUndefined(
106 isolate, avfactory, handle(entry->local_name(), isolate)); 106 isolate, avfactory, handle(entry->local_name(), isolate));
107 result->import_name = FromStringOrUndefined( 107 result->import_name = FromStringOrUndefined(
108 isolate, avfactory, handle(entry->import_name(), isolate)); 108 isolate, avfactory, handle(entry->import_name(), isolate));
109 result->module_request = Smi::cast(entry->module_request())->value(); 109 result->module_request = Smi::cast(entry->module_request())->value();
110 return result; 110 return result;
111 } 111 }
112 112
113 Handle<FixedArray> ModuleDescriptor::SerializeRegularExports(Isolate* isolate,
114 Zone* zone) const {
115 // We serialize regular exports in a way that lets us later iterate over their
116 // local names and for each local name immediately access all its export
117 // names. (Regular exports have neither import name nor module request.)
118
119 ZoneVector<Handle<Object>> data(zone);
120 data.reserve(2 * regular_exports_.size());
121
122 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) {
123 // Find out how many export names this local name has.
124 auto next = it;
125 int size = 0;
126 do {
127 ++next;
128 ++size;
129 } while (next != regular_exports_.end() && next->first == it->first);
130
131 Handle<FixedArray> export_names = isolate->factory()->NewFixedArray(size);
132 data.push_back(it->second->local_name->string());
133 data.push_back(export_names);
134
135 // Collect the export names.
136 int i = 0;
137 for (; it != next; ++it) {
138 export_names->set(i++, *it->second->export_name->string());
139 }
140 DCHECK_EQ(i, size);
141
142 // Continue with the next distinct key.
143 DCHECK(it == next);
144 }
145
146 // We cannot create the FixedArray earlier because we only now know the
147 // precise size (the number of unique keys in regular_exports).
148 int size = static_cast<int>(data.size());
149 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size);
150 for (int i = 0; i < size; ++i) {
151 result->set(i, *data[i]);
152 }
153 return result;
154 }
155
156 void ModuleDescriptor::DeserializeRegularExports(Isolate* isolate,
157 AstValueFactory* avfactory,
158 Handle<FixedArray> data) {
159 for (int i = 0, length_i = data->length(); i < length_i;) {
160 Handle<String> local_name(String::cast(data->get(i++)), isolate);
161 Handle<FixedArray> export_names(FixedArray::cast(data->get(i++)), isolate);
162
163 for (int j = 0, length_j = export_names->length(); j < length_j; ++j) {
164 Handle<String> export_name(String::cast(export_names->get(j)), isolate);
165
166 Entry* entry =
167 new (avfactory->zone()) Entry(Scanner::Location::invalid());
168 entry->local_name = avfactory->GetString(local_name);
169 entry->export_name = avfactory->GetString(export_name);
170
171 AddRegularExport(entry);
172 }
173 }
174 }
175
113 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { 176 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) {
114 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { 177 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) {
115 Entry* entry = it->second; 178 Entry* entry = it->second;
116 DCHECK_NOT_NULL(entry->local_name); 179 DCHECK_NOT_NULL(entry->local_name);
117 auto import = regular_imports_.find(entry->local_name); 180 auto import = regular_imports_.find(entry->local_name);
118 if (import != regular_imports_.end()) { 181 if (import != regular_imports_.end()) {
119 // Found an indirect export. Patch export entry and move it from regular 182 // Found an indirect export. Patch export entry and move it from regular
120 // to special. 183 // to special.
121 DCHECK_NULL(entry->import_name); 184 DCHECK_NULL(entry->import_name);
122 DCHECK_LT(entry->module_request, 0); 185 DCHECK_LT(entry->module_request, 0);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 return false; 262 return false;
200 } 263 }
201 } 264 }
202 265
203 MakeIndirectExportsExplicit(zone); 266 MakeIndirectExportsExplicit(zone);
204 return true; 267 return true;
205 } 268 }
206 269
207 } // namespace internal 270 } // namespace internal
208 } // namespace v8 271 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/modules.h ('k') | src/ast/scopeinfo.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698