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

Unified Diff: client/dart.js

Issue 10693174: Support references in serialization. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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 | no next file » | 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 30b0883c644165b04529082cb0fd1f083f92b928..931f043744873013d992a8e80662061612e8fa43 100644
--- a/client/dart.js
+++ b/client/dart.js
@@ -47,31 +47,49 @@ function ReceivePortSync() {
(function() {
function serialize(message) {
- if (message == null) {
- return null; // Convert undefined to null.
- } else if (typeof(message) == 'string' ||
- typeof(message) == 'number' ||
- typeof(message) == 'boolean') {
- return message;
- } else if (message instanceof Array) {
- var values = new Array(message.length);
- for (var i = 0; i < message.length; i++) {
- values[i] = serialize(message[i]);
+ var visited = [];
+ function checkedSerialization(obj, serializer) {
+ for (var i = 0; i < visited.length; i++) {
+ if (visited[i] === obj) {
vsm 2012/07/13 14:10:40 An expando on obj to avoid the linear search?
Anton Muhin 2012/07/13 15:30:58 That's probably stupid, but for some reason I don'
vsm 2012/07/13 15:39:01 You may be right. From what I understand, deletin
Anton Muhin 2012/07/13 15:43:29 Good idea, done. On 2012/07/13 15:39:01, vsm wrot
+ return ['ref', i];
+ }
}
- return [ 'list', message.length, values ];
- } else if (message instanceof LocalSendPortSync) {
- return [ 'sendport', 'nativejs', message.receivePort.id ];
- } else if (message instanceof DartSendPortSync) {
- return [ 'sendport', 'dart', message.isolateId, message.portId ];
- } 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] = serialize(message[keys[i]]);
+ var id = visited.length;
+ visited.push(obj);
+ return serializer(id);
+ }
+
+ function do(message) {
podivilov 2012/07/13 11:07:55 doSerialize maybe?
Anton Muhin 2012/07/13 15:30:58 Done.
+ if (message == null) {
+ return null; // Convert undefined to null.
+ } else if (typeof(message) == 'string' ||
podivilov 2012/07/13 11:07:55 no else, please
Anton Muhin 2012/07/13 15:30:58 just keeping the way old code was structured.
+ typeof(message) == 'number' ||
podivilov 2012/07/13 11:07:55 wrong indentation
Anton Muhin 2012/07/13 15:30:58 Done.
+ typeof(message) == 'boolean') {
+ return message;
+ } else if (message instanceof Array) {
+ return checkedSerialization(message, function(id) {
podivilov 2012/07/13 11:07:55 up to you, but I think var index = visited.indexOf
Anton Muhin 2012/07/13 15:30:58 Absolutely, thanks, I should learn JS core lib. O
+ var values = new Array(message.length);
+ for (var i = 0; i < message.length; i++) {
+ values[i] = do(message[i]);
+ }
+ return [ 'list', id, values ];
+ });
+ } else if (message instanceof LocalSendPortSync) {
+ return [ 'sendport', 'nativejs', message.receivePort.id ];
+ } else if (message instanceof DartSendPortSync) {
+ return [ 'sendport', 'dart', message.isolateId, message.portId ];
+ } else {
+ return checkedSerialization(message, function(id) {
+ var keys = Object.getOwnPropertyNames(message);
+ var values = new Array(keys.length);
+ for (var i = 0; i < keys.length; i++) {
+ values[i] = do(message[keys[i]]);
+ }
+ return [ 'map', id, keys, values ];
+ });
}
- return [ 'map', id, keys, values ];
}
+ return do(message);
}
function deserialize(message) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698