| 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.js_emitter.program_builder; | 5 library dart2js.js_emitter.program_builder; |
| 6 | 6 |
| 7 import 'model.dart'; | 7 import 'model.dart'; |
| 8 import '../common.dart'; | 8 import '../common.dart'; |
| 9 import '../js/js.dart' as js; | 9 import '../js/js.dart' as js; |
| 10 | 10 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 Universe get universe => _compiler.codegenWorld; | 35 Universe get universe => _compiler.codegenWorld; |
| 36 | 36 |
| 37 /// Mapping from [ClassElement] to constructed [Class]. We need this to | 37 /// Mapping from [ClassElement] to constructed [Class]. We need this to |
| 38 /// update the superclass in the [Class]. | 38 /// update the superclass in the [Class]. |
| 39 final Map<ClassElement, Class> _classes = <ClassElement, Class>{}; | 39 final Map<ClassElement, Class> _classes = <ClassElement, Class>{}; |
| 40 | 40 |
| 41 /// Mapping from [OutputUnit] to constructed [Output]. We need this to | 41 /// Mapping from [OutputUnit] to constructed [Output]. We need this to |
| 42 /// generate the deferredLoadingMap (to know which hunks to load). | 42 /// generate the deferredLoadingMap (to know which hunks to load). |
| 43 final Map<OutputUnit, Output> _outputs = <OutputUnit, Output>{}; | 43 final Map<OutputUnit, Output> _outputs = <OutputUnit, Output>{}; |
| 44 | 44 |
| 45 /// Mapping from [ConstantValue] to constructed [Constant]. We need this to |
| 46 /// update field-initializers to point to the ConstantModel. |
| 47 final Map<ConstantValue, Constant> _constants = <ConstantValue, Constant>{}; |
| 48 |
| 45 Program buildProgram() { | 49 Program buildProgram() { |
| 46 Set<ClassElement> neededClasses = _task.neededClasses; | |
| 47 Iterable<Element> neededStatics = backend.generatedCode.keys | |
| 48 .where((Element e) => !e.isInstanceMember && !e.isField); | |
| 49 | |
| 50 _task.outputClassLists.forEach(_registry.registerElements); | 50 _task.outputClassLists.forEach(_registry.registerElements); |
| 51 _task.outputStaticLists.forEach(_registry.registerElements); | 51 _task.outputStaticLists.forEach(_registry.registerElements); |
| 52 | 52 |
| 53 // TODO(kasperl): There's code that implicitly needs access to the special | 53 // TODO(kasperl): There's code that implicitly needs access to the special |
| 54 // $ holder so we have to register that. Can we track if we have to? | 54 // $ holder so we have to register that. Can we track if we have to? |
| 55 _registry.registerHolder(r'$'); | 55 _registry.registerHolder(r'$'); |
| 56 | 56 |
| 57 MainOutput mainOutput = _buildMainOutput(_registry.mainFragment); | 57 MainOutput mainOutput = _buildMainOutput(_registry.mainFragment); |
| 58 Iterable<Output> deferredOutputs = _registry.deferredFragments | 58 Iterable<Output> deferredOutputs = _registry.deferredFragments |
| 59 .map((fragment) => _buildDeferredOutput(mainOutput, fragment)); | 59 .map((fragment) => _buildDeferredOutput(mainOutput, fragment)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 89 }); | 89 }); |
| 90 return loadMap; | 90 return loadMap; |
| 91 } | 91 } |
| 92 | 92 |
| 93 MainOutput _buildMainOutput(Fragment fragment) { | 93 MainOutput _buildMainOutput(Fragment fragment) { |
| 94 // Construct the main output from the libraries and the registered holders. | 94 // Construct the main output from the libraries and the registered holders. |
| 95 MainOutput result = new MainOutput( | 95 MainOutput result = new MainOutput( |
| 96 "", // The empty string is the name for the main output file. | 96 "", // The empty string is the name for the main output file. |
| 97 namer.elementAccess(_compiler.mainFunction), | 97 namer.elementAccess(_compiler.mainFunction), |
| 98 _buildLibraries(fragment), | 98 _buildLibraries(fragment), |
| 99 _buildConstants(fragment), |
| 99 _registry.holders.toList(growable: false)); | 100 _registry.holders.toList(growable: false)); |
| 100 _outputs[fragment.outputUnit] = result; | 101 _outputs[fragment.outputUnit] = result; |
| 101 return result; | 102 return result; |
| 102 } | 103 } |
| 103 | 104 |
| 104 /// Returns a name composed of the main output file name and [name]. | 105 /// Returns a name composed of the main output file name and [name]. |
| 105 String _outputFileName(String name) { | 106 String _outputFileName(String name) { |
| 106 assert(name != ""); | 107 assert(name != ""); |
| 107 String outPath = _compiler.outputUri != null | 108 String outPath = _compiler.outputUri != null |
| 108 ? _compiler.outputUri.path | 109 ? _compiler.outputUri.path |
| 109 : "out"; | 110 : "out"; |
| 110 String outName = outPath.substring(outPath.lastIndexOf('/') + 1); | 111 String outName = outPath.substring(outPath.lastIndexOf('/') + 1); |
| 111 return "${outName}_$name"; | 112 return "${outName}_$name"; |
| 112 } | 113 } |
| 113 | 114 |
| 114 DeferredOutput _buildDeferredOutput(MainOutput mainOutput, | 115 DeferredOutput _buildDeferredOutput(MainOutput mainOutput, |
| 115 Fragment fragment) { | 116 Fragment fragment) { |
| 116 DeferredOutput result = new DeferredOutput( | 117 DeferredOutput result = new DeferredOutput( |
| 117 _outputFileName(fragment.name), fragment.name, | 118 _outputFileName(fragment.name), fragment.name, |
| 118 mainOutput, _buildLibraries(fragment)); | 119 mainOutput, |
| 120 _buildLibraries(fragment), |
| 121 _buildConstants(fragment)); |
| 119 _outputs[fragment.outputUnit] = result; | 122 _outputs[fragment.outputUnit] = result; |
| 120 return result; | 123 return result; |
| 121 } | 124 } |
| 122 | 125 |
| 126 List<Constant> _buildConstants(Fragment fragment) { |
| 127 List<ConstantValue> constantValues = |
| 128 _task.outputConstantLists[fragment.outputUnit]; |
| 129 if (constantValues == null) return const <Constant>[]; |
| 130 return constantValues.map((ConstantValue constantValue) { |
| 131 assert(!_constants.containsKey(constantValue)); |
| 132 String name = namer.constantName(constantValue); |
| 133 String constantObject = namer.globalObjectForConstant(constantValue); |
| 134 Holder holder = _registry.registerHolder(constantObject); |
| 135 Constant constant = new Constant(name, holder, constantValue); |
| 136 _constants[constantValue] = constant; |
| 137 return constant; |
| 138 }).toList(); |
| 139 } |
| 140 |
| 123 List<Library> _buildLibraries(Fragment fragment) { | 141 List<Library> _buildLibraries(Fragment fragment) { |
| 124 List<Library> libraries = new List<Library>(fragment.length); | 142 List<Library> libraries = new List<Library>(fragment.length); |
| 125 int count = 0; | 143 int count = 0; |
| 126 fragment.forEach((LibraryElement library, List<Element> elements) { | 144 fragment.forEach((LibraryElement library, List<Element> elements) { |
| 127 libraries[count++] = _buildLibrary(library, elements); | 145 libraries[count++] = _buildLibrary(library, elements); |
| 128 }); | 146 }); |
| 129 return libraries; | 147 return libraries; |
| 130 } | 148 } |
| 131 | 149 |
| 132 // Note that a library-element may have multiple [Library]s, if it is split | 150 // Note that a library-element may have multiple [Library]s, if it is split |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 } | 200 } |
| 183 | 201 |
| 184 StaticMethod _buildStaticMethodTearOff(FunctionElement element) { | 202 StaticMethod _buildStaticMethodTearOff(FunctionElement element) { |
| 185 String name = namer.getStaticClosureName(element); | 203 String name = namer.getStaticClosureName(element); |
| 186 String holder = namer.globalObjectFor(element); | 204 String holder = namer.globalObjectFor(element); |
| 187 // TODO(kasperl): This clearly doesn't work yet. | 205 // TODO(kasperl): This clearly doesn't work yet. |
| 188 js.Expression code = js.string("<<unimplemented>>"); | 206 js.Expression code = js.string("<<unimplemented>>"); |
| 189 return new StaticMethod(name, _registry.registerHolder(holder), code); | 207 return new StaticMethod(name, _registry.registerHolder(holder), code); |
| 190 } | 208 } |
| 191 } | 209 } |
| OLD | NEW |