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