OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Utility methods to efficiently manipulate typed JSInterop objects in cases | 5 /// Utility methods to efficiently manipulate typed JSInterop objects in cases |
6 /// where the name to call is not known at runtime. You should only use these | 6 /// where the name to call is not known at runtime. You should only use these |
7 /// methods when the same effect cannot be achieved with @JS annotations. | 7 /// methods when the same effect cannot be achieved with @JS annotations. |
8 /// These methods would be extension methods on JSObject if Dart supported | 8 /// These methods would be extension methods on JSObject if Dart supported |
9 /// extension methods. | 9 /// extension methods. |
10 library dart.js_util; | 10 library dart.js_util; |
11 | 11 |
12 import 'dart:_foreign_helper' show JS; | 12 import 'dart:_foreign_helper' show JS; |
13 import 'dart:collection' show HashMap; | 13 import 'dart:collection' show HashMap; |
14 | 14 |
15 /// WARNING: performance of this method is much worse than other uitil | 15 /// WARNING: performance of this method is much worse than other util |
16 /// methods in this library. Only use this method as a last resort. | 16 /// methods in this library. Only use this method as a last resort. |
17 /// | 17 /// |
18 /// Recursively converts a JSON-like collection of Dart objects to a | 18 /// Recursively converts a JSON-like collection of Dart objects to a |
19 /// collection of JavaScript objects and returns a [JsObject] proxy to it. | 19 /// collection of JavaScript objects and returns a [JsObject] proxy to it. |
20 /// | 20 /// |
21 /// [object] must be a [Map] or [Iterable], the contents of which are also | 21 /// [object] must be a [Map] or [Iterable], the contents of which are also |
22 /// converted. Maps and Iterables are copied to a new JavaScript object. | 22 /// converted. Maps and Iterables are copied to a new JavaScript object. |
23 /// Primitives and other transferrable values are directly converted to their | 23 /// Primitives and other transferable values are directly converted to their |
24 /// JavaScript type, and all other objects are proxied. | 24 /// JavaScript type, and all other objects are proxied. |
25 jsify(object) { | 25 jsify(object) { |
26 if ((object is! Map) && (object is! Iterable)) { | 26 if ((object is! Map) && (object is! Iterable)) { |
27 throw new ArgumentError("object must be a Map or Iterable"); | 27 throw new ArgumentError("object must be a Map or Iterable"); |
28 } | 28 } |
29 return _convertDataTree(object); | 29 return _convertDataTree(object); |
30 } | 30 } |
31 | 31 |
32 _convertDataTree(data) { | 32 _convertDataTree(data) { |
33 var _convertedObjects = new HashMap.identity(); | 33 var _convertedObjects = new HashMap.identity(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // object for which there is an interceptor | 117 // object for which there is an interceptor |
118 return JS('Object', 'new #()', factoryFunction); | 118 return JS('Object', 'new #()', factoryFunction); |
119 | 119 |
120 // TODO(sra): Investigate: | 120 // TODO(sra): Investigate: |
121 // | 121 // |
122 // var jsObj = JS('', 'Object.create(#.prototype)', constr); | 122 // var jsObj = JS('', 'Object.create(#.prototype)', constr); |
123 // JS('', '#.apply(#, #)', constr, jsObj, | 123 // JS('', '#.apply(#, #)', constr, jsObj, |
124 // []..addAll(arguments.map(_convertToJS))); | 124 // []..addAll(arguments.map(_convertToJS))); |
125 // return _wrapToDart(jsObj); | 125 // return _wrapToDart(jsObj); |
126 } | 126 } |
OLD | NEW |