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

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

Issue 9193016: Add the arity to calls, and support noSuchMethodException. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 11 months 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 */
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 } 49 }
50 codeBlock = compiler.universe.generatedBailoutCode[member]; 50 codeBlock = compiler.universe.generatedBailoutCode[member];
51 if (codeBlock !== null) { 51 if (codeBlock !== null) {
52 String name = namer.getBailoutName(member); 52 String name = namer.getBailoutName(member);
53 buffer.add('$prototype.$name = $codeBlock;\n'); 53 buffer.add('$prototype.$name = $codeBlock;\n');
54 } 54 }
55 } else { 55 } else {
56 // TODO(ngeoffray): Have another class generate the code for the 56 // TODO(ngeoffray): Have another class generate the code for the
57 // fields. 57 // fields.
58 assert(member.kind === ElementKind.FIELD); 58 assert(member.kind === ElementKind.FIELD);
59 if (compiler.universe.invokedSetters.contains(member.name.stringValue)) { 59 if (compiler.universe.invokedSetters.contains(member.name)) {
60 String setterName = namer.setterName(member.name); 60 String setterName = namer.setterName(member.name);
61 buffer.add('$prototype.$setterName = function(v){\n' + 61 buffer.add('$prototype.$setterName = function(v){\n' +
62 ' this.${namer.getName(member)} = v;\n}\n'); 62 ' this.${namer.getName(member)} = v;\n}\n');
63 } 63 }
64 if (compiler.universe.invokedGetters.contains(member.name.stringValue)) { 64 if (compiler.universe.invokedGetters.contains(member.name)) {
65 String getterName = namer.getterName(member.name); 65 String getterName = namer.getterName(member.name);
66 buffer.add('$prototype.$getterName = function(){\n' + 66 buffer.add('$prototype.$getterName = function(){\n' +
67 ' return this.${namer.getName(member)};\n}\n'); 67 ' return this.${namer.getName(member)};\n}\n');
68 } 68 }
69 } 69 }
70 } 70 }
71 71
72 bool generateFieldInits(ClassElement classElement, 72 bool generateFieldInits(ClassElement classElement,
73 StringBuffer argumentsBuffer, 73 StringBuffer argumentsBuffer,
74 StringBuffer bodyBuffer) { 74 StringBuffer bodyBuffer) {
75 bool isFirst = true; 75 bool isFirst = true;
76 do { 76 do {
77 // TODO(floitsch): make sure there are no name clashes. 77 // TODO(floitsch): make sure there are no name clashes.
78 String className = namer.getName(classElement); 78 String className = namer.getName(classElement);
79 for (Element member in classElement.members) { 79 for (Element member in classElement.members) {
80 if (member.isInstanceMember() && member.kind == ElementKind.FIELD) { 80 if (member.isInstanceMember() && member.kind == ElementKind.FIELD) {
81 if (!isFirst) argumentsBuffer.add(', '); 81 if (!isFirst) argumentsBuffer.add(', ');
82 isFirst = false; 82 isFirst = false;
83 String memberName = namer.instanceName(member.name); 83 String memberName = namer.instanceFieldName(member.name);
84 argumentsBuffer.add('${className}_$memberName'); 84 argumentsBuffer.add('${className}_$memberName');
85 bodyBuffer.add(' this.$memberName = ${className}_$memberName;\n'); 85 bodyBuffer.add(' this.$memberName = ${className}_$memberName;\n');
86 } 86 }
87 } 87 }
88 classElement = classElement.superClass; 88 classElement = classElement.superClass;
89 } while(classElement !== null); 89 } while(classElement !== null);
90 } 90 }
91 91
92 void generateClass(ClassElement classElement, 92 void generateClass(ClassElement classElement,
93 StringBuffer buffer, 93 StringBuffer buffer,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 CompileTimeConstantHandler constants = compiler.compileTimeConstantHandler; 176 CompileTimeConstantHandler constants = compiler.compileTimeConstantHandler;
177 List<VariableElement> staticFinalFields = 177 List<VariableElement> staticFinalFields =
178 constants.getStaticFinalFieldsForEmission(); 178 constants.getStaticFinalFieldsForEmission();
179 for (VariableElement element in staticFinalFields) { 179 for (VariableElement element in staticFinalFields) {
180 buffer.add('${namer.isolatePropertyAccess(element)} = '); 180 buffer.add('${namer.isolatePropertyAccess(element)} = ');
181 constants.emitJsCodeForField(element, buffer); 181 constants.emitJsCodeForField(element, buffer);
182 buffer.add(';\n'); 182 buffer.add(';\n');
183 } 183 }
184 } 184 }
185 185
186 void emitNoSuchMethodCalls(StringBuffer buffer) {
187 // Do not generate no such method calls if there is no class.
188 if (!addedInheritFunction) return;
floitsch 2012/01/24 11:46:03 I assume you don't want to use instantiatedClasses
floitsch 2012/01/24 11:46:03 Add TODO that we don't need to generate these meth
ngeoffray 2012/01/24 12:09:39 Done.
ngeoffray 2012/01/24 12:09:39 No, I just didn't think of using it (addedInheritF
189
190 ClassElement objectClass =
191 compiler.universe.find(const SourceString('Object'));
192 String className = namer.isolatePropertyAccess(objectClass);
193 String prototype = '$className.prototype';
194 String noSuchMethodName =
195 namer.instanceMethodName(Compiler.NO_SUCH_METHOD, 2);
196
197 void generateMethod(String methodName, String jsName, int arity) {
198 buffer.add('$prototype.$jsName = function');
199 StringBuffer args = new StringBuffer();
200 for (int i = 0; i < arity; i++) {
201 if (i != 0) args.add(', ');
202 args.add('arg$i');
203 }
204 buffer.add(' ($args) {\n');
205 buffer.add(" return this.$noSuchMethodName('$methodName', [$args]);\n");
206 buffer.add('}\n');
207 }
208
209 compiler.universe.invokedNames.forEach((SourceString methodName,
210 Set<int> arities) {
211 if (objectClass.lookupLocalMember(methodName) === null) {
212 for (int arity in arities) {
213 String jsName = namer.instanceMethodName(methodName, arity);
214 generateMethod(methodName.stringValue, jsName, arity);
215 }
216 }
217 });
218
219 compiler.universe.invokedGetters.forEach((SourceString getterName) {
220 String jsName = namer.getterName(getterName);
221 generateMethod('get $getterName', jsName, 0);
222 });
223
224 compiler.universe.invokedSetters.forEach((SourceString setterName) {
225 String jsName = namer.setterName(setterName);
226 generateMethod('set $setterName', jsName, 1);
227 });
228 }
229
186 String assembleProgram() { 230 String assembleProgram() {
187 measure(() { 231 measure(() {
188 StringBuffer buffer = new StringBuffer(); 232 StringBuffer buffer = new StringBuffer();
189 buffer.add('function ${namer.ISOLATE}() {'); 233 buffer.add('function ${namer.ISOLATE}() {');
190 emitStaticNonFinalFieldInitializations(buffer); 234 emitStaticNonFinalFieldInitializations(buffer);
191 buffer.add('}\n\n'); 235 buffer.add('}\n\n');
192 emitClasses(buffer); 236 emitClasses(buffer);
237 emitNoSuchMethodCalls(buffer);
193 emitStaticFunctions(buffer); 238 emitStaticFunctions(buffer);
194 emitStaticFinalFieldInitializations(buffer); 239 emitStaticFinalFieldInitializations(buffer);
195 buffer.add('var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n'); 240 buffer.add('var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n');
196 buffer.add('${namer.CURRENT_ISOLATE}.main();\n'); 241 Element main = compiler.universe.find(Compiler.MAIN);
242 buffer.add('${namer.isolateAccess(main)}();\n');
197 compiler.assembledCode = buffer.toString(); 243 compiler.assembledCode = buffer.toString();
198 }); 244 });
199 return compiler.assembledCode; 245 return compiler.assembledCode;
200 } 246 }
201 } 247 }
OLDNEW
« no previous file with comments | « frog/leg/compiler.dart ('k') | frog/leg/lib/core.dart » ('j') | frog/leg/lib/core.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698