| OLD | NEW |
| (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 abstract class ConstantMap<K, V> implements Map<K, V> { | |
| 8 const ConstantMap._(); | |
| 9 | |
| 10 bool get isEmpty => length == 0; | |
| 11 | |
| 12 bool get isNotEmpty => !isEmpty; | |
| 13 | |
| 14 String toString() => Maps.mapToString(this); | |
| 15 | |
| 16 _throwUnmodifiable() { | |
| 17 throw new UnsupportedError("Cannot modify unmodifiable Map"); | |
| 18 } | |
| 19 void operator []=(K key, V val) => _throwUnmodifiable(); | |
| 20 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable(); | |
| 21 V remove(Object key) => _throwUnmodifiable(); | |
| 22 void clear() => _throwUnmodifiable(); | |
| 23 void addAll(Map<K, V> other) => _throwUnmodifiable(); | |
| 24 } | |
| 25 | |
| 26 class ConstantStringMap<K, V> extends ConstantMap<K, V> | |
| 27 implements _symbol_dev.EfficientLength { | |
| 28 | |
| 29 // This constructor is not used. The instantiation is shortcut by the | |
| 30 // compiler. It is here to make the uninitialized final fields legal. | |
| 31 const ConstantStringMap._(this.length, this._jsObject, this._keys) | |
| 32 : super._(); | |
| 33 | |
| 34 final int length; | |
| 35 // A constant map is backed by a JavaScript object. | |
| 36 final _jsObject; | |
| 37 final List<K> _keys; | |
| 38 | |
| 39 bool containsValue(Object needle) { | |
| 40 return values.any((V value) => value == needle); | |
| 41 } | |
| 42 | |
| 43 bool containsKey(Object key) { | |
| 44 if (key is! String) return false; | |
| 45 if ('__proto__' == key) return false; | |
| 46 return jsHasOwnProperty(_jsObject, key); | |
| 47 } | |
| 48 | |
| 49 V operator [](Object key) { | |
| 50 if (!containsKey(key)) return null; | |
| 51 return _fetch(key); | |
| 52 } | |
| 53 | |
| 54 // [_fetch] is the indexer for keys for which `containsKey(key)` is true. | |
| 55 _fetch(key) => jsPropertyAccess(_jsObject, key); | |
| 56 | |
| 57 void forEach(void f(K key, V value)) { | |
| 58 // Use a JS 'cast' to get efficient loop. Type inferrence doesn't get this | |
| 59 // since constant map representation is chosen after type inferrence and the | |
| 60 // instantiation is shortcut by the compiler. | |
| 61 var keys = JS('JSArray', '#', _keys); | |
| 62 for (int i = 0; i < keys.length; i++) { | |
| 63 var key = keys[i]; | |
| 64 f(key, _fetch(key)); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 Iterable<K> get keys { | |
| 69 return new _ConstantMapKeyIterable<K>(this); | |
| 70 } | |
| 71 | |
| 72 Iterable<V> get values { | |
| 73 return new MappedIterable<K, V>(_keys, (key) => _fetch(key)); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 class ConstantProtoMap<K, V> extends ConstantStringMap<K, V> { | |
| 78 // This constructor is not used. The instantiation is shortcut by the | |
| 79 // compiler. It is here to make the uninitialized final fields legal. | |
| 80 ConstantProtoMap._(length, jsObject, keys, this._protoValue) | |
| 81 : super._(length, jsObject, keys); | |
| 82 | |
| 83 final V _protoValue; | |
| 84 | |
| 85 bool containsKey(Object key) { | |
| 86 if (key is! String) return false; | |
| 87 if ('__proto__' == key) return true; | |
| 88 return jsHasOwnProperty(_jsObject, key); | |
| 89 } | |
| 90 | |
| 91 _fetch(key) => | |
| 92 '__proto__' == key ? _protoValue : jsPropertyAccess(_jsObject, key); | |
| 93 } | |
| 94 | |
| 95 class _ConstantMapKeyIterable<K> extends IterableBase<K> { | |
| 96 ConstantStringMap<K, dynamic> _map; | |
| 97 _ConstantMapKeyIterable(this._map); | |
| 98 | |
| 99 Iterator<K> get iterator => _map._keys.iterator; | |
| 100 | |
| 101 int get length => _map._keys.length; | |
| 102 } | |
| 103 | |
| 104 class GeneralConstantMap<K, V> extends ConstantMap<K, V> { | |
| 105 // This constructor is not used. The instantiation is shortcut by the | |
| 106 // compiler. It is here to make the uninitialized final fields legal. | |
| 107 GeneralConstantMap(this._jsData) : super._(); | |
| 108 | |
| 109 // [_jsData] holds a key-value pair list. | |
| 110 final _jsData; | |
| 111 | |
| 112 // We cannot create the backing map on creation since hashCode interceptors | |
| 113 // have not been defined when constants are created. | |
| 114 Map<K, V> _getMap() { | |
| 115 if (JS('bool', r'!this.$map')) { | |
| 116 Map backingMap = new LinkedHashMap<K, V>(); | |
| 117 JS('', r'this.$map = #', fillLiteralMap(_jsData, backingMap)); | |
| 118 } | |
| 119 return JS('Map<K, V>', r'this.$map'); | |
| 120 } | |
| 121 | |
| 122 bool containsValue(Object needle) { | |
| 123 return _getMap().containsValue(needle); | |
| 124 } | |
| 125 | |
| 126 bool containsKey(Object key) { | |
| 127 return _getMap().containsKey(key); | |
| 128 } | |
| 129 | |
| 130 V operator [](Object key) { | |
| 131 return _getMap()[key]; | |
| 132 } | |
| 133 | |
| 134 void forEach(void f(K key, V value)) { | |
| 135 _getMap().forEach(f); | |
| 136 } | |
| 137 | |
| 138 Iterable<K> get keys { | |
| 139 return _getMap().keys; | |
| 140 } | |
| 141 | |
| 142 Iterable<V> get values { | |
| 143 return _getMap().values; | |
| 144 } | |
| 145 | |
| 146 int get length => _getMap().length; | |
| 147 } | |
| OLD | NEW |