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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/js_names.dart

Issue 974803002: Defer addStubs to class instantiation time. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use JS_NAME and fix deferred loading. Created 5 years, 9 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 | Annotate | Revision Log
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 library dart._js_names; 5 library dart._js_names;
6 6
7 import 'dart:_js_embedded_names' show 7 import 'dart:_js_embedded_names' show
8 JsGetName, 8 JsGetName,
9 MANGLED_GLOBAL_NAMES, 9 MANGLED_GLOBAL_NAMES,
10 MANGLED_NAMES; 10 MANGLED_NAMES;
11 11
12 import 'dart:_foreign_helper' show 12 import 'dart:_foreign_helper' show
13 JS, 13 JS,
14 JS_EMBEDDED_GLOBAL, 14 JS_EMBEDDED_GLOBAL,
15 JS_GET_NAME; 15 JS_GET_NAME;
16 16
17 import 'dart:_js_helper' show 17 import 'dart:_js_helper' show
18 JsCache, 18 JsCache,
19 NoInline; 19 NoInline;
20 20
21 import 'dart:_interceptors' show JSArray; 21 import 'dart:_interceptors' show JSArray;
22 22
23 /// No-op method that is called to inform the compiler that unmangled named 23 /// No-op method that is called to inform the compiler that unmangled named
24 /// must be preserved. 24 /// must be preserved.
25 preserveNames() {} 25 preserveNames() {}
26 26
27 /// A map from mangled names to "reflective" names, that is, unmangled names 27 /// A map from mangled names to "reflective" names, that is, unmangled names
28 /// with some additional information, such as, number of required arguments. 28 /// with some additional information, such as, number of required arguments.
29 /// This map is for mangled names used as instance members. 29 /// This map is for mangled names used as instance members.
30 final Map<String, String> mangledNames = 30 final _LazyMangledNamesMap mangledNames = new _LazyMangledInstanceNamesMap(
31 computeMangledNames( 31 JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES));
32 JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES),
33 false);
34 32
35 /// A map from "reflective" names to mangled names (the reverse of 33 /// A map from "reflective" names to mangled names (the reverse of
36 /// [mangledNames]). 34 /// [mangledNames]).
37 final Map<String, String> reflectiveNames = 35 final _LazyReflectiveNamesMap reflectiveNames =
38 computeReflectiveNames(mangledNames); 36 new _LazyReflectiveNamesMap(JS_EMBEDDED_GLOBAL('=Object', MANGLED_NAMES),
37 true);
39 38
40 /// A map from mangled names to "reflective" names (see [mangledNames]). This 39 /// A map from mangled names to "reflective" names (see [mangledNames]). This
41 /// map is for globals, that is, static and top-level members. 40 /// map is for globals, that is, static and top-level members.
42 final Map<String, String> mangledGlobalNames = computeMangledNames( 41 final _LazyMangledNamesMap mangledGlobalNames = new _LazyMangledNamesMap(
43 JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES), 42 JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES));
44 true);
45 43
46 /// A map from "reflective" names to mangled names (the reverse of 44 /// A map from "reflective" names to mangled names (the reverse of
47 /// [mangledGlobalNames]). 45 /// [mangledGlobalNames]).
48 final Map<String, String> reflectiveGlobalNames = 46 final _LazyReflectiveNamesMap reflectiveGlobalNames =
49 computeReflectiveNames(mangledGlobalNames); 47 new _LazyReflectiveNamesMap(
48 JS_EMBEDDED_GLOBAL('=Object', MANGLED_GLOBAL_NAMES), false);
50 49
50 /// Implements a mapping from mangled names to their refletive counterparts.
floitsch 2015/03/11 13:59:34 reflective
herhut 2015/03/13 12:28:52 Done.
51 /// [jsMangledNames] is a JavaScript object literal. The keys are the mangled 51 /// [jsMangledNames] is a JavaScript object literal. The keys are the mangled
52 /// names, and the values are the "reflective" names. 52 /// names, and the values are the "reflective" names.
53 Map<String, String> computeMangledNames(jsMangledNames, bool isGlobal) { 53 class _LazyMangledNamesMap {
54 preserveNames(); 54 var _jsMangledNames;
floitsch 2015/03/11 13:59:34 Document what type it is here (and not in the dart
herhut 2015/03/13 12:28:52 Done.
55 var keys = extractKeys(jsMangledNames); 55
56 var result = <String, String>{}; 56 _LazyMangledNamesMap(this._jsMangledNames);
57 String getterPrefix = JS_GET_NAME(JsGetName.GETTER_PREFIX); 57
58 int getterPrefixLength = getterPrefix.length; 58 String operator[](String key) {
59 String setterPrefix = JS_GET_NAME(JsGetName.SETTER_PREFIX); 59 String result = JS('var', '#[#]', _jsMangledNames, key);
60 for (String key in keys) { 60 // Filter out all non-string values to protect against polution from
61 String value = JS('String', '#[#]', jsMangledNames, key); 61 // anciliary fields in [_jsMangledNames].
62 result[key] = value; 62 bool filter =
63 if (!isGlobal) { 63 JS('bool', 'typeof # !== "string"', result);
64 if (key.startsWith(getterPrefix)) { 64 return filter ? null : result;
floitsch 2015/03/11 13:59:34 You should type it, so that the inferrer realizes
herhut 2015/03/13 12:28:53 Done.
65 result['$setterPrefix${key.substring(getterPrefixLength)}'] = '$value='; 65 }
66 }
67
68 /// Extends [_LazyMangledNamesMap] with additional support for adding mappings
69 /// from mangled setter names to their reflective counterpart by rewriting a
70 /// corresponding entry for a getter name, if it exists.
71 class _LazyMangledInstanceNamesMap extends _LazyMangledNamesMap {
72 _LazyMangledInstanceNamesMap(_jsMangledNames) : super(_jsMangledNames);
73
74 String operator[](String key) {
75 String result = super[key];
76 String setterPrefix = JS_GET_NAME(JsGetName.SETTER_PREFIX);
77 if (result == null && key.startsWith(setterPrefix)) {
78 String getterPrefix = JS_GET_NAME(JsGetName.GETTER_PREFIX);
79 int setterPrefixLength = getterPrefix.length;
floitsch 2015/03/11 13:59:34 getterPrefixLength ? (If not explain)
herhut 2015/03/13 12:28:52 Done.
80
81 // Generate the setter name from the getter name.
82 key = '$getterPrefix${key.substring(setterPrefixLength)}';
83 result = super[key];
84 return (result != null) ? "${result}=" : null;
85 }
86 return result;
87 }
88 }
89
90 /// Implements the inverse of [_LazyMangledNamesMap]. As it would be too
91 /// expensive to seach the mangled names map for a value that corresponds to
92 /// the lookup key on each invocation, we compute the full mapping in demand
93 /// and cache it. The cache is invalidated when the underlying [_jsMangledNames]
94 /// object changes its lenght. This condition is sufficient as the name mapping
floitsch 2015/03/11 13:59:34 length
herhut 2015/03/13 12:28:52 Done.
95 /// can only grow over time.
96 /// When [_isInstance] is true, we also apply the inverse of the setter/getter
97 /// name conversion implemented by [_LazyMangledInstanceNamesMap].
98 class _LazyReflectiveNamesMap {
99 final _jsMangledNames;
floitsch 2015/03/11 13:59:34 Document what (type) this field is.
herhut 2015/03/13 12:28:52 Done.
100 final bool _isInstance;
101 int _cacheLength = 0;
102 Map<String, String> _cache;
103
104 _LazyReflectiveNamesMap(this._jsMangledNames, this._isInstance);
105
106 Map<String, String> _updateReflectiveNames() {
107 preserveNames();
108 Map<String, String> result = <String, String>{};
109 List keys = JS('List', 'Object.keys(#)', _jsMangledNames);
110 for (String key in keys) {
111 var reflectiveName = JS('var', '#[#]', _jsMangledNames, key);
112 // Filter out all non-string values to protect against polution from
113 // anciliary fields in [_jsMangledNames].
114 bool filter = JS('bool', '# == null || typeof # !== "string"',
115 reflectiveName, reflectiveName);
116 if (filter) continue;
117 result[reflectiveName] = key;
floitsch 2015/03/11 13:59:34 ditto: make sure that the type is known.
herhut 2015/03/13 12:28:52 Done.
118
119 String getterPrefix = JS_GET_NAME(JsGetName.GETTER_PREFIX);
120 if (_isInstance && key.startsWith(getterPrefix)) {
121 int getterPrefixLength = getterPrefix.length;
122 String setterPrefix = JS_GET_NAME(JsGetName.SETTER_PREFIX);
123 result['$reflectiveName='] =
124 '$setterPrefix${key.substring(getterPrefixLength)}';
66 } 125 }
67 } 126 }
127 return result;
68 } 128 }
69 return result;
70 }
71 129
72 Map<String, String> computeReflectiveNames(Map<String, String> map) { 130 int get _jsMangledNamesLength => JS('int', '#.length', _jsMangledNames);
73 preserveNames(); 131
74 var result = <String, String>{}; 132 String operator[](String key) {
75 map.forEach((String mangledName, String reflectiveName) { 133 if (_cache == null || _jsMangledNamesLength != _cacheLength) {
76 result[reflectiveName] = mangledName; 134 _cache = _updateReflectiveNames();
77 }); 135 _cacheLength = _jsMangledNamesLength;
78 return result; 136 }
137 return _cache[key];
138 }
79 } 139 }
80 140
81 @NoInline() 141 @NoInline()
82 List extractKeys(victim) { 142 List extractKeys(victim) {
83 var result = JS('', '# ? Object.keys(#) : []', victim, victim); 143 var result = JS('', '# ? Object.keys(#) : []', victim, victim);
84 return new JSArray.markFixed(result); 144 return new JSArray.markFixed(result);
85 } 145 }
86 146
87 /** 147 /**
88 * Returns the (global) unmangled version of [name]. 148 * Returns the (global) unmangled version of [name].
(...skipping 11 matching lines...) Expand all
100 return JsCache.fetch(names, name); 160 return JsCache.fetch(names, name);
101 } 161 }
102 162
103 String unmangleAllIdentifiersIfPreservedAnyways(String str) { 163 String unmangleAllIdentifiersIfPreservedAnyways(String str) {
104 return JS("String", 164 return JS("String",
105 r"(#).replace(/[^<,> ]+/g," 165 r"(#).replace(/[^<,> ]+/g,"
106 r"function(m) { return #[m] || m; })", 166 r"function(m) { return #[m] || m; })",
107 str, 167 str,
108 JS_EMBEDDED_GLOBAL('', MANGLED_GLOBAL_NAMES)); 168 JS_EMBEDDED_GLOBAL('', MANGLED_GLOBAL_NAMES));
109 } 169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698