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 '../../dart2jslib.dart' show Compiler; | 7 import '../../dart2jslib.dart' show Compiler; |
| 8 import '../../js/js.dart' as js; | 8 import '../../js/js.dart' as js; |
| 9 import '../../js_backend/js_backend.dart' show | 9 import '../../js_backend/js_backend.dart' show |
| 10 JavaScriptBackend, | 10 JavaScriptBackend, |
| 11 Namer, | 11 Namer, |
| 12 ConstantEmitter; | 12 ConstantEmitter; |
| 13 | 13 |
| 14 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show | 14 import 'package:_internal/compiler/js_lib/shared/embedded_names.dart' show |
| 15 DEFERRED_LIBRARY_URIS, | 15 DEFERRED_LIBRARY_URIS, |
| 16 DEFERRED_LIBRARY_HASHES, | 16 DEFERRED_LIBRARY_HASHES, |
| 17 INITIALIZE_LOADED_HUNK, | 17 INITIALIZE_LOADED_HUNK, |
| 18 IS_HUNK_LOADED; | 18 IS_HUNK_LOADED; |
| 19 | 19 |
| 20 import '../js_emitter.dart' show NativeGenerator; | |
| 20 import '../model.dart'; | 21 import '../model.dart'; |
| 21 | 22 |
| 22 class ModelEmitter { | 23 class ModelEmitter { |
| 23 final Compiler compiler; | 24 final Compiler compiler; |
| 24 final Namer namer; | 25 final Namer namer; |
| 25 final ConstantEmitter constantEmitter; | 26 final ConstantEmitter constantEmitter; |
| 26 | 27 |
| 27 JavaScriptBackend get backend => compiler.backend; | 28 JavaScriptBackend get backend => compiler.backend; |
| 28 | 29 |
| 29 /// For deferred loading we communicate the initializers via this global var. | 30 /// For deferred loading we communicate the initializers via this global var. |
| 30 static const String deferredInitializersGlobal = | 31 static const String deferredInitializersGlobal = |
| 31 r"$__dart_deferred_initializers__"; | 32 r"$__dart_deferred_initializers__"; |
| 32 | 33 |
| 33 static const String deferredExtension = "part.js"; | 34 static const String deferredExtension = "part.js"; |
| 34 | 35 |
| 35 ModelEmitter(Compiler compiler, Namer namer) | 36 ModelEmitter(Compiler compiler, Namer namer) |
| 36 : this.compiler = compiler, | 37 : this.compiler = compiler, |
| 37 this.namer = namer, | 38 this.namer = namer, |
| 38 constantEmitter = | 39 constantEmitter = |
| 39 new ConstantEmitter(compiler, namer, makeConstantListTemplate); | 40 new ConstantEmitter(compiler, namer, makeConstantListTemplate); |
| 40 | 41 |
| 42 js.Expression generateEmbeddedGlobalAccess(String global) { | |
| 43 // TODO(floitsch): We should not use "init" for globals. | |
| 44 return js.js("init.$global"); | |
| 45 } | |
| 46 | |
| 41 void emitProgram(Program program) { | 47 void emitProgram(Program program) { |
| 42 List<Output> outputs = program.outputs; | 48 List<Output> outputs = program.outputs; |
| 43 MainOutput mainUnit = outputs.first; | 49 MainOutput mainUnit = outputs.first; |
| 44 js.Statement mainAst = emitMainUnit(program); | 50 js.Statement mainAst = emitMainUnit(program); |
| 45 String mainCode = js.prettyPrint(mainAst, compiler).getText(); | 51 String mainCode = js.prettyPrint(mainAst, compiler).getText(); |
| 46 compiler.outputProvider(mainUnit.outputFileName, 'js') | 52 compiler.outputProvider(mainUnit.outputFileName, 'js') |
| 47 ..add(buildGeneratedBy(compiler)) | 53 ..add(buildGeneratedBy(compiler)) |
| 48 ..add(mainCode) | 54 ..add(mainCode) |
| 49 ..close(); | 55 ..close(); |
| 50 compiler.assembledCode = mainCode; | 56 compiler.assembledCode = mainCode; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 68 var suffix = ''; | 74 var suffix = ''; |
| 69 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; | 75 if (compiler.hasBuildId) suffix = ' version: ${compiler.buildId}'; |
| 70 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; | 76 return '// Generated by dart2js, the Dart to JavaScript compiler$suffix.\n'; |
| 71 } | 77 } |
| 72 | 78 |
| 73 js.Statement emitMainUnit(Program program) { | 79 js.Statement emitMainUnit(Program program) { |
| 74 MainOutput unit = program.outputs.first; | 80 MainOutput unit = program.outputs.first; |
| 75 List<js.Expression> elements = unit.libraries.map(emitLibrary).toList(); | 81 List<js.Expression> elements = unit.libraries.map(emitLibrary).toList(); |
| 76 elements.add( | 82 elements.add( |
| 77 emitLazilyInitializedStatics(unit.staticLazilyInitializedFields)); | 83 emitLazilyInitializedStatics(unit.staticLazilyInitializedFields)); |
| 84 | |
| 85 js.Statement nativeBoilerplate; | |
| 86 if (NativeGenerator.needsBoilerplate(backend)) { | |
| 87 nativeBoilerplate = NativeGenerator.generateBoilerplate( | |
| 88 backend, | |
| 89 generateEmbeddedGlobalAccess, | |
| 90 // TODO(floitsch): convertToFastObject. | |
| 91 js.js("(function(x) { return x; })", [])); | |
| 92 } else { | |
| 93 nativeBoilerplate = js.js.statement(";"); | |
| 94 } | |
| 95 | |
| 78 js.Expression code = new js.ArrayInitializer(elements); | 96 js.Expression code = new js.ArrayInitializer(elements); |
| 97 | |
| 79 return js.js.statement( | 98 return js.js.statement( |
| 80 boilerplate, | 99 boilerplate, |
|
sra1
2015/01/05 20:14:23
Use the uncached version unless you are sure that
floitsch
2015/01/06 12:54:57
Boilerplate is a big string at the bottom of the f
sra1
2015/01/07 04:43:28
My bad. I thought it was computed.
If it is a con
floitsch
2015/01/07 13:11:46
what's a constant-like name?
I wanted to use capit
| |
| 81 {'deferredInitializer': emitDeferredInitializerGlobal(program.loadMap), | 100 {'deferredInitializer': emitDeferredInitializerGlobal(program.loadMap), |
| 82 'holders': emitHolders(unit.holders), | 101 'holders': emitHolders(unit.holders), |
| 83 'cyclicThrow': | 102 'cyclicThrow': |
| 84 backend.emitter.staticFunctionAccess(backend.getCyclicThrowHelper()), | 103 backend.emitter.staticFunctionAccess(backend.getCyclicThrowHelper()), |
| 85 'outputContainsConstantList': program.outputContainsConstantList, | 104 'outputContainsConstantList': program.outputContainsConstantList, |
| 86 'embeddedGlobals': emitEmbeddedGlobals(program.loadMap), | 105 'embeddedGlobals': emitEmbeddedGlobals(program.loadMap), |
| 87 'constants': emitConstants(unit.constants), | 106 'constants': emitConstants(unit.constants), |
| 88 'staticNonFinals': emitStaticNonFinalFields(unit.staticNonFinalFields), | 107 'staticNonFinals': emitStaticNonFinalFields(unit.staticNonFinalFields), |
| 108 'nativeBoilerplate': nativeBoilerplate, | |
| 89 'eagerClasses': emitEagerClassInitializations(unit.libraries), | 109 'eagerClasses': emitEagerClassInitializations(unit.libraries), |
| 90 'main': unit.main, | 110 'main': unit.main, |
| 91 'code': code}); | 111 'code': code}); |
| 92 } | 112 } |
| 93 | 113 |
| 94 js.Block emitHolders(List<Holder> holders) { | 114 js.Block emitHolders(List<Holder> holders) { |
| 95 // The top-level variables for holders must *not* be renamed by the | 115 // The top-level variables for holders must *not* be renamed by the |
| 96 // JavaScript pretty printer because a lot of code already uses the | 116 // JavaScript pretty printer because a lot of code already uses the |
| 97 // non-renamed names. The generated code looks like this: | 117 // non-renamed names. The generated code looks like this: |
| 98 // | 118 // |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 123 | 143 |
| 124 js.Block emitEmbeddedGlobals(Map<String, List<Output>> loadMap) { | 144 js.Block emitEmbeddedGlobals(Map<String, List<Output>> loadMap) { |
| 125 List<js.Property> globals = <js.Property>[]; | 145 List<js.Property> globals = <js.Property>[]; |
| 126 | 146 |
| 127 if (loadMap.isNotEmpty) { | 147 if (loadMap.isNotEmpty) { |
| 128 globals.addAll(emitLoadUrisAndHashes(loadMap)); | 148 globals.addAll(emitLoadUrisAndHashes(loadMap)); |
| 129 globals.add(emitIsHunkLoadedFunction()); | 149 globals.add(emitIsHunkLoadedFunction()); |
| 130 globals.add(emitInitializeLoadedHunk()); | 150 globals.add(emitInitializeLoadedHunk()); |
| 131 } | 151 } |
| 132 | 152 |
| 133 if (globals.isEmpty) return new js.Block.empty(); | |
| 134 | |
| 135 js.ObjectInitializer globalsObject = new js.ObjectInitializer(globals); | 153 js.ObjectInitializer globalsObject = new js.ObjectInitializer(globals); |
| 136 | 154 |
| 137 List<js.Statement> statements = | 155 List<js.Statement> statements = |
| 138 [new js.ExpressionStatement( | 156 [new js.ExpressionStatement( |
| 139 new js.VariableDeclarationList( | 157 new js.VariableDeclarationList( |
| 140 [new js.VariableInitialization( | 158 [new js.VariableInitialization( |
| 141 new js.VariableDeclaration("init", allowRename: false), | 159 new js.VariableDeclaration("init", allowRename: false), |
| 142 globalsObject)]))]; | 160 globalsObject)]))]; |
| 143 return new js.Block(statements); | 161 return new js.Block(statements); |
| 144 } | 162 } |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 532 | 550 |
| 533 // Initialize globals. | 551 // Initialize globals. |
| 534 #embeddedGlobals; | 552 #embeddedGlobals; |
| 535 | 553 |
| 536 // Initialize constants. | 554 // Initialize constants. |
| 537 #constants; | 555 #constants; |
| 538 | 556 |
| 539 // Initialize static non-final fields. | 557 // Initialize static non-final fields. |
| 540 #staticNonFinals; | 558 #staticNonFinals; |
| 541 | 559 |
| 560 // Add native boilerplate code. | |
| 561 #nativeBoilerplate; | |
| 562 | |
| 542 // Initialize eager classes. | 563 // Initialize eager classes. |
| 543 #eagerClasses; | 564 #eagerClasses; |
| 544 | 565 |
| 545 var end = Date.now(); | 566 var end = Date.now(); |
| 546 print('Setup: ' + (end - start) + ' ms.'); | 567 print('Setup: ' + (end - start) + ' ms.'); |
| 547 | 568 |
| 548 #main(); // Start main. | 569 #main(); // Start main. |
| 549 | 570 |
| 550 }(Date.now(), #code) | 571 }(Date.now(), #code) |
| 551 }"""; | 572 }"""; |
| 552 | 573 |
| 553 } | 574 } |
| OLD | NEW |