Chromium Code Reviews| 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; |
| 11 import '../../js_backend/js_backend.dart' show | 11 import '../../js_backend/js_backend.dart' show |
| 12 JavaScriptBackend, | 12 JavaScriptBackend, |
| 13 Namer, | 13 Namer, |
| 14 ConstantEmitter; | 14 ConstantEmitter; |
| 15 | 15 |
| 16 import '../js_emitter.dart' show | 16 import '../js_emitter.dart' show AstContainer, NativeEmitter; |
| 17 NativeEmitter; | |
| 18 | 17 |
| 19 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show | 18 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show |
| 20 CREATE_NEW_ISOLATE, | 19 CREATE_NEW_ISOLATE, |
| 21 DEFERRED_LIBRARY_URIS, | 20 DEFERRED_LIBRARY_URIS, |
| 22 DEFERRED_LIBRARY_HASHES, | 21 DEFERRED_LIBRARY_HASHES, |
| 23 GET_TYPE_FROM_NAME, | 22 GET_TYPE_FROM_NAME, |
| 24 INITIALIZE_LOADED_HUNK, | 23 INITIALIZE_LOADED_HUNK, |
| 25 INTERCEPTORS_BY_TAG, | 24 INTERCEPTORS_BY_TAG, |
| 26 IS_HUNK_INITIALIZED, | 25 IS_HUNK_INITIALIZED, |
| 27 IS_HUNK_LOADED, | 26 IS_HUNK_LOADED, |
| 28 LEAF_TAGS, | 27 LEAF_TAGS, |
| 29 MANGLED_GLOBAL_NAMES, | 28 MANGLED_GLOBAL_NAMES, |
| 30 METADATA, | 29 METADATA, |
| 31 TYPE_TO_INTERCEPTOR_MAP, | 30 TYPE_TO_INTERCEPTOR_MAP, |
| 32 TYPES; | 31 TYPES; |
| 33 | 32 |
| 34 import '../js_emitter.dart' show NativeGenerator, buildTearOffCode; | 33 import '../js_emitter.dart' show NativeGenerator, buildTearOffCode; |
| 35 import '../model.dart'; | 34 import '../model.dart'; |
| 36 | 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 _UnparsedNode(this.ast, this._compiler, this._protectForEval); | |
| 48 | |
| 49 js.LiteralString get _literal { | |
| 50 if (_cachedLiteral == null) { | |
| 51 String text = js.prettyPrint(ast, _compiler).getText(); | |
| 52 if (_protectForEval) { | |
| 53 if (ast is js.Fun) text = '($text)'; | |
| 54 if (ast is js.LiteralExpression) { | |
| 55 js.LiteralExpression literalExpression = ast; | |
| 56 String template = literalExpression.template; | |
| 57 if (template.startsWith("function ") || | |
| 58 template.startsWith("{")) { | |
| 59 text = '($text)'; | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 _cachedLiteral = js.js.escapedString(text); | |
| 64 } | |
| 65 return _cachedLiteral; | |
| 66 } | |
| 67 | |
| 68 @override | |
| 69 String get value => _literal.value; | |
| 70 } | |
| 37 | 71 |
| 38 class ModelEmitter { | 72 class ModelEmitter { |
| 39 final Compiler compiler; | 73 final Compiler compiler; |
| 40 final Namer namer; | 74 final Namer namer; |
| 41 ConstantEmitter constantEmitter; | 75 ConstantEmitter constantEmitter; |
| 42 final NativeEmitter nativeEmitter; | 76 final NativeEmitter nativeEmitter; |
| 43 | 77 |
| 44 JavaScriptBackend get backend => compiler.backend; | 78 JavaScriptBackend get backend => compiler.backend; |
| 45 | 79 |
| 46 /// For deferred loading we communicate the initializers via this global var. | 80 /// For deferred loading we communicate the initializers via this global var. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 | 163 |
| 130 int emitProgram(Program program) { | 164 int emitProgram(Program program) { |
| 131 List<Fragment> fragments = program.fragments; | 165 List<Fragment> fragments = program.fragments; |
| 132 MainFragment mainFragment = fragments.first; | 166 MainFragment mainFragment = fragments.first; |
| 133 | 167 |
| 134 int totalSize = 0; | 168 int totalSize = 0; |
| 135 | 169 |
| 136 // We have to emit the deferred fragments first, since we need their | 170 // We have to emit the deferred fragments first, since we need their |
| 137 // deferred hash (which depends on the output) when emitting the main | 171 // deferred hash (which depends on the output) when emitting the main |
| 138 // fragment. | 172 // fragment. |
| 139 fragments.skip(1).forEach((DeferredFragment deferredUnit) { | 173 List<js.Expression> fragmentsCode = fragments.skip(1).map( |
| 174 (DeferredFragment deferredUnit) { | |
| 140 js.Expression types = | 175 js.Expression types = |
| 141 program.metadataTypesForOutputUnit(deferredUnit.outputUnit); | 176 program.metadataTypesForOutputUnit(deferredUnit.outputUnit); |
| 142 js.Expression ast = emitDeferredFragment(types, deferredUnit, | 177 return emitDeferredFragment(types, deferredUnit, program.holders); |
| 143 program.holders); | 178 }).toList(); |
| 144 String code = js.prettyPrint(ast, compiler).getText(); | |
| 145 totalSize += code.length; | |
| 146 compiler.outputProvider(deferredUnit.outputFileName, deferredExtension) | |
| 147 ..add(code) | |
| 148 ..close(); | |
| 149 }); | |
| 150 | 179 |
| 151 js.Statement mainAst = emitMainFragment(program); | 180 js.Statement mainAst = emitMainFragment(program); |
| 181 | |
| 182 fragmentsCode.forEach(program.metadataFinalizer.countTokensInAst); | |
| 183 program.metadataFinalizer.countTokensInAst(mainAst); | |
| 184 program.metadataFinalizer.finalizeTokens(); | |
| 185 | |
| 186 for (int i = 0; i < fragmentsCode.length; ++i) { | |
| 187 String code = js.prettyPrint(fragmentsCode[i], compiler).getText(); | |
| 188 totalSize += code.length; | |
| 189 compiler.outputProvider(fragments[i+1].outputFileName, deferredExtension) | |
| 190 ..add(code) | |
| 191 ..close(); | |
| 192 } | |
| 193 | |
| 152 String mainCode = js.prettyPrint(mainAst, compiler).getText(); | 194 String mainCode = js.prettyPrint(mainAst, compiler).getText(); |
| 153 compiler.outputProvider(mainFragment.outputFileName, 'js') | 195 compiler.outputProvider(mainFragment.outputFileName, 'js') |
| 154 ..add(buildGeneratedBy(compiler)) | 196 ..add(buildGeneratedBy(compiler)) |
| 155 ..add(mainCode) | 197 ..add(mainCode) |
| 156 ..close(); | 198 ..close(); |
| 157 totalSize += mainCode.length; | 199 totalSize += mainCode.length; |
| 158 | 200 |
| 159 return totalSize; | 201 return totalSize; |
| 160 } | 202 } |
| 161 | 203 |
| 162 /// Unparses the given [value]. | 204 /// Unparses the given [value]. |
|
floitsch
2015/06/17 14:32:27
Update comment.
herhut
2015/06/18 07:40:34
Done.
| |
| 163 /// | 205 /// |
| 164 /// Pretty-prints the given [value] and, if [protectForEval] is | 206 /// Pretty-prints the given [value] and, if [protectForEval] is |
| 165 /// true, wraps the resulting string in parenthesis. The result is escaped | 207 /// true, wraps the resulting string in parenthesis. The result is escaped |
| 166 /// and returned. | 208 /// and returned. |
| 167 js.LiteralString unparse(Compiler compiler, js.Node value, | 209 js.Literal unparse(Compiler compiler, js.Node value, |
| 168 {bool protectForEval: true}) { | 210 {bool protectForEval: true}) { |
| 169 String text = js.prettyPrint(value, compiler).getText(); | 211 return new _UnparsedNode(value, compiler, protectForEval); |
| 170 if (protectForEval) { | |
| 171 if (value is js.Fun) text = '($text)'; | |
| 172 if (value is js.LiteralExpression && | |
| 173 (value.template.startsWith("function ") || | |
| 174 value.template.startsWith("{"))) { | |
| 175 text = '($text)'; | |
| 176 } | |
| 177 } | |
| 178 return js.js.escapedString(text); | |
| 179 } | 212 } |
| 180 | 213 |
| 181 String buildGeneratedBy(compiler) { | 214 String buildGeneratedBy(compiler) { |
| 182 var suffix = ''; | 215 var suffix = ''; |
| 183 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; | 216 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; |
| 184 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; | 217 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; |
| 185 } | 218 } |
| 186 | 219 |
| 187 js.Statement emitMainFragment(Program program) { | 220 js.Statement emitMainFragment(Program program) { |
| 188 MainFragment fragment = program.fragments.first; | 221 MainFragment fragment = program.fragments.first; |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 507 | 540 |
| 508 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); | 541 js.ArrayInitializer deferredArray = new js.ArrayInitializer(deferredCode); |
| 509 | 542 |
| 510 // This is the code that must be evaluated after all deferred classes have | 543 // This is the code that must be evaluated after all deferred classes have |
| 511 // been setup. | 544 // been setup. |
| 512 js.Statement immediateCode = new js.Block([ | 545 js.Statement immediateCode = new js.Block([ |
| 513 emitStaticNonFinalFields(fragment.staticNonFinalFields), | 546 emitStaticNonFinalFields(fragment.staticNonFinalFields), |
| 514 emitEagerClassInitializations(fragment.libraries)]); | 547 emitEagerClassInitializations(fragment.libraries)]); |
| 515 | 548 |
| 516 | 549 |
| 517 js.LiteralString immediateString = unparse(compiler, immediateCode); | 550 js.Literal immediateString = unparse(compiler, immediateCode); |
| 518 | 551 |
| 519 js.ArrayInitializer hunk = | 552 js.ArrayInitializer hunk = |
| 520 new js.ArrayInitializer([deferredArray, immediateString, | 553 new js.ArrayInitializer([deferredArray, immediateString, |
| 521 deferredTypes]); | 554 deferredTypes]); |
| 522 | 555 |
| 523 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); | 556 return js.js("$deferredInitializersGlobal[$hash] = #", hunk); |
| 524 } | 557 } |
| 525 | 558 |
| 526 // This string should be referenced wherever JavaScript code makes assumptions | 559 // This string should be referenced wherever JavaScript code makes assumptions |
| 527 // on the constants format. | 560 // on the constants format. |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1235 | 1268 |
| 1236 var end = Date.now(); | 1269 var end = Date.now(); |
| 1237 // print('Setup: ' + (end - start) + ' ms.'); | 1270 // print('Setup: ' + (end - start) + ' ms.'); |
| 1238 | 1271 |
| 1239 #invokeMain; // Start main. | 1272 #invokeMain; // Start main. |
| 1240 | 1273 |
| 1241 }(Date.now(), #code) | 1274 }(Date.now(), #code) |
| 1242 }"""; | 1275 }"""; |
| 1243 | 1276 |
| 1244 } | 1277 } |
| OLD | NEW |