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

Unified Diff: sdk/lib/_internal/lib/constant_map.dart

Issue 22909056: Support general expressions as keys in literal maps. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/lib/constant_map.dart
diff --git a/sdk/lib/_internal/lib/constant_map.dart b/sdk/lib/_internal/lib/constant_map.dart
index 60300fc39e4d42105da7097350cc7255be4c1399..677dc16b78c70e040627e464d99ba8aabfa71c5e 100644
--- a/sdk/lib/_internal/lib/constant_map.dart
+++ b/sdk/lib/_internal/lib/constant_map.dart
@@ -4,9 +4,26 @@
part of _js_helper;
+abstract class ConstantMap<K, V> implements Map<K, V> {
+ bool get isEmpty => length == 0;
+
+ bool get isNotEmpty => !isEmpty;
+
+ String toString() => Maps.mapToString(this);
+
+ _throwUnmodifiable() {
+ throw new UnsupportedError("Cannot modify unmodifiable Map");
+ }
+ void operator []=(K key, V val) => _throwUnmodifiable();
+ V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable();
+ V remove(K key) => _throwUnmodifiable();
+ void clear() => _throwUnmodifiable();
+ void addAll(Map<K, V> other) => _throwUnmodifiable();
+}
+
// This class has no constructor. This is on purpose since the instantiation
// is shortcut by the compiler.
-class ConstantMap<V> implements Map<String, V> {
+class ConstantStringMap<K, V> extends ConstantMap<K, V> {
ngeoffray 2013/09/13 07:30:15 Why do you need K now?
Johnni Winther 2013/09/18 12:37:21 If you write {"0": 0} K must be dynamic and not St
final int length;
// A constant map is backed by a JavaScript object.
final _jsObject;
@@ -16,63 +33,91 @@ class ConstantMap<V> implements Map<String, V> {
return values.any((V value) => value == needle);
}
- bool containsKey(String key) {
+ bool containsKey(K key) {
ngeoffray 2013/09/13 07:30:15 containsKey takes an Object.
Johnni Winther 2013/09/18 12:37:21 Yay!
+ if (key is! String) return false;
if (key == '__proto__') return false;
- return jsHasOwnProperty(_jsObject, key);
+ var stringKey = key;
+ return jsHasOwnProperty(_jsObject, stringKey);
}
- V operator [](String key) {
+ V operator [](K key) {
ngeoffray 2013/09/13 07:30:15 ditto
Johnni Winther 2013/09/18 12:37:21 ditto
+ if (key is! String) return null;
if (!containsKey(key)) return null;
- return jsPropertyAccess(_jsObject, key);
+ var stringKey = key;
+ return jsPropertyAccess(_jsObject, stringKey);
}
- void forEach(void f(String key, V value)) {
- _keys.forEach((String key) => f(key, this[key]));
+ void forEach(void f(K key, V value)) {
+ _keys.forEach((key) => f(key, this[key]));
}
- Iterable<String> get keys {
- return new _ConstantMapKeyIterable(this);
+ Iterable<K> get keys {
+ return new _ConstantMapKeyIterable<K>(this);
}
Iterable<V> get values {
- return _keys.map((String key) => this[key]);
- }
-
- bool get isEmpty => length == 0;
-
- bool get isNotEmpty => !isEmpty;
-
- String toString() => Maps.mapToString(this);
-
- _throwUnmodifiable() {
- throw new UnsupportedError("Cannot modify unmodifiable Map");
+ return _keys.map((key) => this[key]);
}
- void operator []=(String key, V val) => _throwUnmodifiable();
- V putIfAbsent(String key, V ifAbsent()) => _throwUnmodifiable();
- V remove(String key) => _throwUnmodifiable();
- void clear() => _throwUnmodifiable();
- void addAll(Map<String, V> other) => _throwUnmodifiable();
}
// This class has no constructor. This is on purpose since the instantiation
// is shortcut by the compiler.
-class ConstantProtoMap<V> extends ConstantMap<V> {
+class ConstantProtoMap<K, V> extends ConstantStringMap<K, V> {
final V _protoValue;
- bool containsKey(String key) {
+ bool containsKey(K key) {
ngeoffray 2013/09/13 07:30:15 Object
Johnni Winther 2013/09/18 12:37:21 Done.
if (key == '__proto__') return true;
return super.containsKey(key);
}
- V operator [](String key) {
+ V operator [](K key) {
ngeoffray 2013/09/13 07:30:15 Object
Johnni Winther 2013/09/18 12:37:21 Done.
if (key == '__proto__') return _protoValue;
return super[key];
}
}
-class _ConstantMapKeyIterable extends IterableBase<String> {
+class _ConstantMapKeyIterable<K> extends IterableBase<K> {
ConstantMap _map;
_ConstantMapKeyIterable(this._map);
- Iterator<String> get iterator => _map._keys.iterator;
+ Iterator<K> get iterator => _map._keys.iterator;
+}
+
+// This class has no constructor. This is on purpose since the instantiation
+// is shortcut by the compiler.
+class GeneralConstantMap<K, V> extends ConstantMap<K, V> {
+ final _jsData;
ngeoffray 2013/09/13 07:30:15 Add comment on _jsData. That's a List right?
Johnni Winther 2013/09/18 12:37:21 Done.
+
+ // We cannot create the backing map on creation since hashCode interceptors
ngeoffray 2013/09/13 07:30:15 hashCode is user-defined, and therefore should not
Johnni Winther 2013/09/18 12:37:21 Done.
+ // have not been defined when constants are created.
+ Map<K, V> _getMap() {
+ if (JS('bool', r'!this.$map')) {
+ JS('', r'this.$map = #', makeLiteralMap(_jsData));
+ }
+ return JS('Map', r'this.$map');
+ }
+
+ bool containsValue(V needle) {
+ return _getMap().containsValue(needle);
+ }
+
+ bool containsKey(K key) {
+ return _getMap().containsKey(key);
+ }
+
+ V operator [](String key) {
+ return _getMap()[key];
+ }
+
+ void forEach(void f(K key, V value)) {
+ _getMap().forEach(f);
+ }
+
+ Iterable<K> get keys {
+ return _getMap().keys;
+ }
+
+ Iterable<V> get values {
+ return _getMap().values;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698