Chromium Code Reviews| 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 library dev_compiler.src.codegen.js_codegen; | 5 library dev_compiler.src.codegen.js_codegen; |
| 6 | 6 |
| 7 import 'dart:collection' show HashSet, HashMap; | 7 import 'dart:collection' show HashSet, HashMap; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 @override | 280 @override |
| 281 JS.Expression visitTypeName(TypeName node) => _emitTypeName(node.type); | 281 JS.Expression visitTypeName(TypeName node) => _emitTypeName(node.type); |
| 282 | 282 |
| 283 @override | 283 @override |
| 284 JS.Statement visitClassTypeAlias(ClassTypeAlias node) { | 284 JS.Statement visitClassTypeAlias(ClassTypeAlias node) { |
| 285 // If we've already emitted this class, skip it. | 285 // If we've already emitted this class, skip it. |
| 286 var type = node.element.type; | 286 var type = node.element.type; |
| 287 if (_pendingClasses.remove(node.element) == null) return null; | 287 if (_pendingClasses.remove(node.element) == null) return null; |
| 288 | 288 |
| 289 var name = node.name.name; | 289 var name = node.name.name; |
| 290 var heritage = | 290 var classDecl = new JS.ClassDeclaration(new JS.ClassExpression( |
| 291 js.call('dart.mixin(#)', [_visitList(node.withClause.mixinTypes)]); | 291 new JS.Identifier(name), _classHeritage(node), [])); |
| 292 var classDecl = new JS.ClassDeclaration( | |
| 293 new JS.ClassExpression(new JS.Identifier(name), heritage, [])); | |
| 294 | 292 |
| 295 return _finishClassDef(type, classDecl); | 293 return _finishClassDef(type, classDecl); |
| 296 } | 294 } |
| 297 | 295 |
| 298 JS.Statement _emitJsType(String dartClassName, DartObjectImpl jsName) { | 296 JS.Statement _emitJsType(String dartClassName, DartObjectImpl jsName) { |
| 299 var jsTypeName = getConstantField(jsName, 'name', types.stringType); | 297 var jsTypeName = getConstantField(jsName, 'name', types.stringType); |
| 300 | 298 |
| 301 if (jsTypeName != null && jsTypeName != dartClassName) { | 299 if (jsTypeName != null && jsTypeName != dartClassName) { |
| 302 // We export the JS type as if it was a Dart type. For example this allows | 300 // We export the JS type as if it was a Dart type. For example this allows |
| 303 // `dom.InputElement` to actually be HTMLInputElement. | 301 // `dom.InputElement` to actually be HTMLInputElement. |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 530 var typeParams = type.typeParameters.map((p) => p.name); | 528 var typeParams = type.typeParameters.map((p) => p.name); |
| 531 if (isPublic(name)) _exports.add(genericName); | 529 if (isPublic(name)) _exports.add(genericName); |
| 532 return js.statement('let # = dart.generic(function(#) { #; return #; });', [ | 530 return js.statement('let # = dart.generic(function(#) { #; return #; });', [ |
| 533 genericName, | 531 genericName, |
| 534 typeParams, | 532 typeParams, |
| 535 body, | 533 body, |
| 536 name | 534 name |
| 537 ]); | 535 ]); |
| 538 } | 536 } |
| 539 | 537 |
| 540 JS.Expression _classHeritage(ClassDeclaration node) { | 538 JS.Expression _classHeritage(node) { |
|
vsm
2015/04/24 22:31:55
node could still be typed as AstNode, right?
Jennifer Messerly
2015/04/24 23:06:13
it breaks the .element and .withClause.
| |
| 541 if (node.element.type.isObject) return null; | 539 if (node.element.type.isObject) return null; |
| 542 | 540 |
| 543 JS.Expression heritage = null; | 541 DartType supertype; |
| 544 if (node.extendsClause != null) { | 542 if (node is ClassDeclaration) { |
| 545 heritage = _visit(node.extendsClause.superclass); | 543 var ext = node.extendsClause; |
| 544 supertype = ext != null ? ext.superclass.type : types.objectType; | |
| 546 } else { | 545 } else { |
| 547 heritage = _emitTypeName(types.objectType); | 546 supertype = (node as ClassTypeAlias).superclass.type; |
| 548 } | 547 } |
| 548 | |
| 549 JS.Expression heritage = _emitTypeName(supertype); | |
| 550 | |
| 549 if (node.withClause != null) { | 551 if (node.withClause != null) { |
| 550 var mixins = _visitList(node.withClause.mixinTypes); | 552 var mixins = _visitList(node.withClause.mixinTypes); |
| 551 mixins.insert(0, heritage); | 553 mixins.insert(0, heritage); |
| 552 heritage = js.call('dart.mixin(#)', [mixins]); | 554 heritage = js.call('dart.mixin(#)', [mixins]); |
| 553 } | 555 } |
| 556 | |
| 554 return heritage; | 557 return heritage; |
| 555 } | 558 } |
| 556 | 559 |
| 557 List<JS.Method> _emitClassMethods(ClassDeclaration node, | 560 List<JS.Method> _emitClassMethods(ClassDeclaration node, |
| 558 List<ConstructorDeclaration> ctors, List<FieldDeclaration> fields) { | 561 List<ConstructorDeclaration> ctors, List<FieldDeclaration> fields) { |
| 559 var element = node.element; | 562 var element = node.element; |
| 560 var type = element.type; | 563 var type = element.type; |
| 561 var isObject = type.isObject; | 564 var isObject = type.isObject; |
| 562 var name = node.name.name; | 565 var name = node.name.name; |
| 563 | 566 |
| (...skipping 1944 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2508 // TODO(jmesserly): validate the library. See issue #135. | 2511 // TODO(jmesserly): validate the library. See issue #135. |
| 2509 bool _isJsNameAnnotation(DartObjectImpl value) => value.type.name == 'JsName'; | 2512 bool _isJsNameAnnotation(DartObjectImpl value) => value.type.name == 'JsName'; |
| 2510 | 2513 |
| 2511 bool _isJsPeerInterface(DartObjectImpl value) => | 2514 bool _isJsPeerInterface(DartObjectImpl value) => |
| 2512 value.type.name == 'JsPeerInterface'; | 2515 value.type.name == 'JsPeerInterface'; |
| 2513 | 2516 |
| 2514 // TODO(jacobr): we would like to do something like the following | 2517 // TODO(jacobr): we would like to do something like the following |
| 2515 // but we don't have summary support yet. | 2518 // but we don't have summary support yet. |
| 2516 // bool _supportJsExtensionMethod(AnnotatedNode node) => | 2519 // bool _supportJsExtensionMethod(AnnotatedNode node) => |
| 2517 // _getAnnotation(node, "SupportJsExtensionMethod") != null; | 2520 // _getAnnotation(node, "SupportJsExtensionMethod") != null; |
| OLD | NEW |