| 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 3689 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3700 } | 3700 } |
| 3701 | 3701 |
| 3702 /// Emits code for the `JS(...)` macro. | 3702 /// Emits code for the `JS(...)` macro. |
| 3703 _emitForeignJS(MethodInvocation node) { | 3703 _emitForeignJS(MethodInvocation node) { |
| 3704 var e = node.methodName.staticElement; | 3704 var e = node.methodName.staticElement; |
| 3705 if (isInlineJS(e)) { | 3705 if (isInlineJS(e)) { |
| 3706 var args = node.argumentList.arguments; | 3706 var args = node.argumentList.arguments; |
| 3707 // arg[0] is static return type, used in `RestrictedStaticTypeAnalyzer` | 3707 // arg[0] is static return type, used in `RestrictedStaticTypeAnalyzer` |
| 3708 var code = args[1]; | 3708 var code = args[1]; |
| 3709 List<AstNode> templateArgs; | 3709 List<AstNode> templateArgs; |
| 3710 var source; | 3710 String source; |
| 3711 if (code is StringInterpolation) { | 3711 if (code is StringInterpolation) { |
| 3712 if (args.length > 2) { | 3712 if (args.length > 2) { |
| 3713 throw new ArgumentError( | 3713 throw new ArgumentError( |
| 3714 "Can't mix template args and string interpolation in JS calls."); | 3714 "Can't mix template args and string interpolation in JS calls."); |
| 3715 } | 3715 } |
| 3716 templateArgs = <Expression>[]; | 3716 templateArgs = <Expression>[]; |
| 3717 source = code.elements.map((element) { | 3717 source = code.elements.map((element) { |
| 3718 if (element is InterpolationExpression) { | 3718 if (element is InterpolationExpression) { |
| 3719 templateArgs.add(element.expression); | 3719 templateArgs.add(element.expression); |
| 3720 return '#'; | 3720 return '#'; |
| 3721 } else { | 3721 } else { |
| 3722 return (element as InterpolationString).value; | 3722 return (element as InterpolationString).value; |
| 3723 } | 3723 } |
| 3724 }).join(); | 3724 }).join(); |
| 3725 } else { | 3725 } else { |
| 3726 templateArgs = args.skip(2).toList(); | 3726 templateArgs = args.skip(2).toList(); |
| 3727 source = (code as StringLiteral).stringValue; | 3727 source = (code as StringLiteral).stringValue; |
| 3728 } | 3728 } |
| 3729 | 3729 |
| 3730 // TODO(vsm): Constructors in dart:html and friends are trying to |
| 3731 // allocate a type defined on window/self, but this often conflicts a |
| 3732 // with the generated extenstion class in scope. We really should |
| 3733 // qualify explicitly in dart:html itself. |
| 3734 var constructorPattern = new RegExp("new [A-Z][A-Za-z]+\\("); |
| 3735 if (constructorPattern.matchAsPrefix(source) != null) { |
| 3736 var containingClass = node.parent; |
| 3737 while ( |
| 3738 containingClass != null && containingClass is! ClassDeclaration) { |
| 3739 containingClass = containingClass.parent; |
| 3740 } |
| 3741 if (containingClass is ClassDeclaration && |
| 3742 _extensionTypes.isNativeClass(containingClass.element)) { |
| 3743 var constructorName = source.substring(4, source.indexOf('(')); |
| 3744 var className = containingClass.name.name; |
| 3745 if (className == constructorName) { |
| 3746 source = |
| 3747 source.replaceFirst('new $className(', 'new self.$className('); |
| 3748 } |
| 3749 } |
| 3750 } |
| 3751 |
| 3730 // TODO(rnystrom): The JS() calls are almost never nested, and probably | 3752 // TODO(rnystrom): The JS() calls are almost never nested, and probably |
| 3731 // really shouldn't be, but there are at least a couple of calls in the | 3753 // really shouldn't be, but there are at least a couple of calls in the |
| 3732 // HTML library where an argument to JS() is itself a JS() call. If those | 3754 // HTML library where an argument to JS() is itself a JS() call. If those |
| 3733 // go away, this can just assert(!_isInForeignJS). | 3755 // go away, this can just assert(!_isInForeignJS). |
| 3734 // Inside JS(), type names evaluate to the raw runtime type, not the | 3756 // Inside JS(), type names evaluate to the raw runtime type, not the |
| 3735 // wrapped Type object. | 3757 // wrapped Type object. |
| 3736 var wasInForeignJS = _isInForeignJS; | 3758 var wasInForeignJS = _isInForeignJS; |
| 3737 _isInForeignJS = true; | 3759 _isInForeignJS = true; |
| 3738 | 3760 |
| 3739 var template = js.parseForeignJS(source); | 3761 var template = js.parseForeignJS(source); |
| (...skipping 2128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5868 if (targetIdentifier.staticElement is! PrefixElement) return false; | 5890 if (targetIdentifier.staticElement is! PrefixElement) return false; |
| 5869 var prefix = targetIdentifier.staticElement as PrefixElement; | 5891 var prefix = targetIdentifier.staticElement as PrefixElement; |
| 5870 | 5892 |
| 5871 // The library the prefix is referring to must come from a deferred import. | 5893 // The library the prefix is referring to must come from a deferred import. |
| 5872 var containingLibrary = resolutionMap | 5894 var containingLibrary = resolutionMap |
| 5873 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) | 5895 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) |
| 5874 .library; | 5896 .library; |
| 5875 var imports = containingLibrary.getImportsWithPrefix(prefix); | 5897 var imports = containingLibrary.getImportsWithPrefix(prefix); |
| 5876 return imports.length == 1 && imports[0].isDeferred; | 5898 return imports.length == 1 && imports[0].isDeferred; |
| 5877 } | 5899 } |
| OLD | NEW |