OLD | NEW |
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 Loading... |
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 js.Expression constantReference(Constant value) { | 84 void writeConstantToBuffer(Constant value, CodeBuffer buffer, |
85 return constantEmitter.reference(value); | 85 {emitCanonicalVersion: true}) { |
86 } | 86 if (emitCanonicalVersion) { |
87 | 87 constantEmitter.emitCanonicalVersionOfConstant(value, buffer); |
88 js.Expression constantInitializerExpression(Constant value) { | 88 } else { |
89 return constantEmitter.initializationExpression(value); | 89 constantEmitter.emitJavaScriptCodeForConstant(value, buffer); |
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)); | |
95 } | 91 } |
96 | 92 |
97 String get name => 'CodeEmitter'; | 93 String get name => 'CodeEmitter'; |
98 | 94 |
99 String get defineClassName | 95 String get defineClassName |
100 => '${namer.ISOLATE}.\$defineClass'; | 96 => '${namer.ISOLATE}.\$defineClass'; |
101 String get finishClassesName | 97 String get finishClassesName |
102 => '${namer.ISOLATE}.\$finishClasses'; | 98 => '${namer.ISOLATE}.\$finishClasses'; |
103 String get finishIsolateConstructorName | 99 String get finishIsolateConstructorName |
104 => '${namer.ISOLATE}.\$finishIsolateConstructor'; | 100 => '${namer.ISOLATE}.\$finishIsolateConstructor'; |
(...skipping 1211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1316 defineInstanceMember(invocationName, getterBuffer); | 1312 defineInstanceMember(invocationName, getterBuffer); |
1317 } | 1313 } |
1318 } | 1314 } |
1319 } | 1315 } |
1320 | 1316 |
1321 void emitStaticNonFinalFieldInitializations(CodeBuffer buffer) { | 1317 void emitStaticNonFinalFieldInitializations(CodeBuffer buffer) { |
1322 ConstantHandler handler = compiler.constantHandler; | 1318 ConstantHandler handler = compiler.constantHandler; |
1323 List<VariableElement> staticNonFinalFields = | 1319 List<VariableElement> staticNonFinalFields = |
1324 handler.getStaticNonFinalFieldsForEmission(); | 1320 handler.getStaticNonFinalFieldsForEmission(); |
1325 for (Element element in staticNonFinalFields) { | 1321 for (Element element in staticNonFinalFields) { |
| 1322 buffer.add('$isolateProperties.${namer.getName(element)} = '); |
1326 compiler.withCurrentElement(element, () { | 1323 compiler.withCurrentElement(element, () { |
1327 Constant initialValue = handler.getInitialValueFor(element); | 1324 Constant initialValue = handler.getInitialValueFor(element); |
1328 js.Expression init = | 1325 writeConstantToBuffer(initialValue, buffer); |
1329 new js.Assignment( | 1326 }); |
1330 new js.PropertyAccess.field( | 1327 buffer.add(';\n'); |
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 }); | |
1337 } | 1328 } |
1338 } | 1329 } |
1339 | 1330 |
1340 void emitLazilyInitializedStaticFields(CodeBuffer buffer) { | 1331 void emitLazilyInitializedStaticFields(CodeBuffer buffer) { |
1341 ConstantHandler handler = compiler.constantHandler; | 1332 ConstantHandler handler = compiler.constantHandler; |
1342 List<VariableElement> lazyFields = | 1333 List<VariableElement> lazyFields = |
1343 handler.getLazilyInitializedFieldsForEmission(); | 1334 handler.getLazilyInitializedFieldsForEmission(); |
1344 if (!lazyFields.isEmpty) { | 1335 if (!lazyFields.isEmpty) { |
1345 needsLazyInitializer = true; | 1336 needsLazyInitializer = true; |
1346 for (VariableElement element in lazyFields) { | 1337 for (VariableElement element in lazyFields) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1384 | 1375 |
1385 String name = namer.constantName(constant); | 1376 String name = namer.constantName(constant); |
1386 // The name is null when the constant is already a JS constant. | 1377 // The name is null when the constant is already a JS constant. |
1387 // TODO(floitsch): every constant should be registered, so that we can | 1378 // TODO(floitsch): every constant should be registered, so that we can |
1388 // share the ones that take up too much space (like some strings). | 1379 // share the ones that take up too much space (like some strings). |
1389 if (name == null) continue; | 1380 if (name == null) continue; |
1390 if (!addedMakeConstantList && constant.isList()) { | 1381 if (!addedMakeConstantList && constant.isList()) { |
1391 addedMakeConstantList = true; | 1382 addedMakeConstantList = true; |
1392 emitMakeConstantList(buffer); | 1383 emitMakeConstantList(buffer); |
1393 } | 1384 } |
1394 js.Expression init = | 1385 buffer.add('$isolateProperties.$name = '); |
1395 new js.Assignment( | 1386 writeConstantToBuffer(constant, buffer, emitCanonicalVersion: false); |
1396 new js.PropertyAccess.field( | |
1397 new js.VariableUse(isolateProperties), | |
1398 name), | |
1399 constantInitializerExpression(constant)); | |
1400 buffer.add(js.prettyPrint(init, compiler)); | |
1401 buffer.add(';\n'); | 1387 buffer.add(';\n'); |
1402 } | 1388 } |
1403 } | 1389 } |
1404 | 1390 |
1405 void emitMakeConstantList(CodeBuffer buffer) { | 1391 void emitMakeConstantList(CodeBuffer buffer) { |
1406 buffer.add(namer.ISOLATE); | 1392 buffer.add(namer.ISOLATE); |
1407 buffer.add(r'''.makeConstantList = function(list) { | 1393 buffer.add(r'''.makeConstantList = function(list) { |
1408 list.immutable$list = true; | 1394 list.immutable$list = true; |
1409 list.fixed$length = true; | 1395 list.fixed$length = true; |
1410 return list; | 1396 return list; |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1818 const String HOOKS_API_USAGE = """ | 1804 const String HOOKS_API_USAGE = """ |
1819 // Generated by dart2js, the Dart to JavaScript compiler. | 1805 // Generated by dart2js, the Dart to JavaScript compiler. |
1820 // The code supports the following hooks: | 1806 // The code supports the following hooks: |
1821 // dartPrint(message) - if this function is defined it is called | 1807 // dartPrint(message) - if this function is defined it is called |
1822 // instead of the Dart [print] method. | 1808 // instead of the Dart [print] method. |
1823 // dartMainRunner(main) - if this function is defined, the Dart [main] | 1809 // dartMainRunner(main) - if this function is defined, the Dart [main] |
1824 // method will not be invoked directly. | 1810 // method will not be invoked directly. |
1825 // Instead, a closure that will invoke [main] is | 1811 // Instead, a closure that will invoke [main] is |
1826 // passed to [dartMainRunner]. | 1812 // passed to [dartMainRunner]. |
1827 """; | 1813 """; |
OLD | NEW |