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 | |
7 #include "src/ast/ast-value-factory.h" | 6 #include "src/ast/ast-value-factory.h" |
| 7 #include "src/ast/scopes.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
11 | 11 |
12 | 12 |
13 void ModuleDescriptor::AddLocalExport(const AstRawString* export_name, | 13 void ModuleDescriptor::AddImport( |
14 const AstRawString* local_name, | 14 const AstRawString* import_name, const AstRawString* local_name, |
15 Zone* zone, bool* ok) { | 15 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
16 void* key = const_cast<AstRawString*>(export_name); | 16 DCHECK_NOT_NULL(import_name); |
17 | 17 DCHECK_NOT_NULL(local_name); |
18 ZoneAllocationPolicy allocator(zone); | 18 DCHECK_NOT_NULL(module_request); |
19 | 19 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
20 if (exports_ == nullptr) { | 20 entry->local_name = local_name; |
21 exports_ = new (zone->New(sizeof(ZoneHashMap))) | 21 entry->import_name = import_name; |
22 ZoneHashMap(ZoneHashMap::PointersMatch, | 22 entry->module_request = module_request; |
23 ZoneHashMap::kDefaultHashMapCapacity, allocator); | 23 imports_.Add(entry, zone); |
24 } | |
25 | |
26 ZoneHashMap::Entry* p = | |
27 exports_->LookupOrInsert(key, export_name->hash(), allocator); | |
28 DCHECK_NOT_NULL(p); | |
29 if (p->value != nullptr) { | |
30 // Duplicate export. | |
31 *ok = false; | |
32 return; | |
33 } | |
34 | |
35 p->value = const_cast<AstRawString*>(local_name); | |
36 } | 24 } |
37 | 25 |
38 | 26 |
39 void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier, | 27 void ModuleDescriptor::AddStarImport( |
40 Zone* zone) { | 28 const AstRawString* local_name, const AstRawString* module_request, |
41 // TODO(adamk): Avoid this O(N) operation on each insert by storing | 29 Scanner::Location loc, Zone* zone) { |
42 // a HashMap, or by de-duping after parsing. | 30 DCHECK_NOT_NULL(local_name); |
43 if (requested_modules_.Contains(module_specifier)) return; | 31 DCHECK_NOT_NULL(module_request); |
44 requested_modules_.Add(module_specifier, zone); | 32 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
| 33 entry->local_name = local_name; |
| 34 entry->module_request = module_request; |
| 35 imports_.Add(entry, zone); |
45 } | 36 } |
46 | 37 |
47 | 38 |
48 const AstRawString* ModuleDescriptor::LookupLocalExport( | 39 void ModuleDescriptor::AddEmptyImport( |
49 const AstRawString* export_name, Zone* zone) { | 40 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
50 if (exports_ == nullptr) return nullptr; | 41 DCHECK_NOT_NULL(module_request); |
51 ZoneHashMap::Entry* entry = exports_->Lookup( | 42 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
52 const_cast<AstRawString*>(export_name), export_name->hash()); | 43 entry->module_request = module_request; |
53 if (entry == nullptr) return nullptr; | 44 imports_.Add(entry, zone); |
54 DCHECK_NOT_NULL(entry->value); | 45 } |
55 return static_cast<const AstRawString*>(entry->value); | 46 |
| 47 |
| 48 void ModuleDescriptor::AddExport( |
| 49 const AstRawString* local_name, const AstRawString* export_name, |
| 50 Scanner::Location loc, Zone* zone) { |
| 51 DCHECK_NOT_NULL(local_name); |
| 52 DCHECK_NOT_NULL(export_name); |
| 53 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
| 54 entry->export_name = export_name; |
| 55 entry->local_name = local_name; |
| 56 exports_.Add(entry, zone); |
| 57 } |
| 58 |
| 59 |
| 60 void ModuleDescriptor::AddExport( |
| 61 const AstRawString* import_name, const AstRawString* export_name, |
| 62 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 63 DCHECK_NOT_NULL(import_name); |
| 64 DCHECK_NOT_NULL(export_name); |
| 65 DCHECK_NOT_NULL(module_request); |
| 66 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
| 67 entry->export_name = export_name; |
| 68 entry->import_name = import_name; |
| 69 entry->module_request = module_request; |
| 70 exports_.Add(entry, zone); |
| 71 } |
| 72 |
| 73 |
| 74 void ModuleDescriptor::AddStarExport( |
| 75 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 76 DCHECK_NOT_NULL(module_request); |
| 77 ModuleEntry* entry = new (zone) ModuleEntry(loc); |
| 78 entry->module_request = module_request; |
| 79 exports_.Add(entry, zone); |
| 80 } |
| 81 |
| 82 |
| 83 bool ModuleDescriptor::Validate( |
| 84 Scope* module_scope, PendingCompilationErrorHandler* error_handler, |
| 85 Zone* zone) const { |
| 86 DCHECK(module_scope->is_module_scope()); |
| 87 DCHECK_EQ(this, module_scope->module()); |
| 88 DCHECK_NOT_NULL(error_handler); |
| 89 |
| 90 // Report error iff there are duplicate exports. |
| 91 { |
| 92 ZoneAllocationPolicy allocator(zone); |
| 93 ZoneHashMap* export_names = new (zone->New(sizeof(ZoneHashMap))) |
| 94 ZoneHashMap(ZoneHashMap::PointersMatch, |
| 95 ZoneHashMap::kDefaultHashMapCapacity, allocator); |
| 96 for (auto entry : exports_) { |
| 97 if (entry->export_name == nullptr) continue; |
| 98 AstRawString* key = const_cast<AstRawString*>(entry->export_name); |
| 99 ZoneHashMap::Entry* p = |
| 100 export_names->LookupOrInsert(key, key->hash(), allocator); |
| 101 DCHECK_NOT_NULL(p); |
| 102 if (p->value != nullptr) { |
| 103 error_handler->ReportMessageAt( |
| 104 entry->location.beg_pos, entry->location.end_pos, |
| 105 MessageTemplate::kDuplicateExport, entry->export_name); |
| 106 return false; |
| 107 } |
| 108 p->value = key; // Anything but nullptr. |
| 109 } |
| 110 } |
| 111 |
| 112 // Report error iff there are exports of non-existent local names. |
| 113 for (auto entry : exports_) { |
| 114 if (entry->local_name == nullptr) continue; |
| 115 if (module_scope->LookupLocal(entry->local_name) == nullptr) { |
| 116 error_handler->ReportMessageAt( |
| 117 entry->location.beg_pos, entry->location.end_pos, |
| 118 MessageTemplate::kModuleExportUndefined, entry->local_name); |
| 119 return false; |
| 120 } |
| 121 } |
| 122 |
| 123 return true; |
56 } | 124 } |
57 } // namespace internal | 125 } // namespace internal |
58 } // namespace v8 | 126 } // namespace v8 |
OLD | NEW |