• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef V8_AST_MODULES_H_
6 #define V8_AST_MODULES_H_
7 
8 #include "src/zone.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 
14 class AstRawString;
15 
16 
17 class ModuleDescriptor : public ZoneObject {
18  public:
19   // ---------------------------------------------------------------------------
20   // Factory methods.
21 
New(Zone * zone)22   static ModuleDescriptor* New(Zone* zone) {
23     return new (zone) ModuleDescriptor(zone);
24   }
25 
26   // ---------------------------------------------------------------------------
27   // Mutators.
28 
29   // Add a name to the list of exports. If it already exists, that's an error.
30   void AddLocalExport(const AstRawString* export_name,
31                       const AstRawString* local_name, Zone* zone, bool* ok);
32 
33   // Add module_specifier to the list of requested modules,
34   // if not already present.
35   void AddModuleRequest(const AstRawString* module_specifier, Zone* zone);
36 
37   // Assign an index.
Allocate(int index)38   void Allocate(int index) {
39     DCHECK_EQ(-1, index_);
40     index_ = index;
41   }
42 
43   // ---------------------------------------------------------------------------
44   // Accessors.
45 
Length()46   int Length() {
47     ZoneHashMap* exports = exports_;
48     return exports ? exports->occupancy() : 0;
49   }
50 
51   // The context slot in the hosting script context pointing to this module.
Index()52   int Index() {
53     return index_;
54   }
55 
56   const AstRawString* LookupLocalExport(const AstRawString* export_name,
57                                         Zone* zone);
58 
requested_modules()59   const ZoneList<const AstRawString*>& requested_modules() const {
60     return requested_modules_;
61   }
62 
63   // ---------------------------------------------------------------------------
64   // Iterators.
65 
66   // Use like:
67   //   for (auto it = descriptor->iterator(); !it.done(); it.Advance()) {
68   //     ... it.name() ...
69   //   }
70   class Iterator {
71    public:
done()72     bool done() const { return entry_ == NULL; }
export_name()73     const AstRawString* export_name() const {
74       DCHECK(!done());
75       return static_cast<const AstRawString*>(entry_->key);
76     }
local_name()77     const AstRawString* local_name() const {
78       DCHECK(!done());
79       return static_cast<const AstRawString*>(entry_->value);
80     }
Advance()81     void Advance() { entry_ = exports_->Next(entry_); }
82 
83    private:
84     friend class ModuleDescriptor;
Iterator(const ZoneHashMap * exports)85     explicit Iterator(const ZoneHashMap* exports)
86         : exports_(exports), entry_(exports ? exports->Start() : NULL) {}
87 
88     const ZoneHashMap* exports_;
89     ZoneHashMap::Entry* entry_;
90   };
91 
iterator()92   Iterator iterator() const { return Iterator(this->exports_); }
93 
94   // ---------------------------------------------------------------------------
95   // Implementation.
96  private:
ModuleDescriptor(Zone * zone)97   explicit ModuleDescriptor(Zone* zone)
98       : exports_(NULL), requested_modules_(1, zone), index_(-1) {}
99 
100   ZoneHashMap* exports_;   // Module exports and their types (allocated lazily)
101   ZoneList<const AstRawString*> requested_modules_;
102   int index_;
103 };
104 
105 }  // namespace internal
106 }  // namespace v8
107 
108 #endif  // V8_AST_MODULES_H_
109