Skip to content

Commit

Permalink
Implement Map.unmodifiable
Browse files Browse the repository at this point in the history
This is a temporary solution.
When we have an implementation of const maps, we should switch to that.

BUG=
[email protected]

Review URL: https://codereview.chromium.org/1946663002 .
  • Loading branch information
rakudrama committed May 3, 2016
1 parent dc8e00b commit 6485751
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/dev_compiler/lib/runtime/dart_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -27246,6 +27246,9 @@ dart_library.library('dart_sdk', null, /* Imports */[
static from(other) {
return collection.LinkedHashMap$(K, V).from(other);
}
static unmodifiable(other) {
return new (collection.UnmodifiableMapView$(K, V))(core.Map$(K, V).from(other));
}
static identity() {
return collection.LinkedHashMap$(K, V).identity();
}
Expand All @@ -27260,6 +27263,7 @@ dart_library.library('dart_sdk', null, /* Imports */[
constructors: () => ({
new: [core.Map$(K, V), []],
from: [core.Map$(K, V), [core.Map]],
unmodifiable: [core.Map$(K, V), [core.Map]],
identity: [core.Map$(K, V), []],
fromIterable: [core.Map$(K, V), [core.Iterable], {key: dart.functionType(K, [dart.dynamic]), value: dart.functionType(V, [dart.dynamic])}],
fromIterables: [core.Map$(K, V), [core.Iterable$(K), core.Iterable$(V)]]
Expand Down
18 changes: 17 additions & 1 deletion pkg/dev_compiler/tool/input_sdk/lib/core/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ abstract class Map<K, V> {
*/
factory Map.from(Map other) = LinkedHashMap<K, V>.from;

/**
* Creates an unmodifiable hash based map containing the entries of [other].
*
* The keys must all be assignable to [K] and the values to [V].
* The [other] map itself can have any type.
*
* The map requires the keys to implement compatible
* `operator==` and `hashCode`, and it allows `null` as a key.
* The created map iterates keys in a fixed order,
* preserving the order provided by [other].
*
* The resulting map behaves like the result of [Map.from],
* except that the map returned by this constructor is not modifiable.
*/
external factory Map.unmodifiable(Map other);

/**
* Creates an identity map with the default implementation, [LinkedHashMap].
*
Expand Down Expand Up @@ -133,7 +149,7 @@ abstract class Map<K, V> {
/**
* Returns true if this map contains the given [key].
*
* Returns true if any of the keys in the map ar equal to `key`
* Returns true if any of the keys in the map are equal to `key`
* according to the equality used by the map.
*/
bool containsKey(Object key);
Expand Down
7 changes: 7 additions & 0 deletions pkg/dev_compiler/tool/input_sdk/patch/core_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ class List<E> {
}
}

@patch
class Map<K, V> {
@patch
factory Map.unmodifiable(Map other) {
return new UnmodifiableMapView<K, V>(new Map<K, V>.from(other));
}
}

@patch
class String {
Expand Down

0 comments on commit 6485751

Please sign in to comment.