OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:compiler/src/compiler.dart' show Compiler; | 7 import 'package:compiler/src/compiler.dart' show Compiler; |
8 import 'package:compiler/src/elements/elements.dart' show Element; | 8 import 'package:compiler/src/elements/elements.dart' show Element; |
9 import 'package:compiler/src/js_backend/backend.dart' as js | 9 import 'package:compiler/src/js_backend/backend.dart' as js |
10 show JavaScriptBackend; | 10 show JavaScriptBackend; |
11 import 'package:compiler/src/commandline_options.dart' show Flags; | 11 import 'package:compiler/src/commandline_options.dart' show Flags; |
12 import 'package:test/test.dart'; | 12 import 'package:test/test.dart'; |
13 | 13 |
14 import '../memory_compiler.dart'; | 14 import '../memory_compiler.dart'; |
15 | 15 |
16 Future<String> compile(String code, | 16 Future<String> compile(String code, |
17 {String entry: 'main', | 17 {dynamic lookup: 'main', |
18 bool useKernel: true, | 18 bool useKernel: true, |
19 bool disableTypeInference: true, | 19 bool disableTypeInference: true, |
20 List<String> extraOptions: const <String>[]}) async { | 20 List<String> extraOptions: const <String>[]}) async { |
21 List<String> options = <String>[ | 21 List<String> options = <String>[ |
22 Flags.disableInlining, | 22 Flags.disableInlining, |
23 ]; | 23 ]; |
24 if (disableTypeInference) options.add(Flags.disableTypeInference); | 24 if (disableTypeInference) options.add(Flags.disableTypeInference); |
25 if (useKernel) options.add(Flags.useKernel); | 25 if (useKernel) options.add(Flags.useKernel); |
26 options.addAll(extraOptions); | 26 options.addAll(extraOptions); |
27 | 27 |
28 if (entry != 'main' && !code.contains('main')) { | 28 if (lookup is String && lookup != 'main' && !code.contains('main')) { |
29 code = "$code\n\nmain() => $entry;"; | 29 code = "$code\n\nmain() => $lookup;"; |
30 } | 30 } |
31 CompilationResult result = await runCompiler( | 31 CompilationResult result = await runCompiler( |
32 memorySourceFiles: {'main.dart': code}, options: options); | 32 memorySourceFiles: {'main.dart': code}, options: options); |
33 expect(result.isSuccess, isTrue); | 33 expect(result.isSuccess, isTrue); |
34 Compiler compiler = result.compiler; | 34 Compiler compiler = result.compiler; |
35 Element element = compiler.mainApp.find(entry); | 35 Element element; |
| 36 if (lookup is String) { |
| 37 element = compiler.mainApp.find(lookup); |
| 38 } else { |
| 39 element = lookup(compiler); |
| 40 } |
36 js.JavaScriptBackend backend = compiler.backend; | 41 js.JavaScriptBackend backend = compiler.backend; |
37 return backend.getGeneratedCode(element); | 42 return backend.getGeneratedCode(element); |
38 } | 43 } |
39 | 44 |
| 45 /// Checks that the given Dart [code] compiles to the same JS in kernel and |
| 46 /// normal mode. |
| 47 /// |
| 48 /// The function to check at the end is given by [lookup]. If [lookup] is a |
| 49 /// String, then the generated code for a top-level element named [lookup] is |
| 50 /// checked. Otherwise, [lookup] is a function that takes a [Compiler] and |
| 51 /// returns an [Element], and the returned [Element] is checked. |
40 Future check(String code, | 52 Future check(String code, |
41 {String entry: 'main', | 53 {dynamic lookup: 'main', |
42 bool disableTypeInference: true, | 54 bool disableTypeInference: true, |
43 List<String> extraOptions: const <String>[]}) async { | 55 List<String> extraOptions: const <String>[]}) async { |
44 var original = await compile(code, | 56 var original = await compile(code, |
45 entry: entry, | 57 lookup: lookup, |
46 useKernel: false, | 58 useKernel: false, |
47 disableTypeInference: disableTypeInference, | 59 disableTypeInference: disableTypeInference, |
48 extraOptions: extraOptions); | 60 extraOptions: extraOptions); |
49 var kernel = await compile(code, | 61 var kernel = await compile(code, |
50 entry: entry, | 62 lookup: lookup, |
51 useKernel: true, | 63 useKernel: true, |
52 disableTypeInference: disableTypeInference, | 64 disableTypeInference: disableTypeInference, |
53 extraOptions: extraOptions); | 65 extraOptions: extraOptions); |
54 expect(kernel, original); | 66 expect(kernel, original); |
55 } | 67 } |
OLD | NEW |