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 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 } else { | 609 } else { |
610 return js('#(#,#,#,#)', | 610 return js('#(#,#,#,#)', |
611 [js(lazyInitializerName), | 611 [js(lazyInitializerName), |
612 js.string(namer.globalPropertyName(element)), | 612 js.string(namer.globalPropertyName(element)), |
613 js.string(namer.lazyInitializerName(element)), | 613 js.string(namer.lazyInitializerName(element)), |
614 code, | 614 code, |
615 js.string(element.name)]); | 615 js.string(element.name)]); |
616 } | 616 } |
617 } | 617 } |
618 | 618 |
619 void emitMetadata(Program program, CodeOutput output) { | 619 void emitMetadata(Program program, CodeOutput output, OutputUnit outputUnit) { |
620 | 620 |
621 addMetadataGlobal(List<String> list, String global) { | 621 jsAst.Expression constructList(List<String> list) { |
622 String globalAccess = generateEmbeddedGlobalAccessString(global); | 622 String listAsString = list == null ? '[]' : '[${list.join(",")}]'; |
623 output.add('$globalAccess$_=$_['); | 623 return js.uncachedExpressionTemplate(listAsString).instantiate([]); |
624 for (String data in list) { | 624 } |
625 if (data is String) { | |
626 if (data != 'null') { | |
627 output.add(data); | |
628 } | |
629 } else { | |
630 throw 'Unexpected value in ${global}: ${Error.safeToString(data)}'; | |
631 } | |
632 output.add(',$n'); | |
633 } | |
634 output.add('];$n'); | |
635 } | |
636 | 625 |
637 addMetadataGlobal(program.metadata, embeddedNames.METADATA); | 626 List<String> types = program.metadataTypes[outputUnit]; |
638 addMetadataGlobal(program.metadataTypes, embeddedNames.TYPES); | 627 |
| 628 if (outputUnit == compiler.deferredLoadTask.mainOutputUnit) { |
| 629 jsAst.Expression metadataAccess = |
| 630 generateEmbeddedGlobalAccess(embeddedNames.METADATA); |
| 631 jsAst.Expression typesAccess = |
| 632 generateEmbeddedGlobalAccess(embeddedNames.TYPES); |
| 633 |
| 634 output.addBuffer( |
| 635 jsAst.prettyPrint(new jsAst.Block([ |
| 636 js.statement('# = #;', [metadataAccess, |
| 637 constructList(program.metadata)]), |
| 638 js.statement('# = #;', [typesAccess, constructList(types)])]), |
| 639 compiler, monitor: compiler.dumpInfoTask)); |
| 640 output.add(n); |
| 641 } else if (types != null) { |
| 642 output.addBuffer( |
| 643 jsAst.prettyPrint( |
| 644 js.statement('var ${namer.deferredTypesName} = #;', |
| 645 constructList(types)), |
| 646 compiler, monitor: compiler.dumpInfoTask)); |
| 647 output.add(n); |
| 648 } |
639 } | 649 } |
640 | 650 |
641 void emitCompileTimeConstants(CodeOutput output, | 651 void emitCompileTimeConstants(CodeOutput output, |
642 List<Constant> constants, | 652 List<Constant> constants, |
643 {bool isMainFragment}) { | 653 {bool isMainFragment}) { |
644 assert(isMainFragment != null); | 654 assert(isMainFragment != null); |
645 | 655 |
646 if (constants.isEmpty) return; | 656 if (constants.isEmpty) return; |
647 CodeOutput constantOutput = output; | 657 CodeOutput constantOutput = output; |
648 if (compiler.hasIncrementalSupport && isMainFragment) { | 658 if (compiler.hasIncrementalSupport && isMainFragment) { |
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 buildCspPrecompiledFunctionFor(mainOutputUnit); | 1293 buildCspPrecompiledFunctionFor(mainOutputUnit); |
1284 mainOutput.addBuffer( | 1294 mainOutput.addBuffer( |
1285 jsAst.prettyPrint( | 1295 jsAst.prettyPrint( |
1286 precompiledFunctionAst, | 1296 precompiledFunctionAst, |
1287 compiler, | 1297 compiler, |
1288 monitor: compiler.dumpInfoTask, | 1298 monitor: compiler.dumpInfoTask, |
1289 allowVariableMinification: false)); | 1299 allowVariableMinification: false)); |
1290 mainOutput.add(N); | 1300 mainOutput.add(N); |
1291 } | 1301 } |
1292 | 1302 |
1293 mainOutput.add('$setupProgramName(dart)$N'); | 1303 mainOutput.add('$setupProgramName(dart, 0)$N'); |
1294 | 1304 |
1295 interceptorEmitter.emitGetInterceptorMethods(mainOutput); | 1305 interceptorEmitter.emitGetInterceptorMethods(mainOutput); |
1296 interceptorEmitter.emitOneShotInterceptors(mainOutput); | 1306 interceptorEmitter.emitOneShotInterceptors(mainOutput); |
1297 | 1307 |
1298 if (task.outputContainsConstantList) { | 1308 if (task.outputContainsConstantList) { |
1299 emitMakeConstantList(mainOutput); | 1309 emitMakeConstantList(mainOutput); |
1300 } | 1310 } |
1301 | 1311 |
1302 // Constants in checked mode call into RTI code to set type information | 1312 // Constants in checked mode call into RTI code to set type information |
1303 // which may need getInterceptor (and one-shot interceptor) methods, so | 1313 // which may need getInterceptor (and one-shot interceptor) methods, so |
(...skipping 12 matching lines...) Expand all Loading... |
1316 // constants to be set up. | 1326 // constants to be set up. |
1317 emitStaticNonFinalFieldInitializations(mainOutput, mainOutputUnit); | 1327 emitStaticNonFinalFieldInitializations(mainOutput, mainOutputUnit); |
1318 interceptorEmitter.emitTypeToInterceptorMap(program, mainOutput); | 1328 interceptorEmitter.emitTypeToInterceptorMap(program, mainOutput); |
1319 if (compiler.enableMinification) { | 1329 if (compiler.enableMinification) { |
1320 mainOutput.add(';'); | 1330 mainOutput.add(';'); |
1321 } | 1331 } |
1322 emitLazilyInitializedStaticFields(mainOutput); | 1332 emitLazilyInitializedStaticFields(mainOutput); |
1323 | 1333 |
1324 mainOutput.add('\n'); | 1334 mainOutput.add('\n'); |
1325 | 1335 |
1326 emitMetadata(program, mainOutput); | 1336 emitMetadata(program, mainOutput, mainOutputUnit); |
1327 | 1337 |
1328 isolateProperties = isolatePropertiesName; | 1338 isolateProperties = isolatePropertiesName; |
1329 // The following code should not use the short-hand for the | 1339 // The following code should not use the short-hand for the |
1330 // initialStatics. | 1340 // initialStatics. |
1331 mainOutput.add('${namer.currentIsolate}$_=${_}null$N'); | 1341 mainOutput.add('${namer.currentIsolate}$_=${_}null$N'); |
1332 | 1342 |
1333 emitFinishIsolateConstructorInvocation(mainOutput); | 1343 emitFinishIsolateConstructorInvocation(mainOutput); |
1334 mainOutput.add( | 1344 mainOutput.add( |
1335 '${namer.currentIsolate}$_=${_}new ${namer.isolateName}()$N'); | 1345 '${namer.currentIsolate}$_=${_}new ${namer.isolateName}()$N'); |
1336 | 1346 |
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1721 output | 1731 output |
1722 .add('var $globalObject$_=$_' | 1732 .add('var $globalObject$_=$_' |
1723 '${globalsHolder}.$globalObject$N'); | 1733 '${globalsHolder}.$globalObject$N'); |
1724 } | 1734 } |
1725 output | 1735 output |
1726 ..add('var init$_=$_${globalsHolder}.init$N') | 1736 ..add('var init$_=$_${globalsHolder}.init$N') |
1727 ..add('var $setupProgramName$_=$_' | 1737 ..add('var $setupProgramName$_=$_' |
1728 '$globalsHolder.$setupProgramName$N') | 1738 '$globalsHolder.$setupProgramName$N') |
1729 ..add('var ${namer.isolateName}$_=$_' | 1739 ..add('var ${namer.isolateName}$_=$_' |
1730 '${globalsHolder}.${namer.isolateName}$N'); | 1740 '${globalsHolder}.${namer.isolateName}$N'); |
| 1741 String typesAccess = |
| 1742 generateEmbeddedGlobalAccessString(embeddedNames.TYPES); |
1731 if (libraryDescriptorBuffer != null) { | 1743 if (libraryDescriptorBuffer != null) { |
1732 // TODO(ahe): This defines a lot of properties on the | 1744 // TODO(ahe): This defines a lot of properties on the |
1733 // Isolate.prototype object. We know this will turn it into a | 1745 // Isolate.prototype object. We know this will turn it into a |
1734 // slow object in V8, so instead we should do something similar | 1746 // slow object in V8, so instead we should do something similar |
1735 // to Isolate.$finishIsolateConstructor. | 1747 // to Isolate.$finishIsolateConstructor. |
1736 output | 1748 output |
1737 ..add('var ${namer.currentIsolate}$_=$_$isolatePropertiesName$N') | 1749 ..add('var ${namer.currentIsolate}$_=$_$isolatePropertiesName$N') |
1738 // The argument to reflectionDataParser is assigned to a temporary | 1750 // The argument to reflectionDataParser is assigned to a temporary |
1739 // 'dart' so that 'dart.' will appear as the prefix to dart methods | 1751 // 'dart' so that 'dart.' will appear as the prefix to dart methods |
1740 // in stack traces and profile entries. | 1752 // in stack traces and profile entries. |
1741 ..add('var dart = [$n ') | 1753 ..add('var dart = [$n ') |
1742 ..addBuffer(libraryDescriptorBuffer) | 1754 ..addBuffer(libraryDescriptorBuffer) |
1743 ..add(']$N'); | 1755 ..add(']$N'); |
1744 | 1756 |
1745 if (compiler.useContentSecurityPolicy) { | 1757 if (compiler.useContentSecurityPolicy) { |
1746 jsAst.Statement precompiledFunctionAst = | 1758 jsAst.Statement precompiledFunctionAst = |
1747 buildCspPrecompiledFunctionFor(outputUnit); | 1759 buildCspPrecompiledFunctionFor(outputUnit); |
1748 | 1760 |
1749 output.addBuffer( | 1761 output.addBuffer( |
1750 jsAst.prettyPrint( | 1762 jsAst.prettyPrint( |
1751 precompiledFunctionAst, compiler, | 1763 precompiledFunctionAst, compiler, |
1752 monitor: compiler.dumpInfoTask, | 1764 monitor: compiler.dumpInfoTask, |
1753 allowVariableMinification: false)); | 1765 allowVariableMinification: false)); |
1754 output.add(N); | 1766 output.add(N); |
1755 } | 1767 } |
1756 output.add('$setupProgramName(dart)$N'); | 1768 output.add('$setupProgramName(dart, ${typesAccess}.length)$N'); |
| 1769 } |
| 1770 |
| 1771 if (task.metadataCollector.types[outputUnit] != null) { |
| 1772 emitMetadata(program, output, outputUnit); |
| 1773 output.add('${typesAccess}.' |
| 1774 'push.apply(${typesAccess},$_${namer.deferredTypesName})$N'); |
1757 } | 1775 } |
1758 | 1776 |
1759 // Set the currentIsolate variable to the current isolate (which is | 1777 // Set the currentIsolate variable to the current isolate (which is |
1760 // provided as second argument). | 1778 // provided as second argument). |
1761 // We need to do this, because we use the same variable for setting up | 1779 // We need to do this, because we use the same variable for setting up |
1762 // the isolate-properties and for storing the current isolate. During | 1780 // the isolate-properties and for storing the current isolate. During |
1763 // the setup (the code above this lines) we must set the variable to | 1781 // the setup (the code above this lines) we must set the variable to |
1764 // the isolate-properties. | 1782 // the isolate-properties. |
1765 // After we have done the setup it must point to the current Isolate. | 1783 // After we have done the setup it must point to the current Isolate. |
1766 // Otherwise all methods/functions accessing isolate variables will | 1784 // Otherwise all methods/functions accessing isolate variables will |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1856 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { | 1874 for (Element element in compiler.enqueuer.codegen.newlyEnqueuedElements) { |
1857 if (element.isInstanceMember) { | 1875 if (element.isInstanceMember) { |
1858 cachedClassBuilders.remove(element.enclosingClass); | 1876 cachedClassBuilders.remove(element.enclosingClass); |
1859 | 1877 |
1860 nativeEmitter.cachedBuilders.remove(element.enclosingClass); | 1878 nativeEmitter.cachedBuilders.remove(element.enclosingClass); |
1861 | 1879 |
1862 } | 1880 } |
1863 } | 1881 } |
1864 } | 1882 } |
1865 } | 1883 } |
OLD | NEW |