| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class ClassStubGenerator { | 7 class ClassStubGenerator { |
| 8 final Namer namer; | 8 final Namer namer; |
| 9 final Compiler compiler; | 9 final Compiler compiler; |
| 10 final JavaScriptBackend backend; | 10 final JavaScriptBackend backend; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 return js('function(#, v) { return #.# = v; }', | 41 return js('function(#, v) { return #.# = v; }', |
| 42 [args, receiver, fieldName]); | 42 [args, receiver, fieldName]); |
| 43 } | 43 } |
| 44 | 44 |
| 45 /** | 45 /** |
| 46 * Documentation wanted -- johnniwinther | 46 * Documentation wanted -- johnniwinther |
| 47 * | 47 * |
| 48 * Invariant: [member] must be a declaration element. | 48 * Invariant: [member] must be a declaration element. |
| 49 */ | 49 */ |
| 50 Map<String, jsAst.Expression> generateCallStubsForGetter( | 50 Map<String, jsAst.Expression> generateCallStubsForGetter( |
| 51 Element member, Map<Selector, TypeMaskSet> selectors) { | 51 Element member, Set<Selector> selectors) { |
| 52 assert(invariant(member, member.isDeclaration)); | 52 assert(invariant(member, member.isDeclaration)); |
| 53 | 53 |
| 54 // If the method is intercepted, the stub gets the | 54 // If the method is intercepted, the stub gets the |
| 55 // receiver explicitely and we need to pass it to the getter call. | 55 // receiver explicitely and we need to pass it to the getter call. |
| 56 bool isInterceptedMethod = backend.isInterceptedMethod(member); | 56 bool isInterceptedMethod = backend.isInterceptedMethod(member); |
| 57 bool isInterceptorClass = | 57 bool isInterceptorClass = |
| 58 backend.isInterceptorClass(member.enclosingClass); | 58 backend.isInterceptorClass(member.enclosingClass); |
| 59 | 59 |
| 60 const String receiverArgumentName = r'$receiver'; | 60 const String receiverArgumentName = r'$receiver'; |
| 61 | 61 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 73 return js('#.#', [receiver, fieldName]); | 73 return js('#.#', [receiver, fieldName]); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 Map<String, jsAst.Expression> generatedStubs = <String, jsAst.Expression>{}; | 77 Map<String, jsAst.Expression> generatedStubs = <String, jsAst.Expression>{}; |
| 78 | 78 |
| 79 // Two selectors may match but differ only in type. To avoid generating | 79 // Two selectors may match but differ only in type. To avoid generating |
| 80 // identical stubs for each we track untyped selectors which already have | 80 // identical stubs for each we track untyped selectors which already have |
| 81 // stubs. | 81 // stubs. |
| 82 Set<Selector> generatedSelectors = new Set<Selector>(); | 82 Set<Selector> generatedSelectors = new Set<Selector>(); |
| 83 for (Selector selector in selectors.keys) { | 83 for (Selector selector in selectors) { |
| 84 if (generatedSelectors.contains(selector)) continue; | 84 if (selector.applies(member, compiler.world)) { |
| 85 if (!selector.appliesUnnamed(member, compiler.world)) continue; | 85 selector = selector.asUntyped; |
| 86 for (TypeMask mask in selectors[selector].masks) { | 86 if (generatedSelectors.contains(selector)) continue; |
| 87 if (mask != null && | |
| 88 !mask.canHit(member, selector, compiler.world)) { | |
| 89 continue; | |
| 90 } | |
| 91 | |
| 92 generatedSelectors.add(selector); | 87 generatedSelectors.add(selector); |
| 93 | 88 |
| 94 String invocationName = namer.invocationName(selector); | 89 String invocationName = namer.invocationName(selector); |
| 95 Selector callSelector = new Selector.callClosureFrom(selector); | 90 Selector callSelector = new Selector.callClosureFrom(selector); |
| 96 String closureCallName = namer.invocationName(callSelector); | 91 String closureCallName = namer.invocationName(callSelector); |
| 97 | 92 |
| 98 List<jsAst.Parameter> parameters = <jsAst.Parameter>[]; | 93 List<jsAst.Parameter> parameters = <jsAst.Parameter>[]; |
| 99 List<jsAst.Expression> arguments = <jsAst.Expression>[]; | 94 List<jsAst.Expression> arguments = <jsAst.Expression>[]; |
| 100 if (isInterceptedMethod) { | 95 if (isInterceptedMethod) { |
| 101 parameters.add(new jsAst.Parameter(receiverArgumentName)); | 96 parameters.add(new jsAst.Parameter(receiverArgumentName)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 120 | 115 |
| 121 Map<String, Selector> computeSelectorsForNsmHandlers() { | 116 Map<String, Selector> computeSelectorsForNsmHandlers() { |
| 122 | 117 |
| 123 Map<String, Selector> jsNames = <String, Selector>{}; | 118 Map<String, Selector> jsNames = <String, Selector>{}; |
| 124 | 119 |
| 125 // Do not generate no such method handlers if there is no class. | 120 // Do not generate no such method handlers if there is no class. |
| 126 if (compiler.codegenWorld.directlyInstantiatedClasses.isEmpty) { | 121 if (compiler.codegenWorld.directlyInstantiatedClasses.isEmpty) { |
| 127 return jsNames; | 122 return jsNames; |
| 128 } | 123 } |
| 129 | 124 |
| 130 void addNoSuchMethodHandlers(String ignore, | 125 void addNoSuchMethodHandlers(String ignore, Set<Selector> selectors) { |
| 131 Map<Selector, TypeMaskSet> selectors) { | |
| 132 TypeMask objectSubclassTypeMask = | 126 TypeMask objectSubclassTypeMask = |
| 133 new TypeMask.subclass(compiler.objectClass, compiler.world); | 127 new TypeMask.subclass(compiler.objectClass, compiler.world); |
| 134 | 128 |
| 135 for (Selector selector in selectors.keys) { | 129 for (Selector selector in selectors) { |
| 136 TypeMaskSet maskSet = selectors[selector]; | 130 TypeMask mask = selector.mask; |
| 137 for (TypeMask mask in maskSet.masks) { | 131 if (mask == null) mask = objectSubclassTypeMask; |
| 138 if (mask == null) mask = objectSubclassTypeMask; | |
| 139 | 132 |
| 140 if (mask.needsNoSuchMethodHandling(selector, compiler.world)) { | 133 if (!mask.needsNoSuchMethodHandling(selector, compiler.world)) { |
| 141 String jsName = namer.invocationMirrorInternalName(selector); | 134 continue; |
| 142 jsNames[jsName] = selector; | |
| 143 break; | |
| 144 } | |
| 145 } | 135 } |
| 136 String jsName = namer.invocationMirrorInternalName(selector); |
| 137 jsNames[jsName] = selector; |
| 146 } | 138 } |
| 147 } | 139 } |
| 148 | 140 |
| 149 compiler.codegenWorld.forEachInvokedName(addNoSuchMethodHandlers); | 141 compiler.codegenWorld.invokedNames.forEach(addNoSuchMethodHandlers); |
| 150 compiler.codegenWorld.forEachInvokedGetter(addNoSuchMethodHandlers); | 142 compiler.codegenWorld.invokedGetters.forEach(addNoSuchMethodHandlers); |
| 151 compiler.codegenWorld.forEachInvokedSetter(addNoSuchMethodHandlers); | 143 compiler.codegenWorld.invokedSetters.forEach(addNoSuchMethodHandlers); |
| 152 return jsNames; | 144 return jsNames; |
| 153 } | 145 } |
| 154 | 146 |
| 155 StubMethod generateStubForNoSuchMethod(String name, Selector selector) { | 147 StubMethod generateStubForNoSuchMethod(String name, Selector selector) { |
| 156 // Values match JSInvocationMirror in js-helper library. | 148 // Values match JSInvocationMirror in js-helper library. |
| 157 int type = selector.invocationMirrorKind; | 149 int type = selector.invocationMirrorKind; |
| 158 List<String> parameterNames = | 150 List<String> parameterNames = |
| 159 new List.generate(selector.argumentCount, (i) => '\$$i'); | 151 new List.generate(selector.argumentCount, (i) => '\$$i'); |
| 160 | 152 |
| 161 List<jsAst.Expression> argNames = | 153 List<jsAst.Expression> argNames = |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 ? function() { | 279 ? function() { |
| 288 if (cache === void 0) cache = #tearOff( | 280 if (cache === void 0) cache = #tearOff( |
| 289 this, funcs, reflectionInfo, true, [], name).prototype; | 281 this, funcs, reflectionInfo, true, [], name).prototype; |
| 290 return cache; | 282 return cache; |
| 291 } | 283 } |
| 292 : tearOffGetter(funcs, reflectionInfo, name, isIntercepted); | 284 : tearOffGetter(funcs, reflectionInfo, name, isIntercepted); |
| 293 }''', {'tearOff': tearOffAccessExpression}); | 285 }''', {'tearOff': tearOffAccessExpression}); |
| 294 | 286 |
| 295 return <jsAst.Statement>[tearOffGetter, tearOff]; | 287 return <jsAst.Statement>[tearOffGetter, tearOff]; |
| 296 } | 288 } |
| OLD | NEW |