| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library test.kernel.closures.suite; | 5 library test.kernel.closures.suite; |
| 6 | 6 |
| 7 import 'dart:io' show File; |
| 8 |
| 7 import 'dart:async' show Future; | 9 import 'dart:async' show Future; |
| 8 | 10 |
| 9 import 'package:front_end/physical_file_system.dart' show PhysicalFileSystem; | 11 import 'package:front_end/physical_file_system.dart' show PhysicalFileSystem; |
| 10 | 12 |
| 11 import 'package:kernel/core_types.dart' show CoreTypes; | 13 import 'package:kernel/core_types.dart' show CoreTypes; |
| 12 | 14 |
| 13 import 'package:testing/testing.dart' | 15 import 'package:testing/testing.dart' |
| 14 show Chain, ChainContext, Result, Step, TestDescription, runMe; | 16 show |
| 17 Chain, |
| 18 ChainContext, |
| 19 Result, |
| 20 Step, |
| 21 TestDescription, |
| 22 runMe, |
| 23 StdioProcess; |
| 15 | 24 |
| 16 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart' | 25 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart' |
| 17 show computePatchedSdk; | 26 show computePatchedSdk; |
| 18 | 27 |
| 19 import 'package:kernel/ast.dart' show Program, Library; | 28 import 'package:kernel/ast.dart' show Program, Library; |
| 20 | 29 |
| 21 import 'package:kernel/transformations/closure_conversion.dart' | |
| 22 as closure_conversion; | |
| 23 | |
| 24 import 'package:front_end/src/fasta/testing/kernel_chain.dart' | 30 import 'package:front_end/src/fasta/testing/kernel_chain.dart' |
| 25 show Print, MatchExpectation, WriteDill, ReadDill, Verify; | 31 show Print, MatchExpectation, WriteDill, ReadDill, Verify; |
| 26 | 32 |
| 27 import 'package:front_end/src/fasta/ticker.dart' show Ticker; | 33 import 'package:front_end/src/fasta/ticker.dart' show Ticker; |
| 28 | 34 |
| 29 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; | 35 import 'package:front_end/src/fasta/dill/dill_target.dart' show DillTarget; |
| 30 | 36 |
| 31 import 'package:front_end/src/fasta/kernel/kernel_target.dart' | 37 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
| 32 show KernelTarget; | 38 show KernelTarget; |
| 33 | 39 |
| 34 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; | 40 import 'package:front_end/src/fasta/translate_uri.dart' show TranslateUri; |
| 35 | 41 |
| 36 import 'package:front_end/src/fasta/errors.dart' show InputError; | 42 import 'package:front_end/src/fasta/errors.dart' show InputError; |
| 37 | 43 |
| 38 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; | 44 import 'package:front_end/src/fasta/testing/patched_sdk_location.dart'; |
| 39 | 45 |
| 40 import 'package:kernel/kernel.dart' show loadProgramFromBinary; | 46 import 'package:kernel/kernel.dart' show loadProgramFromBinary; |
| 41 | 47 |
| 48 import 'package:kernel/transformations/closure_conversion.dart' |
| 49 as closure_conversion; |
| 50 |
| 42 import 'package:kernel/target/targets.dart' show TargetFlags; | 51 import 'package:kernel/target/targets.dart' show TargetFlags; |
| 43 | 52 |
| 44 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; | 53 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; |
| 45 | 54 |
| 46 const String STRONG_MODE = " strong mode "; | 55 const String STRONG_MODE = " strong mode "; |
| 47 | 56 |
| 48 class ClosureConversionContext extends ChainContext { | 57 class ClosureConversionContext extends ChainContext { |
| 49 final bool strongMode; | |
| 50 | |
| 51 final TranslateUri uriTranslator; | 58 final TranslateUri uriTranslator; |
| 52 | |
| 53 final List<Step> steps; | 59 final List<Step> steps; |
| 60 final Uri vm; |
| 54 | 61 |
| 55 ClosureConversionContext( | 62 ClosureConversionContext( |
| 56 this.strongMode, bool updateExpectations, this.uriTranslator) | 63 this.vm, bool strongMode, bool updateExpectations, this.uriTranslator) |
| 57 : steps = <Step>[ | 64 : steps = <Step>[ |
| 58 const FastaCompile(), | 65 new FastaCompile(strongMode), |
| 59 const Print(), | 66 const Print(), |
| 60 const Verify(true), | 67 const Verify(true), |
| 61 const ClosureConversion(), | 68 const ConvertClosures(), |
| 62 const Print(), | 69 const Print(), |
| 63 const Verify(true), | 70 const Verify(true), |
| 64 new MatchExpectation(".expect", | 71 new MatchExpectation(".expect", |
| 65 updateExpectations: updateExpectations), | 72 updateExpectations: updateExpectations), |
| 66 const WriteDill(), | 73 const WriteDill(), |
| 67 const ReadDill(), | 74 const ReadDill(), |
| 68 // TODO(29143): add `Run` step when Vectors are added to VM. | 75 const Run(), |
| 69 ]; | 76 ]; |
| 70 | 77 |
| 71 Future<Program> loadPlatform() async { | 78 // The platform in these tests are reloaded for each testscase, because the |
| 72 Uri sdk = await computePatchedSdk(); | 79 // closure conversion transformation is performed during the load, and it's |
| 73 return loadProgramFromBinary(sdk.resolve('platform.dill').toFilePath()); | 80 // not an idempotent transformation yet. |
| 81 Future<Program> loadPlatform() { |
| 82 return new Future<Program>(() async { |
| 83 Uri sdk = await computePatchedSdk(); |
| 84 return loadProgramFromBinary(sdk.resolve('platform.dill').toFilePath()); |
| 85 }); |
| 74 } | 86 } |
| 75 | 87 |
| 76 static Future<ClosureConversionContext> create( | 88 static Future<ClosureConversionContext> create( |
| 77 Chain suite, Map<String, String> environment) async { | 89 Chain suite, Map<String, String> environment) async { |
| 78 Uri sdk = await computePatchedSdk(); | 90 Uri sdk = await computePatchedSdk(); |
| 91 Uri vm = computeDartVm(sdk); |
| 79 Uri packages = Uri.base.resolve(".packages"); | 92 Uri packages = Uri.base.resolve(".packages"); |
| 80 bool strongMode = environment.containsKey(STRONG_MODE); | 93 bool strongMode = environment.containsKey(STRONG_MODE); |
| 81 bool updateExpectations = environment["updateExpectations"] == "true"; | 94 bool updateExpectations = environment["updateExpectations"] == "true"; |
| 82 TranslateUri uriTranslator = await TranslateUri | 95 TranslateUri uriTranslator = await TranslateUri |
| 83 .parse(PhysicalFileSystem.instance, sdk, packages: packages); | 96 .parse(PhysicalFileSystem.instance, sdk, packages: packages); |
| 84 return new ClosureConversionContext( | 97 return new ClosureConversionContext( |
| 85 strongMode, updateExpectations, uriTranslator); | 98 vm, strongMode, updateExpectations, uriTranslator); |
| 86 } | 99 } |
| 87 } | 100 } |
| 88 | 101 |
| 89 Future<ClosureConversionContext> createContext( | 102 Future<ClosureConversionContext> createContext( |
| 90 Chain suite, Map<String, String> environment) async { | 103 Chain suite, Map<String, String> environment) async { |
| 91 environment["updateExpectations"] = | 104 environment["updateExpectations"] = |
| 92 const String.fromEnvironment("updateExpectations"); | 105 const String.fromEnvironment("updateExpectations"); |
| 93 return ClosureConversionContext.create(suite, environment); | 106 return ClosureConversionContext.create(suite, environment); |
| 94 } | 107 } |
| 95 | 108 |
| 96 class FastaCompile | 109 class FastaCompile |
| 97 extends Step<TestDescription, Program, ClosureConversionContext> { | 110 extends Step<TestDescription, Program, ClosureConversionContext> { |
| 98 const FastaCompile(); | 111 final bool strongMode; |
| 112 |
| 113 const FastaCompile(this.strongMode); |
| 99 | 114 |
| 100 String get name => "fasta compilation"; | 115 String get name => "fasta compilation"; |
| 101 | 116 |
| 102 Future<Result<Program>> run( | 117 Future<Result<Program>> run( |
| 103 TestDescription description, ClosureConversionContext context) async { | 118 TestDescription description, ClosureConversionContext context) async { |
| 104 Program platform = await context.loadPlatform(); | 119 Program platform = await context.loadPlatform(); |
| 105 Ticker ticker = new Ticker(); | 120 Ticker ticker = new Ticker(); |
| 106 DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator, | 121 DillTarget dillTarget = new DillTarget(ticker, context.uriTranslator, |
| 107 new VmFastaTarget(new TargetFlags(strongMode: context.strongMode))); | 122 new VmFastaTarget(new TargetFlags(strongMode: this.strongMode))); |
| 108 platform.unbindCanonicalNames(); | 123 platform.unbindCanonicalNames(); |
| 109 dillTarget.loader.appendLibraries(platform); | 124 dillTarget.loader.appendLibraries(platform); |
| 110 KernelTarget sourceTarget = new KernelTarget( | 125 KernelTarget sourceTarget = new KernelTarget( |
| 111 PhysicalFileSystem.instance, dillTarget, context.uriTranslator); | 126 PhysicalFileSystem.instance, dillTarget, context.uriTranslator); |
| 112 | 127 |
| 113 Program p; | 128 Program p; |
| 114 try { | 129 try { |
| 115 sourceTarget.read(description.uri); | 130 sourceTarget.read(description.uri); |
| 116 await dillTarget.buildOutlines(); | 131 await dillTarget.buildOutlines(); |
| 117 await sourceTarget.buildOutlines(); | 132 await sourceTarget.buildOutlines(); |
| 118 p = await sourceTarget.buildProgram(); | 133 p = await sourceTarget.buildProgram(); |
| 119 } on InputError catch (e, s) { | 134 } on InputError catch (e, s) { |
| 120 return fail(null, e.error, s); | 135 return fail(null, e.error, s); |
| 121 } | 136 } |
| 122 return pass(p); | 137 return pass(p); |
| 123 } | 138 } |
| 124 } | 139 } |
| 125 | 140 |
| 126 class ClosureConversion | 141 class ConvertClosures extends Step<Program, Program, ClosureConversionContext> { |
| 127 extends Step<Program, Program, ClosureConversionContext> { | |
| 128 const ClosureConversion(); | |
| 129 | |
| 130 String get name => "closure conversion"; | 142 String get name => "closure conversion"; |
| 131 | 143 |
| 144 const ConvertClosures(); |
| 145 |
| 132 Future<Result<Program>> run( | 146 Future<Result<Program>> run( |
| 133 Program program, ClosureConversionContext testContext) async { | 147 Program program, ClosureConversionContext context) async { |
| 134 try { | 148 try { |
| 135 CoreTypes coreTypes = new CoreTypes(program); | 149 CoreTypes coreTypes = new CoreTypes(program); |
| 136 Library library = program.libraries | 150 Library library = program.libraries |
| 137 .firstWhere((Library library) => library.importUri.scheme != "dart"); | 151 .firstWhere((Library library) => library.importUri.scheme != "dart"); |
| 138 closure_conversion.transformLibraries(coreTypes, <Library>[library]); | 152 closure_conversion.transformLibraries(coreTypes, <Library>[library]); |
| 139 return pass(program); | |
| 140 } catch (e, s) { | 153 } catch (e, s) { |
| 141 return crash(e, s); | 154 return crash(e, s); |
| 142 } | 155 } |
| 156 return pass(program); |
| 143 } | 157 } |
| 144 } | 158 } |
| 145 | 159 |
| 160 class Run extends Step<Uri, int, ClosureConversionContext> { |
| 161 const Run(); |
| 162 |
| 163 String get name => "run"; |
| 164 |
| 165 Future<Result<int>> run(Uri uri, ClosureConversionContext context) async { |
| 166 File generated = new File.fromUri(uri); |
| 167 StdioProcess process; |
| 168 try { |
| 169 process = |
| 170 await StdioProcess.run(context.vm.toFilePath(), [generated.path]); |
| 171 print(process.output); |
| 172 } finally { |
| 173 generated.parent.delete(recursive: true); |
| 174 } |
| 175 return process.toResult(); |
| 176 } |
| 177 } |
| 178 |
| 146 main(List<String> arguments) => runMe(arguments, createContext, "testing.json"); | 179 main(List<String> arguments) => runMe(arguments, createContext, "testing.json"); |
| OLD | NEW |