Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/code_generator.dart

Issue 2659063002: Fix DOM constructors in inline JS (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 a generated class in scope. We really should qualify explicitly
3733 // in dart:html itself.
Jacob 2017/01/27 16:10:31 Alternately, only apply this rename if there is a
3734 var constructorPattern = new RegExp("new [A-Z][A-Za-z]+\\(");
3735 if (constructorPattern.matchAsPrefix(source) != null) {
3736 source = source.replaceFirst('new ', 'new self.');
3737 }
3738
3730 // TODO(rnystrom): The JS() calls are almost never nested, and probably 3739 // 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 3740 // 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 3741 // HTML library where an argument to JS() is itself a JS() call. If those
3733 // go away, this can just assert(!_isInForeignJS). 3742 // go away, this can just assert(!_isInForeignJS).
3734 // Inside JS(), type names evaluate to the raw runtime type, not the 3743 // Inside JS(), type names evaluate to the raw runtime type, not the
3735 // wrapped Type object. 3744 // wrapped Type object.
3736 var wasInForeignJS = _isInForeignJS; 3745 var wasInForeignJS = _isInForeignJS;
3737 _isInForeignJS = true; 3746 _isInForeignJS = true;
3738 3747
3739 var template = js.parseForeignJS(source); 3748 var template = js.parseForeignJS(source);
(...skipping 2128 matching lines...) Expand 10 before | Expand all | Expand 10 after
5868 if (targetIdentifier.staticElement is! PrefixElement) return false; 5877 if (targetIdentifier.staticElement is! PrefixElement) return false;
5869 var prefix = targetIdentifier.staticElement as PrefixElement; 5878 var prefix = targetIdentifier.staticElement as PrefixElement;
5870 5879
5871 // The library the prefix is referring to must come from a deferred import. 5880 // The library the prefix is referring to must come from a deferred import.
5872 var containingLibrary = resolutionMap 5881 var containingLibrary = resolutionMap
5873 .elementDeclaredByCompilationUnit(target.root as CompilationUnit) 5882 .elementDeclaredByCompilationUnit(target.root as CompilationUnit)
5874 .library; 5883 .library;
5875 var imports = containingLibrary.getImportsWithPrefix(prefix); 5884 var imports = containingLibrary.getImportsWithPrefix(prefix);
5876 return imports.length == 1 && imports[0].isDeferred; 5885 return imports.length == 1 && imports[0].isDeferred;
5877 } 5886 }
OLDNEW
« no previous file with comments | « pkg/dev_compiler/lib/js/legacy/dart_sdk.js ('k') | pkg/dev_compiler/test/browser/language_tests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698