OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library polymer.lib.src.common.js_proxy; | 4 library polymer.lib.src.common.js_proxy; |
5 | 5 |
6 import 'dart:js'; | 6 import 'dart:js'; |
7 import 'package:reflectable/reflectable.dart'; | 7 import 'package:reflectable/reflectable.dart'; |
8 import 'behavior.dart'; | |
8 import 'declarations.dart'; | 9 import 'declarations.dart'; |
9 | 10 |
10 // Mixin this class to get js proxy support! | 11 // Mixin this class to get js proxy support! |
11 abstract class JsProxy { | 12 abstract class JsProxy { |
12 /// Lazily create proxy constructors! | 13 /// Lazily create proxy constructors! |
13 static Map<Type, JsFunction> _jsProxyConstructors = {}; | 14 static Map<Type, JsFunction> _jsProxyConstructors = {}; |
14 | 15 |
15 /// Never reads from the dart object, instead reads properties from the | 16 /// Never reads from the dart object, instead reads properties from the |
16 /// `__cache__` object. This is primarily useful for objects that you pass in | 17 /// `__cache__` object. This is primarily useful for objects that you pass in |
17 /// empty, and the javascript code will populate. It should be used carefully | 18 /// empty, and the javascript code will populate. It should be used carefully |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 const jsProxyReflectable = const JsProxyReflectable(); | 53 const jsProxyReflectable = const JsProxyReflectable(); |
53 | 54 |
54 final JsObject _polymerDart = context['Polymer']['Dart']; | 55 final JsObject _polymerDart = context['Polymer']['Dart']; |
55 | 56 |
56 /// Given a dart type, this creates a javascript constructor and prototype | 57 /// Given a dart type, this creates a javascript constructor and prototype |
57 /// which can act as a proxy for it. | 58 /// which can act as a proxy for it. |
58 JsFunction _buildJsConstructorForType(Type dartType) { | 59 JsFunction _buildJsConstructorForType(Type dartType) { |
59 var constructor = _polymerDart.callMethod('functionFactory'); | 60 var constructor = _polymerDart.callMethod('functionFactory'); |
60 var prototype = new JsObject(context['Object']); | 61 var prototype = new JsObject(context['Object']); |
61 | 62 |
62 var declarations = declarationsFor(dartType, jsProxyReflectable); | 63 var declarations = declarationsFor( |
64 dartType, jsProxyReflectable, where: (name, declaration) { | |
65 // Skip declarations from [BehaviorProxy] classes. These should not | |
66 // read/write from the dart class. | |
67 return !declaration.owner.metadata.any((m) => m is BehaviorProxy); | |
Siggi Cherem (dart-lang)
2015/08/13 18:52:41
should we have a type to declare things that don't
jakemac
2015/08/13 19:14:29
Nothing else that I know of today, I think we can
| |
68 }); | |
63 declarations.forEach((String name, DeclarationMirror declaration) { | 69 declarations.forEach((String name, DeclarationMirror declaration) { |
64 if (isProperty(declaration)) { | 70 if (isProperty(declaration)) { |
65 var descriptor = { | 71 var descriptor = { |
66 'get': _polymerDart.callMethod('propertyAccessorFactory', [ | 72 'get': _polymerDart.callMethod('propertyAccessorFactory', [ |
67 name, | 73 name, |
68 (dartInstance) { | 74 (dartInstance) { |
69 var mirror = jsProxyReflectable.reflect(dartInstance); | 75 var mirror = jsProxyReflectable.reflect(dartInstance); |
70 return jsValue(mirror.invokeGetter(name)); | 76 return jsValue(mirror.invokeGetter(name)); |
71 } | 77 } |
72 ]), | 78 ]), |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 {'configurable': false, 'enumerable': false, 'writeable': false,}); | 188 {'configurable': false, 'enumerable': false, 'writeable': false,}); |
183 // Don't want to jsify the instance, if its a map that will make turn it into | 189 // Don't want to jsify the instance, if its a map that will make turn it into |
184 // a JsObject. | 190 // a JsObject. |
185 details['value'] = dartInstance; | 191 details['value'] = dartInstance; |
186 context['Object'].callMethod( | 192 context['Object'].callMethod( |
187 'defineProperty', [jsObject, '__dartClass__', details]); | 193 'defineProperty', [jsObject, '__dartClass__', details]); |
188 } | 194 } |
189 | 195 |
190 /// Gets a reference to the original dart instance from a js proxy object. | 196 /// Gets a reference to the original dart instance from a js proxy object. |
191 dynamic _getDartInstance(JsObject jsObject) => jsObject['__dartClass__']; | 197 dynamic _getDartInstance(JsObject jsObject) => jsObject['__dartClass__']; |
OLD | NEW |