| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.full_emitter; | 5 part of dart2js.js_emitter.full_emitter; |
| 6 | 6 |
| 7 class InterceptorEmitter extends CodeEmitterHelper { | 7 class InterceptorEmitter extends CodeEmitterHelper { |
| 8 final Set<jsAst.Name> interceptorInvocationNames = | 8 final Set<jsAst.Name> interceptorInvocationNames = new Set<jsAst.Name>(); |
| 9 new Set<jsAst.Name>(); | |
| 10 | 9 |
| 11 void recordMangledNameOfMemberMethod(FunctionElement member, | 10 void recordMangledNameOfMemberMethod( |
| 12 jsAst.Name name) { | 11 FunctionElement member, jsAst.Name name) { |
| 13 if (backend.isInterceptedMethod(member)) { | 12 if (backend.isInterceptedMethod(member)) { |
| 14 interceptorInvocationNames.add(name); | 13 interceptorInvocationNames.add(name); |
| 15 } | 14 } |
| 16 } | 15 } |
| 17 | 16 |
| 18 jsAst.Expression buildGetInterceptorMethod(jsAst.Name key, | 17 jsAst.Expression buildGetInterceptorMethod( |
| 19 Set<ClassElement> classes) { | 18 jsAst.Name key, Set<ClassElement> classes) { |
| 20 InterceptorStubGenerator stubGenerator = | 19 InterceptorStubGenerator stubGenerator = |
| 21 new InterceptorStubGenerator(compiler, namer, backend); | 20 new InterceptorStubGenerator(compiler, namer, backend); |
| 22 jsAst.Expression function = | 21 jsAst.Expression function = |
| 23 stubGenerator.generateGetInterceptorMethod(classes); | 22 stubGenerator.generateGetInterceptorMethod(classes); |
| 24 | 23 |
| 25 return function; | 24 return function; |
| 26 } | 25 } |
| 27 | 26 |
| 28 /** | 27 /** |
| 29 * Emit all versions of the [:getInterceptor:] method. | 28 * Emit all versions of the [:getInterceptor:] method. |
| 30 */ | 29 */ |
| 31 jsAst.Statement buildGetInterceptorMethods() { | 30 jsAst.Statement buildGetInterceptorMethods() { |
| 32 List<jsAst.Statement> parts = <jsAst.Statement>[]; | 31 List<jsAst.Statement> parts = <jsAst.Statement>[]; |
| 33 | 32 |
| 34 parts.add(js.comment('getInterceptor methods')); | 33 parts.add(js.comment('getInterceptor methods')); |
| 35 | 34 |
| 36 Map<jsAst.Name, Set<ClassElement>> specializedGetInterceptors = | 35 Map<jsAst.Name, Set<ClassElement>> specializedGetInterceptors = |
| 37 backend.specializedGetInterceptors; | 36 backend.specializedGetInterceptors; |
| 38 List<jsAst.Name> names = specializedGetInterceptors.keys.toList() | 37 List<jsAst.Name> names = specializedGetInterceptors.keys.toList()..sort(); |
| 39 ..sort(); | |
| 40 for (jsAst.Name name in names) { | 38 for (jsAst.Name name in names) { |
| 41 Set<ClassElement> classes = specializedGetInterceptors[name]; | 39 Set<ClassElement> classes = specializedGetInterceptors[name]; |
| 42 parts.add( | 40 parts.add(js.statement('#.# = #', [ |
| 43 js.statement('#.# = #', | 41 namer.globalObjectFor(backend.helpers.interceptorsLibrary), |
| 44 [namer.globalObjectFor(backend.helpers.interceptorsLibrary), | 42 name, |
| 45 name, | 43 buildGetInterceptorMethod(name, classes) |
| 46 buildGetInterceptorMethod(name, classes)])); | 44 ])); |
| 47 } | 45 } |
| 48 | 46 |
| 49 return new jsAst.Block(parts); | 47 return new jsAst.Block(parts); |
| 50 } | 48 } |
| 51 | 49 |
| 52 jsAst.Statement buildOneShotInterceptors() { | 50 jsAst.Statement buildOneShotInterceptors() { |
| 53 List<jsAst.Statement> parts = <jsAst.Statement>[]; | 51 List<jsAst.Statement> parts = <jsAst.Statement>[]; |
| 54 Iterable<jsAst.Name> names = backend.oneShotInterceptors.keys.toList() | 52 Iterable<jsAst.Name> names = backend.oneShotInterceptors.keys.toList() |
| 55 ..sort(); | 53 ..sort(); |
| 56 | 54 |
| 57 InterceptorStubGenerator stubGenerator = | 55 InterceptorStubGenerator stubGenerator = |
| 58 new InterceptorStubGenerator(compiler, namer, backend); | 56 new InterceptorStubGenerator(compiler, namer, backend); |
| 59 String globalObject = | 57 String globalObject = |
| 60 namer.globalObjectFor(backend.helpers.interceptorsLibrary); | 58 namer.globalObjectFor(backend.helpers.interceptorsLibrary); |
| 61 for (jsAst.Name name in names) { | 59 for (jsAst.Name name in names) { |
| 62 jsAst.Expression function = | 60 jsAst.Expression function = |
| 63 stubGenerator.generateOneShotInterceptor(name); | 61 stubGenerator.generateOneShotInterceptor(name); |
| 64 parts.add(js.statement('${globalObject}.# = #', [name, function])); | 62 parts.add(js.statement('${globalObject}.# = #', [name, function])); |
| 65 } | 63 } |
| 66 | 64 |
| 67 return new jsAst.Block(parts); | 65 return new jsAst.Block(parts); |
| 68 } | 66 } |
| 69 | 67 |
| 70 /** | 68 /** |
| 71 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the | 69 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the |
| 72 * possible selector names that are intercepted into the | 70 * possible selector names that are intercepted into the |
| 73 * [interceptedNames] embedded global. The implementation of | 71 * [interceptedNames] embedded global. The implementation of |
| 74 * [_invokeOn] will use it to determine whether it should call the | 72 * [_invokeOn] will use it to determine whether it should call the |
| 75 * method with an extra parameter. | 73 * method with an extra parameter. |
| 76 */ | 74 */ |
| 77 jsAst.ObjectInitializer generateInterceptedNamesSet() { | 75 jsAst.ObjectInitializer generateInterceptedNamesSet() { |
| 78 // We could also generate the list of intercepted names at | 76 // We could also generate the list of intercepted names at |
| 79 // runtime, by running through the subclasses of Interceptor | 77 // runtime, by running through the subclasses of Interceptor |
| 80 // (which can easily be identified). | 78 // (which can easily be identified). |
| 81 if (!compiler.enabledInvokeOn) return null; | 79 if (!compiler.enabledInvokeOn) return null; |
| 82 | 80 |
| 83 Iterable<jsAst.Name> invocationNames = interceptorInvocationNames.toList() | 81 Iterable<jsAst.Name> invocationNames = interceptorInvocationNames.toList() |
| 84 ..sort();; | 82 ..sort(); |
| 83 ; |
| 85 List<jsAst.Property> properties = invocationNames.map((jsAst.Name name) { | 84 List<jsAst.Property> properties = invocationNames.map((jsAst.Name name) { |
| 86 return new jsAst.Property(js.quoteName(name), js.number(1)); | 85 return new jsAst.Property(js.quoteName(name), js.number(1)); |
| 87 }).toList(); | 86 }).toList(); |
| 88 return new jsAst.ObjectInitializer(properties, isOneLiner: true); | 87 return new jsAst.ObjectInitializer(properties, isOneLiner: true); |
| 89 } | 88 } |
| 90 | 89 |
| 91 /** | 90 /** |
| 92 * Emit initializer for `typeToInterceptorMap` data structure used by | 91 * Emit initializer for `typeToInterceptorMap` data structure used by |
| 93 * `findInterceptorForType`. See declaration of `typeToInterceptor` in | 92 * `findInterceptorForType`. See declaration of `typeToInterceptor` in |
| 94 * `interceptors.dart`. | 93 * `interceptors.dart`. |
| 95 */ | 94 */ |
| 96 jsAst.Statement buildTypeToInterceptorMap(Program program) { | 95 jsAst.Statement buildTypeToInterceptorMap(Program program) { |
| 97 jsAst.Expression array = program.typeToInterceptorMap; | 96 jsAst.Expression array = program.typeToInterceptorMap; |
| 98 if (array == null) return js.comment("Empty type-to-interceptor map."); | 97 if (array == null) return js.comment("Empty type-to-interceptor map."); |
| 99 | 98 |
| 100 jsAst.Expression typeToInterceptorMap = emitter | 99 jsAst.Expression typeToInterceptorMap = emitter |
| 101 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); | 100 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); |
| 102 return js.statement('# = #', [typeToInterceptorMap, array]); | 101 return js.statement('# = #', [typeToInterceptorMap, array]); |
| 103 } | 102 } |
| 104 } | 103 } |
| OLD | NEW |