Index: pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart |
diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart |
index 5048fe5f5cb7ea9024cd7c2a60ab652a034bcafa..9797dbb60f546d8fd3ce5080dd46d55ac10955cf 100644 |
--- a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart |
+++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart |
@@ -35,6 +35,8 @@ import 'package:js_runtime/shared/embedded_names.dart' show |
import '../js_emitter.dart' show NativeGenerator, buildTearOffCode; |
import '../model.dart'; |
+part 'fragment_emitter.dart'; |
+ |
class ModelEmitter { |
final Compiler compiler; |
final Namer namer; |
@@ -124,7 +126,55 @@ class ModelEmitter { |
} |
int emitProgram(Program program) { |
- // TODO(floitsch): implement. |
- throw new UnimplementedError("startup-emitter.emitProgram"); |
+ List<Fragment> fragments = program.fragments; |
+ MainFragment mainFragment = fragments.first; |
+ |
+ int totalSize = 0; |
+ |
+ FragmentEmitter fragmentEmitter = |
+ new FragmentEmitter(compiler, namer, backend, constantEmitter, this); |
+ |
+ // We have to emit the deferred fragments first, since we need their |
+ // deferred hash (which depends on the output) when emitting the main |
+ // fragment. |
+ List<js.Expression> fragmentsCode = fragments.skip(1).map( |
+ (DeferredFragment deferredFragment) { |
+ js.Expression types = |
+ program.metadataTypesForOutputUnit(deferredFragment.outputUnit); |
+ return fragmentEmitter.emitDeferredFragment( |
+ deferredFragment, types, program.holders); |
+ }).toList(); |
+ |
+ js.Statement mainAst = fragmentEmitter.emitMainFragment(program); |
+ |
+ js.TokenCounter counter = new js.TokenCounter(); |
+ fragmentsCode.forEach(counter.countTokens); |
+ counter.countTokens(mainAst); |
+ |
+ program.finalizers.forEach((js.TokenFinalizer f) => f.finalizeTokens()); |
+ |
+ for (int i = 0; i < fragmentsCode.length; ++i) { |
+ String code = js.prettyPrint(fragmentsCode[i], compiler).getText(); |
+ totalSize += code.length; |
+ compiler.outputProvider(fragments[i+1].outputFileName, deferredExtension) |
+ ..add(code) |
+ ..close(); |
+ } |
+ |
+ String mainCode = js.prettyPrint(mainAst, compiler).getText(); |
+ compiler.outputProvider(mainFragment.outputFileName, 'js') |
+ ..add(buildGeneratedBy(compiler)) |
+ ..add(mainCode) |
+ ..close(); |
+ totalSize += mainCode.length; |
+ |
+ return totalSize; |
+ } |
+ |
+ String buildGeneratedBy(compiler) { |
+ var suffix = ''; |
+ if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; |
+ return '// Generated by dart2js (fast startup), ' |
+ 'the Dart to JavaScript compiler$suffix.\n'; |
} |
} |