| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |  | 
 | 22 |   static ModuleDescriptor* New(Zone* zone) { | 
 | 23 |     return new (zone) ModuleDescriptor(zone); | 
 | 24 |   } | 
 | 25 |  | 
 | 26 |   // --------------------------------------------------------------------------- | 
 | 27 |   // Mutators. | 
 | 28 |  | 
| Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 29 |   // Add a name to the list of exports. If it already exists, that's an error. | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |  | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 |   // Assign an index. | 
 | 38 |   void Allocate(int index) { | 
| Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 39 |     DCHECK_EQ(-1, index_); | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 |     index_ = index; | 
 | 41 |   } | 
 | 42 |  | 
 | 43 |   // --------------------------------------------------------------------------- | 
 | 44 |   // Accessors. | 
 | 45 |  | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 |   int Length() { | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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. | 
 | 52 |   int Index() { | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 |     return index_; | 
 | 54 |   } | 
 | 55 |  | 
 | 56 |   const AstRawString* LookupLocalExport(const AstRawString* export_name, | 
 | 57 |                                         Zone* zone); | 
 | 58 |  | 
 | 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: | 
 | 72 |     bool done() const { return entry_ == NULL; } | 
 | 73 |     const AstRawString* export_name() const { | 
 | 74 |       DCHECK(!done()); | 
 | 75 |       return static_cast<const AstRawString*>(entry_->key); | 
 | 76 |     } | 
 | 77 |     const AstRawString* local_name() const { | 
 | 78 |       DCHECK(!done()); | 
 | 79 |       return static_cast<const AstRawString*>(entry_->value); | 
 | 80 |     } | 
 | 81 |     void Advance() { entry_ = exports_->Next(entry_); } | 
 | 82 |  | 
 | 83 |    private: | 
 | 84 |     friend class ModuleDescriptor; | 
 | 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 |  | 
 | 92 |   Iterator iterator() const { return Iterator(this->exports_); } | 
 | 93 |  | 
 | 94 |   // --------------------------------------------------------------------------- | 
 | 95 |   // Implementation. | 
 | 96 |  private: | 
 | 97 |   explicit ModuleDescriptor(Zone* zone) | 
| Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 98 |       : exports_(NULL), requested_modules_(1, zone), index_(-1) {} | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 |  | 
| Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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_ |