Chromium Code Reviews| Index: lib/src/js_impl.dart |
| diff --git a/lib/src/js_impl.dart b/lib/src/js_impl.dart |
| index a4cc4ab68db932a3e015cf58f7cf72cfbdb57e9f..d8b5d636816993275c4cae79ea39a0abfb630bb9 100644 |
| --- a/lib/src/js_impl.dart |
| +++ b/lib/src/js_impl.dart |
| @@ -9,6 +9,7 @@ |
| library js.impl; |
| import 'dart:js'; |
| +import 'package:js/src/js_object_map.dart'; |
| export 'dart:js' show context, JsObject; |
| const DART_OBJECT_PROPERTY = '__dart_object__'; |
| @@ -70,22 +71,42 @@ dynamic toJs(dynamic o) { |
| // Exported Dart Object -> JsObject |
| final Expando<JsObject> _exportedProxies = new Expando<JsObject>(); |
| -dynamic toDart(dynamic o) { |
| +/** |
| + * Converts a JS value (primitive or [JsObject]) to Dart. |
| + * |
| + * If [o] is a JS object with a associated Dart proxy class, an instance of that |
| + * proxy class is returned. If [o] is an exported Dart object, the original |
| + * Dart object is returned. The Dart object is stored as a reference on the |
| + * JS object so that the same Dart object is returned from subsequent calls |
| + * to [toDart]. |
| + * |
| + * If [o] is a JS object with no associated proxy class, the [fallbackType] is |
| + * used to create a transient wrapper of the correct type. Currently [Map] is |
| + * the only supported fallback type. [Future] and [Stream] are planned fallback |
| + * types. |
| + */ |
| +dynamic toDart(dynamic o, [Symbol fallbackType]) { |
| if (o == null) return o; |
| if (o is num || o is String || o is bool || o is DateTime) return o; |
| + if (o is JsArray) return o; |
|
alexandre.ardhuin
2014/10/01 20:36:51
We should have a JsList that would be a JsArray li
justinfagnani
2014/10/07 20:32:24
Of course. Adding that. thanks :)
|
| var wrapper = o[DART_OBJECT_PROPERTY]; |
|
alexandre.ardhuin
2014/10/01 20:36:51
Should a test that o is a JsObject be added ?
If n
justinfagnani
2014/10/07 20:32:24
Yes, I'll add that. We'll want to handle all the t
|
| if (wrapper == null) { |
| // look up JsInterface factory |
| var jsConstructor = o['constructor'] as JsObject; |
| var dartConstructor = _interfaceConstructors[jsConstructor]; |
| - if (dartConstructor == null) { |
| - throw new ArgumentError("Could not convert ${o.runtimeType}($o) to Dart"); |
| + if (dartConstructor != null) { |
| + wrapper = dartConstructor(o); |
| + o[DART_OBJECT_PROPERTY] = wrapper; |
| } |
| - wrapper = dartConstructor(o); |
| - o[DART_OBJECT_PROPERTY] = wrapper; |
| } |
| - return wrapper; |
| + if (wrapper != null) return wrapper; |
| + |
| + // no wrapper, handle fallback cases |
| + if (fallbackType == #Map) { |
| + return new JsObjectMap.fromJsObject(o); |
| + } |
| + throw new ArgumentError("Could not convert ${o.runtimeType}($o) to Dart"); |
| } |
| // Dart Type -> JS constructorfor proxy |