Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: pkg/compiler/lib/src/js_emitter/old_emitter/interceptor_emitter.dart

Issue 1198293002: dart2js: Use an abstract Name class for names in the generated JavaScript ast. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix new emitter. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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<jsAst.Name> interceptorInvocationNames =
9 new Set<jsAst.Name>();
9 10
10 void recordMangledNameOfMemberMethod(FunctionElement member, String name) { 11 void recordMangledNameOfMemberMethod(FunctionElement member,
12 jsAst.Name name) {
11 if (backend.isInterceptedMethod(member)) { 13 if (backend.isInterceptedMethod(member)) {
12 interceptorInvocationNames.add(name); 14 interceptorInvocationNames.add(name);
13 } 15 }
14 } 16 }
15 17
16 jsAst.Expression buildGetInterceptorMethod(String key, 18 jsAst.Expression buildGetInterceptorMethod(jsAst.Name key,
17 Set<ClassElement> classes) { 19 Set<ClassElement> classes) {
18 InterceptorStubGenerator stubGenerator = 20 InterceptorStubGenerator stubGenerator =
19 new InterceptorStubGenerator(compiler, namer, backend); 21 new InterceptorStubGenerator(compiler, namer, backend);
20 jsAst.Expression function = 22 jsAst.Expression function =
21 stubGenerator.generateGetInterceptorMethod(classes); 23 stubGenerator.generateGetInterceptorMethod(classes);
22 24
23 return function; 25 return function;
24 } 26 }
25 27
26 /** 28 /**
27 * Emit all versions of the [:getInterceptor:] method. 29 * Emit all versions of the [:getInterceptor:] method.
28 */ 30 */
29 jsAst.Statement buildGetInterceptorMethods() { 31 jsAst.Statement buildGetInterceptorMethods() {
30 List<jsAst.Statement> parts = <jsAst.Statement>[]; 32 List<jsAst.Statement> parts = <jsAst.Statement>[];
31 33
32 parts.add(js.comment('getInterceptor methods')); 34 parts.add(js.comment('getInterceptor methods'));
33 35
34 Map<String, Set<ClassElement>> specializedGetInterceptors = 36 Map<jsAst.Name, Set<ClassElement>> specializedGetInterceptors =
35 backend.specializedGetInterceptors; 37 backend.specializedGetInterceptors;
36 for (String name in specializedGetInterceptors.keys.toList()..sort()) { 38 List<jsAst.Name> names = specializedGetInterceptors.keys.toList()..sort();
floitsch 2015/06/22 17:43:44 I would prefer moving the sort into a separate lin
herhut 2015/06/23 13:26:31 Done.
39 for (jsAst.Name name in names) {
37 Set<ClassElement> classes = specializedGetInterceptors[name]; 40 Set<ClassElement> classes = specializedGetInterceptors[name];
38 parts.add( 41 parts.add(
39 js.statement('#.# = #', 42 js.statement('#.# = #',
40 [namer.globalObjectFor(backend.interceptorsLibrary), 43 [namer.globalObjectFor(backend.interceptorsLibrary),
41 name, 44 name,
42 buildGetInterceptorMethod(name, classes)])); 45 buildGetInterceptorMethod(name, classes)]));
43 } 46 }
44 47
45 return new jsAst.Block(parts); 48 return new jsAst.Block(parts);
46 } 49 }
47 50
48 jsAst.Statement buildOneShotInterceptors() { 51 jsAst.Statement buildOneShotInterceptors() {
49 List<jsAst.Statement> parts = <jsAst.Statement>[]; 52 List<jsAst.Statement> parts = <jsAst.Statement>[];
50 List<String> names = backend.oneShotInterceptors.keys.toList(); 53 Iterable<jsAst.Name> names
51 names.sort(); 54 = backend.oneShotInterceptors.keys.toList()..sort();
floitsch 2015/06/22 17:43:44 I would prefer keeping the sort in a separate line
herhut 2015/06/23 13:26:31 Done.
52 55
53 InterceptorStubGenerator stubGenerator = 56 InterceptorStubGenerator stubGenerator =
54 new InterceptorStubGenerator(compiler, namer, backend); 57 new InterceptorStubGenerator(compiler, namer, backend);
55 String globalObject = namer.globalObjectFor(backend.interceptorsLibrary); 58 String globalObject = namer.globalObjectFor(backend.interceptorsLibrary);
56 for (String name in names) { 59 for (jsAst.Name name in names) {
57 jsAst.Expression function = 60 jsAst.Expression function =
58 stubGenerator.generateOneShotInterceptor(name); 61 stubGenerator.generateOneShotInterceptor(name);
59 parts.add(js.statement('${globalObject}.# = #', [name, function])); 62 parts.add(js.statement('${globalObject}.# = #', [name, function]));
60 } 63 }
61 64
62 return new jsAst.Block(parts); 65 return new jsAst.Block(parts);
63 } 66 }
64 67
65 /** 68 /**
66 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the 69 * If [JSInvocationMirror._invokeOn] has been compiled, emit all the
67 * possible selector names that are intercepted into the 70 * possible selector names that are intercepted into the
68 * [interceptedNames] embedded global. The implementation of 71 * [interceptedNames] embedded global. The implementation of
69 * [_invokeOn] will use it to determine whether it should call the 72 * [_invokeOn] will use it to determine whether it should call the
70 * method with an extra parameter. 73 * method with an extra parameter.
71 */ 74 */
72 jsAst.ObjectInitializer generateInterceptedNamesSet() { 75 jsAst.ObjectInitializer generateInterceptedNamesSet() {
73 // We could also generate the list of intercepted names at 76 // We could also generate the list of intercepted names at
74 // runtime, by running through the subclasses of Interceptor 77 // runtime, by running through the subclasses of Interceptor
75 // (which can easily be identified). 78 // (which can easily be identified).
76 if (!compiler.enabledInvokeOn) return null; 79 if (!compiler.enabledInvokeOn) return null;
77 80
78 List<String> invocationNames = interceptorInvocationNames.toList()..sort(); 81 Iterable<jsAst.Name> invocationNames =
79 List<jsAst.Property> properties = 82 interceptorInvocationNames.toList()..sort();;
floitsch 2015/06/22 17:43:44 ditto.
herhut 2015/06/23 13:26:31 Done.
80 new List<jsAst.Property>(invocationNames.length); 83 List<jsAst.Property> properties = invocationNames.map((jsAst.Name name) {
81 for (int i = 0; i < invocationNames.length; i++) { 84 return new jsAst.Property(js.quoteName(name), js.number(1));
82 String name = invocationNames[i]; 85 }).toList();
83 properties[i] = new jsAst.Property(js.string(name), js.number(1));
84 }
85 return new jsAst.ObjectInitializer(properties, isOneLiner: true); 86 return new jsAst.ObjectInitializer(properties, isOneLiner: true);
86 } 87 }
87 88
88 /** 89 /**
89 * Emit initializer for `typeToInterceptorMap` data structure used by 90 * Emit initializer for `typeToInterceptorMap` data structure used by
90 * `findInterceptorForType`. See declaration of `typeToInterceptor` in 91 * `findInterceptorForType`. See declaration of `typeToInterceptor` in
91 * `interceptors.dart`. 92 * `interceptors.dart`.
92 */ 93 */
93 jsAst.Statement buildTypeToInterceptorMap(Program program) { 94 jsAst.Statement buildTypeToInterceptorMap(Program program) {
94 jsAst.Expression array = program.typeToInterceptorMap; 95 jsAst.Expression array = program.typeToInterceptorMap;
95 if (array == null) return js.comment("Empty type-to-interceptor map."); 96 if (array == null) return js.comment("Empty type-to-interceptor map.");
96 97
97 jsAst.Expression typeToInterceptorMap = emitter 98 jsAst.Expression typeToInterceptorMap = emitter
98 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP); 99 .generateEmbeddedGlobalAccess(embeddedNames.TYPE_TO_INTERCEPTOR_MAP);
99 return js.statement('# = #', [typeToInterceptorMap, array]); 100 return js.statement('# = #', [typeToInterceptorMap, array]);
100 } 101 }
101 } 102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698