Chromium Code Reviews| 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::AddEmptyImport( |
| 14 const AstRawString* local_name, | 14 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 15 Zone* zone, bool* ok) { | 15 DCHECK_NOT_NULL(module_request); |
| 16 void* key = const_cast<AstRawString*>(export_name); | 16 ModuleEntry* entry = new (zone->New(sizeof(ModuleEntry))) ModuleEntry(loc); |
|
adamk
2016/07/13 18:38:21
If you make ModuleEntry a ZoneObject this gets cle
neis
2016/07/14 10:28:23
Done.
| |
| 17 | 17 entry->module_request = module_request; |
| 18 ZoneAllocationPolicy allocator(zone); | 18 imports_.Add(entry, zone); |
| 19 | |
| 20 if (exports_ == nullptr) { | |
| 21 exports_ = new (zone->New(sizeof(ZoneHashMap))) | |
| 22 ZoneHashMap(ZoneHashMap::PointersMatch, | |
| 23 ZoneHashMap::kDefaultHashMapCapacity, allocator); | |
| 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 } | 19 } |
| 37 | 20 |
| 38 | 21 |
| 39 void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier, | 22 void ModuleDescriptor::AddNonStarImport( |
| 40 Zone* zone) { | 23 const AstRawString* import_name, const AstRawString* local_name, |
| 41 // TODO(adamk): Avoid this O(N) operation on each insert by storing | 24 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 42 // a HashMap, or by de-duping after parsing. | 25 DCHECK_NOT_NULL(import_name); |
| 43 if (requested_modules_.Contains(module_specifier)) return; | 26 DCHECK_NOT_NULL(local_name); |
| 44 requested_modules_.Add(module_specifier, zone); | 27 DCHECK_NOT_NULL(module_request); |
| 28 ModuleEntry* entry = new (zone->New(sizeof(ModuleEntry))) ModuleEntry(loc); | |
| 29 entry->local_name = local_name; | |
| 30 entry->import_name = import_name; | |
| 31 entry->module_request = module_request; | |
| 32 imports_.Add(entry, zone); | |
| 45 } | 33 } |
| 46 | 34 |
| 47 | 35 |
| 48 const AstRawString* ModuleDescriptor::LookupLocalExport( | 36 void ModuleDescriptor::AddStarImport( |
| 49 const AstRawString* export_name, Zone* zone) { | 37 const AstRawString* local_name, const AstRawString* module_request, |
| 50 if (exports_ == nullptr) return nullptr; | 38 Scanner::Location loc, Zone* zone) { |
| 51 ZoneHashMap::Entry* entry = exports_->Lookup( | 39 DCHECK_NOT_NULL(local_name); |
| 52 const_cast<AstRawString*>(export_name), export_name->hash()); | 40 DCHECK_NOT_NULL(module_request); |
| 53 if (entry == nullptr) return nullptr; | 41 ModuleEntry* entry = new (zone->New(sizeof(ModuleEntry))) ModuleEntry(loc); |
| 54 DCHECK_NOT_NULL(entry->value); | 42 entry->local_name = local_name; |
| 55 return static_cast<const AstRawString*>(entry->value); | 43 entry->module_request = module_request; |
| 44 imports_.Add(entry, zone); | |
| 45 } | |
| 46 | |
| 47 | |
| 48 void ModuleDescriptor::AddNonStarExport( | |
| 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->New(sizeof(ModuleEntry))) 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::AddNonStarExport( | |
| 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->New(sizeof(ModuleEntry))) 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->New(sizeof(ModuleEntry))) 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& it : exports_) { | |
|
adamk
2016/07/13 18:38:21
"it" seems an odd name for this, as it's not an it
neis
2016/07/14 10:28:23
Thanks, I forgot to update this when the data stru
| |
| 97 if (it->export_name == nullptr) continue; | |
| 98 AstRawString* key = const_cast<AstRawString*>(it->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 it->location.beg_pos, it->location.end_pos, | |
| 105 MessageTemplate::kDuplicateExport, it->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& it : exports_) { | |
| 114 if (it->local_name == nullptr) continue; | |
| 115 if (module_scope->LookupLocal(it->local_name) == nullptr) { | |
| 116 error_handler->ReportMessageAt( | |
| 117 it->location.beg_pos, it->location.end_pos, | |
| 118 MessageTemplate::kModuleExportUndefined, it->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 |