OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERFACE_H_ | 5 #ifndef V8_MODULES_H_ |
6 #define V8_INTERFACE_H_ | 6 #define V8_MODULES_H_ |
7 | 7 |
8 #include "src/zone.h" | 8 #include "src/zone.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 | 13 |
14 class AstRawString; | 14 class AstRawString; |
15 | 15 |
16 | 16 |
17 // This class represents the interface of a module: a set of exported names. | 17 class ModuleDescriptor : public ZoneObject { |
18 // | |
19 // TODO(adamk): Rename this to ModuleRecord, ModuleDescriptor, or similar. | |
20 class Interface : public ZoneObject { | |
21 public: | 18 public: |
22 // --------------------------------------------------------------------------- | 19 // --------------------------------------------------------------------------- |
23 // Factory methods. | 20 // Factory methods. |
24 | 21 |
25 static Interface* New(Zone* zone) { return new (zone) Interface(); } | 22 static ModuleDescriptor* New(Zone* zone) { |
| 23 return new (zone) ModuleDescriptor(); |
| 24 } |
26 | 25 |
27 // --------------------------------------------------------------------------- | 26 // --------------------------------------------------------------------------- |
28 // Mutators. | 27 // Mutators. |
29 | 28 |
30 // Add a name to the list of exports. If it already exists, or this interface | 29 // Add a name to the list of exports. If it already exists, or this descriptor |
31 // is frozen, that's an error. | 30 // is frozen, that's an error. |
32 void Add(const AstRawString* name, Zone* zone, bool* ok); | 31 void Add(const AstRawString* name, Zone* zone, bool* ok); |
33 | 32 |
34 // Do not allow any further refinements, directly or through unification. | 33 // Do not allow any further refinements, directly or through unification. |
35 void Freeze() { frozen_ = true; } | 34 void Freeze() { frozen_ = true; } |
36 | 35 |
37 // Assign an index. | 36 // Assign an index. |
38 void Allocate(int index) { | 37 void Allocate(int index) { |
39 DCHECK(IsFrozen() && index_ == -1); | 38 DCHECK(IsFrozen() && index_ == -1); |
40 index_ = index; | 39 index_ = index; |
(...skipping 14 matching lines...) Expand all Loading... |
55 // The context slot in the hosting script context pointing to this module. | 54 // The context slot in the hosting script context pointing to this module. |
56 int Index() { | 55 int Index() { |
57 DCHECK(IsFrozen()); | 56 DCHECK(IsFrozen()); |
58 return index_; | 57 return index_; |
59 } | 58 } |
60 | 59 |
61 // --------------------------------------------------------------------------- | 60 // --------------------------------------------------------------------------- |
62 // Iterators. | 61 // Iterators. |
63 | 62 |
64 // Use like: | 63 // Use like: |
65 // for (auto it = interface->iterator(); !it.done(); it.Advance()) { | 64 // for (auto it = descriptor->iterator(); !it.done(); it.Advance()) { |
66 // ... it.name() ... it.interface() ... | 65 // ... it.name() ... |
67 // } | 66 // } |
68 class Iterator { | 67 class Iterator { |
69 public: | 68 public: |
70 bool done() const { return entry_ == NULL; } | 69 bool done() const { return entry_ == NULL; } |
71 const AstRawString* name() const { | 70 const AstRawString* name() const { |
72 DCHECK(!done()); | 71 DCHECK(!done()); |
73 return static_cast<const AstRawString*>(entry_->key); | 72 return static_cast<const AstRawString*>(entry_->key); |
74 } | 73 } |
75 void Advance() { entry_ = exports_->Next(entry_); } | 74 void Advance() { entry_ = exports_->Next(entry_); } |
76 | 75 |
77 private: | 76 private: |
78 friend class Interface; | 77 friend class ModuleDescriptor; |
79 explicit Iterator(const ZoneHashMap* exports) | 78 explicit Iterator(const ZoneHashMap* exports) |
80 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} | 79 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} |
81 | 80 |
82 const ZoneHashMap* exports_; | 81 const ZoneHashMap* exports_; |
83 ZoneHashMap::Entry* entry_; | 82 ZoneHashMap::Entry* entry_; |
84 }; | 83 }; |
85 | 84 |
86 Iterator iterator() const { return Iterator(this->exports_); } | 85 Iterator iterator() const { return Iterator(this->exports_); } |
87 | 86 |
88 // --------------------------------------------------------------------------- | 87 // --------------------------------------------------------------------------- |
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. | 88 // Implementation. |
96 private: | 89 private: |
97 bool frozen_; | 90 bool frozen_; |
98 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) | 91 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) |
99 int index_; | 92 int index_; |
100 | 93 |
101 Interface() : frozen_(false), exports_(NULL), index_(-1) { | 94 ModuleDescriptor() : 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 }; | 95 }; |
108 | 96 |
109 } } // namespace v8::internal | 97 } } // namespace v8::internal |
110 | 98 |
111 #endif // V8_INTERFACE_H_ | 99 #endif // V8_MODULES_H_ |
OLD | NEW |