Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart

Issue 11308175: Emit constants using ASTs (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address code-review comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 js_backend; 5 part of js_backend;
6 6
7 /** 7 /**
8 * A function element that represents a closure call. The signature is copied 8 * A function element that represents a closure call. The signature is copied
9 * from the given element. 9 * from the given element.
10 */ 10 */
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 74 }
75 75
76 void computeRequiredTypeChecks() { 76 void computeRequiredTypeChecks() {
77 assert(checkedClasses == null); 77 assert(checkedClasses == null);
78 checkedClasses = new Set<ClassElement>(); 78 checkedClasses = new Set<ClassElement>();
79 compiler.codegenWorld.isChecks.forEach((DartType t) { 79 compiler.codegenWorld.isChecks.forEach((DartType t) {
80 if (t is InterfaceType) checkedClasses.add(t.element); 80 if (t is InterfaceType) checkedClasses.add(t.element);
81 }); 81 });
82 } 82 }
83 83
84 void writeConstantToBuffer(Constant value, CodeBuffer buffer, 84 js.Expression constantReference(Constant value) {
85 {emitCanonicalVersion: true}) { 85 return constantEmitter.reference(value);
86 if (emitCanonicalVersion) { 86 }
87 constantEmitter.emitCanonicalVersionOfConstant(value, buffer); 87
88 } else { 88 js.Expression constantInitializerExpression(Constant value) {
89 constantEmitter.emitJavaScriptCodeForConstant(value, buffer); 89 return constantEmitter.initializationExpression(value);
90 } 90 }
91
92 // Deprecated. Remove after last use is converted to JS ASTs.
93 void writeConstantToBuffer(Constant value, CodeBuffer buffer) {
94 buffer.add(js.prettyPrint(constantReference(value), compiler));
91 } 95 }
92 96
93 String get name => 'CodeEmitter'; 97 String get name => 'CodeEmitter';
94 98
95 String get defineClassName 99 String get defineClassName
96 => '${namer.ISOLATE}.\$defineClass'; 100 => '${namer.ISOLATE}.\$defineClass';
97 String get finishClassesName 101 String get finishClassesName
98 => '${namer.ISOLATE}.\$finishClasses'; 102 => '${namer.ISOLATE}.\$finishClasses';
99 String get finishIsolateConstructorName 103 String get finishIsolateConstructorName
100 => '${namer.ISOLATE}.\$finishIsolateConstructor'; 104 => '${namer.ISOLATE}.\$finishIsolateConstructor';
(...skipping 1211 matching lines...) Expand 10 before | Expand all | Expand 10 after
1312 defineInstanceMember(invocationName, getterBuffer); 1316 defineInstanceMember(invocationName, getterBuffer);
1313 } 1317 }
1314 } 1318 }
1315 } 1319 }
1316 1320
1317 void emitStaticNonFinalFieldInitializations(CodeBuffer buffer) { 1321 void emitStaticNonFinalFieldInitializations(CodeBuffer buffer) {
1318 ConstantHandler handler = compiler.constantHandler; 1322 ConstantHandler handler = compiler.constantHandler;
1319 List<VariableElement> staticNonFinalFields = 1323 List<VariableElement> staticNonFinalFields =
1320 handler.getStaticNonFinalFieldsForEmission(); 1324 handler.getStaticNonFinalFieldsForEmission();
1321 for (Element element in staticNonFinalFields) { 1325 for (Element element in staticNonFinalFields) {
1322 buffer.add('$isolateProperties.${namer.getName(element)} = ');
1323 compiler.withCurrentElement(element, () { 1326 compiler.withCurrentElement(element, () {
1324 Constant initialValue = handler.getInitialValueFor(element); 1327 Constant initialValue = handler.getInitialValueFor(element);
1325 writeConstantToBuffer(initialValue, buffer); 1328 js.Expression init =
1326 }); 1329 new js.Assignment(
1327 buffer.add(';\n'); 1330 new js.PropertyAccess.field(
1331 new js.VariableUse(isolateProperties),
1332 namer.getName(element)),
1333 constantEmitter.referenceInInitializationContext(initialValue));
1334 buffer.add(js.prettyPrint(init, compiler));
1335 buffer.add(';\n');
1336 });
1328 } 1337 }
1329 } 1338 }
1330 1339
1331 void emitLazilyInitializedStaticFields(CodeBuffer buffer) { 1340 void emitLazilyInitializedStaticFields(CodeBuffer buffer) {
1332 ConstantHandler handler = compiler.constantHandler; 1341 ConstantHandler handler = compiler.constantHandler;
1333 List<VariableElement> lazyFields = 1342 List<VariableElement> lazyFields =
1334 handler.getLazilyInitializedFieldsForEmission(); 1343 handler.getLazilyInitializedFieldsForEmission();
1335 if (!lazyFields.isEmpty) { 1344 if (!lazyFields.isEmpty) {
1336 needsLazyInitializer = true; 1345 needsLazyInitializer = true;
1337 for (VariableElement element in lazyFields) { 1346 for (VariableElement element in lazyFields) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1375 1384
1376 String name = namer.constantName(constant); 1385 String name = namer.constantName(constant);
1377 // The name is null when the constant is already a JS constant. 1386 // The name is null when the constant is already a JS constant.
1378 // TODO(floitsch): every constant should be registered, so that we can 1387 // TODO(floitsch): every constant should be registered, so that we can
1379 // share the ones that take up too much space (like some strings). 1388 // share the ones that take up too much space (like some strings).
1380 if (name == null) continue; 1389 if (name == null) continue;
1381 if (!addedMakeConstantList && constant.isList()) { 1390 if (!addedMakeConstantList && constant.isList()) {
1382 addedMakeConstantList = true; 1391 addedMakeConstantList = true;
1383 emitMakeConstantList(buffer); 1392 emitMakeConstantList(buffer);
1384 } 1393 }
1385 buffer.add('$isolateProperties.$name = '); 1394 js.Expression init =
1386 writeConstantToBuffer(constant, buffer, emitCanonicalVersion: false); 1395 new js.Assignment(
1396 new js.PropertyAccess.field(
1397 new js.VariableUse(isolateProperties),
1398 name),
1399 constantInitializerExpression(constant));
1400 buffer.add(js.prettyPrint(init, compiler));
1387 buffer.add(';\n'); 1401 buffer.add(';\n');
1388 } 1402 }
1389 } 1403 }
1390 1404
1391 void emitMakeConstantList(CodeBuffer buffer) { 1405 void emitMakeConstantList(CodeBuffer buffer) {
1392 buffer.add(namer.ISOLATE); 1406 buffer.add(namer.ISOLATE);
1393 buffer.add(r'''.makeConstantList = function(list) { 1407 buffer.add(r'''.makeConstantList = function(list) {
1394 list.immutable$list = true; 1408 list.immutable$list = true;
1395 list.fixed$length = true; 1409 list.fixed$length = true;
1396 return list; 1410 return list;
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
1804 const String HOOKS_API_USAGE = """ 1818 const String HOOKS_API_USAGE = """
1805 // Generated by dart2js, the Dart to JavaScript compiler. 1819 // Generated by dart2js, the Dart to JavaScript compiler.
1806 // The code supports the following hooks: 1820 // The code supports the following hooks:
1807 // dartPrint(message) - if this function is defined it is called 1821 // dartPrint(message) - if this function is defined it is called
1808 // instead of the Dart [print] method. 1822 // instead of the Dart [print] method.
1809 // dartMainRunner(main) - if this function is defined, the Dart [main] 1823 // dartMainRunner(main) - if this function is defined, the Dart [main]
1810 // method will not be invoked directly. 1824 // method will not be invoked directly.
1811 // Instead, a closure that will invoke [main] is 1825 // Instead, a closure that will invoke [main] is
1812 // passed to [dartMainRunner]. 1826 // passed to [dartMainRunner].
1813 """; 1827 """;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698