Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 23 matching lines...) Expand all Loading... | |
| 34 addedInheritFunction = true; | 34 addedInheritFunction = true; |
| 35 buffer.add('$inheritsName = '); | 35 buffer.add('$inheritsName = '); |
| 36 buffer.add(INHERIT_FUNCTION); | 36 buffer.add(INHERIT_FUNCTION); |
| 37 buffer.add(';\n'); | 37 buffer.add(';\n'); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void addParameterStub(FunctionElement member, | 40 void addParameterStub(FunctionElement member, |
| 41 String prototype, | 41 String prototype, |
| 42 StringBuffer buffer, | 42 StringBuffer buffer, |
| 43 Invocation invocation) { | 43 Invocation invocation) { |
| 44 // TODO(ngeoffray): also support invocation with names. | 44 FunctionParameters parameters = member.computeParameters(compiler); |
| 45 int positionalArgumentCount = invocation.positionalArgumentCount; | |
| 46 if (positionalArgumentCount == parameters.parameterCount) return; | |
| 47 List<SourceString> names = invocation.orderedNamedArguments; | |
| 48 assert(names.length == invocation.namedArguments.length); | |
| 49 | |
| 45 String invocationName = | 50 String invocationName = |
| 46 namer.instanceMethodName(member.name, invocation.argumentCount); | 51 namer.instanceMethodInvocationName(member.name, invocation); |
| 47 int allParameters = member.parameterCount(compiler); | |
| 48 int missingParameters = allParameters - invocation.argumentCount; | |
| 49 if (missingParameters == 0) return; | |
| 50 assert(missingParameters > 0); | |
| 51 buffer.add('$prototype.$invocationName = function('); | 52 buffer.add('$prototype.$invocationName = function('); |
| 52 StringBuffer parameters = new StringBuffer(); | 53 |
| 53 for (int i = 0; i < invocation.argumentCount; i++) { | 54 // The parameters that this stub takes. |
|
kasperl
2012/02/01 10:21:21
Maybe you could write a comment with a few example
ngeoffray
2012/02/01 11:44:15
Done.
| |
| 54 if (i != 0) parameters.add(', '); | 55 StringBuffer parametersBuffer = new StringBuffer(); |
| 55 parameters.add('param$i'); | 56 // The missing and named arguments that will be passed to the real |
| 57 // method. | |
| 58 StringBuffer missingAndNamedArgumentsBuffer = new StringBuffer(); | |
| 59 StringBuffer positionalArgumentsBuffer = new StringBuffer(); | |
| 60 | |
| 61 int count = 0; | |
| 62 parameters.forEachParameter((Element element) { | |
| 63 if (count < positionalArgumentCount) { | |
| 64 if (count != 0) parametersBuffer.add(', '); | |
| 65 parametersBuffer.add('${element.name}'); | |
| 66 positionalArgumentsBuffer.add('${element.name}'); | |
| 67 } else { | |
| 68 if (count != positionalArgumentCount || positionalArgumentCount != 0) { | |
| 69 missingAndNamedArgumentsBuffer.add(', '); | |
| 70 } | |
| 71 int index = names.indexOf(element.name); | |
| 72 if (index != -1) { | |
| 73 // The order of the named arguments is not the same as the | |
| 74 // one in the real method (which is in Dart source order). | |
| 75 // Therefore, we don't add the argument to the | |
| 76 // [parametersBuffer] just yet. | |
| 77 missingAndNamedArgumentsBuffer.add('${element.name}'); | |
| 78 } else { | |
| 79 // TODO(ngeoffray): Get the default value. | |
| 80 missingAndNamedArgumentsBuffer.add('(void 0)'); | |
| 81 } | |
| 82 } | |
| 83 count++; | |
| 84 }); | |
| 85 | |
| 86 // Add the named arguments to the parametersBuffer. | |
| 87 for (SourceString name in names) { | |
| 88 if (positionalArgumentCount != 0) parametersBuffer.add(', '); | |
| 89 parametersBuffer.add(name); | |
| 56 } | 90 } |
| 57 buffer.add('$parameters) {\n'); | 91 |
| 58 buffer.add(' this.${namer.getName(member)}($parameters'); | 92 buffer.add('$parametersBuffer) {\n'); |
| 59 for (int i = 0; i < missingParameters; i++) { | 93 buffer.add(' return this.${namer.getName(member)}'); |
| 60 if (i != 0 || invocation.argumentCount != 0) buffer.add(', '); | 94 buffer.add('($positionalArgumentsBuffer$missingAndNamedArgumentsBuffer)'); |
| 61 buffer.add('(void 0)'); | 95 buffer.add('\n}\n'); |
| 62 } | |
| 63 buffer.add(')\n}\n'); | |
| 64 } | 96 } |
| 65 | 97 |
| 66 void addParameterStubs(FunctionElement member, | 98 void addParameterStubs(FunctionElement member, |
| 67 String prototype, | 99 String prototype, |
| 68 StringBuffer buffer) { | 100 StringBuffer buffer) { |
| 69 Set<Invocation> invocations = compiler.universe.invokedNames[member.name]; | 101 Set<Invocation> invocations = compiler.universe.invokedNames[member.name]; |
| 70 if (invocations == null) return; | 102 if (invocations == null) return; |
| 71 for (Invocation invocation in invocations) { | 103 for (Invocation invocation in invocations) { |
| 72 if (!invocation.applies(compiler, member)) continue; | 104 if (!invocation.applies(compiler, member)) continue; |
| 73 addParameterStub(member, prototype, buffer, invocation); | 105 addParameterStub(member, prototype, buffer, invocation); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 emitStaticFunctions(buffer); | 320 emitStaticFunctions(buffer); |
| 289 emitStaticFinalFieldInitializations(buffer); | 321 emitStaticFinalFieldInitializations(buffer); |
| 290 buffer.add('var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n'); | 322 buffer.add('var ${namer.CURRENT_ISOLATE} = new ${namer.ISOLATE}();\n'); |
| 291 Element main = compiler.universe.find(Compiler.MAIN); | 323 Element main = compiler.universe.find(Compiler.MAIN); |
| 292 buffer.add('${namer.isolateAccess(main)}();\n'); | 324 buffer.add('${namer.isolateAccess(main)}();\n'); |
| 293 compiler.assembledCode = buffer.toString(); | 325 compiler.assembledCode = buffer.toString(); |
| 294 }); | 326 }); |
| 295 return compiler.assembledCode; | 327 return compiler.assembledCode; |
| 296 } | 328 } |
| 297 } | 329 } |
| OLD | NEW |