OLD | NEW |
| (Empty) |
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_INTERFACE_H_ | |
6 #define V8_INTERFACE_H_ | |
7 | |
8 #include "src/zone.h" | |
9 | |
10 namespace v8 { | |
11 namespace internal { | |
12 | |
13 | |
14 class AstRawString; | |
15 | |
16 | |
17 // This class represents the interface of a module: a set of exported names. | |
18 // | |
19 // TODO(adamk): Rename this to ModuleRecord, ModuleDescriptor, or similar. | |
20 class Interface : public ZoneObject { | |
21 public: | |
22 // --------------------------------------------------------------------------- | |
23 // Factory methods. | |
24 | |
25 static Interface* New(Zone* zone) { return new (zone) Interface(); } | |
26 | |
27 // --------------------------------------------------------------------------- | |
28 // Mutators. | |
29 | |
30 // Add a name to the list of exports. If it already exists, or this interface | |
31 // is frozen, that's an error. | |
32 void Add(const AstRawString* name, Zone* zone, bool* ok); | |
33 | |
34 // Do not allow any further refinements, directly or through unification. | |
35 void Freeze() { frozen_ = true; } | |
36 | |
37 // Assign an index. | |
38 void Allocate(int index) { | |
39 DCHECK(IsFrozen() && index_ == -1); | |
40 index_ = index; | |
41 } | |
42 | |
43 // --------------------------------------------------------------------------- | |
44 // Accessors. | |
45 | |
46 // Check whether this is closed (i.e. fully determined). | |
47 bool IsFrozen() { return frozen_; } | |
48 | |
49 int Length() { | |
50 DCHECK(IsFrozen()); | |
51 ZoneHashMap* exports = exports_; | |
52 return exports ? exports->occupancy() : 0; | |
53 } | |
54 | |
55 // The context slot in the hosting script context pointing to this module. | |
56 int Index() { | |
57 DCHECK(IsFrozen()); | |
58 return index_; | |
59 } | |
60 | |
61 // --------------------------------------------------------------------------- | |
62 // Iterators. | |
63 | |
64 // Use like: | |
65 // for (auto it = interface->iterator(); !it.done(); it.Advance()) { | |
66 // ... it.name() ... it.interface() ... | |
67 // } | |
68 class Iterator { | |
69 public: | |
70 bool done() const { return entry_ == NULL; } | |
71 const AstRawString* name() const { | |
72 DCHECK(!done()); | |
73 return static_cast<const AstRawString*>(entry_->key); | |
74 } | |
75 void Advance() { entry_ = exports_->Next(entry_); } | |
76 | |
77 private: | |
78 friend class Interface; | |
79 explicit Iterator(const ZoneHashMap* exports) | |
80 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} | |
81 | |
82 const ZoneHashMap* exports_; | |
83 ZoneHashMap::Entry* entry_; | |
84 }; | |
85 | |
86 Iterator iterator() const { return Iterator(this->exports_); } | |
87 | |
88 // --------------------------------------------------------------------------- | |
89 // Debugging. | |
90 #ifdef DEBUG | |
91 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | |
92 #endif | |
93 | |
94 // --------------------------------------------------------------------------- | |
95 // Implementation. | |
96 private: | |
97 bool frozen_; | |
98 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) | |
99 int index_; | |
100 | |
101 Interface() : frozen_(false), exports_(NULL), index_(-1) { | |
102 #ifdef DEBUG | |
103 if (FLAG_print_interface_details) | |
104 PrintF("# Creating %p\n", static_cast<void*>(this)); | |
105 #endif | |
106 } | |
107 }; | |
108 | |
109 } } // namespace v8::internal | |
110 | |
111 #endif // V8_INTERFACE_H_ | |
OLD | NEW |