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

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

Issue 1212513002: sdk files reorganization to make dart2js a proper package (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: renamed Created 5 years, 5 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
(Empty)
1 // Copyright (c) 2012, 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 _js_helper;
6
7 class ConstantMapView<K, V> extends UnmodifiableMapView
8 implements ConstantMap {
9 ConstantMapView(Map base) : super(base);
10 }
11
12 abstract class ConstantMap<K, V> implements Map<K, V> {
13 // Used to create unmodifiable maps from other maps.
14 factory ConstantMap.from(Map other) {
15 List keys = other.keys.toList();
16 bool allStrings = true;
17 for (var k in keys) {
18 if (k is! String) {
19 allStrings = false;
20 break;
21 }
22 }
23 if (allStrings) {
24 bool containsProto = false;
25 var protoValue = null;
26 var object = JS('=Object', '{}');
27 int length = 0;
28 for (var k in keys) {
29 var v = other[k];
30 if (k != "__proto__") {
31 if (!jsHasOwnProperty(object, k)) length++;
32 JS("void", "#[#] = #", object, k, v);
33 } else {
34 containsProto = true;
35 protoValue = v;
36 }
37 }
38 if (containsProto) {
39 length++;
40 return new ConstantProtoMap<K, V>._(length, object, keys, protoValue);
41 }
42 return new ConstantStringMap<K, V>._(length, object, keys);
43 }
44 // TODO(lrn): Make a proper unmodifiable map implementation.
45 return new ConstantMapView<K, V>(new Map.from(other));
46 }
47
48 const ConstantMap._();
49
50 bool get isEmpty => length == 0;
51
52 bool get isNotEmpty => !isEmpty;
53
54 String toString() => Maps.mapToString(this);
55
56 static _throwUnmodifiable() {
57 throw new UnsupportedError("Cannot modify unmodifiable Map");
58 }
59 void operator []=(K key, V val) => _throwUnmodifiable();
60 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable();
61 V remove(K key) => _throwUnmodifiable();
62 void clear() => _throwUnmodifiable();
63 void addAll(Map<K, V> other) => _throwUnmodifiable();
64 }
65
66 class ConstantStringMap<K, V> extends ConstantMap<K, V> {
67
68 // This constructor is not used for actual compile-time constants.
69 // The instantiation of constant maps is shortcut by the compiler.
70 const ConstantStringMap._(this.length, this._jsObject, this._keys)
71 : super._();
72
73 final int length;
74 // A constant map is backed by a JavaScript object.
75 final _jsObject;
76 final List<K> _keys;
77
78 bool containsValue(V needle) {
79 return values.any((V value) => value == needle);
80 }
81
82 bool containsKey(Object key) {
83 if (key is! String) return false;
84 if ('__proto__' == key) return false;
85 return jsHasOwnProperty(_jsObject, key);
86 }
87
88 V operator [](Object key) {
89 if (!containsKey(key)) return null;
90 return _fetch(key);
91 }
92
93 // [_fetch] is the indexer for keys for which `containsKey(key)` is true.
94 _fetch(key) => jsPropertyAccess(_jsObject, key);
95
96 void forEach(void f(K key, V value)) {
97 // Use a JS 'cast' to get efficient loop. Type inferrence doesn't get this
98 // since constant map representation is chosen after type inferrence and the
99 // instantiation is shortcut by the compiler.
100 var keys = JS('JSArray', '#', _keys);
101 for (int i = 0; i < keys.length; i++) {
102 var key = keys[i];
103 f(key, _fetch(key));
104 }
105 }
106
107 Iterable<K> get keys {
108 return new _ConstantMapKeyIterable<K>(this);
109 }
110
111 Iterable<V> get values {
112 return new MappedIterable<K, V>(_keys, (key) => _fetch(key));
113 }
114 }
115
116 class ConstantProtoMap<K, V> extends ConstantStringMap<K, V> {
117 // This constructor is not used. The instantiation is shortcut by the
118 // compiler. It is here to make the uninitialized final fields legal.
119 ConstantProtoMap._(length, jsObject, keys, this._protoValue)
120 : super._(length, jsObject, keys);
121
122 final V _protoValue;
123
124 bool containsKey(Object key) {
125 if (key is! String) return false;
126 if ('__proto__' == key) return true;
127 return jsHasOwnProperty(_jsObject, key);
128 }
129
130 _fetch(key) =>
131 '__proto__' == key ? _protoValue : jsPropertyAccess(_jsObject, key);
132 }
133
134 class _ConstantMapKeyIterable<K> extends Iterable<K> {
135 ConstantStringMap<K, dynamic> _map;
136 _ConstantMapKeyIterable(this._map);
137
138 Iterator<K> get iterator => _map._keys.iterator;
139
140 int get length => _map._keys.length;
141 }
142
143 class GeneralConstantMap<K, V> extends ConstantMap<K, V> {
144 // This constructor is not used. The instantiation is shortcut by the
145 // compiler. It is here to make the uninitialized final fields legal.
146 GeneralConstantMap(this._jsData) : super._();
147
148 // [_jsData] holds a key-value pair list.
149 final _jsData;
150
151 // We cannot create the backing map on creation since hashCode interceptors
152 // have not been defined when constants are created.
153 Map<K, V> _getMap() {
154 LinkedHashMap<K, V> backingMap = JS('LinkedHashMap|Null', r'#.$map', this);
155 if (backingMap == null) {
156 backingMap = new JsLinkedHashMap<K, V>();
157 fillLiteralMap(_jsData, backingMap);
158 JS('', r'#.$map = #', this, backingMap);
159 }
160 return backingMap;
161 }
162
163 bool containsValue(V needle) {
164 return _getMap().containsValue(needle);
165 }
166
167 bool containsKey(Object key) {
168 return _getMap().containsKey(key);
169 }
170
171 V operator [](Object key) {
172 return _getMap()[key];
173 }
174
175 void forEach(void f(K key, V value)) {
176 _getMap().forEach(f);
177 }
178
179 Iterable<K> get keys {
180 return _getMap().keys;
181 }
182
183 Iterable<V> get values {
184 return _getMap().values;
185 }
186
187 int get length => _getMap().length;
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698