Commit 29e85e36 authored by neis's avatar neis Committed by Commit bot

[modules] Split imports into regular and special, store regular ones in a map.

BUG=v8:1569

Review-Url: https://codereview.chromium.org/2224333002
Cr-Commit-Position: refs/heads/master@{#38532}
parent 3306dbc2
......@@ -9,7 +9,6 @@
namespace v8 {
namespace internal {
void ModuleDescriptor::AddImport(
const AstRawString* import_name, const AstRawString* local_name,
const AstRawString* module_request, Scanner::Location loc, Zone* zone) {
......@@ -20,7 +19,9 @@ void ModuleDescriptor::AddImport(
entry->local_name = local_name;
entry->import_name = import_name;
entry->module_request = module_request;
imports_.Add(entry, zone);
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.
}
......@@ -32,7 +33,7 @@ void ModuleDescriptor::AddStarImport(
ModuleEntry* entry = new (zone) ModuleEntry(loc);
entry->local_name = local_name;
entry->module_request = module_request;
imports_.Add(entry, zone);
special_imports_.Add(entry, zone);
}
......@@ -41,7 +42,7 @@ void ModuleDescriptor::AddEmptyImport(
DCHECK_NOT_NULL(module_request);
ModuleEntry* entry = new (zone) ModuleEntry(loc);
entry->module_request = module_request;
imports_.Add(entry, zone);
special_imports_.Add(entry, zone);
}
......
......@@ -7,7 +7,7 @@
#include "src/parsing/scanner.h" // Only for Scanner::Location.
#include "src/pending-compilation-error-handler.h"
#include "src/zone.h"
#include "src/zone-containers.h"
namespace v8 {
namespace internal {
......@@ -19,7 +19,7 @@ class AstRawString;
class ModuleDescriptor : public ZoneObject {
public:
explicit ModuleDescriptor(Zone* zone)
: exports_(1, zone), imports_(1, zone) {}
: exports_(1, zone), special_imports_(1, zone), regular_imports_(zone) {}
// import x from "foo.js";
// import {x} from "foo.js";
......@@ -82,12 +82,23 @@ class ModuleDescriptor : public ZoneObject {
module_request(nullptr) {}
};
const ZoneList<const ModuleEntry*>& exports() { return exports_; }
const ZoneList<const ModuleEntry*>& imports() { return imports_; }
const ZoneList<const ModuleEntry*>& exports() const { return exports_; }
// Empty imports and namespace imports.
const ZoneList<const ModuleEntry*>& special_imports() const {
return special_imports_;
}
// All the remaining imports, indexed by local name.
const ZoneMap<const AstRawString*, const ModuleEntry*>& regular_imports()
const {
return regular_imports_;
}
private:
ZoneList<const ModuleEntry*> exports_;
ZoneList<const ModuleEntry*> imports_;
ZoneList<const ModuleEntry*> special_imports_;
ZoneMap<const AstRawString*, const ModuleEntry*> regular_imports_;
};
} // namespace internal
......
......@@ -1702,13 +1702,13 @@ void DeclarationScope::AllocateLocals(AstValueFactory* ast_value_factory) {
}
void DeclarationScope::AllocateModuleVariables() {
for (auto entry : module()->imports()) {
if (entry->local_name == nullptr) continue;
if (entry->import_name == nullptr) continue; // Namespace import.
Variable* var = LookupLocal(entry->local_name);
for (auto it = module()->regular_imports().begin();
it != module()->regular_imports().end(); ++it) {
Variable* var = LookupLocal(it->second->local_name);
// TODO(neis): Use a meaningful index.
var->AllocateTo(VariableLocation::MODULE, 42);
}
for (auto entry : module()->exports()) {
if (entry->local_name == nullptr) continue;
Variable* var = LookupLocal(entry->local_name);
......
......@@ -5955,6 +5955,7 @@ TEST(ModuleParsingInternals) {
CHECK_NULL(outer_scope->outer_scope());
CHECK(module_scope->is_module_scope());
i::ZoneList<i::Declaration*>* declarations = module_scope->declarations();
CHECK_EQ(13, declarations->length());
CHECK(declarations->at(0)->proxy()->raw_name()->IsOneByteEqualTo("x"));
CHECK(declarations->at(0)->proxy()->var()->mode() == i::LET);
......@@ -6035,11 +6036,129 @@ TEST(ModuleParsingInternals) {
CHECK(declarations->at(12)->proxy()->var()->location() ==
i::VariableLocation::MODULE);
CHECK_EQ(13, declarations->length());
i::ModuleDescriptor* descriptor = module_scope->module();
CHECK_NOT_NULL(descriptor);
// TODO(neis): Test more once we can inspect the imports/exports.
CHECK_EQ(11, descriptor->exports().length());
CHECK(descriptor->exports().at(0)->export_name->IsOneByteEqualTo("y"));
CHECK(descriptor->exports().at(0)->local_name->IsOneByteEqualTo("x"));
CHECK_NULL(descriptor->exports().at(0)->module_request);
CHECK_NULL(descriptor->exports().at(0)->import_name);
CHECK(descriptor->exports().at(1)->export_name->IsOneByteEqualTo("b"));
CHECK(descriptor->exports().at(1)->import_name->IsOneByteEqualTo("a"));
CHECK(descriptor->exports().at(1)->module_request->IsOneByteEqualTo("m.js"));
CHECK_NULL(descriptor->exports().at(1)->local_name);
CHECK(descriptor->exports().at(2)->module_request->IsOneByteEqualTo("p.js"));
CHECK_NULL(descriptor->exports().at(2)->local_name);
CHECK_NULL(descriptor->exports().at(2)->import_name);
CHECK_NULL(descriptor->exports().at(2)->export_name);
CHECK(descriptor->exports().at(3)->export_name->IsOneByteEqualTo("foo"));
CHECK(descriptor->exports().at(3)->local_name->IsOneByteEqualTo("foo"));
CHECK_NULL(descriptor->exports().at(3)->module_request);
CHECK_NULL(descriptor->exports().at(3)->import_name);
CHECK(descriptor->exports().at(4)->export_name->IsOneByteEqualTo("goo"));
CHECK(descriptor->exports().at(4)->local_name->IsOneByteEqualTo("goo"));
CHECK_NULL(descriptor->exports().at(4)->module_request);
CHECK_NULL(descriptor->exports().at(4)->import_name);
CHECK(descriptor->exports().at(5)->export_name->IsOneByteEqualTo("hoo"));
CHECK(descriptor->exports().at(5)->local_name->IsOneByteEqualTo("hoo"));
CHECK_NULL(descriptor->exports().at(5)->module_request);
CHECK_NULL(descriptor->exports().at(5)->import_name);
CHECK(descriptor->exports().at(6)->export_name->IsOneByteEqualTo("joo"));
CHECK(descriptor->exports().at(6)->local_name->IsOneByteEqualTo("joo"));
CHECK_NULL(descriptor->exports().at(6)->module_request);
CHECK_NULL(descriptor->exports().at(6)->import_name);
CHECK(descriptor->exports().at(7)->export_name->IsOneByteEqualTo("default"));
CHECK(descriptor->exports().at(7)->local_name->IsOneByteEqualTo("*default*"));
CHECK_NULL(descriptor->exports().at(7)->module_request);
CHECK_NULL(descriptor->exports().at(7)->import_name);
CHECK(descriptor->exports().at(8)->export_name->IsOneByteEqualTo("bb"));
CHECK(descriptor->exports().at(8)->local_name->IsOneByteEqualTo("aa"));
CHECK_NULL(descriptor->exports().at(8)->module_request);
CHECK_NULL(descriptor->exports().at(8)->import_name);
CHECK(descriptor->exports().at(9)->export_name->IsOneByteEqualTo("x"));
CHECK(descriptor->exports().at(9)->local_name->IsOneByteEqualTo("x"));
CHECK_NULL(descriptor->exports().at(9)->module_request);
CHECK_NULL(descriptor->exports().at(9)->import_name);
CHECK(descriptor->exports().at(10)->export_name->IsOneByteEqualTo("foob"));
CHECK(descriptor->exports().at(10)->local_name->IsOneByteEqualTo("foob"));
CHECK_NULL(descriptor->exports().at(10)->module_request);
CHECK_NULL(descriptor->exports().at(10)->import_name);
CHECK_EQ(3, descriptor->special_imports().length());
CHECK_NULL(descriptor->special_imports().at(0)->local_name);
CHECK_NULL(descriptor->special_imports().at(0)->export_name);
CHECK_NULL(descriptor->special_imports().at(0)->import_name);
CHECK(descriptor->special_imports().at(0)->module_request->IsOneByteEqualTo(
"q.js"));
CHECK(
descriptor->special_imports().at(1)->local_name->IsOneByteEqualTo("loo"));
CHECK_NULL(descriptor->special_imports().at(1)->export_name);
CHECK_NULL(descriptor->special_imports().at(1)->import_name);
CHECK(descriptor->special_imports().at(1)->module_request->IsOneByteEqualTo(
"bar.js"));
CHECK(descriptor->special_imports().at(2)->local_name->IsOneByteEqualTo(
"foob"));
CHECK_NULL(descriptor->special_imports().at(2)->export_name);
CHECK_NULL(descriptor->special_imports().at(2)->import_name);
CHECK(descriptor->special_imports().at(2)->module_request->IsOneByteEqualTo(
"bar.js"));
CHECK_EQ(4, descriptor->regular_imports().size());
CHECK_NULL(descriptor->regular_imports()
.find(declarations->at(1)->proxy()->raw_name())
->second->export_name);
CHECK(descriptor->regular_imports()
.find(declarations->at(1)->proxy()->raw_name())
->second->import_name->IsOneByteEqualTo("q"));
CHECK(descriptor->regular_imports()
.find(declarations->at(1)->proxy()->raw_name())
->second->module_request->IsOneByteEqualTo("m.js"));
CHECK_NULL(descriptor->regular_imports()
.find(declarations->at(2)->proxy()->raw_name())
->second->export_name);
CHECK(descriptor->regular_imports()
.find(declarations->at(2)->proxy()->raw_name())
->second->import_name->IsOneByteEqualTo("default"));
CHECK(descriptor->regular_imports()
.find(declarations->at(2)->proxy()->raw_name())
->second->module_request->IsOneByteEqualTo("n.js"));
CHECK_NULL(descriptor->regular_imports()
.find(declarations->at(9)->proxy()->raw_name())
->second->export_name);
CHECK(descriptor->regular_imports()
.find(declarations->at(9)->proxy()->raw_name())
->second->import_name->IsOneByteEqualTo("m"));
CHECK(descriptor->regular_imports()
.find(declarations->at(9)->proxy()->raw_name())
->second->module_request->IsOneByteEqualTo("m.js"));
CHECK_NULL(descriptor->regular_imports()
.find(declarations->at(10)->proxy()->raw_name())
->second->export_name);
CHECK(descriptor->regular_imports()
.find(declarations->at(10)->proxy()->raw_name())
->second->import_name->IsOneByteEqualTo("aa"));
CHECK(descriptor->regular_imports()
.find(declarations->at(10)->proxy()->raw_name())
->second->module_request->IsOneByteEqualTo("m.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