1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_AST_MODULES_H_
#define V8_AST_MODULES_H_
#include "src/parsing/import-assertions.h"
#include "src/parsing/scanner.h" // Only for Scanner::Location.
#include "src/zone/zone-containers.h"
namespace v8 {
namespace internal {
class AstRawString;
class AstRawStringComparer;
class ModuleRequest;
class SourceTextModuleInfo;
class SourceTextModuleInfoEntry;
class PendingCompilationErrorHandler;
class SourceTextModuleDescriptor : public ZoneObject {
public:
explicit SourceTextModuleDescriptor(Zone* zone)
: module_requests_(zone),
special_exports_(zone),
namespace_imports_(zone),
regular_exports_(zone),
regular_imports_(zone) {}
// The following Add* methods are high-level convenience functions for use by
// the parser.
// import x from "foo.js";
// import {x} from "foo.js";
// import {x as y} from "foo.js";
void AddImport(const AstRawString* import_name,
const AstRawString* local_name,
const AstRawString* module_request,
const ImportAssertions* import_assertions,
const Scanner::Location loc,
const Scanner::Location specifier_loc, Zone* zone);
// import * as x from "foo.js";
void AddStarImport(const AstRawString* local_name,
const AstRawString* module_request,
const ImportAssertions* import_assertions,
const Scanner::Location loc,
const Scanner::Location specifier_loc, Zone* zone);
// import "foo.js";
// import {} from "foo.js";
// export {} from "foo.js"; (sic!)
void AddEmptyImport(const AstRawString* module_request,
const ImportAssertions* import_assertions,
const Scanner::Location specifier_loc, Zone* zone);
// export {x};
// export {x as y};
// export VariableStatement
// export Declaration
// export default ...
void AddExport(
const AstRawString* local_name, const AstRawString* export_name,
const Scanner::Location loc, Zone* zone);
// export {x} from "foo.js";
// export {x as y} from "foo.js";
void AddExport(const AstRawString* export_name,
const AstRawString* import_name,
const AstRawString* module_request,
const ImportAssertions* import_assertions,
const Scanner::Location loc,
const Scanner::Location specifier_loc, Zone* zone);
// export * from "foo.js";
void AddStarExport(const AstRawString* module_request,
const ImportAssertions* import_assertions,
const Scanner::Location loc,
const Scanner::Location specifier_loc, Zone* zone);
// Check if module is well-formed and report error if not.
// Also canonicalize indirect exports.
bool Validate(ModuleScope* module_scope,
PendingCompilationErrorHandler* error_handler, Zone* zone);
struct Entry : public ZoneObject {
Scanner::Location location;
const AstRawString* export_name;
const AstRawString* local_name;
const AstRawString* import_name;
// The module_request value records the order in which modules are
// requested. It also functions as an index into the SourceTextModuleInfo's
// array of module specifiers and into the Module's array of requested
// modules. A negative value means no module request.
int module_request;
// Import/export entries that are associated with a MODULE-allocated
// variable (i.e. regular_imports and regular_exports after Validate) use
// the cell_index value to encode the location of their cell. During
// variable allocation, this will be be copied into the variable's index
// field.
// Entries that are not associated with a MODULE-allocated variable have
// GetCellIndexKind(cell_index) == kInvalid.
int cell_index;
// TODO(neis): Remove local_name component?
explicit Entry(Scanner::Location loc)
: location(loc),
export_name(nullptr),
local_name(nullptr),
import_name(nullptr),
module_request(-1),
cell_index(0) {}
template <typename LocalIsolate>
Handle<SourceTextModuleInfoEntry> Serialize(LocalIsolate* isolate) const;
};
enum CellIndexKind { kInvalid, kExport, kImport };
static CellIndexKind GetCellIndexKind(int cell_index);
class AstModuleRequest : public ZoneObject {
public:
AstModuleRequest(const AstRawString* specifier,
const ImportAssertions* import_assertions, int position,
int index)
: specifier_(specifier),
import_assertions_(import_assertions),
position_(position),
index_(index) {}
template <typename LocalIsolate>
Handle<v8::internal::ModuleRequest> Serialize(LocalIsolate* isolate) const;
const AstRawString* specifier() const { return specifier_; }
const ImportAssertions* import_assertions() const {
return import_assertions_;
}
int position() const { return position_; }
int index() const { return index_; }
private:
const AstRawString* specifier_;
const ImportAssertions* import_assertions_;
// The JS source code position of the request, used for reporting errors.
int position_;
// The index at which we will place the request in SourceTextModuleInfo's
// module_requests FixedArray.
int index_;
};
// Custom content-based comparer for the below maps, to keep them stable
// across parses.
struct V8_EXPORT_PRIVATE AstRawStringComparer {
bool operator()(const AstRawString* lhs, const AstRawString* rhs) const;
};
struct V8_EXPORT_PRIVATE ModuleRequestComparer {
bool operator()(const AstModuleRequest* lhs,
const AstModuleRequest* rhs) const;
};
using ModuleRequestMap =
ZoneSet<const AstModuleRequest*, ModuleRequestComparer>;
using RegularExportMap =
ZoneMultimap<const AstRawString*, Entry*, AstRawStringComparer>;
using RegularImportMap =
ZoneMap<const AstRawString*, Entry*, AstRawStringComparer>;
// Module requests.
const ModuleRequestMap& module_requests() const { return module_requests_; }
// Namespace imports.
const ZoneVector<const Entry*>& namespace_imports() const {
return namespace_imports_;
}
// All the remaining imports, indexed by local name.
const RegularImportMap& regular_imports() const { return regular_imports_; }
// Star exports and explicitly indirect exports.
const ZoneVector<const Entry*>& special_exports() const {
return special_exports_;
}
// All the remaining exports, indexed by local name.
// After canonicalization (see Validate), these are exactly the local exports.
const RegularExportMap& regular_exports() const { return regular_exports_; }
void AddRegularExport(Entry* entry) {
DCHECK_NOT_NULL(entry->export_name);
DCHECK_NOT_NULL(entry->local_name);
DCHECK_NULL(entry->import_name);
DCHECK_LT(entry->module_request, 0);
regular_exports_.insert(std::make_pair(entry->local_name, entry));
}
void AddSpecialExport(const Entry* entry, Zone* zone) {
DCHECK_NULL(entry->local_name);
DCHECK_LE(0, entry->module_request);
special_exports_.push_back(entry);
}
void AddRegularImport(Entry* entry) {
DCHECK_NOT_NULL(entry->import_name);
DCHECK_NOT_NULL(entry->local_name);
DCHECK_NULL(entry->export_name);
DCHECK_LE(0, entry->module_request);
regular_imports_.insert(std::make_pair(entry->local_name, entry));
// We don't care if there's already an entry for this local name, as in that
// case we will report an error when declaring the variable.
}
void AddNamespaceImport(const Entry* entry, Zone* zone) {
DCHECK_NULL(entry->import_name);
DCHECK_NULL(entry->export_name);
DCHECK_NOT_NULL(entry->local_name);
DCHECK_LE(0, entry->module_request);
namespace_imports_.push_back(entry);
}
template <typename LocalIsolate>
Handle<FixedArray> SerializeRegularExports(LocalIsolate* isolate,
Zone* zone) const;
private:
ModuleRequestMap module_requests_;
ZoneVector<const Entry*> special_exports_;
ZoneVector<const Entry*> namespace_imports_;
RegularExportMap regular_exports_;
RegularImportMap regular_imports_;
// If there are multiple export entries with the same export name, return the
// last of them (in source order). Otherwise return nullptr.
const Entry* FindDuplicateExport(Zone* zone) const;
// Find any implicitly indirect exports and make them explicit.
//
// An explicitly indirect export is an export entry arising from an export
// statement of the following form:
// export {a as c} from "X";
// An implicitly indirect export corresponds to
// export {b as c};
// in the presence of an import statement of the form
// import {a as b} from "X";
// This function finds such implicitly indirect export entries and rewrites
// them by filling in the import name and module request, as well as nulling
// out the local name. Effectively, it turns
// import {a as b} from "X"; export {b as c};
// into:
// import {a as b} from "X"; export {a as c} from "X";
// (The import entry is never deleted.)
void MakeIndirectExportsExplicit(Zone* zone);
// Assign a cell_index of -1,-2,... to regular imports.
// Assign a cell_index of +1,+2,... to regular (local) exports.
// Assign a cell_index of 0 to anything else.
void AssignCellIndices();
int AddModuleRequest(const AstRawString* specifier,
const ImportAssertions* import_assertions,
Scanner::Location specifier_loc, Zone* zone) {
DCHECK_NOT_NULL(specifier);
int module_requests_count = static_cast<int>(module_requests_.size());
auto it = module_requests_
.insert(zone->New<AstModuleRequest>(
specifier, import_assertions, specifier_loc.beg_pos,
module_requests_count))
.first;
return (*it)->index();
}
};
} // namespace internal
} // namespace v8
#endif // V8_AST_MODULES_H_
|