| 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 class NativeEmitter { | 7 class NativeEmitter { |
| 8 | 8 |
| 9 CodeEmitterTask emitter; | 9 CodeEmitterTask emitter; |
| 10 CodeBuffer nativeBuffer; | 10 CodeBuffer nativeBuffer; |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 new jsAst.LiteralNumber('$arity')])))); | 184 new jsAst.LiteralNumber('$arity')])))); |
| 185 break; | 185 break; |
| 186 } | 186 } |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 }); | 189 }); |
| 190 } | 190 } |
| 191 | 191 |
| 192 List<jsAst.Statement> generateParameterStubStatements( | 192 List<jsAst.Statement> generateParameterStubStatements( |
| 193 Element member, | 193 Element member, |
| 194 bool isInterceptedMethod, |
| 194 String invocationName, | 195 String invocationName, |
| 195 List<jsAst.Parameter> stubParameters, | 196 List<jsAst.Parameter> stubParameters, |
| 196 List<jsAst.Expression> argumentsBuffer, | 197 List<jsAst.Expression> argumentsBuffer, |
| 197 int indexOfLastOptionalArgumentInParameters) { | 198 int indexOfLastOptionalArgumentInParameters) { |
| 198 // The target JS function may check arguments.length so we need to | 199 // The target JS function may check arguments.length so we need to |
| 199 // make sure not to pass any unspecified optional arguments to it. | 200 // make sure not to pass any unspecified optional arguments to it. |
| 200 // For example, for the following Dart method: | 201 // For example, for the following Dart method: |
| 201 // foo([x, y, z]); | 202 // foo([x, y, z]); |
| 202 // The call: | 203 // The call: |
| 203 // foo(y: 1) | 204 // foo(y: 1) |
| 204 // must be turned into a JS call to: | 205 // must be turned into a JS call to: |
| 205 // foo(null, y). | 206 // foo(null, y). |
| 206 | 207 |
| 207 ClassElement classElement = member.enclosingElement; | 208 ClassElement classElement = member.enclosingElement; |
| 208 String nativeTagInfo = classElement.nativeTagInfo.slowToString(); | 209 String nativeTagInfo = classElement.nativeTagInfo.slowToString(); |
| 209 | 210 |
| 210 List<jsAst.Statement> statements = <jsAst.Statement>[]; | 211 List<jsAst.Statement> statements = <jsAst.Statement>[]; |
| 211 potentiallyConvertDartClosuresToJs(statements, member, stubParameters); | 212 potentiallyConvertDartClosuresToJs(statements, member, stubParameters); |
| 212 | 213 |
| 213 String target; | 214 String target; |
| 215 jsAst.Expression receiver; |
| 214 List<jsAst.Expression> arguments; | 216 List<jsAst.Expression> arguments; |
| 215 | 217 |
| 216 if (!nativeMethods.contains(member)) { | 218 if (!nativeMethods.contains(member)) { |
| 217 // When calling a method that has a native body, we call it with our | 219 // When calling a method that has a native body, we call it with our |
| 218 // calling conventions. | 220 // calling conventions. |
| 219 target = backend.namer.getName(member); | 221 target = backend.namer.getName(member); |
| 220 arguments = argumentsBuffer; | 222 arguments = argumentsBuffer; |
| 221 } else { | 223 } else { |
| 222 // When calling a JS method, we call it with the native name, and only the | 224 // When calling a JS method, we call it with the native name, and only the |
| 223 // arguments up until the last one provided. | 225 // arguments up until the last one provided. |
| 224 target = member.fixedBackendName(); | 226 target = member.fixedBackendName(); |
| 225 arguments = argumentsBuffer.getRange( | 227 |
| 226 0, indexOfLastOptionalArgumentInParameters + 1); | 228 if (isInterceptedMethod) { |
| 229 receiver = argumentsBuffer[0]; |
| 230 arguments = argumentsBuffer.getRange(1, |
| 231 indexOfLastOptionalArgumentInParameters); |
| 232 } else { |
| 233 receiver = new jsAst.VariableUse('this'); |
| 234 arguments = argumentsBuffer.getRange(0, |
| 235 indexOfLastOptionalArgumentInParameters + 1); |
| 236 } |
| 227 } | 237 } |
| 228 statements.add( | 238 statements.add(new jsAst.Return(receiver[target](arguments))); |
| 229 new jsAst.Return( | |
| 230 new jsAst.VariableUse('this')[target](arguments))); | |
| 231 | 239 |
| 232 if (!overriddenMethods.contains(member)) { | 240 if (!overriddenMethods.contains(member)) { |
| 233 // Call the method directly. | 241 // Call the method directly. |
| 234 return statements; | 242 return statements; |
| 235 } else { | 243 } else { |
| 236 return <jsAst.Statement>[ | 244 return <jsAst.Statement>[ |
| 237 generateMethodBodyWithPrototypeCheck( | 245 generateMethodBodyWithPrototypeCheck( |
| 238 invocationName, new jsAst.Block(statements), stubParameters)]; | 246 invocationName, new jsAst.Block(statements), stubParameters)]; |
| 239 } | 247 } |
| 240 } | 248 } |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 if (emitter.compiler.enableMinification) targetBuffer.add(';'); | 546 if (emitter.compiler.enableMinification) targetBuffer.add(';'); |
| 539 targetBuffer.add(jsAst.prettyPrint( | 547 targetBuffer.add(jsAst.prettyPrint( |
| 540 new jsAst.ExpressionStatement(init), compiler)); | 548 new jsAst.ExpressionStatement(init), compiler)); |
| 541 targetBuffer.add('\n'); | 549 targetBuffer.add('\n'); |
| 542 } | 550 } |
| 543 | 551 |
| 544 targetBuffer.add(nativeBuffer); | 552 targetBuffer.add(nativeBuffer); |
| 545 targetBuffer.add('\n'); | 553 targetBuffer.add('\n'); |
| 546 } | 554 } |
| 547 } | 555 } |
| OLD | NEW |