| 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 class NativeEmitter { | 5 class NativeEmitter { |
| 6 | 6 |
| 7 CodeEmitterTask emitter; | 7 CodeEmitterTask emitter; |
| 8 StringBuffer nativeBuffer; | 8 StringBuffer nativeBuffer; |
| 9 | 9 |
| 10 // Classes that participate in dynamic dispatch. These are the | 10 // Classes that participate in dynamic dispatch. These are the |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 | 161 |
| 162 classesWithDynamicDispatch.add(classElement); | 162 classesWithDynamicDispatch.add(classElement); |
| 163 } | 163 } |
| 164 | 164 |
| 165 List<ClassElement> getDirectSubclasses(ClassElement cls) { | 165 List<ClassElement> getDirectSubclasses(ClassElement cls) { |
| 166 List<ClassElement> result = directSubtypes[cls]; | 166 List<ClassElement> result = directSubtypes[cls]; |
| 167 return result === null ? const<ClassElement>[] : result; | 167 return result === null ? const<ClassElement>[] : result; |
| 168 } | 168 } |
| 169 | 169 |
| 170 void potentiallyConvertDartClosuresToJs(StringBuffer code, | 170 void potentiallyConvertDartClosuresToJs(StringBuffer code, |
| 171 FunctionElement member) { | 171 FunctionElement member, |
| 172 List<String> argumentsBuffer) { |
| 172 FunctionSignature parameters = member.computeSignature(compiler); | 173 FunctionSignature parameters = member.computeSignature(compiler); |
| 173 Element converter = | 174 Element converter = |
| 174 compiler.findHelper(const SourceString('convertDartClosureToJS')); | 175 compiler.findHelper(const SourceString('convertDartClosureToJS')); |
| 175 String closureConverter = compiler.namer.isolateAccess(converter); | 176 String closureConverter = compiler.namer.isolateAccess(converter); |
| 176 parameters.forEachParameter((Element parameter) { | 177 parameters.forEachParameter((Element parameter) { |
| 178 String name = parameter.name.slowToString(); |
| 179 // If [name] is not in [argumentsBuffer], then the parameter is |
| 180 // an optional parameter that was not provided for that stub. |
| 181 if (argumentsBuffer.indexOf(name) == -1) return; |
| 177 Type type = parameter.computeType(compiler); | 182 Type type = parameter.computeType(compiler); |
| 178 if (type is FunctionType) { | 183 if (type is FunctionType) { |
| 179 String name = parameter.name.slowToString(); | |
| 180 int arity = type.computeArity(); | 184 int arity = type.computeArity(); |
| 181 code.add(' $name = $closureConverter($name, $arity);\n'); | 185 code.add(' $name = $closureConverter($name, $arity);\n'); |
| 182 } | 186 } |
| 183 }); | 187 }); |
| 184 } | 188 } |
| 185 | 189 |
| 186 String generateParameterStub(Element member, | 190 String generateParameterStub(Element member, |
| 187 String invocationName, | 191 String invocationName, |
| 188 String stubParameters, | 192 String stubParameters, |
| 189 List<String> argumentsBuffer, | 193 List<String> argumentsBuffer, |
| 190 int indexOfLastOptionalArgumentInParameters, | 194 int indexOfLastOptionalArgumentInParameters, |
| 191 StringBuffer buffer) { | 195 StringBuffer buffer) { |
| 192 // The target JS function may check arguments.length so we need to | 196 // The target JS function may check arguments.length so we need to |
| 193 // make sure not to pass any unspecified optional arguments to it. | 197 // make sure not to pass any unspecified optional arguments to it. |
| 194 // For example, for the following Dart method: | 198 // For example, for the following Dart method: |
| 195 // foo([x, y, z]); | 199 // foo([x, y, z]); |
| 196 // The call: | 200 // The call: |
| 197 // foo(y: 1) | 201 // foo(y: 1) |
| 198 // must be turned into a JS call to: | 202 // must be turned into a JS call to: |
| 199 // foo(null, y). | 203 // foo(null, y). |
| 200 | 204 |
| 201 List<String> nativeArgumentsBuffer = argumentsBuffer.getRange( | 205 List<String> nativeArgumentsBuffer = argumentsBuffer.getRange( |
| 202 0, indexOfLastOptionalArgumentInParameters + 1); | 206 0, indexOfLastOptionalArgumentInParameters + 1); |
| 203 | 207 |
| 204 ClassElement classElement = member.enclosingElement; | 208 ClassElement classElement = member.enclosingElement; |
| 205 String nativeName = classElement.nativeName.slowToString(); | 209 String nativeName = classElement.nativeName.slowToString(); |
| 206 String nativeArguments = Strings.join(nativeArgumentsBuffer, ","); | 210 String nativeArguments = Strings.join(nativeArgumentsBuffer, ","); |
| 207 | 211 |
| 208 StringBuffer code = new StringBuffer(); | 212 StringBuffer code = new StringBuffer(); |
| 209 potentiallyConvertDartClosuresToJs(code, member); | 213 potentiallyConvertDartClosuresToJs(code, member, argumentsBuffer); |
| 210 | 214 |
| 211 if (!nativeMethods.contains(member)) { | 215 if (!nativeMethods.contains(member)) { |
| 212 // When calling a method that has a native body, we call it | 216 // When calling a method that has a native body, we call it |
| 213 // with our calling conventions. | 217 // with our calling conventions. |
| 214 String arguments = Strings.join(argumentsBuffer, ","); | 218 String arguments = Strings.join(argumentsBuffer, ","); |
| 215 code.add(' return this.${compiler.namer.getName(member)}($arguments)'); | 219 code.add(' return this.${compiler.namer.getName(member)}($arguments)'); |
| 216 } else { | 220 } else { |
| 217 // When calling a JS method, we call it with the native name. | 221 // When calling a JS method, we call it with the native name. |
| 218 String name = redirectingMethods[member]; | 222 String name = redirectingMethods[member]; |
| 219 if (name === null) name = member.name.slowToString(); | 223 if (name === null) name = member.name.slowToString(); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 String toStringName = compiler.namer.instanceMethodName( | 398 String toStringName = compiler.namer.instanceMethodName( |
| 395 null, const SourceString('toString'), 0); | 399 null, const SourceString('toString'), 0); |
| 396 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); | 400 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); |
| 397 objectProperties.add( | 401 objectProperties.add( |
| 398 'function() { return $toStringHelperName(this); });\n'); | 402 'function() { return $toStringHelperName(this); });\n'); |
| 399 | 403 |
| 400 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n'); | 404 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n'); |
| 401 targetBuffer.add('$objectProperties$nativeBuffer\n'); | 405 targetBuffer.add('$objectProperties$nativeBuffer\n'); |
| 402 } | 406 } |
| 403 } | 407 } |
| OLD | NEW |