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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: lib/src/js_object_map.dart
diff --git a/lib/src/js_object_map.dart b/lib/src/js_object_map.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6de9f0de81934db9eab7d2c24b7e3bb568180eb7
--- /dev/null
+++ b/lib/src/js_object_map.dart
@@ -0,0 +1,69 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library js.js_object_map;
+
+import 'dart:collection' show Maps, MapMixin;
+import 'dart:js';
+
+import 'package:js/src/js_impl.dart';
+
+/**
+ * A [Map] interface wrapper for [JsObject]s.
+ *
+ * Keys must be [String] because they are used as JavaScript properties names.
+ *
+ * Values returned from this map are automatically converted to JavaScript with
+ * the [toJs] function when added, and to Dart with the [toDart] funtion when
+ * accessed.
+ */
+class JsObjectMap<V> extends MapMixin<String, V> {
+ static final _obj = context['Object'];
+
+ final JsObject _o;
+
+ JsObjectMap.fromJsObject(JsObject o) : _o = o;
+
+ @override
+ V operator [](String key) => toDart(_o[key]) as V;
+
+ @override
+ void operator []=(String key, V value) {
+ if (key == '__proto__') {
+ throw new ArgumentError("'__proto__' is disallowed as a key");
+ }
+ _o[key] = toJs(value);
+ }
+
+ @override
+ V remove(String key) {
+ final value = this[key];
+ _o.deleteProperty(key);
+ return value as V;
+ }
+
+ @override
+ Iterable<String> get keys => _obj.callMethod('keys', [_o]) as List<String>;
+
+ @override
+ bool containsKey(String key) => _o.hasProperty(key);
+
+ @override
+ V putIfAbsent(String key, V ifAbsent()) {
+ if (key == '__proto__') {
+ throw new ArgumentError("'__proto__' is disallowed as a key");
+ }
+ return Maps.putIfAbsent(this, key, ifAbsent) as V;
+ }
+
+ @override
+ void addAll(Map<String, V> other) {
+ if (other != null) {
+ other.forEach((k,v) => this[k] = v);
+ }
+ }
+
+ @override
+ void clear() => Maps.clear(this);
+}

Powered by Google App Engine
This is Rietveld 408576698