| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.cmdline; | 5 library dart2js.cmdline; |
| 6 | 6 |
| 7 import 'dart:async' show EventSink, Future; | 7 import 'dart:async' show EventSink, Future; |
| 8 import 'dart:convert' show UTF8, LineSplitter; | 8 import 'dart:convert' show UTF8, LineSplitter; |
| 9 import 'dart:io' show exit, File, FileMode, Platform, stdin, stderr; | 9 import 'dart:io' show exit, File, FileMode, Platform, stdin, stderr; |
| 10 | 10 |
| (...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 print(">>> TEST CRASH"); | 828 print(">>> TEST CRASH"); |
| 829 } else { | 829 } else { |
| 830 print(">>> TEST FAIL"); | 830 print(">>> TEST FAIL"); |
| 831 } | 831 } |
| 832 stderr.writeln(">>> EOF STDERR"); | 832 stderr.writeln(">>> EOF STDERR"); |
| 833 subscription.resume(); | 833 subscription.resume(); |
| 834 }); | 834 }); |
| 835 }); | 835 }); |
| 836 } | 836 } |
| 837 | 837 |
| 838 // TODO(johnniwinther): Add corresponding options to the test script and change |
| 839 // these to use 'bool.fromEnvironment'. |
| 838 final bool USE_SERIALIZED_DART_CORE = | 840 final bool USE_SERIALIZED_DART_CORE = |
| 839 Platform.environment['USE_SERIALIZED_DART_CORE'] == 'true'; | 841 Platform.environment['USE_SERIALIZED_DART_CORE'] == 'true'; |
| 840 | 842 |
| 841 /// Mock URI used only in testing when [USE_SERIALIZED_DART_CORE] is enabled. | 843 final bool SERIALIZED_COMPILATION = |
| 844 Platform.environment['SERIALIZED_COMPILATION'] == 'true'; |
| 845 |
| 846 /// Mock URI used only in testing when [USE_SERIALIZED_DART_CORE] or |
| 847 /// [SERIALIZED_COMPILATION] is enabled. |
| 842 final Uri _SERIALIZED_URI = Uri.parse('file:fake.data'); | 848 final Uri _SERIALIZED_URI = Uri.parse('file:fake.data'); |
| 843 | 849 |
| 844 void _useSerializedDataForDartCore(CompileFunc oldCompileFunc) { | 850 void _useSerializedDataForDartCore(CompileFunc oldCompileFunc) { |
| 845 String serializedData; | 851 /// Run the [oldCompileFunc] with [serializedData] added as resolution input. |
| 846 | |
| 847 Future<api.CompilationResult> compileWithSerializedData( | 852 Future<api.CompilationResult> compileWithSerializedData( |
| 848 CompilerOptions compilerOptions, | 853 CompilerOptions compilerOptions, |
| 849 api.CompilerInput compilerInput, | 854 api.CompilerInput compilerInput, |
| 850 api.CompilerDiagnostics compilerDiagnostics, | 855 api.CompilerDiagnostics compilerDiagnostics, |
| 851 api.CompilerOutput compilerOutput) async { | 856 api.CompilerOutput compilerOutput, |
| 852 CompilerImpl compiler = new CompilerImpl( | 857 String serializedData) { |
| 853 compilerInput, compilerOutput, compilerDiagnostics, compilerOptions); | 858 api.CompilerInput input = compilerInput; |
| 854 compiler.serialization.deserializeFromText(_SERIALIZED_URI, serializedData); | 859 CompilerOptions options = compilerOptions; |
| 855 return compiler.run(compilerOptions.entryPoint).then((bool success) { | 860 if (serializedData != null) { |
| 856 return new api.CompilationResult(compiler, isSuccess: success); | 861 input = new _CompilerInput(input, serializedData); |
| 857 }); | 862 List<Uri> resolutionInputs = compilerOptions.resolutionInputs; |
| 863 if (resolutionInputs == null) { |
| 864 resolutionInputs = <Uri>[_SERIALIZED_URI]; |
| 865 } else if (!resolutionInputs.contains(_SERIALIZED_URI)) { |
| 866 resolutionInputs = new List<Uri>.from(resolutionInputs) |
| 867 ..add(_SERIALIZED_URI); |
| 868 } |
| 869 options = options.copy(resolutionInputs: resolutionInputs); |
| 870 } |
| 871 return oldCompileFunc(options, input, compilerDiagnostics, compilerOutput); |
| 858 } | 872 } |
| 859 | 873 |
| 874 /// Serialize [entryPoint] using [serializedData] if provided. |
| 875 Future<api.CompilationResult> serialize( |
| 876 Uri entryPoint, |
| 877 CompilerOptions compilerOptions, |
| 878 api.CompilerInput compilerInput, |
| 879 api.CompilerDiagnostics compilerDiagnostics, |
| 880 api.CompilerOutput compilerOutput, |
| 881 [String serializedData]) { |
| 882 CompilerOptions options = new CompilerOptions.parse( |
| 883 entryPoint: entryPoint, |
| 884 libraryRoot: compilerOptions.libraryRoot, |
| 885 packageRoot: compilerOptions.packageRoot, |
| 886 packageConfig: compilerOptions.packageConfig, |
| 887 packagesDiscoveryProvider: compilerOptions.packagesDiscoveryProvider, |
| 888 environment: compilerOptions.environment, |
| 889 resolutionOutput: _SERIALIZED_URI, |
| 890 options: [Flags.resolveOnly]); |
| 891 return compileWithSerializedData(options, compilerInput, |
| 892 compilerDiagnostics, compilerOutput, serializedData); |
| 893 } |
| 894 |
| 895 // Local cache for the serialized data for dart:core. |
| 896 String serializedDartCore; |
| 897 |
| 898 /// Serialize the entry point using serialized data from dart:core and run |
| 899 /// [oldCompileFunc] using serialized data for whole program. |
| 900 Future<api.CompilationResult> compileFromSerializedData( |
| 901 CompilerOptions compilerOptions, |
| 902 api.CompilerInput compilerInput, |
| 903 api.CompilerDiagnostics compilerDiagnostics, |
| 904 api.CompilerOutput compilerOutput) async { |
| 905 _CompilerOutput output = new _CompilerOutput(); |
| 906 api.CompilationResult result = await serialize( |
| 907 compilerOptions.entryPoint, |
| 908 compilerOptions, |
| 909 compilerInput, |
| 910 compilerDiagnostics, |
| 911 output, |
| 912 serializedDartCore); |
| 913 if (!result.isSuccess) { |
| 914 return result; |
| 915 } |
| 916 return compileWithSerializedData(compilerOptions, compilerInput, |
| 917 compilerDiagnostics, compilerOutput, output.serializedData); |
| 918 } |
| 919 |
| 920 /// Compiles the entry point using the serialized data from dart:core. |
| 921 Future<api.CompilationResult> compileWithSerializedDartCoreData( |
| 922 CompilerOptions compilerOptions, |
| 923 api.CompilerInput compilerInput, |
| 924 api.CompilerDiagnostics compilerDiagnostics, |
| 925 api.CompilerOutput compilerOutput) async { |
| 926 return compileWithSerializedData(compilerOptions, compilerInput, |
| 927 compilerDiagnostics, compilerOutput, serializedDartCore); |
| 928 } |
| 929 |
| 930 /// Serialize dart:core data into [serializedDartCore] and setup the |
| 931 /// [compileFunc] to run the compiler using this data. |
| 860 Future<api.CompilationResult> generateSerializedDataForDartCore( | 932 Future<api.CompilationResult> generateSerializedDataForDartCore( |
| 861 CompilerOptions compilerOptions, | 933 CompilerOptions compilerOptions, |
| 862 api.CompilerInput compilerInput, | 934 api.CompilerInput compilerInput, |
| 863 api.CompilerDiagnostics compilerDiagnostics, | 935 api.CompilerDiagnostics compilerDiagnostics, |
| 864 api.CompilerOutput compilerOutput) async { | 936 api.CompilerOutput compilerOutput) async { |
| 865 _CompilerOutput output = new _CompilerOutput(); | 937 _CompilerOutput output = new _CompilerOutput(); |
| 866 api.CompilationResult result = await oldCompileFunc( | 938 await serialize(Uris.dart_core, compilerOptions, compilerInput, |
| 867 new CompilerOptions.parse( | 939 compilerDiagnostics, output); |
| 868 entryPoint: Uris.dart_core, | 940 serializedDartCore = output.serializedData; |
| 869 libraryRoot: compilerOptions.libraryRoot, | 941 if (SERIALIZED_COMPILATION) { |
| 870 packageRoot: compilerOptions.packageRoot, | 942 compileFunc = compileFromSerializedData; |
| 871 packageConfig: compilerOptions.packageConfig, | 943 } else { |
| 872 packagesDiscoveryProvider: | 944 compileFunc = compileWithSerializedDartCoreData; |
| 873 compilerOptions.packagesDiscoveryProvider, | 945 } |
| 874 environment: compilerOptions.environment, | 946 return compileFunc( |
| 875 resolutionOutput: _SERIALIZED_URI, | |
| 876 options: [Flags.resolveOnly]), | |
| 877 compilerInput, | |
| 878 compilerDiagnostics, | |
| 879 output); | |
| 880 serializedData = output.serializedData; | |
| 881 compileFunc = compileWithSerializedData; | |
| 882 return compileWithSerializedData( | |
| 883 compilerOptions, compilerInput, compilerDiagnostics, compilerOutput); | 947 compilerOptions, compilerInput, compilerDiagnostics, compilerOutput); |
| 884 } | 948 } |
| 885 | 949 |
| 886 compileFunc = generateSerializedDataForDartCore; | 950 compileFunc = generateSerializedDataForDartCore; |
| 887 } | 951 } |
| 888 | 952 |
| 953 class _CompilerInput implements api.CompilerInput { |
| 954 final api.CompilerInput _input; |
| 955 final String _data; |
| 956 |
| 957 _CompilerInput(this._input, this._data); |
| 958 |
| 959 @override |
| 960 Future readFromUri(Uri uri) { |
| 961 if (uri == _SERIALIZED_URI) { |
| 962 return new Future.value(_data); |
| 963 } |
| 964 return _input.readFromUri(uri); |
| 965 } |
| 966 } |
| 967 |
| 889 class _CompilerOutput extends NullCompilerOutput { | 968 class _CompilerOutput extends NullCompilerOutput { |
| 890 _BufferedEventSink sink; | 969 _BufferedEventSink sink; |
| 891 | 970 |
| 892 @override | 971 @override |
| 893 EventSink<String> createEventSink(String name, String extension) { | 972 EventSink<String> createEventSink(String name, String extension) { |
| 894 if (name == '' && extension == 'data') { | 973 if (name == '' && extension == 'data') { |
| 895 return sink = new _BufferedEventSink(); | 974 return sink = new _BufferedEventSink(); |
| 896 } | 975 } |
| 897 return super.createEventSink(name, extension); | 976 return super.createEventSink(name, extension); |
| 898 } | 977 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 911 @override | 990 @override |
| 912 void close() { | 991 void close() { |
| 913 // Do nothing. | 992 // Do nothing. |
| 914 } | 993 } |
| 915 | 994 |
| 916 @override | 995 @override |
| 917 void addError(errorEvent, [StackTrace stackTrace]) { | 996 void addError(errorEvent, [StackTrace stackTrace]) { |
| 918 // Ignore | 997 // Ignore |
| 919 } | 998 } |
| 920 } | 999 } |
| OLD | NEW |