Chromium Code Reviews| Index: frog/leg/emitter.dart |
| =================================================================== |
| --- frog/leg/emitter.dart (revision 3778) |
| +++ frog/leg/emitter.dart (working copy) |
| @@ -41,26 +41,58 @@ |
| String prototype, |
| StringBuffer buffer, |
| Invocation invocation) { |
| - // TODO(ngeoffray): also support invocation with names. |
| + FunctionParameters parameters = member.computeParameters(compiler); |
| + int positionalArgumentCount = invocation.positionalArgumentCount; |
| + if (positionalArgumentCount == parameters.parameterCount) return; |
| + List<SourceString> names = invocation.orderedNamedArguments; |
| + assert(names.length == invocation.namedArguments.length); |
| + |
| String invocationName = |
| - namer.instanceMethodName(member.name, invocation.argumentCount); |
| - int allParameters = member.parameterCount(compiler); |
| - int missingParameters = allParameters - invocation.argumentCount; |
| - if (missingParameters == 0) return; |
| - assert(missingParameters > 0); |
| + namer.instanceMethodInvocationName(member.name, invocation); |
| buffer.add('$prototype.$invocationName = function('); |
| - StringBuffer parameters = new StringBuffer(); |
| - for (int i = 0; i < invocation.argumentCount; i++) { |
| - if (i != 0) parameters.add(', '); |
| - parameters.add('param$i'); |
| + |
| + // 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.
|
| + StringBuffer parametersBuffer = new StringBuffer(); |
| + // The missing and named arguments that will be passed to the real |
| + // method. |
| + StringBuffer missingAndNamedArgumentsBuffer = new StringBuffer(); |
| + StringBuffer positionalArgumentsBuffer = new StringBuffer(); |
| + |
| + int count = 0; |
| + parameters.forEachParameter((Element element) { |
| + if (count < positionalArgumentCount) { |
| + if (count != 0) parametersBuffer.add(', '); |
| + parametersBuffer.add('${element.name}'); |
| + positionalArgumentsBuffer.add('${element.name}'); |
| + } else { |
| + if (count != positionalArgumentCount || positionalArgumentCount != 0) { |
| + missingAndNamedArgumentsBuffer.add(', '); |
| + } |
| + int index = names.indexOf(element.name); |
| + if (index != -1) { |
| + // The order of the named arguments is not the same as the |
| + // one in the real method (which is in Dart source order). |
| + // Therefore, we don't add the argument to the |
| + // [parametersBuffer] just yet. |
| + missingAndNamedArgumentsBuffer.add('${element.name}'); |
| + } else { |
| + // TODO(ngeoffray): Get the default value. |
| + missingAndNamedArgumentsBuffer.add('(void 0)'); |
| + } |
| + } |
| + count++; |
| + }); |
| + |
| + // Add the named arguments to the parametersBuffer. |
| + for (SourceString name in names) { |
| + if (positionalArgumentCount != 0) parametersBuffer.add(', '); |
| + parametersBuffer.add(name); |
| } |
| - buffer.add('$parameters) {\n'); |
| - buffer.add(' this.${namer.getName(member)}($parameters'); |
| - for (int i = 0; i < missingParameters; i++) { |
| - if (i != 0 || invocation.argumentCount != 0) buffer.add(', '); |
| - buffer.add('(void 0)'); |
| - } |
| - buffer.add(')\n}\n'); |
| + |
| + buffer.add('$parametersBuffer) {\n'); |
| + buffer.add(' return this.${namer.getName(member)}'); |
| + buffer.add('($positionalArgumentsBuffer$missingAndNamedArgumentsBuffer)'); |
| + buffer.add('\n}\n'); |
| } |
| void addParameterStubs(FunctionElement member, |