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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 | 48 |
49 void ModuleDescriptor::AddExport( | 49 void ModuleDescriptor::AddExport( |
50 const AstRawString* local_name, const AstRawString* export_name, | 50 const AstRawString* local_name, const AstRawString* export_name, |
51 Scanner::Location loc, Zone* zone) { | 51 Scanner::Location loc, Zone* zone) { |
52 DCHECK_NOT_NULL(local_name); | 52 DCHECK_NOT_NULL(local_name); |
53 DCHECK_NOT_NULL(export_name); | 53 DCHECK_NOT_NULL(export_name); |
54 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 54 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
55 entry->export_name = export_name; | 55 entry->export_name = export_name; |
56 entry->local_name = local_name; | 56 entry->local_name = local_name; |
57 exports_.Add(entry, zone); | 57 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
58 } | 58 } |
59 | 59 |
60 | 60 |
61 void ModuleDescriptor::AddExport( | 61 void ModuleDescriptor::AddExport( |
62 const AstRawString* import_name, const AstRawString* export_name, | 62 const AstRawString* import_name, const AstRawString* export_name, |
63 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 63 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
64 DCHECK_NOT_NULL(import_name); | 64 DCHECK_NOT_NULL(import_name); |
65 DCHECK_NOT_NULL(export_name); | 65 DCHECK_NOT_NULL(export_name); |
66 DCHECK_NOT_NULL(module_request); | 66 DCHECK_NOT_NULL(module_request); |
67 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 67 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
68 entry->export_name = export_name; | 68 entry->export_name = export_name; |
69 entry->import_name = import_name; | 69 entry->import_name = import_name; |
70 entry->module_request = module_request; | 70 entry->module_request = module_request; |
71 exports_.Add(entry, zone); | 71 special_exports_.Add(entry, zone); |
72 } | 72 } |
73 | 73 |
74 | 74 |
75 void ModuleDescriptor::AddStarExport( | 75 void ModuleDescriptor::AddStarExport( |
76 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 76 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
77 DCHECK_NOT_NULL(module_request); | 77 DCHECK_NOT_NULL(module_request); |
78 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 78 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
79 entry->module_request = module_request; | 79 entry->module_request = module_request; |
80 exports_.Add(entry, zone); | 80 special_exports_.Add(entry, zone); |
81 } | 81 } |
82 | 82 |
83 void ModuleDescriptor::MakeIndirectExportsExplicit() { | 83 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { |
84 for (auto entry : exports_) { | 84 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { |
85 if (entry->export_name == nullptr) continue; | 85 ModuleEntry* entry = it->second; |
86 if (entry->import_name != nullptr) continue; | |
87 DCHECK_NOT_NULL(entry->local_name); | 86 DCHECK_NOT_NULL(entry->local_name); |
88 auto it = regular_imports_.find(entry->local_name); | 87 auto import = regular_imports_.find(entry->local_name); |
89 if (it != regular_imports_.end()) { | 88 if (import != regular_imports_.end()) { |
90 // Found an indirect export. | 89 // Found an indirect export. Patch export entry and move it from regular |
91 DCHECK_NOT_NULL(it->second->module_request); | 90 // to special. |
92 DCHECK_NOT_NULL(it->second->import_name); | 91 DCHECK_NULL(entry->import_name); |
93 entry->import_name = it->second->import_name; | 92 DCHECK_NULL(entry->module_request); |
94 entry->module_request = it->second->module_request; | 93 DCHECK_NOT_NULL(import->second->import_name); |
| 94 DCHECK_NOT_NULL(import->second->module_request); |
| 95 entry->import_name = import->second->import_name; |
| 96 entry->module_request = import->second->module_request; |
95 entry->local_name = nullptr; | 97 entry->local_name = nullptr; |
| 98 special_exports_.Add(entry, zone); |
| 99 it = regular_exports_.erase(it); |
| 100 } else { |
| 101 it++; |
96 } | 102 } |
97 } | 103 } |
98 } | 104 } |
99 | 105 |
| 106 const ModuleDescriptor::ModuleEntry* ModuleDescriptor::FindDuplicateExport( |
| 107 Zone* zone) const { |
| 108 ZoneSet<const AstRawString*> export_names(zone); |
| 109 for (const auto& it : regular_exports_) { |
| 110 const ModuleEntry* entry = it.second; |
| 111 DCHECK_NOT_NULL(entry->export_name); |
| 112 if (!export_names.insert(entry->export_name).second) return entry; |
| 113 } |
| 114 for (auto entry : special_exports_) { |
| 115 if (entry->export_name == nullptr) continue; // Star export. |
| 116 if (!export_names.insert(entry->export_name).second) return entry; |
| 117 } |
| 118 return nullptr; |
| 119 } |
| 120 |
100 bool ModuleDescriptor::Validate(ModuleScope* module_scope, | 121 bool ModuleDescriptor::Validate(ModuleScope* module_scope, |
101 PendingCompilationErrorHandler* error_handler, | 122 PendingCompilationErrorHandler* error_handler, |
102 Zone* zone) { | 123 Zone* zone) { |
103 DCHECK_EQ(this, module_scope->module()); | 124 DCHECK_EQ(this, module_scope->module()); |
104 DCHECK_NOT_NULL(error_handler); | 125 DCHECK_NOT_NULL(error_handler); |
105 | 126 |
106 // Report error iff there are duplicate exports. | 127 // Report error iff there are duplicate exports. |
107 { | 128 { |
108 ZoneAllocationPolicy allocator(zone); | 129 const ModuleEntry* entry = FindDuplicateExport(zone); |
109 ZoneHashMap* export_names = new (zone->New(sizeof(ZoneHashMap))) | 130 if (entry != nullptr) { |
110 ZoneHashMap(ZoneHashMap::PointersMatch, | 131 error_handler->ReportMessageAt( |
111 ZoneHashMap::kDefaultHashMapCapacity, allocator); | 132 entry->location.beg_pos, entry->location.end_pos, |
112 for (auto entry : exports_) { | 133 MessageTemplate::kDuplicateExport, entry->export_name); |
113 if (entry->export_name == nullptr) continue; | 134 return false; |
114 AstRawString* key = const_cast<AstRawString*>(entry->export_name); | |
115 ZoneHashMap::Entry* p = | |
116 export_names->LookupOrInsert(key, key->hash(), allocator); | |
117 DCHECK_NOT_NULL(p); | |
118 if (p->value != nullptr) { | |
119 error_handler->ReportMessageAt( | |
120 entry->location.beg_pos, entry->location.end_pos, | |
121 MessageTemplate::kDuplicateExport, entry->export_name); | |
122 return false; | |
123 } | |
124 p->value = key; // Anything but nullptr. | |
125 } | 135 } |
126 } | 136 } |
127 | 137 |
128 // Report error iff there are exports of non-existent local names. | 138 // Report error iff there are exports of non-existent local names. |
129 for (auto entry : exports_) { | 139 for (const auto& it : regular_exports_) { |
130 if (entry->local_name == nullptr) continue; | 140 const ModuleEntry* entry = it.second; |
| 141 DCHECK_NOT_NULL(entry->local_name); |
131 if (module_scope->LookupLocal(entry->local_name) == nullptr) { | 142 if (module_scope->LookupLocal(entry->local_name) == nullptr) { |
132 error_handler->ReportMessageAt( | 143 error_handler->ReportMessageAt( |
133 entry->location.beg_pos, entry->location.end_pos, | 144 entry->location.beg_pos, entry->location.end_pos, |
134 MessageTemplate::kModuleExportUndefined, entry->local_name); | 145 MessageTemplate::kModuleExportUndefined, entry->local_name); |
135 return false; | 146 return false; |
136 } | 147 } |
137 } | 148 } |
138 | 149 |
139 MakeIndirectExportsExplicit(); | 150 MakeIndirectExportsExplicit(zone); |
140 return true; | 151 return true; |
141 } | 152 } |
142 | 153 |
143 } // namespace internal | 154 } // namespace internal |
144 } // namespace v8 | 155 } // namespace v8 |
OLD | NEW |