| 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 | 2 |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 import 'dart:collection' show HashMap, HashSet; | 6 import 'dart:collection' show HashMap, HashSet; |
| 7 import 'dart:math' show min, max; | 7 import 'dart:math' show min, max; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| (...skipping 1313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1324 // classes and preserve the Dart initialization order. | 1324 // classes and preserve the Dart initialization order. |
| 1325 // | 1325 // |
| 1326 // Instead we use the same trick as named constructors, and do them as | 1326 // Instead we use the same trick as named constructors, and do them as |
| 1327 // instance methods that perform initialization. | 1327 // instance methods that perform initialization. |
| 1328 // | 1328 // |
| 1329 // Therefore, dart:core Object gets the one real `constructor` and | 1329 // Therefore, dart:core Object gets the one real `constructor` and |
| 1330 // immediately bounces to the `new() { ... }` initializer, letting us | 1330 // immediately bounces to the `new() { ... }` initializer, letting us |
| 1331 // bypass the ES6 restrictions. | 1331 // bypass the ES6 restrictions. |
| 1332 // | 1332 // |
| 1333 // TODO(jmesserly): we'll need to rethink this. | 1333 // TODO(jmesserly): we'll need to rethink this. |
| 1334 // See <https://github.com/dart-lang/dev_compiler/issues/51>. | 1334 // See https://github.com/dart-lang/sdk/issues/28322. |
| 1335 // This level of indirection will hurt performance. | 1335 // This level of indirection will hurt performance. |
| 1336 jsMethods.add(new JS.Method( | 1336 jsMethods.add(new JS.Method( |
| 1337 _propertyName('constructor'), | 1337 _propertyName('constructor'), |
| 1338 js.call('function(...args) { return this.new.apply(this, args); }') | 1338 js.call('function(...args) { return this.new.apply(this, args); }') |
| 1339 as JS.Fun)); | 1339 as JS.Fun)); |
| 1340 } else if (ctors.isEmpty) { | 1340 } else if (ctors.isEmpty) { |
| 1341 jsMethods.add(_emitImplicitConstructor(node, fields, virtualFields)); | 1341 jsMethods.add(_emitImplicitConstructor(node, fields, virtualFields)); |
| 1342 } | 1342 } |
| 1343 | 1343 |
| 1344 bool hasJsPeer = findAnnotation(element, isJsPeerInterface) != null; | 1344 bool hasJsPeer = findAnnotation(element, isJsPeerInterface) != null; |
| (...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2286 paramName, | 2286 paramName, |
| 2287 _defaultParamValue(param), | 2287 _defaultParamValue(param), |
| 2288 ])); | 2288 ])); |
| 2289 } else if (param.kind == ParameterKind.POSITIONAL) { | 2289 } else if (param.kind == ParameterKind.POSITIONAL) { |
| 2290 body.add(js.statement('if (# === void 0) # = #;', | 2290 body.add(js.statement('if (# === void 0) # = #;', |
| 2291 [jsParam, jsParam, _defaultParamValue(param)])); | 2291 [jsParam, jsParam, _defaultParamValue(param)])); |
| 2292 } | 2292 } |
| 2293 } | 2293 } |
| 2294 | 2294 |
| 2295 // TODO(jmesserly): various problems here, see: | 2295 // TODO(jmesserly): various problems here, see: |
| 2296 // https://github.com/dart-lang/dev_compiler/issues/116 | 2296 // https://github.com/dart-lang/sdk/issues/27259 |
| 2297 var paramType = | 2297 var paramType = |
| 2298 resolutionMap.elementDeclaredByFormalParameter(param).type; | 2298 resolutionMap.elementDeclaredByFormalParameter(param).type; |
| 2299 if (node is MethodDeclaration && | 2299 if (node is MethodDeclaration && |
| 2300 (resolutionMap.elementDeclaredByFormalParameter(param).isCovariant || | 2300 (resolutionMap.elementDeclaredByFormalParameter(param).isCovariant || |
| 2301 _unsoundCovariant(paramType, true)) && | 2301 _unsoundCovariant(paramType, true)) && |
| 2302 !_inWhitelistCode(node)) { | 2302 !_inWhitelistCode(node)) { |
| 2303 var castType = _emitType(paramType, | 2303 var castType = _emitType(paramType, |
| 2304 nameType: options.nameTypeTests || options.hoistTypeTests, | 2304 nameType: options.nameTypeTests || options.hoistTypeTests, |
| 2305 hoistType: options.hoistTypeTests); | 2305 hoistType: options.hoistTypeTests); |
| 2306 body.add(js.statement('#._check(#);', [castType, jsParam])); | 2306 body.add(js.statement('#._check(#);', [castType, jsParam])); |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2641 // }, T, <args>).bind(this); | 2641 // }, T, <args>).bind(this); |
| 2642 // } | 2642 // } |
| 2643 // | 2643 // |
| 2644 // We need to include <args> in case any are mutated, so each `.iterator` | 2644 // We need to include <args> in case any are mutated, so each `.iterator` |
| 2645 // gets the same initial values. | 2645 // gets the same initial values. |
| 2646 // | 2646 // |
| 2647 // TODO(jmesserly): we could omit the args for the common case where args | 2647 // TODO(jmesserly): we could omit the args for the common case where args |
| 2648 // are not mutated inside the generator. | 2648 // are not mutated inside the generator. |
| 2649 // | 2649 // |
| 2650 // In the future, we might be able to simplify this, see: | 2650 // In the future, we might be able to simplify this, see: |
| 2651 // https://github.com/dart-lang/dev_compiler/issues/247. | 2651 // https://github.com/dart-lang/sdk/issues/28320 |
| 2652 // | |
| 2653 // `async` works the same, but uses the `dart.async` helper. | 2652 // `async` works the same, but uses the `dart.async` helper. |
| 2654 // | 2653 // |
| 2655 // In the body of a `sync*` and `async`, `yield`/`await` are both generated | 2654 // In the body of a `sync*` and `async`, `yield`/`await` are both generated |
| 2656 // simply as `yield`. | 2655 // simply as `yield`. |
| 2657 // | 2656 // |
| 2658 // `async*` uses the `dart.asyncStar` helper, and also has an extra `stream` | 2657 // `async*` uses the `dart.asyncStar` helper, and also has an extra `stream` |
| 2659 // argument to the generator, which is used for passing values to the | 2658 // argument to the generator, which is used for passing values to the |
| 2660 // _AsyncStarStreamController implementation type. | 2659 // _AsyncStarStreamController implementation type. |
| 2661 // `yield` is specially generated inside `async*`, see visitYieldStatement. | 2660 // `yield` is specially generated inside `async*`, see visitYieldStatement. |
| 2662 // `await` is generated as `yield`. | 2661 // `await` is generated as `yield`. |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2949 optionalTypes, parameters?.sublist(parameterTypes.length), | 2948 optionalTypes, parameters?.sublist(parameterTypes.length), |
| 2950 nameType: nameType, hoistType: hoistType); | 2949 nameType: nameType, hoistType: hoistType); |
| 2951 typeParts = [rt, ra, oa]; | 2950 typeParts = [rt, ra, oa]; |
| 2952 } else { | 2951 } else { |
| 2953 typeParts = [rt, ra]; | 2952 typeParts = [rt, ra]; |
| 2954 } | 2953 } |
| 2955 | 2954 |
| 2956 var typeFormals = type.typeFormals; | 2955 var typeFormals = type.typeFormals; |
| 2957 if (typeFormals.isNotEmpty && !lowerTypedef) { | 2956 if (typeFormals.isNotEmpty && !lowerTypedef) { |
| 2958 // TODO(jmesserly): this is a suboptimal representation for universal | 2957 // TODO(jmesserly): this is a suboptimal representation for universal |
| 2959 // function types (as callable functions). See discussion at: | 2958 // function types (as callable functions). See discussion at |
| 2960 // https://github.com/dart-lang/dev_compiler/issues/526 | 2959 // https://github.com/dart-lang/sdk/issues/27333 |
| 2961 var tf = _emitTypeFormals(typeFormals); | 2960 var tf = _emitTypeFormals(typeFormals); |
| 2962 var names = _typeTable.discharge(typeFormals); | 2961 var names = _typeTable.discharge(typeFormals); |
| 2963 var parts = new JS.ArrayInitializer(typeParts); | 2962 var parts = new JS.ArrayInitializer(typeParts); |
| 2964 if (names.isEmpty) { | 2963 if (names.isEmpty) { |
| 2965 typeParts = [ | 2964 typeParts = [ |
| 2966 js.call('(#) => #', [tf, parts]) | 2965 js.call('(#) => #', [tf, parts]) |
| 2967 ]; | 2966 ]; |
| 2968 } else { | 2967 } else { |
| 2969 typeParts = [ | 2968 typeParts = [ |
| 2970 js.call('(#) => {#; return #;}', [tf, names, parts]) | 2969 js.call('(#) => {#; return #;}', [tf, names, parts]) |
| (...skipping 2665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5636 } | 5635 } |
| 5637 var type = functionType.returnType; | 5636 var type = functionType.returnType; |
| 5638 | 5637 |
| 5639 InterfaceType expectedType = null; | 5638 InterfaceType expectedType = null; |
| 5640 if (element.isAsynchronous) { | 5639 if (element.isAsynchronous) { |
| 5641 if (element.isGenerator) { | 5640 if (element.isGenerator) { |
| 5642 // Stream<T> -> T | 5641 // Stream<T> -> T |
| 5643 expectedType = types.streamType; | 5642 expectedType = types.streamType; |
| 5644 } else { | 5643 } else { |
| 5645 // Future<T> -> T | 5644 // Future<T> -> T |
| 5646 // TODO(vsm): Revisit with issue #228. | |
| 5647 expectedType = types.futureType; | 5645 expectedType = types.futureType; |
| 5648 } | 5646 } |
| 5649 } else { | 5647 } else { |
| 5650 if (element.isGenerator) { | 5648 if (element.isGenerator) { |
| 5651 // Iterable<T> -> T | 5649 // Iterable<T> -> T |
| 5652 expectedType = types.iterableType; | 5650 expectedType = types.iterableType; |
| 5653 } else { | 5651 } else { |
| 5654 // T -> T | 5652 // T -> T |
| 5655 return type; | 5653 return type; |
| 5656 } | 5654 } |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5815 if (targetIdentifier.staticElement is! PrefixElement) return false; | 5813 if (targetIdentifier.staticElement is! PrefixElement) return false; |
| 5816 var prefix = targetIdentifier.staticElement as PrefixElement; | 5814 var prefix = targetIdentifier.staticElement as PrefixElement; |
| 5817 | 5815 |
| 5818 // The library the prefix is referring to must come from a deferred import. | 5816 // The library the prefix is referring to must come from a deferred import. |
| 5819 var containingLibrary = resolutionMap | 5817 var containingLibrary = resolutionMap |
| 5820 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) | 5818 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) |
| 5821 .library; | 5819 .library; |
| 5822 var imports = containingLibrary.getImportsWithPrefix(prefix); | 5820 var imports = containingLibrary.getImportsWithPrefix(prefix); |
| 5823 return imports.length == 1 && imports[0].isDeferred; | 5821 return imports.length == 1 && imports[0].isDeferred; |
| 5824 } | 5822 } |
| OLD | NEW |