Commit ca6886c0 authored by neis's avatar neis Committed by Commit bot

[modules] Basic support of import statements.

BUG=v8:1569

Review-Url: https://codereview.chromium.org/2360063002
Cr-Commit-Position: refs/heads/master@{#39639}
parent 813be427
...@@ -122,6 +122,7 @@ class ModuleDescriptor : public ZoneObject { ...@@ -122,6 +122,7 @@ class ModuleDescriptor : public ZoneObject {
} }
// All the remaining exports, indexed by local name. // All the remaining exports, indexed by local name.
// After canonicalization (see Validate), these are exactly the local exports.
const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const {
return regular_exports_; return regular_exports_;
} }
......
...@@ -2005,7 +2005,18 @@ void BytecodeGenerator::VisitVariableLoad(Variable* variable, ...@@ -2005,7 +2005,18 @@ void BytecodeGenerator::VisitVariableLoad(Variable* variable,
.StoreAccumulatorInRegister(export_name) .StoreAccumulatorInRegister(export_name)
.CallRuntime(Runtime::kLoadModuleExport, export_name, 1); .CallRuntime(Runtime::kLoadModuleExport, export_name, 1);
} else { } else {
UNIMPLEMENTED(); auto it = descriptor->regular_imports().find(variable->raw_name());
DCHECK(it != descriptor->regular_imports().end());
register_allocator()->PrepareForConsecutiveAllocations(2);
Register import_name = register_allocator()->NextConsecutiveRegister();
Register module_request =
register_allocator()->NextConsecutiveRegister();
builder()
->LoadLiteral(it->second->import_name->string())
.StoreAccumulatorInRegister(import_name)
.LoadLiteral(Smi::FromInt(it->second->module_request))
.StoreAccumulatorInRegister(module_request)
.CallRuntime(Runtime::kLoadModuleImport, import_name, 2);
} }
break; break;
} }
...@@ -2205,6 +2216,8 @@ void BytecodeGenerator::VisitVariableAssignment(Variable* variable, ...@@ -2205,6 +2216,8 @@ void BytecodeGenerator::VisitVariableAssignment(Variable* variable,
DCHECK(variable->IsExport()); DCHECK(variable->IsExport());
ModuleDescriptor* mod = scope()->GetModuleScope()->module(); ModuleDescriptor* mod = scope()->GetModuleScope()->module();
// There may be several export names for this local name, but it doesn't
// matter which one we pick, as they all map to the same cell.
auto it = mod->regular_exports().find(variable->raw_name()); auto it = mod->regular_exports().find(variable->raw_name());
DCHECK(it != mod->regular_exports().end()); DCHECK(it != mod->regular_exports().end());
......
...@@ -19608,5 +19608,16 @@ Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) { ...@@ -19608,5 +19608,16 @@ Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) {
return handle(cell->value(), isolate); return handle(cell->value(), isolate);
} }
Handle<Object> Module::LoadImport(Handle<Module> module, Handle<String> name,
int module_request) {
Isolate* isolate = module->GetIsolate();
Handle<Module> requested_module(
Module::cast(module->requested_modules()->get(module_request)), isolate);
Handle<ObjectHashTable> exports(requested_module->exports(), isolate);
Object* object = exports->Lookup(name);
if (!object->IsCell()) UNIMPLEMENTED();
return handle(Cell::cast(object)->value(), isolate);
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -7946,9 +7946,12 @@ class Module : public Struct { ...@@ -7946,9 +7946,12 @@ class Module : public Struct {
DECL_INT_ACCESSORS(flags) DECL_INT_ACCESSORS(flags)
static void CreateExport(Handle<Module> module, Handle<FixedArray> names); static void CreateExport(Handle<Module> module, Handle<FixedArray> names);
static Handle<Object> LoadExport(Handle<Module> module, Handle<String> name);
static void StoreExport(Handle<Module> module, Handle<String> name, static void StoreExport(Handle<Module> module, Handle<String> name,
Handle<Object> value); Handle<Object> value);
static Handle<Object> LoadExport(Handle<Module> module, Handle<String> name);
static Handle<Object> LoadImport(Handle<Module> module, Handle<String> name,
int module_request);
static const int kCodeOffset = HeapObject::kHeaderSize; static const int kCodeOffset = HeapObject::kHeaderSize;
static const int kExportsOffset = kCodeOffset + kPointerSize; static const int kExportsOffset = kCodeOffset + kPointerSize;
......
...@@ -968,6 +968,15 @@ RUNTIME_FUNCTION(Runtime_LoadModuleExport) { ...@@ -968,6 +968,15 @@ RUNTIME_FUNCTION(Runtime_LoadModuleExport) {
return *Module::LoadExport(module, name); return *Module::LoadExport(module, name);
} }
RUNTIME_FUNCTION(Runtime_LoadModuleImport) {
HandleScope scope(isolate);
DCHECK(args.length() == 2);
CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
CONVERT_ARG_HANDLE_CHECKED(Smi, module_request, 1);
Handle<Module> module(isolate->context()->module());
return *Module::LoadImport(module, name, module_request->value());
}
RUNTIME_FUNCTION(Runtime_StoreModuleExport) { RUNTIME_FUNCTION(Runtime_StoreModuleExport) {
HandleScope scope(isolate); HandleScope scope(isolate);
DCHECK(args.length() == 2); DCHECK(args.length() == 2);
......
...@@ -420,6 +420,7 @@ namespace internal { ...@@ -420,6 +420,7 @@ namespace internal {
F(IsAccessCheckNeeded, 1, 1) \ F(IsAccessCheckNeeded, 1, 1) \
F(CreateDataProperty, 3, 1) \ F(CreateDataProperty, 3, 1) \
F(LoadModuleExport, 1, 1) \ F(LoadModuleExport, 1, 1) \
F(LoadModuleImport, 2, 1) \
F(StoreModuleExport, 2, 1) F(StoreModuleExport, 2, 1)
#define FOR_EACH_INTRINSIC_OPERATORS(F) \ #define FOR_EACH_INTRINSIC_OPERATORS(F) \
......
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