OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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:io'; | 5 import 'dart:io'; |
6 import 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
7 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
8 import 'package:compiler/src/dart2jslib.dart'; | 8 import 'package:compiler/src/dart2jslib.dart'; |
9 import 'package:compiler/src/elements/elements.dart'; | 9 import 'package:compiler/src/elements/elements.dart'; |
10 import 'package:compiler/src/js_backend/js_backend.dart'; | 10 import 'package:compiler/src/js_backend/js_backend.dart'; |
| 11 import 'package:compiler/src/types/types.dart'; |
| 12 import 'type_mask_test_helper.dart'; |
11 import 'memory_compiler.dart'; | 13 import 'memory_compiler.dart'; |
12 | 14 |
13 const Map MEMORY_SOURCE_FILES = const { | 15 const Map MEMORY_SOURCE_FILES = const { |
14 'main.dart': r""" | 16 'main.dart': r""" |
15 import 'package:expect/expect.dart'; | 17 import 'package:expect/expect.dart'; |
16 | 18 |
17 int method(String arg) => arg.length; | 19 int method(String arg) => arg.length; |
18 | 20 |
19 @AssumeDynamic() | 21 @AssumeDynamic() |
20 int methodAssumeDynamic(String arg) => arg.length; | 22 int methodAssumeDynamic(String arg) => arg.length; |
21 | 23 |
22 @TrustTypeAnnotations() | 24 @TrustTypeAnnotations() |
23 int methodTrustTypeAnnotations(String arg) => arg.length; | 25 int methodTrustTypeAnnotations(String arg) => arg.length; |
24 | 26 |
25 @NoInlining() | 27 @NoInlining() |
26 int methodNoInlining(String arg) => arg.length; | 28 int methodNoInlining(String arg) => arg.length; |
27 | 29 |
28 @NoInlining() @TrustTypeAnnotations() | 30 @NoInlining() @TrustTypeAnnotations() |
29 int methodNoInliningTrustTypeAnnotations(String arg) => arg.length; | 31 int methodNoInliningTrustTypeAnnotations(String arg) => arg.length; |
30 | 32 |
| 33 @AssumeDynamic() @TrustTypeAnnotations() |
| 34 int methodAssumeDynamicTrustTypeAnnotations(String arg) => arg.length; |
| 35 |
| 36 |
31 void main(List<String> args) { | 37 void main(List<String> args) { |
32 print(method(args[0])); | 38 print(method(args[0])); |
33 print(methodAssumeDynamic('foo')); | 39 print(methodAssumeDynamic('foo')); |
34 print(methodTrustTypeAnnotations(null)); | 40 print(methodTrustTypeAnnotations(42)); |
| 41 print(methodTrustTypeAnnotations("fourtyTwo")); |
35 print(methodNoInlining('bar')); | 42 print(methodNoInlining('bar')); |
36 print(methodNoInliningTrustTypeAnnotations(null)); | 43 print(methodNoInliningTrustTypeAnnotations(42)); |
| 44 print(methodNoInliningTrustTypeAnnotations("fourtyTwo")); |
| 45 print(methodAssumeDynamicTrustTypeAnnotations(null)); |
37 } | 46 } |
38 """ | 47 """ |
39 }; | 48 }; |
40 | 49 |
41 main() { | 50 main() { |
42 Compiler compiler = compilerFor(MEMORY_SOURCE_FILES); | 51 Compiler compiler = compilerFor(MEMORY_SOURCE_FILES); |
43 asyncTest(() => compiler.runCompiler(Uri.parse('memory:main.dart')).then((_) { | 52 asyncTest(() => compiler.runCompiler(Uri.parse('memory:main.dart')).then((_) { |
44 Expect.isFalse(compiler.compilationFailed, 'Unsuccessful compilation'); | 53 Expect.isFalse(compiler.compilationFailed, 'Unsuccessful compilation'); |
45 JavaScriptBackend backend = compiler.backend; | 54 JavaScriptBackend backend = compiler.backend; |
46 Expect.isNotNull(backend.annotations.expectNoInliningClass, | 55 Expect.isNotNull(backend.annotations.expectNoInliningClass, |
47 'NoInliningClass is unresolved.'); | 56 'NoInliningClass is unresolved.'); |
48 Expect.isNotNull(backend.annotations.expectTrustTypeAnnotationsClass, | 57 Expect.isNotNull(backend.annotations.expectTrustTypeAnnotationsClass, |
49 'TrustTypeAnnotations is unresolved.'); | 58 'TrustTypeAnnotations is unresolved.'); |
50 Expect.isNotNull(backend.annotations.expectAssumeDynamicClass, | 59 Expect.isNotNull(backend.annotations.expectAssumeDynamicClass, |
51 'AssumeDynamicClass is unresolved.'); | 60 'AssumeDynamicClass is unresolved.'); |
52 | 61 |
| 62 void testTypeMatch(FunctionElement function, TypeMask expectedParameterType, |
| 63 TypeMask expectedReturnType, TypesInferrer inferrer) { |
| 64 for (ParameterElement parameter in function.parameters) { |
| 65 TypeMask type = inferrer.getTypeOfElement(parameter); |
| 66 Expect.equals(expectedParameterType, simplify(type, compiler), |
| 67 "$parameter"); |
| 68 } |
| 69 if (expectedReturnType != null) { |
| 70 TypeMask type = inferrer.getReturnTypeOfElement(function); |
| 71 Expect.equals(expectedReturnType, simplify(type, compiler), |
| 72 "$function"); |
| 73 } |
| 74 } |
| 75 |
53 void test(String name, | 76 void test(String name, |
54 {bool expectNoInlining: false, | 77 {bool expectNoInlining: false, |
55 bool expectTrustTypeAnnotations: false, | 78 bool expectTrustTypeAnnotations: false, |
| 79 TypeMask expectedParameterType: null, |
| 80 TypeMask expectedReturnType: null, |
56 bool expectAssumeDynamic: false}) { | 81 bool expectAssumeDynamic: false}) { |
57 Element method = compiler.mainApp.find(name); | 82 Element method = compiler.mainApp.find(name); |
58 Expect.isNotNull(method); | 83 Expect.isNotNull(method); |
59 Expect.equals( | 84 Expect.equals( |
60 expectNoInlining, | 85 expectNoInlining, |
61 backend.annotations.noInlining(method), | 86 backend.annotations.noInlining(method), |
62 "Unexpected annotation of @NoInlining on '$method'."); | 87 "Unexpected annotation of @NoInlining on '$method'."); |
63 Expect.equals( | 88 Expect.equals( |
64 expectTrustTypeAnnotations, | 89 expectTrustTypeAnnotations, |
65 backend.annotations.trustTypeAnnotations(method), | 90 backend.annotations.trustTypeAnnotations(method), |
66 "Unexpected annotation of @TrustTypeAnnotations on '$method'."); | 91 "Unexpected annotation of @TrustTypeAnnotations on '$method'."); |
67 Expect.equals( | 92 Expect.equals( |
68 expectAssumeDynamic, | 93 expectAssumeDynamic, |
69 backend.annotations.assumeDynamic(method), | 94 backend.annotations.assumeDynamic(method), |
70 "Unexpected annotation of @AssumeDynamic on '$method'."); | 95 "Unexpected annotation of @AssumeDynamic on '$method'."); |
| 96 TypesInferrer inferrer = compiler.typesTask.typesInferrer; |
| 97 if (expectTrustTypeAnnotations && expectedParameterType != null) { |
| 98 testTypeMatch(method, expectedParameterType, expectedReturnType, |
| 99 inferrer); |
| 100 } else if (expectAssumeDynamic) { |
| 101 testTypeMatch(method, compiler.typesTask.dynamicType, null, inferrer); |
| 102 } |
71 } | 103 } |
72 | 104 |
| 105 TypeMask jsStringType = compiler.typesTask.stringType; |
| 106 TypeMask jsIntType = compiler.typesTask.intType; |
| 107 TypeMask coreStringType = new TypeMask.subtype(compiler.stringClass, |
| 108 compiler.world); |
| 109 |
73 test('method'); | 110 test('method'); |
74 test('methodAssumeDynamic', expectAssumeDynamic: true); | 111 test('methodAssumeDynamic', expectAssumeDynamic: true); |
75 test('methodTrustTypeAnnotations', expectTrustTypeAnnotations: true); | 112 test('methodTrustTypeAnnotations', |
| 113 expectTrustTypeAnnotations: true, |
| 114 expectedParameterType: jsStringType); |
76 test('methodNoInlining', expectNoInlining: true); | 115 test('methodNoInlining', expectNoInlining: true); |
77 test('methodNoInliningTrustTypeAnnotations', | 116 test('methodNoInliningTrustTypeAnnotations', |
78 expectNoInlining: true, | 117 expectNoInlining: true, |
79 expectTrustTypeAnnotations: true); | 118 expectTrustTypeAnnotations: true, |
| 119 expectedParameterType: jsStringType, |
| 120 expectedReturnType: jsIntType); |
| 121 test('methodAssumeDynamicTrustTypeAnnotations', |
| 122 expectAssumeDynamic: true, |
| 123 expectTrustTypeAnnotations: true, |
| 124 expectedParameterType: coreStringType); |
| 125 |
80 })); | 126 })); |
81 } | 127 } |
OLD | NEW |