| 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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 | 7 |
| 8 class OldEmitter implements Emitter { | 8 class OldEmitter implements Emitter { |
| 9 final Compiler compiler; | 9 final Compiler compiler; |
| 10 final CodeEmitterTask task; | 10 final CodeEmitterTask task; |
| (...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 } | 1233 } |
| 1234 }''', | 1234 }''', |
| 1235 {"constructorName": constructorName, | 1235 {"constructorName": constructorName, |
| 1236 "constructorNameString": js.string(constructorName), | 1236 "constructorNameString": js.string(constructorName), |
| 1237 "hasIsolateSupport": hasIsolateSupport, | 1237 "hasIsolateSupport": hasIsolateSupport, |
| 1238 "fieldNamesArray": fieldNamesArray})); | 1238 "fieldNamesArray": fieldNamesArray})); |
| 1239 | 1239 |
| 1240 cspPrecompiledConstructorNamesFor(outputUnit).add(js('#', constructorName)); | 1240 cspPrecompiledConstructorNamesFor(outputUnit).add(js('#', constructorName)); |
| 1241 } | 1241 } |
| 1242 | 1242 |
| 1243 /// Extracts the output name of the compiler's outputUri. | |
| 1244 String deferredPartFileName(OutputUnit outputUnit, | |
| 1245 {bool addExtension: true}) { | |
| 1246 String outPath = compiler.outputUri != null | |
| 1247 ? compiler.outputUri.path | |
| 1248 : "out"; | |
| 1249 String outName = outPath.substring(outPath.lastIndexOf('/') + 1); | |
| 1250 String extension = addExtension ? ".part.js" : ""; | |
| 1251 if (outputUnit == compiler.deferredLoadTask.mainOutputUnit) { | |
| 1252 return "$outName$extension"; | |
| 1253 } else { | |
| 1254 String name = outputUnit.name; | |
| 1255 return "${outName}_$name$extension"; | |
| 1256 } | |
| 1257 } | |
| 1258 | |
| 1259 void emitLibraries(Iterable<LibraryElement> libraries) { | 1243 void emitLibraries(Iterable<LibraryElement> libraries) { |
| 1260 if (libraries.isEmpty) return; | 1244 if (libraries.isEmpty) return; |
| 1261 | 1245 |
| 1262 // TODO(karlklose): document what kinds of fields this loop adds to the | 1246 // TODO(karlklose): document what kinds of fields this loop adds to the |
| 1263 // library class builder. | 1247 // library class builder. |
| 1264 for (LibraryElement element in libraries) { | 1248 for (LibraryElement element in libraries) { |
| 1265 LibraryElement library = element; | 1249 LibraryElement library = element; |
| 1266 ClassBuilder builder = new ClassBuilder(library, namer); | 1250 ClassBuilder builder = new ClassBuilder(library, namer); |
| 1267 if (classEmitter.emitFields(library, builder, emitStatics: true)) { | 1251 if (classEmitter.emitFields(library, builder, emitStatics: true)) { |
| 1268 jsAst.ObjectInitializer initializer = builder.toObjectInitializer(); | 1252 jsAst.ObjectInitializer initializer = builder.toObjectInitializer(); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1507 } | 1491 } |
| 1508 | 1492 |
| 1509 // Constants in checked mode call into RTI code to set type information | 1493 // Constants in checked mode call into RTI code to set type information |
| 1510 // which may need getInterceptor (and one-shot interceptor) methods, so | 1494 // which may need getInterceptor (and one-shot interceptor) methods, so |
| 1511 // we have to make sure that [emitGetInterceptorMethods] and | 1495 // we have to make sure that [emitGetInterceptorMethods] and |
| 1512 // [emitOneShotInterceptors] have been called. | 1496 // [emitOneShotInterceptors] have been called. |
| 1513 emitCompileTimeConstants(mainBuffer, mainOutputUnit); | 1497 emitCompileTimeConstants(mainBuffer, mainOutputUnit); |
| 1514 | 1498 |
| 1515 emitDeferredBoilerPlate(mainBuffer, deferredLoadHashes); | 1499 emitDeferredBoilerPlate(mainBuffer, deferredLoadHashes); |
| 1516 | 1500 |
| 1501 if (compiler.deferredMapUri != null) { |
| 1502 outputDeferredMap(); |
| 1503 } |
| 1504 |
| 1517 // Static field initializations require the classes and compile-time | 1505 // Static field initializations require the classes and compile-time |
| 1518 // constants to be set up. | 1506 // constants to be set up. |
| 1519 emitStaticNonFinalFieldInitializations(mainBuffer, mainOutputUnit); | 1507 emitStaticNonFinalFieldInitializations(mainBuffer, mainOutputUnit); |
| 1520 interceptorEmitter.emitMapTypeToInterceptor(mainBuffer); | 1508 interceptorEmitter.emitMapTypeToInterceptor(mainBuffer); |
| 1521 emitLazilyInitializedStaticFields(mainBuffer); | 1509 emitLazilyInitializedStaticFields(mainBuffer); |
| 1522 | 1510 |
| 1523 mainBuffer.writeln(); | 1511 mainBuffer.writeln(); |
| 1524 mainBuffer.write(nativeBuffer); | 1512 mainBuffer.write(nativeBuffer); |
| 1525 | 1513 |
| 1526 metadataEmitter.emitMetadata(mainBuffer); | 1514 metadataEmitter.emitMetadata(mainBuffer); |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1877 Map<String, List<String>> deferredLibraryUris = | 1865 Map<String, List<String>> deferredLibraryUris = |
| 1878 new Map<String, List<String>>(); | 1866 new Map<String, List<String>>(); |
| 1879 Map<String, List<String>> deferredLibraryHashes = | 1867 Map<String, List<String>> deferredLibraryHashes = |
| 1880 new Map<String, List<String>>(); | 1868 new Map<String, List<String>>(); |
| 1881 compiler.deferredLoadTask.hunksToLoad.forEach( | 1869 compiler.deferredLoadTask.hunksToLoad.forEach( |
| 1882 (String loadId, List<OutputUnit>outputUnits) { | 1870 (String loadId, List<OutputUnit>outputUnits) { |
| 1883 List<String> uris = new List<String>(); | 1871 List<String> uris = new List<String>(); |
| 1884 List<String> hashes = new List<String>(); | 1872 List<String> hashes = new List<String>(); |
| 1885 deferredLibraryHashes[loadId] = new List<String>(); | 1873 deferredLibraryHashes[loadId] = new List<String>(); |
| 1886 for (OutputUnit outputUnit in outputUnits) { | 1874 for (OutputUnit outputUnit in outputUnits) { |
| 1887 uris.add(deferredPartFileName(outputUnit)); | 1875 uris.add(backend.deferredPartFileName(outputUnit.name)); |
| 1888 hashes.add(deferredLoadHashes[outputUnit]); | 1876 hashes.add(deferredLoadHashes[outputUnit]); |
| 1889 } | 1877 } |
| 1890 | 1878 |
| 1891 deferredLibraryUris[loadId] = uris; | 1879 deferredLibraryUris[loadId] = uris; |
| 1892 deferredLibraryHashes[loadId] = hashes; | 1880 deferredLibraryHashes[loadId] = hashes; |
| 1893 }); | 1881 }); |
| 1894 | 1882 |
| 1895 void emitMapping(String name, Map<String, List<String>> mapping) { | 1883 void emitMapping(String name, Map<String, List<String>> mapping) { |
| 1896 List<jsAst.Property> properties = new List<jsAst.Property>(); | 1884 List<jsAst.Property> properties = new List<jsAst.Property>(); |
| 1897 mapping.forEach((String key, List<String> values) { | 1885 mapping.forEach((String key, List<String> values) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1988 } | 1976 } |
| 1989 | 1977 |
| 1990 // Make a unique hash of the code (before the sourcemaps are added) | 1978 // Make a unique hash of the code (before the sourcemaps are added) |
| 1991 // This will be used to retrieve the initializing function from the global | 1979 // This will be used to retrieve the initializing function from the global |
| 1992 // variable. | 1980 // variable. |
| 1993 String hash = hashOfString(outputBuffer.getText()); | 1981 String hash = hashOfString(outputBuffer.getText()); |
| 1994 | 1982 |
| 1995 outputBuffer.write('${deferredInitializers}["$hash"]$_=$_' | 1983 outputBuffer.write('${deferredInitializers}["$hash"]$_=$_' |
| 1996 '${deferredInitializers}.current$N'); | 1984 '${deferredInitializers}.current$N'); |
| 1997 | 1985 |
| 1998 String partPrefix = deferredPartFileName(outputUnit, addExtension: false); | 1986 String partPrefix = |
| 1987 backend.deferredPartFileName(outputUnit.name, addExtension: false); |
| 1999 if (generateSourceMap) { | 1988 if (generateSourceMap) { |
| 2000 Uri mapUri, partUri; | 1989 Uri mapUri, partUri; |
| 2001 Uri sourceMapUri = compiler.sourceMapUri; | 1990 Uri sourceMapUri = compiler.sourceMapUri; |
| 2002 Uri outputUri = compiler.outputUri; | 1991 Uri outputUri = compiler.outputUri; |
| 2003 | 1992 |
| 2004 String partName = "$partPrefix.part"; | 1993 String partName = "$partPrefix.part"; |
| 2005 | 1994 |
| 2006 if (sourceMapUri != null) { | 1995 if (sourceMapUri != null) { |
| 2007 String mapFileName = partName + ".js.map"; | 1996 String mapFileName = partName + ".js.map"; |
| 2008 List<String> mapSegments = sourceMapUri.pathSegments.toList(); | 1997 List<String> mapSegments = sourceMapUri.pathSegments.toList(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2046 SourceFile compiledFile = new StringSourceFile(null, code); | 2035 SourceFile compiledFile = new StringSourceFile(null, code); |
| 2047 SourceMapBuilder sourceMapBuilder = | 2036 SourceMapBuilder sourceMapBuilder = |
| 2048 new SourceMapBuilder(sourceMapUri, fileUri, compiledFile); | 2037 new SourceMapBuilder(sourceMapUri, fileUri, compiledFile); |
| 2049 buffer.forEachSourceLocation(sourceMapBuilder.addMapping); | 2038 buffer.forEachSourceLocation(sourceMapBuilder.addMapping); |
| 2050 String sourceMap = sourceMapBuilder.build(); | 2039 String sourceMap = sourceMapBuilder.build(); |
| 2051 compiler.outputProvider(name, 'js.map') | 2040 compiler.outputProvider(name, 'js.map') |
| 2052 ..add(sourceMap) | 2041 ..add(sourceMap) |
| 2053 ..close(); | 2042 ..close(); |
| 2054 } | 2043 } |
| 2055 | 2044 |
| 2045 void outputDeferredMap() { |
| 2046 Map<String, dynamic> mapping = new Map<String, dynamic>(); |
| 2047 // Json does not support comments, so we embed the explanation in the |
| 2048 // data. |
| 2049 mapping["_comment"] = "This mapping shows which compiled `.js` files are " |
| 2050 "needed for a given deferred library import."; |
| 2051 mapping.addAll(compiler.deferredLoadTask.computeDeferredMap()); |
| 2052 compiler.outputProvider(compiler.deferredMapUri.path, 'deferred_map') |
| 2053 ..add(const JsonEncoder.withIndent(" ").convert(mapping)) |
| 2054 ..close(); |
| 2055 } |
| 2056 |
| 2056 void invalidateCaches() { | 2057 void invalidateCaches() { |
| 2057 if (!compiler.hasIncrementalSupport) return; | 2058 if (!compiler.hasIncrementalSupport) return; |
| 2058 if (cachedElements.isEmpty) return; | 2059 if (cachedElements.isEmpty) return; |
| 2059 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { | 2060 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { |
| 2060 if (element.isInstanceMember) { | 2061 if (element.isInstanceMember) { |
| 2061 cachedClassBuilders.remove(element.enclosingClass); | 2062 cachedClassBuilders.remove(element.enclosingClass); |
| 2062 | 2063 |
| 2063 nativeEmitter.cachedBuilders.remove(element.enclosingClass); | 2064 nativeEmitter.cachedBuilders.remove(element.enclosingClass); |
| 2064 | 2065 |
| 2065 } | 2066 } |
| 2066 } | 2067 } |
| 2067 } | 2068 } |
| 2068 } | 2069 } |
| OLD | NEW |