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
3 // found in the LICENSE file.
4 
5 #include "src/ast/modules.h"
6 
7 #include "src/ast/ast-value-factory.h"
8 
9 namespace v8 {
10 namespace internal {
11 
12 
AddLocalExport(const AstRawString * export_name,const AstRawString * local_name,Zone * zone,bool * ok)13 void ModuleDescriptor::AddLocalExport(const AstRawString* export_name,
14                                       const AstRawString* local_name,
15                                       Zone* zone, bool* ok) {
16   DCHECK(!IsFrozen());
17   void* key = const_cast<AstRawString*>(export_name);
18 
19   ZoneAllocationPolicy allocator(zone);
20 
21   if (exports_ == nullptr) {
22     exports_ = new (zone->New(sizeof(ZoneHashMap)))
23         ZoneHashMap(ZoneHashMap::PointersMatch,
24                     ZoneHashMap::kDefaultHashMapCapacity, allocator);
25   }
26 
27   ZoneHashMap::Entry* p =
28       exports_->LookupOrInsert(key, export_name->hash(), allocator);
29   DCHECK_NOT_NULL(p);
30   if (p->value != nullptr) {
31     // Duplicate export.
32     *ok = false;
33     return;
34   }
35 
36   p->value = const_cast<AstRawString*>(local_name);
37 }
38 
39 
AddModuleRequest(const AstRawString * module_specifier,Zone * zone)40 void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier,
41                                         Zone* zone) {
42   // TODO(adamk): Avoid this O(N) operation on each insert by storing
43   // a HashMap, or by de-duping after parsing.
44   if (requested_modules_.Contains(module_specifier)) return;
45   requested_modules_.Add(module_specifier, zone);
46 }
47 
48 
LookupLocalExport(const AstRawString * export_name,Zone * zone)49 const AstRawString* ModuleDescriptor::LookupLocalExport(
50     const AstRawString* export_name, Zone* zone) {
51   if (exports_ == nullptr) return nullptr;
52   ZoneHashMap::Entry* entry = exports_->Lookup(
53       const_cast<AstRawString*>(export_name), export_name->hash());
54   if (entry == nullptr) return nullptr;
55   DCHECK_NOT_NULL(entry->value);
56   return static_cast<const AstRawString*>(entry->value);
57 }
58 }  // namespace internal
59 }  // namespace v8
60