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

Side by Side Diff: frog/leg/emitter.dart

Issue 8888010: First instantiations of objects. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: small refactorings. Created 9 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 /** 5 /**
6 * Generates the code for all used classes in the program. Static fields (even 6 * Generates the code for all used classes in the program. Static fields (even
7 * in classes) are ignored, since they can be treated as non-class elements. 7 * in classes) are ignored, since they can be treated as non-class elements.
8 * 8 *
9 * The code for the containing (used) methods must exist in the [:universe:]. 9 * The code for the containing (used) methods must exist in the [:universe:].
10 */ 10 */
11 class CodeEmitterTask extends CompilerTask { 11 class CodeEmitterTask extends CompilerTask {
12 static final String INHERIT_FUNCTION = ''' 12 static final String INHERIT_FUNCTION = '''
13 function(child, parent) { 13 function(child, parent) {
14 if (child.prototype.__proto__) { 14 if (child.prototype.__proto__) {
15 child.prototype.__proto__ = parent.prototype; 15 child.prototype.__proto__ = parent.prototype;
16 } else { 16 } else {
17 function tmp() {}; 17 function tmp() {};
18 tmp.prototype = parent.prototype; 18 tmp.prototype = parent.prototype;
19 child.prototype = new tmp(); 19 child.prototype = new tmp();
20 child.prototype.constructor = child; 20 child.prototype.constructor = child;
21 } 21 }
22 }'''; 22 }''';
23 23
24 bool addedInheritFunction = false; 24 bool addedInheritFunction = false;
25 final Namer namer;
25 26
26 CodeEmitterTask(Compiler compiler) : super(compiler); 27 CodeEmitterTask(Compiler compiler) : namer = compiler.namer, super(compiler);
27 String get name() => 'CodeEmitter'; 28 String get name() => 'CodeEmitter';
28 29
29 String get inheritsName() => '${compiler.namer.isolate}.\$inherits'; 30 String get inheritsName() => '${compiler.namer.isolate}.\$inherits';
30 31
31 void addInheritFunctionIfNecessary(StringBuffer buffer) { 32 void addInheritFunctionIfNecessary(StringBuffer buffer) {
32 if (addedInheritFunction) return; 33 if (addedInheritFunction) return;
33 addedInheritFunction = true; 34 addedInheritFunction = true;
34 buffer.add('$inheritsName = '); 35 buffer.add('$inheritsName = ');
35 buffer.add(INHERIT_FUNCTION); 36 buffer.add(INHERIT_FUNCTION);
36 buffer.add(';\n'); 37 buffer.add(';\n');
37 } 38 }
38 39
40 void addInstanceMember(Element member,
41 String prototype,
42 StringBuffer buffer) {
43 assert(member is FunctionElement);
44 assert(!member.isStatic());
45 String codeBlock = compiler.universe.generatedCode[member];
46 assert(codeBlock !== null);
47 buffer.add('$prototype.${namer.instanceName(member)} = $codeBlock;\n');
48 }
49
39 void generateClass(ClassElement classElement, 50 void generateClass(ClassElement classElement,
40 StringBuffer buffer, 51 StringBuffer buffer,
41 Set<ClassElement> seenClasses) { 52 Set<ClassElement> seenClasses) {
42 Namer namer = compiler.namer;
43 if (seenClasses.contains(classElement)) return; 53 if (seenClasses.contains(classElement)) return;
44 seenClasses.add(classElement); 54 seenClasses.add(classElement);
45 Type supertype = classElement.supertype; 55 Type supertype = classElement.supertype;
46 ClassElement superClass = (supertype === null ? null : supertype.element); 56 ClassElement superClass = (supertype === null ? null : supertype.element);
47 if (superClass !== null) { 57 if (superClass !== null) {
48 generateClass(superClass, buffer, seenClasses); 58 generateClass(superClass, buffer, seenClasses);
49 } 59 }
50 60
51 String className = namer.isolatePropertyAccess(classElement); 61 String className = namer.isolatePropertyAccess(classElement);
52 buffer.add('$className = function() {};\n'); 62 buffer.add('$className = function() {};\n');
53 if (superClass !== null) { 63 if (superClass !== null) {
54 addInheritFunctionIfNecessary(buffer); 64 addInheritFunctionIfNecessary(buffer);
55 String superName = namer.isolatePropertyAccess(superClass); 65 String superName = namer.isolatePropertyAccess(superClass);
56 buffer.add('${inheritsName}($className, $superName);\n'); 66 buffer.add('${inheritsName}($className, $superName);\n');
57 } 67 }
58 String prototype = '$className.prototype'; 68 String prototype = '$className.prototype';
59 // TODO(floitsch): run through classElement.members() instead of []. 69
60 for (Element member in []) { 70 for (Element member in classElement.members) {
61 if (member.kind !== ElementKind.FUNCTION) continue; 71 if (!member.isStatic()) addInstanceMember(member, prototype, buffer);
62 assert(member is FunctionElement); 72 }
63 // TODO(floitsch): if (element.isStatic()) continue; 73 for (Element member in classElement.backendMembers) {
64 String codeBlock = compiler.universe.generatedCode[member]; 74 if (!member.isStatic()) addInstanceMember(member, prototype, buffer);
65 assert(codeBlock !== null);
66 buffer.add('$prototype.${namer.methodName(member)} = $codeBlock;\n');
67 } 75 }
68 76
69 Element synthesized = classElement.synthesizedConstructor; 77 Element synthesized = classElement.synthesizedConstructor;
70 if (synthesized !== null) { 78 if (synthesized !== null) {
71 String codeBlock = compiler.universe.generatedCode[synthesized]; 79 String codeBlock = compiler.universe.generatedCode[synthesized];
72 assert(codeBlock !== null); 80 assert(codeBlock !== null);
73 buffer.add('$prototype.${synthesized.name} = $codeBlock;\n'); 81 buffer.add('${namer.isolatePropertyAccess(synthesized)} = $codeBlock;\n');
74 } 82 }
75 } 83 }
76 84
77 String compileClasses(StringBuffer buffer) { 85 String compileClasses(StringBuffer buffer) {
78 Set seenClasses = new Set<ClassElement>(); 86 Set seenClasses = new Set<ClassElement>();
79 for (ClassElement element in compiler.universe.instantiatedClasses) { 87 for (ClassElement element in compiler.universe.instantiatedClasses) {
80 generateClass(element, buffer, seenClasses); 88 generateClass(element, buffer, seenClasses);
81 } 89 }
82 return buffer.toString(); 90 return buffer.toString();
83 } 91 }
84 92
85 String assembleProgram() { 93 String assembleProgram() {
86 measure(() { 94 measure(() {
87 Namer namer = compiler.namer;
88 StringBuffer buffer = new StringBuffer(); 95 StringBuffer buffer = new StringBuffer();
89 buffer.add('function ${namer.isolate}() {};\n\n'); 96 buffer.add('function ${namer.isolate}() {};\n\n');
90 compileClasses(buffer); 97 compileClasses(buffer);
91 Map<Element, String> generatedCode = compiler.universe.generatedCode; 98 Map<Element, String> generatedCode = compiler.universe.generatedCode;
92 generatedCode.forEach((Element element, String codeBlock) { 99 generatedCode.forEach((Element element, String codeBlock) {
93 if (element.isStatic()) { 100 if (element.isStatic()) {
94 buffer.add('${namer.isolatePropertyAccess(element)} = '); 101 buffer.add('${namer.isolatePropertyAccess(element)} = ');
95 buffer.add(codeBlock); 102 buffer.add(codeBlock);
96 buffer.add(';\n\n'); 103 buffer.add(';\n\n');
97 } 104 }
98 }); 105 });
99 buffer.add('var ${namer.currentIsolate} = new ${namer.isolate}();\n'); 106 buffer.add('var ${namer.currentIsolate} = new ${namer.isolate}();\n');
100 buffer.add('${namer.currentIsolate}.main();\n'); 107 buffer.add('${namer.currentIsolate}.main();\n');
101 compiler.assembledCode = buffer.toString(); 108 compiler.assembledCode = buffer.toString();
102 }); 109 });
103 return compiler.assembledCode; 110 return compiler.assembledCode;
104 } 111 }
105 } 112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698