Chromium Code Reviews| 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 |
| 11 // classes that contain used members. | 11 // classes that contain used members. |
| 12 Set<ClassElement> classesWithDynamicDispatch; | 12 Set<ClassElement> classesWithDynamicDispatch; |
| 13 | 13 |
| 14 // Native classes found in the application. | 14 // Native classes found in the application. |
| 15 Set<ClassElement> nativeClasses; | 15 Set<ClassElement> nativeClasses; |
| 16 | 16 |
| 17 // Caches the native subtypes of a native class. | 17 // Caches the native subtypes of a native class. |
| 18 Map<ClassElement, List<ClassElement>> subtypes; | 18 Map<ClassElement, List<ClassElement>> subtypes; |
| 19 | 19 |
| 20 // Caches the direct native subtypes of a native class. | 20 // Caches the direct native subtypes of a native class. |
| 21 Map<ClassElement, List<ClassElement>> directSubtypes; | 21 Map<ClassElement, List<ClassElement>> directSubtypes; |
| 22 | 22 |
| 23 // Caches the native methods that are overridden by a native class. | 23 // Caches the native methods that are overridden by a native class. |
| 24 // Note that the method that overrides does not have to be native: | 24 // Note that the method that overrides does not have to be native: |
| 25 // it's the overridden method that must make sure it will dispatch | 25 // it's the overridden method that must make sure it will dispatch |
| 26 // to its subclass if it sees an instance whose class is a subclass. | 26 // to its subclass if it sees an instance whose class is a subclass. |
| 27 Set<FunctionElement> overriddenMethods; | 27 Set<FunctionElement> overriddenMethods; |
| 28 | 28 |
| 29 // Caches the methods that should just call a native JS | 29 // Caches the methods that have a native body. |
| 30 // implementation. | |
| 31 Set<FunctionElement> nativeMethods; | 30 Set<FunctionElement> nativeMethods; |
| 32 | 31 |
| 32 // Caches the methods that redirects to a JS method. | |
|
floitsch
2012/05/21 10:45:01
redirect
ngeoffray
2012/05/21 10:47:19
Done.
| |
| 33 Map<FunctionElement, String> redirectingMethods; | |
| 34 | |
| 33 NativeEmitter(this.emitter) | 35 NativeEmitter(this.emitter) |
| 34 : classesWithDynamicDispatch = new Set<ClassElement>(), | 36 : classesWithDynamicDispatch = new Set<ClassElement>(), |
| 35 nativeClasses = new Set<ClassElement>(), | 37 nativeClasses = new Set<ClassElement>(), |
| 36 subtypes = new Map<ClassElement, List<ClassElement>>(), | 38 subtypes = new Map<ClassElement, List<ClassElement>>(), |
| 37 directSubtypes = new Map<ClassElement, List<ClassElement>>(), | 39 directSubtypes = new Map<ClassElement, List<ClassElement>>(), |
| 38 overriddenMethods = new Set<FunctionElement>(), | 40 overriddenMethods = new Set<FunctionElement>(), |
| 39 nativeMethods = new Set<FunctionElement>(), | 41 nativeMethods = new Set<FunctionElement>(), |
| 42 redirectingMethods = new Map<FunctionElement, String>(), | |
| 40 nativeBuffer = new StringBuffer(); | 43 nativeBuffer = new StringBuffer(); |
| 41 | 44 |
| 42 Compiler get compiler() => emitter.compiler; | 45 Compiler get compiler() => emitter.compiler; |
| 43 | 46 |
| 47 void addRedirectingMethod(FunctionElement element, String name) { | |
| 48 redirectingMethods[element] = name; | |
| 49 } | |
| 50 | |
| 44 String get dynamicName() { | 51 String get dynamicName() { |
| 45 Element element = compiler.findHelper( | 52 Element element = compiler.findHelper( |
| 46 const SourceString('dynamicFunction')); | 53 const SourceString('dynamicFunction')); |
| 47 return compiler.namer.isolateAccess(element); | 54 return compiler.namer.isolateAccess(element); |
| 48 } | 55 } |
| 49 | 56 |
| 50 String get dynamicSetMetadataName() { | 57 String get dynamicSetMetadataName() { |
| 51 Element element = compiler.findHelper( | 58 Element element = compiler.findHelper( |
| 52 const SourceString('dynamicSetMetadata')); | 59 const SourceString('dynamicSetMetadata')); |
| 53 return compiler.namer.isolateAccess(element); | 60 return compiler.namer.isolateAccess(element); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 StringBuffer code = new StringBuffer(); | 207 StringBuffer code = new StringBuffer(); |
| 201 potentiallyConvertDartClosuresToJs(code, member); | 208 potentiallyConvertDartClosuresToJs(code, member); |
| 202 | 209 |
| 203 if (!nativeMethods.contains(member)) { | 210 if (!nativeMethods.contains(member)) { |
| 204 // When calling a method that has a native body, we call it | 211 // When calling a method that has a native body, we call it |
| 205 // with our calling conventions. | 212 // with our calling conventions. |
| 206 String arguments = Strings.join(argumentsBuffer, ","); | 213 String arguments = Strings.join(argumentsBuffer, ","); |
| 207 code.add(' return this.${compiler.namer.getName(member)}($arguments)'); | 214 code.add(' return this.${compiler.namer.getName(member)}($arguments)'); |
| 208 } else { | 215 } else { |
| 209 // When calling a JS method, we call it with the native name. | 216 // When calling a JS method, we call it with the native name. |
| 210 String name = member.name.slowToString(); | 217 String name = redirectingMethods[member]; |
| 218 if (name === null) name = member.name.slowToString(); | |
| 211 code.add(' return this.$name($nativeArguments);'); | 219 code.add(' return this.$name($nativeArguments);'); |
| 212 } | 220 } |
| 213 | 221 |
| 214 if (isNativeLiteral(nativeName) || !overriddenMethods.contains(member)) { | 222 if (isNativeLiteral(nativeName) || !overriddenMethods.contains(member)) { |
| 215 // Call the method directly. | 223 // Call the method directly. |
| 216 buffer.add(code.toString()); | 224 buffer.add(code.toString()); |
| 217 } else { | 225 } else { |
| 218 native.generateMethodWithPrototypeCheck( | 226 native.generateMethodWithPrototypeCheck( |
| 219 compiler, buffer, invocationName, code.toString(), stubParameters); | 227 compiler, buffer, invocationName, code.toString(), stubParameters); |
| 220 } | 228 } |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 String toStringName = compiler.namer.instanceMethodName( | 393 String toStringName = compiler.namer.instanceMethodName( |
| 386 null, const SourceString('toString'), 0); | 394 null, const SourceString('toString'), 0); |
| 387 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); | 395 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); |
| 388 objectProperties.add( | 396 objectProperties.add( |
| 389 'function() { return $toStringHelperName(this); });\n'); | 397 'function() { return $toStringHelperName(this); });\n'); |
| 390 | 398 |
| 391 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n'); | 399 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n'); |
| 392 targetBuffer.add('$objectProperties$nativeBuffer\n'); | 400 targetBuffer.add('$objectProperties$nativeBuffer\n'); |
| 393 } | 401 } |
| 394 } | 402 } |
| OLD | NEW |