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