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

Unified Diff: client/dart.js

Issue 10700101: First experimental steps towards a better story for JS interop. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments Created 8 years, 5 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 | « no previous file | lib/dom/templates/html/frog/html_frog.darttemplate » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/dart.js
diff --git a/client/dart.js b/client/dart.js
index a21644065ff6e26778c9b66836e11a280f19594b..a1eaa20bcbe64cd3fb0dffa3757ef56c88216c1a 100644
--- a/client/dart.js
+++ b/client/dart.js
@@ -33,3 +33,87 @@ if (navigator.webkitStartDart) {
}
}, false);
}
+
+// ---------------------------------------------------------------------------
+// Experimental support for JS interoperability
+// ---------------------------------------------------------------------------
+
+function SendPortSync(receivePort) {
+ this.receivePort = receivePort;
+}
+
+function ReceivePortSync() {
+ this.id = ReceivePortSync.id++;
+ ReceivePortSync.map[this.id] = this;
+}
+
+ReceivePortSync.id = 0;
+ReceivePortSync.map = {};
+
+ReceivePortSync.prototype.receive = function(callback) {
+ this.callback = callback;
+};
+
+ReceivePortSync.prototype.toSendPort = function() {
+ return new SendPortSync(this);
+};
+
+ReceivePortSync.prototype.close = function() {
+ delete ReceivePortSync.map[this.id];
+};
+
+// TODO(kasperl): Hide these serialization methods so they
+// do not clutter up the global scope.
+function _serialize(message) {
+ if (message == null ||
+ typeof(message) == 'string' ||
+ typeof(message) == 'number' ||
+ typeof(message) == 'boolean') {
+ return message;
+ } else if (message instanceof SendPortSync) {
+ return [ 'sendport', message.receivePort.id ];
+ } else {
+ var id = 0;
+ var keys = Object.getOwnPropertyNames(message);
+ var values = new Array(keys.length);
+ for (var i = 0; i < keys.length; i++) {
+ values[i] = message[keys[i]];
+ }
+ return [ 'map', id, keys, values ];
+ }
+}
+
+function _deserialize(message) {
+ return _deserializeHelper(message);
+}
+
+function _deserializeHelper(x) {
+ if (x == null ||
+ typeof(x) == 'string' ||
+ typeof(x) == 'number' ||
+ typeof(x) == 'boolean') {
+ return x;
+ }
+ switch (x[0]) {
+ case 'map': return _deserializeMap(x);
+ default: throw 'unimplemented';
+ }
+}
+
+function _deserializeMap(x) {
+ var result = { };
+ var id = x[1];
+ var keys = x[2];
+ var values = x[3];
+ for (var i = 0, length = keys.length; i < length; i++) {
+ var key = _deserializeHelper(keys[i]);
+ var value = _deserializeHelper(values[i]);
+ result[key] = value;
+ }
+ return result;
+}
+
+function registerPort(name, port) {
+ var stringified = JSON.stringify(_serialize(port));
+ window.localStorage['dart-port:' + name] = stringified;
+}
« no previous file with comments | « no previous file | lib/dom/templates/html/frog/html_frog.darttemplate » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698