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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 | 100 |
101 /** | 101 /** |
102 * Raw Typedef symbols occuring in is-checks and type assertions. If the | 102 * Raw Typedef symbols occuring in is-checks and type assertions. If the |
103 * program contains `x is F<int>` and `x is F<bool>` then the TypedefElement | 103 * program contains `x is F<int>` and `x is F<bool>` then the TypedefElement |
104 * `F` will occur once in [checkedTypedefs]. | 104 * `F` will occur once in [checkedTypedefs]. |
105 */ | 105 */ |
106 Set<TypedefElement> checkedTypedefs; | 106 Set<TypedefElement> checkedTypedefs; |
107 | 107 |
108 final bool generateSourceMap; | 108 final bool generateSourceMap; |
109 | 109 |
110 Iterable<ClassElement> cachedClassesUsingTypeVariableTests; | |
111 | |
112 Iterable<ClassElement> get classesUsingTypeVariableTests { | |
113 if (cachedClassesUsingTypeVariableTests == null) { | |
114 cachedClassesUsingTypeVariableTests = compiler.codegenWorld.isChecks | |
115 .where((DartType t) => t is TypeVariableType) | |
116 .map((TypeVariableType v) => v.element.getEnclosingClass()) | |
117 .toList(); | |
118 } | |
119 return cachedClassesUsingTypeVariableTests; | |
120 } | |
121 | |
110 CodeEmitterTask(Compiler compiler, Namer namer, this.generateSourceMap) | 122 CodeEmitterTask(Compiler compiler, Namer namer, this.generateSourceMap) |
111 : boundClosureBuffer = new CodeBuffer(), | 123 : boundClosureBuffer = new CodeBuffer(), |
112 mainBuffer = new CodeBuffer(), | 124 mainBuffer = new CodeBuffer(), |
113 this.namer = namer, | 125 this.namer = namer, |
114 boundClosureCache = new Map<int, String>(), | 126 boundClosureCache = new Map<int, String>(), |
115 interceptorClosureCache = new Map<int, String>(), | 127 interceptorClosureCache = new Map<int, String>(), |
116 constantEmitter = new ConstantEmitter(compiler, namer), | 128 constantEmitter = new ConstantEmitter(compiler, namer), |
117 super(compiler) { | 129 super(compiler) { |
118 nativeEmitter = new NativeEmitter(this); | 130 nativeEmitter = new NativeEmitter(this); |
119 } | 131 } |
120 | 132 |
121 void computeRequiredTypeChecks() { | 133 void computeRequiredTypeChecks() { |
122 assert(checkedClasses == null); | 134 assert(checkedClasses == null && |
135 checkedTypedefs == null); | |
ngeoffray
2013/02/26 14:11:37
Fits in one line.
karlklose
2013/02/27 10:12:58
Done.
| |
136 compiler.codegenWorld.addImplicitChecks(classesUsingTypeVariableTests); | |
137 | |
123 checkedClasses = new Set<ClassElement>(); | 138 checkedClasses = new Set<ClassElement>(); |
124 checkedTypedefs = new Set<TypedefElement>(); | 139 checkedTypedefs = new Set<TypedefElement>(); |
125 compiler.codegenWorld.isChecks.forEach((DartType t) { | 140 compiler.codegenWorld.isChecks.forEach((DartType t) { |
126 if (t is InterfaceType) { | 141 if (t is InterfaceType) { |
127 checkedClasses.add(t.element); | 142 checkedClasses.add(t.element); |
128 } else if (t is TypedefType) { | 143 } else if (t is TypedefType) { |
129 checkedTypedefs.add(t.element); | 144 checkedTypedefs.add(t.element); |
130 } | 145 } |
131 }); | 146 }); |
132 } | 147 } |
(...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1068 includeSuperMembers: false); | 1083 includeSuperMembers: false); |
1069 }); | 1084 }); |
1070 | 1085 |
1071 classElement.implementation.forEachMember( | 1086 classElement.implementation.forEachMember( |
1072 visitMember, | 1087 visitMember, |
1073 includeBackendMembers: true, | 1088 includeBackendMembers: true, |
1074 includeSuperMembers: false); | 1089 includeSuperMembers: false); |
1075 | 1090 |
1076 void generateIsTest(Element other) { | 1091 void generateIsTest(Element other) { |
1077 jsAst.Expression code; | 1092 jsAst.Expression code; |
1078 if (compiler.objectClass == other) return; | 1093 if (other == compiler.objectClass && other != classElement) return; |
ngeoffray
2013/02/26 14:11:37
Why did you add it back now? This deserves a comme
karlklose
2013/02/27 10:12:58
We need Object.$isObject. Comment added.
| |
1079 if (nativeEmitter.requiresNativeIsCheck(other)) { | 1094 if (nativeEmitter.requiresNativeIsCheck(other)) { |
1080 code = js.fun([], [js.return_(true)]); | 1095 code = js.fun([], [js.return_(true)]); |
1081 } else { | 1096 } else { |
1082 code = new jsAst.LiteralBool(true); | 1097 code = new jsAst.LiteralBool(true); |
1083 } | 1098 } |
1084 builder.addProperty(namer.operatorIs(other), code); | 1099 builder.addProperty(namer.operatorIs(other), code); |
1085 } | 1100 } |
1086 | 1101 |
1087 void generateSubstitution(Element other, {bool emitNull: false}) { | 1102 void generateSubstitution(Element other, {bool emitNull: false}) { |
1088 RuntimeTypeInformation rti = backend.rti; | 1103 RuntimeTypeInformation rti = backend.rti; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1129 String name = backend.namer.publicInstanceMethodNameByArity( | 1144 String name = backend.namer.publicInstanceMethodNameByArity( |
1130 const SourceString('=='), 1); | 1145 const SourceString('=='), 1); |
1131 Function kind = (classElement == backend.jsNullClass) | 1146 Function kind = (classElement == backend.jsNullClass) |
1132 ? js.equals | 1147 ? js.equals |
1133 : js.strictEquals; | 1148 : js.strictEquals; |
1134 builder.addProperty(name, js.fun(['receiver', 'a'], | 1149 builder.addProperty(name, js.fun(['receiver', 'a'], |
1135 js.block(js.return_(kind(js['receiver'], js['a']))))); | 1150 js.block(js.return_(kind(js['receiver'], js['a']))))); |
1136 } | 1151 } |
1137 } | 1152 } |
1138 | 1153 |
1139 void emitRuntimeClassesAndTests(CodeBuffer buffer) { | 1154 void emitRuntimeTypeSupport(CodeBuffer buffer) { |
1140 RuntimeTypeInformation rti = backend.rti; | 1155 RuntimeTypeInformation rti = backend.rti; |
1141 TypeChecks typeChecks = rti.getRequiredChecks(); | 1156 TypeChecks typeChecks = rti.getRequiredChecks(); |
1142 | 1157 |
1143 bool needsHolder(ClassElement cls) { | 1158 bool needsHolder(ClassElement cls) { |
ngeoffray
2013/02/26 14:11:37
Please add a comment on who needs a holder.
karlklose
2013/02/27 10:12:58
Done.
| |
1144 return !neededClasses.contains(cls) || cls.isNative() || | 1159 return !neededClasses.contains(cls) || cls.isNative(); |
1145 rti.isJsNative(cls); | |
1146 } | 1160 } |
1147 | 1161 |
1148 /** | 1162 /** |
1149 * Generates a holder object if it is needed. A holder is a JavaScript | 1163 * Generates a holder object if it is needed. A holder is a JavaScript |
1150 * object literal with a field [builtin$cls] that contains the name of the | 1164 * object literal with a field [builtin$cls] that contains the name of the |
1151 * class as a string (just like object constructors do). The is-checks for | 1165 * class as a string (just like object constructors do). The is-checks for |
1152 * the class are are added to the holder object later. | 1166 * the class are are added to the holder object later. |
1153 */ | 1167 */ |
1154 void maybeGenerateHolder(ClassElement cls) { | 1168 void maybeGenerateHolder(ClassElement cls) { |
1155 if (!needsHolder(cls)) return; | 1169 if (!needsHolder(cls)) return; |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1558 // substitutions for all checks and make emitSubstitution a NOP for the | 1572 // substitutions for all checks and make emitSubstitution a NOP for the |
1559 // rest of this function. | 1573 // rest of this function. |
1560 Set<ClassElement> emitted = new Set<ClassElement>(); | 1574 Set<ClassElement> emitted = new Set<ClassElement>(); |
1561 // TODO(karlklose): move the computation of these checks to | 1575 // TODO(karlklose): move the computation of these checks to |
1562 // RuntimeTypeInformation. | 1576 // RuntimeTypeInformation. |
1563 if (compiler.world.needsRti(cls)) { | 1577 if (compiler.world.needsRti(cls)) { |
1564 emitSubstitution(superclass, emitNull: true); | 1578 emitSubstitution(superclass, emitNull: true); |
1565 emitted.add(superclass); | 1579 emitted.add(superclass); |
1566 } | 1580 } |
1567 for (DartType supertype in cls.allSupertypes) { | 1581 for (DartType supertype in cls.allSupertypes) { |
1582 ClassElement superclass = supertype.element; | |
1583 if (classesUsingTypeVariableTests.contains(superclass)) { | |
1584 emitSubstitution(superclass, emitNull: true); | |
1585 emitted.add(superclass); | |
1586 } | |
1568 for (ClassElement check in checkedClasses) { | 1587 for (ClassElement check in checkedClasses) { |
1569 if (supertype.element == check && !emitted.contains(check)) { | 1588 if (supertype.element == check && !emitted.contains(check)) { |
1570 // Generate substitution. If no substitution is necessary, emit | 1589 // Generate substitution. If no substitution is necessary, emit |
1571 // [:null:] to overwrite a (possibly) existing substitution from the | 1590 // [:null:] to overwrite a (possibly) existing substitution from the |
1572 // super classes. | 1591 // super classes. |
1573 emitSubstitution(check, emitNull: true); | 1592 emitSubstitution(check, emitNull: true); |
1574 emitted.add(check); | 1593 emitted.add(check); |
1575 } | 1594 } |
1576 } | 1595 } |
1577 } | 1596 } |
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2575 ..addAll(buildFinishIsolateConstructor()) | 2594 ..addAll(buildFinishIsolateConstructor()) |
2576 ); | 2595 ); |
2577 jsAst.FunctionDeclaration decl = new jsAst.FunctionDeclaration( | 2596 jsAst.FunctionDeclaration decl = new jsAst.FunctionDeclaration( |
2578 new jsAst.VariableDeclaration('init'), fun); | 2597 new jsAst.VariableDeclaration('init'), fun); |
2579 buffer.add(jsAst.prettyPrint(decl, compiler).getText()); | 2598 buffer.add(jsAst.prettyPrint(decl, compiler).getText()); |
2580 } | 2599 } |
2581 | 2600 |
2582 String assembleProgram() { | 2601 String assembleProgram() { |
2583 measure(() { | 2602 measure(() { |
2584 computeNeededClasses(); | 2603 computeNeededClasses(); |
2585 | |
2586 mainBuffer.add(GENERATED_BY); | 2604 mainBuffer.add(GENERATED_BY); |
2587 if (!compiler.enableMinification) mainBuffer.add(HOOKS_API_USAGE); | 2605 if (!compiler.enableMinification) mainBuffer.add(HOOKS_API_USAGE); |
2588 mainBuffer.add('function ${namer.isolateName}()$_{}\n'); | 2606 mainBuffer.add('function ${namer.isolateName}()$_{}\n'); |
2589 mainBuffer.add('init()$N$n'); | 2607 mainBuffer.add('init()$N$n'); |
2590 // Shorten the code by using "$$" as temporary. | 2608 // Shorten the code by using "$$" as temporary. |
2591 classesCollector = r"$$"; | 2609 classesCollector = r"$$"; |
2592 mainBuffer.add('var $classesCollector$_=$_{}$N'); | 2610 mainBuffer.add('var $classesCollector$_=$_{}$N'); |
2593 // Shorten the code by using [namer.CURRENT_ISOLATE] as temporary. | 2611 // Shorten the code by using [namer.CURRENT_ISOLATE] as temporary. |
2594 isolateProperties = namer.CURRENT_ISOLATE; | 2612 isolateProperties = namer.CURRENT_ISOLATE; |
2595 mainBuffer.add( | 2613 mainBuffer.add( |
2596 'var $isolateProperties$_=$_$isolatePropertiesName$N'); | 2614 'var $isolateProperties$_=$_$isolatePropertiesName$N'); |
2597 emitClasses(mainBuffer); | 2615 emitClasses(mainBuffer); |
2598 mainBuffer.add(boundClosureBuffer); | 2616 mainBuffer.add(boundClosureBuffer); |
2599 // Clear the buffer, so that we can reuse it for the native classes. | 2617 // Clear the buffer, so that we can reuse it for the native classes. |
2600 boundClosureBuffer.clear(); | 2618 boundClosureBuffer.clear(); |
2601 emitStaticFunctions(mainBuffer); | 2619 emitStaticFunctions(mainBuffer); |
2602 emitStaticFunctionGetters(mainBuffer); | 2620 emitStaticFunctionGetters(mainBuffer); |
2603 // We need to finish the classes before we construct compile time | 2621 // We need to finish the classes before we construct compile time |
2604 // constants. | 2622 // constants. |
2605 emitFinishClassesInvocationIfNecessary(mainBuffer); | 2623 emitFinishClassesInvocationIfNecessary(mainBuffer); |
2606 emitRuntimeClassesAndTests(mainBuffer); | 2624 emitRuntimeTypeSupport(mainBuffer); |
2607 emitCompileTimeConstants(mainBuffer); | 2625 emitCompileTimeConstants(mainBuffer); |
2608 // Static field initializations require the classes and compile-time | 2626 // Static field initializations require the classes and compile-time |
2609 // constants to be set up. | 2627 // constants to be set up. |
2610 emitStaticNonFinalFieldInitializations(mainBuffer); | 2628 emitStaticNonFinalFieldInitializations(mainBuffer); |
2611 emitOneShotInterceptors(mainBuffer); | 2629 emitOneShotInterceptors(mainBuffer); |
2612 emitGetInterceptorMethods(mainBuffer); | 2630 emitGetInterceptorMethods(mainBuffer); |
2613 emitLazilyInitializedStaticFields(mainBuffer); | 2631 emitLazilyInitializedStaticFields(mainBuffer); |
2614 | 2632 |
2615 isolateProperties = isolatePropertiesName; | 2633 isolateProperties = isolatePropertiesName; |
2616 // The following code should not use the short-hand for the | 2634 // The following code should not use the short-hand for the |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2676 """; | 2694 """; |
2677 const String HOOKS_API_USAGE = """ | 2695 const String HOOKS_API_USAGE = """ |
2678 // The code supports the following hooks: | 2696 // The code supports the following hooks: |
2679 // dartPrint(message) - if this function is defined it is called | 2697 // dartPrint(message) - if this function is defined it is called |
2680 // instead of the Dart [print] method. | 2698 // instead of the Dart [print] method. |
2681 // dartMainRunner(main) - if this function is defined, the Dart [main] | 2699 // dartMainRunner(main) - if this function is defined, the Dart [main] |
2682 // method will not be invoked directly. | 2700 // method will not be invoked directly. |
2683 // Instead, a closure that will invoke [main] is | 2701 // Instead, a closure that will invoke [main] is |
2684 // passed to [dartMainRunner]. | 2702 // passed to [dartMainRunner]. |
2685 """; | 2703 """; |
OLD | NEW |