OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of js_backend; | 5 part of js_backend; |
6 | 6 |
7 /** | 7 /** |
8 * A function element that represents a closure call. The signature is copied | 8 * A function element that represents a closure call. The signature is copied |
9 * from the given element. | 9 * from the given element. |
10 */ | 10 */ |
(...skipping 1164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1175 // if they share methods with the same signature. Currently we do this only | 1175 // if they share methods with the same signature. Currently we do this only |
1176 // if there are no optional parameters. Closures with optional parameters | 1176 // if there are no optional parameters. Closures with optional parameters |
1177 // are more difficult to canonicalize because they would need to have the | 1177 // are more difficult to canonicalize because they would need to have the |
1178 // same default values. | 1178 // same default values. |
1179 | 1179 |
1180 bool hasOptionalParameters = member.optionalParameterCount(compiler) != 0; | 1180 bool hasOptionalParameters = member.optionalParameterCount(compiler) != 0; |
1181 int parameterCount = member.parameterCount(compiler); | 1181 int parameterCount = member.parameterCount(compiler); |
1182 | 1182 |
1183 Map<int, String> cache; | 1183 Map<int, String> cache; |
1184 String extraArg; | 1184 String extraArg; |
1185 String extraArgWithThis; | |
1186 String extraArgWithoutComma; | 1185 String extraArgWithoutComma; |
1187 // Methods on foreign classes take an extra parameter, which is | 1186 // Methods on foreign classes take an extra parameter, which is |
1188 // the actual receiver of the call. | 1187 // the actual receiver of the call. |
1189 JavaScriptBackend backend = compiler.backend; | 1188 JavaScriptBackend backend = compiler.backend; |
1190 if (backend.isInterceptorClass(member.getEnclosingClass())) { | 1189 if (backend.isInterceptorClass(member.getEnclosingClass())) { |
1191 cache = interceptorClosureCache; | 1190 cache = interceptorClosureCache; |
1192 extraArg = 'receiver, '; | 1191 extraArg = 'receiver, '; |
1193 extraArgWithThis = 'this.receiver, '; | |
1194 extraArgWithoutComma = 'receiver'; | 1192 extraArgWithoutComma = 'receiver'; |
1195 } else { | 1193 } else { |
1196 cache = boundClosureCache; | 1194 cache = boundClosureCache; |
1197 extraArg = ''; | 1195 extraArg = ''; |
1198 extraArgWithoutComma = ''; | 1196 extraArgWithoutComma = ''; |
1199 extraArgWithThis = ''; | |
1200 } | 1197 } |
1201 | 1198 |
1202 String closureClass = | 1199 String closureClass = |
1203 hasOptionalParameters ? null : cache[parameterCount]; | 1200 hasOptionalParameters ? null : cache[parameterCount]; |
1204 if (closureClass == null) { | 1201 if (closureClass == null) { |
1205 // Either the class was not cached yet, or there are optional parameters. | 1202 // Either the class was not cached yet, or there are optional parameters. |
1206 // Create a new closure class. | 1203 // Create a new closure class. |
1207 SourceString name = const SourceString("BoundClosure"); | 1204 SourceString name = const SourceString("BoundClosure"); |
1208 ClassElement closureClassElement = new ClosureClassElement( | 1205 ClassElement closureClassElement = new ClosureClassElement( |
1209 name, compiler, member, member.getCompilationUnit()); | 1206 name, compiler, member, member.getCompilationUnit()); |
(...skipping 13 matching lines...) Expand all Loading... | |
1223 new ClosureInvocationElement(namer.CLOSURE_INVOCATION_NAME, member); | 1220 new ClosureInvocationElement(namer.CLOSURE_INVOCATION_NAME, member); |
1224 | 1221 |
1225 String invocationName = namer.instanceMethodName(callElement); | 1222 String invocationName = namer.instanceMethodName(callElement); |
1226 List<String> arguments = new List<String>(parameterCount); | 1223 List<String> arguments = new List<String>(parameterCount); |
1227 for (int i = 0; i < parameterCount; i++) { | 1224 for (int i = 0; i < parameterCount; i++) { |
1228 arguments[i] = "p$i"; | 1225 arguments[i] = "p$i"; |
1229 } | 1226 } |
1230 String joinedArgs = Strings.join(arguments, ", "); | 1227 String joinedArgs = Strings.join(arguments, ", "); |
1231 boundClosureBuffer.add( | 1228 boundClosureBuffer.add( |
1232 "$invocationName: function($joinedArgs) {"); | 1229 "$invocationName: function($joinedArgs) {"); |
1233 boundClosureBuffer.add( | 1230 String callArgs = extraArg.isEmpty |
1234 " return this.self[this.target]($extraArgWithThis$joinedArgs);"); | 1231 ? joinedArgs |
1232 : joinedArgs.isEmpty | |
1233 ? 'this.$extraArgWithoutComma' | |
1234 : 'this.$extraArg$joinedArgs'; | |
Johnni Winther
2012/11/20 09:26:20
Why the 'this.' prefix?
ngeoffray
2012/11/20 10:30:50
Because you're using a field of the bound closure,
| |
1235 boundClosureBuffer.add(" return this.self[this.target]($callArgs);"); | |
Johnni Winther
2012/11/20 09:26:20
If extraArgWithoutComma is '' then callArgs become
ngeoffray
2012/11/20 10:30:50
If extraArgWithoutComma is '', then extrArg is ''.
karlklose
2012/11/20 10:31:00
I don't think this can happen, because extraArgWit
ngeoffray
2012/11/20 10:36:21
I added one flag: hasExtraArgument. What other fla
karlklose
2012/11/20 14:06:23
I thought about isInterceptor and hasArguments, bu
| |
1235 boundClosureBuffer.add(" }"); | 1236 boundClosureBuffer.add(" }"); |
1236 addParameterStubs(callElement, (String stubName, CodeBuffer memberValue) { | 1237 addParameterStubs(callElement, (String stubName, CodeBuffer memberValue) { |
1237 boundClosureBuffer.add(',\n $stubName: $memberValue'); | 1238 boundClosureBuffer.add(',\n $stubName: $memberValue'); |
1238 }); | 1239 }); |
1239 boundClosureBuffer.add("\n};\n"); | 1240 boundClosureBuffer.add("\n};\n"); |
1240 | 1241 |
1241 closureClass = namer.isolateAccess(closureClassElement); | 1242 closureClass = namer.isolateAccess(closureClassElement); |
1242 | 1243 |
1243 // Cache it. | 1244 // Cache it. |
1244 if (!hasOptionalParameters) { | 1245 if (!hasOptionalParameters) { |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1731 const String HOOKS_API_USAGE = """ | 1732 const String HOOKS_API_USAGE = """ |
1732 // Generated by dart2js, the Dart to JavaScript compiler. | 1733 // Generated by dart2js, the Dart to JavaScript compiler. |
1733 // The code supports the following hooks: | 1734 // The code supports the following hooks: |
1734 // dartPrint(message) - if this function is defined it is called | 1735 // dartPrint(message) - if this function is defined it is called |
1735 // instead of the Dart [print] method. | 1736 // instead of the Dart [print] method. |
1736 // dartMainRunner(main) - if this function is defined, the Dart [main] | 1737 // dartMainRunner(main) - if this function is defined, the Dart [main] |
1737 // method will not be invoked directly. | 1738 // method will not be invoked directly. |
1738 // Instead, a closure that will invoke [main] is | 1739 // Instead, a closure that will invoke [main] is |
1739 // passed to [dartMainRunner]. | 1740 // passed to [dartMainRunner]. |
1740 """; | 1741 """; |
OLD | NEW |