blob: 1fdf526cd136145bc38167fb72458adff7b40de8 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
10namespace v8 {
11namespace internal {
12
13
14class AstRawString;
15
16
17class 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 Murdoch097c5b22016-05-18 11:27:45 +010029 // Add a name to the list of exports. If it already exists, that's an error.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030 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 Murdoch4a90d5f2016-03-22 12:00:34 +000037 // Assign an index.
38 void Allocate(int index) {
Ben Murdoch097c5b22016-05-18 11:27:45 +010039 DCHECK_EQ(-1, index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 index_ = index;
41 }
42
43 // ---------------------------------------------------------------------------
44 // Accessors.
45
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046 int Length() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 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 Murdoch4a90d5f2016-03-22 12:00:34 +000053 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 Murdoch097c5b22016-05-18 11:27:45 +010098 : exports_(NULL), requested_modules_(1, zone), index_(-1) {}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 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_