| 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; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class InterceptorEmitter extends CodeEmitterHelper { | 7 class InterceptorEmitter extends CodeEmitterHelper { |
| 8 final Set<String> interceptorInvocationNames = new Set<String>(); | 8 final Set<String> interceptorInvocationNames = new Set<String>(); |
| 9 | 9 |
| 10 void recordMangledNameOfMemberMethod(FunctionElement member, String name) { | 10 void recordMangledNameOfMemberMethod(FunctionElement member, String name) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 * [interceptedNames] embedded global. The implementation of | 65 * [interceptedNames] embedded global. The implementation of |
| 66 * [_invokeOn] will use it to determine whether it should call the | 66 * [_invokeOn] will use it to determine whether it should call the |
| 67 * method with an extra parameter. | 67 * method with an extra parameter. |
| 68 */ | 68 */ |
| 69 jsAst.ObjectInitializer generateInterceptedNamesSet() { | 69 jsAst.ObjectInitializer generateInterceptedNamesSet() { |
| 70 // We could also generate the list of intercepted names at | 70 // We could also generate the list of intercepted names at |
| 71 // runtime, by running through the subclasses of Interceptor | 71 // runtime, by running through the subclasses of Interceptor |
| 72 // (which can easily be identified). | 72 // (which can easily be identified). |
| 73 if (!compiler.enabledInvokeOn) return null; | 73 if (!compiler.enabledInvokeOn) return null; |
| 74 | 74 |
| 75 int index = 0; | |
| 76 List<String> invocationNames = interceptorInvocationNames.toList()..sort(); | 75 List<String> invocationNames = interceptorInvocationNames.toList()..sort(); |
| 77 List<jsAst.Property> properties = | 76 List<jsAst.Property> properties = |
| 78 new List<jsAst.Property>(invocationNames.length); | 77 new List<jsAst.Property>(invocationNames.length); |
| 79 for (int i = 0; i < invocationNames.length; i++) { | 78 for (int i = 0; i < invocationNames.length; i++) { |
| 80 String name = invocationNames[i]; | 79 String name = invocationNames[i]; |
| 81 properties[i] = new jsAst.Property(js.string(name), js.number(1)); | 80 properties[i] = new jsAst.Property(js.string(name), js.number(1)); |
| 82 } | 81 } |
| 83 return new jsAst.ObjectInitializer(properties, isOneLiner: true); | 82 return new jsAst.ObjectInitializer(properties, isOneLiner: true); |
| 84 } | 83 } |
| 85 | 84 |
| 86 /** | 85 /** |
| 87 * Emit initializer for [mapTypeToInterceptor] data structure used by | 86 * Emit initializer for `typeToInterceptorMap` data structure used by |
| 88 * [findInterceptorForType]. See declaration of [mapTypeToInterceptor] in | 87 * `findInterceptorForType`. See declaration of `typeToInterceptor` in |
| 89 * `interceptors.dart`. | 88 * `interceptors.dart`. |
| 90 */ | 89 */ |
| 91 void emitMapTypeToInterceptor(CodeOutput output) { | 90 void emitTypeToInterceptorMap(CodeOutput output) { |
| 92 // TODO(sra): Perhaps inject a constant instead? | 91 InterceptorStubGenerator stubGenerator = |
| 93 CustomElementsAnalysis analysis = backend.customElementsAnalysis; | 92 new InterceptorStubGenerator(compiler, namer, backend); |
| 94 if (!analysis.needsTable) return; | 93 jsAst.Expression array = stubGenerator.generateTypeToInterceptorMap(); |
| 94 if (array == null) return; |
| 95 | 95 |
| 96 List<jsAst.Expression> elements = <jsAst.Expression>[]; | 96 jsAst.Expression typeToInterceptorMap = emitter |
| 97 JavaScriptConstantCompiler handler = backend.constants; | 97 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); |
| 98 List<ConstantValue> constants = | 98 jsAst.Expression assignment = js('# = #', [typeToInterceptorMap, array]); |
| 99 handler.getConstantsForEmission(emitter.compareConstants); | |
| 100 for (ConstantValue constant in constants) { | |
| 101 if (constant is TypeConstantValue) { | |
| 102 TypeConstantValue typeConstant = constant; | |
| 103 Element element = typeConstant.representedType.element; | |
| 104 if (element is ClassElement) { | |
| 105 ClassElement classElement = element; | |
| 106 if (!analysis.needsClass(classElement)) continue; | |
| 107 | |
| 108 elements.add(emitter.constantReference(constant)); | |
| 109 elements.add(backend.emitter.interceptorClassAccess(classElement)); | |
| 110 | |
| 111 // Create JavaScript Object map for by-name lookup of generative | |
| 112 // constructors. For example, the class A has three generative | |
| 113 // constructors | |
| 114 // | |
| 115 // class A { | |
| 116 // A() {} | |
| 117 // A.foo() {} | |
| 118 // A.bar() {} | |
| 119 // } | |
| 120 // | |
| 121 // Which are described by the map | |
| 122 // | |
| 123 // {"": A.A$, "foo": A.A$foo, "bar": A.A$bar} | |
| 124 // | |
| 125 // We expect most of the time the map will be a singleton. | |
| 126 var properties = []; | |
| 127 for (Element member in analysis.constructors(classElement)) { | |
| 128 properties.add( | |
| 129 new jsAst.Property( | |
| 130 js.string(member.name), | |
| 131 backend.emitter.staticFunctionAccess(member))); | |
| 132 } | |
| 133 | |
| 134 var map = new jsAst.ObjectInitializer(properties); | |
| 135 elements.add(map); | |
| 136 } | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 jsAst.ArrayInitializer array = new jsAst.ArrayInitializer(elements); | |
| 141 jsAst.Expression mapTypeToInterceptor = emitter | |
| 142 .generateEmbeddedGlobalAccess(embeddedNames.MAP_TYPE_TO_INTERCEPTOR); | |
| 143 jsAst.Expression assignment = js('# = #', [mapTypeToInterceptor, array]); | |
| 144 | 99 |
| 145 output.addBuffer(jsAst.prettyPrint(assignment, compiler)); | 100 output.addBuffer(jsAst.prettyPrint(assignment, compiler)); |
| 146 output.add(N); | 101 output.add(N); |
| 147 } | 102 } |
| 148 } | 103 } |
| OLD | NEW |