| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart2js.new_js_emitter.model_emitter; | 5 library dart2js.new_js_emitter.model_emitter; |
| 6 | 6 |
| 7 import '../../constants/values.dart' show ConstantValue, FunctionConstantValue; | 7 import '../../constants/values.dart' show ConstantValue, FunctionConstantValue; |
| 8 import '../../dart2jslib.dart' show Compiler; | 8 import '../../dart2jslib.dart' show Compiler; |
| 9 import '../../elements/elements.dart' show ClassElement, FunctionElement; | 9 import '../../elements/elements.dart' show ClassElement, FunctionElement; |
| 10 import '../../js/js.dart' as js; | 10 import '../../js/js.dart' as js; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 IS_HUNK_LOADED, | 26 IS_HUNK_LOADED, |
| 27 LEAF_TAGS, | 27 LEAF_TAGS, |
| 28 MANGLED_GLOBAL_NAMES, | 28 MANGLED_GLOBAL_NAMES, |
| 29 METADATA, | 29 METADATA, |
| 30 TYPE_TO_INTERCEPTOR_MAP, | 30 TYPE_TO_INTERCEPTOR_MAP, |
| 31 TYPES; | 31 TYPES; |
| 32 | 32 |
| 33 import '../js_emitter.dart' show NativeGenerator, buildTearOffCode; | 33 import '../js_emitter.dart' show NativeGenerator, buildTearOffCode; |
| 34 import '../model.dart'; | 34 import '../model.dart'; |
| 35 | 35 |
| 36 /// Represents the LiteralString resulting from unparsing [expression]. The | |
| 37 /// actual unparsing is done on demand when requesting the [value] of this | |
| 38 /// node. | |
| 39 class _UnparsedNode extends js.DeferredString | |
| 40 implements AstContainer { | |
| 41 @override | |
| 42 final js.Node ast; | |
| 43 final Compiler _compiler; | |
| 44 final bool _protectForEval; | |
| 45 js.LiteralString _cachedLiteral; | |
| 46 | |
| 47 /// A [js.Literal] that represents the string result of unparsing [ast]. | |
| 48 /// | |
| 49 /// When its string [value] is requested, the node pretty-prints the given | |
| 50 /// [ast] and, if [protectForEval] is true, wraps the resulting | |
| 51 /// string in parenthesis. The result is also escaped. | |
| 52 _UnparsedNode(this.ast, this._compiler, this._protectForEval); | |
| 53 | |
| 54 js.LiteralString get _literal { | |
| 55 if (_cachedLiteral == null) { | |
| 56 String text = js.prettyPrint(ast, _compiler).getText(); | |
| 57 if (_protectForEval) { | |
| 58 if (ast is js.Fun) text = '($text)'; | |
| 59 if (ast is js.LiteralExpression) { | |
| 60 js.LiteralExpression literalExpression = ast; | |
| 61 String template = literalExpression.template; | |
| 62 if (template.startsWith("function ") || | |
| 63 template.startsWith("{")) { | |
| 64 text = '($text)'; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 _cachedLiteral = js.js.escapedString(text); | |
| 69 } | |
| 70 return _cachedLiteral; | |
| 71 } | |
| 72 | |
| 73 @override | |
| 74 String get value => _literal.value; | |
| 75 } | |
| 76 | |
| 77 class ModelEmitter { | 36 class ModelEmitter { |
| 78 final Compiler compiler; | 37 final Compiler compiler; |
| 79 final Namer namer; | 38 final Namer namer; |
| 80 ConstantEmitter constantEmitter; | 39 ConstantEmitter constantEmitter; |
| 81 final NativeEmitter nativeEmitter; | 40 final NativeEmitter nativeEmitter; |
| 82 | 41 |
| 83 JavaScriptBackend get backend => compiler.backend; | 42 JavaScriptBackend get backend => compiler.backend; |
| 84 | 43 |
| 85 /// For deferred loading we communicate the initializers via this global var. | 44 /// For deferred loading we communicate the initializers via this global var. |
| 86 static const String deferredInitializersGlobal = | 45 static const String deferredInitializersGlobal = |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // fragment. | 137 // fragment. |
| 179 List<js.Expression> fragmentsCode = fragments.skip(1).map( | 138 List<js.Expression> fragmentsCode = fragments.skip(1).map( |
| 180 (DeferredFragment deferredUnit) { | 139 (DeferredFragment deferredUnit) { |
| 181 js.Expression types = | 140 js.Expression types = |
| 182 program.metadataTypesForOutputUnit(deferredUnit.outputUnit); | 141 program.metadataTypesForOutputUnit(deferredUnit.outputUnit); |
| 183 return emitDeferredFragment(types, deferredUnit, program.holders); | 142 return emitDeferredFragment(types, deferredUnit, program.holders); |
| 184 }).toList(); | 143 }).toList(); |
| 185 | 144 |
| 186 js.Statement mainAst = emitMainFragment(program); | 145 js.Statement mainAst = emitMainFragment(program); |
| 187 | 146 |
| 188 fragmentsCode.forEach(program.metadataFinalizer.countTokensInAst); | 147 js.TokenCounter counter = new js.TokenCounter(); |
| 189 program.metadataFinalizer.countTokensInAst(mainAst); | 148 fragmentsCode.forEach(counter.countTokens); |
| 190 program.metadataFinalizer.finalizeTokens(); | 149 counter.countTokens(mainAst); |
| 150 |
| 151 program.finalizers.forEach((js.TokenFinalizer f) => f.finalizeTokens()); |
| 191 | 152 |
| 192 for (int i = 0; i < fragmentsCode.length; ++i) { | 153 for (int i = 0; i < fragmentsCode.length; ++i) { |
| 193 String code = js.prettyPrint(fragmentsCode[i], compiler).getText(); | 154 String code = js.prettyPrint(fragmentsCode[i], compiler).getText(); |
| 194 totalSize += code.length; | 155 totalSize += code.length; |
| 195 compiler.outputProvider(fragments[i+1].outputFileName, deferredExtension) | 156 compiler.outputProvider(fragments[i+1].outputFileName, deferredExtension) |
| 196 ..add(code) | 157 ..add(code) |
| 197 ..close(); | 158 ..close(); |
| 198 } | 159 } |
| 199 | 160 |
| 200 String mainCode = js.prettyPrint(mainAst, compiler).getText(); | 161 String mainCode = js.prettyPrint(mainAst, compiler).getText(); |
| 201 compiler.outputProvider(mainFragment.outputFileName, 'js') | 162 compiler.outputProvider(mainFragment.outputFileName, 'js') |
| 202 ..add(buildGeneratedBy(compiler)) | 163 ..add(buildGeneratedBy(compiler)) |
| 203 ..add(mainCode) | 164 ..add(mainCode) |
| 204 ..close(); | 165 ..close(); |
| 205 totalSize += mainCode.length; | 166 totalSize += mainCode.length; |
| 206 | 167 |
| 207 return totalSize; | 168 return totalSize; |
| 208 } | 169 } |
| 209 | 170 |
| 210 /// Returns a [js.Literal] that represents the string result of unparsing | 171 /// Returns a [js.Literal] that represents the string result of unparsing |
| 211 /// [value]. | 172 /// [value]. |
| 212 /// | 173 /// |
| 213 /// The returned node will, when its string value is requested, pretty-print | 174 /// The returned node will, when its string value is requested, pretty-print |
| 214 /// the given [value] and, if [protectForEval] is true, wrap the resulting | 175 /// the given [value] and, if [protectForEval] is true, wrap the resulting |
| 215 /// string in parenthesis. The result is also escaped. | 176 /// string in parenthesis. The result is also escaped. |
| 216 /// | 177 /// |
| 217 /// See [_UnparsedNode] for details. | 178 /// See [_UnparsedNode] for details. |
| 218 js.Literal unparse(Compiler compiler, js.Node value, | 179 js.Literal unparse(Compiler compiler, js.Node value, |
| 219 {bool protectForEval: true}) { | 180 {bool protectForEval: true}) { |
| 220 return new _UnparsedNode(value, compiler, protectForEval); | 181 return new js.UnparsedNode(value, compiler, protectForEval); |
| 221 } | 182 } |
| 222 | 183 |
| 223 String buildGeneratedBy(compiler) { | 184 String buildGeneratedBy(compiler) { |
| 224 var suffix = ''; | 185 var suffix = ''; |
| 225 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; | 186 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; |
| 226 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; | 187 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; |
| 227 } | 188 } |
| 228 | 189 |
| 229 js.Statement emitMainFragment(Program program) { | 190 js.Statement emitMainFragment(Program program) { |
| 230 MainFragment fragment = program.fragments.first; | 191 MainFragment fragment = program.fragments.first; |
| (...skipping 1045 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1276 | 1237 |
| 1277 var end = Date.now(); | 1238 var end = Date.now(); |
| 1278 // print('Setup: ' + (end - start) + ' ms.'); | 1239 // print('Setup: ' + (end - start) + ' ms.'); |
| 1279 | 1240 |
| 1280 #invokeMain; // Start main. | 1241 #invokeMain; // Start main. |
| 1281 | 1242 |
| 1282 })(Date.now(), #code) | 1243 })(Date.now(), #code) |
| 1283 }"""; | 1244 }"""; |
| 1284 | 1245 |
| 1285 } | 1246 } |
| OLD | NEW |