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

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

Issue 10878010: Restore js interop code (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Restore js function passing code 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:
View side-by-side diff with in-line comments
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 d8d4b5d5df73a2732c4b9bf6aceb61ba835b1b18..b42ddb2958aaf72d5e13937e7c08d3c9ecafbc38 100644
--- a/lib/html/dartium/html_dartium.dart
+++ b/lib/html/dartium/html_dartium.dart
@@ -40742,10 +40742,296 @@ class _WebSocketFactoryProvider {
class _TextFactoryProvider {
factory Text(String data) => _document.$dom_createTextNode(data);
}
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Utils for device detection.
+ */
+class _Device {
+ /**
+ * Gets the browser's user agent. Using this function allows tests to inject
+ * the user agent.
+ * Returns the user agent.
+ */
+ static String get userAgent() => window.navigator.userAgent;
+
+ /**
+ * Determines if the current device is running Firefox.
+ */
+ static bool get isFirefox() => userAgent.contains("Firefox", 0);
+}
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+_serialize(var message) {
+ return new _JsSerializer().traverse(message);
+}
+
+class _JsSerializer extends _Serializer {
+
+ visitSendPortSync(SendPortSync x) {
+ if (x is _JsSendPortSync) return visitJsSendPortSync(x);
+ if (x is _LocalSendPortSync) return visitLocalSendPortSync(x);
+ if (x is _RemoteSendPortSync) return visitRemoteSendPortSync(x);
+ throw "Illegal underlying port $x";
+ }
+
+ visitJsSendPortSync(_JsSendPortSync x) {
+ return [ 'sendport', 'nativejs', x._id ];
+ }
+
+ visitLocalSendPortSync(_LocalSendPortSync x) {
+ return [ 'sendport', 'dart',
+ ReceivePortSync._isolateId, x._receivePort._portId ];
+ }
+
+ visitRemoteSendPortSync(_RemoteSendPortSync x) {
+ return [ 'sendport', 'dart',
+ x._receivePort._isolateId, x._receivePort._portId ];
+ }
+
+ visitObject(Object x) {
+ if (x is Function) return visitFunction(x);
+ // TODO: Handle DOM elements and proxy other objects.
+ throw "Unserializable object $x";
+ }
+
+ visitFunction(Function func) {
+ return [ 'funcref',
+ _makeFunctionRef(func), visitSendPortSync(_sendPort()), null ];
+ }
+}
+
+// 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 {
+ final ReceivePortSync _port;
+ int _nextId;
+ final Map<String, Function> _registry;
+
+ _FunctionRegistry() :
+ _port = new ReceivePortSync(),
+ _nextId = 0,
+ _registry = <Function>{} {
+ _port.receive((msg) {
+ final id = msg[0];
+ final args = msg[1];
+ final f = _registry[id];
+ switch (args.length) {
+ case 0: return f();
+ case 1: return f(args[0]);
+ case 2: return f(args[0], args[1]);
+ case 3: return f(args[0], args[1], args[2]);
+ case 4: return f(args[0], args[1], args[2], args[3]);
+ default: throw 'Unsupported number of arguments.';
+ }
+ });
+ }
+
+ String _add(Function f) {
+ final id = 'func-ref-${_nextId++}';
+ _registry[id] = f;
+ return id;
+ }
+
+ get _sendPort() => _port.toSendPort();
+}
+
+_FunctionRegistry __functionRegistry;
+get _functionRegistry() {
+ if (__functionRegistry === null) __functionRegistry = new _FunctionRegistry();
+ return __functionRegistry;
+}
+
+_makeFunctionRef(f) => _functionRegistry._add(f);
+_sendPort() => _functionRegistry._sendPort;
+/// End of function serialization implementation.
+
+_deserialize(var message) {
+ return new _JsDeserializer().deserialize(message);
+}
+
+class _JsDeserializer extends _Deserializer {
+
+ static final _UNSPECIFIED = const Object();
+
+ deserializeSendPort(List x) {
+ String tag = x[1];
+ switch (tag) {
+ case 'nativejs':
+ num id = x[2];
+ return new _JsSendPortSync(id);
+ case 'dart':
+ num isolateId = x[2];
+ num portId = x[3];
+ return ReceivePortSync._lookup(isolateId, portId);
+ default:
+ throw 'Illegal SendPortSync type: $tag';
+ }
+ }
+
+ deserializeObject(List x) {
+ String tag = x[0];
+ switch (tag) {
+ case 'funcref': return deserializeFunction(x);
+ default: throw 'Illegal object type: $x';
+ }
+ }
+
+ deserializeFunction(List x) {
+ var id = x[1];
+ SendPortSync port = deserializeSendPort(x[2]);
+ // TODO: Support varargs when there is support in the language.
+ return ([arg0 = _UNSPECIFIED, arg1 = _UNSPECIFIED,
+ arg2 = _UNSPECIFIED, arg3 = _UNSPECIFIED]) {
+ var args = [arg0, arg1, arg2, arg3];
+ var last = args.indexOf(_UNSPECIFIED);
+ if (last >= 0) args = args.getRange(0, last);
+ var message = [id, args];
+ return port.callSync(message);
+ };
+ }
+}
+
+// The receiver is JS.
+class _JsSendPortSync implements SendPortSync {
+
+ num _id;
+ _JsSendPortSync(this._id);
+
+ callSync(var message) {
+ var serialized = _serialize(message);
+ var result = _callPortSync(_id, serialized);
+ return _deserialize(result);
+ }
+
+}
+
+// TODO(vsm): Differentiate between Dart2Js and Dartium isolates.
+// The receiver is a different Dart isolate, compiled to JS.
+class _RemoteSendPortSync implements SendPortSync {
+
+ int _isolateId;
+ int _portId;
+ _RemoteSendPortSync(this._isolateId, this._portId);
+
+ callSync(var message) {
+ var serialized = _serialize(message);
+ var result = _call(_isolateId, _portId, serialized);
+ return _deserialize(result);
+ }
+
+ static _call(int isolateId, int portId, var message) {
+ var target = 'dart-port-$isolateId-$portId';
+ // TODO(vsm): Make this re-entrant.
+ // TODO(vsm): Set this up set once, on the first call.
+ var source = '$target-result';
+ var result = null;
+ var listener = (TextEvent e) {
+ result = JSON.parse(e.data);
+ };
+ window.on[source].add(listener);
+ _dispatchEvent(target, [source, message]);
+ window.on[source].remove(listener);
+ return result;
+ }
+}
+
+// The receiver is in the same Dart isolate, compiled to JS.
+class _LocalSendPortSync implements SendPortSync {
+
+ ReceivePortSync _receivePort;
+
+ _LocalSendPortSync._internal(this._receivePort);
+
+ callSync(var message) {
+ // TODO(vsm): Do a more efficient deep copy.
+ var copy = _deserialize(_serialize(message));
+ var result = _receivePort._callback(copy);
+ return _deserialize(_serialize(result));
+ }
+}
+
+// TODO(vsm): Move this to dart:isolate. This will take some
+// refactoring as there are dependences here on the DOM. Users
+// interact with this class (or interface if we change it) directly -
+// new ReceivePortSync. I think most of the DOM logic could be
+// delayed until the corresponding SendPort is registered on the
+// window.
+
+// A Dart ReceivePortSync (tagged 'dart' when serialized) is
+// identifiable / resolvable by the combination of its isolateid and
+// portid. When a corresponding SendPort is used within the same
+// isolate, the _portMap below can be used to obtain the
+// ReceivePortSync directly. Across isolates (or from JS), an
+// EventListener can be used to communicate with the port indirectly.
+class ReceivePortSync {
+
+ static Map<int, ReceivePortSync> _portMap;
+ static int _portIdCount;
+ static int _cachedIsolateId;
+
+ num _portId;
+ Function _callback;
+ EventListener _listener;
+
+ ReceivePortSync() {
+ if (_portIdCount == null) {
+ _portIdCount = 0;
+ _portMap = new Map<int, ReceivePortSync>();
+ }
+ _portId = _portIdCount++;
+ _portMap[_portId] = this;
+ }
+
+ static int get _isolateId() {
+ // TODO(vsm): Make this coherent with existing isolate code.
+ if (_cachedIsolateId == null) {
+ _cachedIsolateId = _getNewIsolateId();
+ }
+ return _cachedIsolateId;
+ }
+
+ static String _getListenerName(isolateId, portId) =>
+ 'dart-port-$isolateId-$portId';
+ String get _listenerName() => _getListenerName(_isolateId, _portId);
+
+ void receive(callback(var message)) {
+ _callback = callback;
+ if (_listener === null) {
+ _listener = (TextEvent e) {
+ var data = JSON.parse(e.data);
+ var replyTo = data[0];
+ var message = _deserialize(data[1]);
+ var result = _callback(message);
+ _dispatchEvent(replyTo, _serialize(result));
+ };
+ window.on[_listenerName].add(_listener);
+ }
+ }
+
+ void close() {
+ _portMap.remove(_portId);
+ if (_listener !== null) window.on[_listenerName].remove(_listener);
+ }
+
+ SendPortSync toSendPort() {
+ return new _LocalSendPortSync._internal(this);
+ }
+
+ static SendPortSync _lookup(int isolateId, int portId) {
+ if (isolateId == _isolateId) {
+ return _portMap[portId].toSendPort();
+ } else {
+ return new _RemoteSendPortSync(isolateId, portId);
+ }
+ }
+}
+
void _dispatchEvent(String receiver, var message) {
var event = document.$dom_createEvent('TextEvent');
event.initTextEvent(receiver, false, false, window, JSON.stringify(message));
@@ -40871,26 +41157,219 @@ void _completeMeasurementFutures() {
}
}
}
-// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
-/**
- * Utils for device detection.
- */
-class _Device {
- /**
- * Gets the browser's user agent. Using this function allows tests to inject
- * the user agent.
- * Returns the user agent.
- */
- static String get userAgent() => window.navigator.userAgent;
+// Patch file for the dart:isolate library.
+
+/********************************************************
+ Inserted from lib/isolate/serialization.dart
+ ********************************************************/
+
+class _MessageTraverserVisitedMap {
+
+ operator[](var object) => null;
+ void operator[]=(var object, var info) { }
+
+ void reset() { }
+ void cleanup() { }
+
+}
+
+/** Abstract visitor for dart objects that can be sent as isolate messages. */
+class _MessageTraverser {
+
+ _MessageTraverserVisitedMap _visited;
+ _MessageTraverser() : _visited = new _MessageTraverserVisitedMap();
+
+ /** Visitor's entry point. */
+ traverse(var x) {
+ if (isPrimitive(x)) return visitPrimitive(x);
+ _visited.reset();
+ var result;
+ try {
+ result = _dispatch(x);
+ } finally {
+ _visited.cleanup();
+ }
+ return result;
+ }
+
+ _dispatch(var x) {
+ if (isPrimitive(x)) return visitPrimitive(x);
+ if (x is List) return visitList(x);
+ if (x is Map) return visitMap(x);
+ if (x is SendPort) return visitSendPort(x);
+ if (x is SendPortSync) return visitSendPortSync(x);
+
+ // Overridable fallback.
+ return visitObject(x);
+ }
+
+ abstract visitPrimitive(x);
+ abstract visitList(List x);
+ abstract visitMap(Map x);
+ abstract visitSendPort(SendPort x);
+ abstract visitSendPortSync(SendPortSync x);
+
+ visitObject(Object x) {
+ // TODO(floitsch): make this a real exception. (which one)?
+ throw "Message serialization: Illegal value $x passed";
+ }
+
+ static bool isPrimitive(x) {
+ return (x === null) || (x is String) || (x is num) || (x is bool);
+ }
+}
+
+
+/** A visitor that recursively copies a message. */
+class _Copier extends _MessageTraverser {
+
+ visitPrimitive(x) => x;
+
+ List visitList(List list) {
+ List copy = _visited[list];
+ if (copy !== null) return copy;
+
+ int len = list.length;
+
+ // TODO(floitsch): we loose the generic type of the List.
+ copy = new List(len);
+ _visited[list] = copy;
+ for (int i = 0; i < len; i++) {
+ copy[i] = _dispatch(list[i]);
+ }
+ return copy;
+ }
+
+ Map visitMap(Map map) {
+ Map copy = _visited[map];
+ if (copy !== null) return copy;
+
+ // TODO(floitsch): we loose the generic type of the map.
+ copy = new Map();
+ _visited[map] = copy;
+ map.forEach((key, val) {
+ copy[_dispatch(key)] = _dispatch(val);
+ });
+ return copy;
+ }
- /**
- * Determines if the current device is running Firefox.
- */
- static bool get isFirefox() => userAgent.contains("Firefox", 0);
}
+
+/** Visitor that serializes a message as a JSON array. */
+class _Serializer extends _MessageTraverser {
+ int _nextFreeRefId = 0;
+
+ visitPrimitive(x) => x;
+
+ visitList(List list) {
+ int copyId = _visited[list];
+ if (copyId !== null) return ['ref', copyId];
+
+ int id = _nextFreeRefId++;
+ _visited[list] = id;
+ var jsArray = _serializeList(list);
+ // TODO(floitsch): we are losing the generic type.
+ return ['list', id, jsArray];
+ }
+
+ visitMap(Map map) {
+ int copyId = _visited[map];
+ if (copyId !== null) return ['ref', copyId];
+
+ int id = _nextFreeRefId++;
+ _visited[map] = id;
+ var keys = _serializeList(map.getKeys());
+ var values = _serializeList(map.getValues());
+ // TODO(floitsch): we are losing the generic type.
+ return ['map', id, keys, values];
+ }
+
+ _serializeList(List list) {
+ int len = list.length;
+ var result = new List(len);
+ for (int i = 0; i < len; i++) {
+ result[i] = _dispatch(list[i]);
+ }
+ return result;
+ }
+}
+
+/** Deserializes arrays created with [_Serializer]. */
+class _Deserializer {
+ Map<int, Dynamic> _deserialized;
+
+ _Deserializer();
+
+ static bool isPrimitive(x) {
+ return (x === null) || (x is String) || (x is num) || (x is bool);
+ }
+
+ deserialize(x) {
+ if (isPrimitive(x)) return x;
+ // TODO(floitsch): this should be new HashMap<int, var|Dynamic>()
+ _deserialized = new HashMap();
+ return _deserializeHelper(x);
+ }
+
+ _deserializeHelper(x) {
+ if (isPrimitive(x)) return x;
+ assert(x is List);
+ switch (x[0]) {
+ case 'ref': return _deserializeRef(x);
+ case 'list': return _deserializeList(x);
+ case 'map': return _deserializeMap(x);
+ case 'sendport': return deserializeSendPort(x);
+ default: return deserializeObject(x);
+ }
+ }
+
+ _deserializeRef(List x) {
+ int id = x[1];
+ var result = _deserialized[id];
+ assert(result !== null);
+ return result;
+ }
+
+ List _deserializeList(List x) {
+ int id = x[1];
+ // We rely on the fact that Dart-lists are directly mapped to Js-arrays.
+ List dartList = x[2];
+ _deserialized[id] = dartList;
+ int len = dartList.length;
+ for (int i = 0; i < len; i++) {
+ dartList[i] = _deserializeHelper(dartList[i]);
+ }
+ return dartList;
+ }
+
+ Map _deserializeMap(List x) {
+ Map result = new Map();
+ int id = x[1];
+ _deserialized[id] = result;
+ List keys = x[2];
+ List values = x[3];
+ int len = keys.length;
+ assert(len == values.length);
+ for (int i = 0; i < len; i++) {
+ var key = _deserializeHelper(keys[i]);
+ var value = _deserializeHelper(values[i]);
+ result[key] = value;
+ }
+ return result;
+ }
+
+ abstract deserializeSendPort(List x);
+
+ deserializeObject(List x) {
+ // TODO(floitsch): Use real exception (which one?).
+ throw "Unexpected serialized object";
+ }
+}
+
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
« 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