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 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 /// The dart:_interceptors JSArray element. | 117 /// The dart:_interceptors JSArray element. |
118 final ClassElement _jsArray; | 118 final ClassElement _jsArray; |
119 | 119 |
120 final ClassElement boolClass; | 120 final ClassElement boolClass; |
121 final ClassElement intClass; | 121 final ClassElement intClass; |
122 final ClassElement interceptorClass; | 122 final ClassElement interceptorClass; |
123 final ClassElement nullClass; | 123 final ClassElement nullClass; |
124 final ClassElement numClass; | 124 final ClassElement numClass; |
125 final ClassElement objectClass; | 125 final ClassElement objectClass; |
126 final ClassElement stringClass; | 126 final ClassElement stringClass; |
| 127 final ClassElement symbolClass; |
127 | 128 |
128 ConstFieldVisitor _constants; | 129 ConstFieldVisitor _constants; |
129 | 130 |
130 /// The current function body being compiled. | 131 /// The current function body being compiled. |
131 FunctionBody _currentFunction; | 132 FunctionBody _currentFunction; |
132 | 133 |
133 /// Helper class for emitting elements in the proper order to allow | 134 /// Helper class for emitting elements in the proper order to allow |
134 /// JS to load the module. | 135 /// JS to load the module. |
135 ElementLoader _loader; | 136 ElementLoader _loader; |
136 | 137 |
(...skipping 20 matching lines...) Expand all Loading... |
157 _jsArray = _getLibrary(c, 'dart:_interceptors').getType('JSArray'), | 158 _jsArray = _getLibrary(c, 'dart:_interceptors').getType('JSArray'), |
158 interceptorClass = | 159 interceptorClass = |
159 _getLibrary(c, 'dart:_interceptors').getType('Interceptor'), | 160 _getLibrary(c, 'dart:_interceptors').getType('Interceptor'), |
160 dartCoreLibrary = _getLibrary(c, 'dart:core'), | 161 dartCoreLibrary = _getLibrary(c, 'dart:core'), |
161 boolClass = _getLibrary(c, 'dart:core').getType('bool'), | 162 boolClass = _getLibrary(c, 'dart:core').getType('bool'), |
162 intClass = _getLibrary(c, 'dart:core').getType('int'), | 163 intClass = _getLibrary(c, 'dart:core').getType('int'), |
163 numClass = _getLibrary(c, 'dart:core').getType('num'), | 164 numClass = _getLibrary(c, 'dart:core').getType('num'), |
164 nullClass = _getLibrary(c, 'dart:core').getType('Null'), | 165 nullClass = _getLibrary(c, 'dart:core').getType('Null'), |
165 objectClass = _getLibrary(c, 'dart:core').getType('Object'), | 166 objectClass = _getLibrary(c, 'dart:core').getType('Object'), |
166 stringClass = _getLibrary(c, 'dart:core').getType('String'), | 167 stringClass = _getLibrary(c, 'dart:core').getType('String'), |
| 168 symbolClass = _getLibrary(c, 'dart:_internal').getType('Symbol'), |
167 dartJSLibrary = _getLibrary(c, 'dart:js'); | 169 dartJSLibrary = _getLibrary(c, 'dart:js'); |
168 | 170 |
169 LibraryElement get currentLibrary => _loader.currentElement.library; | 171 LibraryElement get currentLibrary => _loader.currentElement.library; |
170 | 172 |
171 /// The main entry point to JavaScript code generation. | 173 /// The main entry point to JavaScript code generation. |
172 /// | 174 /// |
173 /// Takes the metadata for the build unit, as well as resolved trees and | 175 /// Takes the metadata for the build unit, as well as resolved trees and |
174 /// errors, and computes the output module code and optionally the source map. | 176 /// errors, and computes the output module code and optionally the source map. |
175 JSModuleFile compile(BuildUnit unit, List<CompilationUnit> compilationUnits, | 177 JSModuleFile compile(BuildUnit unit, List<CompilationUnit> compilationUnits, |
176 List<String> errors) { | 178 List<String> errors) { |
(...skipping 5008 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5185 | 5187 |
5186 @override | 5188 @override |
5187 visitDoubleLiteral(DoubleLiteral node) => js.number(node.value); | 5189 visitDoubleLiteral(DoubleLiteral node) => js.number(node.value); |
5188 | 5190 |
5189 @override | 5191 @override |
5190 visitNullLiteral(NullLiteral node) => new JS.LiteralNull(); | 5192 visitNullLiteral(NullLiteral node) => new JS.LiteralNull(); |
5191 | 5193 |
5192 @override | 5194 @override |
5193 visitSymbolLiteral(SymbolLiteral node) { | 5195 visitSymbolLiteral(SymbolLiteral node) { |
5194 JS.Expression emitSymbol() { | 5196 JS.Expression emitSymbol() { |
5195 // TODO(vsm): When we canonicalize, we need to treat private symbols | 5197 // TODO(vsm): Handle qualified symbols correctly. |
5196 // correctly. | 5198 var last = node.components.last.toString(); |
5197 var name = js.string(node.components.join('.'), "'"); | 5199 var name = js.string(node.components.join('.'), "'"); |
5198 return js | 5200 if (last.startsWith('_')) { |
5199 .call('#.new(#)', [_emitConstructorAccess(types.symbolType), name]); | 5201 var nativeSymbol = _emitPrivateNameSymbol(currentLibrary, last); |
| 5202 return js.call('new #.es6(#, #)', |
| 5203 [_emitConstructorAccess(symbolClass.type), name, nativeSymbol]); |
| 5204 } else { |
| 5205 return js |
| 5206 .call('#.new(#)', [_emitConstructorAccess(types.symbolType), name]); |
| 5207 } |
5200 } | 5208 } |
5201 | 5209 |
5202 return _emitConst(emitSymbol); | 5210 return _emitConst(emitSymbol); |
5203 } | 5211 } |
5204 | 5212 |
5205 @override | 5213 @override |
5206 visitListLiteral(ListLiteral node) { | 5214 visitListLiteral(ListLiteral node) { |
5207 var isConst = node.constKeyword != null; | 5215 var isConst = node.constKeyword != null; |
5208 JS.Expression emitList() { | 5216 JS.Expression emitList() { |
5209 JS.Expression list = new JS.ArrayInitializer( | 5217 JS.Expression list = new JS.ArrayInitializer( |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5717 var targetIdentifier = target as SimpleIdentifier; | 5725 var targetIdentifier = target as SimpleIdentifier; |
5718 | 5726 |
5719 if (targetIdentifier.staticElement is! PrefixElement) return false; | 5727 if (targetIdentifier.staticElement is! PrefixElement) return false; |
5720 var prefix = targetIdentifier.staticElement as PrefixElement; | 5728 var prefix = targetIdentifier.staticElement as PrefixElement; |
5721 | 5729 |
5722 // The library the prefix is referring to must come from a deferred import. | 5730 // The library the prefix is referring to must come from a deferred import. |
5723 var containingLibrary = (target.root as CompilationUnit).element.library; | 5731 var containingLibrary = (target.root as CompilationUnit).element.library; |
5724 var imports = containingLibrary.getImportsWithPrefix(prefix); | 5732 var imports = containingLibrary.getImportsWithPrefix(prefix); |
5725 return imports.length == 1 && imports[0].isDeferred; | 5733 return imports.length == 1 && imports[0].isDeferred; |
5726 } | 5734 } |
OLD | NEW |