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

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: fix html.status 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
« no previous file with comments | « lib/html/dart2js/html_dart2js.dart ('k') | lib/html/src/Isolates.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/html/dartium/html_dartium.dart
diff --git a/lib/html/dartium/html_dartium.dart b/lib/html/dartium/html_dartium.dart
index b42ddb2958aaf72d5e13937e7c08d3c9ecafbc38..12fe18c1de8928f7aed92a2b13d189cabf2c11fe 100644
--- a/lib/html/dartium/html_dartium.dart
+++ b/lib/html/dartium/html_dartium.dart
@@ -40770,6 +40770,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) {
@@ -40795,28 +40801,51 @@ 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];
@@ -40832,17 +40861,11 @@ class _FunctionRegistry {
});
}
- String _add(Function f) {
- final id = 'func-ref-${_nextId++}';
- _registry[id] = f;
- return id;
- }
-
- get _sendPort() => _port.toSendPort();
+ get _sendPort => _port.toSendPort();
}
_FunctionRegistry __functionRegistry;
-get _functionRegistry() {
+get _functionRegistry {
if (__functionRegistry === null) __functionRegistry = new _FunctionRegistry();
return __functionRegistry;
}
@@ -40851,6 +40874,25 @@ _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);
}
@@ -40878,6 +40920,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';
}
}
@@ -40895,6 +40938,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.
@@ -40988,7 +41046,7 @@ class ReceivePortSync {
_portMap[_portId] = this;
}
- static int get _isolateId() {
+ static int get _isolateId {
// TODO(vsm): Make this coherent with existing isolate code.
if (_cachedIsolateId == null) {
_cachedIsolateId = _getNewIsolateId();
@@ -40998,7 +41056,7 @@ class ReceivePortSync {
static String _getListenerName(isolateId, portId) =>
'dart-port-$isolateId-$portId';
- String get _listenerName() => _getListenerName(_isolateId, _portId);
+ String get _listenerName => _getListenerName(_isolateId, _portId);
void receive(callback(var message)) {
_callback = callback;
« no previous file with comments | « lib/html/dart2js/html_dart2js.dart ('k') | lib/html/src/Isolates.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698