OLD | NEW |
---|---|
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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 result->cell_index = entry->cell_index(); | 105 result->cell_index = entry->cell_index(); |
106 return result; | 106 return result; |
107 } | 107 } |
108 | 108 |
109 Handle<FixedArray> ModuleDescriptor::SerializeRegularExports(Isolate* isolate, | 109 Handle<FixedArray> ModuleDescriptor::SerializeRegularExports(Isolate* isolate, |
110 Zone* zone) const { | 110 Zone* zone) const { |
111 // We serialize regular exports in a way that lets us later iterate over their | 111 // We serialize regular exports in a way that lets us later iterate over their |
112 // local names and for each local name immediately access all its export | 112 // local names and for each local name immediately access all its export |
113 // names. (Regular exports have neither import name nor module request.) | 113 // names. (Regular exports have neither import name nor module request.) |
114 | 114 |
115 ZoneVector<Handle<Object>> data(zone); | 115 ZoneVector<Handle<Object>> data( |
116 data.reserve(3 * regular_exports_.size()); | 116 ModuleInfo::kRegularExportLength * regular_exports_.size(), zone); |
117 int index = 0; | |
117 | 118 |
118 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { | 119 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { |
119 // Find out how many export names this local name has. | 120 // Find out how many export names this local name has. |
120 auto next = it; | 121 auto next = it; |
121 int size = 0; | 122 int count = 0; |
122 do { | 123 do { |
123 DCHECK_EQ(it->second->local_name, next->second->local_name); | 124 DCHECK_EQ(it->second->local_name, next->second->local_name); |
124 DCHECK_EQ(it->second->cell_index, next->second->cell_index); | 125 DCHECK_EQ(it->second->cell_index, next->second->cell_index); |
125 ++next; | 126 ++next; |
126 ++size; | 127 ++count; |
127 } while (next != regular_exports_.end() && next->first == it->first); | 128 } while (next != regular_exports_.end() && next->first == it->first); |
128 | 129 |
129 Handle<FixedArray> export_names = isolate->factory()->NewFixedArray(size); | 130 Handle<FixedArray> export_names = isolate->factory()->NewFixedArray(count); |
130 data.push_back(it->second->local_name->string()); | 131 data[index + ModuleInfo::kRegularExportLocalNameOffset] = |
131 data.push_back(handle(Smi::FromInt(it->second->cell_index), isolate)); | 132 it->second->local_name->string(); |
132 data.push_back(export_names); | 133 data[index + ModuleInfo::kRegularExportCellIndexOffset] = |
134 handle(Smi::FromInt(it->second->cell_index), isolate); | |
135 data[index + ModuleInfo::kRegularExportExportNamesOffset] = export_names; | |
adamk
2016/11/03 18:20:52
If you don't like the look of this, you could keep
neis
2016/11/04 08:30:29
I prefer the current way.
| |
136 index += ModuleInfo::kRegularExportLength; | |
133 | 137 |
134 // Collect the export names. | 138 // Collect the export names. |
135 int i = 0; | 139 int i = 0; |
136 for (; it != next; ++it) { | 140 for (; it != next; ++it) { |
137 export_names->set(i++, *it->second->export_name->string()); | 141 export_names->set(i++, *it->second->export_name->string()); |
138 } | 142 } |
139 DCHECK_EQ(i, size); | 143 DCHECK_EQ(i, count); |
140 | 144 |
141 // Continue with the next distinct key. | 145 // Continue with the next distinct key. |
142 DCHECK(it == next); | 146 DCHECK(it == next); |
143 } | 147 } |
148 DCHECK_LE(index, data.size()); | |
149 data.resize(index); | |
adamk
2016/11/03 18:20:52
When would index be less then data.size()? I don't
neis
2016/11/04 08:30:29
It's less when there are multiple exports of the s
| |
144 | 150 |
145 // We cannot create the FixedArray earlier because we only now know the | 151 // We cannot create the FixedArray earlier because we only now know the |
146 // precise size (the number of unique keys in regular_exports). | 152 // precise size. |
147 int size = static_cast<int>(data.size()); | 153 Handle<FixedArray> result = isolate->factory()->NewFixedArray(index); |
148 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); | 154 for (int i = 0; i < index; ++i) { |
149 for (int i = 0; i < size; ++i) { | |
150 result->set(i, *data[i]); | 155 result->set(i, *data[i]); |
151 } | 156 } |
152 return result; | 157 return result; |
153 } | 158 } |
154 | 159 |
155 void ModuleDescriptor::DeserializeRegularExports(Isolate* isolate, | 160 void ModuleDescriptor::DeserializeRegularExports( |
156 AstValueFactory* avfactory, | 161 Isolate* isolate, AstValueFactory* avfactory, |
157 Handle<FixedArray> data) { | 162 Handle<ModuleInfo> module_info) { |
158 for (int i = 0, length_i = data->length(); i < length_i;) { | 163 for (int i = 0, count = module_info->RegularExportCount(); i < count; ++i) { |
159 Handle<String> local_name(String::cast(data->get(i++)), isolate); | 164 Handle<String> local_name(module_info->RegularExportLocalName(i), isolate); |
160 int cell_index = Smi::cast(data->get(i++))->value(); | 165 int cell_index = module_info->RegularExportCellIndex(i); |
161 Handle<FixedArray> export_names(FixedArray::cast(data->get(i++)), isolate); | 166 Handle<FixedArray> export_names(module_info->RegularExportExportNames(i), |
167 isolate); | |
162 | 168 |
163 for (int j = 0, length_j = export_names->length(); j < length_j; ++j) { | 169 for (int j = 0, length = export_names->length(); j < length; ++j) { |
164 Handle<String> export_name(String::cast(export_names->get(j)), isolate); | 170 Handle<String> export_name(String::cast(export_names->get(j)), isolate); |
165 | 171 |
166 Entry* entry = | 172 Entry* entry = |
167 new (avfactory->zone()) Entry(Scanner::Location::invalid()); | 173 new (avfactory->zone()) Entry(Scanner::Location::invalid()); |
168 entry->local_name = avfactory->GetString(local_name); | 174 entry->local_name = avfactory->GetString(local_name); |
169 entry->export_name = avfactory->GetString(export_name); | 175 entry->export_name = avfactory->GetString(export_name); |
170 entry->cell_index = cell_index; | 176 entry->cell_index = cell_index; |
171 | 177 |
172 AddRegularExport(entry); | 178 AddRegularExport(entry); |
173 } | 179 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 } | 307 } |
302 } | 308 } |
303 | 309 |
304 MakeIndirectExportsExplicit(zone); | 310 MakeIndirectExportsExplicit(zone); |
305 AssignCellIndices(); | 311 AssignCellIndices(); |
306 return true; | 312 return true; |
307 } | 313 } |
308 | 314 |
309 } // namespace internal | 315 } // namespace internal |
310 } // namespace v8 | 316 } // namespace v8 |
OLD | NEW |