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