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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Bootstrap support for Dart scripts on the page as this script. 5 // Bootstrap support for Dart scripts on the page as this script.
6 if (navigator.webkitStartDart) { 6 if (navigator.webkitStartDart) {
7 if (!navigator.webkitStartDart()) { 7 if (!navigator.webkitStartDart()) {
8 document.body.innerHTML = 'This build has expired. Please download a new Da rtium at http://www.dartlang.org/dartium/index.html'; 8 document.body.innerHTML = 'This build has expired. Please download a new Da rtium at http://www.dartlang.org/dartium/index.html';
9 } 9 }
10 } else { 10 } else {
(...skipping 29 matching lines...) Expand all
40 function SendPortSync() { 40 function SendPortSync() {
41 } 41 }
42 42
43 function ReceivePortSync() { 43 function ReceivePortSync() {
44 this.id = ReceivePortSync.id++; 44 this.id = ReceivePortSync.id++;
45 ReceivePortSync.map[this.id] = this; 45 ReceivePortSync.map[this.id] = this;
46 } 46 }
47 47
48 (function() { 48 (function() {
49 function serialize(message) { 49 function serialize(message) {
50 if (message == null) { 50 var visited = [];
51 return null; // Convert undefined to null. 51 function checkedSerialization(obj, serializer) {
52 } else if (typeof(message) == 'string' || 52 // Implementation detail: for now use linear search.
53 typeof(message) == 'number' || 53 // Another option is expando, but it may prohibit
54 typeof(message) == 'boolean') { 54 // VM optimizations (like putting object into slow mode
55 return message; 55 // on property deletion.)
56 } else if (message instanceof Array) { 56 var id = visited.indexOf(obj);
57 var values = new Array(message.length); 57 if (id != -1) return [ 'ref', id ];
58 for (var i = 0; i < message.length; i++) { 58 var id = visited.length;
59 values[i] = serialize(message[i]); 59 visited.push(obj);
60 return serializer(id);
61 }
62
63 function doSerialize(message) {
64 if (message == null) {
65 return null; // Convert undefined to null.
66 } else if (typeof(message) == 'string' ||
67 typeof(message) == 'number' ||
68 typeof(message) == 'boolean') {
69 return message;
70 } else if (message instanceof Array) {
71 return checkedSerialization(message, function(id) {
72 var values = new Array(message.length);
73 for (var i = 0; i < message.length; i++) {
74 values[i] = doSerialize(message[i]);
75 }
76 return [ 'list', id, values ];
77 });
78 } else if (message instanceof LocalSendPortSync) {
79 return [ 'sendport', 'nativejs', message.receivePort.id ];
80 } else if (message instanceof DartSendPortSync) {
81 return [ 'sendport', 'dart', message.isolateId, message.portId ];
82 } else {
83 return checkedSerialization(message, function(id) {
84 var keys = Object.getOwnPropertyNames(message);
85 var values = new Array(keys.length);
86 for (var i = 0; i < keys.length; i++) {
87 values[i] = doSerialize(message[keys[i]]);
88 }
89 return [ 'map', id, keys, values ];
90 });
60 } 91 }
61 return [ 'list', message.length, values ];
62 } else if (message instanceof LocalSendPortSync) {
63 return [ 'sendport', 'nativejs', message.receivePort.id ];
64 } else if (message instanceof DartSendPortSync) {
65 return [ 'sendport', 'dart', message.isolateId, message.portId ];
66 } else {
67 var id = 0;
68 var keys = Object.getOwnPropertyNames(message);
69 var values = new Array(keys.length);
70 for (var i = 0; i < keys.length; i++) {
71 values[i] = serialize(message[keys[i]]);
72 }
73 return [ 'map', id, keys, values ];
74 } 92 }
93 return doSerialize(message);
75 } 94 }
76 95
77 function deserialize(message) { 96 function deserialize(message) {
78 return deserializeHelper(message); 97 return deserializeHelper(message);
79 } 98 }
80 99
81 function deserializeHelper(x) { 100 function deserializeHelper(x) {
82 if (x == null || 101 if (x == null ||
83 typeof(x) == 'string' || 102 typeof(x) == 'string' ||
84 typeof(x) == 'number' || 103 typeof(x) == 'number' ||
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 var result = null; 227 var result = null;
209 var listener = function (e) { 228 var listener = function (e) {
210 result = JSON.parse(e.data); 229 result = JSON.parse(e.data);
211 }; 230 };
212 window.addEventListener(source, listener, false); 231 window.addEventListener(source, listener, false);
213 dispatchEvent(target, [source, serialized]); 232 dispatchEvent(target, [source, serialized]);
214 window.removeEventListener(source, listener, false); 233 window.removeEventListener(source, listener, false);
215 return deserialize(result); 234 return deserialize(result);
216 } 235 }
217 })(); 236 })();
OLDNEW
« 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