Commit 52bbb4f6 authored by adamk's avatar adamk Committed by Commit bot

Collect list of requested modules in ModuleDescriptor while parsing

BUG=v8:1569
LOG=n

Review URL: https://codereview.chromium.org/1078903002

Cr-Commit-Position: refs/heads/master@{#27724}
parent ae475b54
......@@ -35,6 +35,15 @@ void ModuleDescriptor::AddLocalExport(const AstRawString* export_name,
}
void ModuleDescriptor::AddModuleRequest(const AstRawString* module_specifier,
Zone* zone) {
// TODO(adamk): Avoid this O(N) operation on each insert by storing
// a HashMap, or by de-duping after parsing.
if (requested_modules_.Contains(module_specifier)) return;
requested_modules_.Add(module_specifier, zone);
}
const AstRawString* ModuleDescriptor::LookupLocalExport(
const AstRawString* export_name, Zone* zone) {
if (exports_ == nullptr) return nullptr;
......
......@@ -20,7 +20,7 @@ class ModuleDescriptor : public ZoneObject {
// Factory methods.
static ModuleDescriptor* New(Zone* zone) {
return new (zone) ModuleDescriptor();
return new (zone) ModuleDescriptor(zone);
}
// ---------------------------------------------------------------------------
......@@ -31,6 +31,10 @@ class ModuleDescriptor : public ZoneObject {
void AddLocalExport(const AstRawString* export_name,
const AstRawString* local_name, Zone* zone, bool* ok);
// Add module_specifier to the list of requested modules,
// if not already present.
void AddModuleRequest(const AstRawString* module_specifier, Zone* zone);
// Do not allow any further refinements, directly or through unification.
void Freeze() { frozen_ = true; }
......@@ -61,6 +65,10 @@ class ModuleDescriptor : public ZoneObject {
const AstRawString* LookupLocalExport(const AstRawString* export_name,
Zone* zone);
const ZoneList<const AstRawString*>& requested_modules() const {
return requested_modules_;
}
// ---------------------------------------------------------------------------
// Iterators.
......@@ -95,11 +103,16 @@ class ModuleDescriptor : public ZoneObject {
// ---------------------------------------------------------------------------
// Implementation.
private:
explicit ModuleDescriptor(Zone* zone)
: frozen_(false),
exports_(NULL),
requested_modules_(1, zone),
index_(-1) {}
bool frozen_;
ZoneHashMap* exports_; // Module exports and their types (allocated lazily)
ZoneList<const AstRawString*> requested_modules_;
int index_;
ModuleDescriptor() : frozen_(false), exports_(NULL), index_(-1) {}
};
} } // namespace v8::internal
......
......@@ -1560,6 +1560,8 @@ Statement* Parser::ParseImportDeclaration(bool* ok) {
const AstRawString* module_specifier = ParseModuleSpecifier(CHECK_OK);
ExpectSemicolon(CHECK_OK);
scope_->module()->AddModuleRequest(module_specifier, zone());
if (module_instance_binding != NULL) {
// TODO(ES6): Set the module specifier for the module namespace binding.
}
......@@ -1697,6 +1699,8 @@ Statement* Parser::ParseExportDeclaration(bool* ok) {
}
}
} else {
scope_->module()->AddModuleRequest(indirect_export_module_specifier,
zone());
for (int i = 0; i < length; ++i) {
// TODO(ES6): scope_->module()->AddIndirectExport(...);(
}
......
......@@ -5445,7 +5445,8 @@ TEST(ModuleParsingInternals) {
"let x = 5;"
"export { x as y };"
"import { q as z } from 'm.js';"
"import n from 'n.js'";
"import n from 'n.js';"
"export { a as b } from 'm.js';";
i::Handle<i::String> source = factory->NewStringFromAsciiChecked(kSource);
i::Handle<i::Script> script = factory->NewScript(source);
i::Zone zone;
......@@ -5485,6 +5486,12 @@ TEST(ModuleParsingInternals) {
CHECK(import_decl->import_name()->IsOneByteEqualTo("default"));
CHECK(import_decl->proxy()->raw_name()->IsOneByteEqualTo("n"));
CHECK(import_decl->module_specifier()->IsOneByteEqualTo("n.js"));
// TODO(adamk): Add test for indirect exports once they're fully implemented.
const i::ZoneList<const i::AstRawString*>& requested_modules =
descriptor->requested_modules();
CHECK_EQ(2, requested_modules.length());
CHECK(requested_modules[0]->IsOneByteEqualTo("m.js"));
CHECK(requested_modules[1]->IsOneByteEqualTo("n.js"));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment