| 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 2218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2229 // Keep track of the JavaScript names we've already added so we | 2229 // Keep track of the JavaScript names we've already added so we |
| 2230 // do not introduce duplicates (bad for code size). | 2230 // do not introduce duplicates (bad for code size). |
| 2231 Map<String, Selector> addedJsNames = new Map<String, Selector>(); | 2231 Map<String, Selector> addedJsNames = new Map<String, Selector>(); |
| 2232 | 2232 |
| 2233 void addNoSuchMethodHandlers(SourceString ignore, Set<Selector> selectors) { | 2233 void addNoSuchMethodHandlers(SourceString ignore, Set<Selector> selectors) { |
| 2234 // Cache the object class and type. | 2234 // Cache the object class and type. |
| 2235 ClassElement objectClass = compiler.objectClass; | 2235 ClassElement objectClass = compiler.objectClass; |
| 2236 DartType objectType = objectClass.computeType(compiler); | 2236 DartType objectType = objectClass.computeType(compiler); |
| 2237 | 2237 |
| 2238 for (Selector selector in selectors) { | 2238 for (Selector selector in selectors) { |
| 2239 // Introduce a helper function that determines if the given | |
| 2240 // class has a member that matches the current name and | |
| 2241 // selector (grabbed from the scope). | |
| 2242 bool hasMatchingMember(ClassElement holder) { | |
| 2243 Element element = holder.lookupSelector(selector); | |
| 2244 return (element != null) | |
| 2245 ? selector.applies(element, compiler) | |
| 2246 : false; | |
| 2247 } | |
| 2248 | |
| 2249 // If the selector is typed, we check to see if that type may | 2239 // If the selector is typed, we check to see if that type may |
| 2250 // have a user-defined noSuchMethod implementation. If not, we | 2240 // have a user-defined noSuchMethod implementation. If not, we |
| 2251 // skip the selector altogether. | 2241 // skip the selector altogether. |
| 2252 | 2242 |
| 2253 // TODO(kasperl): This shouldn't depend on the internals of | |
| 2254 // the type mask. Move more of this code to the type mask. | |
| 2255 ClassElement receiverClass = objectClass; | |
| 2256 TypeMask mask = selector.mask; | 2243 TypeMask mask = selector.mask; |
| 2257 if (mask != null) { | 2244 if (mask == null) { |
| 2258 // If the mask is empty it doesn't contain a noSuchMethod | 2245 mask = new TypeMask.subclass(compiler.objectClass.rawType); |
| 2259 // handler -- not even if it is nullable. | |
| 2260 if (mask.isEmpty) continue; | |
| 2261 receiverClass = mask.base.element; | |
| 2262 } | 2246 } |
| 2263 | 2247 |
| 2264 // If the receiver class is guaranteed to have a member that | 2248 // If the receiver is guaranteed to have a member that |
| 2265 // matches what we're looking for, there's no need to | 2249 // matches what we're looking for, there's no need to |
| 2266 // introduce a noSuchMethod handler. It will never be called. | 2250 // introduce a noSuchMethod handler. It will never be called. |
| 2267 // | 2251 // |
| 2268 // As an example, consider this class hierarchy: | 2252 // As an example, consider this class hierarchy: |
| 2269 // | 2253 // |
| 2270 // A <-- noSuchMethod | 2254 // A <-- noSuchMethod |
| 2271 // / \ | 2255 // / \ |
| 2272 // C B <-- foo | 2256 // C B <-- foo |
| 2273 // | 2257 // |
| 2274 // If we know we're calling foo on an object of type B we | 2258 // If we know we're calling foo on an object of type B we |
| 2275 // don't have to worry about the noSuchMethod method in A | 2259 // don't have to worry about the noSuchMethod method in A |
| 2276 // because objects of type B implement foo. On the other hand, | 2260 // because objects of type B implement foo. On the other hand, |
| 2277 // if we end up calling foo on something of type C we have to | 2261 // if we end up calling foo on something of type C we have to |
| 2278 // add a handler for it. | 2262 // add a handler for it. |
| 2279 if (hasMatchingMember(receiverClass)) continue; | |
| 2280 | 2263 |
| 2281 // If the holders of all user-defined noSuchMethod | 2264 // If the holders of all user-defined noSuchMethod |
| 2282 // implementations that might be applicable to the receiver | 2265 // implementations that might be applicable to the receiver |
| 2283 // type have a matching member for the current name and | 2266 // type have a matching member for the current name and |
| 2284 // selector, we avoid introducing a noSuchMethod handler. | 2267 // selector, we avoid introducing a noSuchMethod handler. |
| 2285 // | 2268 // |
| 2286 // As an example, consider this class hierarchy: | 2269 // As an example, consider this class hierarchy: |
| 2287 // | 2270 // |
| 2288 // A <-- foo | 2271 // A <-- foo |
| 2289 // / \ | 2272 // / \ |
| 2290 // noSuchMethod --> B C <-- bar | 2273 // noSuchMethod --> B C <-- bar |
| 2291 // | | | 2274 // | | |
| 2292 // C D <-- noSuchMethod | 2275 // C D <-- noSuchMethod |
| 2293 // | 2276 // |
| 2294 // When calling foo on an object of type A, we know that the | 2277 // When calling foo on an object of type A, we know that the |
| 2295 // implementations of noSuchMethod are in the classes B and D | 2278 // implementations of noSuchMethod are in the classes B and D |
| 2296 // that also (indirectly) implement foo, so we do not need a | 2279 // that also (indirectly) implement foo, so we do not need a |
| 2297 // handler for it. | 2280 // handler for it. |
| 2298 // | 2281 // |
| 2299 // If we're calling bar on an object of type D, we don't need | 2282 // If we're calling bar on an object of type D, we don't need |
| 2300 // the handler either because all objects of type D implement | 2283 // the handler either because all objects of type D implement |
| 2301 // bar through inheritance. | 2284 // bar through inheritance. |
| 2302 // | 2285 // |
| 2303 // If we're calling bar on an object of type A we do need the | 2286 // If we're calling bar on an object of type A we do need the |
| 2304 // handler because we may have to call B.noSuchMethod since B | 2287 // handler because we may have to call B.noSuchMethod since B |
| 2305 // does not implement bar. | 2288 // does not implement bar. |
| 2306 Iterable<ClassElement> holders = | 2289 if (mask.willHit(selector, compiler)) continue; |
| 2307 compiler.world.locateNoSuchMethodHolders(selector); | |
| 2308 if (holders.every(hasMatchingMember)) continue; | |
| 2309 String jsName = namer.invocationMirrorInternalName(selector); | 2290 String jsName = namer.invocationMirrorInternalName(selector); |
| 2310 addedJsNames[jsName] = selector; | 2291 addedJsNames[jsName] = selector; |
| 2311 } | 2292 } |
| 2312 } | 2293 } |
| 2313 | 2294 |
| 2314 compiler.codegenWorld.invokedNames.forEach(addNoSuchMethodHandlers); | 2295 compiler.codegenWorld.invokedNames.forEach(addNoSuchMethodHandlers); |
| 2315 compiler.codegenWorld.invokedGetters.forEach(addNoSuchMethodHandlers); | 2296 compiler.codegenWorld.invokedGetters.forEach(addNoSuchMethodHandlers); |
| 2316 compiler.codegenWorld.invokedSetters.forEach(addNoSuchMethodHandlers); | 2297 compiler.codegenWorld.invokedSetters.forEach(addNoSuchMethodHandlers); |
| 2317 | 2298 |
| 2318 // Set flag used by generateMethod helper below. If we have very few | 2299 // Set flag used by generateMethod helper below. If we have very few |
| (...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3081 """; | 3062 """; |
| 3082 const String HOOKS_API_USAGE = """ | 3063 const String HOOKS_API_USAGE = """ |
| 3083 // The code supports the following hooks: | 3064 // The code supports the following hooks: |
| 3084 // dartPrint(message) - if this function is defined it is called | 3065 // dartPrint(message) - if this function is defined it is called |
| 3085 // instead of the Dart [print] method. | 3066 // instead of the Dart [print] method. |
| 3086 // dartMainRunner(main) - if this function is defined, the Dart [main] | 3067 // dartMainRunner(main) - if this function is defined, the Dart [main] |
| 3087 // method will not be invoked directly. | 3068 // method will not be invoked directly. |
| 3088 // Instead, a closure that will invoke [main] is | 3069 // Instead, a closure that will invoke [main] is |
| 3089 // passed to [dartMainRunner]. | 3070 // passed to [dartMainRunner]. |
| 3090 """; | 3071 """; |
| OLD | NEW |