| 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.full_emitter; | 5 library dart2js.js_emitter.full_emitter; |
| 6 | 6 |
| 7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; | 10 import 'package:js_runtime/shared/embedded_names.dart' as embeddedNames; |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 Iterable<Method> staticFunctions, Fragment fragment) { | 585 Iterable<Method> staticFunctions, Fragment fragment) { |
| 586 if (staticFunctions == null) return; | 586 if (staticFunctions == null) return; |
| 587 | 587 |
| 588 for (Method method in staticFunctions) { | 588 for (Method method in staticFunctions) { |
| 589 MethodElement element = method.element; | 589 MethodElement element = method.element; |
| 590 // We need to filter out null-elements for the interceptors. | 590 // We need to filter out null-elements for the interceptors. |
| 591 // TODO(floitsch): use the precomputed interceptors here. | 591 // TODO(floitsch): use the precomputed interceptors here. |
| 592 if (element == null) continue; | 592 if (element == null) continue; |
| 593 ClassBuilder builder = new ClassBuilder.forStatics(element, namer); | 593 ClassBuilder builder = new ClassBuilder.forStatics(element, namer); |
| 594 containerBuilder.addMemberMethod(method, builder); | 594 containerBuilder.addMemberMethod(method, builder); |
| 595 getElementDescriptor(element, fragment) | 595 getStaticMethodDescriptor(element, fragment) |
| 596 .properties | 596 .properties |
| 597 .addAll(builder.properties); | 597 .addAll(builder.properties); |
| 598 } | 598 } |
| 599 } | 599 } |
| 600 | 600 |
| 601 jsAst.Statement buildStaticNonFinalFieldInitializations( | 601 jsAst.Statement buildStaticNonFinalFieldInitializations( |
| 602 OutputUnit outputUnit) { | 602 OutputUnit outputUnit) { |
| 603 jsAst.Statement buildInitialization( | 603 jsAst.Statement buildInitialization( |
| 604 Element element, jsAst.Expression initialValue) { | 604 Element element, jsAst.Expression initialValue) { |
| 605 return js.statement('${namer.staticStateHolder}.# = #', | 605 return js.statement('${namer.staticStateHolder}.# = #', |
| (...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1144 embeddedNames.TYPEDEF_PREDICATE_PROPERTY_NAME, js.boolean(true)); | 1144 embeddedNames.TYPEDEF_PREDICATE_PROPERTY_NAME, js.boolean(true)); |
| 1145 | 1145 |
| 1146 // We can be pretty sure that the objectClass is initialized, since | 1146 // We can be pretty sure that the objectClass is initialized, since |
| 1147 // typedefs are only emitted with reflection, which requires lots of | 1147 // typedefs are only emitted with reflection, which requires lots of |
| 1148 // classes. | 1148 // classes. |
| 1149 assert(commonElements.objectClass != null); | 1149 assert(commonElements.objectClass != null); |
| 1150 builder.superName = namer.className(commonElements.objectClass); | 1150 builder.superName = namer.className(commonElements.objectClass); |
| 1151 jsAst.Node declaration = builder.toObjectInitializer(); | 1151 jsAst.Node declaration = builder.toObjectInitializer(); |
| 1152 jsAst.Name mangledName = namer.globalPropertyName(typedef); | 1152 jsAst.Name mangledName = namer.globalPropertyName(typedef); |
| 1153 String reflectionName = getReflectionName(typedef, mangledName); | 1153 String reflectionName = getReflectionName(typedef, mangledName); |
| 1154 getElementDescriptor(library, mainFragment) | 1154 getLibraryDescriptor(library, mainFragment) |
| 1155 ..addProperty(mangledName, declaration) | 1155 ..addProperty(mangledName, declaration) |
| 1156 ..addPropertyByName("+$reflectionName", js.string('')); | 1156 ..addPropertyByName("+$reflectionName", js.string('')); |
| 1157 // Also emit a trivial constructor for CSP mode. | 1157 // Also emit a trivial constructor for CSP mode. |
| 1158 jsAst.Name constructorName = mangledName; | 1158 jsAst.Name constructorName = mangledName; |
| 1159 jsAst.Expression constructorAst = js('function() {}'); | 1159 jsAst.Expression constructorAst = js('function() {}'); |
| 1160 List<jsAst.Name> fieldNames = []; | 1160 List<jsAst.Name> fieldNames = []; |
| 1161 assemblePrecompiledConstructor( | 1161 assemblePrecompiledConstructor( |
| 1162 mainOutputUnit, constructorName, constructorAst, fieldNames); | 1162 mainOutputUnit, constructorName, constructorAst, fieldNames); |
| 1163 } | 1163 } |
| 1164 } | 1164 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1295 pendingStatics.first, 'Pending statics (see above).'); | 1295 pendingStatics.first, 'Pending statics (see above).'); |
| 1296 } | 1296 } |
| 1297 } | 1297 } |
| 1298 | 1298 |
| 1299 void assembleLibrary(Library library, Fragment fragment) { | 1299 void assembleLibrary(Library library, Fragment fragment) { |
| 1300 LibraryElement libraryElement = library.element; | 1300 LibraryElement libraryElement = library.element; |
| 1301 | 1301 |
| 1302 assembleStaticFunctions(library.statics, fragment); | 1302 assembleStaticFunctions(library.statics, fragment); |
| 1303 | 1303 |
| 1304 ClassBuilder libraryBuilder = | 1304 ClassBuilder libraryBuilder = |
| 1305 getElementDescriptor(libraryElement, fragment); | 1305 getLibraryDescriptor(libraryElement, fragment); |
| 1306 for (Class cls in library.classes) { | 1306 for (Class cls in library.classes) { |
| 1307 assembleClass(cls, libraryBuilder, fragment); | 1307 assembleClass(cls, libraryBuilder, fragment); |
| 1308 } | 1308 } |
| 1309 | 1309 |
| 1310 classEmitter.emitFields(library, libraryBuilder, emitStatics: true); | 1310 classEmitter.emitFields(library, libraryBuilder, emitStatics: true); |
| 1311 } | 1311 } |
| 1312 | 1312 |
| 1313 void assembleProgram(Program program) { | 1313 void assembleProgram(Program program) { |
| 1314 for (Fragment fragment in program.fragments) { | 1314 for (Fragment fragment in program.fragments) { |
| 1315 for (Library library in fragment.libraries) { | 1315 for (Library library in fragment.libraries) { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1639 }); | 1639 }); |
| 1640 emitMainOutputUnit(program.mainFragment.outputUnit, mainOutput); | 1640 emitMainOutputUnit(program.mainFragment.outputUnit, mainOutput); |
| 1641 | 1641 |
| 1642 if (backend.backendUsage.requiresPreamble && !backend.htmlLibraryIsLoaded) { | 1642 if (backend.backendUsage.requiresPreamble && !backend.htmlLibraryIsLoaded) { |
| 1643 reporter.reportHintMessage(NO_LOCATION_SPANNABLE, MessageKind.PREAMBLE); | 1643 reporter.reportHintMessage(NO_LOCATION_SPANNABLE, MessageKind.PREAMBLE); |
| 1644 } | 1644 } |
| 1645 // Return the total program size. | 1645 // Return the total program size. |
| 1646 return outputBuffers.values.fold(0, (a, b) => a + b.length); | 1646 return outputBuffers.values.fold(0, (a, b) => a + b.length); |
| 1647 } | 1647 } |
| 1648 | 1648 |
| 1649 ClassBuilder getElementDescriptor(Element element, Fragment fragment) { | 1649 ClassBuilder getStaticMethodDescriptor( |
| 1650 MethodElement element, Fragment fragment) { |
| 1650 Element owner = element.library; | 1651 Element owner = element.library; |
| 1651 if (!element.isLibrary && | 1652 if (!backend.nativeData.isNativeMember(element)) { |
| 1652 !element.isTopLevel && | |
| 1653 !backend.isNative(element)) { | |
| 1654 // For static (not top level) elements, record their code in a buffer | 1653 // For static (not top level) elements, record their code in a buffer |
| 1655 // specific to the class. For now, not supported for native classes and | 1654 // specific to the class. For now, not supported for native classes and |
| 1656 // native elements. | 1655 // native elements. |
| 1657 ClassElement cls = element.enclosingClassOrCompilationUnit.declaration; | 1656 ClassElement cls = element.enclosingClass; |
| 1658 if (compiler.codegenWorldBuilder.directlyInstantiatedClasses | 1657 if (compiler.codegenWorldBuilder.directlyInstantiatedClasses |
| 1659 .contains(cls) && | 1658 .contains(cls) && |
| 1660 !backend.isNative(cls) && | 1659 !backend.nativeData.isNativeClass(cls) && |
| 1661 compiler.deferredLoadTask.outputUnitForElement(element) == | 1660 compiler.deferredLoadTask.outputUnitForElement(element) == |
| 1662 compiler.deferredLoadTask.outputUnitForElement(cls)) { | 1661 compiler.deferredLoadTask.outputUnitForElement(cls)) { |
| 1663 owner = cls; | 1662 owner = cls; |
| 1664 } | 1663 } |
| 1665 } | 1664 } |
| 1665 return _getElementDescriptor(element, owner, fragment); |
| 1666 } |
| 1667 |
| 1668 ClassBuilder getLibraryDescriptor(LibraryElement element, Fragment fragment) { |
| 1669 return _getElementDescriptor(element, element, fragment); |
| 1670 } |
| 1671 |
| 1672 ClassBuilder _getElementDescriptor( |
| 1673 Element element, Element owner, Fragment fragment) { |
| 1666 if (owner == null) { | 1674 if (owner == null) { |
| 1667 reporter.internalError(element, 'Owner is null.'); | 1675 reporter.internalError(element, 'Owner is null.'); |
| 1668 } | 1676 } |
| 1669 return elementDescriptors | 1677 return elementDescriptors |
| 1670 .putIfAbsent(fragment, () => new Map<Element, ClassBuilder>()) | 1678 .putIfAbsent(fragment, () => new Map<Element, ClassBuilder>()) |
| 1671 .putIfAbsent(owner, () { | 1679 .putIfAbsent(owner, () { |
| 1672 return new ClassBuilder(owner, namer, owner.isClass); | 1680 return new ClassBuilder(owner, namer, owner.isClass); |
| 1673 }); | 1681 }); |
| 1674 } | 1682 } |
| 1675 | 1683 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1920 // data. | 1928 // data. |
| 1921 mapping["_comment"] = "This mapping shows which compiled `.js` files are " | 1929 mapping["_comment"] = "This mapping shows which compiled `.js` files are " |
| 1922 "needed for a given deferred library import."; | 1930 "needed for a given deferred library import."; |
| 1923 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); | 1931 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); |
| 1924 compiler.outputProvider( | 1932 compiler.outputProvider( |
| 1925 compiler.options.deferredMapUri.path, '', OutputType.info) | 1933 compiler.options.deferredMapUri.path, '', OutputType.info) |
| 1926 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) | 1934 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) |
| 1927 ..close(); | 1935 ..close(); |
| 1928 } | 1936 } |
| 1929 } | 1937 } |
| OLD | NEW |