| 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) { |
| 11 if (backend.isInterceptedMethod(member)) { | 11 if (backend.isInterceptedMethod(member)) { |
| 12 interceptorInvocationNames.add(name); | 12 interceptorInvocationNames.add(name); |
| 13 } | 13 } |
| 14 } | 14 } |
| 15 | 15 |
| 16 void emitGetInterceptorMethod(CodeOutput output, | 16 jsAst.Expression buildGetInterceptorMethod(String key, |
| 17 String key, | 17 Set<ClassElement> classes) { |
| 18 Set<ClassElement> classes) { | |
| 19 InterceptorStubGenerator stubGenerator = | 18 InterceptorStubGenerator stubGenerator = |
| 20 new InterceptorStubGenerator(compiler, namer, backend); | 19 new InterceptorStubGenerator(compiler, namer, backend); |
| 21 jsAst.Expression function = | 20 jsAst.Expression function = |
| 22 stubGenerator.generateGetInterceptorMethod(classes); | 21 stubGenerator.generateGetInterceptorMethod(classes); |
| 23 | 22 |
| 24 output.addBuffer(jsAst.prettyPrint( | 23 return function; |
| 25 js('${namer.globalObjectFor(backend.interceptorsLibrary)}.# = #', | |
| 26 [key, function]), | |
| 27 compiler)); | |
| 28 output.add(N); | |
| 29 } | 24 } |
| 30 | 25 |
| 31 /** | 26 /** |
| 32 * Emit all versions of the [:getInterceptor:] method. | 27 * Emit all versions of the [:getInterceptor:] method. |
| 33 */ | 28 */ |
| 34 void emitGetInterceptorMethods(CodeOutput output) { | 29 jsAst.Statement buildGetInterceptorMethods() { |
| 35 emitter.addComment('getInterceptor methods', output); | 30 List<jsAst.Statement> parts = <jsAst.Statement>[]; |
| 31 |
| 32 parts.add(js.comment('getInterceptor methods')); |
| 33 |
| 36 Map<String, Set<ClassElement>> specializedGetInterceptors = | 34 Map<String, Set<ClassElement>> specializedGetInterceptors = |
| 37 backend.specializedGetInterceptors; | 35 backend.specializedGetInterceptors; |
| 38 for (String name in specializedGetInterceptors.keys.toList()..sort()) { | 36 for (String name in specializedGetInterceptors.keys.toList()..sort()) { |
| 39 Set<ClassElement> classes = specializedGetInterceptors[name]; | 37 Set<ClassElement> classes = specializedGetInterceptors[name]; |
| 40 emitGetInterceptorMethod(output, name, classes); | 38 parts.add( |
| 39 js.statement('#.# = #', |
| 40 [namer.globalObjectFor(backend.interceptorsLibrary), |
| 41 name, |
| 42 buildGetInterceptorMethod(name, classes)])); |
| 41 } | 43 } |
| 44 |
| 45 return new jsAst.Block(parts); |
| 42 } | 46 } |
| 43 | 47 |
| 44 void emitOneShotInterceptors(CodeOutput output) { | 48 jsAst.Statement buildOneShotInterceptors() { |
| 49 List<jsAst.Statement> parts = <jsAst.Statement>[]; |
| 45 List<String> names = backend.oneShotInterceptors.keys.toList(); | 50 List<String> names = backend.oneShotInterceptors.keys.toList(); |
| 46 names.sort(); | 51 names.sort(); |
| 47 | 52 |
| 48 InterceptorStubGenerator stubGenerator = | 53 InterceptorStubGenerator stubGenerator = |
| 49 new InterceptorStubGenerator(compiler, namer, backend); | 54 new InterceptorStubGenerator(compiler, namer, backend); |
| 50 String globalObject = namer.globalObjectFor(backend.interceptorsLibrary); | 55 String globalObject = namer.globalObjectFor(backend.interceptorsLibrary); |
| 51 for (String name in names) { | 56 for (String name in names) { |
| 52 jsAst.Expression function = | 57 jsAst.Expression function = |
| 53 stubGenerator.generateOneShotInterceptor(name); | 58 stubGenerator.generateOneShotInterceptor(name); |
| 54 jsAst.Expression assignment = | 59 parts.add(js.statement('${globalObject}.# = #', [name, function])); |
| 55 js('${globalObject}.# = #', [name, function]); | 60 } |
| 56 | 61 |
| 57 output.addBuffer(jsAst.prettyPrint(assignment, compiler)); | 62 return new jsAst.Block(parts); |
| 58 output.add(N); | |
| 59 } | |
| 60 } | 63 } |
| 61 | 64 |
| 62 /** | 65 /** |
| 63 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the | 66 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the |
| 64 * possible selector names that are intercepted into the | 67 * possible selector names that are intercepted into the |
| 65 * [interceptedNames] embedded global. The implementation of | 68 * [interceptedNames] embedded global. The implementation of |
| 66 * [_invokeOn] will use it to determine whether it should call the | 69 * [_invokeOn] will use it to determine whether it should call the |
| 67 * method with an extra parameter. | 70 * method with an extra parameter. |
| 68 */ | 71 */ |
| 69 jsAst.ObjectInitializer generateInterceptedNamesSet() { | 72 jsAst.ObjectInitializer generateInterceptedNamesSet() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 80 properties[i] = new jsAst.Property(js.string(name), js.number(1)); | 83 properties[i] = new jsAst.Property(js.string(name), js.number(1)); |
| 81 } | 84 } |
| 82 return new jsAst.ObjectInitializer(properties, isOneLiner: true); | 85 return new jsAst.ObjectInitializer(properties, isOneLiner: true); |
| 83 } | 86 } |
| 84 | 87 |
| 85 /** | 88 /** |
| 86 * Emit initializer for `typeToInterceptorMap` data structure used by | 89 * Emit initializer for `typeToInterceptorMap` data structure used by |
| 87 * `findInterceptorForType`. See declaration of `typeToInterceptor` in | 90 * `findInterceptorForType`. See declaration of `typeToInterceptor` in |
| 88 * `interceptors.dart`. | 91 * `interceptors.dart`. |
| 89 */ | 92 */ |
| 90 void emitTypeToInterceptorMap(Program program, CodeOutput output) { | 93 jsAst.Statement buildTypeToInterceptorMap(Program program) { |
| 91 jsAst.Expression array = program.typeToInterceptorMap; | 94 jsAst.Expression array = program.typeToInterceptorMap; |
| 92 if (array == null) return; | 95 if (array == null) return js.comment("Empty type-to-interceptor map."); |
| 93 | 96 |
| 94 jsAst.Expression typeToInterceptorMap = emitter | 97 jsAst.Expression typeToInterceptorMap = emitter |
| 95 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); | 98 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); |
| 96 jsAst.Expression assignment = js('# = #', [typeToInterceptorMap, array]); | 99 return js.statement('# = #', [typeToInterceptorMap, array]); |
| 97 | |
| 98 output.addBuffer(jsAst.prettyPrint(assignment, compiler)); | |
| 99 output.add(N); | |
| 100 } | 100 } |
| 101 } | 101 } |
| OLD | NEW |