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

[modules] Support star exports.

R=adamk@chromium.org
BUG=v8:1569

Review-Url: https://codereview.chromium.org/2362153003
Cr-Commit-Position: refs/heads/master@{#39700}
parent a4354b6c
......@@ -1989,7 +1989,8 @@ static bool InstantiateModule(Local<Module> v8_module,
i::ModuleInfoEntry::cast(regular_imports->get(i)), isolate);
i::Handle<i::String> name(i::String::cast(entry->import_name()), isolate);
int module_request = i::Smi::cast(entry->module_request())->value();
if (i::Module::ResolveImport(module, name, module_request).is_null()) {
if (i::Module::ResolveImport(module, name, module_request, true)
.is_null()) {
return false;
}
}
......@@ -1998,8 +1999,10 @@ static bool InstantiateModule(Local<Module> v8_module,
for (int i = 0, n = special_exports->length(); i < n; ++i) {
i::Handle<i::ModuleInfoEntry> entry(
i::ModuleInfoEntry::cast(special_exports->get(i)), isolate);
i::Handle<i::String> name(i::String::cast(entry->export_name()), isolate);
if (i::Module::ResolveExport(module, name).is_null()) {
i::Handle<i::Object> name(entry->export_name(), isolate);
if (name->IsUndefined(isolate)) continue; // Star export.
if (i::Module::ResolveExport(module, i::Handle<i::String>::cast(name), true)
.is_null()) {
return false;
}
}
......
......@@ -498,6 +498,7 @@ class ErrorUtils : public AllStatic {
T(UnsupportedTimeZone, "Unsupported time zone specified %") \
T(ValueOutOfRange, "Value % out of range for % options property %") \
/* SyntaxError */ \
T(AmbiguousExport, "Multiple star exports provide name '%'") \
T(BadGetterArity, "Getter must not have any formal parameters.") \
T(BadSetterArity, "Setter must have exactly one formal parameter.") \
T(ConstructorIsAccessor, "Class constructor may not be an accessor") \
......
......@@ -19613,8 +19613,8 @@ Handle<Object> Module::LoadExport(Handle<Module> module, Handle<String> name) {
Isolate* isolate = module->GetIsolate();
Handle<Object> object(module->exports()->Lookup(name), isolate);
// TODO(neis): Star exports and namespace imports are not yet implemented.
// Trying to use these features may crash here.
// TODO(neis): Namespace imports are not yet implemented. Trying to use this
// feature may crash here.
if (!object->IsCell()) UNIMPLEMENTED();
return handle(Handle<Cell>::cast(object)->value(), isolate);
......@@ -19629,16 +19629,17 @@ Handle<Object> Module::LoadImport(Handle<Module> module, Handle<String> name,
}
MaybeHandle<Cell> Module::ResolveImport(Handle<Module> module,
Handle<String> name,
int module_request) {
Handle<String> name, int module_request,
bool must_resolve) {
Isolate* isolate = module->GetIsolate();
Handle<Module> requested_module(
Module::cast(module->requested_modules()->get(module_request)), isolate);
return Module::ResolveExport(requested_module, name);
return Module::ResolveExport(requested_module, name, must_resolve);
}
MaybeHandle<Cell> Module::ResolveExport(Handle<Module> module,
Handle<String> name) {
Handle<String> name,
bool must_resolve) {
// TODO(neis): Detect cycles.
Isolate* isolate = module->GetIsolate();
......@@ -19656,7 +19657,9 @@ MaybeHandle<Cell> Module::ResolveExport(Handle<Module> module,
Handle<String> import_name(String::cast(entry->import_name()), isolate);
Handle<Cell> cell;
if (!ResolveImport(module, import_name, module_request).ToHandle(&cell)) {
if (!ResolveImport(module, import_name, module_request, true)
.ToHandle(&cell)) {
DCHECK(isolate->has_pending_exception());
return MaybeHandle<Cell>();
}
......@@ -19671,12 +19674,57 @@ MaybeHandle<Cell> Module::ResolveExport(Handle<Module> module,
}
DCHECK(object->IsTheHole(isolate));
// TODO(neis): Implement star exports.
return Module::ResolveExportUsingStarExports(module, name, must_resolve);
}
MaybeHandle<Cell> Module::ResolveExportUsingStarExports(Handle<Module> module,
Handle<String> name,
bool must_resolve) {
Isolate* isolate = module->GetIsolate();
if (!name->Equals(isolate->heap()->default_string())) {
// Go through all star exports looking for the given name. If multiple star
// exports provide the name, make sure they all map it to the same cell.
Handle<Cell> unique_cell;
Handle<FixedArray> special_exports(module->info()->special_exports(),
isolate);
for (int i = 0, n = special_exports->length(); i < n; ++i) {
i::Handle<i::ModuleInfoEntry> entry(
i::ModuleInfoEntry::cast(special_exports->get(i)), isolate);
if (!entry->export_name()->IsUndefined(isolate)) {
continue; // Indirect export.
}
int module_request = Smi::cast(entry->module_request())->value();
Handle<Cell> cell;
if (ResolveImport(module, name, module_request, false).ToHandle(&cell)) {
if (unique_cell.is_null()) unique_cell = cell;
if (*unique_cell != *cell) {
THROW_NEW_ERROR(
isolate, NewSyntaxError(MessageTemplate::kAmbiguousExport, name),
Cell);
}
} else if (isolate->has_pending_exception()) {
return MaybeHandle<Cell>();
}
}
if (!unique_cell.is_null()) {
// Found a unique star export for this name.
Handle<ObjectHashTable> exports(module->exports(), isolate);
DCHECK(exports->Lookup(name)->IsTheHole(isolate));
exports = ObjectHashTable::Put(exports, name, unique_cell);
module->set_exports(*exports);
return unique_cell;
}
}
// Unresolvable.
THROW_NEW_ERROR(isolate,
NewSyntaxError(MessageTemplate::kUnresolvableExport, name),
Cell);
if (must_resolve) {
THROW_NEW_ERROR(isolate,
NewSyntaxError(MessageTemplate::kUnresolvableExport, name),
Cell);
}
return MaybeHandle<Cell>();
}
} // namespace internal
......
......@@ -7962,11 +7962,22 @@ class Module : public Struct {
static Handle<Object> LoadImport(Handle<Module> module, Handle<String> name,
int module_request);
// The [must_resolve] argument indicates whether or not an exception should be
// thrown if the module does not provide an export named [name].
//
// If [must_resolve] is true, a null result indicates an exception. If
// [must_resolve] is false, a null result does not necessarily indicate an
// exception, but there may be one pending.
//
// Currently, an exception is always thrown in the case of a cycle and in the
// case of conflicting star exports. TODO(neis): Make that spec-compliant.
static MUST_USE_RESULT MaybeHandle<Cell> ResolveExport(Handle<Module> module,
Handle<String> name,
bool must_resolve);
static MUST_USE_RESULT MaybeHandle<Cell> ResolveImport(Handle<Module> module,
Handle<String> name,
int module_request);
static MUST_USE_RESULT MaybeHandle<Cell> ResolveExport(Handle<Module> module,
Handle<String> name);
int module_request,
bool must_resolve);
static const int kCodeOffset = HeapObject::kHeaderSize;
static const int kExportsOffset = kCodeOffset + kPointerSize;
......@@ -7978,6 +7989,10 @@ class Module : public Struct {
private:
enum { kEvaluatedBit };
// Helper for ResolveExport.
static MUST_USE_RESULT MaybeHandle<Cell> ResolveExportUsingStarExports(
Handle<Module> module, Handle<String> name, bool must_resolve);
DISALLOW_IMPLICIT_CONSTRUCTORS(Module);
};
......
// Copyright 2016 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.
//
// MODULE
// Star exports do not propagate a default export.
import a from "modules-skip-4.js";
// Copyright 2016 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.
//
// MODULE
// Star exports do not propagate a default export.
import a from "modules-skip-4.js";
// Copyright 2016 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.
//
// MODULE
import {a} from "modules-skip-7.js";
// Copyright 2016 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.
//
// MODULE
import {b, c} from "modules-skip-4.js";
import {a, set_a} from "modules-skip-4.js";
assertEquals(1, a);
assertEquals(1, b);
assertEquals(1, c);
set_a(2);
assertEquals(2, a);
assertEquals(2, b);
assertEquals(2, c);
assertThrows(() => a = 3, TypeError);
assertThrows(() => b = 3, TypeError);
assertThrows(() => c = 3, TypeError);
assertEquals(2, a);
assertEquals(2, b);
assertEquals(2, c);
// Copyright 2016 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.
//
// MODULE
import {a} from "modules-skip-6.js";
assertEquals(10, a);
// Copyright 2016 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.
export * from "modules-skip-1.js";
export * from "modules-skip-2.js";
// Copyright 2016 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.
export function a() { return "ooo" }
// Copyright 2016 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.
export * from "modules-skip-1.js";
export * from "modules-skip-5.js";
export const a = 10;
// Copyright 2016 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.
export * from "modules-skip-1.js"
export * from "modules-skip-5.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