OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library tests.dart2js.interop_anonymous_unreachable_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:test/test.dart'; |
| 10 import 'compiler_helper.dart'; |
| 11 |
| 12 |
| 13 main() { |
| 14 test("unreachable code doesn't crash the compiler", () async { |
| 15 // This test is a regression for Issue #24974 |
| 16 String generated = await compile(""" |
| 17 import 'package:js/js.dart'; |
| 18 |
| 19 @JS() @anonymous |
| 20 class UniqueLongNameForTesting_A { |
| 21 external factory UniqueLongNameForTesting_A(); |
| 22 } |
| 23 main() {} |
| 24 """, returnAll: true); |
| 25 |
| 26 // the code should not be included in the output either. |
| 27 expect(generated, isNot(contains("UniqueLongNameForTesting_A"))); |
| 28 }); |
| 29 |
| 30 group('tree-shaking interop types', () { |
| 31 String program = """ |
| 32 import 'package:js/js.dart'; |
| 33 |
| 34 // reachable and allocated |
| 35 @JS() @anonymous |
| 36 class UniqueLongNameForTesting_A { |
| 37 external bool get x; |
| 38 external UniqueLongNameForTesting_D get d; |
| 39 external UniqueLongNameForTesting_E get e; |
| 40 external factory UniqueLongNameForTesting_A( |
| 41 {UniqueLongNameForTesting_B arg0}); |
| 42 } |
| 43 |
| 44 // visible through the parameter above, but not used. |
| 45 @JS() @anonymous |
| 46 class UniqueLongNameForTesting_B { |
| 47 external factory UniqueLongNameForTesting_B(); |
| 48 } |
| 49 |
| 50 // unreachable |
| 51 @JS() @anonymous |
| 52 class UniqueLongNameForTesting_C { |
| 53 external factory UniqueLongNameForTesting_C(); |
| 54 } |
| 55 |
| 56 // visible and reached through `d`. |
| 57 @JS() @anonymous |
| 58 class UniqueLongNameForTesting_D { |
| 59 external factory UniqueLongNameForTesting_D(); |
| 60 } |
| 61 |
| 62 // visible through `e`, but not reached. |
| 63 @JS() @anonymous |
| 64 class UniqueLongNameForTesting_E { |
| 65 external factory UniqueLongNameForTesting_E(); |
| 66 } |
| 67 |
| 68 main() { |
| 69 print(new UniqueLongNameForTesting_A().x); |
| 70 print(new UniqueLongNameForTesting_A().d); |
| 71 } |
| 72 """; |
| 73 |
| 74 test('no tree-shaking by default', () async { |
| 75 String generated = await compile(program, returnAll: true); |
| 76 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 77 expect(generated.contains("UniqueLongNameForTesting_D"), isTrue); |
| 78 |
| 79 expect(generated.contains("UniqueLongNameForTesting_B"), isTrue); |
| 80 expect(generated.contains("UniqueLongNameForTesting_C"), isTrue); |
| 81 expect(generated.contains("UniqueLongNameForTesting_E"), isTrue); |
| 82 }); |
| 83 |
| 84 test('tree-shake when using flag', () async { |
| 85 String generated = await compile(program, |
| 86 trustJSInteropTypeAnnotations: true, |
| 87 returnAll: true); |
| 88 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 89 expect(generated.contains("UniqueLongNameForTesting_D"), isTrue); |
| 90 |
| 91 expect(generated.contains("UniqueLongNameForTesting_B"), isFalse); |
| 92 expect(generated.contains("UniqueLongNameForTesting_C"), isFalse); |
| 93 expect(generated.contains("UniqueLongNameForTesting_E"), isFalse); |
| 94 }); |
| 95 }); |
| 96 |
| 97 group('tree-shaking other native types', () { |
| 98 String program = """ |
| 99 import 'dart:html'; |
| 100 import 'package:js/js.dart'; |
| 101 |
| 102 @JS() @anonymous |
| 103 class UniqueLongNameForTesting_A { |
| 104 external dynamic get x; |
| 105 } |
| 106 |
| 107 @JS() @anonymous |
| 108 class UniqueLongNameForTesting_B { |
| 109 external dynamic get y; |
| 110 } |
| 111 |
| 112 main() { |
| 113 print(new UniqueLongNameForTesting_A().x); |
| 114 } |
| 115 """; |
| 116 |
| 117 test('allocation effect of dynamic excludes native types', () async { |
| 118 String generated = await compile(program, returnAll: true); |
| 119 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 120 // any js-interop type could be allocated by `get x` |
| 121 expect(generated.contains("UniqueLongNameForTesting_B"), isTrue); |
| 122 // but we exclude other native types like HTMLAudioElement |
| 123 expect(generated.contains("HTMLAudioElement"), isFalse); |
| 124 }); |
| 125 |
| 126 test('allocation effect of dynamic excludes native types [flag]', () async { |
| 127 // Trusting types doesn't make a difference. |
| 128 String generated = await compile(program, |
| 129 trustJSInteropTypeAnnotations: true, |
| 130 returnAll: true); |
| 131 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 132 expect(generated.contains("UniqueLongNameForTesting_B"), isTrue); |
| 133 expect(generated.contains("HTMLAudioElement"), isFalse); |
| 134 }); |
| 135 |
| 136 test('declared native types are included in allocation effect', () async { |
| 137 String program2 = """ |
| 138 import 'dart:html'; |
| 139 import 'package:js/js.dart'; |
| 140 |
| 141 @JS() @anonymous |
| 142 class UniqueLongNameForTesting_A { |
| 143 external AudioElement get x; |
| 144 } |
| 145 |
| 146 main() { |
| 147 print(new UniqueLongNameForTesting_A().x is AudioElement); |
| 148 } |
| 149 """; |
| 150 |
| 151 String generated = await compile(program2, returnAll: true); |
| 152 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 153 expect(generated.contains("HTMLAudioElement"), isTrue); |
| 154 |
| 155 program2 = """ |
| 156 import 'dart:html'; |
| 157 import 'package:js/js.dart'; |
| 158 |
| 159 @JS() @anonymous |
| 160 class UniqueLongNameForTesting_A { |
| 161 external dynamic get x; |
| 162 } |
| 163 |
| 164 main() { |
| 165 print(new UniqueLongNameForTesting_A().x is AudioElement); |
| 166 } |
| 167 """; |
| 168 |
| 169 generated = await compile(program2, returnAll: true); |
| 170 expect(generated.contains("UniqueLongNameForTesting_A"), isTrue); |
| 171 // This extra check is to make sure that we don't include HTMLAudioElement |
| 172 // just because of the is-check. It is optimized away in this case because |
| 173 // we believe it was never instantiated. |
| 174 expect(generated.contains("HTMLAudioElement"), isFalse); |
| 175 }); |
| 176 }); |
| 177 } |
OLD | NEW |