OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 *ok = !IsModule(); | 79 *ok = !IsModule(); |
80 if (*ok) Chase()->flags_ |= VALUE; | 80 if (*ok) Chase()->flags_ |= VALUE; |
81 } | 81 } |
82 | 82 |
83 // Determine this interface to be a module interface. | 83 // Determine this interface to be a module interface. |
84 void MakeModule(bool* ok) { | 84 void MakeModule(bool* ok) { |
85 *ok = !IsValue(); | 85 *ok = !IsValue(); |
86 if (*ok) Chase()->flags_ |= MODULE; | 86 if (*ok) Chase()->flags_ |= MODULE; |
87 } | 87 } |
88 | 88 |
| 89 // Set associated instance object. |
| 90 void MakeSingleton(Handle<JSModule> instance, bool* ok) { |
| 91 *ok = IsModule() && Chase()->instance_.is_null(); |
| 92 if (*ok) Chase()->instance_ = instance; |
| 93 } |
| 94 |
89 // Do not allow any further refinements, directly or through unification. | 95 // Do not allow any further refinements, directly or through unification. |
90 void Freeze(bool* ok) { | 96 void Freeze(bool* ok) { |
91 *ok = IsValue() || IsModule(); | 97 *ok = IsValue() || IsModule(); |
92 if (*ok) Chase()->flags_ |= FROZEN; | 98 if (*ok) Chase()->flags_ |= FROZEN; |
93 } | 99 } |
94 | 100 |
95 // --------------------------------------------------------------------------- | 101 // --------------------------------------------------------------------------- |
96 // Accessors. | 102 // Accessors. |
97 | 103 |
98 // Look up an exported name. Returns NULL if not (yet) defined. | |
99 Interface* Lookup(Handle<String> name); | |
100 | |
101 // Check whether this is still a fully undetermined type. | 104 // Check whether this is still a fully undetermined type. |
102 bool IsUnknown() { return Chase()->flags_ == NONE; } | 105 bool IsUnknown() { return Chase()->flags_ == NONE; } |
103 | 106 |
104 // Check whether this is a value type. | 107 // Check whether this is a value type. |
105 bool IsValue() { return Chase()->flags_ & VALUE; } | 108 bool IsValue() { return Chase()->flags_ & VALUE; } |
106 | 109 |
107 // Check whether this is a module type. | 110 // Check whether this is a module type. |
108 bool IsModule() { return Chase()->flags_ & MODULE; } | 111 bool IsModule() { return Chase()->flags_ & MODULE; } |
109 | 112 |
110 // Check whether this is closed (i.e. fully determined). | 113 // Check whether this is closed (i.e. fully determined). |
111 bool IsFrozen() { return Chase()->flags_ & FROZEN; } | 114 bool IsFrozen() { return Chase()->flags_ & FROZEN; } |
112 | 115 |
| 116 Handle<JSModule> Instance() { return Chase()->instance_; } |
| 117 |
| 118 // Look up an exported name. Returns NULL if not (yet) defined. |
| 119 Interface* Lookup(Handle<String> name); |
| 120 |
| 121 // --------------------------------------------------------------------------- |
| 122 // Iterators. |
| 123 |
| 124 // Use like: |
| 125 // for (auto it = interface->iterator(); !it.done(); it.Advance()) { |
| 126 // ... it.name() ... it.interface() ... |
| 127 // } |
| 128 class Iterator { |
| 129 public: |
| 130 bool done() const { return entry_ == NULL; } |
| 131 Handle<String> name() const { |
| 132 ASSERT(!done()); |
| 133 return Handle<String>(*static_cast<String**>(entry_->key)); |
| 134 } |
| 135 Interface* interface() const { |
| 136 ASSERT(!done()); |
| 137 return static_cast<Interface*>(entry_->value); |
| 138 } |
| 139 void Advance() { entry_ = exports_->Next(entry_); } |
| 140 |
| 141 private: |
| 142 friend class Interface; |
| 143 explicit Iterator(const ZoneHashMap* exports) |
| 144 : exports_(exports), entry_(exports ? exports->Start() : NULL) {} |
| 145 |
| 146 const ZoneHashMap* exports_; |
| 147 ZoneHashMap::Entry* entry_; |
| 148 }; |
| 149 |
| 150 Iterator iterator() const { return Iterator(this->exports_); } |
| 151 |
113 // --------------------------------------------------------------------------- | 152 // --------------------------------------------------------------------------- |
114 // Debugging. | 153 // Debugging. |
115 #ifdef DEBUG | 154 #ifdef DEBUG |
116 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 155 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
117 #endif | 156 #endif |
118 | 157 |
119 // --------------------------------------------------------------------------- | 158 // --------------------------------------------------------------------------- |
120 // Implementation. | 159 // Implementation. |
121 private: | 160 private: |
122 enum Flags { // All flags are monotonic | 161 enum Flags { // All flags are monotonic |
123 NONE = 0, | 162 NONE = 0, |
124 VALUE = 1, // This type describes a value | 163 VALUE = 1, // This type describes a value |
125 MODULE = 2, // This type describes a module | 164 MODULE = 2, // This type describes a module |
126 FROZEN = 4 // This type is fully determined | 165 FROZEN = 4 // This type is fully determined |
127 }; | 166 }; |
128 | 167 |
129 int flags_; | 168 int flags_; |
130 Interface* forward_; // Unification link | 169 Interface* forward_; // Unification link |
131 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) | 170 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) |
| 171 Handle<JSModule> instance_; |
132 | 172 |
133 explicit Interface(int flags) | 173 explicit Interface(int flags) |
134 : flags_(flags), | 174 : flags_(flags), |
135 forward_(NULL), | 175 forward_(NULL), |
136 exports_(NULL) { | 176 exports_(NULL) { |
137 #ifdef DEBUG | 177 #ifdef DEBUG |
138 if (FLAG_print_interface_details) | 178 if (FLAG_print_interface_details) |
139 PrintF("# Creating %p\n", static_cast<void*>(this)); | 179 PrintF("# Creating %p\n", static_cast<void*>(this)); |
140 #endif | 180 #endif |
141 } | 181 } |
142 | 182 |
143 Interface* Chase() { | 183 Interface* Chase() { |
144 Interface* result = this; | 184 Interface* result = this; |
145 while (result->forward_ != NULL) result = result->forward_; | 185 while (result->forward_ != NULL) result = result->forward_; |
146 if (result != this) forward_ = result; // On-the-fly path compression. | 186 if (result != this) forward_ = result; // On-the-fly path compression. |
147 return result; | 187 return result; |
148 } | 188 } |
149 | 189 |
150 void DoAdd(void* name, uint32_t hash, Interface* interface, bool* ok); | 190 void DoAdd(void* name, uint32_t hash, Interface* interface, bool* ok); |
151 void DoUnify(Interface* that, bool* ok); | 191 void DoUnify(Interface* that, bool* ok); |
152 }; | 192 }; |
153 | 193 |
154 } } // namespace v8::internal | 194 } } // namespace v8::internal |
155 | 195 |
156 #endif // V8_INTERFACE_H_ | 196 #endif // V8_INTERFACE_H_ |
OLD | NEW |