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

Unified Diff: lib/html/dartium/html_dartium.dart

Side-by-side diff isn't available for this file because of its large size.
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/dartium/html_dartium.dart
diff --git a/lib/html/dartium/html_dartium.dart b/lib/html/dartium/html_dartium.dart
index 6edcdfa5c3453ddaf7a0587c3c6302f581728fd0..1f74932f749f3f0b95ebaf31b74c1ba7eba03441 100644
--- a/lib/html/dartium/html_dartium.dart
+++ b/lib/html/dartium/html_dartium.dart
@@ -40740,6 +40740,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) {
@@ -40765,28 +40771,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];
@@ -40802,12 +40833,6 @@ class _FunctionRegistry {
});
}
- String _add(Function f) {
- final id = 'func-ref-${_nextId++}';
- _registry[id] = f;
- return id;
- }
-
get _sendPort() => _port.toSendPort();
}
@@ -40821,6 +40846,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);
}
@@ -40848,6 +40890,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';
}
}
@@ -40865,6 +40908,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