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, SplayTreeSet; | 7 import 'dart:collection' show HashSet, HashMap, SplayTreeSet; |
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 3233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3244 if (node.constKeyword != null) return _emitConst(emitList); | 3244 if (node.constKeyword != null) return _emitConst(emitList); |
3245 return emitList(); | 3245 return emitList(); |
3246 } | 3246 } |
3247 | 3247 |
3248 @override | 3248 @override |
3249 visitMapLiteral(MapLiteral node) { | 3249 visitMapLiteral(MapLiteral node) { |
3250 // TODO(jmesserly): we can likely make these faster. | 3250 // TODO(jmesserly): we can likely make these faster. |
3251 JS.Expression emitMap() { | 3251 JS.Expression emitMap() { |
3252 var entries = node.entries; | 3252 var entries = node.entries; |
3253 var mapArguments = null; | 3253 var mapArguments = null; |
3254 if (entries.isEmpty) { | 3254 var typeArgs = node.typeArguments; |
| 3255 if (entries.isEmpty && typeArgs == null) { |
3255 mapArguments = []; | 3256 mapArguments = []; |
3256 } else if (entries.every((e) => e.key is StringLiteral)) { | 3257 } else if (entries.every((e) => e.key is StringLiteral)) { |
3257 // Use JS object literal notation if possible, otherwise use an array. | 3258 // Use JS object literal notation if possible, otherwise use an array. |
3258 // We could do this any time all keys are non-nullable String type. | 3259 // We could do this any time all keys are non-nullable String type. |
3259 // For now, support StringLiteral as the common non-nullable String case
. | 3260 // For now, support StringLiteral as the common non-nullable String case
. |
3260 var props = <JS.Property>[]; | 3261 var props = <JS.Property>[]; |
3261 for (var e in entries) { | 3262 for (var e in entries) { |
3262 props.add(new JS.Property(_visit(e.key), _visit(e.value))); | 3263 props.add(new JS.Property(_visit(e.key), _visit(e.value))); |
3263 } | 3264 } |
3264 mapArguments = new JS.ObjectInitializer(props); | 3265 mapArguments = new JS.ObjectInitializer(props); |
3265 } else { | 3266 } else { |
3266 var values = <JS.Expression>[]; | 3267 var values = <JS.Expression>[]; |
3267 for (var e in entries) { | 3268 for (var e in entries) { |
3268 values.add(_visit(e.key)); | 3269 values.add(_visit(e.key)); |
3269 values.add(_visit(e.value)); | 3270 values.add(_visit(e.value)); |
3270 } | 3271 } |
3271 mapArguments = new JS.ArrayInitializer(values); | 3272 mapArguments = new JS.ArrayInitializer(values); |
3272 } | 3273 } |
3273 // TODO(jmesserly): add generic types args. | 3274 var types = <JS.Expression>[]; |
3274 return js.call('dart.map(#)', [mapArguments]); | 3275 if (typeArgs != null) { |
| 3276 types.addAll(typeArgs.arguments.map((e) => _emitTypeName(e.type))); |
| 3277 } |
| 3278 return js.call('dart.map(#, #)', [mapArguments, types]); |
3275 } | 3279 } |
3276 if (node.constKeyword != null) return _emitConst(emitMap); | 3280 if (node.constKeyword != null) return _emitConst(emitMap); |
3277 return emitMap(); | 3281 return emitMap(); |
3278 } | 3282 } |
3279 | 3283 |
3280 @override | 3284 @override |
3281 JS.LiteralString visitSimpleStringLiteral(SimpleStringLiteral node) => | 3285 JS.LiteralString visitSimpleStringLiteral(SimpleStringLiteral node) => |
3282 js.escapedString(node.value, node.isSingleQuoted ? "'" : '"'); | 3286 js.escapedString(node.value, node.isSingleQuoted ? "'" : '"'); |
3283 | 3287 |
3284 @override | 3288 @override |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3634 | 3638 |
3635 /// A special kind of element created by the compiler, signifying a temporary | 3639 /// A special kind of element created by the compiler, signifying a temporary |
3636 /// variable. These objects use instance equality, and should be shared | 3640 /// variable. These objects use instance equality, and should be shared |
3637 /// everywhere in the tree where they are treated as the same variable. | 3641 /// everywhere in the tree where they are treated as the same variable. |
3638 class TemporaryVariableElement extends LocalVariableElementImpl { | 3642 class TemporaryVariableElement extends LocalVariableElementImpl { |
3639 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); | 3643 TemporaryVariableElement.forNode(Identifier name) : super.forNode(name); |
3640 | 3644 |
3641 int get hashCode => identityHashCode(this); | 3645 int get hashCode => identityHashCode(this); |
3642 bool operator ==(Object other) => identical(this, other); | 3646 bool operator ==(Object other) => identical(this, other); |
3643 } | 3647 } |
OLD | NEW |