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 import 'dart:io' show Directory, File; | 8 import 'dart:io' show Directory, File; |
9 | 9 |
10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 js.call('dart.as(#)', [[_visit(node), _emitTypeName(type)]]); | 228 js.call('dart.as(#)', [[_visit(node), _emitTypeName(type)]]); |
229 | 229 |
230 @override | 230 @override |
231 visitIsExpression(IsExpression node) { | 231 visitIsExpression(IsExpression node) { |
232 // Generate `is` as `dart.is` or `typeof` depending on the RHS type. | 232 // Generate `is` as `dart.is` or `typeof` depending on the RHS type. |
233 JS.Expression result; | 233 JS.Expression result; |
234 var type = node.type.type; | 234 var type = node.type.type; |
235 var lhs = _visit(node.expression); | 235 var lhs = _visit(node.expression); |
236 var typeofName = _jsTypeofName(type); | 236 var typeofName = _jsTypeofName(type); |
237 if (typeofName != null) { | 237 if (typeofName != null) { |
238 result = js.call('typeof # == #', [lhs, typeofName]); | 238 result = js.call('typeof # == #', [lhs, js.string(typeofName, "'")]); |
239 } else { | 239 } else { |
240 // Always go through a runtime helper, because implicit interfaces. | 240 // Always go through a runtime helper, because implicit interfaces. |
241 result = js.call('dart.is(#, #)', [lhs, _emitTypeName(type)]); | 241 result = js.call('dart.is(#, #)', [lhs, _emitTypeName(type)]); |
242 } | 242 } |
243 | 243 |
244 if (node.notOperator != null) { | 244 if (node.notOperator != null) { |
245 return js.call('!#', result); | 245 return js.call('!#', result); |
246 } | 246 } |
247 return result; | 247 return result; |
248 } | 248 } |
(...skipping 1657 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1906 | 1906 |
1907 @override | 1907 @override |
1908 visitDoubleLiteral(DoubleLiteral node) => js.number(node.value); | 1908 visitDoubleLiteral(DoubleLiteral node) => js.number(node.value); |
1909 | 1909 |
1910 @override | 1910 @override |
1911 visitNullLiteral(NullLiteral node) => new JS.LiteralNull(); | 1911 visitNullLiteral(NullLiteral node) => new JS.LiteralNull(); |
1912 | 1912 |
1913 @override | 1913 @override |
1914 visitListLiteral(ListLiteral node) { | 1914 visitListLiteral(ListLiteral node) { |
1915 // TODO(jmesserly): make this faster. We're wasting an array. | 1915 // TODO(jmesserly): make this faster. We're wasting an array. |
1916 var list = js.call('new List.from(#)', | 1916 var list = js.call('new #.from(#)', |
1917 [new JS.ArrayInitializer(_visitList(node.elements))]); | 1917 [_emitTypeName(node.staticType), new JS.ArrayInitializer(_visitList(node
.elements))]); |
1918 if (node.constKeyword != null) { | 1918 if (node.constKeyword != null) { |
1919 list = js.commentExpression('Unimplemented const', list); | 1919 list = js.commentExpression('Unimplemented const', list); |
1920 } | 1920 } |
1921 return list; | 1921 return list; |
1922 } | 1922 } |
1923 | 1923 |
1924 @override | 1924 @override |
1925 visitMapLiteral(MapLiteral node) { | 1925 visitMapLiteral(MapLiteral node) { |
1926 var entries = node.entries; | 1926 var entries = node.entries; |
1927 var mapArguments = null; | 1927 var mapArguments = null; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2018 return _constEvaluator.evaluate(initializer); | 2018 return _constEvaluator.evaluate(initializer); |
2019 } | 2019 } |
2020 | 2020 |
2021 _visit(AstNode node) { | 2021 _visit(AstNode node) { |
2022 if (node == null) return null; | 2022 if (node == null) return null; |
2023 var result = node.accept(this); | 2023 var result = node.accept(this); |
2024 if (result is JS.Node) result.sourceInformation = node; | 2024 if (result is JS.Node) result.sourceInformation = node; |
2025 return result; | 2025 return result; |
2026 } | 2026 } |
2027 | 2027 |
2028 JS.Statement _visitOrEmpty(Statement node) { | |
2029 if (node == null) return new JS.EmptyStatement(); | |
2030 return _visit(node); | |
2031 } | |
2032 | |
2033 List _visitList(Iterable<AstNode> nodes) { | 2028 List _visitList(Iterable<AstNode> nodes) { |
2034 if (nodes == null) return null; | 2029 if (nodes == null) return null; |
2035 var result = []; | 2030 var result = []; |
2036 for (var node in nodes) result.add(_visit(node)); | 2031 for (var node in nodes) result.add(_visit(node)); |
2037 return result; | 2032 return result; |
2038 } | 2033 } |
2039 | 2034 |
2040 /// Visits a list of expressions, creating a comma expression if needed in JS. | 2035 /// Visits a list of expressions, creating a comma expression if needed in JS. |
2041 JS.Expression _visitListToBinary(List<Expression> nodes, String operator) { | 2036 JS.Expression _visitListToBinary(List<Expression> nodes, String operator) { |
2042 if (nodes == null || nodes.isEmpty) return null; | 2037 if (nodes == null || nodes.isEmpty) return null; |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2329 | 2324 |
2330 // TODO(jmesserly): in many cases marking the end will be unncessary. | 2325 // TODO(jmesserly): in many cases marking the end will be unncessary. |
2331 printer.mark(_location(node.end)); | 2326 printer.mark(_location(node.end)); |
2332 } | 2327 } |
2333 | 2328 |
2334 String _getIdentifier(AstNode node) { | 2329 String _getIdentifier(AstNode node) { |
2335 if (node is SimpleIdentifier) return node.name; | 2330 if (node is SimpleIdentifier) return node.name; |
2336 return null; | 2331 return null; |
2337 } | 2332 } |
2338 } | 2333 } |
OLD | NEW |