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

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

Issue 8893001: Second try for instantiation of objects. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Clean up even more. 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
« no previous file with comments | « frog/leg/elements/elements.dart ('k') | frog/leg/namer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.isInstanceMember());
45 String codeBlock = compiler.universe.generatedCode[member];
46 if (codeBlock !== null) {
47 buffer.add('$prototype.${namer.getName(member)} = $codeBlock;\n');
48 }
49 }
50
39 void generateClass(ClassElement classElement, 51 void generateClass(ClassElement classElement,
40 StringBuffer buffer, 52 StringBuffer buffer,
41 Set<ClassElement> seenClasses) { 53 Set<ClassElement> seenClasses) {
42 Namer namer = compiler.namer;
43 if (seenClasses.contains(classElement)) return; 54 if (seenClasses.contains(classElement)) return;
44 seenClasses.add(classElement); 55 seenClasses.add(classElement);
45 Type supertype = classElement.supertype; 56 Type supertype = classElement.supertype;
46 ClassElement superClass = (supertype === null ? null : supertype.element); 57 ClassElement superClass = (supertype === null ? null : supertype.element);
47 if (superClass !== null) { 58 if (superClass !== null) {
48 generateClass(superClass, buffer, seenClasses); 59 generateClass(superClass, buffer, seenClasses);
49 } 60 }
50 61
51 String className = namer.isolatePropertyAccess(classElement); 62 String className = namer.isolatePropertyAccess(classElement);
52 buffer.add('$className = function() {};\n'); 63 buffer.add('$className = function() {};\n');
53 if (superClass !== null) { 64 if (superClass !== null) {
54 addInheritFunctionIfNecessary(buffer); 65 addInheritFunctionIfNecessary(buffer);
55 String superName = namer.isolatePropertyAccess(superClass); 66 String superName = namer.isolatePropertyAccess(superClass);
56 buffer.add('${inheritsName}($className, $superName);\n'); 67 buffer.add('${inheritsName}($className, $superName);\n');
57 } 68 }
58 String prototype = '$className.prototype'; 69 String prototype = '$className.prototype';
59 // TODO(floitsch): run through classElement.members() instead of []. 70
60 for (Element member in []) { 71 for (Element member in classElement.members) {
61 if (member.kind !== ElementKind.FUNCTION) continue; 72 if (member.isInstanceMember()) {
62 assert(member is FunctionElement); 73 addInstanceMember(member, prototype, buffer);
63 // TODO(floitsch): if (element.isStatic()) continue; 74 }
64 String codeBlock = compiler.universe.generatedCode[member]; 75 }
65 assert(codeBlock !== null); 76 for (Element member in classElement.backendMembers) {
66 buffer.add('$prototype.${namer.methodName(member)} = $codeBlock;\n'); 77 if (member.isInstanceMember()) {
78 addInstanceMember(member, prototype, buffer);
79 }
67 } 80 }
68 81
69 Element synthesized = classElement.synthesizedConstructor; 82 Element synthesized = classElement.synthesizedConstructor;
70 if (synthesized !== null) { 83 if (synthesized !== null) {
71 String codeBlock = compiler.universe.generatedCode[synthesized]; 84 String codeBlock = compiler.universe.generatedCode[synthesized];
72 assert(codeBlock !== null); 85 assert(codeBlock !== null);
73 buffer.add('$prototype.${synthesized.name} = $codeBlock;\n'); 86 buffer.add('${namer.isolatePropertyAccess(synthesized)} = $codeBlock;\n');
74 } 87 }
75 } 88 }
76 89
77 String compileClasses(StringBuffer buffer) { 90 String compileClasses(StringBuffer buffer) {
78 Set seenClasses = new Set<ClassElement>(); 91 Set seenClasses = new Set<ClassElement>();
79 for (ClassElement element in compiler.universe.instantiatedClasses) { 92 for (ClassElement element in compiler.universe.instantiatedClasses) {
80 generateClass(element, buffer, seenClasses); 93 generateClass(element, buffer, seenClasses);
81 } 94 }
82 return buffer.toString(); 95 return buffer.toString();
83 } 96 }
84 97
85 String assembleProgram() { 98 String assembleProgram() {
86 measure(() { 99 measure(() {
87 Namer namer = compiler.namer;
88 StringBuffer buffer = new StringBuffer(); 100 StringBuffer buffer = new StringBuffer();
89 buffer.add('function ${namer.isolate}() {};\n\n'); 101 buffer.add('function ${namer.isolate}() {};\n\n');
90 compileClasses(buffer); 102 compileClasses(buffer);
91 Map<Element, String> generatedCode = compiler.universe.generatedCode; 103 Map<Element, String> generatedCode = compiler.universe.generatedCode;
92 generatedCode.forEach((Element element, String codeBlock) { 104 generatedCode.forEach((Element element, String codeBlock) {
93 if (element.isStatic()) { 105 if (!element.isInstanceMember()) {
94 buffer.add('${namer.isolatePropertyAccess(element)} = '); 106 buffer.add('${namer.isolatePropertyAccess(element)} = ');
95 buffer.add(codeBlock); 107 buffer.add(codeBlock);
96 buffer.add(';\n\n'); 108 buffer.add(';\n\n');
97 } 109 }
98 }); 110 });
99 buffer.add('var ${namer.currentIsolate} = new ${namer.isolate}();\n'); 111 buffer.add('var ${namer.currentIsolate} = new ${namer.isolate}();\n');
100 buffer.add('${namer.currentIsolate}.main();\n'); 112 buffer.add('${namer.currentIsolate}.main();\n');
101 compiler.assembledCode = buffer.toString(); 113 compiler.assembledCode = buffer.toString();
102 }); 114 });
103 return compiler.assembledCode; 115 return compiler.assembledCode;
104 } 116 }
105 } 117 }
OLDNEW
« no previous file with comments | « frog/leg/elements/elements.dart ('k') | frog/leg/namer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698