ac04e47c4d76c7d9179b6097d72d410a552ed5e6
[platform/upstream/nodejs.git] / deps / v8 / src / modules.h
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_MODULES_H_
6 #define V8_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();
24   }
25
26   // ---------------------------------------------------------------------------
27   // Mutators.
28
29   // Add a name to the list of exports. If it already exists, or this descriptor
30   // is frozen, that's an error.
31   void Add(const AstRawString* name, Zone* zone, bool* ok);
32
33   // Do not allow any further refinements, directly or through unification.
34   void Freeze() { frozen_ = true; }
35
36   // Assign an index.
37   void Allocate(int index) {
38     DCHECK(IsFrozen() && index_ == -1);
39     index_ = index;
40   }
41
42   // ---------------------------------------------------------------------------
43   // Accessors.
44
45   // Check whether this is closed (i.e. fully determined).
46   bool IsFrozen() { return frozen_; }
47
48   int Length() {
49     DCHECK(IsFrozen());
50     ZoneHashMap* exports = exports_;
51     return exports ? exports->occupancy() : 0;
52   }
53
54   // The context slot in the hosting script context pointing to this module.
55   int Index() {
56     DCHECK(IsFrozen());
57     return index_;
58   }
59
60   // ---------------------------------------------------------------------------
61   // Iterators.
62
63   // Use like:
64   //   for (auto it = descriptor->iterator(); !it.done(); it.Advance()) {
65   //     ... it.name() ...
66   //   }
67   class Iterator {
68    public:
69     bool done() const { return entry_ == NULL; }
70     const AstRawString* name() const {
71       DCHECK(!done());
72       return static_cast<const AstRawString*>(entry_->key);
73     }
74     void Advance() { entry_ = exports_->Next(entry_); }
75
76    private:
77     friend class ModuleDescriptor;
78     explicit Iterator(const ZoneHashMap* exports)
79         : exports_(exports), entry_(exports ? exports->Start() : NULL) {}
80
81     const ZoneHashMap* exports_;
82     ZoneHashMap::Entry* entry_;
83   };
84
85   Iterator iterator() const { return Iterator(this->exports_); }
86
87   // ---------------------------------------------------------------------------
88   // Implementation.
89  private:
90   bool frozen_;
91   ZoneHashMap* exports_;   // Module exports and their types (allocated lazily)
92   int index_;
93
94   ModuleDescriptor() : frozen_(false), exports_(NULL), index_(-1) {}
95 };
96
97 } }  // namespace v8::internal
98
99 #endif  // V8_MODULES_H_