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; |
| 1186 bool hasExtraArgument = false; |
1187 // Methods on foreign classes take an extra parameter, which is | 1187 // Methods on foreign classes take an extra parameter, which is |
1188 // the actual receiver of the call. | 1188 // the actual receiver of the call. |
1189 JavaScriptBackend backend = compiler.backend; | 1189 JavaScriptBackend backend = compiler.backend; |
1190 if (backend.isInterceptorClass(member.getEnclosingClass())) { | 1190 if (backend.isInterceptorClass(member.getEnclosingClass())) { |
| 1191 hasExtraArgument = true; |
1191 cache = interceptorClosureCache; | 1192 cache = interceptorClosureCache; |
1192 extraArg = 'receiver, '; | 1193 extraArg = 'receiver, '; |
1193 extraArgWithThis = 'this.receiver, '; | |
1194 extraArgWithoutComma = 'receiver'; | 1194 extraArgWithoutComma = 'receiver'; |
1195 } else { | 1195 } else { |
1196 cache = boundClosureCache; | 1196 cache = boundClosureCache; |
1197 extraArg = ''; | 1197 extraArg = ''; |
1198 extraArgWithoutComma = ''; | 1198 extraArgWithoutComma = ''; |
1199 extraArgWithThis = ''; | |
1200 } | 1199 } |
1201 | 1200 |
1202 String closureClass = | 1201 String closureClass = |
1203 hasOptionalParameters ? null : cache[parameterCount]; | 1202 hasOptionalParameters ? null : cache[parameterCount]; |
1204 if (closureClass == null) { | 1203 if (closureClass == null) { |
1205 // Either the class was not cached yet, or there are optional parameters. | 1204 // Either the class was not cached yet, or there are optional parameters. |
1206 // Create a new closure class. | 1205 // Create a new closure class. |
1207 SourceString name = const SourceString("BoundClosure"); | 1206 SourceString name = const SourceString("BoundClosure"); |
1208 ClassElement closureClassElement = new ClosureClassElement( | 1207 ClassElement closureClassElement = new ClosureClassElement( |
1209 name, compiler, member, member.getCompilationUnit()); | 1208 name, compiler, member, member.getCompilationUnit()); |
(...skipping 13 matching lines...) Expand all Loading... |
1223 new ClosureInvocationElement(namer.CLOSURE_INVOCATION_NAME, member); | 1222 new ClosureInvocationElement(namer.CLOSURE_INVOCATION_NAME, member); |
1224 | 1223 |
1225 String invocationName = namer.instanceMethodName(callElement); | 1224 String invocationName = namer.instanceMethodName(callElement); |
1226 List<String> arguments = new List<String>(parameterCount); | 1225 List<String> arguments = new List<String>(parameterCount); |
1227 for (int i = 0; i < parameterCount; i++) { | 1226 for (int i = 0; i < parameterCount; i++) { |
1228 arguments[i] = "p$i"; | 1227 arguments[i] = "p$i"; |
1229 } | 1228 } |
1230 String joinedArgs = Strings.join(arguments, ", "); | 1229 String joinedArgs = Strings.join(arguments, ", "); |
1231 boundClosureBuffer.add( | 1230 boundClosureBuffer.add( |
1232 "$invocationName: function($joinedArgs) {"); | 1231 "$invocationName: function($joinedArgs) {"); |
1233 boundClosureBuffer.add( | 1232 String callArgs = hasExtraArgument |
1234 " return this.self[this.target]($extraArgWithThis$joinedArgs);"); | 1233 ? joinedArgs.isEmpty |
| 1234 ? 'this.$extraArgWithoutComma' |
| 1235 : 'this.$extraArg$joinedArgs' |
| 1236 : joinedArgs; |
| 1237 boundClosureBuffer.add(" return this.self[this.target]($callArgs);"); |
1235 boundClosureBuffer.add(" }"); | 1238 boundClosureBuffer.add(" }"); |
1236 addParameterStubs(callElement, (String stubName, CodeBuffer memberValue) { | 1239 addParameterStubs(callElement, (String stubName, CodeBuffer memberValue) { |
1237 boundClosureBuffer.add(',\n $stubName: $memberValue'); | 1240 boundClosureBuffer.add(',\n $stubName: $memberValue'); |
1238 }); | 1241 }); |
1239 boundClosureBuffer.add("\n};\n"); | 1242 boundClosureBuffer.add("\n};\n"); |
1240 | 1243 |
1241 closureClass = namer.isolateAccess(closureClassElement); | 1244 closureClass = namer.isolateAccess(closureClassElement); |
1242 | 1245 |
1243 // Cache it. | 1246 // Cache it. |
1244 if (!hasOptionalParameters) { | 1247 if (!hasOptionalParameters) { |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1731 const String HOOKS_API_USAGE = """ | 1734 const String HOOKS_API_USAGE = """ |
1732 // Generated by dart2js, the Dart to JavaScript compiler. | 1735 // Generated by dart2js, the Dart to JavaScript compiler. |
1733 // The code supports the following hooks: | 1736 // The code supports the following hooks: |
1734 // dartPrint(message) - if this function is defined it is called | 1737 // dartPrint(message) - if this function is defined it is called |
1735 // instead of the Dart [print] method. | 1738 // instead of the Dart [print] method. |
1736 // dartMainRunner(main) - if this function is defined, the Dart [main] | 1739 // dartMainRunner(main) - if this function is defined, the Dart [main] |
1737 // method will not be invoked directly. | 1740 // method will not be invoked directly. |
1738 // Instead, a closure that will invoke [main] is | 1741 // Instead, a closure that will invoke [main] is |
1739 // passed to [dartMainRunner]. | 1742 // passed to [dartMainRunner]. |
1740 """; | 1743 """; |
OLD | NEW |