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

Side by Side Diff: lib/src/js_object_map.dart

Issue 615913004: Add @jsify annotation. Support automatically proxying Lists and Maps to Dart in Proxies. Support co… (Closed) Base URL: https://github.com/dart-lang/js-interop.git@master
Patch Set: Created 6 years, 2 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) 2014, 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 library js.js_object_map;
6
7 import 'dart:collection' show Maps, MapMixin;
8 import 'dart:js';
9
10 import 'package:js/src/js_impl.dart';
11
12 /**
13 * A [Map] interface wrapper for [JsObject]s.
14 *
15 * Keys must be [String] because they are used as JavaScript properties names.
16 *
17 * Values returned from this map are automatically converted to JavaScript with
18 * the [toJs] function when added, and to Dart with the [toDart] funtion when
19 * accessed.
20 */
21 class JsObjectMap<V> extends MapMixin<String, V> {
22 static final _obj = context['Object'];
23
24 final JsObject _o;
25
26 JsObjectMap.fromJsObject(JsObject o) : _o = o;
27
28 @override
29 V operator [](String key) => toDart(_o[key]) as V;
30
31 @override
32 void operator []=(String key, V value) {
33 if (key == '__proto__') {
34 throw new ArgumentError("'__proto__' is disallowed as a key");
35 }
36 _o[key] = toJs(value);
37 }
38
39 @override
40 V remove(String key) {
41 final value = this[key];
42 _o.deleteProperty(key);
43 return value as V;
44 }
45
46 @override
47 Iterable<String> get keys => _obj.callMethod('keys', [_o]) as List<String>;
48
49 @override
50 bool containsKey(String key) => _o.hasProperty(key);
51
52 @override
53 V putIfAbsent(String key, V ifAbsent()) {
54 if (key == '__proto__') {
55 throw new ArgumentError("'__proto__' is disallowed as a key");
56 }
57 return Maps.putIfAbsent(this, key, ifAbsent) as V;
58 }
59
60 @override
61 void addAll(Map<String, V> other) {
62 if (other != null) {
63 other.forEach((k,v) => this[k] = v);
64 }
65 }
66
67 @override
68 void clear() => Maps.clear(this);
69 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698