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/html/dart2js/html_dart2js.dart

Issue 10827462: Support proxying of objects between JS and Dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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:
Download patch
Index: lib/html/dart2js/html_dart2js.dart
diff --git a/lib/html/dart2js/html_dart2js.dart b/lib/html/dart2js/html_dart2js.dart
index 8dea02e5bc2ef041a587e7f8ecb16ca6676b336b..1d1984fe503fdf9987891cd227a57d6f2a7b97e7 100644
--- a/lib/html/dart2js/html_dart2js.dart
+++ b/lib/html/dart2js/html_dart2js.dart
@@ -37098,6 +37098,12 @@ _serialize(var message) {
return new _JsSerializer().traverse(message);
}
+class JsProxy {
+ final int _id;
+
+ JsProxy._internal(this._id);
+}
+
class _JsSerializer extends _Serializer {
visitSendPortSync(SendPortSync x) {
@@ -37123,28 +37129,53 @@ class _JsSerializer extends _Serializer {
visitObject(Object x) {
if (x is Function) return visitFunction(x);
+ if (x is JsProxy) return visitJsProxy(x);
+
// TODO: Handle DOM elements and proxy other objects.
- throw "Unserializable object $x";
+ var proxyId = _makeDartProxyRef(x);
+ return [ 'objref', 'dart', proxyId ];
}
visitFunction(Function func) {
return [ 'funcref',
_makeFunctionRef(func), visitSendPortSync(_sendPort()), null ];
}
+
+ visitJsProxy(JsProxy proxy) {
+ return [ 'objref', 'nativejs', proxy._id ];
+ }
}
// Leaking implementation. Later will be backend specific and hopefully
// not leaking (at least in most of the cases.)
// TODO: provide better, backend specific implementation.
-class _FunctionRegistry {
+class _Registry<T> {
+ final String _name;
+ final int _nextId;
+ final Map<String, T> _registry;
+
+ _Registry(this._name) :
+ _nextId = 0,
+ _registry = <T>{};
+
+ String _add(T x) {
+ // TODO(vsm): Cache x and reuse id.
+ final id = '$_name-${_nextId++}';
+ _registry[id] = x;
+ return id;
+ }
+
+ T _get(String id) {
+ return _registry[id];
+ }
+}
+
+class _FunctionRegistry extends _Registry<Function> {
final ReceivePortSync _port;
- int _nextId;
- final Map<String, Function> _registry;
_FunctionRegistry() :
- _port = new ReceivePortSync(),
- _nextId = 0,
- _registry = <Function>{} {
+ super('func-ref'),
+ _port = new ReceivePortSync() {
_port.receive((msg) {
final id = msg[0];
final args = msg[1];
@@ -37160,12 +37191,6 @@ class _FunctionRegistry {
});
}
- String _add(Function f) {
- final id = 'func-ref-${_nextId++}';
- _registry[id] = f;
- return id;
- }
-
get _sendPort() => _port.toSendPort();
}
@@ -37179,6 +37204,23 @@ _makeFunctionRef(f) => _functionRegistry._add(f);
_sendPort() => _functionRegistry._sendPort;
/// End of function serialization implementation.
+/// Object proxy implementation.
+
+class _DartProxyRegistry extends _Registry<Object> {
+ _DartProxyRegistry() : super('dart-ref');
+}
+
+_DartProxyRegistry __dartProxyRegistry;
+get _dartProxyRegistry() {
+ if (__dartProxyRegistry === null) __dartProxyRegistry = new _DartProxyRegistry();
+ return __dartProxyRegistry;
+}
+
+_makeDartProxyRef(f) => _dartProxyRegistry._add(f);
+_getDartProxyObj(id) => _dartProxyRegistry._get(id);
+
+/// End of object proxy implementation.
+
_deserialize(var message) {
return new _JsDeserializer().deserialize(message);
}
@@ -37206,6 +37248,7 @@ class _JsDeserializer extends _Deserializer {
String tag = x[0];
switch (tag) {
case 'funcref': return deserializeFunction(x);
+ case 'objref': return deserializeProxy(x);
default: throw 'Illegal object type: $x';
}
}
@@ -37223,6 +37266,21 @@ class _JsDeserializer extends _Deserializer {
return port.callSync(message);
};
}
+
+ deserializeProxy(x) {
+ String tag = x[1];
+ switch (tag) {
+ case 'nativejs':
+ int id = x[2];
+ return new JsProxy._internal(id);
+ case 'dart':
+ int id = x[2];
+ // TODO(vsm): Check for isolate id. If the isolate isn't the
+ // current isolate, return a DartProxy.
+ return _getDartProxyObj(id);
+ default: throw 'Illegal proxy: $x';
+ }
+ }
}
// The receiver is JS.

Powered by Google App Engine
This is Rietveld 408576698