Rework DillLoader to allow adding multiple dills.

This allows us to compose a consistent bundle with SDK and the transitive
closure of required libraries for a libarary, and then compile it
separately (or as a part of its own library cycle).

There is still more data public than I'd like, but I will leave this
clean up fo later.

R=ahe@google.com, kmillikin@google.com, paulberry@google.com, sigmund@google.com
BUG=

Review-Url: https://codereview.chromium.org/2872903005 .
This commit is contained in:
Konstantin Shcheglov 2017-05-11 08:56:33 -07:00
parent 935aeb43ce
commit 8751b91062
7 changed files with 40 additions and 45 deletions

View file

@ -6,47 +6,39 @@ library fasta.dill_loader;
import 'dart:async' show Future;
import 'dart:io' show File;
import 'package:kernel/kernel.dart' show loadProgramFromBinary;
import 'package:kernel/ast.dart' show Library, Program;
import '../loader.dart' show Loader;
import '../target_implementation.dart' show TargetImplementation;
import 'dill_library_builder.dart' show DillLibraryBuilder;
class DillLoader extends Loader<Library> {
Uri input;
Program program;
DillLoader(TargetImplementation target) : super(target);
DillLibraryBuilder read(Uri uri, [Uri fileUri]) => super.read(uri, fileUri);
Future<Null> buildOutline(DillLibraryBuilder builder) async {
if (program == null) {
byteCount = await new File.fromUri(input).length();
setProgram(await loadProgramFromBinary(input.toFilePath()));
/// Append compiled libraries from the given [program]. If the [filter] is
/// provided, append only libraries whose [Uri] is accepted by the [filter].
void appendLibraries(Program program, [bool filter(Uri uri)]) {
this.program ??= new Program();
program.unbindCanonicalNames();
for (Library library in program.libraries) {
if (filter == null || filter(library.importUri)) {
this.program.libraries.add(library);
read(library.importUri).library = library;
}
}
builder.library.classes.forEach(builder.addClass);
builder.library.procedures.forEach(builder.addMember);
builder.library.fields.forEach(builder.addMember);
}
Future<Null> buildBody(DillLibraryBuilder builder) {
return buildOutline(builder);
}
void setProgram(Program program) {
assert(input != null);
this.program = program;
program.unbindCanonicalNames();
for (Library library in program.libraries) {
read(library.importUri).library = library;
}
Future<Null> buildOutline(DillLibraryBuilder builder) async {
builder.library.classes.forEach(builder.addClass);
builder.library.procedures.forEach(builder.addMember);
builder.library.fields.forEach(builder.addMember);
}
DillLibraryBuilder read(Uri uri, [Uri fileUri]) => super.read(uri, fileUri);
}

View file

@ -10,7 +10,7 @@ import 'package:kernel/ast.dart' show Class;
import 'dill_loader.dart' show DillLoader;
import '../errors.dart' show inputError, internalError;
import '../errors.dart' show internalError;
import '../target_implementation.dart' show TargetImplementation;
@ -37,11 +37,7 @@ class DillTarget extends TargetImplementation {
}
void read(Uri uri) {
if (loader.input == null) {
loader.input = uri;
} else {
inputError(uri, -1, "Can only read one dill file.");
}
internalError("Unsupported operation.");
}
Future<Null> writeProgram(Uri uri) {
@ -49,7 +45,7 @@ class DillTarget extends TargetImplementation {
}
Future<Null> writeOutline(Uri uri) async {
if (loader.input == null) return null;
if (loader.program == null) return null;
await loader.buildOutlines();
isLoaded = true;
return null;

View file

@ -14,7 +14,7 @@ import 'package:front_end/physical_file_system.dart';
import 'package:kernel/binary/ast_to_binary.dart'
show LibraryFilteringBinaryPrinter;
import 'package:kernel/kernel.dart' show Program, Library;
import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes;
import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget;
@ -180,7 +180,7 @@ Future<CompilationResult> parseScript(
await TranslateUri.parse(PhysicalFileSystem.instance, null, packages);
final Ticker ticker = new Ticker(isVerbose: verbose);
final DillTarget dillTarget = new DillTarget(ticker, uriTranslator);
dillTarget.read(patchedSdk.resolve('platform.dill'));
_appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill'));
final KernelTarget kernelTarget = new KernelTarget(
PhysicalFileSystem.instance, dillTarget, uriTranslator, strongMode);
kernelTarget.read(fileName);
@ -250,8 +250,8 @@ Future writeDepsFile(Uri script, Uri depsFile, Uri output,
TranslateUri uriTranslator = await TranslateUri.parse(
c.fileSystem, c.options.sdk, c.options.packages);
ticker.logMs("Read packages file");
DillTarget dillTarget = new DillTarget(ticker, uriTranslator)
..read(platform);
DillTarget dillTarget = new DillTarget(ticker, uriTranslator);
_appendDillForUri(dillTarget, platform);
KernelTarget kernelTarget = new KernelTarget(PhysicalFileSystem.instance,
dillTarget, uriTranslator, false, c.uriToSource);
@ -263,6 +263,14 @@ Future writeDepsFile(Uri script, Uri depsFile, Uri output,
});
}
/// Load the [Program] from the given [uri] and append its libraries
/// to the [dillTarget].
void _appendDillForUri(DillTarget dillTarget, Uri uri) {
var bytes = new File.fromUri(uri).readAsBytesSync();
var platformProgram = loadProgramFromBytes(bytes);
dillTarget.loader.appendLibraries(platformProgram);
}
// TODO(ahe): https://github.com/dart-lang/sdk/issues/28316
class ByteSink implements Sink<List<int>> {
final BytesBuilder builder = new BytesBuilder();

View file

@ -193,9 +193,7 @@ class Outline extends Step<TestDescription, Program, FastaContext> {
Program platform = await context.loadPlatform();
Ticker ticker = new Ticker();
DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator);
dillTarget.loader
..input = Uri.parse("org.dartlang:platform") // Make up a name.
..setProgram(platform);
dillTarget.loader.appendLibraries(platform);
KernelTarget sourceTarget = astKind == AstKind.Analyzer
? new AnalyzerTarget(dillTarget, context.uriTranslator, strongMode)
: new KernelTarget(PhysicalFileSystem.instance, dillTarget,

View file

@ -23,8 +23,13 @@ import 'text/ast_to_text.dart';
export 'ast.dart';
Program loadProgramFromBinary(String path, [Program program]) {
List<int> bytes = new File(path).readAsBytesSync();
return loadProgramFromBytes(bytes, program);
}
Program loadProgramFromBytes(List<int> bytes, [Program program]) {
program ??= new Program();
new BinaryBuilder(new File(path).readAsBytesSync()).readProgram(program);
new BinaryBuilder(bytes).readProgram(program);
return program;
}

View file

@ -93,9 +93,7 @@ class FastaCompile
Program platform = await context.loadPlatform();
Ticker ticker = new Ticker();
DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator);
dillTarget.loader
..input = Uri.parse("org.dartlang:platform") // Make up a name.
..setProgram(platform);
dillTarget.loader.appendLibraries(platform);
KernelTarget sourceTarget = new KernelTarget(PhysicalFileSystem.instance,
dillTarget, context.uriTranslator, context.strongMode);

View file

@ -76,9 +76,7 @@ class FastaCompile extends Step<TestDescription, Program, InterpreterContext> {
Program platform = await context.loadPlatform();
Ticker ticker = new Ticker();
DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator);
dillTarget.loader
..input = Uri.parse("org.dartlang:platform") // Make up a name.
..setProgram(platform);
dillTarget.loader.appendLibraries(platform);
KernelTarget sourceTarget = new KernelTarget(PhysicalFileSystem.instance,
dillTarget, context.uriTranslator, context.strongMode);