| 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 #include "src/ast/modules.h" | 5 #include "src/ast/modules.h" |
| 6 #include "src/ast/ast-value-factory.h" | 6 #include "src/ast/ast-value-factory.h" |
| 7 #include "src/ast/scopes.h" | 7 #include "src/ast/scopes.h" |
| 8 | 8 |
| 9 namespace v8 { | 9 namespace v8 { |
| 10 namespace internal { | 10 namespace internal { |
| 11 | 11 |
| 12 void ModuleDescriptor::AddImport( | 12 void ModuleDescriptor::AddImport( |
| 13 const AstRawString* import_name, const AstRawString* local_name, | 13 const AstRawString* import_name, const AstRawString* local_name, |
| 14 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 14 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 15 DCHECK_NOT_NULL(import_name); | |
| 16 DCHECK_NOT_NULL(local_name); | |
| 17 DCHECK_NOT_NULL(module_request); | |
| 18 Entry* entry = new (zone) Entry(loc); | 15 Entry* entry = new (zone) Entry(loc); |
| 19 entry->local_name = local_name; | 16 entry->local_name = local_name; |
| 20 entry->import_name = import_name; | 17 entry->import_name = import_name; |
| 21 entry->module_request = module_request; | 18 entry->module_request = module_request; |
| 22 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 19 AddRegularImport(entry); |
| 23 // We don't care if there's already an entry for this local name, as in that | |
| 24 // case we will report an error when declaring the variable. | |
| 25 } | 20 } |
| 26 | 21 |
| 27 | 22 |
| 28 void ModuleDescriptor::AddStarImport( | 23 void ModuleDescriptor::AddStarImport( |
| 29 const AstRawString* local_name, const AstRawString* module_request, | 24 const AstRawString* local_name, const AstRawString* module_request, |
| 30 Scanner::Location loc, Zone* zone) { | 25 Scanner::Location loc, Zone* zone) { |
| 31 DCHECK_NOT_NULL(local_name); | 26 DCHECK_NOT_NULL(local_name); |
| 32 DCHECK_NOT_NULL(module_request); | 27 DCHECK_NOT_NULL(module_request); |
| 33 Entry* entry = new (zone) Entry(loc); | 28 Entry* entry = new (zone) Entry(loc); |
| 34 entry->local_name = local_name; | 29 entry->local_name = local_name; |
| 35 entry->module_request = module_request; | 30 entry->module_request = module_request; |
| 36 special_imports_.Add(entry, zone); | 31 AddSpecialImport(entry, zone); |
| 37 } | 32 } |
| 38 | 33 |
| 39 | 34 |
| 40 void ModuleDescriptor::AddEmptyImport( | 35 void ModuleDescriptor::AddEmptyImport( |
| 41 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 36 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 42 DCHECK_NOT_NULL(module_request); | |
| 43 Entry* entry = new (zone) Entry(loc); | 37 Entry* entry = new (zone) Entry(loc); |
| 44 entry->module_request = module_request; | 38 entry->module_request = module_request; |
| 45 special_imports_.Add(entry, zone); | 39 AddSpecialImport(entry, zone); |
| 46 } | 40 } |
| 47 | 41 |
| 48 | 42 |
| 49 void ModuleDescriptor::AddExport( | 43 void ModuleDescriptor::AddExport( |
| 50 const AstRawString* local_name, const AstRawString* export_name, | 44 const AstRawString* local_name, const AstRawString* export_name, |
| 51 Scanner::Location loc, Zone* zone) { | 45 Scanner::Location loc, Zone* zone) { |
| 52 DCHECK_NOT_NULL(local_name); | |
| 53 DCHECK_NOT_NULL(export_name); | |
| 54 Entry* entry = new (zone) Entry(loc); | 46 Entry* entry = new (zone) Entry(loc); |
| 55 entry->export_name = export_name; | 47 entry->export_name = export_name; |
| 56 entry->local_name = local_name; | 48 entry->local_name = local_name; |
| 57 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 49 AddRegularExport(entry); |
| 58 } | 50 } |
| 59 | 51 |
| 60 | 52 |
| 61 void ModuleDescriptor::AddExport( | 53 void ModuleDescriptor::AddExport( |
| 62 const AstRawString* import_name, const AstRawString* export_name, | 54 const AstRawString* import_name, const AstRawString* export_name, |
| 63 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 55 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 64 DCHECK_NOT_NULL(import_name); | 56 DCHECK_NOT_NULL(import_name); |
| 65 DCHECK_NOT_NULL(export_name); | 57 DCHECK_NOT_NULL(export_name); |
| 66 DCHECK_NOT_NULL(module_request); | |
| 67 Entry* entry = new (zone) Entry(loc); | 58 Entry* entry = new (zone) Entry(loc); |
| 68 entry->export_name = export_name; | 59 entry->export_name = export_name; |
| 69 entry->import_name = import_name; | 60 entry->import_name = import_name; |
| 70 entry->module_request = module_request; | 61 entry->module_request = module_request; |
| 71 special_exports_.Add(entry, zone); | 62 AddSpecialExport(entry, zone); |
| 72 } | 63 } |
| 73 | 64 |
| 74 | 65 |
| 75 void ModuleDescriptor::AddStarExport( | 66 void ModuleDescriptor::AddStarExport( |
| 76 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 67 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
| 77 DCHECK_NOT_NULL(module_request); | |
| 78 Entry* entry = new (zone) Entry(loc); | 68 Entry* entry = new (zone) Entry(loc); |
| 79 entry->module_request = module_request; | 69 entry->module_request = module_request; |
| 80 special_exports_.Add(entry, zone); | 70 AddSpecialExport(entry, zone); |
| 71 } |
| 72 |
| 73 namespace { |
| 74 |
| 75 Handle<Object> ToStringOrUndefined(Isolate* isolate, const AstRawString* s) { |
| 76 return (s == nullptr) |
| 77 ? Handle<Object>::cast(isolate->factory()->undefined_value()) |
| 78 : Handle<Object>::cast(s->string()); |
| 79 } |
| 80 |
| 81 const AstRawString* FromStringOrUndefined(Isolate* isolate, |
| 82 AstValueFactory* avfactory, |
| 83 Handle<Object> object) { |
| 84 if (object->IsUndefined(isolate)) return nullptr; |
| 85 return avfactory->GetString(Handle<String>::cast(object)); |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 Handle<FixedArray> ModuleDescriptor::Entry::Serialize(Isolate* isolate) const { |
| 91 Handle<FixedArray> result = isolate->factory()->NewFixedArray(4); |
| 92 result->set(0, *ToStringOrUndefined(isolate, export_name)); |
| 93 result->set(1, *ToStringOrUndefined(isolate, local_name)); |
| 94 result->set(2, *ToStringOrUndefined(isolate, import_name)); |
| 95 result->set(3, *ToStringOrUndefined(isolate, module_request)); |
| 96 return result; |
| 97 } |
| 98 |
| 99 ModuleDescriptor::Entry* ModuleDescriptor::Entry::Deserialize( |
| 100 Isolate* isolate, AstValueFactory* avfactory, Handle<FixedArray> data) { |
| 101 Entry* entry = new (avfactory->zone()) Entry(Scanner::Location::invalid()); |
| 102 entry->export_name = |
| 103 FromStringOrUndefined(isolate, avfactory, handle(data->get(0), isolate)); |
| 104 entry->local_name = |
| 105 FromStringOrUndefined(isolate, avfactory, handle(data->get(1), isolate)); |
| 106 entry->import_name = |
| 107 FromStringOrUndefined(isolate, avfactory, handle(data->get(2), isolate)); |
| 108 entry->module_request = |
| 109 FromStringOrUndefined(isolate, avfactory, handle(data->get(3), isolate)); |
| 110 return entry; |
| 81 } | 111 } |
| 82 | 112 |
| 83 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { | 113 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { |
| 84 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { | 114 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { |
| 85 Entry* entry = it->second; | 115 Entry* entry = it->second; |
| 86 DCHECK_NOT_NULL(entry->local_name); | 116 DCHECK_NOT_NULL(entry->local_name); |
| 87 auto import = regular_imports_.find(entry->local_name); | 117 auto import = regular_imports_.find(entry->local_name); |
| 88 if (import != regular_imports_.end()) { | 118 if (import != regular_imports_.end()) { |
| 89 // Found an indirect export. Patch export entry and move it from regular | 119 // Found an indirect export. Patch export entry and move it from regular |
| 90 // to special. | 120 // to special. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 return false; | 176 return false; |
| 147 } | 177 } |
| 148 } | 178 } |
| 149 | 179 |
| 150 MakeIndirectExportsExplicit(zone); | 180 MakeIndirectExportsExplicit(zone); |
| 151 return true; | 181 return true; |
| 152 } | 182 } |
| 153 | 183 |
| 154 } // namespace internal | 184 } // namespace internal |
| 155 } // namespace v8 | 185 } // namespace v8 |
| OLD | NEW |