@@ -7,8 +7,7 @@ import 'dart:typed_data';
77
88import 'package:build_integration/file_system/multi_root.dart'
99 show MultiRootFileSystem, MultiRootFileSystemEntity;
10- import 'package:front_end/src/api_prototype/standard_file_system.dart'
11- show StandardFileSystem;
10+ import 'package:front_end/src/api_prototype/file_system.dart' show FileSystem;
1211import 'package:front_end/src/api_unstable/vm.dart'
1312 show
1413 CompilerOptions,
@@ -21,6 +20,7 @@ import 'package:kernel/class_hierarchy.dart';
2120import 'package:kernel/core_types.dart' ;
2221import 'package:kernel/kernel.dart' show writeComponentToText;
2322import 'package:kernel/library_index.dart' ;
23+ import 'package:kernel/text/ast_to_text.dart' ;
2424import 'package:kernel/type_environment.dart' ;
2525import 'package:kernel/verifier.dart' ;
2626import 'package:vm/kernel_front_end.dart' show writeDepfile;
@@ -30,6 +30,7 @@ import 'package:vm/transformations/to_string_transformer.dart'
3030 as to_string_transformer;
3131import 'package:vm/transformations/type_flow/transformer.dart' as globalTypeFlow
3232 show transformComponent;
33+ import 'package:vm/transformations/type_flow/utils.dart' as tfa_utils;
3334import 'package:vm/transformations/unreachable_code_elimination.dart'
3435 as unreachable_code_elimination;
3536import 'package:wasm_builder/wasm_builder.dart' show Serializer;
@@ -64,7 +65,7 @@ class CompilationSuccess extends CompilationResult {
6465 CompilationSuccess (this .wasmModules, this .jsRuntime, this .supportJs);
6566}
6667
67- class CompilationError extends CompilationResult {}
68+ abstract class CompilationError extends CompilationResult {}
6869
6970/// The CFE has crashed with an exception.
7071///
@@ -74,6 +75,9 @@ class CFECrashError extends CompilationError {
7475 final StackTrace stackTrace;
7576
7677 CFECrashError (this .error, this .stackTrace);
78+
79+ @override
80+ String toString () => 'CFECrashError($error ):\n $stackTrace ' ;
7781}
7882
7983/// Compiling the Dart program resulted in compile-time errors.
@@ -122,8 +126,10 @@ const List<String> _librariesToIndex = [
122126/// mappings.
123127Future <CompilationResult > compileToModule (
124128 compiler.WasmCompilerOptions options,
129+ FileSystem fileSystem,
125130 Uri Function (String moduleName)? sourceMapUrlGenerator,
126- void Function (DiagnosticMessage ) handleDiagnosticMessage) async {
131+ void Function (DiagnosticMessage ) handleDiagnosticMessage,
132+ {void Function (String , String )? writeFile}) async {
127133 var hadCompileTimeError = false ;
128134 void diagnosticMessageHandler (DiagnosticMessage message) {
129135 if (message.severity == Severity .error) {
@@ -161,12 +167,13 @@ Future<CompilationResult> compileToModule(
161167 }
162168 ..explicitExperimentalFlags = options.feExperimentalFlags
163169 ..verbose = false
164- ..onDiagnostic = diagnosticMessageHandler;
170+ ..onDiagnostic = diagnosticMessageHandler
171+ ..fileSystem = fileSystem;
165172 if (options.multiRootScheme != null ) {
166173 compilerOptions.fileSystem = MultiRootFileSystem (
167174 options.multiRootScheme! ,
168175 options.multiRoots.isEmpty ? [Uri .base ] : options.multiRoots,
169- StandardFileSystem .instance );
176+ compilerOptions.fileSystem );
170177 }
171178
172179 Future <Uri ?> resolveUri (Uri ? uri) async {
@@ -229,8 +236,8 @@ Future<CompilationResult> compileToModule(
229236 ClassHierarchy (component, coreTypes) as ClosedWorldClassHierarchy ;
230237 LibraryIndex libraryIndex = LibraryIndex (component, _librariesToIndex);
231238
232- if (options.dumpKernelAfterCfe != null ) {
233- writeComponentToText (component, path : options.dumpKernelAfterCfe! );
239+ if (options.dumpKernelAfterCfe != null && writeFile != null ) {
240+ writeFile ( options.dumpKernelAfterCfe! , writeComponentToString (component) );
234241 }
235242
236243 if (options.deleteToStringPackageUri.isNotEmpty) {
@@ -269,8 +276,8 @@ Future<CompilationResult> compileToModule(
269276 isDynamicSubmodule: isDynamicSubmodule);
270277 target.recordClasses = recordClasses;
271278
272- if (options.dumpKernelBeforeTfa != null ) {
273- writeComponentToText (component, path : options.dumpKernelBeforeTfa! );
279+ if (options.dumpKernelBeforeTfa != null && writeFile != null ) {
280+ writeFile ( options.dumpKernelBeforeTfa! , writeComponentToString (component) );
274281 }
275282
276283 mixin_deduplication.transformLibraries (librariesToTransform);
@@ -313,6 +320,11 @@ Future<CompilationResult> compileToModule(
313320 if (! isDynamicSubmodule) {
314321 _patchMainTearOffs (coreTypes, component);
315322
323+ // We initialize the [printStats] to `false` to prevent it's field
324+ // initializer to run (which only works on VM -- but we want our compiler
325+ // to also run if compiled via dart2js/dart2wasm)
326+ tfa_utils.printStats = false ;
327+
316328 // Keep the flags in-sync with
317329 // pkg/vm/test/transformations/type_flow/transformer_test.dart
318330 globalTypeFlow.transformComponent (target, coreTypes, component,
@@ -454,3 +466,9 @@ String _generateSupportJs(TranslatorOptions options) {
454466 ];
455467 return '(${requiredFeatures .join ('&&' )})' ;
456468}
469+
470+ String writeComponentToString (Component component) {
471+ final buffer = StringBuffer ();
472+ Printer (buffer).writeComponentFile (component);
473+ return '$buffer ' ;
474+ }
0 commit comments