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

Unified Diff: frog/leg/emitter.dart

Issue 9316026: Support named arguments for dynamic calls. (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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | frog/leg/namer.dart » ('j') | frog/leg/ssa/builder.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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,
« no previous file with comments | « no previous file | frog/leg/namer.dart » ('j') | frog/leg/ssa/builder.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698