| 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_AST_MODULES_H_ | 5 #ifndef V8_AST_MODULES_H_ |
| 6 #define V8_AST_MODULES_H_ | 6 #define V8_AST_MODULES_H_ |
| 7 | 7 |
| 8 #include "src/parsing/scanner.h" // Only for Scanner::Location. |
| 9 #include "src/pending-compilation-error-handler.h" |
| 8 #include "src/zone.h" | 10 #include "src/zone.h" |
| 9 | 11 |
| 10 namespace v8 { | 12 namespace v8 { |
| 11 namespace internal { | 13 namespace internal { |
| 12 | 14 |
| 13 | 15 |
| 14 class AstRawString; | 16 class AstRawString; |
| 15 | 17 |
| 16 | 18 |
| 17 class ModuleDescriptor : public ZoneObject { | 19 class ModuleDescriptor : public ZoneObject { |
| 18 public: | 20 public: |
| 19 // --------------------------------------------------------------------------- | 21 explicit ModuleDescriptor(Zone* zone) |
| 20 // Factory methods. | 22 : exports_(1, zone), imports_(1, zone) {} |
| 21 | 23 |
| 22 static ModuleDescriptor* New(Zone* zone) { | 24 // import x from "foo.js"; |
| 23 return new (zone) ModuleDescriptor(zone); | 25 // import {x} from "foo.js"; |
| 24 } | 26 // import {x as y} from "foo.js"; |
| 27 void AddImport( |
| 28 const AstRawString* import_name, const AstRawString* local_name, |
| 29 const AstRawString* module_request, const Scanner::Location loc, |
| 30 Zone* zone); |
| 25 | 31 |
| 26 // --------------------------------------------------------------------------- | 32 // import * as x from "foo.js"; |
| 27 // Mutators. | 33 void AddStarImport( |
| 34 const AstRawString* local_name, const AstRawString* module_request, |
| 35 const Scanner::Location loc, Zone* zone); |
| 28 | 36 |
| 29 // Add a name to the list of exports. If it already exists, that's an error. | 37 // import "foo.js"; |
| 30 void AddLocalExport(const AstRawString* export_name, | 38 // import {} from "foo.js"; |
| 31 const AstRawString* local_name, Zone* zone, bool* ok); | 39 // export {} from "foo.js"; (sic!) |
| 40 void AddEmptyImport( |
| 41 const AstRawString* module_request, const Scanner::Location loc, |
| 42 Zone* zone); |
| 32 | 43 |
| 33 // Add module_specifier to the list of requested modules, | 44 // export {x}; |
| 34 // if not already present. | 45 // export {x as y}; |
| 35 void AddModuleRequest(const AstRawString* module_specifier, Zone* zone); | 46 // export VariableStatement |
| 47 // export Declaration |
| 48 // export default ... |
| 49 void AddExport( |
| 50 const AstRawString* local_name, const AstRawString* export_name, |
| 51 const Scanner::Location loc, Zone* zone); |
| 36 | 52 |
| 37 // Assign an index. | 53 // export {x} from "foo.js"; |
| 38 void Allocate(int index) { | 54 // export {x as y} from "foo.js"; |
| 39 DCHECK_EQ(-1, index_); | 55 void AddExport( |
| 40 index_ = index; | 56 const AstRawString* export_name, const AstRawString* import_name, |
| 41 } | 57 const AstRawString* module_request, const Scanner::Location loc, |
| 58 Zone* zone); |
| 42 | 59 |
| 43 // --------------------------------------------------------------------------- | 60 // export * from "foo.js"; |
| 44 // Accessors. | 61 void AddStarExport( |
| 62 const AstRawString* module_request, const Scanner::Location loc, |
| 63 Zone* zone); |
| 45 | 64 |
| 46 int Length() { | 65 // Check if module is well-formed and report error if not. |
| 47 ZoneHashMap* exports = exports_; | 66 bool Validate( |
| 48 return exports ? exports->occupancy() : 0; | 67 Scope* module_scope, PendingCompilationErrorHandler* error_handler, |
| 49 } | 68 Zone* zone) const; |
| 50 | 69 |
| 51 // The context slot in the hosting script context pointing to this module. | |
| 52 int Index() { | |
| 53 return index_; | |
| 54 } | |
| 55 | 70 |
| 56 const AstRawString* LookupLocalExport(const AstRawString* export_name, | 71 private: |
| 57 Zone* zone); | 72 struct ModuleEntry : public ZoneObject { |
| 73 const Scanner::Location location; |
| 74 const AstRawString* export_name; |
| 75 const AstRawString* local_name; |
| 76 const AstRawString* import_name; |
| 77 const AstRawString* module_request; |
| 58 | 78 |
| 59 const ZoneList<const AstRawString*>& requested_modules() const { | 79 explicit ModuleEntry(Scanner::Location loc) |
| 60 return requested_modules_; | 80 : location(loc), |
| 61 } | 81 export_name(nullptr), |
| 62 | 82 local_name(nullptr), |
| 63 // --------------------------------------------------------------------------- | 83 import_name(nullptr), |
| 64 // Iterators. | 84 module_request(nullptr) {} |
| 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 }; | 85 }; |
| 91 | 86 |
| 92 Iterator iterator() const { return Iterator(this->exports_); } | 87 ZoneList<const ModuleEntry*> exports_; |
| 93 | 88 ZoneList<const ModuleEntry*> imports_; |
| 94 // --------------------------------------------------------------------------- | |
| 95 // Implementation. | |
| 96 private: | |
| 97 explicit ModuleDescriptor(Zone* zone) | |
| 98 : exports_(NULL), requested_modules_(1, zone), index_(-1) {} | |
| 99 | |
| 100 ZoneHashMap* exports_; // Module exports and their types (allocated lazily) | |
| 101 ZoneList<const AstRawString*> requested_modules_; | |
| 102 int index_; | |
| 103 }; | 89 }; |
| 104 | 90 |
| 105 } // namespace internal | 91 } // namespace internal |
| 106 } // namespace v8 | 92 } // namespace v8 |
| 107 | 93 |
| 108 #endif // V8_AST_MODULES_H_ | 94 #endif // V8_AST_MODULES_H_ |
| OLD | NEW |