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

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

Issue 1127533002: Add Map.unmodifiable constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 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/compiler/js_lib/constant_map.dart
diff --git a/sdk/lib/_internal/compiler/js_lib/constant_map.dart b/sdk/lib/_internal/compiler/js_lib/constant_map.dart
index e716cf14bf97d1e11e686f414fac5db8dbe281e6..c4c977a027b4dedaf67a11931a0f97d2d1c66e54 100644
--- a/sdk/lib/_internal/compiler/js_lib/constant_map.dart
+++ b/sdk/lib/_internal/compiler/js_lib/constant_map.dart
@@ -4,7 +4,47 @@
part of _js_helper;
+class ConstantMapView<K, V> extends UnmodifiableMapView
+ implements ConstantMap {
+ ConstantMapView(Map base) : super(base);
sra1 2015/05/07 19:24:42 indentation
+}
+
abstract class ConstantMap<K, V> implements Map<K, V> {
+ // Used to create unmodifiable maps from other maps.
+ factory ConstantMap.from(Map other) {
sra1 2015/05/07 19:24:41 Under what conditions can you return 'other'? Or c
+ List keys = other.keys.toList();
+ bool allStrings = true;
+ for (var k in keys) {
+ if (k is! String) {
+ allStrings = false;
+ break;
+ }
+ }
+ if (allStrings) {
+ bool containsProto = false;
+ var protoValue = null;
+ var object = JS('=Object', '{}');
+ int length = 0;
+ for (var k in keys) {
+ var v = other[k];
+ if (k != "__proto__") {
sra1 2015/05/07 19:24:42 Use single quotes for consistency with rest of fil
+ if (!jsHasOwnProperty(object, k)) length++;
+ JS("void", "#[#] = #", object, k, v);
sra1 2015/05/07 19:24:42 ditto
+ } else {
+ containsProto = true;
+ protoValue = v;
+ }
+ }
+ if (containsProto) {
+ length++;
+ return new ConstantProtoMap<K, V>._(length, object, keys, protoValue);
+ }
+ return new ConstantStringMap<K, V>._(length, object, keys);
+ }
+ // TODO(lrn): Make a proper unmodifiable map implementation.
+ return new ConstantMapView<K, V>(new Map.from(other));
+ }
+
const ConstantMap._();
bool get isEmpty => length == 0;
@@ -13,7 +53,7 @@ abstract class ConstantMap<K, V> implements Map<K, V> {
String toString() => Maps.mapToString(this);
- _throwUnmodifiable() {
+ static _throwUnmodifiable() {
throw new UnsupportedError("Cannot modify unmodifiable Map");
}
void operator []=(K key, V val) => _throwUnmodifiable();
@@ -26,8 +66,8 @@ abstract class ConstantMap<K, V> implements Map<K, V> {
class ConstantStringMap<K, V> extends ConstantMap<K, V>
implements _symbol_dev.EfficientLength {
- // This constructor is not used. The instantiation is shortcut by the
- // compiler. It is here to make the uninitialized final fields legal.
+ // This constructor is not used for actual compile-time constants.
+ // The instantiation of constant maps is shortcut by the compiler.
const ConstantStringMap._(this.length, this._jsObject, this._keys)
: super._();

Powered by Google App Engine
This is Rietveld 408576698